From 8e9a189ba1a6c205842b6913e91d6fd9a08fdaa9 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 1 Aug 2023 21:48:36 +0800 Subject: [PATCH] handle whiteboard built-in properties --- .../src/logseq/graph_parser/property.cljs | 2 +- src/main/electron/listener.cljs | 6 +-- src/main/frontend/components/block.cljs | 4 +- src/main/frontend/handler/property/util.cljs | 11 ++++- src/main/frontend/handler/whiteboard.cljs | 43 ++++++++++--------- src/main/frontend/modules/outliner/tree.cljs | 4 +- 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/deps/graph-parser/src/logseq/graph_parser/property.cljs b/deps/graph-parser/src/logseq/graph_parser/property.cljs index d0f7b1772..8736e3912 100644 --- a/deps/graph-parser/src/logseq/graph_parser/property.cljs +++ b/deps/graph-parser/src/logseq/graph_parser/property.cljs @@ -61,7 +61,7 @@ [] (set/union #{:title :icon :template :template-including-parent :public :filters :exclude-from-graph-view :logseq.query/nlp-date - ;; org-mode only + ;; org-mode only :macro :filetags} editable-linkable-built-in-properties editable-view-and-table-properties)) diff --git a/src/main/electron/listener.cljs b/src/main/electron/listener.cljs index 9507e74cf..a6b33fa51 100644 --- a/src/main/electron/listener.cljs +++ b/src/main/electron/listener.cljs @@ -21,8 +21,8 @@ [frontend.ui :as ui] [logseq.common.path :as path] [logseq.graph-parser.util :as gp-util] - [logseq.graph-parser.whiteboard :as gp-whiteboard] - [promesa.core :as p])) + [promesa.core :as p] + [frontend.handler.property.util :as pu])) (defn- safe-api-call "Force the callback result to be nil, otherwise, ipc calls could lead to @@ -113,7 +113,7 @@ block-id (if-let [block (db-model/get-block-by-uuid block-id)] - (if (gp-whiteboard/shape-block? block) + (if (pu/shape-block? block) (route-handler/redirect-to-whiteboard! (get-in block [:block/page :block/name]) {:block-id block-id}) (route-handler/redirect-to-page! block-id)) (notification/show! (str "Open link failed. Block-id `" block-id "` doesn't exist in the graph.") :error false)) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index b2fc42fac..4d98b7826 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -1677,7 +1677,7 @@ (defn- bullet-on-click [e block uuid] (cond - (gp-whiteboard/shape-block? block) + (pu/shape-block? block) (route-handler/redirect-to-whiteboard! (get-in block [:block/page :block/name]) {:block-id uuid}) (gobj/get e "shiftKey") @@ -2788,7 +2788,7 @@ doc-mode? (:document/mode? config) embed? (:embed? config) reference? (:reference? config) - whiteboard-block? (gp-whiteboard/shape-block? block) + whiteboard-block? (pu/shape-block? block) block-id (str "ls-block-" blocks-container-id "-" uuid) has-child? (first (:block/_parent (db/entity (:db/id block)))) attrs (on-drag-and-mouse-attrs block uuid top? block-id *move-to) diff --git a/src/main/frontend/handler/property/util.cljs b/src/main/frontend/handler/property/util.cljs index 662da31f0..681e2660f 100644 --- a/src/main/frontend/handler/property/util.cljs +++ b/src/main/frontend/handler/property/util.cljs @@ -22,4 +22,13 @@ [block key] (let [block (db/entity (:db/id block))] (when-let [properties (:block/properties block)] - (lookup properties key)))) + (lookup properties key)))) + +(defn block->shape [block] + (get-property block :logseq.tldraw.shape)) + +(defn page-block->tldr-page [block] + (get-property block :logseq.tldraw.page)) + +(defn shape-block? [block] + (= :whiteboard-shape (get-property block :ls-type))) diff --git a/src/main/frontend/handler/whiteboard.cljs b/src/main/frontend/handler/whiteboard.cljs index 2837a7078..13990e980 100644 --- a/src/main/frontend/handler/whiteboard.cljs +++ b/src/main/frontend/handler/whiteboard.cljs @@ -8,6 +8,7 @@ [frontend.handler.editor :as editor-handler] [frontend.handler.route :as route-handler] [frontend.handler.property.util :as pu] + [frontend.handler.property :as property-handler] [frontend.modules.editor.undo-redo :as history] [frontend.modules.outliner.core :as outliner] [frontend.modules.outliner.file :as outliner-file] @@ -51,14 +52,14 @@ (->> blocks (map (fn [block] (assoc block :index (get shape-id->index (str (:block/uuid block)) 0)))) - (filter gp-whiteboard/shape-block?) - (map gp-whiteboard/block->shape) + (filter pu/shape-block?) + (map pu/block->shape) (sort-by :index)))) (defn- whiteboard-clj->tldr [page-block blocks] (let [id (str (:block/uuid page-block)) shapes (build-shapes page-block blocks) - tldr-page (gp-whiteboard/page-block->tldr-page page-block) + tldr-page (pu/page-block->tldr-page page-block) assets (:assets tldr-page) tldr-page (dissoc tldr-page :assets)] (clj->js {:currentPageId id @@ -188,21 +189,23 @@ (defn get-default-new-whiteboard-tx [page-name id] - [#:block{:uuid id - :name (util/page-name-sanity-lc page-name), - :original-name page-name - :type "whiteboard", - :properties - {:ls-type :whiteboard-page, - :logseq.tldraw.page - {:id id, - :name page-name, - :ls-type :whiteboard-page, - :bindings {}, - :nonce 1, - :assets []}}, - :updated-at (util/time-ms), - :created-at (util/time-ms)}]) + (let [properties (->> + {:ls-type :whiteboard-page, + :logseq.tldraw.page + {:id id, + :name page-name, + :ls-type :whiteboard-page, + :bindings {}, + :nonce 1, + :assets []}} + (property-handler/replace-key-with-id! (state/get-current-repo)))] + [#:block{:uuid id + :name (util/page-name-sanity-lc page-name), + :original-name page-name + :type "whiteboard", + :properties properties, + :updated-at (util/time-ms), + :created-at (util/time-ms)}])) (defn get-whiteboard-entity [page-name] (db-utils/entity [:block/name (util/page-name-sanity-lc page-name)])) @@ -268,7 +271,7 @@ "Given a page, return all the logseq blocks (exclude all shapes)" [page-name] (let [blocks (model/get-page-blocks-no-cache page-name)] - (remove gp-whiteboard/shape-block? blocks))) + (remove pu/shape-block? blocks))) (defn- get-last-root-block "Get the last root Logseq block in the page. Main purpose is to calculate the new :block/left id" @@ -323,7 +326,7 @@ (let [page-block (first pages) ;; FIXME: should also clone normal blocks shapes (build-shapes page-block blocks) - tldr-page (gp-whiteboard/page-block->tldr-page page-block) + tldr-page (pu/page-block->tldr-page page-block) assets (:assets tldr-page) bindings (:bindings tldr-page)] (.cloneShapesIntoCurrentPage ^js api (clj->js {:shapes shapes diff --git a/src/main/frontend/modules/outliner/tree.cljs b/src/main/frontend/modules/outliner/tree.cljs index 9471b424a..dc401b64d 100644 --- a/src/main/frontend/modules/outliner/tree.cljs +++ b/src/main/frontend/modules/outliner/tree.cljs @@ -3,7 +3,7 @@ [frontend.db.model :as model] [clojure.string :as string] [frontend.state :as state] - [logseq.graph-parser.whiteboard :as gp-whiteboard])) + [frontend.handler.property.util :as pu])) (defprotocol INode (-get-id [this]) @@ -28,7 +28,7 @@ (defn- blocks->vec-tree-aux [blocks root] (let [root-id (:db/id root) - blocks (remove gp-whiteboard/shape-block? blocks) + blocks (remove pu/shape-block? blocks) parent-blocks (group-by #(get-in % [:block/parent :db/id]) blocks) ;; exclude whiteboard shapes sort-fn (fn [parent] (db/sort-by-left (get parent-blocks parent) {:db/id parent}))