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

fix: don't retract attributes for db if block content is not updated

Tienson Qin 2 жил өмнө
parent
commit
3df099a15e

+ 13 - 2
deps/db/src/logseq/db/frontend/schema.cljs

@@ -126,7 +126,6 @@
            :block/properties-order)
    {:file/last-modified-at {}}))
 
-;; TODO: some attributes shouldn't be retracted for the db version
 (def retract-attributes
   #{
     :block/refs
@@ -147,6 +146,18 @@
     }
   )
 
+;; If only block/content changes
+(def db-version-retract-attributes
+  #{:block/tags
+    :block/refs
+    :block/marker
+    :block/priority
+    :block/scheduled
+    :block/deadline
+    :block/repeated?
+    :block/macros
+    :block/warning})
+
 
 ;;; use `(map [:db.fn/retractAttribute <id> <attr>] retract-page-attributes)`
 ;;; to remove attrs to make the page as it's just created and no file attached to it
@@ -189,4 +200,4 @@
        (keep (fn [[k v]]
                (when (not (:db/valueType v))
                  k)))
-       set))
+       set))

+ 3 - 2
src/main/frontend/db/datascript/entity_plus.cljs

@@ -16,10 +16,11 @@
 
      (and (= k :block/content) (config/db-based-graph? (state/get-current-repo)))
      (let [result (lookup-entity e k default-value)
-           refs (:block/refs e)]
+           refs (:block/refs e)
+           tags (:block/tags e)]
        (or
         (when (string? result)
-          (db-utils/special-id->page result refs))
+          (db-utils/special-id->page result (distinct (concat refs tags))))
         default-value))
 
      :else

+ 7 - 6
src/main/frontend/db/model.cljs

@@ -310,12 +310,13 @@ independent of format as format specific heading characters are stripped"
                 page-name
                 route-name
                 (fn content-matches? [block-content external-content block-id]
-                  (= (as-> (:block/refs (db-utils/entity repo block-id)) block-refs
-                       (-> block-content
-                           (db-utils/special-id->page block-refs)
-                           (db-utils/special-id-ref->page block-refs)
-                           heading-content->route-name))
-                     (string/lower-case external-content))))
+                  (let [block (db-utils/entity repo block-id)
+                        ref-tags (distinct (concat (:block/tags block) (:block/refs block)))]
+                    (= (-> block-content
+                           (db-utils/special-id->page ref-tags)
+                           (db-utils/special-id-ref->page ref-tags)
+                           heading-content->route-name)
+                       (string/lower-case external-content)))))
            ffirst)
 
       (->> (d/q '[:find (pull ?b [:block/uuid])

+ 32 - 29
src/main/frontend/modules/outliner/core.cljs

@@ -77,25 +77,26 @@
 
 (defn- remove-orphaned-page-refs!
   [db-id txs-state old-refs new-refs]
-  (when (not= old-refs new-refs)
-    (let [new-refs (set (map (fn [ref]
-                               (or (:block/name ref)
-                                   (and (:db/id ref)
-                                        (:block/name (db/entity (:db/id ref)))))) new-refs))
-          old-pages (->> (map :db/id old-refs)
-                         (db-model/get-entities-by-ids)
-                         (remove (fn [e] (contains? new-refs (:block/name e))))
-                         (map :block/name)
-                         (remove nil?))
-          orphaned-pages (when (seq old-pages)
-                           (db-model/get-orphaned-pages {:pages old-pages
-                                                         :empty-ref-f (fn [page]
-                                                                        (let [refs (:block/_refs page)]
-                                                                          (or (zero? (count refs))
-                                                                              (= #{db-id} (set (map :db/id refs))))))}))]
-      (when (seq orphaned-pages)
-        (let [tx (mapv (fn [page] [:db/retractEntity (:db/id page)]) orphaned-pages)]
-          (swap! txs-state (fn [state] (vec (concat state tx)))))))))
+  (let [old-refs (remove #(some #{"class" "property"} (:block/type %)) old-refs)]
+    (when (not= old-refs new-refs)
+      (let [new-refs (set (map (fn [ref]
+                                 (or (:block/name ref)
+                                     (and (:db/id ref)
+                                          (:block/name (db/entity (:db/id ref)))))) new-refs))
+            old-pages (->> (map :db/id old-refs)
+                           (db-model/get-entities-by-ids)
+                           (remove (fn [e] (contains? new-refs (:block/name e))))
+                           (map :block/name)
+                           (remove nil?))
+            orphaned-pages (when (seq old-pages)
+                             (db-model/get-orphaned-pages {:pages old-pages
+                                                           :empty-ref-f (fn [page]
+                                                                          (let [refs (:block/_refs page)]
+                                                                            (or (zero? (count refs))
+                                                                                (= #{db-id} (set (map :db/id refs))))))}))]
+        (when (seq orphaned-pages)
+          (let [tx (mapv (fn [page] [:db/retractEntity (:db/id page)]) orphaned-pages)]
+            (swap! txs-state (fn [state] (vec (concat state tx))))))))))
 
 (defn- update-page-when-save-block
   [txs-state block-entity m]
@@ -222,7 +223,8 @@
   (when (config/db-based-graph? repo)
     (let [refs (->> (rebuild-block-refs repo block (:block/properties block)
                                         :skip-content-parsing? true)
-                    (concat (:block/refs m)))]
+                    (concat (:block/refs m))
+                    (concat (:block/tags m)))]
       (swap! txs-state (fn [txs] (concat txs [{:db/id (:db/id block)
                                                :block/refs refs}]))))))
 
@@ -310,20 +312,21 @@
                                      (:block/tags m)))]
       (when eid
         ;; Retract attributes to prepare for tx which rewrites block attributes
-        (let [retract-attributes (when db-based?
-                                   (remove #{:block/properties} db-schema/retract-attributes))]
-          (swap! txs-state (fn [txs]
-                             (vec
-                              (concat txs
-                                      (map (fn [attribute]
-                                             [:db/retract eid attribute])
-                                           retract-attributes))))))
+        (when (:block/content m)
+          (let [retract-attributes (if db-based?
+                                     db-schema/db-version-retract-attributes
+                                     db-schema/retract-attributes)]
+            (swap! txs-state (fn [txs]
+                               (vec
+                                (concat txs
+                                        (map (fn [attribute]
+                                               [:db/retract eid attribute])
+                                             retract-attributes)))))))
 
         ;; Update block's page attributes
         (update-page-when-save-block txs-state block-entity m)
         ;; Remove macros as they are replaced by new ones
         (remove-macros-when-save repo txs-state block-entity)
-
         ;; Remove orphaned refs from block
         (remove-orphaned-refs-when-save txs-state block-entity m))
 

+ 1 - 1
src/test/frontend/handler/db_based/property_test.cljs

@@ -208,7 +208,7 @@
       ;; make sure class2 will not be deleted when removing it from the first block
       (editor-handler/save-block! repo sbid "Block 2 #class2")
       (editor-handler/save-block! repo fbid "Block 1 #class1 #class3")
-      (is (= 2 (count (:block/tags (db/entity [:block/uuid fbid]))))))
+      (is (= 3 (count (:block/tags (db/entity [:block/uuid fbid]))))))
     (testing "Get block's classes properties"
       ;; set c2 as parent of c3
       (let [c3 (db/entity [:block/name "class3"])]