Skip to content

Commit

Permalink
fix: collapsed state not persisted in their files
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Feb 16, 2022
1 parent c8621a7 commit 4c8d852
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/main/frontend/modules/file/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@
[frontend.db.utils :as db-utils]
[frontend.state :as state]
[frontend.util :as util]
[frontend.debug :as debug]))
[frontend.debug :as debug]
[frontend.util.property :as property]))

(defn- indented-block-content
[content spaces-tabs]
(let [lines (string/split-lines content)]
(string/join (str "\n" spaces-tabs) lines)))

(defn- content-with-collapsed-state
[format content collapsed? properties]
(cond
collapsed?
(property/insert-property format content :collapsed true)

(and (:collapsed properties) (false? collapsed?))
(property/remove-property format :collapsed content)

:else
content))

(defn transform-content
[{:block/keys [format pre-block? unordered content heading-level left page parent]} level {:keys [heading-to-list?]}]
[{:block/keys [collapsed? format pre-block? unordered content heading-level left page parent properties]} level {:keys [heading-to-list?]}]
(let [content (or content "")
first-block? (= left page)
pre-block? (and first-block? pre-block?)
Expand Down Expand Up @@ -60,27 +73,24 @@
""
" ")]
(str prefix sep new-content)))]
content))
(content-with-collapsed-state format content collapsed? properties)))

(defn tree->file-content
[tree {:keys [init-level]
:as opts}]
(loop [block-contents []
[f & r] tree
level init-level]
(let [f (if (:block/collapsed? f)
(assoc-in f [:block/properties :collapsed] true)
f)]
(if (nil? f)
(string/join "\n" block-contents)
(let [page? (nil? (:block/page f))
content (if page? nil (transform-content f level opts))
new-content
(->> (if-let [children (seq (:block/children f))]
[content (tree->file-content children {:init-level (inc level)})]
[content])
(remove nil?))]
(recur (into block-contents new-content) r level))))))
(if (nil? f)
(string/join "\n" block-contents)
(let [page? (nil? (:block/page f))
content (if page? nil (transform-content f level opts))
new-content
(->> (if-let [children (seq (:block/children f))]
[content (tree->file-content children {:init-level (inc level)})]
[content])
(remove nil?))]
(recur (into block-contents new-content) r level)))))

(def init-level 1)

Expand Down

0 comments on commit 4c8d852

Please sign in to comment.