From 2181525bd815712095ee905cc23dea9e18d06ff7 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 14 Aug 2023 19:10:45 +0800 Subject: [PATCH] feat: object type --- src/main/frontend/commands.cljs | 2 +- src/main/frontend/components/property.cljs | 17 +++++++++ .../frontend/components/property/value.cljs | 37 ++++++++++++------- src/main/frontend/db/model.cljs | 11 +++--- .../frontend/handler/db_based/property.cljs | 2 +- src/main/frontend/handler/editor.cljs | 2 +- src/main/frontend/handler/events.cljs | 12 +++++- src/main/frontend/handler/property.cljs | 3 +- src/main/frontend/ui.cljs | 8 ++-- 9 files changed, 67 insertions(+), 27 deletions(-) diff --git a/src/main/frontend/commands.cljs b/src/main/frontend/commands.cljs index 47ed91194..5fa06a34a 100644 --- a/src/main/frontend/commands.cljs +++ b/src/main/frontend/commands.cljs @@ -721,7 +721,7 @@ (state/clear-edit!)) (defmethod handle-step :editor/new-property [[_]] - (property-handler/editing-new-property!)) + (state/pub-event! [:editor/new-property])) (defmethod handle-step :default [[type & _args]] (prn "No handler for step: " type)) diff --git a/src/main/frontend/components/property.cljs b/src/main/frontend/components/property.cljs index 48dbbcbcc..36c7f022a 100644 --- a/src/main/frontend/components/property.cljs +++ b/src/main/frontend/components/property.cljs @@ -5,6 +5,7 @@ [frontend.components.property.value :as pv] [frontend.components.select :as select] [frontend.db :as db] + [frontend.db.model :as db-model] [frontend.handler.db-based.property :as db-property] [frontend.handler.notification :as notification] [frontend.handler.property :as property-handler] @@ -40,6 +41,17 @@ :id id}}})) (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/reactive (rum/local nil ::property-name) @@ -89,6 +101,11 @@ (let [type (keyword (string/lower-case v))] (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) [:div.grid.grid-cols-4.gap-1.items-center.leading-8 [:label "Multiple values:"] diff --git a/src/main/frontend/components/property/value.cljs b/src/main/frontend/components/property/value.cljs index 3e11b4ef0..3ba61db67 100644 --- a/src/main/frontend/components/property/value.cljs +++ b/src/main/frontend/components/property/value.cljs @@ -82,11 +82,15 @@ "origin-top-right.absolute.left-0.rounded-md.shadow-lg.mt-2")}))) (defn- select-page - [block property opts] + [block property {:keys [class] :as opts}] (let [repo (state/get-current-repo) - pages (->> (model/get-all-page-original-names repo) - (map (fn [p] {:value p})))] - (select/select {:items pages + pages (if class + (some->> (:block/name (db/entity [:block/uuid (uuid class)])) + (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 :on-chosen (fn [chosen] (let [page (string/trim (:value chosen)) @@ -282,8 +286,9 @@ :as opts}] (let [property (model/sub-block (:db/id property)) repo (state/get-current-repo) - type (:type (:block/schema property)) - multiple-values? (= :many (:cardinality (:block/schema property))) + schema (: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))) editing? (or editing? (state/sub [:editor/editing? editor-id])) select-opts {:on-chosen (fn [] @@ -309,6 +314,9 @@ (list :number :url) [:div.h-6 (select block property select-opts)] + :object + [:div.h-6 (select-page block property (assoc select-opts :class (:class schema)))] + :page [:div.h-6 (select-page block property select-opts)] @@ -326,9 +334,9 @@ :class class :style {:min-height 24} :on-click (fn [] - (let [page-or-block? (contains? #{:page :block} type)] - (when (or (not page-or-block?) - (and (string/blank? value) page-or-block?)) + (let [ref? (contains? #{:page :block :object} type)] + (when (or (not ref?) + (and (string/blank? value) ref?)) (set-editing! property editor-id dom-id value))))} (let [type (if (and (= type :default) (uuid? value)) (if-let [e (db/entity [:block/uuid value])] @@ -338,6 +346,10 @@ (if (string/blank? value) [:div.opacity-50.text-sm "Input something"] (case type + :object + (when-let [object (db/entity [:block/uuid value])] + (page-cp {} object)) + :page (when-let [page (db/entity [:block/uuid value])] (page-cp {} page)) @@ -421,15 +433,14 @@ *add-new-item? (::add-new-item? state) type (:type schema) block? (= type :block) - page? (= type :page) default? (= type :default) - row? (contains? #{:page} type) + row? (contains? #{:page :object} type) items (if (coll? v) v (when v [v]))] [:div.relative {:class (cond row? (cond-> "flex flex-1 flex-row items-center flex-wrap" - page? + row? (str " gap-2")) block? "grid" @@ -470,7 +481,7 @@ [:div.rounded-sm {:on-click (fn [] (reset! *add-new-item? true))} [:div.opacity-50.text-sm "Input something"]] - (and @*show-add? page?) + (and @*show-add? row?) [:a.add-button-link.flex {:on-click (fn [] (reset! *add-new-item? true))} (ui/icon "circle-plus")] diff --git a/src/main/frontend/db/model.cljs b/src/main/frontend/db/model.cljs index 72e893ce0..4e204c677 100644 --- a/src/main/frontend/db/model.cljs +++ b/src/main/frontend/db/model.cljs @@ -1505,11 +1505,12 @@ independent of format as format specific heading characters are stripped" (defn get-all-classes [repo] (d/q - '[:find [?name ...] - :where - [?page :block/type ?t] - [(= ?t "class")] - [?page :block/original-name ?name]] + '[:find ?name ?id + :where + [?page :block/type ?t] + [(= ?t "class")] + [?page :block/original-name ?name] + [?page :block/uuid ?id]] (conn/get-db repo))) (comment diff --git a/src/main/frontend/handler/db_based/property.cljs b/src/main/frontend/handler/db_based/property.cljs index 94e65abe4..8b71b7cac 100644 --- a/src/main/frontend/handler/db_based/property.cljs +++ b/src/main/frontend/handler/db_based/property.cljs @@ -30,7 +30,7 @@ [id] (and (uuid? id) (when-let [e (db/entity [:block/uuid id])] - (seq (:block/class e))))) + (seq (:block/tags e))))) (def builtin-schema-types {:default string? ; refs/tags will not be extracted diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 86b300a78..62271be7d 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -3383,7 +3383,7 @@ (let [config (last (state/get-editor-args))] (:ref? config))) -(defn- set-blocks-collapsed! +(defn set-blocks-collapsed! [block-ids value] (let [block-ids (map (fn [block-id] (if (string? block-id) (uuid block-id) block-id)) block-ids) repo (state/get-current-repo) diff --git a/src/main/frontend/handler/events.cljs b/src/main/frontend/handler/events.cljs index 360d8ca2b..2ed1bcafa 100644 --- a/src/main/frontend/handler/events.cljs +++ b/src/main/frontend/handler/events.cljs @@ -25,7 +25,6 @@ [frontend.components.user.login :as login] [frontend.components.shortcut :as shortcut] [frontend.components.repo :as repo] - [frontend.components.page :as page] [frontend.config :as config] [frontend.context.i18n :refer [t]] [frontend.db :as db] @@ -54,6 +53,7 @@ [frontend.handler.ui :as ui-handler] [frontend.handler.user :as user-handler] [frontend.handler.property.util :as pu] + [frontend.handler.property :as property-handler] [frontend.handler.whiteboard :as whiteboard-handler] [frontend.handler.web.nfs :as nfs-handler] [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)))] (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! [] (let [chan (state/get-events-chan)] diff --git a/src/main/frontend/handler/property.cljs b/src/main/frontend/handler/property.cljs index 6b4c599b8..891f7acd0 100644 --- a/src/main/frontend/handler/property.cljs +++ b/src/main/frontend/handler/property.cljs @@ -4,7 +4,8 @@ [frontend.handler.file-based.property :as file-property] [frontend.config :as config] [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 internal-builtin-schema-types db-property/internal-builtin-schema-types) diff --git a/src/main/frontend/ui.cljs b/src/main/frontend/ui.cljs index 83a438d55..23f521614 100644 --- a/src/main/frontend/ui.cljs +++ b/src/main/frontend/ui.cljs @@ -896,10 +896,10 @@ [:option (cond-> {:key label :value (or value label)} ;; NOTE: value might be an empty string, `or` is safe here - disabled - (assoc :disabled disabled) - selected - (assoc :selected selected)) + disabled + (assoc :disabled disabled) + selected + (assoc :selected selected)) label])])) (rum/defc radio-list