Browse Source

fix(copyText) use a helper library

It does a more elaborate way of textarea copying, hopefully it's more reliable.
j8
Saúl Ibarra Corretgé 4 years ago
parent
commit
5c46b03251
3 changed files with 11 additions and 23 deletions
  1. 5
    0
      package-lock.json
  2. 1
    0
      package.json
  3. 5
    23
      react/features/base/util/helpers.js

+ 5
- 0
package-lock.json View File

5858
       "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-2.2.0.tgz",
5858
       "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-2.2.0.tgz",
5859
       "integrity": "sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk="
5859
       "integrity": "sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk="
5860
     },
5860
     },
5861
+    "clipboard-copy": {
5862
+      "version": "4.0.1",
5863
+      "resolved": "https://registry.npmjs.org/clipboard-copy/-/clipboard-copy-4.0.1.tgz",
5864
+      "integrity": "sha512-wOlqdqziE/NNTUJsfSgXmBMIrYmfd5V0HCGsR8uAKHcg+h9NENWINcfRjtWGU77wDHC8B8ijV4hMTGYbrKovng=="
5865
+    },
5861
     "cliui": {
5866
     "cliui": {
5862
       "version": "3.2.0",
5867
       "version": "3.2.0",
5863
       "resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz",
5868
       "resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz",

+ 1
- 0
package.json View File

41
     "amplitude-js": "7.3.3",
41
     "amplitude-js": "7.3.3",
42
     "base64-js": "1.3.1",
42
     "base64-js": "1.3.1",
43
     "bc-css-flags": "3.0.0",
43
     "bc-css-flags": "3.0.0",
44
+    "clipboard-copy": "4.0.1",
44
     "dropbox": "4.0.9",
45
     "dropbox": "4.0.9",
45
     "focus-visible": "5.1.0",
46
     "focus-visible": "5.1.0",
46
     "i18n-iso-countries": "3.7.8",
47
     "i18n-iso-countries": "3.7.8",

+ 5
- 23
react/features/base/util/helpers.js View File

1
 // @flow
1
 // @flow
2
 
2
 
3
+import clipboardCopy from 'clipboard-copy';
4
+
3
 /**
5
 /**
4
  * A helper function that behaves similar to Object.assign, but only reassigns a
6
  * A helper function that behaves similar to Object.assign, but only reassigns a
5
  * property in target if it's defined in source.
7
  * property in target if it's defined in source.
33
  */
35
  */
34
 export async function copyText(textToCopy: string) {
36
 export async function copyText(textToCopy: string) {
35
     try {
37
     try {
36
-        await navigator.clipboard.writeText(textToCopy);
38
+        await clipboardCopy(textToCopy);
37
 
39
 
38
         return true;
40
         return true;
39
-    } catch (clipboardAPIError) { // The Clipboard API is not supported.
40
-        let fakeTextArea = document.createElement('textarea');
41
-
42
-        // $FlowFixMe
43
-        fakeTextArea = document.body.appendChild(fakeTextArea);
44
-        fakeTextArea.value = textToCopy;
45
-        fakeTextArea.focus();
46
-        fakeTextArea.select();
47
-
48
-        let result;
49
-
50
-        try {
51
-            result = document.execCommand('copy');
52
-        } catch (error) {
53
-            result = false;
54
-        }
55
-
56
-        // $FlowFixMe
57
-        document.body.removeChild(fakeTextArea);
58
-
59
-
60
-        return result;
41
+    } catch (e) {
42
+        return false;
61
     }
43
     }
62
 }
44
 }
63
 
45
 

Loading…
Cancel
Save