fix: improved copy text helper function (#8677)

This commit is contained in:
Calinteodor 2021-02-24 17:12:41 +02:00 committed by GitHub
parent a7db7ecaff
commit 87a110b9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 13 deletions

View File

@ -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 = {

View File

@ -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;
} }