fix(copyText) use a helper library

It does a more elaborate way of textarea copying, hopefully it's more reliable.
This commit is contained in:
Saúl Ibarra Corretgé 2021-03-04 16:28:33 +01:00 committed by Дамян Минков
parent eeb5abbbe8
commit 5c46b03251
3 changed files with 11 additions and 23 deletions

5
package-lock.json generated
View File

@ -5858,6 +5858,11 @@
"resolved": "https://registry.npmjs.org/cli-width/-/cli-width-2.2.0.tgz",
"integrity": "sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk="
},
"clipboard-copy": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/clipboard-copy/-/clipboard-copy-4.0.1.tgz",
"integrity": "sha512-wOlqdqziE/NNTUJsfSgXmBMIrYmfd5V0HCGsR8uAKHcg+h9NENWINcfRjtWGU77wDHC8B8ijV4hMTGYbrKovng=="
},
"cliui": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz",

View File

@ -41,6 +41,7 @@
"amplitude-js": "7.3.3",
"base64-js": "1.3.1",
"bc-css-flags": "3.0.0",
"clipboard-copy": "4.0.1",
"dropbox": "4.0.9",
"focus-visible": "5.1.0",
"i18n-iso-countries": "3.7.8",

View File

@ -1,5 +1,7 @@
// @flow
import clipboardCopy from 'clipboard-copy';
/**
* A helper function that behaves similar to Object.assign, but only reassigns a
* property in target if it's defined in source.
@ -33,31 +35,11 @@ export function assignIfDefined(target: Object, source: Object) {
*/
export async function copyText(textToCopy: string) {
try {
await navigator.clipboard.writeText(textToCopy);
await clipboardCopy(textToCopy);
return true;
} catch (clipboardAPIError) { // The Clipboard API is not supported.
let fakeTextArea = document.createElement('textarea');
// $FlowFixMe
fakeTextArea = document.body.appendChild(fakeTextArea);
fakeTextArea.value = textToCopy;
fakeTextArea.focus();
fakeTextArea.select();
let result;
try {
result = document.execCommand('copy');
} catch (error) {
result = false;
}
// $FlowFixMe
document.body.removeChild(fakeTextArea);
return result;
} catch (e) {
return false;
}
}