1
0
Эх сурвалжийг харах

refactor: reorder favorites

rcmerci 1 жил өмнө
parent
commit
c93c8b8c3e

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

@@ -155,7 +155,7 @@
                         favorite-entities)]
          (dnd-component/items favorites
                               {:on-drag-end (fn [favorites]
-                                              (page-handler/reorder-favorites! favorites))
+                                              (page-handler/<reorder-favorites! favorites))
                                :parent-node :ul.favorites.text-sm}))))))
 
 (rum/defc recent-pages < rum/reactive db-mixins/query

+ 5 - 5
src/main/frontend/handler/common/page.cljs

@@ -130,11 +130,11 @@
   {:pre [(uuid? page-block-uuid)]}
   (let [favorites-page (d/entity (conn/get-db) [:block/name favorites-page-name])
         favorites-page-tx-data (build-hidden-page-tx-data "favorites")]
-    (p/let [r1 (when-not favorites-page (ldb/transact! nil [favorites-page-tx-data]))
-            r2 (editor-handler/api-insert-new-block!
-                (str page-block-uuid)
-                {:page favorites-page-name :edit-block? false})]
-      (prn ::insert-favorite-page r1 r2))))
+    (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}))))
 
 (defn <unfavorite-page!-v2
   [page-block-uuid]

+ 22 - 6
src/main/frontend/handler/page.cljs

@@ -39,7 +39,9 @@
             [cljs-bean.core :as bean]
             [datascript.core :as d]
             [frontend.db.conn :as conn]
-            [logseq.db :as ldb]))
+            [logseq.db :as ldb]
+            [frontend.modules.outliner.ui :as ui-outliner-tx]
+            [logseq.outliner.core :as outliner-core]))
 
 (def create! page-common-handler/create!)
 (def <create! page-common-handler/<create!)
@@ -82,7 +84,7 @@
       (if (config/db-based-graph? repo)
         (let [blocks (ldb/get-page-blocks db page-common-handler/favorites-page-name {})]
           (keep (fn [block]
-                  (when-let [block-uuid (parse-uuid (:block/content block))]
+                  (when-let [block-uuid (some-> (:block/content block) parse-uuid)]
                     (d/entity db [:block/uuid block-uuid]))) blocks))
         (let [page-names (->> (:favorites (state/sub-config))
                               (remove string/blank?)
@@ -129,10 +131,24 @@
         (notification/show! "Can't merge whiteboard pages" :error)
         nil))))
 
-(defn reorder-favorites!
-  [_favorites]
-  ;; TODO
-  nil)
+(defn <reorder-favorites!
+  [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
+            (keep (fn [page-name]
+                    (some-> (d/entity @conn [:block/name (common-util/page-name-sanity-lc page-name)])
+                            :block/uuid
+                            str))
+                  favorites)
+            current-blocks (ldb/get-page-blocks @conn page-common-handler/favorites-page-name {})]
+        (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))
+             (outliner-core/save-block! repo conn (state/get-date-formatter)
+                                        (assoc block :block/content page-block-uuid)))))))))
 
 (defn has-more-journals?
   []