Skip to content

Commit f702c57

Browse files
authored
Merge pull request #12 from diffplug/feat/once-per-page-initialization
refactor api to make per-page initialization a little easier
2 parents a7f4ea4 + fc9b1df commit f702c57

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

browser-extension/src/lib/enhancer.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { OverTypeInstance } from '../overtype/overtype'
22

33
/**
4-
* stores enough info about the location of a draft to:
4+
* Stores enough info about the location of a draft to:
55
* - display it in a table
66
* - reopen the draft in-context
77
*/
@@ -10,15 +10,23 @@ export interface CommentSpot {
1010
type: string
1111
}
1212

13-
/** wraps the textareas of a given platform with Gitcasso's enhancements */
13+
/** Wraps the textareas of a given platform with Gitcasso's enhancements. */
1414
export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
15-
/** guarantees to only return a type within this list */
15+
/** Guarantees to only return a type within this list. */
1616
forSpotTypes(): string[]
1717
/**
18-
* whenever a new `textarea` is added to any webpage, this method is called.
19-
* if we return non-null, then we become the handler for that text area.
18+
* Whenever a new `textarea` is added to any webpage, this method is called.
19+
* If we return non-null, then we become the handler for that text area.
2020
*/
21-
tryToEnhance(textarea: HTMLTextAreaElement): [OverTypeInstance, Spot] | null
21+
tryToEnhance(textarea: HTMLTextAreaElement): Spot | null
22+
/** This gets called the first time that `tryToEnhance` returns non-null. */
23+
prepareForFirstEnhancement(): void
24+
/**
25+
* If `tryToEnhance` returns non-null, then this gets called.
26+
* It is guaranteed that `prepareForFirstEnhancement` has been called
27+
* exactly once since pageload before this gets called.
28+
*/
29+
enhance(textarea: HTMLTextAreaElement, spot: Spot): OverTypeInstance
2230

2331
tableIcon(spot: Spot): string
2432
tableTitle(spot: Spot): string

browser-extension/src/lib/enhancers/github.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
2929
return [...GITHUB_SPOT_TYPES]
3030
}
3131

32-
tryToEnhance(textarea: HTMLTextAreaElement): [OverTypeInstance, GitHubAddCommentSpot] | null {
32+
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubAddCommentSpot | null {
3333
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
3434
if (window.location.hostname !== 'github.com') {
3535
return null
@@ -44,22 +44,22 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
4444
const [, owner, repo, numberStr] = match
4545
const slug = `${owner}/${repo}`
4646
const number = parseInt(numberStr!, 10)
47-
4847
const unique_key = `github.com:${slug}:${number}`
49-
50-
const spot: GitHubAddCommentSpot = {
48+
return {
5149
domain: 'github.com',
5250
number,
5351
slug,
5452
type: 'GH_PR_ADD_COMMENT',
5553
unique_key,
5654
}
57-
return [this.createOvertypeFor(textarea), spot]
5855
}
5956

60-
private createOvertypeFor(ghCommentBox: HTMLTextAreaElement): OverTypeInstance {
57+
prepareForFirstEnhancement(): void {
6158
OverType.setCodeHighlighter(hljsHighlighter)
62-
const overtypeContainer = this.modifyDOM(ghCommentBox)
59+
}
60+
61+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubAddCommentSpot): OverTypeInstance {
62+
const overtypeContainer = this.modifyDOM(textArea)
6363
return new OverType(overtypeContainer, {
6464
autoResize: true,
6565
minHeight: '102px',

browser-extension/src/lib/registries.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
1010
}
1111

1212
export class EnhancerRegistry {
13-
private enhancers = new Set<CommentEnhancer<any>>()
13+
private enhancers = new Set<CommentEnhancer>()
14+
private preparedEnhancers = new Set<CommentEnhancer>()
1415

1516
constructor() {
1617
// Register all available handlers
@@ -21,12 +22,17 @@ export class EnhancerRegistry {
2122
this.enhancers.add(handler)
2223
}
2324

24-
tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea<any> | null {
25+
tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea | null {
2526
for (const enhancer of this.enhancers) {
2627
try {
27-
const result = enhancer.tryToEnhance(textarea)
28-
if (result) {
29-
const [overtype, spot] = result
28+
const spot = enhancer.tryToEnhance(textarea)
29+
if (spot) {
30+
// Prepare enhancer on first use
31+
if (!this.preparedEnhancers.has(enhancer)) {
32+
enhancer.prepareForFirstEnhancement()
33+
this.preparedEnhancers.add(enhancer)
34+
}
35+
const overtype = enhancer.enhance(textarea, spot)
3036
return { enhancer, overtype, spot, textarea }
3137
}
3238
} catch (error) {
@@ -42,7 +48,7 @@ export class EnhancerRegistry {
4248
}
4349

4450
export class TextareaRegistry {
45-
private textareas = new Map<HTMLTextAreaElement, EnhancedTextarea<any>>()
51+
private textareas = new Map<HTMLTextAreaElement, EnhancedTextarea>()
4652

4753
register<T extends CommentSpot>(textareaInfo: EnhancedTextarea<T>): void {
4854
this.textareas.set(textareaInfo.textarea, textareaInfo)
@@ -56,7 +62,7 @@ export class TextareaRegistry {
5662
}
5763
}
5864

59-
get(textarea: HTMLTextAreaElement): EnhancedTextarea<any> | undefined {
65+
get(textarea: HTMLTextAreaElement): EnhancedTextarea | undefined {
6066
return this.textareas.get(textarea)
6167
}
6268
}

0 commit comments

Comments
 (0)