Skip to content

Commit

Permalink
PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomedina248 committed Jun 2, 2022
1 parent a545d7e commit c8f4de9
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions superset-frontend/src/utils/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,47 @@ const copyTextWithClipboardApi = async (getText: () => Promise<string>) => {
}
};

const copyTextToClipboard = async (getText: () => Promise<string>) => {
try {
await copyTextWithClipboardApi(getText);
} catch {
const copyTextToClipboard = (getText: () => Promise<string>) =>
copyTextWithClipboardApi(getText)
// If the Clipboard API is not supported, fallback to the older method.
const text = await getText();
const copyPromise = new Promise<void>((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<void>((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;

0 comments on commit c8f4de9

Please sign in to comment.