Skip to content

Commit

Permalink
Make config flag more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew L committed Sep 20, 2022
1 parent cd58a52 commit 2661306
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/option-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface OptionConfig {
pasteAsPlainText?: boolean
pasteLinkAsPlainTextOverSelectedText?: boolean
}
11 changes: 7 additions & 4 deletions src/paste-markdown-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {OptionConfig} from './option-config'
import {insertText} from './text'
import {shouldSkipFormatting} from './paste-keyboard-shortcut-helper'

const pasteAsPlainTextMap = new WeakMap<HTMLElement, boolean>()
const pasteLinkAsPlainTextOverSelectedTextMap = new WeakMap<HTMLElement, boolean>()

export function install(el: HTMLElement, optionConfig?: OptionConfig): void {
pasteAsPlainTextMap.set(el, optionConfig?.pasteAsPlainText === true)
pasteLinkAsPlainTextOverSelectedTextMap.set(el, optionConfig?.pasteLinkAsPlainTextOverSelectedText === true)
el.addEventListener('paste', onPaste)
}

Expand All @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2661306

Please sign in to comment.