瀏覽代碼

feat: add {{namespace [[page]]}} macro which can be used as a toc

Tienson Qin 3 年之前
父節點
當前提交
8d605afa9f

+ 1 - 0
resources/icons.edn

@@ -1,3 +1,4 @@
+;; https://tabler-icons.io/
 ["Icon2fa"
  "Icon3dCubeSphere"
  "IconAB"

+ 41 - 16
src/main/frontend/components/block.cljs

@@ -238,18 +238,18 @@
           [:a.delete.ml-1
            {:title    "maximize image"
             :on-click (fn [^js e] (let [images (js/document.querySelectorAll ".asset-container img")
-                                        images (to-array images)
-                                        images (if-not (= (count images) 1)
-                                                 (let [^js _image (.closest (.-target e) ".asset-container")
-                                                       image (. _image querySelector "img")]
-                                                   (cons image (remove #(= image %) images)))
-                                                 images)
-                                        images (for [^js it images] {:src (.-src it)
-                                                                     :w (.-naturalWidth it)
-                                                                     :h (.-naturalHeight it)})]
-
-                                    (when (seq images)
-                                      (lightbox/preview-images! images))))}
+                                       images (to-array images)
+                                       images (if-not (= (count images) 1)
+                                                (let [^js _image (.closest (.-target e) ".asset-container")
+                                                      image (. _image querySelector "img")]
+                                                  (cons image (remove #(= image %) images)))
+                                                images)
+                                       images (for [^js it images] {:src (.-src it)
+                                                                    :w (.-naturalWidth it)
+                                                                    :h (.-naturalHeight it)})]
+
+                                   (when (seq images)
+                                     (lightbox/preview-images! images))))}
 
            (svg/maximize)]]])))))
 
@@ -802,6 +802,24 @@
         (join (config/get-repo-dir (state/get-current-repo))
               (config/get-local-asset-absolute-path path)))))
 
+(rum/defc namespace-hierarchy-aux
+  [config namespace children]
+  [:ul
+   (for [child children]
+     [:li {:key (str "namespace-" namespace "-" (:db/id child))}
+      (page-cp config child)
+      (when (seq (:namespace/children child))
+        (namespace-hierarchy-aux config (:block/name child)
+                                 (:namespace/children child)))])])
+
+(rum/defc namespace-hierarchy
+  [config namespace children]
+  [:div.namespace
+   [:div.font-medium.flex.flex-row.items-center.pb-2
+    [:span.text-sm.mr-1 "Namespace "]
+    (page-cp config {:block/name namespace})]
+   (namespace-hierarchy-aux config namespace children)])
+
 (defn inline
   [{:keys [html-export?] :as config} item]
   (match item
@@ -1130,6 +1148,13 @@
          [:span.warning
           (util/format "{{function %s}}" (first arguments))])
 
+        (= name "namespace")
+        (let [namespace (first arguments)]
+          (when-not (string/blank? namespace)
+            (let [namespace (string/lower-case (text/page-ref-un-brackets! namespace))
+                  children (model/get-namespace-hierarchy (state/get-current-repo) namespace)]
+              (namespace-hierarchy config namespace children))))
+
         (= name "youtube")
         (when-let [url (first arguments)]
           (let [YouTube-regex #"^((?:https?:)?//)?((?:www|m).)?((?:youtube.com|youtu.be))(/(?:[\w-]+\?v=|embed/|v/)?)([\w-]+)(\S+)?$"]
@@ -1376,9 +1401,9 @@
             (when (map? child)
               (let [child (dissoc child :block/meta)
                     config (cond->
-                            (-> config
-                                (assoc :block/uuid (:block/uuid child))
-                                (dissoc :breadcrumb-show? :embed-parent))
+                             (-> config
+                                 (assoc :block/uuid (:block/uuid child))
+                                 (dissoc :breadcrumb-show? :embed-parent))
                              ref?
                              (assoc :ref-child? true))]
                 (rum/with-key (block-container config child)
@@ -2377,7 +2402,7 @@
         states (filter #(not (string/starts-with? % "CLOCK:")) log)]
     (when (seq clocks)
       (let [tr (fn [elm cols] (->elem :tr
-                                      (mapv (fn [col] (->elem elm col)) cols)))
+                                     (mapv (fn [col] (->elem elm col)) cols)))
             head  [:thead.overflow-x-scroll (tr :th.py-0 ["Type" "Start" "End" "Span"])]
             clock-tbody (->elem
                          :tbody.overflow-scroll.sm:overflow-auto

+ 0 - 1
src/main/frontend/components/hierarchy.cljs

@@ -17,7 +17,6 @@
           (map (fn [page]
                  (or (:block/original-name page) (:block/name page))))
           (map #(string/split % #"/"))
-          (remove #(= % [page]))
           (sort)))))
 
 (rum/defc structures

+ 30 - 15
src/main/frontend/db/model.cljs

@@ -1428,21 +1428,36 @@
 (defn get-namespace-pages
   [repo namespace]
   (assert (string? namespace))
-  (when-let [db (conn/get-conn repo)]
-    (when-not (string/blank? namespace)
-      (let [namespace (string/lower-case (string/trim namespace))
-            ids (->> (d/datoms db :aevt :block/name)
-                     (filter (fn [datom]
-                               (let [page (:v datom)]
-                                 (or
-                                  (= page namespace)
-                                  (string/starts-with? page (str namespace "/"))))))
-                     (map :e))]
-        (when (seq ids)
-          (db-utils/pull-many repo
-                              '[:db/id :block/name :block/original-name
-                                {:block/file [:db/id :file/path]}]
-                              ids))))))
+  (let [namespace (string/lower-case namespace)]
+    (d/q
+      '[:find [(pull ?c [:db/id :block/name :block/original-name
+                         :block/namespace
+                         {:block/file [:db/id :file/path]}]) ...]
+        :in $ % ?namespace
+        :where
+        (namespace ?p ?c)
+        [?p :block/name ?namespace]]
+      (conn/get-conn repo)
+      rules
+      namespace)))
+
+(defn- tree [flat-col root]
+  (let [sort-fn #(sort-by :block/name %)
+        children (group-by :block/namespace flat-col)
+        namespace-children (fn namespace-children [parent-id]
+                             (map (fn [m]
+                                    (assoc m :namespace/children
+                                           (sort-fn (namespace-children {:db/id (:db/id m)}))))
+                               (sort-fn (get children parent-id))))]
+    (namespace-children root)))
+
+(defn get-namespace-hierarchy
+  [repo namespace]
+  (let [children (get-namespace-pages repo namespace)
+        namespace-id (:db/id (db-utils/entity [:block/name (string/lower-case namespace)]))
+        root {:db/id namespace-id}
+        col (conj children root)]
+    (tree col root)))
 
 (defn get-latest-changed-pages
   [repo]