Merge pull request #4087 from logseq/fix/long-page-loading-and-collapsed-state

fix: long page stall loading and collapsed state
pull/4200/head
Tienson Qin 2022-02-11 14:19:50 +08:00 committed by GitHub
commit e65c81bfea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 103 deletions

View File

@ -1416,7 +1416,6 @@
has-children-blocks?)
control-show? (util/react *control-show?)
ref? (:ref? config)
block? (:block? config)
empty-content? (block-content-empty? block)]
[:div.mr-1.flex.flex-row.items-center.sm:mr-2
{:style {:height 24
@ -1428,7 +1427,7 @@
:on-click (fn [event]
(util/stop event)
(when-not (and (not collapsed?) (not has-child?))
(if (or ref? block?)
(if ref?
(state/toggle-collapsed-block! uuid)
(if collapsed?
(editor-handler/expand-block! uuid)
@ -2168,13 +2167,27 @@
children)
(distinct @refs)))
(defn- root-block?
[config block]
(and (:block? config)
(util/collapsed? block)
(= (:id config)
(str (:block/uuid block)))))
(rum/defcs ^:large-vars/cleanup-todo block-container < rum/reactive
{:init (fn [state]
(let [[config block] (:rum/args state)]
(when (and (not (some? (state/sub-collapsed (:block/uuid block))))
(or (:ref? config) (:block? config)))
(state/set-collapsed-block! (:block/uuid block)
(editor-handler/block-default-collapsed? block config)))
(let [[config block] (:rum/args state)
block-id (:block/uuid block)]
(cond
(:ref? config)
(state/set-collapsed-block! block-id
(editor-handler/block-default-collapsed? block config))
(root-block? config block)
(state/set-collapsed-block! block-id false)
:else
nil)
(assoc state ::control-show? (atom false))))
:should-update (fn [old-state new-state]
(let [compare-keys [:block/uuid :block/content :block/parent :block/collapsed? :block/children
@ -2199,11 +2212,14 @@
config)
heading? (and (= type :heading) heading-level (<= heading-level 6))
*control-show? (get state ::control-show?)
ref? (boolean (:ref? config))
block? (boolean (:block? config))
collapsed? (if (or ref? block?)
ref? (:ref? config)
db-collapsed? (util/collapsed? block)
collapsed? (cond
(or ref? (root-block? config block))
(state/sub-collapsed uuid)
(util/collapsed? block))
:else
db-collapsed?)
breadcrumb-show? (:breadcrumb-show? config)
slide? (boolean (:slide? config))
custom-query? (boolean (:custom-query? config))
@ -2846,8 +2862,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)
@ -2856,8 +2870,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))
@ -2879,15 +2894,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"

View File

@ -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 {}))]
(cons root-block blocks)))
(db/get-page-blocks repo page-name))))
(defn- open-first-block!
@ -62,11 +65,7 @@
state)
(rum/defc page-blocks-inner <
{:init (fn [state]
(when-let [block-id (last (:rum/args state))]
(state/set-collapsed-block! block-id false))
state)
:did-mount open-first-block!
{:did-mount open-first-block!
:did-update open-first-block!}
[page-name _blocks hiccup sidebar? _block-uuid]
[:div.page-blocks-inner {:style {:margin-left (if sidebar? 0 -20)}}

View File

@ -420,7 +420,7 @@
;; 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] :as config}]
(let [ids->blocks (zipmap (map
(fn [b]
[(:db/id (:block/parent b))
@ -437,25 +437,17 @@
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? (: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)]

View File

@ -3449,6 +3449,7 @@
if :root-block is not nil, only return root block with its children
if :expanded? true, return expanded children
if :collapse? true, return without any collapsed children
if :incremental? true, collapse/expand will be step by step
for example:
- a
- b (collapsed)
@ -3460,7 +3461,8 @@
[{:block a :level 1}
{:block b :level 2}
{:block e :level 2}]"
[{:keys [collapse? expanded? root-block] :or {collapse? false expanded? false root-block nil}}]
[{:keys [collapse? expanded? incremental? root-block]
:or {collapse? false expanded? false incremental? true root-block nil}}]
(when-let [page (or (state/get-current-page)
(date/today))]
(let [block? (util/uuid-string? page)
@ -3468,36 +3470,49 @@
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 []))))))
(if incremental?
(let [blocks (tree/blocks->vec-tree blocks (or block-id 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) (util/collapsed? b))
(assoc b :block/children []) b)))
collapse?
(w/postwalk
(fn [b]
(if (and (map? b)
(util/collapsed? b)
(not= root-block (:block/uuid b)))
(assoc b :block/children []) b)))
true
(mapcat (fn [x] (tree-seq map? :block/children x)))
true
(mapcat (fn [x] (tree-seq map? :block/children x)))
expanded?
(filter (fn [b] (collapsable? (:block/uuid b))))
expanded?
(filter (fn [b] (collapsable? (:block/uuid b))))
true
(map (fn [x] (dissoc x :block/children))))
(remove nil?)))))
true
(map (fn [x] (dissoc x :block/children))))
(remove nil?)))
(cond->> blocks
collapse?
(filter util/collapsed?)
expanded?
(filter (fn [b] (collapsable? (:block/uuid b))))
true
(remove nil?))))))
(defn- skip-collapsing-in-db?
[]
(let [config (:config (state/get-editor-args))]
(or (:ref? config) (:block? config))))
(:ref? config)))
(defn- set-blocks-collapsed!
[block-ids value]
@ -3531,15 +3546,13 @@
(defn collapse-block! [block-id]
(when (collapsable? block-id)
(when-not (skip-collapsing-in-db?)
(set-block-property! block-id :collapsed true)
(set-blocks-collapsed! [block-id] true)))
(state/set-collapsed-block! block-id true))
(defn expand-block! [block-id]
(when-not (skip-collapsing-in-db?)
(set-block-property! block-id :collapsed false)
(set-blocks-collapsed! [block-id] false))
(state/set-collapsed-block! block-id false))
(set-blocks-collapsed! [block-id] false)
(state/set-collapsed-block! block-id false)))
(defn expand!
([e] (expand! e false))
@ -3577,7 +3590,7 @@
(defn collapse!
([e] (collapse! e false))
([e clear-selection?]
(util/stop e)
(when e (util/stop e))
(cond
(state/editing?)
(when-let [block-id (:block/uuid (state/get-edit-block))]
@ -3614,24 +3627,28 @@
([]
(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 {:incremental? false
:expanded? true
:root-block block-id})
block-ids (map :block/uuid blocks)]
(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 {:incremental? false
:collapse? true
:root-block block-id})
block-ids (map :block/uuid blocks)]
(set-blocks-collapsed! block-ids false))))
(defn toggle-open! []
(let [all-collapsed?
(->> (all-blocks-with-level {:collapse? true})
(filter (fn [b] (collapsable? (:block/uuid b))))
(empty?))]
(if all-collapsed?
(expand-all!)
(collapse-all!))))
(let [all-expanded? (empty? (all-blocks-with-level {:incremental? false
:collapse? true}))]
(if all-expanded?
(collapse-all!)
(expand-all!))))
(defn select-all-blocks!
[]
@ -3713,27 +3730,11 @@
(defn block-default-collapsed?
"Whether a block should be collapsed by default.
Currently, this handles several cases:
1. Zoom in mode, it will open the current block if it's collapsed.
2. Queries.
3. References."
1. References."
[block config]
(let [collapsed? (cond
(and (:block? config)
(= (:id config) (str (:block/uuid block))))
false
(and (:block? config)
(util/collapsed? block))
true
:else
(boolean
(and
(seq (:block/children block))
(or (:custom-query? config)
(and (:ref? config)
(>= (:ref/level block)
(state/get-ref-open-blocks-level)))))))]
(if (or (:ref? config) (:block? config))
collapsed?
(util/collapsed? block))))
(if (:ref? config)
(and
(seq (:block/children block))
(>= (:ref/level block)
(state/get-ref-open-blocks-level)))
(util/collapsed? block)))

View File

@ -1448,6 +1448,4 @@
(defn collapsed?
[block]
(or (:block/collapsed? block)
;; for backward compatiblity
(get-in block [:properties :collapsed])))
(:block/collapsed? block))