Browse Source

fix: mod+e to embed block

Also being able to paste copied block ref when /node embed.
Tienson Qin 8 months ago
parent
commit
8f6a7d85b5

+ 12 - 1
src/main/frontend/components/editor.cljs

@@ -30,6 +30,7 @@
             [goog.dom :as gdom]
             [goog.string :as gstring]
             [logseq.common.util :as common-util]
+            [logseq.common.util.page-ref :as page-ref]
             [logseq.db :as ldb]
             [logseq.db.frontend.class :as db-class]
             [logseq.graph-parser.property :as gp-property]
@@ -338,7 +339,17 @@
            (when (>= (count edit-content) current-pos)
              (subs edit-content pos current-pos)))]
     (when input
-      (block-search-auto-complete edit-block input id q format selected-text))))
+      (let [db? (config/db-based-graph? (state/get-current-repo))
+            embed? (and db? (= @commands/*current-command "Block embed"))
+            page (when embed? (page-ref/get-page-name edit-content))
+            embed-block-id (when (and embed? page (common-util/uuid-string? page))
+                             (uuid page))]
+        (if embed-block-id
+          (let [f (block-on-chosen-handler true input id q format nil)
+                block (db/entity embed-block-id)]
+            (when block (f block))
+            nil)
+          (block-search-auto-complete edit-block input id q format selected-text))))))
 
 (rum/defc template-search-aux
   [id q]

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

@@ -3235,10 +3235,14 @@
     (when-let [block-id (:block/uuid current-block)]
       (let [db? (config/db-based-graph? (state/get-current-repo))]
         (if (= format "embed")
-          (copy-block-ref! block-id
-                           (if db?
-                             block-ref/->block-ref
-                             #(str "{{embed ((" % "))}}")))
+          (if db?
+            (p/do!
+             (save-current-block!)
+             (util/copy-to-clipboard! (page-ref/->page-ref block-id)
+                                      {:graph (state/get-current-repo)
+                                       :blocks [{:block/uuid (:block/uuid current-block)}]
+                                       :embed-block? true}))
+            (copy-block-ref! block-id #(str "{{embed ((" % "))}}")))
           (copy-block-ref! block-id
                            (if db?
                              page-ref/->page-ref

+ 13 - 3
src/main/frontend/handler/paste.cljs

@@ -190,7 +190,7 @@
   [input text e html]
   (util/stop e)
   (->
-   (p/let [{:keys [graph blocks]} (get-copied-blocks)]
+   (p/let [{:keys [graph blocks embed-block?]} (get-copied-blocks)]
      (if (and (seq blocks) (= graph (state/get-current-repo)))
        ;; Handle internal paste
        (let [revert-cut-txs (get-revert-cut-txs blocks)
@@ -198,8 +198,18 @@
              blocks (if (config/db-based-graph? (state/get-current-repo))
                       (map (fn [b] (dissoc b :block/properties)) blocks)
                       blocks)]
-         (editor-handler/paste-blocks blocks {:revert-cut-txs revert-cut-txs
-                                              :keep-uuid? keep-uuid?}))
+         (if embed-block?
+           (when-let [block-id (:block/uuid (first blocks))]
+             (when-let [current-block (state/get-edit-block)]
+               (p/do!
+                (editor-handler/api-insert-new-block! ""
+                                                      {:block-uuid (:block/uuid current-block)
+                                                       :sibling? true
+                                                       :replace-empty-target? true
+                                                       :other-attrs {:block/link (:db/id (db/entity [:block/uuid block-id]))}})
+                (state/clear-edit!))))
+           (editor-handler/paste-blocks blocks {:revert-cut-txs revert-cut-txs
+                                                :keep-uuid? keep-uuid?})))
        (paste-copied-text input text html)))
    (p/catch (fn [error]
               (log/error :msg "Paste failed" :exception error)

+ 2 - 1
src/main/frontend/util.cljc

@@ -807,7 +807,7 @@
 
 #?(:cljs
    (defn copy-to-clipboard!
-     [text & {:keys [graph html blocks owner-window]}]
+     [text & {:keys [graph html blocks embed-block? owner-window]}]
      (let [blocks (map (fn [block] (if (de/entity? block)
                                      (-> (into {} block)
                                          ;; FIXME: why :db/id is not included?
@@ -820,6 +820,7 @@
                    :blocks (when (and graph (seq blocks))
                              (pr-str
                               {:graph graph
+                               :embed-block? embed-block?
                                :blocks (mapv #(dissoc % :block.temp/fully-loaded? %) blocks)}))}))]
        (if owner-window
          (utils/writeClipboard data owner-window)