Skip to content

Commit c8cab78

Browse files
author
Andrew L
committed
Support paste config
1 parent 6a2f6df commit c8cab78

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {OptionConfig, PASTE_AS_PLAIN_TEXT_ATTRIBUTE} from './option-config'
12
import {install as installHTML, uninstall as uninstallHTML} from './paste-markdown-html'
23
import {install as installImageLink, uninstall as uninstallImageLink} from './paste-markdown-image-link'
34
import {install as installLink, uninstall as uninstallLink} from './paste-markdown-link'
@@ -12,9 +13,10 @@ interface Subscription {
1213
unsubscribe: () => void
1314
}
1415

15-
function subscribe(el: HTMLElement): Subscription {
16-
installSkipFormatting(el, installTable, installImageLink, installLink, installText, installHTML)
16+
function subscribe(el: HTMLElement, optionConfig?: OptionConfig): Subscription {
17+
markElementWithConfigIfNeeded(el, optionConfig)
1718

19+
installSkipFormatting(el, installTable, installImageLink, installLink, installText, installHTML)
1820
return {
1921
unsubscribe: () => {
2022
uninstallSkipFormatting(el)
@@ -27,6 +29,18 @@ function subscribe(el: HTMLElement): Subscription {
2729
}
2830
}
2931

32+
function markElementWithConfigIfNeeded(el: HTMLElement, optionConfig?: OptionConfig) {
33+
// eslint-disable-next-line no-console
34+
console.log(optionConfig)
35+
36+
if (optionConfig?.pasteAsPlainText) {
37+
// eslint-disable-next-line no-console
38+
console.log(optionConfig?.pasteAsPlainText, 'HERE')
39+
40+
el.setAttribute(PASTE_AS_PLAIN_TEXT_ATTRIBUTE, 'true')
41+
}
42+
}
43+
3044
export {
3145
subscribe,
3246
installHTML,

src/option-config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const PASTE_AS_PLAIN_TEXT_ATTRIBUTE = 'data-paste-as-plain-text'
2+
3+
interface OptionConfig {
4+
pasteAsPlainText?: boolean
5+
}
6+
7+
export {PASTE_AS_PLAIN_TEXT_ATTRIBUTE, OptionConfig}

src/paste-markdown-link.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {PASTE_AS_PLAIN_TEXT_ATTRIBUTE} from './option-config'
12
import {insertText} from './text'
23
import {shouldSkipFormatting} from './paste-keyboard-shortcut-helper'
34

@@ -11,7 +12,13 @@ export function uninstall(el: HTMLElement): void {
1112

1213
function onPaste(event: ClipboardEvent) {
1314
const {currentTarget: el} = event
14-
if (shouldSkipFormatting(el as HTMLElement)) return
15+
const element = el as HTMLElement
16+
const shouldPastePlainText = element.hasAttribute(PASTE_AS_PLAIN_TEXT_ATTRIBUTE)
17+
const shouldSkipDefaultBehavior = shouldSkipFormatting(element)
18+
19+
if ((!shouldPastePlainText && shouldSkipDefaultBehavior) || (shouldPastePlainText && !shouldSkipDefaultBehavior)) {
20+
return
21+
}
1522

1623
const transfer = event.clipboardData
1724
if (!transfer || !hasPlainText(transfer)) return
@@ -26,6 +33,7 @@ function onPaste(event: ClipboardEvent) {
2633

2734
const selectedText = field.value.substring(field.selectionStart, field.selectionEnd)
2835
if (!selectedText.length) return
36+
2937
// Prevent linkification when replacing an URL
3038
// Trim whitespace in case whitespace is selected by mistake or by intention
3139
if (isURL(selectedText.trim())) return

test/test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ describe('paste-markdown', function () {
4343
assert.equal(textarea.value, 'The examples can be found [here](https://github.com).')
4444
})
4545

46+
it('turns pasted urls on selected text into markdown links if pasteAsPlainText is false', function () {
47+
subscription = subscribeWithOptionConfig(subscription, textarea, false)
48+
49+
// eslint-disable-next-line i18n-text/no-en
50+
textarea.value = 'The examples can be found here.'
51+
textarea.setSelectionRange(26, 30)
52+
paste(textarea, {'text/plain': 'https://github.com'})
53+
assert.equal(textarea.value, 'The examples can be found [here](https://github.com).')
54+
})
55+
56+
it('turns pasted urls on selected text into markdown links if pasteAsPlainText is true and skip format flag is true', function () {
57+
subscription = subscribeWithOptionConfig(subscription, textarea, true)
58+
59+
// eslint-disable-next-line i18n-text/no-en
60+
textarea.value = 'The examples can be found here.'
61+
textarea.setSelectionRange(26, 30)
62+
dispatchSkipFormattingKeyEvent(textarea)
63+
paste(textarea, {'text/plain': 'https://github.com'})
64+
assert.equal(textarea.value, 'The examples can be found [here](https://github.com).')
65+
})
66+
67+
it('pastes as plain text on selected text if pasteAsPlainText is true', function () {
68+
subscription = subscribeWithOptionConfig(subscription, textarea, true)
69+
70+
// eslint-disable-next-line i18n-text/no-en
71+
textarea.value = 'The examples can be found here.'
72+
textarea.setSelectionRange(26, 30)
73+
paste(textarea, {'text/plain': 'https://github.com'})
74+
// The text area will be unchanged at this stage as the paste won't be handled by our listener
75+
assert.equal(textarea.value, 'The examples can be found here.')
76+
})
77+
4678
it('creates a markdown link when the pasted url includes a trailing slash', function () {
4779
// eslint-disable-next-line i18n-text/no-en
4880
textarea.value = 'The examples can be found here.'
@@ -353,6 +385,12 @@ function dispatchSkipFormattingKeyEvent(textarea) {
353385
)
354386
}
355387

388+
function subscribeWithOptionConfig(subscription, textarea, pasteAsPlainText) {
389+
// Clear the before test subscription with no config and re-subscribe with config
390+
subscription.unsubscribe()
391+
return subscribe(textarea, {pasteAsPlainText})
392+
}
393+
356394
function paste(textarea, data) {
357395
const dataTransfer = new DataTransfer()
358396
for (const key in data) {

0 commit comments

Comments
 (0)