Browse Source

fix: outliner.core/delete-block(s) shouldn't delete any page

fixes https://github.com/logseq/db-test/issues/367
Tienson Qin 5 months ago
parent
commit
6e0295ddb7

+ 17 - 20
deps/outliner/src/logseq/outliner/core.cljs

@@ -382,18 +382,23 @@
     (assert (ds/outliner-txs-state? *txs-state)
             "db should be satisfied outliner-tx-state?")
     (let [block-id (:block/uuid this)
-          ids (cons (:db/id this) (ldb/get-block-full-children-ids db block-id))
-          txs (map (fn [id] [:db.fn/retractEntity id]) ids)
-          page-tx (let [block (d/entity db [:block/uuid block-id])]
-                    (when (:block/pre-block? block)
-                      (when-let [id (:db/id (:block/page block))]
-                        [[:db/retract id :block/properties]
-                         [:db/retract id :block/properties-order]
-                         [:db/retract id :block/properties-text-values]
-                         [:db/retract id :block/alias]
-                         [:db/retract id :block/tags]])))]
-      (swap! *txs-state concat txs page-tx)
-      block-id)))
+          block (d/entity db [:block/uuid block-id])]
+      (if (ldb/page? block)
+        (swap! *txs-state concat [[:db/retract (:db/id block) :block/parent]
+                                  [:db/retract (:db/id block) :block/order]
+                                  [:db/retract (:db/id block) :block/page]])
+        (let [ids (cons (:db/id this) (ldb/get-block-full-children-ids db block-id))
+              txs (map (fn [id] [:db.fn/retractEntity id]) ids)
+              page-tx (let [block (d/entity db [:block/uuid block-id])]
+                        (when (:block/pre-block? block)
+                          (when-let [id (:db/id (:block/page block))]
+                            [[:db/retract id :block/properties]
+                             [:db/retract id :block/properties-order]
+                             [:db/retract id :block/properties-text-values]
+                             [:db/retract id :block/alias]
+                             [:db/retract id :block/tags]])))]
+          (swap! *txs-state concat txs page-tx)
+          block-id)))))
 
 (defn- assoc-level-aux
   [tree-vec children-key init-level]
@@ -847,11 +852,6 @@
               (ldb/sort-page-random-blocks db blocks))
             page-blocks)))
 
-(defn- delete-block
-  [db txs-state node]
-  (otree/-del node txs-state db)
-  @txs-state)
-
 (defn- get-top-level-blocks
   [top-level-blocks non-consecutive?]
   (let [reversed? (and (not non-consecutive?)
@@ -893,9 +893,6 @@
                                       (:db/ident from-property) :logseq.property/empty-placeholder}) datoms)]
             (when (seq tx-data) (swap! txs-state concat tx-data)))
 
-          delete-one-block?
-          (delete-block db txs-state start-block)
-
           :else
           (doseq [id block-ids]
             (let [node (d/entity db id)]

+ 25 - 0
deps/outliner/test/logseq/outliner/core_test.cljs

@@ -1,5 +1,7 @@
 (ns logseq.outliner.core-test
   (:require [cljs.test :refer [deftest is testing]]
+            [datascript.core :as d]
+            [logseq.db :as ldb]
             [logseq.db.test.helper :as db-test]
             [logseq.outliner.core :as outliner-core]))
 
@@ -14,3 +16,26 @@
       (outliner-core/delete-blocks! nil conn nil [block] {})
       (is (nil? (db-test/find-block-by-content @conn "b1")))
       (is (nil? (db-test/find-block-by-content @conn "test block"))))))
+
+(deftest test-delete-page-with-outliner-core
+  (testing "Pages shouldn't be deleted through outliner-core/delete-blocks"
+    (let [conn (db-test/create-conn-with-blocks
+                [{:page {:block/title "page1"}
+                  :blocks [{:block/title "b1"}]}
+                 {:page {:block/title "page2"}
+                  :blocks [{:block/title "b3"}
+                           {:block/title "b4"}]}])
+          page1 (ldb/get-page @conn "page1")
+          page2 (ldb/get-page @conn "page2")
+          _ (d/transact! conn [{:db/id (:db/id page2)
+                                :block/order "a1"
+                                :block/parent (:db/id page1)}])
+          b3 (db-test/find-block-by-content @conn "b3")
+          b4 (db-test/find-block-by-content @conn "b4")]
+      (outliner-core/delete-blocks! nil conn nil [b3 b4 page2] {})
+      (is (some? (db-test/find-block-by-content @conn "b3")))
+      (is (some? (db-test/find-block-by-content @conn "b4")))
+      (let [page2' (ldb/get-page @conn "page2")]
+        (is (= "page2" (:block/title page2')))
+        (is (nil? (:block/parent page2')))
+        (is (nil? (:block/order page2')))))))

+ 19 - 25
src/main/frontend/handler/editor.cljs

@@ -802,7 +802,7 @@
 
 (defn delete-block-inner!
   [repo {:keys [block-id value format config block-container current-block next-block delete-concat?]}]
-  (when block-id
+  (when (and block-id (not (some ldb/page? [current-block next-block])))
     (when-let [block-e (db/entity [:block/uuid block-id])]
       (let [prev-block (db-model/get-prev (db/get-db) (:db/id block-e))
             input-empty? (string/blank? (state/get-edit-content))]
@@ -859,20 +859,22 @@
                      (edit-block! (db/entity (:db/id next-block)) 0)))
 
                   concat-prev-block?
-                  (let [children (:block/_parent (db/entity (:db/id block)))]
-                    (p/do!
-                     (state/set-state! :editor/edit-block-fn edit-block-f)
-                     (ui-outliner-tx/transact!
-                      transact-opts
-                      (when (seq children)
-                        (outliner-op/move-blocks! children prev-block {:sibling? false}))
-                      (delete-block-aux! block)
-                      (save-block! repo prev-block new-content {}))))
+                  (when-not (some ldb/page? [prev-block block])
+                    (let [children (:block/_parent (db/entity (:db/id block)))]
+                      (p/do!
+                       (state/set-state! :editor/edit-block-fn edit-block-f)
+                       (ui-outliner-tx/transact!
+                        transact-opts
+                        (when (seq children)
+                          (outliner-op/move-blocks! children prev-block {:sibling? false}))
+                        (delete-block-aux! block)
+                        (save-block! repo prev-block new-content {})))))
 
                   :else
-                  (p/do!
-                   (state/set-state! :editor/edit-block-fn edit-block-f)
-                   (delete-block-aux! block)))))))))))
+                  (when-not (some ldb/page? [prev-block block])
+                    (p/do!
+                     (state/set-state! :editor/edit-block-fn edit-block-f)
+                     (delete-block-aux! block))))))))))))
 
 (defn move-blocks!
   [blocks target opts]
@@ -1114,20 +1116,12 @@
         (let [repo (state/get-current-repo)
               block-uuids (distinct (map #(uuid (dom/attr % "blockid")) dom-blocks))
               lookup-refs (map (fn [id] [:block/uuid id]) block-uuids)
-              blocks (map db/entity lookup-refs)
-              pages (filter ldb/page? blocks)
-              pages-with-parent (filter (fn [page] (and (:block/parent page) (not (string/blank? (:block/title page))))) pages)]
+              blocks (map db/entity lookup-refs)]
           (ui-outliner-tx/transact!
            {:outliner-op :delete-blocks}
-           (doseq [page pages-with-parent]
-             (outliner-op/remove-block-property! (:db/id page) :block/parent))
-           (let [blocks' (if (seq pages-with-parent)
-                           (let [ids (set (map :db/id pages-with-parent))]
-                             (remove (fn [b] (ids (:db/id b))) blocks))
-                           blocks)]
-             (when (seq blocks')
-               (let [top-level-blocks (block-handler/get-top-level-blocks blocks')
-                     sorted-blocks (mapcat (fn [block]
+           (let [top-level-blocks (block-handler/get-top-level-blocks blocks)]
+             (when (seq top-level-blocks)
+               (let [sorted-blocks (mapcat (fn [block]
                                              (tree/get-sorted-block-and-children repo (:db/id block)))
                                            top-level-blocks)]
                  (when (seq sorted-blocks)