Browse Source

refactor: use db idents for more property identification

e.g. internal property validation, status and priority. The less we rely
on :block/name the more features we can open up later e.g.
interchangeable block/page and rename any page
Gabriel Horner 1 year ago
parent
commit
c053fbab41

+ 1 - 2
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -175,8 +175,7 @@
     page-or-block-attrs)))
 
 (def property-page
-  [:multi {:dispatch
-           (fn [m] (contains? db-property/built-in-properties-keys-str (:block/name m)))}
+  [:multi {:dispatch (fn [m] (contains? m :db/ident))}
    [true internal-property]
    [:malli.core/default user-property]])
 

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

@@ -197,9 +197,21 @@
   [repo db block]
   (= :whiteboard-shape (get-block-property-value repo db block :ls-type)))
 
+(defn get-built-in
+  "Gets a built-in page/class/property/X by its :db/ident"
+  [db db-ident]
+  (d/entity db db-ident))
+
+(defn get-by-ident-or-name
+  "Gets a property by ident or name"
+  [db ident-or-name]
+  (if (and (keyword? ident-or-name) (namespace ident-or-name))
+    (get-built-in db ident-or-name)
+    (d/entity db [:block/name (common-util/page-name-sanity-lc (name ident-or-name))])))
+
 (defn get-closed-property-values
-  [db property-name]
-  (when-let [property (get-property db property-name)]
+  [db ident-or-name]
+  (when-let [property (get-by-ident-or-name db ident-or-name)]
     (get-in property [:block/schema :values])))
 
 (defn closed-value-name
@@ -210,8 +222,8 @@
 (defn get-closed-value-entity-by-name
   "Given a property, finds one of its closed values by name or nil if none
   found. Works for all closed value types"
-  [db property-name value-name]
-  (let [values (get-closed-property-values db property-name)]
+  [db db-ident value-name]
+  (let [values (get-closed-property-values db db-ident)]
     (some (fn [id]
             (let [e (d/entity db [:block/uuid id])]
               (when (= (closed-value-name e) value-name)

+ 4 - 4
src/main/frontend/commands.cljs

@@ -125,7 +125,7 @@
 (defn db-based-statuses
   []
   (map (fn [id] (get-in (db/entity [:block/uuid id]) [:block/schema :value]))
-    (pu/get-closed-property-values "status")))
+    (pu/get-closed-property-values :task/status)))
 
 (defn db-based-embed-page
   []
@@ -161,7 +161,7 @@
 (defn db-based-priorities
   []
   (map (fn [id] (get-in (db/entity [:block/uuid id]) [:block/schema :value]))
-    (pu/get-closed-property-values "priority")))
+    (pu/get-closed-property-values :task/priority)))
 
 (defn get-priorities
   []
@@ -659,7 +659,7 @@
 (defn- db-based-set-status
   [status]
   (when-let [block (state/get-edit-block)]
-    (db-property-handler/batch-set-property-closed-value! [(:block/uuid block)] "status" status)))
+    (db-property-handler/batch-set-property-closed-value! [(:block/uuid block)] :task/status status)))
 
 (defmethod handle-step :editor/set-status [[_ status] format]
   (if (config/db-based-graph? (state/get-current-repo))
@@ -679,7 +679,7 @@
 (defn- db-based-set-priority
   [priority]
   (when-let [block (state/get-edit-block)]
-    (db-property-handler/batch-set-property-closed-value! [(:block/uuid block)] "priority" priority)))
+    (db-property-handler/batch-set-property-closed-value! [(:block/uuid block)] :task/priority priority)))
 
 (defmethod handle-step :editor/set-priority [[_ priority] _format]
   (if (config/db-based-graph? (state/get-current-repo))

+ 7 - 8
src/main/frontend/handler/db_based/property.cljs

@@ -234,7 +234,7 @@
                   (do
                     (upsert-property! repo k-name (assoc property-schema :type property-type)
                                       {:property-uuid property-uuid})
-                    (let [status? (= "status" (string/lower-case k-name))
+                    (let [status? (= :task/status (:db/ident property))
                           new-value (cond
                                       (and multiple-values? old-value
                                            (not= old-value :frontend.components.property/new-value-placeholder))
@@ -378,17 +378,16 @@
 (defn batch-set-property!
   "Notice that this works only for properties with cardinality equals to `one`."
   [repo block-ids k-name v]
-  (let [k-name (name k-name)
-        property (db/entity repo [:block/name (common-util/page-name-sanity-lc k-name)])
+  (let [property (db-property/get-by-ident-or-name (db/get-db repo) k-name)
         property-uuid (or (:block/uuid property) (db/new-block-id))
         type (:type (:block/schema property))
         infer-schema (when-not type (infer-schema-from-input-string v))
         property-type (or type infer-schema :default)
         _ (when (nil? property)
-            (upsert-property! repo k-name (assoc (:block/schema property) :type property-type)
+            (upsert-property! repo (name k-name) (assoc (:block/schema property) :type property-type)
                               {:property-uuid property-uuid}))
         {:keys [cardinality]} (:block/schema property)
-        status? (= "status" (string/lower-case k-name))
+        status? (= :task/status (:db/ident property))
         txs (mapcat
              (fn [id]
                (when-let [block (db/entity [:block/uuid id])]
@@ -845,11 +844,11 @@
      :from-property-id (:db/id from-property)}))
 
 (defn batch-set-property-closed-value!
-  [block-ids property-name closed-value]
+  [block-ids db-ident closed-value]
   (let [repo (state/get-current-repo)
-        closed-value-id (:block/uuid (pu/get-closed-value-entity-by-name property-name closed-value))]
+        closed-value-id (:block/uuid (pu/get-closed-value-entity-by-name db-ident closed-value))]
     (when closed-value-id
       (batch-set-property! repo
                            block-ids
-                           property-name
+                           db-ident
                            closed-value-id))))