Skip to content

Commit

Permalink
Update to use weak map
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew L committed Sep 16, 2022
1 parent bd9a7fb commit cd58a52
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
12 changes: 2 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {OptionConfig, PASTE_AS_PLAIN_TEXT_ATTRIBUTE} from './option-config'
import {install as installHTML, uninstall as uninstallHTML} from './paste-markdown-html'
import {install as installImageLink, uninstall as uninstallImageLink} from './paste-markdown-image-link'
import {install as installLink, uninstall as uninstallLink} from './paste-markdown-link'
Expand All @@ -8,15 +7,14 @@ import {
} from './paste-keyboard-shortcut-helper'
import {install as installTable, uninstall as uninstallTable} from './paste-markdown-table'
import {install as installText, uninstall as uninstallText} from './paste-markdown-text'
import {OptionConfig} from './option-config'

interface Subscription {
unsubscribe: () => void
}

function subscribe(el: HTMLElement, optionConfig?: OptionConfig): Subscription {
markElementWithConfigIfNeeded(el, optionConfig)

installSkipFormatting(el, installTable, installImageLink, installLink, installText, installHTML)
installSkipFormatting(el, [installTable, installImageLink, installText, installHTML], [installLink], optionConfig)
return {
unsubscribe: () => {
uninstallSkipFormatting(el)
Expand All @@ -29,12 +27,6 @@ function subscribe(el: HTMLElement, optionConfig?: OptionConfig): Subscription {
}
}

function markElementWithConfigIfNeeded(el: HTMLElement, optionConfig?: OptionConfig) {
if (optionConfig?.pasteAsPlainText) {
el.setAttribute(PASTE_AS_PLAIN_TEXT_ATTRIBUTE, 'true')
}
}

export {
subscribe,
installHTML,
Expand Down
6 changes: 1 addition & 5 deletions src/option-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const PASTE_AS_PLAIN_TEXT_ATTRIBUTE = 'data-paste-as-plain-text'

interface OptionConfig {
export interface OptionConfig {
pasteAsPlainText?: boolean
}

export {PASTE_AS_PLAIN_TEXT_ATTRIBUTE, OptionConfig}
13 changes: 12 additions & 1 deletion src/paste-keyboard-shortcut-helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {OptionConfig} from './option-config'

const skipFormattingMap = new WeakMap<HTMLElement, boolean>()

function setSkipFormattingFlag(event: KeyboardEvent): void {
Expand All @@ -21,13 +23,22 @@ export function shouldSkipFormatting(el: HTMLElement): boolean {
return shouldSkipFormattingState
}

export function installAround(el: HTMLElement, ...installCallbacks: Array<(el: HTMLElement) => void>): void {
export function installAround(
el: HTMLElement,
installCallbacks: Array<(el: HTMLElement) => void>,
installCallbacksWithOptions: Array<(el: HTMLElement, optionConfig?: OptionConfig) => void>,
optionConfig?: OptionConfig
): void {
el.addEventListener('keydown', setSkipFormattingFlag)

for (const installCallback of installCallbacks) {
installCallback(el)
}

for (const installCallback of installCallbacksWithOptions) {
installCallback(el, optionConfig)
}

el.addEventListener('paste', unsetSkipFormattedFlag)
}

Expand Down
9 changes: 6 additions & 3 deletions src/paste-markdown-link.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {PASTE_AS_PLAIN_TEXT_ATTRIBUTE} from './option-config'
import {OptionConfig} from './option-config'
import {insertText} from './text'
import {shouldSkipFormatting} from './paste-keyboard-shortcut-helper'

export function install(el: HTMLElement): void {
const pasteAsPlainTextMap = new WeakMap<HTMLElement, boolean>()

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

Expand All @@ -13,7 +16,7 @@ export function uninstall(el: HTMLElement): void {
function onPaste(event: ClipboardEvent) {
const {currentTarget: el} = event
const element = el as HTMLElement
const shouldPastePlainText = element.hasAttribute(PASTE_AS_PLAIN_TEXT_ATTRIBUTE)
const shouldPastePlainText = pasteAsPlainTextMap.get(element) ?? false
const shouldSkipDefaultBehavior = shouldSkipFormatting(element)

if ((!shouldPastePlainText && shouldSkipDefaultBehavior) || (shouldPastePlainText && !shouldSkipDefaultBehavior)) {
Expand Down

0 comments on commit cd58a52

Please sign in to comment.