Skip to content

Commit 9e46e00

Browse files
committed
give enhancers a way to return an optional cleanup function
1 parent d78e3a8 commit 9e46e00

File tree

8 files changed

+85
-40
lines changed

8 files changed

+85
-40
lines changed

src/lib/enhancer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
4040
/**
4141
* If `tryToEnhance` returns non-null, then this gets called.
4242
*/
43-
enhance(textarea: HTMLTextAreaElement, spot: Spot): OverTypeInstance
43+
enhance(textarea: HTMLTextAreaElement, spot: Spot): OvertypeWithCleanup
4444
/** Returns a ReactNode which will be displayed in the table row. */
4545
tableUpperDecoration(spot: Spot): ReactNode
4646
/** The default title of a row */
4747
tableTitle(spot: Spot): string
4848
}
49+
50+
/** Allows enhancers to attach an optional cleanup field to the return value of `enhance()`. */
51+
export interface OvertypeWithCleanup {
52+
instance: OverTypeInstance
53+
cleanup?: () => void
54+
}

src/lib/enhancers/CommentEnhancerMissing.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import type { OverTypeInstance } from 'overtype'
21
import type { ReactNode } from 'react'
3-
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../enhancer'
2+
import type {
3+
CommentEnhancer,
4+
CommentSpot,
5+
OvertypeWithCleanup,
6+
StrippedLocation,
7+
} from '../enhancer'
48

59
/** Used when an entry is in the table which we don't recognize. */
610
export class CommentEnhancerMissing implements CommentEnhancer {
@@ -43,7 +47,7 @@ export class CommentEnhancerMissing implements CommentEnhancer {
4347
tryToEnhance(_textarea: HTMLTextAreaElement, _location: StrippedLocation): CommentSpot | null {
4448
throw new Error('Method not implemented.')
4549
}
46-
enhance(_textarea: HTMLTextAreaElement, _spot: CommentSpot): OverTypeInstance {
50+
enhance(_textarea: HTMLTextAreaElement, _spot: CommentSpot): OvertypeWithCleanup {
4751
throw new Error('Method not implemented.')
4852
}
4953
}

src/lib/enhancers/github/GitHubEditEnhancer.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import OverType, { type OverTypeInstance } from 'overtype'
1+
import OverType from 'overtype'
22
import type React from 'react'
3-
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
3+
import type {
4+
CommentEnhancer,
5+
CommentSpot,
6+
OvertypeWithCleanup,
7+
StrippedLocation,
8+
} from '@/lib/enhancer'
49
import { logger } from '@/lib/logger'
510
import { modifyDOM } from '../modifyDOM'
611
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
@@ -50,7 +55,7 @@ export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
5055
}
5156
}
5257

53-
enhance(textArea: HTMLTextAreaElement, spot: GitHubEditSpot): OverTypeInstance {
58+
enhance(textArea: HTMLTextAreaElement, spot: GitHubEditSpot): OvertypeWithCleanup {
5459
prepareGitHubHighlighter()
5560
const overtypeContainer = modifyDOM(textArea)
5661
const overtype = new OverType(overtypeContainer, {
@@ -60,7 +65,7 @@ export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
6065
if (!spot.isIssue) {
6166
// TODO: autoheight not working
6267
}
63-
return overtype
68+
return { instance: overtype }
6469
}
6570

6671
tableUpperDecoration(_spot: GitHubEditSpot): React.ReactNode {

src/lib/enhancers/github/GitHubIssueAppendEnhancer.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { IssueOpenedIcon } from '@primer/octicons-react'
22
import OverType, { type OverTypeInstance } from 'overtype'
33
import type React from 'react'
4-
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
4+
import type {
5+
CommentEnhancer,
6+
CommentSpot,
7+
OvertypeWithCleanup,
8+
StrippedLocation,
9+
} from '@/lib/enhancer'
510
import { logger } from '@/lib/logger'
611
import { oncePerRefresh } from '@/lib/once-per-refresh'
712
import { modifyDOM } from '../modifyDOM'
@@ -66,12 +71,12 @@ export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueApp
6671

6772
instance: OverTypeInstance | undefined
6873

69-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OverTypeInstance {
74+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OvertypeWithCleanup {
7075
prepareGitHubHighlighter()
7176
const overtypeContainer = modifyDOM(textArea)
7277
if (this.instance) {
7378
OverType.instances.delete(overtypeContainer)
74-
; (overtypeContainer as any).overTypeInstance = undefined
79+
;(overtypeContainer as any).overTypeInstance = undefined
7580
}
7681
this.registerSubmitHandler(textArea, _spot)
7782
const thing = new OverType(overtypeContainer, {
@@ -80,7 +85,7 @@ export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueApp
8085
placeholder: 'Use Markdown to format your comment',
8186
})[0]!
8287
this.instance = thing
83-
return thing
88+
return { instance: thing }
8489
}
8590

8691
private registerSubmitHandler(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot) {
@@ -90,7 +95,10 @@ export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueApp
9095
if (target) {
9196
const btn = (e.target as HTMLElement).closest('button')
9297
if (btn) {
93-
if (btn.textContent.trim() === 'Comment' || btn.matches('button[data-variant="primary"]')) {
98+
if (
99+
btn.textContent.trim() === 'Comment' ||
100+
btn.matches('button[data-variant="primary"]')
101+
) {
94102
this.enhance(textArea, _spot)
95103
return true
96104
}

src/lib/enhancers/github/GitHubIssueCreateEnhancer.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import OverType, { type OverTypeInstance } from 'overtype'
2-
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
1+
import OverType from 'overtype'
2+
import type {
3+
CommentEnhancer,
4+
CommentSpot,
5+
OvertypeWithCleanup,
6+
StrippedLocation,
7+
} from '../../enhancer'
38
import { logger } from '../../logger'
49
import { modifyDOM } from '../modifyDOM'
510
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
@@ -50,14 +55,16 @@ export class GitHubIssueCreateEnhancer implements CommentEnhancer<GitHubIssueCre
5055
}
5156
}
5257

53-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueCreateSpot): OverTypeInstance {
58+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueCreateSpot): OvertypeWithCleanup {
5459
prepareGitHubHighlighter()
5560
const overtypeContainer = modifyDOM(textArea)
56-
return new OverType(overtypeContainer, {
57-
...commonGitHubOptions,
58-
minHeight: '400px',
59-
placeholder: 'Type your description here...',
60-
})[0]!
61+
return {
62+
instance: new OverType(overtypeContainer, {
63+
...commonGitHubOptions,
64+
minHeight: '400px',
65+
placeholder: 'Type your description here...',
66+
})[0]!,
67+
}
6168
}
6269

6370
tableUpperDecoration(spot: GitHubIssueCreateSpot): React.ReactNode {

src/lib/enhancers/github/GitHubPrAppendEnhancer.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import OverType, { type OverTypeInstance } from 'overtype'
1+
import OverType from 'overtype'
22
import type React from 'react'
3-
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
3+
import type {
4+
CommentEnhancer,
5+
CommentSpot,
6+
OvertypeWithCleanup,
7+
StrippedLocation,
8+
} from '@/lib/enhancer'
49
import { logger } from '@/lib/logger'
510
import { modifyDOM } from '../modifyDOM'
611
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
@@ -53,15 +58,17 @@ export class GitHubPrAppendEnhancer implements CommentEnhancer<GitHubPrAppendSpo
5358
}
5459
}
5560

56-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrAppendSpot): OverTypeInstance {
61+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrAppendSpot): OvertypeWithCleanup {
5762
prepareGitHubHighlighter()
5863
const overtypeContainer = modifyDOM(textArea)
59-
return new OverType(overtypeContainer, {
60-
...commonGitHubOptions,
61-
minHeight: '102px',
62-
padding: 'var(--base-size-8)',
63-
placeholder: 'Add your comment here...',
64-
})[0]!
64+
return {
65+
instance: new OverType(overtypeContainer, {
66+
...commonGitHubOptions,
67+
minHeight: '102px',
68+
padding: 'var(--base-size-8)',
69+
placeholder: 'Add your comment here...',
70+
})[0]!,
71+
}
6572
}
6673

6774
tableUpperDecoration(spot: GitHubPrAppendSpot): React.ReactNode {

src/lib/enhancers/github/GitHubPrCreateEnhancer.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import OverType, { type OverTypeInstance } from 'overtype'
2-
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
1+
import OverType from 'overtype'
2+
import type {
3+
CommentEnhancer,
4+
CommentSpot,
5+
OvertypeWithCleanup,
6+
StrippedLocation,
7+
} from '../../enhancer'
38
import { logger } from '../../logger'
49
import { modifyDOM } from '../modifyDOM'
510
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
@@ -60,14 +65,16 @@ export class GitHubPrCreateEnhancer implements CommentEnhancer<GitHubPrCreateSpo
6065
}
6166
}
6267

63-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrCreateSpot): OverTypeInstance {
68+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrCreateSpot): OvertypeWithCleanup {
6469
prepareGitHubHighlighter()
6570
const overtypeContainer = modifyDOM(textArea)
66-
return new OverType(overtypeContainer, {
67-
...commonGitHubOptions,
68-
minHeight: '250px',
69-
placeholder: 'Type your description here...',
70-
})[0]!
71+
return {
72+
instance: new OverType(overtypeContainer, {
73+
...commonGitHubOptions,
74+
minHeight: '250px',
75+
placeholder: 'Type your description here...',
76+
})[0]!,
77+
}
7178
}
7279

7380
tableUpperDecoration(spot: GitHubPrCreateSpot): React.ReactNode {

src/lib/registries.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
1919
spot: T
2020
enhancer: CommentEnhancer<T>
2121
overtype: OverTypeInstance
22+
cleanup?: () => void
2223
}
2324

2425
export class EnhancerRegistry {
@@ -71,9 +72,9 @@ export class EnhancerRegistry {
7172
try {
7273
const spot = enhancer.tryToEnhance(textarea, location)
7374
if (spot) {
74-
const overtype = enhancer.enhance(textarea, spot)
75+
const { instance: overtype, cleanup } = enhancer.enhance(textarea, spot)
7576
this.handleDelayedValueInjection(overtype)
76-
return { enhancer, overtype, spot, textarea }
77+
return { enhancer, overtype, spot, textarea, ...(cleanup && { cleanup }) }
7778
}
7879
} catch (error) {
7980
console.warn('Handler failed to identify textarea:', error)

0 commit comments

Comments
 (0)