Skip to content

Commit c890527

Browse files
committed
use an installAround to install skip formatting
to avoid fragility of the install before and after approach
1 parent fc32d4d commit c890527

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import {install as installHTML, uninstall as uninstallHTML} from './paste-markdo
22
import {install as installImageLink, uninstall as uninstallImageLink} from './paste-markdown-image-link'
33
import {install as installLink, uninstall as uninstallLink} from './paste-markdown-link'
44
import {
5-
installBefore as installSkipFormatting,
6-
installAfter as installSkipFormattingPaste,
5+
installAround as installSkipFormatting,
76
uninstall as uninstallSkipFormatting
87
} from './paste-keyboard-shortcut-helper'
98
import {install as installTable, uninstall as uninstallTable} from './paste-markdown-table'
@@ -13,16 +12,8 @@ interface Subscription {
1312
unsubscribe: () => void
1413
}
1514

16-
// Inorder to intercept default paste behavior correctly using keyboard shorcuts, the
17-
// order of skip formatting handler functions here does matter.
1815
function subscribe(el: HTMLElement): Subscription {
19-
installSkipFormatting(el)
20-
installTable(el)
21-
installImageLink(el)
22-
installLink(el)
23-
installText(el)
24-
installHTML(el)
25-
installSkipFormattingPaste(el)
16+
installSkipFormatting(el, installTable, installImageLink, installLink, installText, installHTML)
2617

2718
return {
2819
unsubscribe: () => {

src/paste-keyboard-shortcut-helper.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ export function shouldSkipformatting(el: HTMLElement): boolean {
2121
return shouldSkipformattingState
2222
}
2323

24-
export function installBefore(el: HTMLElement): void {
24+
export function installAround(el: HTMLElement, ...installCallbacks: Array<(el: HTMLElement) => void>): void {
2525
el.addEventListener('keydown', setSkipFormattingFlag)
26-
}
2726

28-
export function installAfter(el: HTMLElement): void {
27+
for (const installCallback of installCallbacks) {
28+
installCallback(el)
29+
}
30+
2931
el.addEventListener('paste', unsetSkipFormattedFlag)
3032
}
3133

0 commit comments

Comments
 (0)