-
Notifications
You must be signed in to change notification settings - Fork 1
/
toc.cljs
63 lines (56 loc) · 2 KB
/
toc.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(ns toc
(:require
[clojure.string]
[roam.block :as block]
[roam.datascript :as d]
[roam.datascript.reactive :as dr]
[roam.util :as u]))
(defn ancestor-block-uids [uid]
(d/q '[:find [?puid ...]
:in $ ?cuid
:where
[?c :block/uid ?cuid]
[?c :block/parents ?p]
[?p :block/string]
[?p :block/uid ?puid]]
uid))
(defn block-html-el [uid]
(some (fn [x]
(when (clojure.string/ends-with? (.-id x) uid) x))
(.getElementsByClassName js/document "rm-block__input")))
(defn try-scroll-into-view [uid]
(when-some [el (block-html-el uid)]
(.scrollIntoView el)))
(defn scroll-to-block [uid]
(doseq [parent (ancestor-block-uids uid)]
(block/update
{:block {:uid parent :open true}}))
(when-not (try-scroll-into-view uid)
(js/setTimeout #(try-scroll-into-view uid) 300)))
(defn flatten-block [acc block]
(reduce flatten-block
(conj acc (dissoc block :block/children))
(sort-by :block/order (:block/children block))))
(defn block->table-entry [block]
[:div {:style {:margin-left (str (dec (:block/heading block)) "em")
:font-size (str (* 0.25 (- 7 (:block/heading block)))
"em")}}
[:a {:on-click #(scroll-to-block (:block/uid block))}
(u/parse (:block/string block))]])
(defn component [{uid :block-uid}]
(let [block @(dr/q '[:find (pull ?p [:block/string
:block/uid
:block/heading
:block/order
{:block/children ...}]) .
:in $ ?uid
:where
[?e :block/uid ?uid]
[?e :block/parents ?p]
[?p :node/title]]
uid)]
(into [:div]
(comp
(filter #(-> % :block/heading pos?))
(map block->table-entry))
(flatten-block [] block))))