fix: improved copy text helper function (#8677)
This commit is contained in:
parent
a7db7ecaff
commit
87a110b9c3
|
@ -2,9 +2,9 @@
|
|||
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { translate } from '../../base/i18n';
|
||||
import { Icon, IconCheck, IconCopy } from '../../base/icons';
|
||||
import { copyText } from '../../base/util';
|
||||
import { translate } from '../i18n';
|
||||
import { copyText } from '../util';
|
||||
|
||||
|
||||
type Props = {
|
||||
|
|
|
@ -31,24 +31,15 @@ export function assignIfDefined(target: Object, source: Object) {
|
|||
* @param {string} textToCopy - Text to be copied.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function copyText(textToCopy: string) {
|
||||
const fakeTextArea = document.createElement('textarea');
|
||||
export async function copyText(textToCopy: string) {
|
||||
let result;
|
||||
|
||||
// $FlowFixMe
|
||||
document.body.appendChild(fakeTextArea);
|
||||
fakeTextArea.value = textToCopy;
|
||||
fakeTextArea.select();
|
||||
|
||||
try {
|
||||
result = document.execCommand('copy');
|
||||
result = await navigator.clipboard.writeText(textToCopy);
|
||||
} catch (err) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
// $FlowFixMe
|
||||
document.body.removeChild(fakeTextArea);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue