From 26613065ba4e242eaad5d3bffef0022a8ec06bf3 Mon Sep 17 00:00:00 2001 From: Andrew L Date: Tue, 20 Sep 2022 13:21:27 +0200 Subject: [PATCH] Make config flag more specific --- src/option-config.ts | 2 +- src/paste-markdown-link.ts | 11 +++++++---- test/test.js | 10 +++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/option-config.ts b/src/option-config.ts index d4d727a..0e91bfd 100644 --- a/src/option-config.ts +++ b/src/option-config.ts @@ -1,3 +1,3 @@ export interface OptionConfig { - pasteAsPlainText?: boolean + pasteLinkAsPlainTextOverSelectedText?: boolean } diff --git a/src/paste-markdown-link.ts b/src/paste-markdown-link.ts index 1ded924..1e4e158 100644 --- a/src/paste-markdown-link.ts +++ b/src/paste-markdown-link.ts @@ -2,10 +2,10 @@ import {OptionConfig} from './option-config' import {insertText} from './text' import {shouldSkipFormatting} from './paste-keyboard-shortcut-helper' -const pasteAsPlainTextMap = new WeakMap() +const pasteLinkAsPlainTextOverSelectedTextMap = new WeakMap() export function install(el: HTMLElement, optionConfig?: OptionConfig): void { - pasteAsPlainTextMap.set(el, optionConfig?.pasteAsPlainText === true) + pasteLinkAsPlainTextOverSelectedTextMap.set(el, optionConfig?.pasteLinkAsPlainTextOverSelectedText === true) el.addEventListener('paste', onPaste) } @@ -16,10 +16,13 @@ export function uninstall(el: HTMLElement): void { function onPaste(event: ClipboardEvent) { const {currentTarget: el} = event const element = el as HTMLElement - const shouldPastePlainText = pasteAsPlainTextMap.get(element) ?? false + const shouldPasteAsPlainText = pasteLinkAsPlainTextOverSelectedTextMap.get(element) ?? false const shouldSkipDefaultBehavior = shouldSkipFormatting(element) - if ((!shouldPastePlainText && shouldSkipDefaultBehavior) || (shouldPastePlainText && !shouldSkipDefaultBehavior)) { + if ( + (!shouldPasteAsPlainText && shouldSkipDefaultBehavior) || + (shouldPasteAsPlainText && !shouldSkipDefaultBehavior) + ) { return } diff --git a/test/test.js b/test/test.js index 2255604..bcb6f42 100644 --- a/test/test.js +++ b/test/test.js @@ -43,7 +43,7 @@ describe('paste-markdown', function () { assert.equal(textarea.value, 'The examples can be found [here](https://github.com).') }) - it('turns pasted urls on selected text into markdown links if pasteAsPlainText is false', function () { + it('turns pasted urls on selected text into markdown links if pasteLinkAsPlainTextOverSelectedText is false', function () { subscription = subscribeWithOptionConfig(subscription, textarea, false) // eslint-disable-next-line i18n-text/no-en @@ -53,7 +53,7 @@ describe('paste-markdown', function () { assert.equal(textarea.value, 'The examples can be found [here](https://github.com).') }) - it('turns pasted urls on selected text into markdown links if pasteAsPlainText is true and skip format flag is true', function () { + it('turns pasted urls on selected text into markdown links if pasteLinkAsPlainTextOverSelectedText is true and skip format flag is true', function () { subscription = subscribeWithOptionConfig(subscription, textarea, true) // eslint-disable-next-line i18n-text/no-en @@ -64,7 +64,7 @@ describe('paste-markdown', function () { assert.equal(textarea.value, 'The examples can be found [here](https://github.com).') }) - it('pastes as plain text on selected text if pasteAsPlainText is true', function () { + it('pastes as plain text on selected text if pasteLinkAsPlainTextOverSelectedText is true', function () { subscription = subscribeWithOptionConfig(subscription, textarea, true) // eslint-disable-next-line i18n-text/no-en @@ -385,10 +385,10 @@ function dispatchSkipFormattingKeyEvent(textarea) { ) } -function subscribeWithOptionConfig(subscription, textarea, pasteAsPlainText) { +function subscribeWithOptionConfig(subscription, textarea, pasteLinkAsPlainTextOverSelectedText) { // Clear the before test subscription with no config and re-subscribe with config subscription.unsubscribe() - return subscribe(textarea, {pasteAsPlainText}) + return subscribe(textarea, {pasteLinkAsPlainTextOverSelectedText}) } function paste(textarea, data) {