Browse Source

fix: e2e test

Tienson Qin 2 years ago
parent
commit
c876e47e8d
2 changed files with 85 additions and 77 deletions
  1. 2 1
      e2e-tests/editor.spec.ts
  2. 83 76
      src/main/frontend/handler/paste.cljs

+ 2 - 1
e2e-tests/editor.spec.ts

@@ -173,8 +173,9 @@ test('copy & paste block ref and replace its content', async ({ page, block }) =
 
   await page.press('textarea >> nth=0', 'Enter')
   await block.waitForBlocks(2)
-
+  await page.waitForTimeout(100)
   await page.keyboard.press(modKey + '+v')
+  await page.waitForTimeout(100)
   await page.keyboard.press('Enter')
 
   // Check if the newly created block-ref has the same referenced content

+ 83 - 76
src/main/frontend/handler/paste.cljs

@@ -86,82 +86,89 @@
   ;; 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))]
-    (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]
-                           (commands/delete-selection! input-id)
-                           (commands/simple-insert! input-id text nil))
-          internal-paste? (seq copied-blocks)]
-      (if internal-paste?
-        (editor-handler/paste-blocks copied-blocks {})
-        (let [shape-refs-text (when (and (not (string/blank? html))
-                                         (get-whiteboard-tldr-from-text html))
-                                ;; text should always be prepared block-ref generated in tldr
-                                text)
-              {:keys [value selection] :as selection-and-format} (editor-handler/get-selection-and-format)
-              text-url? (gp-util/url? text)
-              selection-url? (gp-util/url? selection)]
-          (cond
-            (not (string/blank? shape-refs-text))
-            (commands/simple-insert! input-id shape-refs-text nil)
-
-            (or (and (or text-url? selection-url?)
-                     (selection-within-link? selection-and-format))
-                (and text-url? selection-url?))
-            (replace-text-f text)
-
-            (and (or text-url?
-                     (and value (gp-util/url? (string/trim value))))
-                 (not (string/blank? (util/get-selected-text))))
-            (editor-handler/html-link-format! text)
-
-            (and (block-ref/block-ref? text)
-                 (editor-handler/wrapped-by? input block-ref/left-parens block-ref/right-parens))
-            (commands/simple-insert! input-id (block-ref/get-block-ref-id text) nil)
-
-            :else
-            ;; from external
-            (let [format (or (db/get-page-format (state/get-current-page)) :markdown)
-                  html-text (let [result (when-not (string/blank? html)
-                                           (try
-                                             (html-parser/convert format html)
-                                             (catch :default e
-                                               (log/error :exception e)
-                                               nil)))]
-                              (if (string/blank? result) nil result))
-                  text (or html-text text)]
-              (match [format
-                      (nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
-                      (nil? (util/safe-re-find #"(?m)^\s*\*+\s+" text))
-                      (nil? (util/safe-re-find #"(?:\r?\n){2,}" text))]
-                [:markdown false _ _]
-                (paste-text-parseable format text)
-
-                [:org _ false _]
-                (paste-text-parseable format text)
-
-                [:markdown true _ false]
-                (paste-segmented-text format text)
-
-                [:markdown true _ true]
-                (replace-text-f text)
-
-                [:org _ true false]
-                (paste-segmented-text format text)
-
-                [:org _ true true]
-                (replace-text-f text)))))))))
+  (->
+   (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))]
+     (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)]
+       (if internal-paste?
+         (editor-handler/paste-blocks copied-blocks {})
+         (let [shape-refs-text (when (and (not (string/blank? html))
+                                          (get-whiteboard-tldr-from-text html))
+                                 ;; text should always be prepared block-ref generated in tldr
+                                 text)
+               {:keys [value selection] :as selection-and-format} (editor-handler/get-selection-and-format)
+               text-url? (gp-util/url? text)
+               selection-url? (gp-util/url? selection)]
+           (cond
+             (not (string/blank? shape-refs-text))
+             (commands/simple-insert! input-id shape-refs-text nil)
+
+             (or (and (or text-url? selection-url?)
+                      (selection-within-link? selection-and-format))
+                 (and text-url? selection-url?))
+             (replace-text-f text)
+
+             (and (or text-url?
+                      (and value (gp-util/url? (string/trim value))))
+                  (not (string/blank? (util/get-selected-text))))
+             (editor-handler/html-link-format! text)
+
+             (and (block-ref/block-ref? text)
+                  (editor-handler/wrapped-by? input block-ref/left-parens block-ref/right-parens))
+             (commands/simple-insert! input-id (block-ref/get-block-ref-id text) nil)
+
+             :else
+             ;; from external
+             (let [format (or (db/get-page-format (state/get-current-page)) :markdown)
+                   html-text (let [result (when-not (string/blank? html)
+                                            (try
+                                              (html-parser/convert format html)
+                                              (catch :default e
+                                                (log/error :exception e)
+                                                nil)))]
+                               (if (string/blank? result) nil result))
+                   text (or html-text text)]
+               (match [format
+                       (nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
+                       (nil? (util/safe-re-find #"(?m)^\s*\*+\s+" text))
+                       (nil? (util/safe-re-find #"(?:\r?\n){2,}" text))]
+                 [:markdown false _ _]
+                 (paste-text-parseable format text)
+
+                 [:org _ false _]
+                 (paste-text-parseable format text)
+
+                 [:markdown true _ false]
+                 (paste-segmented-text format text)
+
+                 [:markdown true _ true]
+                 (replace-text-f text)
+
+                 [:org _ true false]
+                 (paste-segmented-text format text)
+
+                 [:org _ true true]
+                 (replace-text-f text))))))))
+   (p/catch (fn [error]
+              (prn "Paste failed: ")
+              (log/error :exception error)))))
 
 (defn paste-text-in-one-block-at-point
   []