浏览代码

feat: inherit class

Tienson Qin 2 年之前
父节点
当前提交
bb39eac186
共有 3 个文件被更改,包括 71 次插入5 次删除
  1. 35 2
      src/main/frontend/components/page.cljs
  2. 23 3
      src/main/frontend/components/property.cljs
  3. 13 0
      src/main/frontend/db/model.cljs

+ 35 - 2
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

+ 23 - 3
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)

+ 13 - 0
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