Skip to content

Commit

Permalink
fix: long page stall loading and collapsed state
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Feb 8, 2022
1 parent 8ef6fd3 commit fe3f066
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
17 changes: 7 additions & 10 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2173,7 +2173,7 @@
(rum/defcs block-container < rum/reactive
{:init (fn [state]
(let [[config block] (:rum/args state)]
(when (and (not (some? (state/sub-collapsed (:block/uuid block))))
(when (and (nil? (state/get-collapsed (:block/uuid block)))
(or (:ref? config) (:block? config)))
(state/set-collapsed-block! (:block/uuid block)
(editor-handler/block-default-collapsed? block config)))
Expand Down Expand Up @@ -2848,8 +2848,6 @@
(rum/with-key (block-container config item)
(str (:block/uuid item))))))

(defonce ignore-scroll? (atom false))

(defn- custom-query-or-ref?
[config]
(let [ref? (:ref? config)
Expand All @@ -2858,8 +2856,9 @@

;; TODO: virtual tree for better UX and memory usage reduce


(defn- get-segment
[flat-blocks idx blocks->vec-tree]
[config flat-blocks idx blocks->vec-tree]
(let [new-idx (if (< idx block-handler/initial-blocks-length)
block-handler/initial-blocks-length
(+ idx block-handler/step-loading-blocks))
Expand All @@ -2881,15 +2880,13 @@
(rum/local 0 ::last-idx)
[state config flat-blocks blocks->vec-tree]
(let [*last-idx (::last-idx state)
[segment idx] (get-segment flat-blocks
[segment idx] (get-segment config
flat-blocks
@*last-idx
blocks->vec-tree)
bottom-reached (fn []
(reset! *last-idx idx)
(reset! ignore-scroll? false))
has-more? (and (>= (count flat-blocks) (inc idx))
(not (and (:block? config)
(state/sub-collapsed (uuid (:id config))))))]
(reset! *last-idx idx))
has-more? (>= (count flat-blocks) (inc idx))]
[:div#lazy-blocks
(ui/infinite-list
"main-content-container"
Expand Down
5 changes: 4 additions & 1 deletion src/main/frontend/components/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
[repo page-name block-id]
(when page-name
(if block-id
(db/get-block-and-children repo block-id)
(when-let [root-block (db/pull [:block/uuid block-id])]
(let [blocks (-> (db/get-block-and-children repo block-id)
(model/sort-blocks root-block {:block? true}))]
(cons root-block blocks)))
(db/get-page-blocks repo page-name))))

(defn- open-first-block!
Expand Down
26 changes: 12 additions & 14 deletions src/main/frontend/db/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,14 @@
;; TODO: both zipmap and map lookup are slow in cljs
;; zipmap 20k blocks takes 30ms on my M1 Air.
(defn sort-blocks
[blocks parent limit]
[blocks parent {:keys [limit block?] :as config}]
(let [ids->blocks (zipmap (map
(fn [b]
[(:db/id (:block/parent b))
(:db/id (:block/left b))])
blocks)
blocks)]
blocks)
collapsed-blocks (if block? (state/sub-collapsed-blocks) nil)]
(loop [node parent
next-siblings '()
result []]
Expand All @@ -437,25 +438,22 @@
next-sibling (get ids->blocks [(:db/id (:block/parent node)) id])
next-siblings (if (and next-sibling child-block)
(cons next-sibling next-siblings)
next-siblings)]
next-siblings)
collapsed? (if block?
(let [v (get collapsed-blocks (:block/uuid node))]
(if (some? v)
v
(:block/collapsed? node)))
(:block/collapsed? node))]
(if-let [node (and
(not (:block/collapsed? node))
(or (not collapsed?)
(= (:db/id node) (:db/id parent)))
(or child-block next-sibling))]
(recur node next-siblings (conj result node))
(if-let [sibling (first next-siblings)]
(recur sibling (rest next-siblings) (conj result sibling))
result)))))))

(comment
(let [page "Scripture (NASB 1995)"
page-entity (db-utils/pull [:block/name (string/lower-case page)])
blocks (->> (get-page-blocks (state/get-current-repo) (string/lower-case page) {:use-cache? false})
(map (fn [b] (assoc b :block/content (:block/content (db-utils/entity (:db/id b)))))))]
(def page-entity page-entity)
(def blocks blocks)
(time (prn (count (sort-blocks blocks page-entity 1)))))
)

(defn get-block-refs-count
[block-id]
(when-let [repo-url (state/get-current-repo)]
Expand Down
24 changes: 20 additions & 4 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3594,19 +3594,35 @@
(doseq [{:block/keys [uuid]} blocks-to-collapse]
(collapse-block! uuid))))))))))

(defn- zoom-in?
[]
(let [page (state/get-current-page)]
(boolean
(and
(string? page)
(util/uuid-string? page)))))

(defn collapse-all!
([]
(collapse-all! nil))
([block-id]
(let [blocks (all-blocks-with-level {:expanded? true :root-block block-id})]
(set-blocks-collapsed! (map :block/uuid blocks) true))))
(let [blocks (all-blocks-with-level {:expanded? true :root-block block-id})
block-ids (map :block/uuid blocks)]
(if (zoom-in?)
(doseq [block-id block-ids]
(state/set-collapsed-block! block-id true))
(set-blocks-collapsed! block-ids true)))))

(defn expand-all!
([]
(expand-all! nil))
([block-id]
(let [blocks (all-blocks-with-level {:root-block block-id})]
(set-blocks-collapsed! (map :block/uuid blocks) false))))
(let [blocks (all-blocks-with-level {:root-block block-id})
block-ids (map :block/uuid blocks)]
(if (zoom-in?)
(doseq [block-id block-ids]
(state/set-collapsed-block! block-id false))
(set-blocks-collapsed! block-ids false)))))

(defn toggle-open! []
(let [all-collapsed?
Expand Down
8 changes: 8 additions & 0 deletions src/main/frontend/state.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,3 +1605,11 @@
(defn sub-collapsed
[block-id]
(sub [:ui/collapsed-blocks (get-current-repo) block-id]))

(defn get-collapsed
[block-id]
(get @state [:ui/collapsed-blocks (get-current-repo) block-id]))

(defn sub-collapsed-blocks
[]
(sub [:ui/collapsed-blocks (get-current-repo)]))

0 comments on commit fe3f066

Please sign in to comment.