Skip to content

Commit f0df91f

Browse files
committed
Abandon the registerSubmitHandler, and rely only on reliable cleanup.
1 parent 9e46e00 commit f0df91f

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

src/lib/enhancers/github/GitHubIssueAppendEnhancer.tsx

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IssueOpenedIcon } from '@primer/octicons-react'
2-
import OverType, { type OverTypeInstance } from 'overtype'
2+
import OverType from 'overtype'
33
import type React from 'react'
44
import type {
55
CommentEnhancer,
@@ -8,7 +8,6 @@ import type {
88
StrippedLocation,
99
} from '@/lib/enhancer'
1010
import { logger } from '@/lib/logger'
11-
import { oncePerRefresh } from '@/lib/once-per-refresh'
1211
import { modifyDOM } from '../modifyDOM'
1312
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
1413

@@ -69,44 +68,22 @@ export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueApp
6968
}
7069
}
7170

72-
instance: OverTypeInstance | undefined
73-
7471
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OvertypeWithCleanup {
7572
prepareGitHubHighlighter()
7673
const overtypeContainer = modifyDOM(textArea)
77-
if (this.instance) {
78-
OverType.instances.delete(overtypeContainer)
79-
;(overtypeContainer as any).overTypeInstance = undefined
80-
}
81-
this.registerSubmitHandler(textArea, _spot)
82-
const thing = new OverType(overtypeContainer, {
74+
const instance = new OverType(overtypeContainer, {
8375
...commonGitHubOptions,
8476
minHeight: '100px',
8577
placeholder: 'Use Markdown to format your comment',
8678
})[0]!
87-
this.instance = thing
88-
return { instance: thing }
89-
}
90-
91-
private registerSubmitHandler(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot) {
92-
oncePerRefresh('gh-issue-append-events', () => {
93-
document.addEventListener('click', (e) => {
94-
const target = e.target
95-
if (target) {
96-
const btn = (e.target as HTMLElement).closest('button')
97-
if (btn) {
98-
if (
99-
btn.textContent.trim() === 'Comment' ||
100-
btn.matches('button[data-variant="primary"]')
101-
) {
102-
this.enhance(textArea, _spot)
103-
return true
104-
}
105-
}
106-
}
107-
return false
108-
})
109-
})
79+
const cleanup = () => {
80+
OverType.instances.delete(overtypeContainer)
81+
;(overtypeContainer as any).overTypeInstance = undefined
82+
}
83+
return {
84+
cleanup,
85+
instance,
86+
}
11087
}
11188

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

src/lib/registries.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export class TextareaRegistry {
133133
unregisterDueToModification(textarea: HTMLTextAreaElement): void {
134134
const enhanced = this.textareas.get(textarea)
135135
if (enhanced) {
136+
if (enhanced.cleanup) {
137+
enhanced.cleanup()
138+
}
136139
this.sendEvent('DESTROYED', enhanced)
137140
this.textareas.delete(textarea)
138141
}

0 commit comments

Comments
 (0)