enhance(ux): render blocks by their orders (#11948)

* enhance: render nested children when rendering its parent

except that for long pages (500+ blocks).

* refactor: rename :block.temp/fully-loaded? to :block.temp/load-status

* fix: tests

* enhance: define keyword :block.temp/load-status

* update to :self

* chore: remove buggy parent cycle detect

* enhance(ux): don't delay rendering block properties

---------

Co-authored-by: rcmerci <rcmerci@gmail.com>
This commit is contained in:
Tienson Qin
2025-06-17 19:41:13 +08:00
committed by GitHub
parent ace71b2b25
commit 3b718f70c8
18 changed files with 128 additions and 149 deletions

View File

@@ -9,4 +9,9 @@
:block/raw-title {:doc "like `:block/title`,
but when eval `(:block/raw-title block-entity)`, return raw title of this block"}
:kv/value {:doc "Used to store key-value, the value could be anything,
e.g. {:db/ident :logseq.kv/xxx :kv/value value}"})
e.g. {:db/ident :logseq.kv/xxx :kv/value value}"}
;; :block.temp/xxx keywords
:block.temp/load-status {:doc "`:full` means the block and its children have been loaded,
`:self` means the block itself has been loaded."})

View File

@@ -2642,8 +2642,7 @@
(editor-handler/clear-selection!)
(editor-handler/unhighlight-blocks!)
(let [f #(p/do!
(when-not (:block.temp/fully-loaded? (db/entity (:db/id block)))
(db-async/<get-block (state/get-current-repo) (:db/id block) {:children? false}))
(db-async/<get-block (state/get-current-repo) (:db/id block) {:children? false})
(let [cursor-range (some-> (gdom/getElement block-id)
(dom/by-class "block-content-inner")
first

View File

@@ -696,7 +696,7 @@
*loading? (atom true)
page (db/get-page page-id-uuid-or-name)
*page (atom page)]
(when (:block.temp/fully-loaded? page) (reset! *loading? false))
(when (:block.temp/load-status page) (reset! *loading? false))
(p/let [page-block (db-async/<get-block (state/get-current-repo) page-id-uuid-or-name)]
(reset! *loading? false)
(reset! *page (db/entity (:db/id page-block)))

View File

@@ -909,7 +909,7 @@
(rum/defc table-row < rum/reactive db-mixins/query
[table row props option]
(let [block (db/sub-block (:db/id row))
row' (if (:block.temp/fully-loaded? block)
row' (if (:block.temp/load-status block)
(assoc block :block.temp/refs-count (:block.temp/refs-count row))
row)]
(table-row-inner table row' props option)))

View File

@@ -80,11 +80,11 @@
(assoc opts :property-ident property-id))))
(defn <get-block
[graph id-uuid-or-name & {:keys [children? nested-children? skip-transact? skip-refresh? children-only? properties]
[graph id-uuid-or-name & {:keys [children? skip-transact? skip-refresh? children-only? properties]
:or {children? true}
:as opts}]
;; (prn :debug :<get-block id-uuid-or-name)
;; (prn :debug :<get-block id-uuid-or-name :children? children?)
;; (js/console.trace)
(let [name' (str id-uuid-or-name)
opts (assoc opts :children? children?)
@@ -97,12 +97,11 @@
(db/get-page name'))
id (or (and (:block/uuid e) (str (:block/uuid e)))
(and (util/uuid-string? name') name')
id-uuid-or-name)]
id-uuid-or-name)
load-status (:block.temp/load-status e)]
(cond
(and (:block.temp/fully-loaded? e) ; children may not be fully loaded
(not children-only?)
(not children?)
(not nested-children?)
(and (or (= load-status :full)
(and (= load-status :self) (not children?) (not children-only?)))
(not (some #{:block.temp/refs-count} properties)))
(p/promise e)
@@ -113,11 +112,16 @@
{:keys [block children]} (first result)]
(when-not skip-transact?
(let [conn (db/get-db graph false)
load-status' (if (or children? children-only?) :full :self)
block-load-status-tx (when block
[{:db/id (:db/id block)
:block.temp/load-status load-status'}])
block-and-children (if block (cons block children) children)
affected-keys [[:frontend.worker.react/block (:db/id block)]]
tx-data (->> (remove (fn [b] (:block.temp/fully-loaded? (db/entity (:db/id b)))) block-and-children)
tx-data (->> (remove (fn [b] (:block.temp/load-status (db/entity (:db/id b)))) block-and-children)
(common-util/fast-remove-nils)
(remove empty?))]
(remove empty?)
(concat block-load-status-tx))]
(when (seq tx-data) (d/transact! conn tx-data))
(when-not skip-refresh?
(react/refresh-affected-queries! graph affected-keys {:skip-kv-custom-keys? true}))))
@@ -129,7 +133,7 @@
(defn <get-blocks
[graph ids* & {:as opts}]
(let [ids (remove (fn [id] (:block.temp/fully-loaded? (db/entity id))) ids*)]
(let [ids (remove (fn [id] (:block.temp/load-status (db/entity id))) ids*)]
(when (seq ids)
(p/let [result (state/<invoke-db-worker :thread-api/get-blocks graph
(map (fn [id]
@@ -138,7 +142,7 @@
(let [conn (db/get-db graph false)
result' (map :block result)]
(when (seq result')
(let [result'' (map (fn [b] (assoc b :block.temp/fully-loaded? true)) result')]
(let [result'' (map (fn [b] (assoc b :block.temp/load-status :self)) result')]
(d/transact! conn result'')))
result')))))

View File

@@ -130,8 +130,8 @@
area? (not (nil? (:image content)))
wrap-props #(if-let [stamp (:image content)]
(assoc %
:hl-type :area
:hl-stamp stamp)
:hl-type :area
:hl-stamp stamp)
%)
props {:id (if (string? id) (uuid id) id)
(pu/get-pid :logseq.property/ls-type) :annotation
@@ -140,11 +140,11 @@
properties (wrap-props props)]
(when (string? text)
(editor-handler/api-insert-new-block!
text (merge {:page (:block/name ref-page)
:custom-uuid id
:edit-block? (not area?)
:properties properties}
insert-opts)))))))))
text (merge {:page (:block/name ref-page)
:custom-uuid id
:edit-block? (not area?)
:properties properties}
insert-opts)))))))))
(defn db-based-ensure-ref-block!
[pdf-current {:keys [id content page properties] :as hl} insert-opts]
@@ -187,8 +187,7 @@
[hls-page]
(p/let [result (db-async/<get-block (state/get-current-repo)
(:block/uuid hls-page)
{:children? true
:nested-children? false})]
{:children? true})]
{:highlights (keep :logseq.property.pdf/hl-value result)}))
(defn file-based-load-hls-data$

View File

@@ -178,8 +178,7 @@
(util/mobile-keep-keyboard-open)
(let [repo (state/get-current-repo)]
(p/do!
(when-not (:block.temp/fully-loaded? (db/entity (:db/id block)))
(db-async/<get-block repo (:db/id block) {:children? false}))
(db-async/<get-block repo (:db/id block) {:children? false})
(when save-code-editor? (state/pub-event! [:editor/save-code-editor]))
(when (not= (:block/uuid block) (:block/uuid (state/get-edit-block)))
(state/clear-edit! {:clear-editing-block? false}))

View File

@@ -2091,8 +2091,7 @@
(db-async/<get-template-by-name (name db-id)))
block (when (:block/uuid block)
(db-async/<get-block repo (:block/uuid block)
{:children? true
:nested-children? true}))]
{:children? true}))]
(when (:db/id block)
(let [journal? (ldb/journal? target)
target (or target (state/get-edit-block))
@@ -3486,7 +3485,7 @@
repo (state/get-current-repo)
result (db-async/<get-block repo (or block-id page-id)
{:children-only? true
:nested-children? true})
:include-collapsed-children? true})
blocks (if page-id
result
(cons (db/entity [:block/uuid block-id]) result))
@@ -3562,7 +3561,8 @@
(defn expand-block! [block-id & {:keys [skip-db-collpsing?]}]
(let [repo (state/get-current-repo)]
(p/do!
(db-async/<get-block repo block-id {:children-only? true})
(db-async/<get-block repo block-id {:children-only? true
:include-collapsed-children? true})
(when-not (or skip-db-collpsing? (skip-collapsing-in-db?))
(set-blocks-collapsed! [block-id] false))
(state/set-collapsed-block! block-id false))))

View File

@@ -422,8 +422,7 @@
;; load all nested children here for copy/export
(p/all (map (fn [id]
(db-async/<get-block (state/get-current-repo) id
{:nested-children? true
:skip-refresh? false})) ids))))
{:skip-refresh? false})) ids))))
(defn run!
[]

View File

@@ -63,7 +63,7 @@
(if (contains? #{:create-property-text-block :insert-blocks} (:outliner-op tx-meta))
(let [update-blocks-fully-loaded (keep (fn [datom] (when (= :block/uuid (:a datom))
{:db/id (:e datom)
:block.temp/fully-loaded? true})) tx-data)]
:block.temp/load-status :self})) tx-data)]
(concat update-blocks-fully-loaded tx-data))
tx-data))]
(d/transact! conn tx-data' tx-meta))

View File

@@ -814,7 +814,7 @@
(pr-str
{:graph graph
:embed-block? embed-block?
:blocks (mapv #(dissoc % :block.temp/fully-loaded? %) blocks)}))}))]
:blocks (mapv #(dissoc % :block.temp/load-status %) blocks)}))}))]
(if owner-window
(utils/writeClipboard data owner-window)
(utils/writeClipboard data)))))