Browse Source

fix: property handler with the new properties pair storage

Tienson Qin 1 year ago
parent
commit
1a9608bda1

+ 4 - 3
deps/db/src/logseq/db/frontend/entity_plus.cljc

@@ -33,9 +33,10 @@
        (if (db-based-graph? db)
          (let [result (lookup-entity e k default-value)]
            (->>
-            (map (fn [pair-e]
-                   (let [pid (:db/ident (lookup-entity pair-e :property/pair-property nil))]
-                     {pid (lookup-entity pair-e pid nil)})) result)
+            (keep (fn [pair-e]
+                    (when pair-e
+                      (let [pid (:db/ident (lookup-entity pair-e :property/pair-property nil))]
+                        {pid (lookup-entity pair-e pid nil)}))) result)
             (into {})))
          (lookup-entity e :block/properties nil)))
 

+ 17 - 14
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -102,22 +102,24 @@
 ;; Main malli schemas
 ;; ==================
 ;; These schemas should be data vars to remain as simple and reusable as possible
-(def property-tuple
-  "Represents a tuple of a property and its property value. This schema
+(comment
+  (def property-tuple
+    "Represents a tuple of a property and its property value. This schema
    has 2 metadata hooks which are used to inject a datascript db later"
-  (into
-   [:multi {:dispatch ^:add-db (fn [db property-tuple]
-                                 (get-in (d/entity db (first property-tuple))
-                                         [:block/schema :type]))}]
-   (map (fn [[prop-type value-schema]]
-          ^:property-value [prop-type (if (vector? value-schema) (last value-schema) value-schema)])
-        db-property-type/built-in-validation-schemas)))
-
-(def block-properties
-  "Validates a slightly modified version of :block/properties. Properties are
+    (into
+     [:multi {:dispatch ^:add-db (fn [db property-tuple]
+                                   (get-in (d/entity db (first property-tuple))
+                                           [:block/schema :type]))}]
+     (map (fn [[prop-type value-schema]]
+            ^:property-value [prop-type (if (vector? value-schema) (last value-schema) value-schema)])
+          db-property-type/built-in-validation-schemas)))
+  (def block-properties
+    "Validates a slightly modified version of :block/properties. Properties are
   expected to be a vector of tuples instead of a map in order to validate each
   property with its property value that is valid for its type"
-  [:sequential property-tuple])
+    [:sequential property-tuple]))
+
+
 
 (def page-or-block-attrs
   "Common attributes for page and normal blocks"
@@ -125,7 +127,8 @@
    [:block/created-at :int]
    [:block/updated-at :int]
    [:block/format [:enum :markdown]]
-   [:block/properties {:optional true} block-properties]
+   [:block/properties {:optional true} [:set :int]]
+   [:property/pair-property {:optional true} :int]
    [:block/refs {:optional true} [:set :int]]
    [:block/tags {:optional true} [:set :int]]
    [:block/collapsed-properties {:optional true} [:set :int]]

+ 4 - 0
deps/db/src/logseq/db/frontend/property.cljs

@@ -241,6 +241,10 @@
   [repo coll db-ident]
   (get coll (get-pid repo db-ident)))
 
+(defn get-pair-e
+  [block db-ident]
+  (first (filter #(some? (db-ident %)) (:block/raw-properties block))))
+
 (defn get-block-property-value
   "Get the value of built-in block's property by its db-ident"
   [repo db block db-ident]

+ 1 - 1
deps/outliner/src/logseq/outliner/core.cljs

@@ -69,7 +69,7 @@
     (when entity
       (block db entity))))
 
-(defn- block-with-timestamps
+(defn ^:api block-with-timestamps
   [block]
   (let [updated-at (common-util/time-ms)
         block (cond->

+ 7 - 20
src/main/frontend/handler/common/developer.cljs

@@ -14,26 +14,13 @@
 
 ;; Fns used between menus and commands
 (defn show-entity-data
-  [& pull-args]
-  (let [result* (apply db/pull pull-args)
-        ;; handles page uuids and closed values w/o knowing type
-        get-uuid-prop-value (fn [v]
-                              (or (db-pu/get-property-name v)
-                                  (get-in (db/entity [:block/uuid v]) [:block/schema :value])))
+  [eid]
+  (let [result* (db/entity eid)
+        ;; ;; handles page uuids and closed values w/o knowing type
+        ;; get-uuid-prop-value (fn [v]
+        ;;                       (or (db-pu/get-property-name v)
+        ;;                           (get-in (db/entity [:block/uuid v]) [:block/schema :value])))
         result (cond-> result*
-                 (and (seq (:block/properties result*)) (config/db-based-graph? (state/get-current-repo)))
-                 (assoc :block.debug/properties
-                        (->> (update-keys (:block/properties result*) db-pu/get-property-name)
-                             (map (fn [[k v]]
-                                    [k
-                                     (cond
-                                       (and (set? v) (uuid? (first v)))
-                                       (set (map get-uuid-prop-value v))
-                                       (uuid? v)
-                                       (get-uuid-prop-value v)
-                                       :else
-                                       v)]))
-                             (into {})))
                  (seq (:block/refs result*))
                  (assoc :block.debug/refs
                         (mapv #(or (:block/original-name (db/entity (:db/id %))) %) (:block/refs result*))))
@@ -97,4 +84,4 @@
     (notification/show! "Graph updated!" :success)))
 
 (defn ^:export replace-graph-with-db-file []
-  (state/set-state! :ui/open-select :db-graph-replace))
+  (state/set-state! :ui/open-select :db-graph-replace))

+ 34 - 18
src/main/frontend/handler/db_based/property.cljs

@@ -27,6 +27,27 @@
 ;; schema -> type, cardinality, object's class
 ;;           min, max -> string length, number range, cardinality size limit
 
+(defn- build-property-pair
+  [db-ident value]
+  (outliner-core/block-with-timestamps
+   {:property/pair-property db-ident
+    db-ident value}))
+
+(defn- build-property-value-tx-data
+  [block property-id value status?]
+  (when value
+    (let [property-pair-e (db-property/get-pair-e block property-id)
+          property-tx-data (outliner-core/block-with-updated-at
+                            (if property-pair-e
+                              {:db/id (:db/id property-pair-e)
+                               property-id value}
+                              {:db/id (:db/id block)
+                               :block/properties (build-property-pair property-id value)}))
+          block-tx-data (when status?
+                          {:db/id (:db/id block)
+                           :block/tags :logseq.class/task})]
+      [property-tx-data block-tx-data])))
+
 (defn built-in-validation-schemas
   "A frontend version of built-in-validation-schemas that adds the current database to
    schema fns"
@@ -193,11 +214,11 @@
               (notification/show! msg' :warning))
             (do
               (when-not tags-or-alias? (upsert-property! repo property-id (assoc property-schema :type property-type) {}))
-              (db/transact! repo
-                            [[:db/retract (:db/id block) property-id]
-                             {:db/id block-eid
-                              property-id values'}]
-                            {:outliner-op :save-block}))))))))
+              (let [pair-id (:db/id (db-property/get-pair-e block property-id))
+                    tx-data (concat
+                             [(when pair-id [:db/retractEnitty pair-id])]
+                             (build-property-value-tx-data block property-id values' false))]
+                (db/transact! repo tx-data {:outliner-op :save-block})))))))))
 
 (defn- resolve-tag
   "Change `v` to a tag's db id if v is a string tag, e.g. `#book`"
@@ -276,18 +297,14 @@
 
                                     :else
                                     v*)
-                          ;; don't modify maps
+                        ;; don't modify maps
                         new-value (if (or (sequential? new-value) (set? new-value))
                                     (if (= :coll property-type)
                                       (vec (remove string/blank? new-value))
                                       (set (remove string/blank? new-value)))
                                     new-value)
-                        block (cond->
-                               {:block/uuid (:block/uuid block)
-                                property-id new-value}
-                                status?
-                                (assoc :block/tags [:logseq.class/task]))]
-                    (db/transact! repo [block] {:outliner-op :save-block})))))))))))
+                        tx-data (build-property-value-tx-data block property-id new-value status?)]
+                    (db/transact! repo tx-data {:outliner-op :save-block})))))))))))
 
 (defn class-add-property!
   [repo class-uuid property-id]
@@ -356,10 +373,7 @@
                                         (catch :default e
                                           (notification/show! (str e) :error false)
                                           nil))]
-                          [{:block/uuid (:block/uuid block)
-                            property-id v*}
-                           (when status?
-                             [:db/add (:db/id block) :block/tags :logseq.class/task])]))))
+                          (build-property-value-tx-data block property-id v* status?)))))
                   block-eids)
                  (remove nil?))]
         (when (seq txs)
@@ -385,9 +399,11 @@
                                                                              txs-state
                                                                              (outliner-core/->Block property-block)
                                                                              {:children? true})
-                                                 @txs-state))]
+                                                 @txs-state))
+                           pair-id (:db/id (db-property/get-pair-e block property-id))]
                        (concat
-                        [[:db/retract (:db/id block) property-id]]
+                        (when pair-id
+                          [[:db/retractEntity pair-id]])
                         retract-blocks-tx))))
                  block-eids)]
         (when (seq txs)