Browse Source

enhance: use eid instead of uuid for set block's properties

Tienson Qin 1 year ago
parent
commit
1b2771fe42

+ 6 - 2
src/main/frontend/components/content.cljs

@@ -40,8 +40,12 @@
   []
   (let [repo (state/get-current-repo)]
     [:.menu-links-wrapper
-     (ui/menu-background-color #(property-handler/batch-set-block-property! repo (state/get-selection-block-ids) :logseq.property/background-color %)
-                               #(property-handler/batch-remove-block-property! repo (state/get-selection-block-ids) :logseq.property/background-color))
+     (ui/menu-background-color #(property-handler/batch-set-block-property! repo
+                                                                            (state/get-selection-block-ids)
+                                                                            :logseq.property/background-color %)
+                               #(property-handler/batch-remove-block-property! repo
+                                                                               (state/get-selection-block-ids)
+                                                                               :logseq.property/background-color))
 
      (ui/menu-heading #(editor-handler/batch-set-heading! (state/get-selection-block-ids) %)
                       #(editor-handler/batch-set-heading! (state/get-selection-block-ids) true)

+ 2 - 4
src/main/frontend/components/db_based/page.cljs

@@ -9,8 +9,6 @@
             [frontend.config :as config]
             [frontend.db :as db]
             [frontend.handler.db-based.property :as db-property-handler]
-            [frontend.handler.property.util :as pu]
-            [frontend.handler.db-based.property.util :as db-pu]
             [frontend.ui :as ui]
             [frontend.state :as state]
             [rum.core :as rum]
@@ -72,7 +70,7 @@
                                    :on-chosen (fn [_e icon]
                                                 (db-property-handler/set-block-property!
                                                  (state/get-current-repo)
-                                                 (:block/uuid page)
+                                                 (:db/id page)
                                                  :logseq.property/icon
                                                  icon
                                                  {}))})
@@ -80,7 +78,7 @@
         [:a.fade-link.flex {:on-click (fn [_e]
                                         (db-property-handler/remove-block-property!
                                          (state/get-current-repo)
-                                         (:block/uuid page)
+                                         (:db/id page)
                                          :logseq.property/icon))
                             :title "Delete this icon"}
          (ui/icon "X")])])])

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

@@ -339,7 +339,7 @@
                                             {:on-chosen (fn [_e icon]
                                                           (db-property-handler/set-block-property!
                                                            repo
-                                                           (:block/uuid page)
+                                                           (:db/id page)
                                                            :logseq.property/icon
                                                            icon
                                                            {}))

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

@@ -256,7 +256,7 @@
                 [:a.fade-link.flex {:on-click (fn [_e]
                                                 (db-property-handler/remove-block-property!
                                                  (state/get-current-repo)
-                                                 (:block/uuid property)
+                                                 (:db/ident property)
                                                  :logseq.property/icon))
                                     :title "Delete this icon"}
                  (ui/icon "X")])])]

+ 49 - 42
src/main/frontend/handler/db_based/property.cljs

@@ -174,8 +174,8 @@
           (:block/uuid e'))))))
 
 (defn set-block-property!
-  [repo block-id property-id v {:keys [old-value] :as opts}]
-  (let [block (db/entity repo [:block/uuid block-id])
+  [repo block-eid property-id v {:keys [old-value] :as opts}]
+  (let [block (db/entity repo block-eid)
         property (db/entity property-id)
         k-name (:block/original-name property)
         property-schema (:block/schema property)
@@ -183,7 +183,7 @@
         multiple-values? (= cardinality :many)
         v (or (resolve-tag v) v)]
     (if (and multiple-values? (coll? v))
-      (reset-block-property-multiple-values! repo block-id property-id v opts)
+      (reset-block-property-multiple-values! repo block-eid property-id v opts)
       (let [v (if property v (or v ""))]
         (when (some? v)
           (let [infer-schema (when-not type (infer-schema-from-input-string v))
@@ -299,19 +299,24 @@
                            :block/schema schema}]
                     {:outliner-op :save-block}))))
 
+(defn- ->eid
+  [id]
+  (if (uuid? id) [:block/uuid id] id))
+
 (defn batch-set-property!
   "Notice that this works only for properties with cardinality equals to `one`."
   [repo block-ids property-id v]
   (assert property-id "property-id is nil")
-  (let [property (db/entity property-id)
+  (let [block-eids (map ->eid block-ids)
+        property (db/entity property-id)
         type (:type (:block/schema property))
         infer-schema (when-not type (infer-schema-from-input-string v))
         property-type (or type infer-schema :default)
         {:keys [cardinality]} (:block/schema property)
         status? (= :logseq.property/status (:db/ident property))
         txs (mapcat
-             (fn [id]
-               (when-let [block (db/entity [:block/uuid id])]
+             (fn [eid]
+               (when-let [block (db/entity eid)]
                  (when (and (some? v) (not= cardinality :many))
                    (when-let [v* (try
                                    (convert-property-input-string property-type v)
@@ -322,46 +327,48 @@
                        property-id v*}
                       (when status?
                         [:db/add (:db/id block) :block/tags :logseq.class/task])]))))
-             block-ids)]
+             block-eids)]
     (when (seq txs)
       (db/transact! repo txs {:outliner-op :save-block}))))
 
 (defn batch-remove-property!
   [repo block-ids property-id]
-  (when-let [property (db/entity property-id)]
-    (let [txs (mapcat
-               (fn [id]
-                 (when-let [block (db/entity [:block/uuid id])]
-                   (when (get block property-id)
-                     (let [value (get block property-id)
-                           block-value? (and (= :default (get-in property [:block/schema :type] :default))
-                                             (uuid? value))
-                           property-block (when block-value? (db/entity [:block/uuid value]))
-                           retract-blocks-tx (when (and property-block
-                                                        (some? (get property-block :logseq.property/created-from-block))
-                                                        (some? (get property-block :logseq.property/created-from-property)))
-                                               (let [txs-state (atom [])]
-                                                 (outliner-core/delete-block repo
-                                                                             (db/get-db false)
-                                                                             txs-state
-                                                                             (outliner-core/->Block property-block)
-                                                                             {:children? true})
-                                                 @txs-state))]
-                       (concat
-                        [[:db/retract (:db/id block) property-id]]
-                        retract-blocks-tx)))))
-               block-ids)]
-      (when (seq txs)
-        (db/transact! repo txs {:outliner-op :save-block})))))
+  (let [block-eids (map ->eid block-ids)]
+    (when-let [property (db/entity property-id)]
+      (let [txs (mapcat
+                 (fn [eid]
+                   (when-let [block (db/entity eid)]
+                     (when (get block property-id)
+                       (let [value (get block property-id)
+                             block-value? (and (= :default (get-in property [:block/schema :type] :default))
+                                               (uuid? value))
+                             property-block (when block-value? (db/entity [:block/uuid value]))
+                             retract-blocks-tx (when (and property-block
+                                                          (some? (get property-block :logseq.property/created-from-block))
+                                                          (some? (get property-block :logseq.property/created-from-property)))
+                                                 (let [txs-state (atom [])]
+                                                   (outliner-core/delete-block repo
+                                                                               (db/get-db false)
+                                                                               txs-state
+                                                                               (outliner-core/->Block property-block)
+                                                                               {:children? true})
+                                                   @txs-state))]
+                         (concat
+                          [[:db/retract (:db/id block) property-id]]
+                          retract-blocks-tx)))))
+                 block-eids)]
+        (when (seq txs)
+          (db/transact! repo txs {:outliner-op :save-block}))))))
 
 (defn remove-block-property!
-  [repo block-id property-id]
+  [repo eid property-id]
+  (prn :debug :eid eid :property-id property-id)
   (if (contains? #{:block/alias :block/tags} property-id)
-    (when-let [block (db/entity [:block/uuid block-id])]
+    (when-let [block (db/entity eid)]
       (db/transact! repo
                     [[:db/retract (:db/id block) property-id]]
                     {:outliner-op :save-block}))
-    (batch-remove-property! repo [block-id] property-id)))
+    (batch-remove-property! repo [eid] property-id)))
 
 (defn delete-property-value!
   "Delete value if a property has multiple values"
@@ -385,11 +392,11 @@
                             [[:db/retract (:db/id block) property-id]]
                             {:outliner-op :save-block})
               (if (= :default (get-in property [:block/schema :type]))
-                (set-block-property! repo (:block/uuid block)
-                                     (:block/original-name property)
+                (set-block-property! repo (:db/id block)
+                                     (:db/ident property)
                                      ""
                                      {})
-                (remove-block-property! repo (:block/uuid block) property-id)))))))))
+                (remove-block-property! repo (:db/id block) property-id)))))))))
 
 (defn replace-key-with-id
   "Notice: properties need to be created first"
@@ -512,12 +519,12 @@
         first-block (first blocks)
         last-block-id (:block/uuid (last blocks))
         class? (contains? (:block/type block) "class")
-        property-key (:block/original-name property)]
+        property-id (:db/ident property)]
     (db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})
-    (let [result (when property-key
+    (let [result (when property-id
                    (if (and class? class-schema?)
-                     (class-add-property! repo (:block/uuid block) property-key)
-                     (set-block-property! repo (:block/uuid block) property-key (:block/uuid first-block) {})))]
+                     (class-add-property! repo (:db/id block) property-id)
+                     (set-block-property! repo (:db/id block) property-id (:block/uuid first-block) {})))]
       {:last-block-id last-block-id
        :result result})))
 

+ 6 - 4
src/main/frontend/handler/property.cljs

@@ -11,15 +11,17 @@
 (defn remove-block-property!
   [repo block-id property-id-or-key]
   (if (config/db-based-graph? repo)
-    (db-property-handler/remove-block-property! repo block-id property-id-or-key)
+    (let [eid (if (uuid? block-id) [:block/uuid block-id] block-id)]
+      (db-property-handler/remove-block-property! repo eid property-id-or-key))
     (file-property-handler/remove-block-property! block-id property-id-or-key)))
 
 (defn set-block-property!
   [repo block-id key v & opts]
   (if (config/db-based-graph? repo)
-    (if (or (nil? v) (and (coll? v) (empty? v)))
-      (db-property-handler/remove-block-property! repo block-id key)
-      (db-property-handler/set-block-property! repo block-id key v opts))
+    (let [eid (if (uuid? block-id) [:block/uuid block-id] block-id)]
+      (if (or (nil? v) (and (coll? v) (empty? v)))
+       (db-property-handler/remove-block-property! repo eid key)
+       (db-property-handler/set-block-property! repo eid key v opts)))
     (file-property-handler/set-block-property! block-id key v)))
 
 (defn add-page-property!