Skip to content

Commit e484541

Browse files
author
ntwigg
committed
Rename
1 parent 7d845f5 commit e484541

File tree

8 files changed

+104
-98
lines changed

8 files changed

+104
-98
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* stores enough info about the location of a draft to:
3+
* - display it in a table
4+
* - reopen the draft in-context
5+
*/
6+
export interface CommentContext {
7+
unique_key: string
8+
type: string
9+
}
10+
11+
/** wraps the textareas of a given platform with Gitcasso's enhancements */
12+
export interface CommentEnhancer<T extends CommentContext = CommentContext> {
13+
/** guarantees to only return a type within this list */
14+
forCommentTypes(): string[]
15+
/**
16+
* whenever a new `textarea` is added to any webpage, this method is called.
17+
* if we return non-null, then we become the handler for that text area.
18+
*/
19+
identifyContextOf(textarea: HTMLTextAreaElement): T | null
20+
21+
generateIcon(context: T): string
22+
generateDisplayTitle(context: T): string
23+
buildUrl(context: T, withDraft?: boolean): string
24+
}

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

Lines changed: 0 additions & 48 deletions
This file was deleted.

browser-extension/src/handlers/github-handler.ts renamed to browser-extension/src/datamodel/handlers/github-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CommentContext, TextareaHandler } from '../datamodel/textarea-handler'
1+
import type { CommentContext, CommentEnhancer } from '../enhancer'
22

33
export type GitHubCommentType =
44
| 'GH_ISSUE_NEW'
@@ -17,7 +17,7 @@ export interface GitHubContext extends CommentContext {
1717
commentId?: string | undefined // for editing existing comments
1818
}
1919

20-
export class GitHubHandler implements TextareaHandler<GitHubContext> {
20+
export class GitHubHandler implements CommentEnhancer<GitHubContext> {
2121
forCommentTypes(): string[] {
2222
return [
2323
'GH_ISSUE_NEW',

browser-extension/src/handlers/reddit-handler.ts renamed to browser-extension/src/datamodel/handlers/reddit-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CommentContext, TextareaHandler } from '../datamodel/textarea-handler'
1+
import type { CommentContext, CommentEnhancer } from '../enhancer'
22

33
export type RedditCommentType = 'REDDIT_POST_NEW' | 'REDDIT_COMMENT_NEW' | 'REDDIT_COMMENT_EDIT'
44

@@ -9,7 +9,7 @@ export interface RedditContext extends CommentContext {
99
commentId?: string | undefined // for editing existing comments
1010
}
1111

12-
export class RedditHandler implements TextareaHandler<RedditContext> {
12+
export class RedditHandler implements CommentEnhancer<RedditContext> {
1313
forCommentTypes(): string[] {
1414
return ['REDDIT_POST_NEW', 'REDDIT_COMMENT_NEW', 'REDDIT_COMMENT_EDIT']
1515
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type { CommentContext, CommentEnhancer } from './enhancer'
2+
import { GitHubHandler as GitHubEnhancer } from './handlers/github-handler'
3+
import { RedditHandler as RedditEnhancer } from './handlers/reddit-handler'
4+
5+
export interface EnhancedTextarea<T extends CommentContext = CommentContext> {
6+
element: HTMLTextAreaElement
7+
context: T
8+
handler: CommentEnhancer<T>
9+
}
10+
11+
export class EnhancerRegistry {
12+
private enhancers = new Set<CommentEnhancer<any>>()
13+
14+
constructor() {
15+
// Register all available handlers
16+
this.register(new GitHubEnhancer())
17+
this.register(new RedditEnhancer())
18+
}
19+
20+
private register<T extends CommentContext>(handler: CommentEnhancer<T>): void {
21+
this.enhancers.add(handler)
22+
}
23+
24+
getHandlerForType(type: string): CommentEnhancer<any> | null {
25+
for (const handler of this.enhancers) {
26+
if (handler.forCommentTypes().includes(type)) {
27+
return handler
28+
}
29+
}
30+
return null
31+
}
32+
33+
identifyTextarea(textarea: HTMLTextAreaElement): EnhancedTextarea<any> | null {
34+
for (const handler of this.enhancers) {
35+
try {
36+
const context = handler.identifyContextOf(textarea)
37+
if (context) {
38+
return { context, element: textarea, handler }
39+
}
40+
} catch (error) {
41+
console.warn('Handler failed to identify textarea:', error)
42+
}
43+
}
44+
return null
45+
}
46+
47+
getAllHandlers(): CommentEnhancer<any>[] {
48+
return Array.from(this.enhancers)
49+
}
50+
51+
getCommentTypesForHandler(handler: CommentEnhancer<any>): string[] {
52+
return handler.forCommentTypes()
53+
}
54+
}
55+
56+
export class TextareaRegistry {
57+
private textareas = new Map<HTMLTextAreaElement, EnhancedTextarea<any>>()
58+
59+
register<T extends CommentContext>(textareaInfo: EnhancedTextarea<T>): void {
60+
this.textareas.set(textareaInfo.element, textareaInfo)
61+
// TODO: register as a draft in progress with the global list
62+
}
63+
64+
unregisterDueToModification(textarea: HTMLTextAreaElement): void {
65+
if (this.textareas.has(textarea)) {
66+
// TODO: register as abandoned or maybe submitted with the global list
67+
this.textareas.delete(textarea)
68+
}
69+
}
70+
71+
get(textarea: HTMLTextAreaElement): EnhancedTextarea<any> | undefined {
72+
return this.textareas.get(textarea)
73+
}
74+
}

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

browser-extension/src/entrypoints/content.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { HandlerRegistry } from '../datamodel/handler-registry'
2-
import { TextareaRegistry } from '../datamodel/textarea-registry'
1+
import { EnhancerRegistry, TextareaRegistry } from '../datamodel/registries'
32
import { logger } from './content/logger'
43
import { injectStyles } from './content/styles'
54

6-
const handlerRegistry = new HandlerRegistry()
5+
const handlerRegistry = new EnhancerRegistry()
76
const textareaRegistry = new TextareaRegistry()
87

98
export default defineContentScript({

0 commit comments

Comments
 (0)