Browse Source

Add a test for copied blocks

Also removed a prn
Gabriel Horner 2 years ago
parent
commit
2a78e72f46
2 changed files with 33 additions and 12 deletions
  1. 15 12
      src/main/frontend/handler/paste.cljs
  2. 18 0
      src/test/frontend/handler/paste_test.cljs

+ 15 - 12
src/main/frontend/handler/paste.cljs

@@ -82,28 +82,31 @@
                            (clojure.string/includes? matched-text selection))))
               some?))))
 
+;; See https://developer.chrome.com/blog/web-custom-formats-for-the-async-clipboard-api/
+;; for a similar example
+(defn get-copied-blocks []
+  (p/let [clipboard-items (when (and js/window (gobj/get js/window "navigator") js/navigator.clipboard)
+                            (js/navigator.clipboard.read))
+          blocks-blob ^js (when clipboard-items
+                            (let [types (.-types ^js (first clipboard-items))]
+                              (when (contains? (set types) "web application/logseq")
+                                (.getType ^js (first clipboard-items)
+                                          "web application/logseq"))))
+          blocks-str (when blocks-blob (.text blocks-blob))]
+         (when blocks-str
+           (gp-util/safe-read-string blocks-str))))
+
 (defn- paste-copied-blocks-or-text
   ;; todo: logseq/whiteboard-shapes is now text/html
   [text e html]
   (util/stop e)
   (->
-   (p/let [clipboard-items (when (and js/window (gobj/get js/window "navigator") js/navigator.clipboard)
-                             (js/navigator.clipboard.read))
-           blocks-blob ^js (when clipboard-items
-                             (let [types (.-types ^js (first clipboard-items))]
-                               (when (contains? (set types) "web application/logseq")
-                                 (.getType ^js (first clipboard-items)
-                                           "web application/logseq"))))
-           blocks-str (when blocks-blob (.text blocks-blob))
-           copied-blocks (when blocks-str
-                           (gp-util/safe-read-string blocks-str))]
+   (p/let [copied-blocks (get-copied-blocks)]
      (let [input (state/get-input)
            input-id (state/get-edit-input-id)
            text (string/replace text "\r\n" "\n") ;; Fix for Windows platform
            replace-text-f (fn [text]
                             (let [input-id (state/get-edit-input-id)]
-                              (prn {:text text
-                                    :input-id input-id})
                               (commands/delete-selection! input-id)
                               (commands/simple-insert! input-id text nil)))
            internal-paste? (seq copied-blocks)]

+ 18 - 0
src/test/frontend/handler/paste_test.cljs

@@ -121,6 +121,24 @@
                (is (= expected-paste result))
                (reset))))))
 
+(deftest-async editor-on-paste-with-copied-blocks
+  (let [actual-blocks (atom nil)
+        ;; Simplified version of block attributes that are copied
+        expected-blocks [{:block/content "Test node"}
+                         {:block/content "Notes\nid:: 6422ec75-85c7-4e09-9a4d-2a1639a69b2f"}]
+        clipboard "- Test node\n\t- Notes\nid:: 6422ec75-85c7-4e09-9a4d-2a1639a69b2f"]
+    (test-helper/with-reset
+      reset
+      [state/get-input (constantly #js {:value "block"})
+       ;; paste-copied-blocks-or-text mocks below
+       util/stop (constantly nil)
+       paste-handler/get-copied-blocks (constantly (p/resolved expected-blocks))
+       editor-handler/paste-blocks (fn [blocks _] (reset! actual-blocks blocks))]
+      (p/let [_ ((paste-handler/editor-on-paste! nil)
+                      #js {:clipboardData #js {:getData (constantly clipboard)}})]
+             (is (= expected-blocks @actual-blocks))
+             (reset)))))
+
 (deftest-async editor-on-paste-with-selection-in-property
   (let [clipboard "after"
         expected-paste "after"