Browse Source

feat: schema supports types, cardinality, description and number range

Tienson Qin 3 năm trước cách đây
mục cha
commit
00adf560d1

+ 3 - 0
deps/db/src/logseq/db/schema.cljs

@@ -64,6 +64,9 @@
    ;; TODO: remove this for EDN graphs
    :block/properties-text-values {}
 
+   ;; block property schema
+   :block/property-schema {}
+
    ;; TODO: EDN version doesn't need this, we probably need multiple schemas for
    ;; different formats
    ;; first block that's not a heading or unordered list

+ 93 - 44
src/main/frontend/components/property/schema.cljs

@@ -14,7 +14,8 @@
             [frontend.components.search.highlight :as highlight]
             [frontend.components.svg :as svg]
             [frontend.modules.shortcut.core :as shortcut]
-            [frontend.context.i18n :refer [t]]))
+            [frontend.context.i18n :refer [t]]
+            [frontend.handler.notification :as notification]))
 
 (rum/defc property-item
   [k value]
@@ -22,52 +23,100 @@
    [:label.col-span-1.font-medium.py-2 k]
    value])
 
-(rum/defc schema
-  [entity]
-  [:div.property-schema
-   ;; type
-   (property-item (t :schema/type)
-                  (ui/select
-                    [{:label "Text" :value "text"}
-                     {:label "Number" :value "number"}
-                     {:label "Date" :value "date"}
-                     {:label "Choice" :value "choice"}
-                     {:label "Url" :value "url"}
-                     {:label "Object" :value "object"}
-                     {:label "Any" :value "any"}]
-                    (fn [selected]
-                      (prn "selected: " selected))
-                    "form-select w-32 sm:w-32"))
+(rum/defc schema-type
+  [entity schema]
+  (let [schema-type (:type schema)
+        options (map
+                  (fn [item] (if (= schema-type (:value item))
+                               (assoc item :selected true)
+                               item))
+                  [{:label "Text" :value "text"}
+                   {:label "Number" :value "number"}
+                   {:label "Date" :value "date"}
+                   {:label "Choice" :value "choice"}
+                   {:label "Url" :value "url"}
+                   {:label "Object" :value "object"}
+                   {:label "Any" :value "any"}])]
+    (property-item
+     (t :schema/type)
+     (ui/select options
+       (fn [selected]
+         (property-handler/set-property-schema! entity :type (string/lower-case selected)))
+       "form-select w-32 sm:w-32"))))
 
-   ;; icon
+(rum/defc schema-number-range
+  [entity schema]
+  (when (= (:type schema) "number")
+    (let [on-submitted (fn [key]
+                         (fn [e]
+                           (if-let [result (parse-long (util/evalue e))]
+                             (property-handler/set-property-schema! entity key result)
+                             (notification/show!
+                              [:div "A number is needed"]
+                              :warning))))]
+      (property-item
+       (t :schema/number-range)
+       [:div.flex.flex-row.items-center
+        [:input.form-input.simple-input.block.focus:outline-none.col-span-1
+         {:placeholder "Min"
+          :default-value (:min schema)
+          :on-blur (on-submitted :min)
+          :on-key-up (fn [e]
+                       (case (util/ekey e)
+                         "Enter"
+                         ((on-submitted :min) e)
+                         nil))}]
+        [:div.px-4 "-"]
+        [:input.form-input.simple-input.block.focus:outline-none.col-span-1
+         {:placeholder "Max"
+          :default-value (:max schema)
+          :on-blur (on-submitted :max)
+          :on-key-up (fn [e]
+                       (case (util/ekey e)
+                         "Enter"
+                         ((on-submitted :max) e)
+                         nil))}]]))))
 
-   ;; cardinality
-   (property-item
-    (t :schema/multiple-values)
-    (ui/checkbox
-     {:checked true
-      :on-change (fn [event]
-                   (prn "on changed"))}))
+(rum/defc schema-cardinality
+  [entity schema]
+  (let [multiple? (boolean (:multiple-values? schema))]
+    (property-item
+     (t :schema/multiple-values)
+     (ui/checkbox
+      {:checked multiple?
+       :on-change (fn []
+                    (property-handler/set-property-schema! entity :multiple-values? (not multiple?)))}))))
 
-   ;; description
-   (property-item
-    (t :schema/description)
-    [:input.form-input.simple-input.block.focus:outline-none.col-span-4
-     {:placeholder "Optional"
-      :on-blur (fn [e]
-                 (prn "on-blur: " (util/evalue e))
-                 )
-      :on-key-up (fn [e]
-                   (prn "on-key-up: " (util/evalue e))
-                   (case (util/ekey e)
-                     "Enter"
-                     (prn "save")
+(rum/defc schema-description
+  [entity schema]
+  (property-item
+   (t :schema/description)
+   (let [description (:description schema)
+         on-submitted (fn [e]
+                        (let [desc (util/evalue e)]
+                          (when-not (string/blank? desc)
+                            (property-handler/set-property-schema! entity :description desc))))]
+     [:input.form-input.simple-input.block.focus:outline-none.col-span-4
+      {:placeholder "Optional"
+       :default-value description
+       :on-blur on-submitted
+       :on-key-up (fn [e]
+                    (case (util/ekey e)
+                      "Enter"
+                      (on-submitted e)
 
-                     "Escape"
-                     (prn "clear")
+                      nil))}])))
 
-                     nil))}])
+(rum/defc schema
+  [entity]
+  (let [schema (:block/property-schema entity)]
+    [:div.property-schema
+     (schema-type entity schema)
+     (schema-number-range entity schema)
+     (schema-cardinality entity schema)
+     (schema-description entity schema)
 
-   ;; predicates
-   ;; integrations
-   ])
+     ;; TODO: icon
+     ;; predicates
+     ;; integrations
+     ]))

+ 1 - 0
src/main/frontend/dicts.cljc

@@ -248,6 +248,7 @@
         :settings-page/sync "Sync"
         :settings-page/enable-whiteboards "Whiteboards"
         :schema/type "Schema type"
+        :schema/number-range "Number range"
         :schema/multiple-values "Multiple values?"
         :schema/description "Description"
         :logseq "Logseq"

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

@@ -48,9 +48,13 @@
         [{:db/id page-db-id
           :block/namespace (:db/id page)}]))))
 
+;; TODO spec
 (defn set-property-schema!
-  [entity key]
-  )
+  [entity key value]
+  (let [schema (assoc (:block/property-schema entity) key value)]
+    (db/transact! (state/get-current-repo)
+      [{:db/id (:db/id entity)
+        :block/property-schema schema}])))
 
 (defn- extract-refs
   [entity properties]