Browse Source

enhance: add enum choices to built-in table properties

Also hide all built-in table properties like we do for file graphs
Gabriel Horner 2 years ago
parent
commit
f860553d89

+ 33 - 31
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -112,17 +112,40 @@
     page-attrs
     page-or-block-attrs)))
 
+(def property-schema-attrs
+  [[:hide? {:optional true} :boolean]
+   [:description {:optional true} :string]
+   ;; For any types except for :checkbox :default :template :enum
+   [:cardinality {:optional true} [:enum :one :many]]
+   ;; Just for :enum type
+   [:enum-config {:optional true}
+    [:map
+     [:values
+      [:map-of
+       :uuid [:map
+              [:name :string]
+              [:description :string]
+              [:icon {:optional true}
+               [:map
+                [:id :string]
+                [:name :string]
+                [:type [:enum :tabler-icon :emoji]]]]]]]
+     ;; Optional b/c built-in props don't have it set
+     [:order {:optional true} [:vector :uuid]]]]
+   ;; Just for :enum
+   [:position {:optional true} :string]
+   ;; For :page and :template
+   [:classes {:optional true} [:set [:or :uuid :keyword]]]])
+
 (def internal-property
   (vec
    (concat
     [:map
      [:block/schema
-      [:map
-       [:type (apply vector :enum (into db-property-type/internal-builtin-schema-types
-                                        db-property-type/user-builtin-schema-types))]
-       [:hide? {:optional true} :boolean]
-       [:cardinality {:optional true} [:enum :one :many]]
-       [:classes {:optional true} [:set :keyword]]]]]
+      (into [:map
+             [:type (apply vector :enum (into db-property-type/internal-builtin-schema-types
+                                              db-property-type/user-builtin-schema-types))]]
+            property-schema-attrs)]]
     page-attrs
     page-or-block-attrs)))
 
@@ -132,31 +155,10 @@
     [:map
      [:block/schema
       {:optional true}
-      [:map
-       ;; Once a schema is defined it must have :type as this is an irreversible decision
-       [:type (apply vector :enum db-property-type/user-builtin-schema-types)]
-       [:hide? {:optional true} :boolean]
-       [:description {:optional true} :string]
-       ;; For any types except for :checkbox :default :template :enum
-       [:cardinality {:optional true} [:enum :one :many]]
-       ;; Just for :enum type
-       [:enum-config {:optional true}
-        [:map
-         [:values
-          [:map-of
-           :uuid [:map
-                  [:name :string]
-                  [:description :string]
-                  [:icon {:optional true}
-                   [:map
-                    [:id :string]
-                    [:name :string]
-                    [:type [:enum :tabler-icon :emoji]]]]]]]
-         [:order [:vector :uuid]]]]
-       ;; Just for :enum
-       [:position {:optional true} :string]
-       ;; For :page and :template
-       [:classes {:optional true} [:set [:or :uuid :keyword]]]]]]
+      (into [:map
+             ;; Once a schema is defined it must have :type as this is an irreversible decision
+             [:type (apply vector :enum db-property-type/user-builtin-schema-types)]]
+            property-schema-attrs)]]
     page-attrs
     page-or-block-attrs)))
 

+ 29 - 8
deps/db/src/logseq/db/frontend/property.cljs

@@ -43,22 +43,43 @@
    :logseq.tldraw.page {:schema {:type :map}}
    :logseq.tldraw.shape {:schema {:type :map}}
    ;; color props
-   :logseq.color {:schema {:type :default}
+   :logseq.color {:schema
+                  {:type :enum
+                   :hide? true
+                   :enum-config
+                   {:values
+                    (into {}
+                          (map #(vector (random-uuid) {:name % :description ""})
+                               ["red" "orange" "green" "blue" "purple"]))}}
                   :visible true}
    ;; table-v2 props
-   :logseq.table.version {:schema {:type :number}
+   :logseq.table.version {:schema {:type :number :hide? true}
                           :visible true}
-   :logseq.table.compact {:schema {:type :checkbox}
+   :logseq.table.compact {:schema {:type :checkbox :hide? true}
                           :visible true}
-   :logseq.table.headers {:schema {:type :default}
+   :logseq.table.headers {:schema
+                          {:type :enum
+                           :hide? true
+                           :enum-config
+                           {:values
+                            (into {}
+                                  (map #(vector (random-uuid) {:name % :description ""})
+                                       ["uppercase" "capitalize" "capitalize-first" "lowercase"]))}}
                           :visible true}
-   :logseq.table.hover {:schema {:type :default}
+   :logseq.table.hover {:schema
+                        {:type :enum
+                         :hide? true
+                         :enum-config
+                         {:values
+                          (into {}
+                                (map #(vector (random-uuid) {:name % :description ""})
+                                     ["row" "col" "both" "none"]))}}
                         :visible true}
-   :logseq.table.borders {:schema {:type :checkbox}
+   :logseq.table.borders {:schema {:type :checkbox :hide? true}
                           :visible true}
-   :logseq.table.stripes {:schema {:type :checkbox}
+   :logseq.table.stripes {:schema {:type :checkbox :hide? true}
                           :visible true}
-   :logseq.table.max-width {:schema {:type :number}
+   :logseq.table.max-width {:schema {:type :number :hide? true}
                             :visible true}
 
    :icon {:original-name "Icon"

+ 9 - 3
src/main/frontend/components/query_table.cljs

@@ -298,10 +298,16 @@
       (case table-version
         2 (let [v2-columns (mapv #(if (uuid? %) (pu/get-property-name %) %) columns)
                 v2-config (assoc-in config [:block :properties]
-                                    (update-keys (get-in config [:block :block/properties])
-                                                 #(-> (db/entity (state/get-current-repo) [:block/uuid %])
+                                    (->> (get-in config [:block :block/properties])
+                                         (map (fn [[k v]]
+                                                (let [prop-ent (db/entity [:block/uuid k])]
+                                                  [(-> prop-ent
                                                       :block/name
-                                                      keyword)))
+                                                      keyword)
+                                                  (if (= :enum (get-in prop-ent [:block/schema :type]))
+                                                    (pu/enum-value prop-ent v)
+                                                    v)])))
+                                         (into {})))
                 result-as-text (for [row result]
                                  (for [column columns]
                                    (build-column-text row column)))]

+ 5 - 0
src/main/frontend/handler/property/util.cljs

@@ -61,3 +61,8 @@
                                       db-property/built-in-properties-keys))]
         (swap! *db-built-in-properties assoc repo built-in-properties)))
     (set/subset? (set properties) (get @*db-built-in-properties repo))))
+
+(defn enum-value
+  "Given an enum ent and the value's uuid, return the value's string"
+  [ent value-uuid]
+  (get-in ent [:block/schema :enum-config :values value-uuid :name]))