Skip to content

Commit

Permalink
detects has-more loading by whether there's a next open block
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Apr 2, 2022
1 parent b0999ef commit daf0b61
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
19 changes: 11 additions & 8 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2879,14 +2879,15 @@
[state config flat-blocks blocks->vec-tree]
(let [db-id (:db/id config)
blocks (blocks->vec-tree flat-blocks)]
(if (:custom-query? config)
(if-not db-id
(block-list config blocks)
(let [last-block-id (:db/id (last flat-blocks))
bottom-reached (fn []
(when (and db-id
;; To prevent scrolling after inserting new blocks
(> (- (util/time-ms) (:start-time config)) 100))
(let [bottom-reached (fn []
;; To prevent scrolling after inserting new blocks
(when (> (- (util/time-ms) (:start-time config)) 100)
(load-more-blocks! config flat-blocks)))
has-more? (and
(> (count flat-blocks) model/initial-blocks-length)
(some? (model/get-next-open-block (last flat-blocks) db-id)))
dom-id (str "lazy-blocks-" (::id state))]
[:div {:id dom-id}
(ui/infinite-list
Expand All @@ -2896,8 +2897,10 @@
:bottom-reached (fn []
(when-let [node (gdom/getElement dom-id)]
(ui/bottom-reached? node 1000)))
:has-more true
:more "More"})]))))
:has-more has-more?
:more (if (or (:preview? config) (:sidebar? config))
"More"
(ui/loading "Loading"))})]))))

(rum/defcs blocks-container <
{:init (fn [state]
Expand Down
43 changes: 28 additions & 15 deletions src/main/frontend/db/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,33 @@
'[:db/id :block/collapsed? :block/properties {:block/parent ...}]
[:block/uuid block-id]))

(defn get-next-open-block
([block]
(get-next-open-block block nil))
([block scoped-block-id]
(let [conn (conn/get-conn false)
block-id (:db/id block)
block-parent-id (:db/id (:block/parent block))
next-block (or
(if (collapsed-and-has-children? block) ; skips children
;; Sibling
(get-by-parent-&-left conn block-parent-id block-id)
(or
;; Child
(get-by-parent-&-left conn block-id block-id)
;; Sibling
(get-by-parent-&-left conn block-parent-id block-id)))

;; Next outdented block
(get-next-outdented-block block-id))]
(if (and scoped-block-id next-block)
(let [parents (->> (get-block-parents (state/get-current-repo) (:block/uuid next-block))
(map :db/id)
(set))]
(when (contains? parents scoped-block-id)
next-block))
next-block))))

(defn get-paginated-blocks-no-cache
"Result should be sorted."
[start-id {:keys [limit include-start? scoped-block-id]}]
Expand All @@ -491,25 +518,11 @@
(->> (get-block-parents (state/get-current-repo) (:block/uuid block))
(map :db/id)
(set))))
conn (conn/get-conn false)
result (loop [block start
result []]
(if (and limit (>= (count result) limit))
result
(let [block-id (:db/id block)
block-parent-id (:db/id (:block/parent block))
next-block (or
(if (collapsed-and-has-children? block) ; skips children
;; Sibling
(get-by-parent-&-left conn block-parent-id block-id)
(or
;; Child
(get-by-parent-&-left conn block-id block-id)
;; Sibling
(get-by-parent-&-left conn block-parent-id block-id)))

;; Next outdented block
(get-next-outdented-block block-id))]
(let [next-block (get-next-open-block block)]
(if next-block
(if (and (seq scoped-block-parents)
(contains? scoped-block-parents (:db/id (:block/parent next-block))))
Expand Down

0 comments on commit daf0b61

Please sign in to comment.