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", "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-2.2.0.tgz",
"integrity": "sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=" "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": { "cliui": {
"version": "3.2.0", "version": "3.2.0",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz", "resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz",

View File

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

View File

@ -1,5 +1,7 @@
// @flow // @flow
import clipboardCopy from 'clipboard-copy';
/** /**
* A helper function that behaves similar to Object.assign, but only reassigns a * A helper function that behaves similar to Object.assign, but only reassigns a
* property in target if it's defined in source. * 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) { export async function copyText(textToCopy: string) {
try { try {
await navigator.clipboard.writeText(textToCopy); await clipboardCopy(textToCopy);
return true; return true;
} catch (clipboardAPIError) { // The Clipboard API is not supported. } catch (e) {
let fakeTextArea = document.createElement('textarea'); return false;
// $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;
} }
} }