Browse Source

enhance: make property type immutable for db safety

Tienson Qin 2 years ago
parent
commit
5f6e2596e8

+ 11 - 6
src/main/frontend/components/property.cljs

@@ -293,7 +293,8 @@
         disabled? (or built-in-property? config/publishing?)
         hide-delete? (or (= (:db/id block) (:db/id property)) ; property page
                          add-new-property?)
-        class? (contains? (:block/type block) "class")]
+        class? (contains? (:block/type block) "class")
+        property-type (get-in property [:block/schema :type])]
     [:div.property-configure.flex.flex-1.flex-col
      [:div.grid.gap-2.p-1
       [:div.grid.grid-cols-4.gap-1.items-center.leading-8
@@ -329,10 +330,14 @@
                                        :selected (= (keyword (string/lower-case type))
                                                     (:type @*property-schema))})))]
          [:div.col-span-2
-          (ui/select schema-types
-                     (fn [_e v]
-                       (let [type (keyword (string/lower-case v))]
-                         (swap! *property-schema assoc :type type))))])]
+          (if property-type
+            (if (= property-type :default)
+              "Text"
+              ((comp string/capitalize name) property-type))
+            (ui/select schema-types
+                       (fn [_e v]
+                         (let [type (keyword (string/lower-case v))]
+                           (swap! *property-schema assoc :type type)))))])]
 
       (case (:type @*property-schema)
         :page
@@ -454,7 +459,7 @@
         (if (and (contains? (:block/type entity) "class") page-configure?)
           (pv/add-property! entity property-name "" {:class-schema? class-schema? :exit-edit? page-configure?})
           (do
-            (db-property-handler/upsert-property! repo property-name {:type :default} {})
+            (db-property-handler/upsert-property! repo property-name {} {})
             (when *show-new-property-config?
               (reset! *show-new-property-config? true))))
         (do (notification/show! "This is an invalid property name. A property name cannot start with page reference characters '#' or '[['." :error)

+ 17 - 15
src/main/frontend/handler/db_based/property.cljs

@@ -261,21 +261,23 @@
                               properties]}]
   {:pre [(uuid? property-uuid)]}
   (when-let [property (db/entity [:block/uuid property-uuid])]
-    (when (and (= :many (:cardinality property-schema))
-               (not= :many (:cardinality (:block/schema property))))
-      ;; cardinality changed from :one to :many
-      (fix-cardinality-many-values! repo property-uuid))
-    (let [tx-data (cond-> {:block/uuid property-uuid}
-                    property-name (merge
-                                   {:block/original-name property-name
-                                    :block/name (gp-util/page-name-sanity-lc property-name)})
-                    property-schema (assoc :block/schema property-schema)
-                    properties (assoc :block/properties
-                                      (merge (:block/properties property)
-                                             properties))
-                    true outliner-core/block-with-updated-at)]
-      (db/transact! repo [tx-data]
-                    {:outliner-op :save-block}))))
+    (let [type (get property [:block/schema :type])]
+      (when-not (and type (:type property-schema) (not= type (:type property-schema))) ; property type changed
+        (when (and (= :many (:cardinality property-schema))
+                   (not= :many (:cardinality (:block/schema property))))
+          ;; cardinality changed from :one to :many
+          (fix-cardinality-many-values! repo property-uuid))
+        (let [tx-data (cond-> {:block/uuid property-uuid}
+                        property-name (merge
+                                       {:block/original-name property-name
+                                        :block/name (gp-util/page-name-sanity-lc property-name)})
+                        property-schema (assoc :block/schema property-schema)
+                        properties (assoc :block/properties
+                                          (merge (:block/properties property)
+                                                 properties))
+                        true outliner-core/block-with-updated-at)]
+          (db/transact! repo [tx-data]
+                        {:outliner-op :save-block}))))))
 
 (defn class-add-property!
   [repo class k-name]

+ 1 - 2
src/main/frontend/handler/property.cljs

@@ -9,8 +9,7 @@
             [frontend.db :as db]
             [frontend.format.block :as block]
             [frontend.db.model :as model]
-            [frontend.modules.outliner.core :as outliner-core]
-            [clojure.string :as string]))
+            [frontend.modules.outliner.core :as outliner-core]))
 
 (defn remove-block-property!
   [repo block-id key]