Pārlūkot izejas kodu

refactor: move tldraw shape related props into logseq.tldraw.shape/page

Peng Xiao 3 gadi atpakaļ
vecāks
revīzija
7bae8313d3

+ 17 - 53
deps/graph-parser/src/logseq/graph_parser/extract.cljc

@@ -10,14 +10,13 @@
             [datascript.core :as d]
             [logseq.graph-parser.text :as text]
             [logseq.graph-parser.util :as gp-util]
-            [logseq.graph-parser.util.page-ref :as page-ref]
-            [logseq.graph-parser.util.block-ref :as block-ref]
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.property :as gp-property]
             [logseq.graph-parser.config :as gp-config]
             #?(:org.babashka/nbb [logseq.graph-parser.log :as log]
-               :default [lambdaisland.glogi :as log])))
+               :default [lambdaisland.glogi :as log])
+            [logseq.graph-parser.whiteboard :as gp-whiteboard]))
 
 (defn- filepath->page-name
   [filepath]
@@ -202,43 +201,6 @@
          :blocks blocks
          :ast ast}))))
 
-(defn get-shape-refs [shape]
-  (when (= "logseq-portal" (:type shape))
-    [(if (= (:blockType shape) "P")
-       {:block/name (gp-util/page-name-sanity-lc (:pageId shape))}
-       {:block/uuid (uuid (:pageId shape))})]))
-
-(defn- with-whiteboard-block-refs
-  [shape]
-  (let [refs (or (get-shape-refs shape) [])]
-    (merge {:block/refs refs})))
-
-(defn- with-whiteboard-content
-  [shape]
-  {:block/content (case (:type shape)
-                    "text" (:text shape)
-                    "logseq-portal" (if (= (:blockType shape) "P")
-                                      (page-ref/->page-ref (:pageId shape))
-                                      (block-ref/->block-ref (:pageId shape)))
-                    "line" (str "whiteboard arrow" (when-let [label (:label shape)] (str ": " label)))
-                    (str "whiteboard " (:type shape)))})
-
-(defn with-whiteboard-block-props
-  [block page-name]
-  (let [shape (:block/properties block)
-        shape? (gp-block/whiteboard-properties? shape)
-        default-page-ref {:block/name (gp-util/page-name-sanity-lc page-name)}]
-    (merge (if shape?
-             (merge
-              {:block/uuid (uuid (:id shape))
-               :block/type "whiteboard"}
-              (with-whiteboard-block-refs shape)
-              (with-whiteboard-content shape))
-             {:block/unordered true})
-           (when (nil? (:block/parent block)) {:block/parent default-page-ref})
-           (when (nil? (:block/format block)) {:block/format :markdown}) ;; TODO: read from config
-           {:block/page default-page-ref})))
-
 (defn extract-whiteboard-edn
   "Extracts whiteboard page from given edn file
    Whiteboard page edn is a subset of page schema
@@ -247,20 +209,22 @@
   [file content {:keys [verbose] :or {verbose true}}]
   (let [_ (when verbose (println "Parsing start: " file))
         {:keys [pages blocks]} (gp-util/safe-read-string content)
-        page-block (first pages)
-        page-name (or (:block/name page-block)
-                      (filepath->page-name file))
-        page-original-name (:block/original-name page-block)
-        page-name (gp-util/page-name-sanity-lc page-name)
-        page {:block/name page-name
-              :block/type "whiteboard"
-              :block/original-name page-original-name
-              :block/file {:file/path (gp-util/path-normalize file)}}
-        page-block (merge page-block page (when-not (:block/uuid page-block) {:block/uuid (d/squuid)}))
+        serialized-page (first pages)
+        ;; whiteboard edn file should normally have valid :block/original-name, :block/name, :block/uuid
+        page-name (-> (or (:block/name serialized-page)
+                          (filepath->page-name file))
+                      (gp-util/page-name-sanity-lc))
+        original-name (or (:block/original-name serialized-page)
+                          page-name)
+        page-block (merge {:block/name page-name
+                           :block/original-name original-name
+                           :block/type "whiteboard"
+                           :block/file {:file/path (gp-util/path-normalize file)}}
+                          serialized-page)
+        page-block (gp-whiteboard/migrate-page-block page-block)
         blocks (->> blocks
-                    (map #(merge % {:block/uuid (or (:block/uuid %)
-                                                    (gp-block/get-custom-id-or-new-id (:block/properties %)))}
-                                 (with-whiteboard-block-props % page-name))))
+                    (map gp-whiteboard/migrate-shape-block)
+                    (map #(merge % (gp-whiteboard/with-whiteboard-block-props % page-name))))
         _ (when verbose (println "Parsing finished: " file))]
     {:pages (list page-block)
      :blocks blocks}))

+ 82 - 0
deps/graph-parser/src/logseq/graph_parser/whiteboard.cljs

@@ -0,0 +1,82 @@
+(ns logseq.graph-parser.whiteboard
+  "Whiteboard related parser utilities" 
+  (:require [logseq.graph-parser.util :as gp-util]
+            [logseq.graph-parser.util.block-ref :as block-ref]
+            [logseq.graph-parser.util.page-ref :as page-ref]))
+
+(defn block->shape [block]
+  (get-in block [:block/properties :logseq.tldraw.shape] nil))
+
+(defn page-block->tldr-page [block]
+  (get-in block [:block/properties :logseq.tldraw.page] nil))
+
+(defn shape-block? [block]
+  (= :whiteboard-shape (get-in block [:block/properties :ls-type] nil)))
+
+;; tldraw shape props is now saved into [:block/properties :logseq.tldraw.shape]
+;; migrate
+(defn shape-block-needs-migrate? [block]
+  (let [properties (:block/properties block)]
+    (and (seq properties)
+         (and (= :whiteboard-shape (:ls-type properties))
+              (not (seq (get properties :logseq.tldraw.shape nil)))))))
+
+(defn page-block-needs-migrate? [block]
+  (let [properties (:block/properties block)]
+    (and (seq properties)
+         (and (= :whiteboard-page (:ls-type properties))
+              (not (seq (get properties :logseq.tldraw.page nil)))))))
+
+(defn migrate-shape-block [block]
+  (if (shape-block-needs-migrate? block)
+    (let [properties (:block/properties block)
+          properties (assoc properties :logseq.tldraw.shape properties)]
+      (assoc block :block/properties properties))
+    block))
+
+(defn migrate-page-block [block]
+  (if (page-block-needs-migrate? block)
+    (let [properties (:block/properties block)
+          properties (assoc properties :logseq.tldraw.page properties)]
+      (assoc block :block/properties properties))
+    block))
+
+
+(defn- get-shape-refs [shape]
+  (when (= "logseq-portal" (:type shape))
+    [(if (= (:blockType shape) "P")
+       {:block/name (gp-util/page-name-sanity-lc (:pageId shape))}
+       {:block/uuid (uuid (:pageId shape))})]))
+
+(defn- with-whiteboard-block-refs
+  [shape]
+  (let [refs (or (get-shape-refs shape) [])]
+    (merge {:block/refs refs})))
+
+(defn- with-whiteboard-content
+  "Main purpose of this function is to populate contents when shapes are used as references in outliner."
+  [shape]
+  {:block/content (case (:type shape)
+                    "text" (:text shape)
+                    "logseq-portal" (if (= (:blockType shape) "P")
+                                      (page-ref/->page-ref (:pageId shape))
+                                      (block-ref/->block-ref (:pageId shape)))
+                    "line" (str "whiteboard arrow" (when-let [label (:label shape)] (str ": " label)))
+                    (str "whiteboard " (:type shape)))})
+
+(defn with-whiteboard-block-props
+  [block page-name]
+  (let [shape? (shape-block? block)
+        shape (block->shape block)
+        default-page-ref {:block/name (gp-util/page-name-sanity-lc page-name)}]
+    (merge (if shape?
+             (merge
+              {:block/uuid (uuid (:id shape))}
+              (with-whiteboard-block-refs shape)
+              (with-whiteboard-content shape))
+
+             ;; TODO: remove?
+             {:block/unordered true})
+           (when (nil? (:block/parent block)) {:block/parent default-page-ref})
+           (when (nil? (:block/format block)) {:block/format :markdown}) ;; TODO: read from config
+           {:block/page default-page-ref})))

+ 1 - 1
src/main/frontend/components/sidebar.cljs

@@ -255,7 +255,7 @@
                            :extension? true})}])
    {}))
 
-(rum/defc sidebar-nav
+(rum/defc sidebar-nav < rum/reactive
   [route-match close-modal-fn left-sidebar-open? srs-open?]
   (let [default-home (get-default-home-if-valid)
         route-name (get-in route-match [:data :name])

+ 0 - 4
src/main/frontend/db/model.cljs

@@ -1654,10 +1654,6 @@
        (when-let [path (:file/path (db-utils/entity (:db/id file)))]
          (gp-config/whiteboard? path))))))
 
-(defn whiteboard-shape?
-  [block]
-  (gp-block/whiteboard-properties? (:properties block)))
-
 (defn get-all-whiteboards
   [repo]
   (->> (d/q

+ 1 - 0
src/main/frontend/handler/events.cljs

@@ -92,6 +92,7 @@
                                     (util/uuid-string? (first (:sync-meta %)))
                                     (util/uuid-string? (second (:sync-meta %)))) repos)
                     (sync/sync-start)))))
+            (ui-handler/re-render-root!)
             (file-sync/maybe-onboarding-show status)))))))
 
 (defmethod handle :user/logout [[_]]

+ 19 - 19
src/main/frontend/handler/whiteboard.cljs

@@ -9,18 +9,15 @@
             [frontend.modules.outliner.file :as outliner-file]
             [frontend.state :as state]
             [frontend.util :as util]
-            [logseq.graph-parser.block :as gp-block]
-            [logseq.graph-parser.extract :as gp-extract]))
+            [logseq.graph-parser.whiteboard :as gp-whiteboard]))
 
-(defn- block->shape [block]
-  (:block/properties block))
-
-(defn- shape->block [shape page-name idx]
-  (let [properties (assoc shape :ls-type :whiteboard-shape :index idx)
+(defn shape->block [shape page-name idx]
+  (let [properties {:ls-type :whiteboard-shape
+                    :logseq.tldraw.shape (assoc shape :index idx)}
         block {:block/page {:block/name (util/page-name-sanity-lc page-name)}
                :block/parent {:block/name page-name}
                :block/properties properties}
-        additional-props (gp-extract/with-whiteboard-block-props block page-name)]
+        additional-props (gp-whiteboard/with-whiteboard-block-props block page-name)]
     (merge block additional-props)))
 
 (defn- tldr-page->blocks-tx [page-name tldr-data]
@@ -28,9 +25,8 @@
         page-entity (model/get-page page-name)
         page-block (merge {:block/name page-name
                            :block/type "whiteboard"
-                           :block/properties (-> tldr-data
-                                                 (dissoc :shapes)
-                                                 (assoc :ls-type :whiteboard-page))}
+                           :block/properties {:ls-type :whiteboard-page
+                                              :logseq.tldraw.page (dissoc tldr-data :shapes)}}
                           (when page-entity (select-keys page-entity [:block/created-at])))
         page-block (outliner/block-with-timestamps page-block)
         ;; todo: use get-paginated-blocks instead?
@@ -50,11 +46,15 @@
                        (concat (map :block/uuid blocks))
                        (remove nil?)
                        (set))
+        ;; delete blocks when all of the following are false
+        ;; - the block is not in the new blocks list
+        ;; - the block's parent is not in the new block list
+        ;; - the block is not a shape block 
         delete-blocks (filterv (fn [block]
                                  (not
                                   (or (block-ids (:block/uuid block))
                                       (block-ids (:block/uuid (:block/parent block)))
-                                      (not= :whiteboard-shape (:ls-type (:block/properties block))))))
+                                      (not (gp-whiteboard/shape-block? block)))))
                                existing-blocks)
         delete-blocks-tx (mapv (fn [s] [:db/retractEntity (:db/id s)]) delete-blocks)]
     (concat [page-block] blocks delete-blocks-tx)))
@@ -69,16 +69,16 @@
 (defn- whiteboard-clj->tldr [page-block blocks shape-id]
   (let [id (str (:block/uuid page-block))
         shapes (->> blocks
-                    (map block->shape)
-                    (filter gp-block/whiteboard-properties?)
+                    (filter gp-whiteboard/shape-block?)
+                    (map gp-whiteboard/block->shape)
                     (sort-by :index))
-        page-properties (:block/properties page-block)
-        assets (:assets page-properties)
-        page-properties (dissoc page-properties :assets)]
+        tldr-page (gp-whiteboard/page-block->tldr-page page-block)
+        assets (:assets tldr-page)
+        tldr-page (dissoc tldr-page :assets)]
     (clj->js {:currentPageId id
               :assets (or assets #js[])
               :selectedIds (if (not-empty shape-id) #js[shape-id] #js[])
-              :pages [(merge page-properties
+              :pages [(merge tldr-page
                              {:id id
                               :name "page"
                               :shapes shapes})]})))
@@ -168,7 +168,7 @@
   "Given a page, return all the logseq blocks (exlude all shapes)"
   [page-name]
   (let [blocks (model/get-page-blocks-no-cache page-name)]
-    (remove model/whiteboard-shape? blocks)))
+    (remove gp-whiteboard/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"

+ 3 - 2
src/main/frontend/modules/outliner/tree.cljs

@@ -2,7 +2,8 @@
   (:require [frontend.db :as db]
             [frontend.db.model :as model]
             [clojure.string :as string]
-            [frontend.state :as state]))
+            [frontend.state :as state]
+            [logseq.graph-parser.whiteboard :as gp-whiteboard]))
 
 (defprotocol INode
   (-get-id [this])
@@ -28,7 +29,7 @@
   [blocks root]
   (let [id-map (fn [m] {:db/id (:db/id m)})
         root (id-map root)
-        blocks (remove model/whiteboard-shape? blocks)
+        blocks (remove gp-whiteboard/shape-block? blocks)
         parent-blocks (group-by :block/parent blocks) ;; exclude whiteboard shapes
         sort-fn (fn [parent]
                   (db/sort-by-left (get parent-blocks parent) parent))

+ 0 - 5
src/main/frontend/state.cljs

@@ -1851,11 +1851,6 @@ Similar to re-frame subscriptions"
       (when (:url graph)
         graph))))
 
-;; (defn get-tldraw-api
-;;   []
-;;   (some-> (get-current-whiteboard)
-;;           (gobj/get "api")))
-
 (defn unlinked-dir?
   [dir]
   (contains? (:file/unlinked-dirs @state) dir))