Просмотр исходного кода

fix: 3 bugs with persisting whiteboard data

- :block/uuid was a string
- no timestamps on whiteboard blocks
- no created-at on whiteboard shape blocks

Also added missing page attributes for whiteboard page. Initially I
modeled whiteboards as its own schema type with fewer page attributes
but then I noticed it got format and journal? when referenced so just
added the missing attributes. Fixes LOG-2915
Gabriel Horner 2 лет назад
Родитель
Сommit
61eeb96709

+ 1 - 0
.clj-kondo/config.edn

@@ -99,6 +99,7 @@
              frontend.idb idb
              frontend.loader loader
              frontend.mixins mixins
+             frontend.modules.outliner.core outliner-core
              frontend.mobile.util mobile-util
              frontend.page page
              frontend.schema.handler.common-config common-config-schema

+ 23 - 12
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -224,6 +224,16 @@
     block-attrs
     (remove #(= :block/tags (first %)) page-or-block-attrs))))
 
+(def whiteboard-block
+  "A (shape) block for whiteboard"
+  (vec
+   (concat
+    [:map]
+    [[:block/parent :int]
+     ;; These blocks only associate with pages of type "whiteboard"
+     [:block/page :int]]
+    page-or-block-attrs)))
+
 (def enum-block
   "An enum value for enum property"
   (vec
@@ -252,7 +262,8 @@
   [:or
    normal-block
    object-block
-   enum-block])
+   enum-block
+   whiteboard-block])
 
 ;; TODO: invalid macros should not generate unknown
 (def unknown-block
@@ -289,9 +300,11 @@
    ;; Should this be removed?
    [:block/tx-id {:optional true} :int]])
 
-(def DB-known
-  "A stricter version of the DB schema that doesn't allow for unknown blocks.
-   When we've fixed all known causes of unknown blocks this should be the DB schema"
+(def DB
+  "Malli schema for entities from schema/schema-for-db-based-graph. In order to
+  thoroughly validate properties, the entities and this schema should be
+  prepared with update-properties-in-ents and update-properties-in-schema
+  respectively"
   [:sequential
    [:or
     page
@@ -299,13 +312,12 @@
     file-block
     schema-version
     db-ident
-    macro]])
+    macro
+    unknown-block]])
 
-(def DB
-  "Malli schema for entities from schema/schema-for-db-based-graph. In order to
-  thoroughly validate properties, the entities and this schema should be
-  prepared with update-properties-in-ents and update-properties-in-schema
-  respectively"
+(def DB-known
+  "A stricter version of the DB schema that doesn't allow for unknown blocks.
+   When we've fixed all known causes of unknown blocks this should be the DB schema"
   [:sequential
    [:or
     page
@@ -313,8 +325,7 @@
     file-block
     schema-version
     db-ident
-    macro
-    unknown-block]])
+    macro]])
 
 ;; Keep malli schema in sync with db schema
 ;; ========================================

+ 15 - 16
src/main/frontend/handler/whiteboard.cljs

@@ -9,7 +9,6 @@
             [frontend.handler.route :as route-handler]
             [frontend.handler.property.util :as pu]
             [frontend.modules.editor.undo-redo :as history]
-            [frontend.modules.outliner.core :as outliner]
             [frontend.modules.outliner.file :as outliner-file]
             [frontend.state :as state]
             [frontend.config :as config]
@@ -21,7 +20,8 @@
             [goog.object :as gobj]
             [clojure.set :as set]
             [clojure.string :as string]
-            [cljs-bean.core :as bean]))
+            [cljs-bean.core :as bean]
+            [logseq.db.sqlite.util :as sqlite-util]))
 
 (defn js->clj-keywordize
   [obj]
@@ -115,10 +115,6 @@
                               (mapv (fn [b] (pu/get-property b :logseq.tldraw.shape)))
                               (remove nil?)))
         deleted-shapes-tx (mapv (fn [id] [:db/retractEntity [:block/uuid (uuid id)]]) deleted-ids)
-        with-timestamps (fn [block]
-                          (if (contains? created-ids (str (:block/uuid block)))
-                            (assoc block :block/updated-at (util/time-ms))
-                            (outliner/block-with-timestamps block)))
         changed-shapes (set/difference upsert-shapes created-shapes)
         prev-changed-blocks (when (seq changed-shapes)
                               (db/pull-many repo '[*] (mapv (fn [shape]
@@ -126,7 +122,7 @@
     {:page-block (build-page-block page-name tl-page assets shapes-index)
      :upserted-blocks (->> upsert-shapes
                            (map #(shape->block % page-name))
-                           (map with-timestamps))
+                           (map sqlite-util/block-with-timestamps))
      :delete-blocks deleted-shapes-tx
      :metadata {:whiteboard/transact? (not replace?)
                 :replace? replace?
@@ -194,7 +190,7 @@
   [page-name id]
   (let [properties {(pu/get-pid :ls-type) :whiteboard-page,
                     (pu/get-pid :logseq.tldraw.page)
-                    {:id id,
+                    {:id (str id),
                      :name page-name,
                      :ls-type :whiteboard-page,
                      :bindings {},
@@ -205,6 +201,8 @@
              :original-name page-name
              :type "whiteboard",
              :properties properties,
+             :journal? false
+             :format :markdown
              :updated-at (util/time-ms),
              :created-at (util/time-ms)}]))
 
@@ -217,7 +215,7 @@
   ([name]
    (let [uuid (or (and name (parse-uuid name)) (d/squuid))
          name (or name (str uuid))]
-     (db/transact! (get-default-new-whiteboard-tx name (str uuid)))
+     (db/transact! (get-default-new-whiteboard-tx name uuid))
      (let [page-entity (get-whiteboard-entity name)]
        (when (and page-entity
                   (nil? (:block/file page-entity))
@@ -292,13 +290,14 @@
   (let [uuid (d/squuid)
         page-entity (model/get-page page-name)
         last-root-block (or (get-last-root-block page-name) page-entity)
-        tx {:block/left (select-keys last-root-block [:db/id])
-            :block/uuid uuid
-            :block/content (or content "")
-            :block/format :markdown
-            :block/page {:block/name (util/page-name-sanity-lc page-name)
-                         :block/original-name page-name}
-            :block/parent {:block/name page-name}}]
+        tx (sqlite-util/block-with-timestamps
+            {:block/left (select-keys last-root-block [:db/id])
+             :block/uuid uuid
+             :block/content (or content "")
+             :block/format :markdown
+             :block/page {:block/name (util/page-name-sanity-lc page-name)
+                          :block/original-name page-name}
+             :block/parent {:block/name page-name}})]
     (db-utils/transact! [tx])
     uuid))
 

+ 3 - 3
src/main/logseq/api.cljs

@@ -29,7 +29,7 @@
             [frontend.handler.common.plugin :as plugin-common-handler]
             [frontend.handler.property :as property-handler]
             [frontend.handler.property.util :as pu]
-            [frontend.modules.outliner.core :as outliner]
+            [frontend.modules.outliner.core :as outliner-core]
             [frontend.modules.outliner.tree :as outliner-tree]
             [frontend.handler.command-palette :as palette-handler]
             [frontend.modules.shortcut.core :as st]
@@ -644,7 +644,7 @@
               {:keys [sibling keepUUID]} (bean/->clj opts)
               keep-uuid? (or keepUUID false)
               _          (when keep-uuid? (doseq
-                                            [block (outliner/tree-vec-flatten bb :children)]
+                                            [block (outliner-core/tree-vec-flatten bb :children)]
                                             (let [uuid (:id (:properties block))]
                                               (when (and uuid (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error uuid)))
                                                 (throw (js/Error.
@@ -708,7 +708,7 @@
 (def ^:export get_next_sibling_block
   (fn [block-uuid]
     (when-let [block (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error block-uuid))]
-      (when-let [right-sibling (outliner/get-right-sibling (:db/id block))]
+      (when-let [right-sibling (outliner-core/get-right-sibling (:db/id block))]
         (let [block (db/pull (:db/id right-sibling))]
           (bean/->js (sdk-utils/normalize-keyword-for-json block)))))))