Browse Source

fix: can't add a new page property

Gabriel Horner 1 year ago
parent
commit
eb9f422a6b

+ 12 - 2
src/main/frontend/components/property/value.cljs

@@ -69,7 +69,7 @@
 
 
 (defn <add-property!
 (defn <add-property!
   "If a class and in a class schema context, add the property to its schema.
   "If a class and in a class schema context, add the property to its schema.
-  Otherwise, add a block's property and its value"
+  Otherwise, add a block's property and its value. Creates a new property as needed"
   ([block property-key property-value] (<add-property! block property-key property-value {}))
   ([block property-key property-value] (<add-property! block property-key property-value {}))
   ([block property-key property-value {:keys [exit-edit? class-schema?]
   ([block property-key property-value {:keys [exit-edit? class-schema?]
                                        :or {exit-edit? true}}]
                                        :or {exit-edit? true}}]
@@ -79,7 +79,17 @@
       (when property-key
       (when property-key
         (if (and class? class-schema?)
         (if (and class? class-schema?)
           (db-property-handler/class-add-property! repo (:block/uuid block) property-key)
           (db-property-handler/class-add-property! repo (:block/uuid block) property-key)
-          (property-handler/set-block-property! repo (:block/uuid block) property-key property-value)))
+          (let [[property-id property-value']
+                (if (string? property-key)
+                 (if-let [ent (db/entity [:block/original-name property-key])]
+                   [(:db/ident ent) property-value]
+                  ;; This is a new property. Create a new property id to use of set-block-property!
+                   [(db-property-handler/ensure-unique-db-ident
+                     (db/get-db (state/get-current-repo))
+                     (db-property/create-user-property-ident-from-name property-key))
+                    :logseq.property/empty-placeholder])
+                  [property-key property-value])]
+            (property-handler/set-block-property! repo (:block/uuid block) property-id property-value'))))
       (when exit-edit?
       (when exit-edit?
         (shui/popup-hide!)
         (shui/popup-hide!)
         (exit-edit-property))))))
         (exit-edit-property))))))

+ 4 - 2
src/main/frontend/handler/db_based/property.cljs

@@ -121,7 +121,7 @@
         {:db/ident ident
         {:db/ident ident
          :db/cardinality cardinality})))
          :db/cardinality cardinality})))
 
 
-(defn- ensure-unique-db-ident
+(defn ensure-unique-db-ident
   "Ensures the given db-ident is unique. If a db-ident conflicts, it is made
   "Ensures the given db-ident is unique. If a db-ident conflicts, it is made
   unique by adding a suffix with a unique number e.g. :db-ident-1 :db-ident-2"
   unique by adding a suffix with a unique number e.g. :db-ident-1 :db-ident-2"
   [db db-ident]
   [db db-ident]
@@ -247,9 +247,11 @@
   (if (uuid? id) [:block/uuid id] id))
   (if (uuid? id) [:block/uuid id] id))
 
 
 (defn set-block-property!
 (defn set-block-property!
+  "Updates a block property's value for the an existing property-id. If possibly
+  creating a new property, use upsert-property!"
   [repo block-eid property-id v {:keys [property-name] :as opts}]
   [repo block-eid property-id v {:keys [property-name] :as opts}]
   (let [block-eid (->eid block-eid)
   (let [block-eid (->eid block-eid)
-        _ (assert (keyword? property-id) "property-id should be a keyword")
+        _ (assert (qualified-keyword? property-id) "property-id should be a keyword")
         block (db/entity repo block-eid)
         block (db/entity repo block-eid)
         property (db/entity property-id)
         property (db/entity property-id)
         v (if (and (uuid? v)
         v (if (and (uuid? v)