diff --git a/src/clipboard-copy-element.js b/src/clipboard-copy-element.js index 4d9ead7..523d44e 100644 --- a/src/clipboard-copy-element.js +++ b/src/clipboard-copy-element.js @@ -6,26 +6,24 @@ function copy(button: HTMLElement) { const id = button.getAttribute('for') const text = button.getAttribute('value') if (text) { - copyText(button, text) + copyText(text) } else if (id) { - copyTarget(button, id) + const node = button.ownerDocument.getElementById(id) + if (node) copyTarget(node) } } -function copyTarget(button: Element, id: string) { - const content = button.ownerDocument.getElementById(id) - if (!content) return - +function copyTarget(content: Element) { if (content instanceof HTMLInputElement || content instanceof HTMLTextAreaElement) { if (content.type === 'hidden') { - copyText(button, content.value) + copyText(content.value) } else { - copyInput(button, content) + copyInput(content) } } else if (content instanceof HTMLAnchorElement && content.hasAttribute('href')) { copyText(button, content.href) } else { - copyNode(button, content) + copyNode(content) } } diff --git a/src/clipboard.js b/src/clipboard.js index 1d99ffd..bbaf6b9 100644 --- a/src/clipboard.js +++ b/src/clipboard.js @@ -10,12 +10,16 @@ function createNode(text: string): Element { return node } -export function copyNode(button: Element, node: Element) { - if (writeAsync(button, node.textContent)) return +export function copyNode(node: Element): Promise { + if ('clipboard' in navigator) { + // eslint-disable-next-line flowtype/no-flow-fix-me-comments + // $FlowFixMe Clipboard is not defined in Flow yet. + return navigator.clipboard.writeText(node.textContent) + } const selection = getSelection() if (selection == null) { - return + return Promise.reject(new Error()) } selection.removeAllRanges() @@ -26,22 +30,34 @@ export function copyNode(button: Element, node: Element) { document.execCommand('copy') selection.removeAllRanges() + return Promise.resolve() } -export function copyText(button: Element, text: string) { - if (writeAsync(button, text)) return +export function copyText(text: string): Promise { + if ('clipboard' in navigator) { + // eslint-disable-next-line flowtype/no-flow-fix-me-comments + // $FlowFixMe Clipboard is not defined in Flow yet. + return navigator.clipboard.writeText(text) + } const body = document.body - if (!body) return + if (!body) { + return Promise.reject(new Error()) + } const node = createNode(text) body.appendChild(node) - copyNode(button, node) + copyNode(node) body.removeChild(node) + return Promise.resolve() } -export function copyInput(button: Element, node: HTMLInputElement | HTMLTextAreaElement) { - if (writeAsync(button, node.value)) return +export function copyInput(node: HTMLInputElement | HTMLTextAreaElement): Promise { + if ('clipboard' in navigator) { + // eslint-disable-next-line flowtype/no-flow-fix-me-comments + // $FlowFixMe Clipboard is not defined in Flow yet. + return navigator.clipboard.writeText(node.value) + } node.select() document.execCommand('copy') @@ -49,14 +65,5 @@ export function copyInput(button: Element, node: HTMLInputElement | HTMLTextArea if (selection != null) { selection.removeAllRanges() } -} - -function writeAsync(button: Element, text: string): boolean { - const clipboard = navigator.clipboard - if (!clipboard) return false - - clipboard.writeText(text).then(function() { - button.dispatchEvent(new CustomEvent('copy', {bubbles: true})) - }) - return true + return Promise.resolve() }