From e3723141faec0aeec39798ededfd1e78a424ab0f Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 22 Jul 2024 22:01:21 +0800 Subject: [PATCH] fix: copy block ref for db graphs Copy block embed has been disabled for now, it needs to be fixed first. --- src/main/frontend/components/content.cljs | 42 ++++++++++++++--------- src/main/frontend/handler/editor.cljs | 35 +++++++++++++------ 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/main/frontend/components/content.cljs b/src/main/frontend/components/content.cljs index cecf4173c..c13305043 100644 --- a/src/main/frontend/components/content.cljs +++ b/src/main/frontend/components/content.cljs @@ -28,6 +28,7 @@ [goog.object :as gobj] [logseq.common.util :as common-util] [logseq.common.util.block-ref :as block-ref] + [logseq.common.util.page-ref :as page-ref] [promesa.core :as p] [rum.core :as rum] [logseq.db :as ldb])) @@ -36,7 +37,8 @@ (rum/defc custom-context-menu-content [] - (let [repo (state/get-current-repo)] + (let [repo (state/get-current-repo) + db-based? (config/db-based-graph? repo)] [:<> (ui/menu-background-color #(property-handler/batch-set-block-property! repo (state/get-selection-block-ids) @@ -74,11 +76,11 @@ (shui/dropdown-menu-item {:key "copy as" :on-pointer-down (fn [e] - (util/stop-propagation e) - (let [block-uuids (state/get-selection-block-ids)] - (shui/popup-hide!) - (shui/dialog-open! - #(export/export-blocks block-uuids {:whiteboard? false}))))} + (util/stop-propagation e) + (let [block-uuids (state/get-selection-block-ids)] + (shui/popup-hide!) + (shui/dialog-open! + #(export/export-blocks block-uuids {:whiteboard? false}))))} (t :content/copy-export-as)) (shui/dropdown-menu-item @@ -86,10 +88,11 @@ :on-click editor-handler/copy-block-refs} (t :content/copy-block-ref)) - (shui/dropdown-menu-item - {:key "copy block embeds" - :on-click editor-handler/copy-block-embeds} - (t :content/copy-block-emebed)) + (when-not db-based? + (shui/dropdown-menu-item + {:key "copy block embeds" + :on-click editor-handler/copy-block-embeds} + (t :content/copy-block-emebed))) (shui/dropdown-menu-separator) @@ -226,14 +229,19 @@ (shui/dropdown-menu-item {:key "Copy block ref" :on-click (fn [_e] - (editor-handler/copy-block-ref! block-id block-ref/->block-ref))} + (editor-handler/copy-block-ref! block-id + (if db? page-ref/->page-ref block-ref/->block-ref)))} (t :content/copy-block-ref)) - (shui/dropdown-menu-item - {:key "Copy block embed" - :on-click (fn [_e] - (editor-handler/copy-block-ref! block-id #(util/format "{{embed ((%s))}}" %)))} - (t :content/copy-block-emebed)) + (when-not db? + (shui/dropdown-menu-item + {:key "Copy block embed" + :on-click (fn [_e] + (editor-handler/copy-block-ref! block-id + (if db? + block-ref/->block-ref + #(util/format "{{embed ((%s))}}" %))))} + (t :content/copy-block-emebed))) ;; TODO Logseq protocol mobile support (when (util/electron?) @@ -250,7 +258,7 @@ {:key "Copy as" :on-click (fn [_] (shui/dialog-open! - #(export/export-blocks [block-id] {:whiteboard? false})))} + #(export/export-blocks [block-id] {:whiteboard? false})))} (t :content/copy-export-as)) (shui/dropdown-menu-item diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index f9d73b4e8..79dd915b6 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -1034,11 +1034,13 @@ block (db/entity [:block/uuid (:id first-block)]) copy-str (some->> adjusted-blocks (map (fn [{:keys [id level]}] - (condp = (:block/format block) - :org - (str (string/join (repeat level "*")) " " (block-ref/->block-ref id)) - :markdown - (str (string/join (repeat (dec level) "\t")) "- " (block-ref/->block-ref id))))) + (if (config/db-based-graph? (state/get-current-repo)) + (str (string/join (repeat (dec level) "\t")) "- " (page-ref/->page-ref id)) + (condp = (:block/format block) + :org + (str (string/join (repeat level "*")) " " (block-ref/->block-ref id)) + :markdown + (str (string/join (repeat (dec level) "\t")) "- " (block-ref/->block-ref id)))))) (string/join "\n\n"))] (set-blocks-id! (map :id blocks)) (util/copy-to-clipboard! copy-str)))) @@ -1049,9 +1051,13 @@ (let [ids (->> (distinct (map #(when-let [id (dom/attr % "blockid")] (uuid id)) blocks)) (remove nil?)) - ids-str (some->> ids - (map (fn [id] (util/format "{{embed ((%s))}}" id))) - (string/join "\n\n"))] + ids-str (if (config/db-based-graph? (state/get-current-repo)) + (some->> ids + (map (fn [id] (block-ref/->block-ref id))) + (string/join "\n\n")) + (some->> ids + (map (fn [id] (util/format "{{embed ((%s))}}" id))) + (string/join "\n\n")))] (set-blocks-id! ids) (util/copy-to-clipboard! ids-str)))) @@ -3145,9 +3151,16 @@ [format] (when-let [current-block (state/get-edit-block)] (when-let [block-id (:block/uuid current-block)] - (if (= format "embed") - (copy-block-ref! block-id #(str "{{embed ((" % "))}}")) - (copy-block-ref! block-id block-ref/->block-ref))))) + (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 ((" % "))}}"))) + (copy-block-ref! block-id + (if db? + page-ref/->page-ref + block-ref/->block-ref))))))) (defn copy-current-block-embed [] (copy-current-block-ref "embed"))