Skip to content

Commit fc32d4d

Browse files
rename and add support for macOS
1 parent d28e5d1 commit fc32d4d

File tree

7 files changed

+36
-31
lines changed

7 files changed

+36
-31
lines changed

src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import {install as installHTML, uninstall as uninstallHTML} from './paste-markdown-html'
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'
4-
import {install as installTable, uninstall as uninstallTable} from './paste-markdown-table'
5-
import {install as installText, uninstall as uninstallText} from './paste-markdown-text'
64
import {
7-
installBefore as installUnformatted,
8-
installAfter as installUnformattedAfter,
9-
uninstall as uninstallUnformatted
5+
installBefore as installSkipFormatting,
6+
installAfter as installSkipFormattingPaste,
7+
uninstall as uninstallSkipFormatting
108
} from './paste-keyboard-shortcut-helper'
9+
import {install as installTable, uninstall as uninstallTable} from './paste-markdown-table'
10+
import {install as installText, uninstall as uninstallText} from './paste-markdown-text'
1111

1212
interface Subscription {
1313
unsubscribe: () => void
1414
}
1515

16+
// Inorder to intercept default paste behavior correctly using keyboard shorcuts, the
17+
// order of skip formatting handler functions here does matter.
1618
function subscribe(el: HTMLElement): Subscription {
17-
installUnformatted(el)
19+
installSkipFormatting(el)
1820
installTable(el)
1921
installImageLink(el)
2022
installLink(el)
2123
installText(el)
2224
installHTML(el)
23-
installUnformattedAfter(el)
25+
installSkipFormattingPaste(el)
2426

2527
return {
2628
unsubscribe: () => {
27-
uninstallUnformatted(el)
29+
uninstallSkipFormatting(el)
2830
uninstallTable(el)
2931
uninstallHTML(el)
3032
uninstallImageLink(el)
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
const unformatted = new WeakMap<HTMLElement, boolean>()
1+
const skipformattingMap = new WeakMap<HTMLElement, boolean>()
22

3-
function setUnformattedFlag(event: KeyboardEvent): void {
3+
function setSkipFormattingFlag(event: KeyboardEvent): void {
44
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)
710
}
811
}
912

10-
function unsetUnformattedFlag(event: ClipboardEvent): void {
13+
function unsetSkipFormattedFlag(event: ClipboardEvent): void {
1114
const {currentTarget: el} = event
12-
unformatted.delete(el as HTMLElement)
15+
skipformattingMap.delete(el as HTMLElement)
1316
}
1417

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
1720

18-
return isUnformattedState
21+
return shouldSkipformattingState
1922
}
2023

2124
export function installBefore(el: HTMLElement): void {
22-
el.addEventListener('keydown', setUnformattedFlag)
25+
el.addEventListener('keydown', setSkipFormattingFlag)
2326
}
2427

2528
export function installAfter(el: HTMLElement): void {
26-
el.addEventListener('paste', unsetUnformattedFlag)
29+
el.addEventListener('paste', unsetSkipFormattedFlag)
2730
}
2831

2932
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)
3235
}

src/paste-markdown-html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {insertText} from './text'
2-
import {isUnformatted} from './paste-keyboard-shortcut-helper'
2+
import {shouldSkipformatting} from './paste-keyboard-shortcut-helper'
33

44
export function install(el: HTMLElement): void {
55
el.addEventListener('paste', onPaste)
@@ -12,7 +12,7 @@ export function uninstall(el: HTMLElement): void {
1212
function onPaste(event: ClipboardEvent) {
1313
const transfer = event.clipboardData
1414
const {currentTarget: el} = event
15-
if (isUnformatted(el as HTMLElement)) return
15+
if (shouldSkipformatting(el as HTMLElement)) return
1616
// if there is no clipboard data, or
1717
// if there is no html content in the clipboard, return
1818
if (!transfer || !hasHTML(transfer)) return

src/paste-markdown-image-link.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow strict */
22
import {insertText} from './text'
3-
import {isUnformatted} from './paste-keyboard-shortcut-helper'
3+
import {shouldSkipformatting} from './paste-keyboard-shortcut-helper'
44

55
export function install(el: HTMLElement): void {
66
el.addEventListener('dragover', onDragover)
@@ -39,7 +39,7 @@ function onDragover(event: DragEvent) {
3939

4040
function onPaste(event: ClipboardEvent) {
4141
const {currentTarget: el} = event
42-
if (isUnformatted(el as HTMLElement)) return
42+
if (shouldSkipformatting(el as HTMLElement)) return
4343

4444
const transfer = event.clipboardData
4545
if (!transfer || !hasLink(transfer)) return

src/paste-markdown-link.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {insertText} from './text'
2-
import {isUnformatted} from './paste-keyboard-shortcut-helper'
2+
import {shouldSkipformatting} from './paste-keyboard-shortcut-helper'
33

44
export function install(el: HTMLElement): void {
55
el.addEventListener('paste', onPaste)
@@ -11,7 +11,7 @@ export function uninstall(el: HTMLElement): void {
1111

1212
function onPaste(event: ClipboardEvent) {
1313
const {currentTarget: el} = event
14-
if (isUnformatted(el as HTMLElement)) return
14+
if (shouldSkipformatting(el as HTMLElement)) return
1515

1616
const transfer = event.clipboardData
1717
if (!transfer || !hasPlainText(transfer)) return

src/paste-markdown-table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {insertText} from './text'
2-
import {isUnformatted} from './paste-keyboard-shortcut-helper'
2+
import {shouldSkipformatting} from './paste-keyboard-shortcut-helper'
33

44
export function install(el: HTMLElement): void {
55
el.addEventListener('dragover', onDragover)
@@ -37,7 +37,7 @@ function onDragover(event: DragEvent) {
3737

3838
function onPaste(event: ClipboardEvent) {
3939
const {currentTarget: el} = event
40-
if (isUnformatted(el as HTMLElement)) return
40+
if (shouldSkipformatting(el as HTMLElement)) return
4141

4242
if (!event.clipboardData) return
4343

src/paste-markdown-text.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {insertText} from './text'
2-
import {isUnformatted} from './paste-keyboard-shortcut-helper'
2+
import {shouldSkipformatting} from './paste-keyboard-shortcut-helper'
33

44
export function install(el: HTMLElement): void {
55
el.addEventListener('paste', onPaste)
@@ -11,7 +11,7 @@ export function uninstall(el: HTMLElement): void {
1111

1212
function onPaste(event: ClipboardEvent) {
1313
const {currentTarget: el} = event
14-
if (isUnformatted(el as HTMLElement)) return
14+
if (shouldSkipformatting(el as HTMLElement)) return
1515

1616
const transfer = event.clipboardData
1717
if (!transfer || !hasMarkdown(transfer)) return

0 commit comments

Comments
 (0)