Browse Source

enhance: simplify copied state

Tienson Qin 3 years ago
parent
commit
f8ace0bc8d

+ 12 - 3
src/main/frontend/handler/editor.cljs

@@ -974,6 +974,16 @@
                  (into [] (state/get-export-block-text-remove-options)))]
     [top-level-block-uuids content]))
 
+(defn- get-all-blocks-by-ids
+  [repo ids]
+  (loop [ids ids
+         result []]
+    (if (seq ids)
+      (let [blocks (db/get-block-and-children repo (first ids))
+            result (vec (concat result blocks))]
+        (recur (remove (set (map :block/uuid result)) (rest ids)) result))
+      result)))
+
 (defn copy-selection-blocks
   [html?]
   (when-let [blocks (seq (state/get-selection-blocks))]
@@ -985,7 +995,7 @@
       (when block
         (let [html (export/export-blocks-as-html repo top-level-block-uuids)]
           (common-handler/copy-to-clipboard-without-id-property! (:block/format block) content (when html? html)))
-        (state/set-copied-blocks content ids)
+        (state/set-copied-blocks! content (get-all-blocks-by-ids repo top-level-block-uuids))
         (notification/show! "Copied!" :success)))))
 
 (defn copy-block-refs
@@ -1068,7 +1078,6 @@
               sorted-blocks (mapcat (fn [block]
                                       (tree/get-sorted-block-and-children repo (:db/id block)))
                                     top-level-blocks)]
-          (state/set-copied-full-blocks nil sorted-blocks)
           (delete-blocks! repo (map :block/uuid sorted-blocks) sorted-blocks dom-blocks))))))
 
 (def url-regex
@@ -1199,7 +1208,7 @@
           [_top-level-block-uuids md-content] (compose-copied-blocks-contents repo [block-id])
           html (export/export-blocks-as-html repo [block-id])
           sorted-blocks (tree/get-sorted-block-and-children repo (:db/id block))]
-      (state/set-copied-full-blocks md-content sorted-blocks)
+      (state/set-copied-blocks! md-content sorted-blocks)
       (common-handler/copy-to-clipboard-without-id-property! (:block/format block) md-content html)
       (delete-block-aux! block true))))
 

+ 17 - 33
src/main/frontend/handler/paste.cljs

@@ -18,7 +18,8 @@
             [logseq.graph-parser.text :as text]
             [frontend.handler.notification :as notification]
             [frontend.util.text :as text-util]
-            [frontend.format.mldoc :as mldoc]))
+            [frontend.format.mldoc :as mldoc]
+            [lambdaisland.glogi :as log]))
 
 (defn- paste-text-parseable
   [format text]
@@ -44,16 +45,6 @@
                            paragraphs))]
     (paste-text-parseable format updated-paragraphs)))
 
-(defn- get-all-blocks-by-ids
-  [repo ids]
-  (loop [ids ids
-         result []]
-    (if (seq ids)
-      (let [blocks (db/get-block-and-children repo (first ids))
-            result (vec (concat result blocks))]
-        (recur (remove (set (map :block/uuid result)) (rest ids)) result))
-      result)))
-
 (defn- wrap-macro-url
   [url]
   (cond
@@ -70,51 +61,44 @@
 
 (defn- paste-copied-blocks-or-text
   [text e html]
+  (util/stop e)
   (let [copied-blocks (state/get-copied-blocks)
-        copied-block-ids (:copy/block-ids copied-blocks)
         copied-graph (:copy/graph copied-blocks)
         input (state/get-input)
         internal-paste? (and
-                         (= copied-graph (state/get-current-repo))
-                         (or (seq copied-block-ids)
-                             (seq (:copy/full-blocks copied-blocks)))
+                         (seq (:copy/blocks copied-blocks))
                          ;; not copied from the external clipboard
                          (= text (:copy/content copied-blocks)))]
     (if internal-paste?
-      (do
-        (util/stop e)
-        (let [blocks (or
-                      (:copy/full-blocks copied-blocks)
-                      (get-all-blocks-by-ids (state/get-current-repo) copied-block-ids))]
-          (when (seq blocks)
-            (state/set-copied-full-blocks! blocks)
-            (editor-handler/paste-blocks blocks {}))))
+      (let [blocks (:copy/blocks copied-blocks)]
+        (when (seq blocks)
+          (editor-handler/paste-blocks blocks {})))
       (let [{:keys [value]} (editor-handler/get-selection-and-format)]
         (cond
           (and (or (gp-util/url? text)
                    (and value (gp-util/url? (string/trim value))))
                (not (string/blank? (util/get-selected-text))))
-          (do
-            (util/stop e)
-            (editor-handler/html-link-format! text))
+          (editor-handler/html-link-format! text)
 
           (and (text/block-ref? text)
                (editor-handler/wrapped-by? input "((" "))"))
-          (do
-            (util/stop e)
-            (commands/simple-insert! (state/get-edit-input-id) (text/get-block-ref text) nil))
+          (commands/simple-insert! (state/get-edit-input-id) (text/get-block-ref text) nil)
 
           :else
           ;; from external
           (let [format (or (db/get-page-format (state/get-current-page)) :markdown)
-                text (or (when-not (string/blank? html)
-                           (html-parser/convert format html))
-                         text)
+                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)
                 input-id (state/get-edit-input-id)
                 replace-text-f (fn []
                                  (commands/delete-selection! input-id)
                                  (commands/simple-insert! input-id text nil))]
-            (util/stop e)
             (match [format
                     (nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
                     (nil? (util/safe-re-find #"(?m)^\s*\*+\s+" text))

+ 4 - 15
src/main/frontend/state.cljs

@@ -194,8 +194,8 @@
 
      ;; copied blocks
      :copy/blocks                           {:copy/content nil
-                                             :copy/block-ids nil
-                                             :copy/graph nil}
+                                             :copy/graph nil
+                                             :copy/blocks nil}
 
      :copy/export-block-text-indent-style   (or (storage/get :copy/export-block-text-indent-style)
                                                 "dashes")
@@ -1439,22 +1439,11 @@
   []
   (:copy/blocks @state))
 
-(defn set-copied-blocks
-  [content ids]
-  (set-state! :copy/blocks {:copy/graph (get-current-repo)
-                            :copy/content content
-                            :copy/block-ids ids
-                            :copy/full-blocks nil}))
-
-(defn set-copied-full-blocks
+(defn set-copied-blocks!
   [content blocks]
   (set-state! :copy/blocks {:copy/graph (get-current-repo)
                             :copy/content (or content (get-in @state [:copy/blocks :copy/content]))
-                            :copy/full-blocks blocks}))
-
-(defn set-copied-full-blocks!
-  [blocks]
-  (set-state! [:copy/blocks :copy/full-blocks] blocks))
+                            :copy/blocks blocks}))
 
 (defn get-export-block-text-indent-style []
   (:copy/export-block-text-indent-style @state))