Skip to content

Commit 843ae03

Browse files
authored
handle multiple setup/teardown on a single page, also handle textarea reuse (#91 closes #76)
2 parents 27ba43e + 2655e2b commit 843ae03

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/lib/enhancers/github/GitHubPrAppendEnhancer.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ export class GitHubPrAppendEnhancer implements CommentEnhancer<GitHubPrAppendSpo
5656
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrAppendSpot): OverTypeInstance {
5757
prepareGitHubHighlighter()
5858
const overtypeContainer = modifyDOM(textArea)
59-
return new OverType(overtypeContainer, {
59+
const overtype = new OverType(overtypeContainer, {
6060
...commonGitHubOptions,
6161
minHeight: '102px',
6262
padding: 'var(--base-size-8)',
6363
placeholder: 'Add your comment here...',
6464
})[0]!
65+
const listenForEmpty = new MutationObserver(() => {
66+
if (textArea.value === '') {
67+
overtype.updatePreview()
68+
}
69+
})
70+
listenForEmpty.observe(textArea, { attributes: true, characterData: true })
71+
return overtype
6572
}
6673

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

src/lib/registries.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,15 @@ export class TextareaRegistry {
129129
this.sendEvent('ENHANCED', enhanced)
130130
}
131131

132+
private cleanupOvertype(overtype: OverTypeInstance) {
133+
const container = overtype.element
134+
OverType.instances.delete(container)
135+
;(container as any).overTypeInstance = undefined
136+
}
132137
unregisterDueToModification(textarea: HTMLTextAreaElement): void {
133138
const enhanced = this.textareas.get(textarea)
134139
if (enhanced) {
140+
this.cleanupOvertype(enhanced.overtype)
135141
this.sendEvent('DESTROYED', enhanced)
136142
this.textareas.delete(textarea)
137143
}

0 commit comments

Comments
 (0)