feat: object type

pull/10016/head
Tienson Qin 2023-08-14 19:10:45 +08:00
parent debde5bdf6
commit 2181525bd8
9 changed files with 67 additions and 27 deletions

View File

@ -721,7 +721,7 @@
(state/clear-edit!)) (state/clear-edit!))
(defmethod handle-step :editor/new-property [[_]] (defmethod handle-step :editor/new-property [[_]]
(property-handler/editing-new-property!)) (state/pub-event! [:editor/new-property]))
(defmethod handle-step :default [[type & _args]] (defmethod handle-step :default [[type & _args]]
(prn "No handler for step: " type)) (prn "No handler for step: " type))

View File

@ -5,6 +5,7 @@
[frontend.components.property.value :as pv] [frontend.components.property.value :as pv]
[frontend.components.select :as select] [frontend.components.select :as select]
[frontend.db :as db] [frontend.db :as db]
[frontend.db.model :as db-model]
[frontend.handler.db-based.property :as db-property] [frontend.handler.db-based.property :as db-property]
[frontend.handler.notification :as notification] [frontend.handler.notification :as notification]
[frontend.handler.property :as property-handler] [frontend.handler.property :as property-handler]
@ -40,6 +41,17 @@
:id id}}})) :id id}}}))
(toggle-fn))}))))) (toggle-fn))})))))
(rum/defc class-select
[*property-schema class]
(let [classes (db-model/get-all-classes (state/get-current-repo))
options (map (fn [[name id]] {:label name
:value id
:selected (= class id)})
classes)]
(ui/select options
(fn [_e value]
(swap! *property-schema assoc :class (str value))))))
(rum/defcs property-config < (rum/defcs property-config <
rum/reactive rum/reactive
(rum/local nil ::property-name) (rum/local nil ::property-name)
@ -89,6 +101,11 @@
(let [type (keyword (string/lower-case v))] (let [type (keyword (string/lower-case v))]
(swap! *property-schema assoc :type type)))))] (swap! *property-schema assoc :type type)))))]
(when (= :object (:type @*property-schema))
[:div.grid.grid-cols-4.gap-1.leading-8
[:label "Choose class:"]
(class-select *property-schema (:class @*property-schema))])
(when-not (= (:type @*property-schema) :checkbox) (when-not (= (:type @*property-schema) :checkbox)
[:div.grid.grid-cols-4.gap-1.items-center.leading-8 [:div.grid.grid-cols-4.gap-1.items-center.leading-8
[:label "Multiple values:"] [:label "Multiple values:"]

View File

@ -82,11 +82,15 @@
"origin-top-right.absolute.left-0.rounded-md.shadow-lg.mt-2")}))) "origin-top-right.absolute.left-0.rounded-md.shadow-lg.mt-2")})))
(defn- select-page (defn- select-page
[block property opts] [block property {:keys [class] :as opts}]
(let [repo (state/get-current-repo) (let [repo (state/get-current-repo)
pages (->> (model/get-all-page-original-names repo) pages (if class
(map (fn [p] {:value p})))] (some->> (:block/name (db/entity [:block/uuid (uuid class)]))
(select/select {:items pages (db/get-tag-pages repo)
(map first))
(model/get-all-page-original-names repo))
options (map (fn [p] {:value p}) pages)]
(select/select {:items options
:dropdown? true :dropdown? true
:on-chosen (fn [chosen] :on-chosen (fn [chosen]
(let [page (string/trim (:value chosen)) (let [page (string/trim (:value chosen))
@ -282,8 +286,9 @@
:as opts}] :as opts}]
(let [property (model/sub-block (:db/id property)) (let [property (model/sub-block (:db/id property))
repo (state/get-current-repo) repo (state/get-current-repo)
type (:type (:block/schema property)) schema (:block/schema property)
multiple-values? (= :many (:cardinality (:block/schema property))) type (:type schema)
multiple-values? (= :many (:cardinality schema))
editor-id (or editor-id (str "ls-property-" blocks-container-id "-" (:db/id block) "-" (:db/id property))) editor-id (or editor-id (str "ls-property-" blocks-container-id "-" (:db/id block) "-" (:db/id property)))
editing? (or editing? (state/sub [:editor/editing? editor-id])) editing? (or editing? (state/sub [:editor/editing? editor-id]))
select-opts {:on-chosen (fn [] select-opts {:on-chosen (fn []
@ -309,6 +314,9 @@
(list :number :url) (list :number :url)
[:div.h-6 (select block property select-opts)] [:div.h-6 (select block property select-opts)]
:object
[:div.h-6 (select-page block property (assoc select-opts :class (:class schema)))]
:page :page
[:div.h-6 (select-page block property select-opts)] [:div.h-6 (select-page block property select-opts)]
@ -326,9 +334,9 @@
:class class :class class
:style {:min-height 24} :style {:min-height 24}
:on-click (fn [] :on-click (fn []
(let [page-or-block? (contains? #{:page :block} type)] (let [ref? (contains? #{:page :block :object} type)]
(when (or (not page-or-block?) (when (or (not ref?)
(and (string/blank? value) page-or-block?)) (and (string/blank? value) ref?))
(set-editing! property editor-id dom-id value))))} (set-editing! property editor-id dom-id value))))}
(let [type (if (and (= type :default) (uuid? value)) (let [type (if (and (= type :default) (uuid? value))
(if-let [e (db/entity [:block/uuid value])] (if-let [e (db/entity [:block/uuid value])]
@ -338,6 +346,10 @@
(if (string/blank? value) (if (string/blank? value)
[:div.opacity-50.text-sm "Input something"] [:div.opacity-50.text-sm "Input something"]
(case type (case type
:object
(when-let [object (db/entity [:block/uuid value])]
(page-cp {} object))
:page :page
(when-let [page (db/entity [:block/uuid value])] (when-let [page (db/entity [:block/uuid value])]
(page-cp {} page)) (page-cp {} page))
@ -421,15 +433,14 @@
*add-new-item? (::add-new-item? state) *add-new-item? (::add-new-item? state)
type (:type schema) type (:type schema)
block? (= type :block) block? (= type :block)
page? (= type :page)
default? (= type :default) default? (= type :default)
row? (contains? #{:page} type) row? (contains? #{:page :object} type)
items (if (coll? v) v (when v [v]))] items (if (coll? v) v (when v [v]))]
[:div.relative [:div.relative
{:class (cond {:class (cond
row? row?
(cond-> "flex flex-1 flex-row items-center flex-wrap" (cond-> "flex flex-1 flex-row items-center flex-wrap"
page? row?
(str " gap-2")) (str " gap-2"))
block? block?
"grid" "grid"
@ -470,7 +481,7 @@
[:div.rounded-sm {:on-click (fn [] (reset! *add-new-item? true))} [:div.rounded-sm {:on-click (fn [] (reset! *add-new-item? true))}
[:div.opacity-50.text-sm "Input something"]] [:div.opacity-50.text-sm "Input something"]]
(and @*show-add? page?) (and @*show-add? row?)
[:a.add-button-link.flex [:a.add-button-link.flex
{:on-click (fn [] (reset! *add-new-item? true))} {:on-click (fn [] (reset! *add-new-item? true))}
(ui/icon "circle-plus")] (ui/icon "circle-plus")]

View File

@ -1505,11 +1505,12 @@ independent of format as format specific heading characters are stripped"
(defn get-all-classes (defn get-all-classes
[repo] [repo]
(d/q (d/q
'[:find [?name ...] '[:find ?name ?id
:where :where
[?page :block/type ?t] [?page :block/type ?t]
[(= ?t "class")] [(= ?t "class")]
[?page :block/original-name ?name]] [?page :block/original-name ?name]
[?page :block/uuid ?id]]
(conn/get-db repo))) (conn/get-db repo)))
(comment (comment

View File

@ -30,7 +30,7 @@
[id] [id]
(and (uuid? id) (and (uuid? id)
(when-let [e (db/entity [:block/uuid id])] (when-let [e (db/entity [:block/uuid id])]
(seq (:block/class e))))) (seq (:block/tags e)))))
(def builtin-schema-types (def builtin-schema-types
{:default string? ; refs/tags will not be extracted {:default string? ; refs/tags will not be extracted

View File

@ -3383,7 +3383,7 @@
(let [config (last (state/get-editor-args))] (let [config (last (state/get-editor-args))]
(:ref? config))) (:ref? config)))
(defn- set-blocks-collapsed! (defn set-blocks-collapsed!
[block-ids value] [block-ids value]
(let [block-ids (map (fn [block-id] (if (string? block-id) (uuid block-id) block-id)) block-ids) (let [block-ids (map (fn [block-id] (if (string? block-id) (uuid block-id) block-id)) block-ids)
repo (state/get-current-repo) repo (state/get-current-repo)

View File

@ -25,7 +25,6 @@
[frontend.components.user.login :as login] [frontend.components.user.login :as login]
[frontend.components.shortcut :as shortcut] [frontend.components.shortcut :as shortcut]
[frontend.components.repo :as repo] [frontend.components.repo :as repo]
[frontend.components.page :as page]
[frontend.config :as config] [frontend.config :as config]
[frontend.context.i18n :refer [t]] [frontend.context.i18n :refer [t]]
[frontend.db :as db] [frontend.db :as db]
@ -54,6 +53,7 @@
[frontend.handler.ui :as ui-handler] [frontend.handler.ui :as ui-handler]
[frontend.handler.user :as user-handler] [frontend.handler.user :as user-handler]
[frontend.handler.property.util :as pu] [frontend.handler.property.util :as pu]
[frontend.handler.property :as property-handler]
[frontend.handler.whiteboard :as whiteboard-handler] [frontend.handler.whiteboard :as whiteboard-handler]
[frontend.handler.web.nfs :as nfs-handler] [frontend.handler.web.nfs :as nfs-handler]
[frontend.mobile.core :as mobile] [frontend.mobile.core :as mobile]
@ -982,6 +982,16 @@
(when-let [blocks (and block (db-model/get-block-immediate-children (state/get-current-repo) (:block/uuid block)))] (when-let [blocks (and block (db-model/get-block-immediate-children (state/get-current-repo) (:block/uuid block)))]
(editor-handler/toggle-blocks-as-own-order-list! blocks))) (editor-handler/toggle-blocks-as-own-order-list! blocks)))
(defmethod handle :editor/new-property [[_]]
(when-let [edit-block (state/get-edit-block)]
(when-let [block-id (:block/uuid edit-block)]
(let [block (db/entity [:block/uuid block-id])
collapsed? (or (get-in @state/state [:ui/collapsed-blocks (state/get-current-repo) block-id])
(:block/collapsed? block))]
(when collapsed?
(editor-handler/set-blocks-collapsed! [block-id] false)))))
(property-handler/editing-new-property!))
(defn run! (defn run!
[] []
(let [chan (state/get-events-chan)] (let [chan (state/get-events-chan)]

View File

@ -4,7 +4,8 @@
[frontend.handler.file-based.property :as file-property] [frontend.handler.file-based.property :as file-property]
[frontend.config :as config] [frontend.config :as config]
[frontend.state :as state] [frontend.state :as state]
[frontend.db :as db])) [frontend.db :as db]
[frontend.modules.outliner.core :as outliner-core]))
(def user-face-builtin-schema-types db-property/user-face-builtin-schema-types) (def user-face-builtin-schema-types db-property/user-face-builtin-schema-types)
(def internal-builtin-schema-types db-property/internal-builtin-schema-types) (def internal-builtin-schema-types db-property/internal-builtin-schema-types)

View File

@ -896,10 +896,10 @@
[:option (cond-> [:option (cond->
{:key label {:key label
:value (or value label)} ;; NOTE: value might be an empty string, `or` is safe here :value (or value label)} ;; NOTE: value might be an empty string, `or` is safe here
disabled disabled
(assoc :disabled disabled) (assoc :disabled disabled)
selected selected
(assoc :selected selected)) (assoc :selected selected))
label])])) label])]))
(rum/defc radio-list (rum/defc radio-list