1
0
Эх сурвалжийг харах

fix copy to clipboard failure on iOS

llcc 3 жил өмнө
parent
commit
4207df1b1a
3 өөрчлөгдсөн 38 нэмэгдсэн , 26 устгасан
  1. 1 0
      package.json
  2. 32 26
      src/main/frontend/utils.js
  3. 5 0
      yarn.lock

+ 1 - 0
package.json

@@ -66,6 +66,7 @@
         "@capacitor/android": "3.2.2",
         "@capacitor/app": "1.0.6",
         "@capacitor/camera": "1.2.1",
+        "@capacitor/clipboard": "^1.0.8",
         "@capacitor/core": "3.2.2",
         "@capacitor/filesystem": "1.0.6",
         "@capacitor/ios": "3.2.2",

+ 32 - 26
src/main/frontend/utils.js

@@ -1,5 +1,6 @@
 import path from 'path/path.js'
 import { StatusBar, Style } from '@capacitor/status-bar'
+import { Clipboard } from '@capacitor/clipboard';
 
 if (typeof window === 'undefined') {
   global.window = {}
@@ -242,32 +243,37 @@ export const getClipText = (cb, errorHandler) => {
 }
 
 export const writeClipboard = (text, isHtml) => {
-  let blob = new Blob([text], {
-    type: ["text/plain"]
-  });
-  let data = [new ClipboardItem({
-    ["text/plain"]: blob
-  })];
-  if (isHtml) {
-    blob = new Blob([text], {
-      type: ["text/plain", "text/html"]
-    })
-    data = [new ClipboardItem({
-      ["text/plain"]: blob,
-      ["text/html"]: blob
-    })];
-  }
-  navigator.permissions.query({
-    name: "clipboard-write"
-  }).then((result) => {
-    if (result.state == "granted" || result.state == "prompt") {
-      navigator.clipboard.write(data).then(() => {
-        /* success */
-      }).catch(e => {
-        console.log(e, "fail")
-      })
-    }
-  })
+    if (typeof navigator.permissions !== "undefined") {
+        let blob = new Blob([text], {
+            type: ["text/plain"]
+        });
+        let data = [new ClipboardItem({
+            ["text/plain"]: blob
+        })];
+        if (isHtml) {
+            blob = new Blob([text], {
+                type: ["text/plain", "text/html"]
+            })
+            data = [new ClipboardItem({
+                ["text/plain"]: blob,
+                ["text/html"]: blob
+            })];
+        }
+        navigator.permissions.query({
+            name: "clipboard-write"
+        }).then((result) => {
+            if (result.state == "granted" || result.state == "prompt") {
+                navigator.clipboard.write(data).then(() => {
+                    /* success */
+                }).catch(e => {
+                    console.log(e, "fail")
+                })
+            }
+        })} else {
+            Clipboard.write({
+                string: text
+            });
+        }
 }
 
 export const toPosixPath = (input) => {

+ 5 - 0
yarn.lock

@@ -535,6 +535,11 @@
     tslib "^2.1.0"
     xml2js "^0.4.23"
 
+"@capacitor/clipboard@^1.0.8":
+  version "1.0.8"
+  resolved "https://registry.yarnpkg.com/@capacitor/clipboard/-/clipboard-1.0.8.tgz#5799cad4518618d45501509348a5c20d3ed32647"
+  integrity sha512-q8Fb5imJHQtYA+1sGqqigCrXmf0R1ZGXf/XMRtTznQqm0jhiayUusQN63Rv1YtKIPTJeeVJqy/i6rv72d4GH0Q==
+
 "@capacitor/[email protected]":
   version "3.2.2"
   resolved "https://registry.yarnpkg.com/@capacitor/core/-/core-3.2.2.tgz#5926788920ba9117aa735d2941f5b2bdc4a6889a"