Skip to content

Commit aa073ef

Browse files
author
ntwigg
committed
Add a TextAreaRegistry for tracking the textareas themselves at runtime.
1 parent 5a5d3a8 commit aa073ef

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

browser-extension/src/datamodel/handler-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class HandlerRegistry {
2929
try {
3030
const context = handler.identifyContextOf(textarea);
3131
if (context) {
32-
return { element: textarea, context };
32+
return { element: textarea, context, handler };
3333
}
3434
} catch (error) {
3535
console.warn('Handler failed to identify textarea:', error);

browser-extension/src/datamodel/textarea-handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface CommentContext {
77
export interface TextareaInfo<T extends CommentContext = CommentContext> {
88
element: HTMLTextAreaElement;
99
context: T;
10+
handler: TextareaHandler<T>;
1011
}
1112

1213
export interface TextareaHandler<T extends CommentContext = CommentContext> {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TextareaInfo, CommentContext } from './textarea-handler';
2+
3+
export class TextareaRegistry {
4+
private textareas = new Map<HTMLTextAreaElement, TextareaInfo<any>>();
5+
6+
register<T extends CommentContext>(textareaInfo: TextareaInfo<T>): void {
7+
this.textareas.set(textareaInfo.element, textareaInfo);
8+
}
9+
10+
get(textarea: HTMLTextAreaElement): TextareaInfo<any> | undefined {
11+
return this.textareas.get(textarea);
12+
}
13+
}

browser-extension/src/entrypoints/content.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { CONFIG } from './content/config'
22
import { logger } from './content/logger'
33
import { injectStyles } from './content/styles'
44
import { HandlerRegistry } from '../datamodel/handler-registry'
5+
import { TextareaRegistry } from '../datamodel/textarea-registry'
56

6-
const registry = new HandlerRegistry()
7+
const handlerRegistry = new HandlerRegistry()
8+
const textareaRegistry = new TextareaRegistry()
79

810
export default defineContentScript({
911
main() {
@@ -16,7 +18,7 @@ export default defineContentScript({
1618
childList: true,
1719
subtree: true,
1820
})
19-
logger.debug('Extension loaded with', registry.getAllHandlers().length, 'handlers')
21+
logger.debug('Extension loaded with', handlerRegistry.getAllHandlers().length, 'handlers')
2022
},
2123
matches: ['<all_urls>'],
2224
runAt: 'document_end',
@@ -49,10 +51,10 @@ function initializeMaybe(textarea: HTMLTextAreaElement) {
4951
textarea.classList.add(CONFIG.ADDED_OVERTYPE_CLASS)
5052

5153
// Use registry to identify and handle this specific textarea
52-
const textareaInfo = registry.identifyTextarea(textarea)
54+
const textareaInfo = handlerRegistry.identifyTextarea(textarea)
5355
if (textareaInfo) {
5456
logger.debug('Identified textarea:', textareaInfo.context.type, textareaInfo.context.unique_key)
55-
// TODO: Set up textarea monitoring and draft saving
57+
textareaRegistry.register(textareaInfo)
5658
} else {
5759
logger.debug('No handler found for textarea')
5860
}

0 commit comments

Comments
 (0)