|
1 | | -const unformatted = new WeakMap<HTMLElement, boolean>() |
| 1 | +const skipformattingMap = new WeakMap<HTMLElement, boolean>() |
2 | 2 |
|
3 | | -function setUnformattedFlag(event: KeyboardEvent): void { |
| 3 | +function setSkipFormattingFlag(event: KeyboardEvent): void { |
4 | 4 | const {currentTarget: el} = event |
5 | | - if (event.code === 'KeyV' && (event.ctrlKey || event.metaKey) && event.shiftKey) { |
6 | | - unformatted.set(el as HTMLElement, true) |
| 5 | + const isSkipFormattingKeys = event.code === 'KeyV' && (event.ctrlKey || event.metaKey) && event.shiftKey |
| 6 | + |
| 7 | + // Supports Cmd+Shift+V (Chrome) / Cmd+Shift+Opt+V (Safari, Firefox and Edge) to mimic paste and match style shortcut on MacOS. |
| 8 | + if (isSkipFormattingKeys || (isSkipFormattingKeys && event.altKey)) { |
| 9 | + skipformattingMap.set(el as HTMLElement, true) |
7 | 10 | } |
8 | 11 | } |
9 | 12 |
|
10 | | -function unsetUnformattedFlag(event: ClipboardEvent): void { |
| 13 | +function unsetSkipFormattedFlag(event: ClipboardEvent): void { |
11 | 14 | const {currentTarget: el} = event |
12 | | - unformatted.delete(el as HTMLElement) |
| 15 | + skipformattingMap.delete(el as HTMLElement) |
13 | 16 | } |
14 | 17 |
|
15 | | -export function isUnformatted(el: HTMLElement): boolean { |
16 | | - const isUnformattedState = unformatted.get(el) ?? false |
| 18 | +export function shouldSkipformatting(el: HTMLElement): boolean { |
| 19 | + const shouldSkipformattingState = skipformattingMap.get(el) ?? false |
17 | 20 |
|
18 | | - return isUnformattedState |
| 21 | + return shouldSkipformattingState |
19 | 22 | } |
20 | 23 |
|
21 | 24 | export function installBefore(el: HTMLElement): void { |
22 | | - el.addEventListener('keydown', setUnformattedFlag) |
| 25 | + el.addEventListener('keydown', setSkipFormattingFlag) |
23 | 26 | } |
24 | 27 |
|
25 | 28 | export function installAfter(el: HTMLElement): void { |
26 | | - el.addEventListener('paste', unsetUnformattedFlag) |
| 29 | + el.addEventListener('paste', unsetSkipFormattedFlag) |
27 | 30 | } |
28 | 31 |
|
29 | 32 | export function uninstall(el: HTMLElement): void { |
30 | | - el.removeEventListener('keydown', setUnformattedFlag) |
31 | | - el.removeEventListener('paste', unsetUnformattedFlag) |
| 33 | + el.removeEventListener('keydown', setSkipFormattingFlag) |
| 34 | + el.removeEventListener('paste', unsetSkipFormattedFlag) |
32 | 35 | } |
0 commit comments