Browse Source

fix: can't add a class schema or page property

Removed related unused upsert property code. This became unused with #11311.
Also fixed new page property not moving focus to property value
Gabriel Horner 1 year ago
parent
commit
3fdaf0474f

+ 6 - 24
deps/outliner/src/logseq/outliner/property.cljs

@@ -535,30 +535,12 @@
 (defn class-add-property!
   [conn class-id property-id]
   (when-let [class (d/entity @conn class-id)]
-    (when (contains? (:block/type class) "class")
-      (let [[db-ident property options]
-            ;; strings come from user
-            (if (string? property-id)
-              (if-let [ent (ldb/get-case-page @conn property-id)]
-                [(:db/ident ent) ent {}]
-                ;; creates ident beforehand b/c needed in later transact and this avoids
-                ;; making this whole fn async for now
-                [(ensure-unique-db-ident
-                  @conn
-                  (db-property/create-user-property-ident-from-name property-id))
-                 nil
-                 {:property-name property-id}])
-              [property-id (d/entity @conn property-id) {}])
-            property-type (get-in property [:block/schema :type])
-            _ (upsert-property! conn
-                                db-ident
-                                (cond-> (:block/schema property)
-                                  (some? property-type)
-                                  (assoc :type property-type))
-                                options)]
-        (d/transact! conn
-                     [[:db/add (:db/id class) :class/schema.properties db-ident]]
-                     {:outliner-op :save-block})))))
+    (if (contains? (:block/type class) "class")
+      (d/transact! conn
+                   [[:db/add (:db/id class) :class/schema.properties property-id]]
+                   {:outliner-op :save-block})
+      (throw (ex-info "Can't add a property to a block that isn't a class"
+                      {:class-id class-id :property-id property-id})))))
 
 (defn class-remove-property!
   [conn class-id property-id]

+ 4 - 2
src/main/frontend/components/property.cljs

@@ -401,8 +401,10 @@
                                                              :exit-edit? page-configure?})))
       ;; new property entered
       (if (db-property/valid-property-name? property-uuid-or-name)
-        (if (and (contains? (:block/type entity) "class") page-configure?)
-          (pv/<add-property! entity property-uuid-or-name "" {:class-schema? class-schema? :exit-edit? page-configure?})
+        (if (and (contains? (:block/type entity) "class") page-configure? class-schema?)
+          (p/let [_ (db-property-handler/upsert-property! nil {:type :default} {:property-name property-uuid-or-name})
+                  new-property-id (:db/ident (ldb/get-case-page (db/get-db repo) property-uuid-or-name))]
+            (pv/<add-property! entity new-property-id "" {:class-schema? class-schema? :exit-edit? page-configure?}))
           (p/do!
            (db-property-handler/upsert-property! nil {:type :default} {:property-name property-uuid-or-name})
            true))

+ 11 - 26
src/main/frontend/components/property/value.cljs

@@ -10,7 +10,6 @@
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.page :as page-handler]
             [frontend.handler.property :as property-handler]
-            [logseq.outliner.property :as outliner-property]
             [frontend.handler.db-based.property :as db-property-handler]
             [frontend.state :as state]
             [frontend.ui :as ui]
@@ -88,35 +87,21 @@
 
 (defn <add-property!
   "If a class and in a class schema context, add the property to its schema.
-  Otherwise, add a block's property and its value. Creates a new property as needed"
+  Otherwise, add a block's property and its 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-id property-value' {:keys [exit-edit? class-schema?]
                                        :or {exit-edit? true}}]
-
    (let [repo (state/get-current-repo)
-         class? (contains? (:block/type block) "class")
-         property (db/entity property-key)]
+         class? (contains? (:block/type block) "class")]
+     (assert (qualified-keyword? property-id) "property to add must be a keyword")
      (p/do!
-      (when property-key
-        (if (and class? class-schema?)
-          (db-property-handler/class-add-property! (:db/id block) property-key)
-          (let [[property-id property-value']
-                (if (string? property-key)
-                  (if-let [ent (ldb/get-case-page (db/get-db repo) property-key)]
-                    [(:db/ident ent) property-value]
-                    ;; This is a new property. Create a new property id to use of set-block-property!
-                    [(outliner-property/ensure-unique-db-ident
-                      (db/get-db (state/get-current-repo))
-                      (db-property/create-user-property-ident-from-name property-key))
-                     (if (= :checkbox (get-in property [:block/schema :type]))
-                       false
-                       :logseq.property/empty-placeholder)])
-                  [property-key property-value])]
-            (p/let [property (db/entity property-key)]
-              (if (and (db-property-type/ref-property-types (get-in property [:block/schema :type]))
-                       (not (int? property-value')))
-                (<create-new-block! block (db/entity property-id) property-value' {:edit-block? false})
-                (property-handler/set-block-property! repo (:block/uuid block) property-id property-value'))))))
+      (if (and class? class-schema?)
+        (db-property-handler/class-add-property! (:db/id block) property-id)
+        (p/let [property (db/entity property-id)]
+          (if (and (db-property-type/ref-property-types (get-in property [:block/schema :type]))
+                   (not (int? property-value')))
+            (<create-new-block! block (db/entity property-id) property-value' {:edit-block? false})
+            (property-handler/set-block-property! repo (:block/uuid block) property-id property-value'))))
       (when exit-edit?
         (shui/popup-hide!)
         (exit-edit-property))))))