Merge page and block queries to get-paginated-blocks

pull/4750/head
Tienson Qin 2022-03-25 00:06:18 +08:00
parent ee897c244b
commit b9df07ee45
6 changed files with 78 additions and 95 deletions

View File

@ -576,7 +576,7 @@
(rum/defc block-embed < rum/reactive db-mixins/query
[config id]
(let [blocks (db/sub-block-and-children (state/get-current-repo) id)]
(let [blocks (db/get-paginated-blocks (state/get-current-repo) id)]
[:div.color-level.embed-block.bg-base-2
{:style {:z-index 2}
:on-double-click #(edit-parent-block % config)
@ -605,7 +605,8 @@
page-name)
(not= (util/page-name-sanity-lc (get config :id ""))
page-name))
(let [blocks (db/get-page-blocks (state/get-current-repo) page-name)]
(let [page (model/get-page page-name)
blocks (db/get-paginated-blocks (state/get-current-repo) (:db/id page))]
(blocks-container blocks (assoc config
:id page-name
:embed? true

View File

@ -44,13 +44,13 @@
(defn- get-blocks
[repo page-name block-id]
(when page-name
(if block-id
(when-let [root-block (db/pull [:block/uuid block-id])]
(let [blocks (-> (db/sub-block-and-children repo block-id)
(model/sort-blocks root-block {}))]
(cons root-block blocks)))
(when-let [page (db/pull [:block/name (util/safe-page-name-sanity-lc page-name)])]
(db/get-page-blocks repo page-name)))))
(let [root (if block-id
(db/pull [:block/uuid block-id])
(model/get-page page-name))
blocks (db/get-paginated-blocks repo (:db/id root))]
(if block-id
(cons root blocks)
blocks))))
(defn- open-first-block!
[state]

View File

@ -39,14 +39,14 @@
blocks-count blocks-count-cache clean-export! cloned? delete-blocks get-pre-block
delete-file! delete-file-blocks! delete-page-blocks delete-file-pages! delete-file-tx delete-files delete-pages-by-files
filter-only-public-pages-and-blocks get-all-block-contents get-all-tagged-pages
get-all-templates get-block-and-children sub-block-and-children get-block-by-uuid get-block-children sort-by-left
get-all-templates get-block-and-children get-block-by-uuid get-block-children sort-by-left
get-block-parent get-block-parents parents-collapsed? get-block-referenced-blocks
get-block-children-ids get-block-immediate-children get-block-page
get-blocks-contents get-custom-css
get-date-scheduled-or-deadlines get-db-type
get-file-blocks get-file-contents get-file-last-modified-at get-file get-file-page get-file-page-id file-exists?
get-file-pages get-files get-files-blocks get-files-full get-journals-length
get-latest-journals get-matched-blocks get-page get-page-alias get-page-alias-names get-page-blocks get-page-linked-refs-refed-pages
get-latest-journals get-matched-blocks get-page get-page-alias get-page-alias-names get-paginated-blocks get-page-linked-refs-refed-pages
get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format get-page-properties
get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references get-page-referenced-blocks-no-cache
get-all-pages get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages

View File

@ -481,79 +481,77 @@
parent-sibling
(get-next-outdented-block (:db/id parent))))))
(defn- get-paginated-blocks
(defn- get-paginated-blocks-aux
"Result should be sorted."
[start-id {:keys [limit]}]
[start-id {:keys [limit include-start?]}]
(when-let [start (db-utils/entity start-id)]
(let [conn (conn/get-conn false)]
(loop [block start
next-siblings '()
result []]
(let [block-id (:db/id block)
block-parent-id (:db/id (:block/parent block))
next-sibling (get-by-parent-&-left conn block-parent-id block-id)
next-block (if (collapsed-and-has-children? block) ; skips children
next-sibling
(let [child-block (get-by-parent-&-left conn block-id block-id)]
(or child-block
next-sibling
(get-next-outdented-block block-id))))
next-siblings (if next-sibling
(cons next-sibling next-siblings)
next-siblings)]
(cond
(and limit (>= (count result) limit))
result
(let [conn (conn/get-conn false)
result (loop [block start
next-siblings '()
result []]
(let [block-id (:db/id block)
block-parent-id (:db/id (:block/parent block))
next-sibling (get-by-parent-&-left conn block-parent-id block-id)
next-block (if (collapsed-and-has-children? block) ; skips children
next-sibling
(let [child-block (get-by-parent-&-left conn block-id block-id)]
(or child-block
next-sibling
(get-next-outdented-block block-id))))
next-siblings (if next-sibling
(cons next-sibling next-siblings)
next-siblings)]
(cond
(and limit (>= (count result) limit))
result
(and (nil? next-block) (empty? next-siblings))
result
(and (nil? next-block) (empty? next-siblings))
result
next-block
(let [next-siblings (if (= next-block (first next-siblings))
(rest next-siblings)
next-siblings)]
(recur next-block
next-siblings
(conj result next-block)))
next-block
(let [next-siblings (if (= next-block (first next-siblings))
(rest next-siblings)
next-siblings)]
(recur next-block
next-siblings
(conj result next-block)))
(and (nil? next-block) (seq next-siblings))
(recur (first next-siblings)
(rest next-siblings)
(conj result (first next-siblings)))
(and (nil? next-block) (seq next-siblings))
(recur (first next-siblings)
(rest next-siblings)
(conj result (first next-siblings)))
:else
result))))))
:else
result)))]
(if include-start?
(cons start result)
result))))
(defn get-page-blocks
([page]
(get-page-blocks (state/get-current-repo) page nil))
([repo-url page]
(get-page-blocks repo-url page nil))
([repo-url page {:keys [use-cache? pull-keys start-block limit]
:or {use-cache? true
pull-keys '[*]
limit 50}}]
(when page
(let [page-entity (if (integer? page)
(db-utils/entity repo-url page)
(let [page (util/page-name-sanity-lc (string/trim page))]
(db-utils/entity repo-url [:block/name page])))
page-id (:db/id page-entity)
bare-page-map {:db/id page-id
:block/name (:block/name page-entity)
:block/original-name (:block/original-name page-entity)
:block/journal-day (:block/journal-day page-entity)}]
(when page-id
(some->
(react/q repo-url [:frontend.db.react/page-blocks page-id]
{:use-cache? use-cache?
:query-fn (fn [db tx-report result]
(let [blocks (get-paginated-blocks page-id {:limit limit})
block-eids (map :db/id blocks)
blocks (db-utils/pull-many repo-url pull-keys block-eids)]
(map (fn [b] (assoc b :block/page bare-page-map)) blocks)))}
nil)
react))))))
(defn get-paginated-blocks
"Get paginated blocks for a page or a specific block."
([repo-url block-id]
(get-paginated-blocks repo-url block-id {}))
([repo-url block-id {:keys [pull-keys start-block limit]
:or {pull-keys '[*]
limit 50}}]
(assert (integer? block-id))
(let [entity (db-utils/entity repo-url block-id)
page? (some? (:block/name entity))
page-entity (if page? entity (:block/page entity))
bare-page-map {:db/id block-id
:block/name (:block/name page-entity)
:block/original-name (:block/original-name page-entity)
:block/journal-day (:block/journal-day page-entity)}]
(some->
(react/q repo-url [:frontend.db.react/page-blocks block-id]
{:query-fn (fn [db tx-report result]
(let [blocks (get-paginated-blocks-aux block-id {:limit limit
:include-start? (not page?)})
block-eids (map :db/id blocks)
blocks (db-utils/pull-many repo-url pull-keys block-eids)]
(map (fn [b] (assoc b :block/page bare-page-map)) blocks)))}
nil)
react))))
(defn get-page-blocks-no-cache
([page]
@ -721,22 +719,6 @@
first
flatten-tree))
(defn sub-block-and-children
([repo block-uuid]
(sub-block-and-children repo block-uuid true))
([repo block-uuid use-cache?]
(some-> (react/q repo [:frontend.db.react/block-and-children block-uuid]
{:use-cache? use-cache?}
'[:find [(pull ?block ?block-attrs) ...]
:in $ ?id ?block-attrs
:where
[?block :block/uuid ?id]]
block-uuid
block-attrs)
react
first
flatten-tree)))
(defn get-file-page
([file-path]
(get-file-page file-path true))

View File

@ -94,7 +94,7 @@
page (db/entity [:block/name page-name])
journal? (:journal? page)
repo (state/get-current-repo)
blocks (-> (db/get-page-blocks repo page-name)
blocks (-> (db/get-paginated-blocks repo (:db/id page))
(outliner-tree/blocks->vec-tree page-name))
blocks (if journal?
(rest blocks)

View File

@ -509,7 +509,7 @@
(rum/defc preview-cp
[block-id]
(let [blocks-f (fn [] (db/sub-block-and-children (state/get-current-repo) block-id))]
(let [blocks-f (fn [] (db/get-paginated-blocks (state/get-current-repo) block-id))]
(view-modal blocks-f {:preview? true} (atom 0))))
(defn preview