diff --git a/browser-extension/src/lib/enhancer.ts b/browser-extension/src/lib/enhancer.ts index 5eaaa76..1cb61c9 100644 --- a/browser-extension/src/lib/enhancer.ts +++ b/browser-extension/src/lib/enhancer.ts @@ -1,7 +1,7 @@ import type { OverTypeInstance } from '../overtype/overtype' /** - * stores enough info about the location of a draft to: + * Stores enough info about the location of a draft to: * - display it in a table * - reopen the draft in-context */ @@ -10,15 +10,23 @@ export interface CommentSpot { type: string } -/** wraps the textareas of a given platform with Gitcasso's enhancements */ +/** Wraps the textareas of a given platform with Gitcasso's enhancements. */ export interface CommentEnhancer { - /** guarantees to only return a type within this list */ + /** Guarantees to only return a type within this list. */ forSpotTypes(): string[] /** - * whenever a new `textarea` is added to any webpage, this method is called. - * if we return non-null, then we become the handler for that text area. + * Whenever a new `textarea` is added to any webpage, this method is called. + * If we return non-null, then we become the handler for that text area. */ - tryToEnhance(textarea: HTMLTextAreaElement): [OverTypeInstance, Spot] | null + tryToEnhance(textarea: HTMLTextAreaElement): Spot | null + /** This gets called the first time that `tryToEnhance` returns non-null. */ + prepareForFirstEnhancement(): void + /** + * If `tryToEnhance` returns non-null, then this gets called. + * It is guaranteed that `prepareForFirstEnhancement` has been called + * exactly once since pageload before this gets called. + */ + enhance(textarea: HTMLTextAreaElement, spot: Spot): OverTypeInstance tableIcon(spot: Spot): string tableTitle(spot: Spot): string diff --git a/browser-extension/src/lib/enhancers/github.ts b/browser-extension/src/lib/enhancers/github.ts index 58dcbeb..0c22980 100644 --- a/browser-extension/src/lib/enhancers/github.ts +++ b/browser-extension/src/lib/enhancers/github.ts @@ -29,7 +29,7 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer { } export class EnhancerRegistry { - private enhancers = new Set>() + private enhancers = new Set() + private preparedEnhancers = new Set() constructor() { // Register all available handlers @@ -21,12 +22,17 @@ export class EnhancerRegistry { this.enhancers.add(handler) } - tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea | null { + tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea | null { for (const enhancer of this.enhancers) { try { - const result = enhancer.tryToEnhance(textarea) - if (result) { - const [overtype, spot] = result + const spot = enhancer.tryToEnhance(textarea) + if (spot) { + // Prepare enhancer on first use + if (!this.preparedEnhancers.has(enhancer)) { + enhancer.prepareForFirstEnhancement() + this.preparedEnhancers.add(enhancer) + } + const overtype = enhancer.enhance(textarea, spot) return { enhancer, overtype, spot, textarea } } } catch (error) { @@ -42,7 +48,7 @@ export class EnhancerRegistry { } export class TextareaRegistry { - private textareas = new Map>() + private textareas = new Map() register(textareaInfo: EnhancedTextarea): void { this.textareas.set(textareaInfo.textarea, textareaInfo) @@ -56,7 +62,7 @@ export class TextareaRegistry { } } - get(textarea: HTMLTextAreaElement): EnhancedTextarea | undefined { + get(textarea: HTMLTextAreaElement): EnhancedTextarea | undefined { return this.textareas.get(textarea) } }