Browse Source

fix: expand/Collapse all doesn't work when zooming in a block

addresses #3734, #3735
Tienson Qin 4 years ago
parent
commit
8cc194ce60
2 changed files with 31 additions and 24 deletions
  1. 1 0
      src/main/frontend/db/model.cljs
  2. 30 24
      src/main/frontend/handler/editor.cljs

+ 1 - 0
src/main/frontend/db/model.cljs

@@ -465,6 +465,7 @@
         nil)
       react))))
 
+;; FIXME: merge get-page-blocks and get-block-and-children to simplify the logic
 (defn get-page-blocks
   ([page]
    (get-page-blocks (state/get-current-repo) page nil))

+ 30 - 24
src/main/frontend/handler/editor.cljs

@@ -3334,30 +3334,36 @@
   [{:keys [collapse? expanded? root-block] :or {collapse? false expanded? false root-block nil}}]
   (when-let [page (or (state/get-current-page)
                       (date/today))]
-    (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 []))))))
-
-        collapse?
-        (w/postwalk
-         (fn [b]
-           (if (and (map? b) (-> b :block/properties :collapsed))
-             (assoc b :block/children []) b)))
-
-        true
-        (mapcat (fn [x] (tree-seq map? :block/children x)))
-
-        expanded?
-        (filter (fn [b] (collapsable? (:block/uuid b))))
-
-        true
-        (map (fn [x] (dissoc x :block/children)))))))
+    (let [block? (util/uuid-string? page)
+          block-id (or (and block? (uuid page)) root-block)
+          blocks (if block-id
+                   (db/get-block-and-children (state/get-current-repo) block-id)
+                   (db/get-page-blocks-no-cache page))
+          blocks (tree/blocks->vec-tree blocks (or block-id page))
+          root-block (or block-id root-block)]
+      (->>
+       (cond->> blocks
+         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)))
+
+         true
+         (mapcat (fn [x] (tree-seq map? :block/children x)))
+
+         expanded?
+         (filter (fn [b] (collapsable? (:block/uuid b))))
+
+         true
+         (map (fn [x] (dissoc x :block/children))))
+       (remove nil?)))))
 
 (defn collapse-block! [block-id]
   (when (collapsable? block-id)