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

fix: delete default property values when deleting blocks

fixes
https://test.logseq.com/#/page/685c3794-3b3e-4909-9292-9c0cdf0d1d80
fixes
https://test.logseq.com/#/page/685c38a1-3e2d-4089-b34d-add09ef9a20f
fixes
https://test.logseq.com/#/page/685aad5f-67dc-48c8-9e7c-6505baaa9074
Tienson Qin 6 месяцев назад
Родитель
Сommit
bbbb2f32a8

+ 1 - 1
deps/db/src/logseq/db.cljs

@@ -372,7 +372,7 @@
         parents'))))
 
 (def get-block-children-ids common-initial-data/get-block-children-ids)
-(def get-block-children common-initial-data/get-block-children)
+(def get-block-full-children-ids common-initial-data/get-block-full-children-ids)
 
 (defn- get-sorted-page-block-ids
   [db page-id]

+ 16 - 43
deps/db/src/logseq/db/common/initial_data.cljs

@@ -86,48 +86,8 @@
       (update block :block/link (fn [link] (d/pull db '[*] (:db/id link))))
       block)))
 
-(comment
-  (defn- property-without-db-attrs
-    [property]
-    (dissoc property :db/index :db/valueType :db/cardinality))
-
-  (defn- property-with-values
-    [db block properties]
-    (when (entity-plus/db-based-graph? db)
-      (let [block (d/entity db (:db/id block))
-            property-vals (if properties
-                            (map block properties)
-                            (vals (:block/properties block)))]
-        (->> property-vals
-             (mapcat
-              (fn [property-values]
-                (let [values (->>
-                              (if (and (coll? property-values)
-                                       (map? (first property-values)))
-                                property-values
-                                #{property-values}))
-                      value-ids (when (every? map? values)
-                                  (->> (map :db/id values)
-                                       (filter (fn [id] (or (int? id) (keyword? id))))))
-                      value-blocks (->>
-                                    (when (seq value-ids)
-                                      (map
-                                       (fn [id] (d/pull db '[:db/id :block/uuid
-                                                             :block/name :block/title
-                                                             :logseq.property/value
-                                                             :block/tags :block/page
-                                                             :logseq.property/created-from-property] id))
-                                       value-ids))
-                                  ;; FIXME: why d/pull returns {:db/id db-ident} instead of {:db/id number-eid}?
-                                    (keep (fn [block]
-                                            (let [from-property-id (get-in block [:logseq.property/created-from-property :db/id])]
-                                              (if (keyword? from-property-id)
-                                                (assoc-in block [:logseq.property/created-from-property :db/id] (:db/id (d/entity db from-property-id)))
-                                                block)))))]
-                  value-blocks))))))))
-
 (defn get-block-children-ids
-  "Returns children UUIDs"
+  "Returns children UUIDs, notice the result doesn't include property value children ids."
   [db block-uuid & {:keys [include-collapsed-children?]
                     :or {include-collapsed-children? true}}]
   (when-let [eid (:db/id (d/entity db [:block/uuid block-uuid]))]
@@ -140,7 +100,6 @@
                             (when (or include-collapsed-children?
                                       (not (:block/collapsed? e))
                                       (common-entity-util/page? e))
-
                               (:block/_parent e)))) eids-to-expand)
                 uuids-to-add (keep :block/uuid children)]
             (vswap! seen (partial apply conj) uuids-to-add)
@@ -148,13 +107,27 @@
       @seen)))
 
 (defn get-block-children
-  "Including nested children."
+  "Including nested children, notice the result doesn't include property values."
   {:arglists '([db block-uuid & {:keys [include-collapsed-children?]}])}
   [db block-uuid & {:as opts}]
   (let [ids (get-block-children-ids db block-uuid opts)]
     (when (seq ids)
       (map (fn [id] (d/entity db [:block/uuid id])) ids))))
 
+(defn get-block-full-children-ids
+  "Including nested, collapsed and property value children."
+  {:arglists '([db block-uuid])}
+  [db block-uuid]
+  (d/q
+   '[:find [?c ...]
+     :in $ ?id %
+     :where
+     [?p :block/uuid ?id]
+     (parent ?p ?c)]
+   db
+   block-uuid
+   (:parent rules/rules)))
+
 (defn- with-raw-title
   [m entity]
   (if-let [raw-title (:block/raw-title entity)]

+ 4 - 8
deps/outliner/src/logseq/outliner/core.cljs

@@ -348,12 +348,8 @@
     (assert (ds/outliner-txs-state? *txs-state)
             "db should be satisfied outliner-tx-state?")
     (let [block-id (:block/uuid this)
-          ids (->>
-               (let [children (ldb/get-block-children db block-id)
-                     children-ids (map :block/uuid children)]
-                 (conj children-ids block-id))
-               (remove nil?))
-          txs (map (fn [id] [:db.fn/retractEntity [:block/uuid id]]) ids)
+          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))]
@@ -871,9 +867,9 @@
                    (not (ldb/page? block))
                    (assoc :block/page target-page))]
         children-page-tx (when not-same-page?
-                           (let [children-ids (ldb/get-block-children-ids db (:block/uuid block))]
+                           (let [children-ids (ldb/get-block-full-children-ids db (:block/uuid block))]
                              (keep (fn [id]
-                                     (let [child (d/entity db [:block/uuid id])]
+                                     (let [child (d/entity db id)]
                                        (when-not (ldb/page? child)
                                          {:block/uuid id
                                           :block/page target-page}))) children-ids)))

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

@@ -0,0 +1,16 @@
+(ns logseq.outliner.core-test
+  (:require [cljs.test :refer [deftest is testing]]
+            [logseq.db.test.helper :as db-test]
+            [logseq.outliner.core :as outliner-core]))
+
+(deftest test-delete-block-with-default-property
+  (testing "Delete block with default property"
+    (let [conn (db-test/create-conn-with-blocks
+                [{:page {:block/title "page1"}
+                  :blocks [{:block/title "b1" :build/properties {:default "test block"}}]}])
+          property-value (:user.property/default (db-test/find-block-by-content @conn "b1"))
+          _ (assert (:db/id property-value))
+          block (db-test/find-block-by-content @conn "b1")]
+      (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"))))))

+ 1 - 1
src/main/frontend/db.cljs

@@ -24,7 +24,7 @@
   entity pull pull-many]
 
  [frontend.db.model
-  delete-files get-block-and-children get-block-by-uuid get-block-children sort-by-order
+  delete-files get-block-and-children get-block-by-uuid sort-by-order
   get-block-parent get-block-parents parents-collapsed?
   get-block-immediate-children get-block-page
   get-file file-exists?  get-files-full

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

@@ -313,15 +313,6 @@ independent of format as format specific heading characters are stripped"
   (when-let [db (conn/get-db repo)]
     (ldb/get-children db block-uuid)))
 
-(defn get-block-children
-  "Including nested children."
-  [repo block-uuid]
-  (when-let [db (conn/get-db repo)]
-    (let [ids (ldb/get-block-children-ids db block-uuid)]
-      (when (seq ids)
-        (let [ids' (map (fn [id] [:block/uuid id]) ids)]
-          (db-utils/pull-many repo '[*] ids'))))))
-
 (defn get-block-and-children
   [repo block-uuid & {:as opts}]
   (let [db (conn/get-db repo)]

+ 5 - 5
src/main/frontend/mobile/action_bar.cljs

@@ -7,13 +7,14 @@
             [frontend.handler.editor :as editor-handler]
             [frontend.mixins :as mixins]
             [frontend.mobile.util :as mobile-util]
-            [frontend.util.ref :as ref]
             [frontend.state :as state]
             [frontend.ui :as ui]
             [frontend.util :as util]
+            [frontend.util.ref :as ref]
             [frontend.util.url :as url-util]
             [goog.dom :as gdom]
             [goog.object :as gobj]
+            [logseq.db :as ldb]
             [rum.core :as rum]))
 
 (defn- action-command
@@ -41,10 +42,9 @@
   (when-let [block (state/sub :mobile/actioned-block)]
     (let [{:block/keys [uuid children]} block
           last-child-block-id (when-not (empty? children)
-                                (->> (db/get-block-children (state/get-current-repo) uuid)
-                                     (filter #(not (db/parents-collapsed? (state/get-current-repo) (:block/uuid %1)))) ;; DOM nodes of blocks the have collapsed parents have no bounding client rect
-                                     last
-                                     :block/uuid))]
+                                (->> (ldb/get-block-children-ids (db/get-db) uuid
+                                                                 {:include-collapsed-children? false})
+                                     last))]
 
       ;; scroll to the most bottom element of the selected block
       (let [tag-id (or last-child-block-id uuid)