Skip to content

Commit b3f7730

Browse files
author
ntwigg
committed
Rename refactor.
1 parent 33a2aba commit b3f7730

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

browser-extension/src/datamodel/enhancer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* - display it in a table
44
* - reopen the draft in-context
55
*/
6-
export interface CommentContext {
6+
export interface CommentSpot {
77
unique_key: string
88
type: string
99
}
1010

1111
/** wraps the textareas of a given platform with Gitcasso's enhancements */
12-
export interface CommentEnhancer<T extends CommentContext = CommentContext> {
12+
export interface CommentEnhancer<T extends CommentSpot = CommentSpot> {
1313
/** guarantees to only return a type within this list */
1414
forCommentTypes(): string[]
1515
/**

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, CommentEnhancer } from '../enhancer'
1+
import type { CommentEnhancer, CommentSpot } from '../enhancer'
22

33
export type GitHubCommentType =
44
| 'GH_ISSUE_NEW'
@@ -9,7 +9,7 @@ export type GitHubCommentType =
99
| 'GH_PR_EDIT_COMMENT'
1010
| 'GH_PR_CODE_COMMENT'
1111

12-
export interface GitHubContext extends CommentContext {
12+
export interface GitHubContext extends CommentSpot {
1313
type: GitHubCommentType // Override to narrow from string to specific union
1414
domain: string
1515
slug: string // owner/repo

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

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

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

5-
export interface RedditContext extends CommentContext {
5+
export interface RedditContext extends CommentSpot {
66
type: RedditCommentType // Override to narrow from string to specific union
77
subreddit: string
88
postId?: string | undefined

browser-extension/src/datamodel/registries.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { CommentContext, CommentEnhancer } from './enhancer'
1+
import type { CommentEnhancer, CommentSpot } from './enhancer'
22
import { GitHubHandler as GitHubEnhancer } from './handlers/github-handler'
33
import { RedditHandler as RedditEnhancer } from './handlers/reddit-handler'
44

5-
export interface EnhancedTextarea<T extends CommentContext = CommentContext> {
5+
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
66
element: HTMLTextAreaElement
77
context: T
88
handler: CommentEnhancer<T>
@@ -17,7 +17,7 @@ export class EnhancerRegistry {
1717
this.register(new RedditEnhancer())
1818
}
1919

20-
private register<T extends CommentContext>(handler: CommentEnhancer<T>): void {
20+
private register<T extends CommentSpot>(handler: CommentEnhancer<T>): void {
2121
this.enhancers.add(handler)
2222
}
2323

@@ -56,7 +56,7 @@ export class EnhancerRegistry {
5656
export class TextareaRegistry {
5757
private textareas = new Map<HTMLTextAreaElement, EnhancedTextarea<any>>()
5858

59-
register<T extends CommentContext>(textareaInfo: EnhancedTextarea<T>): void {
59+
register<T extends CommentSpot>(textareaInfo: EnhancedTextarea<T>): void {
6060
this.textareas.set(textareaInfo.element, textareaInfo)
6161
// TODO: register as a draft in progress with the global list
6262
}

browser-extension/src/entrypoints/content.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ import { CONFIG } from '../common/config'
22
import { logger } from '../common/logger'
33
import { EnhancerRegistry, TextareaRegistry } from '../datamodel/registries'
44

5-
const handlerRegistry = new EnhancerRegistry()
6-
const textareaRegistry = new TextareaRegistry()
5+
const enhancers = new EnhancerRegistry()
6+
const enhancedTextareas = new TextareaRegistry()
77

88
export default defineContentScript({
99
main() {
1010
const textAreasOnPageLoad = document.querySelectorAll<HTMLTextAreaElement>(`textarea`)
1111
for (const textarea of textAreasOnPageLoad) {
12-
initializeMaybeIsPageload(textarea)
12+
enhanceMaybe(textarea)
1313
}
1414
const observer = new MutationObserver(handleMutations)
1515
observer.observe(document.body, {
1616
childList: true,
1717
subtree: true,
1818
})
19-
logger.debug('Extension loaded with', handlerRegistry.getAllHandlers().length, 'handlers')
19+
logger.debug('Extension loaded with', enhancers.getAllHandlers().length, 'handlers')
2020
},
2121
matches: ['<all_urls>'],
2222
runAt: 'document_end',
@@ -29,13 +29,13 @@ function handleMutations(mutations: MutationRecord[]): void {
2929
if (node.nodeType === Node.ELEMENT_NODE) {
3030
const element = node as Element
3131
if (element.tagName === 'TEXTAREA') {
32-
initializeMaybeIsPageload(element as HTMLTextAreaElement)
32+
enhanceMaybe(element as HTMLTextAreaElement)
3333
} else {
3434
// Also check for textareas within added subtrees
3535
const textareas = element.querySelectorAll?.('textarea')
3636
if (textareas) {
3737
for (const textarea of textareas) {
38-
initializeMaybeIsPageload(textarea)
38+
enhanceMaybe(textarea)
3939
}
4040
}
4141
}
@@ -47,13 +47,13 @@ function handleMutations(mutations: MutationRecord[]): void {
4747
if (node.nodeType === Node.ELEMENT_NODE) {
4848
const element = node as Element
4949
if (element.tagName === 'TEXTAREA') {
50-
textareaRegistry.unregisterDueToModification(element as HTMLTextAreaElement)
50+
enhancedTextareas.unregisterDueToModification(element as HTMLTextAreaElement)
5151
} else {
5252
// Also check for textareas within removed subtrees
5353
const textareas = element.querySelectorAll?.('textarea')
5454
if (textareas) {
5555
for (const textarea of textareas) {
56-
textareaRegistry.unregisterDueToModification(textarea)
56+
enhancedTextareas.unregisterDueToModification(textarea)
5757
}
5858
}
5959
}
@@ -62,9 +62,9 @@ function handleMutations(mutations: MutationRecord[]): void {
6262
}
6363
}
6464

65-
function initializeMaybeIsPageload(textarea: HTMLTextAreaElement) {
65+
function enhanceMaybe(textarea: HTMLTextAreaElement) {
6666
// Check if this textarea is already registered
67-
if (textareaRegistry.get(textarea)) {
67+
if (enhancedTextareas.get(textarea)) {
6868
logger.debug('textarea already registered {}', textarea)
6969
return
7070
}
@@ -73,10 +73,14 @@ function initializeMaybeIsPageload(textarea: HTMLTextAreaElement) {
7373
injectStyles()
7474

7575
// Use registry to identify and handle this specific textarea
76-
const textareaInfo = handlerRegistry.identifyTextarea(textarea)
77-
if (textareaInfo) {
78-
logger.debug('Identified textarea:', textareaInfo.context.type, textareaInfo.context.unique_key)
79-
textareaRegistry.register(textareaInfo)
76+
const enhancedTextarea = enhancers.identifyTextarea(textarea)
77+
if (enhancedTextarea) {
78+
logger.debug(
79+
'Identified textarea:',
80+
enhancedTextarea.context.type,
81+
enhancedTextarea.context.unique_key,
82+
)
83+
enhancedTextareas.register(enhancedTextarea)
8084
} else {
8185
logger.debug('No handler found for textarea')
8286
}

0 commit comments

Comments
 (0)