From c8f4de98ccae139ca028772bcce6593e2e8bca2a Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Mon, 16 May 2022 16:54:32 -0300 Subject: [PATCH] PR comment --- superset-frontend/src/utils/copy.ts | 76 ++++++++++++++--------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/superset-frontend/src/utils/copy.ts b/superset-frontend/src/utils/copy.ts index 1aa1aca8a26e..f6895c8a7f33 100644 --- a/superset-frontend/src/utils/copy.ts +++ b/superset-frontend/src/utils/copy.ts @@ -41,49 +41,47 @@ const copyTextWithClipboardApi = async (getText: () => Promise) => { } }; -const copyTextToClipboard = async (getText: () => Promise) => { - try { - await copyTextWithClipboardApi(getText); - } catch { +const copyTextToClipboard = (getText: () => Promise) => + copyTextWithClipboardApi(getText) // If the Clipboard API is not supported, fallback to the older method. - const text = await getText(); - const copyPromise = new Promise((resolve, reject) => { - const selection: Selection | null = document.getSelection(); - if (selection) { - selection.removeAllRanges(); - const range = document.createRange(); - const span = document.createElement('span'); - span.textContent = text; - span.style.position = 'fixed'; - span.style.top = '0'; - span.style.clip = 'rect(0, 0, 0, 0)'; - span.style.whiteSpace = 'pre'; + .catch(() => + getText().then( + text => + new Promise((resolve, reject) => { + const selection: Selection | null = document.getSelection(); + if (selection) { + selection.removeAllRanges(); + const range = document.createRange(); + const span = document.createElement('span'); + span.textContent = text; + span.style.position = 'fixed'; + span.style.top = '0'; + span.style.clip = 'rect(0, 0, 0, 0)'; + span.style.whiteSpace = 'pre'; - document.body.appendChild(span); - range.selectNode(span); - selection.addRange(range); + document.body.appendChild(span); + range.selectNode(span); + selection.addRange(range); - try { - if (!document.execCommand('copy')) { - reject(); - } - } catch (err) { - reject(); - } + try { + if (!document.execCommand('copy')) { + reject(); + } + } catch (err) { + reject(); + } - document.body.removeChild(span); - if (selection.removeRange) { - selection.removeRange(range); - } else { - selection.removeAllRanges(); - } - } + document.body.removeChild(span); + if (selection.removeRange) { + selection.removeRange(range); + } else { + selection.removeAllRanges(); + } + } - resolve(); - }); - - await copyPromise; - } -}; + resolve(); + }), + ), + ); export default copyTextToClipboard;