Skip to content

Commit cf3ada0

Browse files
committed
Re-enhance when submitted.
1 parent f8b5faa commit cf3ada0

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/lib/enhancers/github/GitHubIssueAppendEnhancer.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import OverType, { type OverTypeInstance } from 'overtype'
33
import type React from 'react'
44
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
55
import { logger } from '@/lib/logger'
6+
import { oncePerRefresh } from '@/lib/once-per-refresh'
67
import { modifyDOM } from '../modifyDOM'
78
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
89

@@ -63,14 +64,39 @@ export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueApp
6364
}
6465
}
6566

67+
instance: OverTypeInstance | undefined
68+
6669
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OverTypeInstance {
70+
this.registerSubmitHandler(textArea, _spot)
6771
prepareGitHubHighlighter()
6872
const overtypeContainer = modifyDOM(textArea)
69-
return new OverType(overtypeContainer, {
73+
if (this.instance) {
74+
OverType.instances.delete(overtypeContainer)
75+
;(overtypeContainer as any).overTypeInstance = undefined
76+
}
77+
const thing = new OverType(overtypeContainer, {
7078
...commonGitHubOptions,
7179
minHeight: '100px',
7280
placeholder: 'Use Markdown to format your comment',
7381
})[0]!
82+
this.instance = thing
83+
return thing
84+
}
85+
86+
private registerSubmitHandler(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot) {
87+
oncePerRefresh('gh-issue-append-events', () => {
88+
document.addEventListener('click', (e) => {
89+
const target = e.target
90+
if (!target) return false
91+
const btn = (e.target as HTMLElement).closest('button')
92+
if (!btn) return false
93+
if (btn.textContent.trim() === 'Comment' || btn.matches('button[data-variant="primary"]')) {
94+
this.enhance(textArea, _spot)
95+
return true
96+
}
97+
return false
98+
})
99+
})
74100
}
75101

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

0 commit comments

Comments
 (0)