Browse Source

enhance: ask clipboard permission

Notice, it still doesn't work for firefox.
Tienson Qin 4 years ago
parent
commit
1008efdc0d
2 changed files with 20 additions and 4 deletions
  1. 6 4
      src/main/frontend/handler/editor.cljs
  2. 14 0
      src/main/frontend/utils.js

+ 6 - 4
src/main/frontend/handler/editor.cljs

@@ -45,7 +45,8 @@
             [goog.object :as gobj]
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
             [lambdaisland.glogi :as log]
             [medley.core :as medley]
             [medley.core :as medley]
-            [promesa.core :as p]))
+            [promesa.core :as p]
+            ["/frontend/utils" :as utils]))
 
 
 ;; FIXME: should support multiple images concurrently uploading
 ;; FIXME: should support multiple images concurrently uploading
 
 
@@ -3072,8 +3073,9 @@
 
 
 (defn paste-text-in-one-block-at-point
 (defn paste-text-in-one-block-at-point
   []
   []
-  (.then
-   (js/navigator.clipboard.readText)
+  (utils/getClipText
    (fn [clipboard-data]
    (fn [clipboard-data]
      (when-let [_ (state/get-input)]
      (when-let [_ (state/get-input)]
-       (state/append-current-edit-content! clipboard-data)))))
+       (state/append-current-edit-content! clipboard-data)))
+   (fn [error]
+     (js/console.error error))))

+ 14 - 0
src/main/frontend/utils.js

@@ -217,3 +217,17 @@ export const ios = function () {
   // iPad on iOS 13 detection
   // iPad on iOS 13 detection
     || (navigator.userAgent.includes("Mac") && "ontouchend" in document)
     || (navigator.userAgent.includes("Mac") && "ontouchend" in document)
 }
 }
+
+export const getClipText = function (cb, errorHandler) {
+  navigator.permissions.query({ name: "clipboard-read" }).then((result) => {
+    if (result.state == "granted" || result.state == "prompt") {
+      navigator.clipboard.readText()
+        .then(text => {
+          cb(text);
+        })
+        .catch(err => {
+          errorHandler(err)
+        });
+    }
+  })
+}