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 React, { useState } from 'react';
|
||||||
|
|
||||||
import { translate } from '../../base/i18n';
|
|
||||||
import { Icon, IconCheck, IconCopy } from '../../base/icons';
|
import { Icon, IconCheck, IconCopy } from '../../base/icons';
|
||||||
import { copyText } from '../../base/util';
|
import { translate } from '../i18n';
|
||||||
|
import { copyText } from '../util';
|
||||||
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
|
|
@ -31,24 +31,15 @@ export function assignIfDefined(target: Object, source: Object) {
|
||||||
* @param {string} textToCopy - Text to be copied.
|
* @param {string} textToCopy - Text to be copied.
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
export function copyText(textToCopy: string) {
|
export async function copyText(textToCopy: string) {
|
||||||
const fakeTextArea = document.createElement('textarea');
|
|
||||||
let result;
|
let result;
|
||||||
|
|
||||||
// $FlowFixMe
|
|
||||||
document.body.appendChild(fakeTextArea);
|
|
||||||
fakeTextArea.value = textToCopy;
|
|
||||||
fakeTextArea.select();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = document.execCommand('copy');
|
result = await navigator.clipboard.writeText(textToCopy);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// $FlowFixMe
|
|
||||||
document.body.removeChild(fakeTextArea);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue