Skip to content

Commit 2655e2b

Browse files
committed
Same result with less code by doing cleanup on *every* overtype that gets destroyed (probably a good idea).
1 parent c20cbd3 commit 2655e2b

File tree

8 files changed

+42
-89
lines changed

8 files changed

+42
-89
lines changed

src/lib/enhancer.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,9 @@ 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): OvertypeWithCleanup
43+
enhance(textarea: HTMLTextAreaElement, spot: Spot): OverTypeInstance
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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1+
import type { OverTypeInstance } from 'overtype'
12
import type { ReactNode } from 'react'
2-
import type {
3-
CommentEnhancer,
4-
CommentSpot,
5-
OvertypeWithCleanup,
6-
StrippedLocation,
7-
} from '../enhancer'
3+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../enhancer'
84

95
/** Used when an entry is in the table which we don't recognize. */
106
export class CommentEnhancerMissing implements CommentEnhancer {
@@ -47,7 +43,7 @@ export class CommentEnhancerMissing implements CommentEnhancer {
4743
tryToEnhance(_textarea: HTMLTextAreaElement, _location: StrippedLocation): CommentSpot | null {
4844
throw new Error('Method not implemented.')
4945
}
50-
enhance(_textarea: HTMLTextAreaElement, _spot: CommentSpot): OvertypeWithCleanup {
46+
enhance(_textarea: HTMLTextAreaElement, _spot: CommentSpot): OverTypeInstance {
5147
throw new Error('Method not implemented.')
5248
}
5349
}

src/lib/enhancers/github/GitHubEditEnhancer.tsx

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

58-
enhance(textArea: HTMLTextAreaElement, spot: GitHubEditSpot): OvertypeWithCleanup {
53+
enhance(textArea: HTMLTextAreaElement, spot: GitHubEditSpot): OverTypeInstance {
5954
prepareGitHubHighlighter()
6055
const overtypeContainer = modifyDOM(textArea)
6156
const overtype = new OverType(overtypeContainer, {
@@ -65,7 +60,7 @@ export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
6560
if (!spot.isIssue) {
6661
// TODO: autoheight not working
6762
}
68-
return { instance: overtype }
63+
return overtype
6964
}
7065

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

src/lib/enhancers/github/GitHubIssueAppendEnhancer.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { IssueOpenedIcon } from '@primer/octicons-react'
2-
import OverType from 'overtype'
2+
import OverType, { type OverTypeInstance } from 'overtype'
33
import type React from 'react'
4-
import type {
5-
CommentEnhancer,
6-
CommentSpot,
7-
OvertypeWithCleanup,
8-
StrippedLocation,
9-
} from '@/lib/enhancer'
4+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
105
import { logger } from '@/lib/logger'
116
import { modifyDOM } from '../modifyDOM'
127
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
@@ -68,22 +63,14 @@ export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueApp
6863
}
6964
}
7065

71-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OvertypeWithCleanup {
66+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OverTypeInstance {
7267
prepareGitHubHighlighter()
7368
const overtypeContainer = modifyDOM(textArea)
74-
const instance = new OverType(overtypeContainer, {
69+
return new OverType(overtypeContainer, {
7570
...commonGitHubOptions,
7671
minHeight: '100px',
7772
placeholder: 'Use Markdown to format your comment',
7873
})[0]!
79-
const cleanup = () => {
80-
OverType.instances.delete(overtypeContainer)
81-
;(overtypeContainer as any).overTypeInstance = undefined
82-
}
83-
return {
84-
cleanup,
85-
instance,
86-
}
8774
}
8875

8976
tableUpperDecoration(spot: GitHubIssueAppendSpot): React.ReactNode {

src/lib/enhancers/github/GitHubIssueCreateEnhancer.tsx

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

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

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

src/lib/enhancers/github/GitHubPrAppendEnhancer.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import OverType from 'overtype'
1+
import OverType, { type OverTypeInstance } from 'overtype'
22
import type React from 'react'
3-
import type {
4-
CommentEnhancer,
5-
CommentSpot,
6-
OvertypeWithCleanup,
7-
StrippedLocation,
8-
} from '@/lib/enhancer'
3+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
94
import { logger } from '@/lib/logger'
105
import { modifyDOM } from '../modifyDOM'
116
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
@@ -58,24 +53,22 @@ export class GitHubPrAppendEnhancer implements CommentEnhancer<GitHubPrAppendSpo
5853
}
5954
}
6055

61-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrAppendSpot): OvertypeWithCleanup {
56+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrAppendSpot): OverTypeInstance {
6257
prepareGitHubHighlighter()
6358
const overtypeContainer = modifyDOM(textArea)
64-
const instance = new OverType(overtypeContainer, {
59+
const overtype = new OverType(overtypeContainer, {
6560
...commonGitHubOptions,
6661
minHeight: '102px',
6762
padding: 'var(--base-size-8)',
6863
placeholder: 'Add your comment here...',
6964
})[0]!
7065
const listenForEmpty = new MutationObserver(() => {
7166
if (textArea.value === '') {
72-
instance.updatePreview()
67+
overtype.updatePreview()
7368
}
7469
})
7570
listenForEmpty.observe(textArea, { attributes: true, characterData: true })
76-
return {
77-
instance,
78-
}
71+
return overtype
7972
}
8073

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

src/lib/enhancers/github/GitHubPrCreateEnhancer.tsx

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

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

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

src/lib/registries.ts

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

2524
export class EnhancerRegistry {
@@ -72,9 +71,9 @@ export class EnhancerRegistry {
7271
try {
7372
const spot = enhancer.tryToEnhance(textarea, location)
7473
if (spot) {
75-
const { instance: overtype, cleanup } = enhancer.enhance(textarea, spot)
74+
const overtype = enhancer.enhance(textarea, spot)
7675
this.handleDelayedValueInjection(overtype)
77-
return { enhancer, overtype, spot, textarea, ...(cleanup && { cleanup }) }
76+
return { enhancer, overtype, spot, textarea }
7877
}
7978
} catch (error) {
8079
console.warn('Handler failed to identify textarea:', error)
@@ -130,12 +129,15 @@ export class TextareaRegistry {
130129
this.sendEvent('ENHANCED', enhanced)
131130
}
132131

132+
private cleanupOvertype(overtype: OverTypeInstance) {
133+
const container = overtype.element
134+
OverType.instances.delete(container)
135+
;(container as any).overTypeInstance = undefined
136+
}
133137
unregisterDueToModification(textarea: HTMLTextAreaElement): void {
134138
const enhanced = this.textareas.get(textarea)
135139
if (enhanced) {
136-
if (enhanced.cleanup) {
137-
enhanced.cleanup()
138-
}
140+
this.cleanupOvertype(enhanced.overtype)
139141
this.sendEvent('DESTROYED', enhanced)
140142
this.textareas.delete(textarea)
141143
}

0 commit comments

Comments
 (0)