enhance: block expand/collapse toggle

1. Display `Expand all` where there're already collapsed blocks. This applies to `Collapse all` too.
2. Make the guideline bold when hovering on it.
3. Simplify some logic
This commit is contained in:
Tienson Qin
2021-12-26 13:13:21 +08:00
committed by Andelf
parent f54326f24e
commit 1cff06f347
5 changed files with 104 additions and 88 deletions

View File

@@ -3306,13 +3306,14 @@
(state/set-edit-content! (state/get-edit-input-id) (.-value input))))
(defn collapsable? [block-id]
(if-let [block (db-model/query-block-by-uuid block-id)]
(let [block (block/parse-title-and-body block)]
(and
(nil? (-> block :block/properties :collapsed))
(or (not-empty (:block/body block))
(db-model/has-children? block-id))))
false))
(when block-id
(if-let [block (db-model/query-block-by-uuid block-id)]
(let [block (block/parse-title-and-body block)]
(and
(nil? (-> block :block/properties :collapsed))
(or (not-empty (:block/body block))
(db-model/has-children? block-id))))
false)))
(defn all-blocks-with-level
"Return all blocks associated with correct level
@@ -3333,29 +3334,30 @@
[{:keys [collapse? expanded? root-block] :or {collapse? false expanded? false root-block nil}}]
(when-let [page (or (state/get-current-page)
(date/today))]
(->>
(-> page
(db/get-page-blocks-no-cache)
(tree/blocks->vec-tree page))
(let [blocks (-> page
(db/get-page-blocks-no-cache)
(tree/blocks->vec-tree page))]
(cond->> blocks
root-block
(map (fn find [root]
(if (= root-block (:block/uuid root))
root
(first (filter find (:block/children root []))))))
(#(cond->> %
root-block (map (fn find [root]
(if (= root-block (:block/uuid root))
root
(first (filter find (:block/children root []))))))))
collapse?
(w/postwalk
(fn [b]
(if (and (map? b) (-> b :block/properties :collapsed))
(assoc b :block/children []) b)))
(#(cond->> %
collapse? (w/postwalk
(fn [x]
(if (and (map? x) (-> x :block/properties :collapsed))
(assoc x :block/children []) x)))))
true
(mapcat (fn [x] (tree-seq map? :block/children x)))
(mapcat (fn [x] (tree-seq map? :block/children x)))
expanded?
(filter (fn [b] (collapsable? (:block/uuid b))))
(#(cond->> %
expanded? (filter (fn [b] (collapsable? (:block/uuid b))))))
(map (fn [x] (dissoc x :block/children))))))
true
(map (fn [x] (dissoc x :block/children)))))))
(defn collapse-block! [block-id]
(when (collapsable? block-id)
@@ -3440,9 +3442,8 @@
(collapse-all! nil))
([block-id]
(let [blocks-to-collapse (all-blocks-with-level {:expanded? true :root-block block-id})]
(when (seq blocks-to-collapse)
(doseq [{:block/keys [uuid]} blocks-to-collapse]
(collapse-block! uuid))))))
(doseq [{:block/keys [uuid]} blocks-to-collapse]
(collapse-block! uuid)))))
(defn expand-all!
([]
@@ -3451,7 +3452,24 @@
(->> (all-blocks-with-level {:root-block block-id})
(filter (fn [b] (-> b :block/properties :collapsed)))
(map (comp expand-block! :block/uuid))
doall)))
dorun)))
(defn- get-block-with-its-children
[block-uuid]
(let [repo (state/get-current-repo)
children (db/get-block-children repo block-uuid)
block (db/pull [:block/uuid block-uuid])]
(cons block (seq children))))
(defn expand-all?
[block-uuid]
(let [blocks (get-block-with-its-children block-uuid)]
(some #(get-in % [:block/properties :collapsed]) blocks)))
(defn collapse-all?
[block-uuid]
(let [blocks (get-block-with-its-children block-uuid)]
(some #(collapsable? (:block/uuid %)) blocks)))
(defn toggle-open! []
(let [all-collapsed?