From bb39eac186d3ff1cd2cc88d5cc27b6b008618a53 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 17 Aug 2023 20:13:46 +0800 Subject: [PATCH] feat: inherit class --- src/main/frontend/components/page.cljs | 37 ++++++++++++++++++++-- src/main/frontend/components/property.cljs | 26 +++++++++++++-- src/main/frontend/db/model.cljs | 13 ++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/main/frontend/components/page.cljs b/src/main/frontend/components/page.cljs index 614676ff6..44646d54e 100644 --- a/src/main/frontend/components/page.cljs +++ b/src/main/frontend/components/page.cljs @@ -406,6 +406,28 @@ "control-show cursor-pointer" "control-hide")} (ui/rotating-arrow @*all-collapsed?)]]) +(rum/defc class-select + [page class on-select] + (let [repo (state/get-current-repo) + children-pages (model/get-namespace-children repo (:db/id page)) + exclude-ids (-> (set (map (fn [id] (:block/uuid (db/entity id))) children-pages)) + (conj (:block/uuid page))) ; break cycle + classes (->> (model/get-all-classes repo) + (remove (fn [[_name id]] (contains? exclude-ids id)))) + options (map (fn [[name id]] {:label name + :value id + :selected (= class id)}) + classes) + options (if class options (cons + {:label "Choose parent page" + :disabled true + :selected true + :value ""} + options))] + (ui/select options + (fn [_e value] + (on-select value))))) + (rum/defcs configure < rum/reactive [state page opts] (let [page-id (:db/id page) @@ -422,14 +444,25 @@ [:div [:div.structured-schema ;; properties - [:h2.text-lg.font-medium.mb-2 "Schema:"] + [:p.font-medium.mb-2 "Properties:"] [:div.grid.gap-1 (let [edit-input-id (str "edit-block-" (:block/uuid page) "-schema")] (component-block/db-properties-cp {:editor-box editor/box} page edit-input-id - (assoc properties-opts :class-schema? true)))]] + (assoc properties-opts :class-schema? true)))] + + [:div.flex.flex-row.items-center.flex-wrap.gap-2 + [:div.font-medium.my-4 "Parent:"] + (let [namespace (some-> (:db/id (:block/namespace page)) + db/entity + :block/uuid)] + [:div.w-60 + (class-select page namespace (fn [value] + (db/transact! + [{:db/id (:db/id page) + :block/namespace [:block/uuid (uuid value)]}])))])]] (when (seq (:block/properties page)) [:div.my-4 diff --git a/src/main/frontend/components/property.cljs b/src/main/frontend/components/property.cljs index e7b11055c..4dd1dc62d 100644 --- a/src/main/frontend/components/property.cljs +++ b/src/main/frontend/components/property.cljs @@ -325,6 +325,23 @@ (db/sub-block (:db/id linked-block)) (db/sub-block (:db/id block)))) +(defn- get-namespace-properties + [tags] + (let [tags' (filter (fn [tag] (= "class" (:block/type tag))) tags) + *namespaces (atom #{})] + (doseq [tag tags'] + (when-let [ns (:block/namespace tag)] + (loop [current-ns ns] + (when (and + current-ns + (= "class" (:block/type ns)) + (not (contains? @*namespaces (:db/id ns)))) + (swap! *namespaces conj current-ns) + (recur (:block/namespace current-ns)))))) + (mapcat + (fn [e] (:properties (:block/schema e))) + @*namespaces))) + (rum/defcs properties-area < rum/reactive {:init (fn [state] (assoc state ::blocks-container-id (or (:blocks-container-id (last (:rum/args state))) @@ -346,15 +363,18 @@ (when (= "class" (:block/type tag)) (let [e (db/entity (:db/id tag))] (:properties (:block/schema e)))))) - (map (fn [id] - [id nil]))) + (map (fn [id] [id nil]))) + namespace-properties (->> (:block/tags block) + (get-namespace-properties) + (map (fn [id] [id nil]))) built-in-properties (set/difference (set (map name gp-property/db-built-in-properties-keys)) #{"alias" "tags"}) properties (->> (concat (seq tags-properties) (seq alias-properties) (seq properties) - class-properties) + class-properties + namespace-properties) (util/distinct-by first) (remove (fn [[k _v]] (when (uuid? k) diff --git a/src/main/frontend/db/model.cljs b/src/main/frontend/db/model.cljs index 831e7aa58..2229f8af5 100644 --- a/src/main/frontend/db/model.cljs +++ b/src/main/frontend/db/model.cljs @@ -1514,6 +1514,19 @@ independent of format as format specific heading characters are stripped" [?page :block/uuid ?id]] (conn/get-db repo))) +(defn get-namespace-children + [repo-url eid] + (->> + (d/q '[:find ?children + :in $ ?parent % + :where + (namespace ?parent ?children)] + (conn/get-db repo-url) + eid + (:namespace rules/rules)) + db-utils/seq-flatten + (set))) + (comment ;; For debugging (defn get-all-blocks