Browse Source

refactor: use :block/link to store favorite-page-db-id

rcmerci 1 year ago
parent
commit
2573e66d0a
2 changed files with 31 additions and 22 deletions
  1. 17 11
      src/main/frontend/handler/common/page.cljs
  2. 14 11
      src/main/frontend/handler/page.cljs

+ 17 - 11
src/main/frontend/handler/common/page.cljs

@@ -113,12 +113,12 @@
 (defn- find-block-in-favorites-page
   [page-block-uuid]
   (let [db (conn/get-db)
-        page-block-uuid-str (str page-block-uuid)
         blocks (ldb/get-page-blocks db favorites-page-name {})]
-    (some (fn [block]
-            (when (= page-block-uuid-str (:block/content block))
-              block))
-          blocks)))
+    (when-let [page-block-entity (d/entity db [:block/uuid page-block-uuid])]
+      (some (fn [block]
+              (when (= (:db/id (:block/link block)) (:db/id page-block-entity))
+                block))
+            blocks))))
 
 (defn favorited?-v2
   [page-block-uuid]
@@ -128,13 +128,19 @@
 (defn <favorite-page!-v2
   [page-block-uuid]
   {:pre [(uuid? page-block-uuid)]}
-  (let [favorites-page (d/entity (conn/get-db) [:block/name favorites-page-name])
+  (let [repo (state/get-current-repo)
+        favorites-page (d/entity (conn/get-db) [:block/name favorites-page-name])
         favorites-page-tx-data (build-hidden-page-tx-data "favorites")]
-    (p/do!
-     (when-not favorites-page (ldb/transact! nil [favorites-page-tx-data]))
-     (editor-handler/api-insert-new-block!
-      (str page-block-uuid)
-      {:page favorites-page-name :edit-block? false}))))
+    (when (d/entity (conn/get-db) [:block/uuid page-block-uuid])
+      (p/do!
+       (when-not favorites-page (ldb/transact! nil [favorites-page-tx-data]))
+       (ui-outliner-tx/transact!
+        {:outliner-op :insert-blocks}
+        (outliner-core/insert-blocks! repo (conn/get-db false) [{:block/link [:block/uuid page-block-uuid]
+                                                                 :block/content ""
+                                                                 :block/format :markdown}]
+                                      (d/entity (conn/get-db) [:block/name favorites-page-name])
+                                      {}))))))
 
 (defn <unfavorite-page!-v2
   [page-block-uuid]

+ 14 - 11
src/main/frontend/handler/page.cljs

@@ -82,10 +82,12 @@
   (when-let [db (conn/get-db)]
     (let [repo (state/get-current-repo)]
       (if (config/db-based-graph? repo)
-        (let [blocks (ldb/get-page-blocks db page-common-handler/favorites-page-name {})]
+        (let [blocks (ldb/sort-by-left
+                      (ldb/get-page-blocks db page-common-handler/favorites-page-name {})
+                      (d/entity db [:block/name page-common-handler/favorites-page-name]))]
           (keep (fn [block]
-                  (when-let [block-uuid (some-> (:block/content block) parse-uuid)]
-                    (d/entity db [:block/uuid block-uuid]))) blocks))
+                  (when-let [block-db-id (:db/id (:block/link block))]
+                    (d/entity db block-db-id))) blocks))
         (let [page-names (->> (:favorites (state/sub-config))
                               (remove string/blank?)
                               (filter string?)
@@ -135,20 +137,21 @@
   [favorites]
   (let [repo (state/get-current-repo)
         conn (conn/get-db false)]
-    (when (d/entity @conn [:block/name page-common-handler/favorites-page-name])
-      (let [favorite-page-block-uuid-coll
+    (when-let [favorites-page-entity (d/entity @conn [:block/name page-common-handler/favorites-page-name])]
+      (let [favorite-page-block-db-id-coll
             (keep (fn [page-name]
                     (some-> (d/entity @conn [:block/name (common-util/page-name-sanity-lc page-name)])
-                            :block/uuid
-                            str))
+                            :db/id))
                   favorites)
-            current-blocks (ldb/get-page-blocks @conn page-common-handler/favorites-page-name {})]
+            current-blocks (ldb/sort-by-left (ldb/get-page-blocks @conn page-common-handler/favorites-page-name {})
+                                             favorites-page-entity)]
+        (prn :favorite-page-block-db-id-coll favorite-page-block-db-id-coll)
         (ui-outliner-tx/transact!
          {}
-         (doseq [[page-block-uuid block] (zipmap favorite-page-block-uuid-coll current-blocks)]
-           (when (not= page-block-uuid (:block/content block))
+         (doseq [[page-block-db-id block] (zipmap favorite-page-block-db-id-coll current-blocks)]
+           (when (not= page-block-db-id (:db/id (:block/link block)))
              (outliner-core/save-block! repo conn (state/get-date-formatter)
-                                        (assoc block :block/content page-block-uuid)))))))))
+                                        (assoc block :block/link page-block-db-id)))))))))
 
 (defn has-more-journals?
   []