improve performance for drag && drop

pull/4750/head
Tienson Qin 2022-03-30 21:43:39 +08:00
parent 096c69ffb8
commit c46c68cd14
3 changed files with 49 additions and 29 deletions

View File

@ -571,38 +571,57 @@
(recur parent))) (recur parent)))
false))) false)))
(defn- get-start-id-for-pagination-query
[repo-url current-db {:keys [db-before tx-meta] :as tx-report}
result outliner-op page-id block-id tx-block-ids]
(let [db-before (or db-before current-db)
cached-ids (map :db/id @result)
cached-ids-set (set (conj cached-ids page-id))
first-changed-id (if (= outliner-op :move-subtree)
(let [{:keys [move-blocks target from-page to-page]} tx-meta]
(if (not= from-page to-page)
(if (= page-id from-page)
(first move-blocks)
target)
;; same page, get the most top block before dragging
(let [match-ids (set (conj move-blocks target))]
(loop [[id & others] cached-ids]
(if (contains? match-ids id)
id
(recur others))))))
(let [insert? (contains? #{:insert-node :insert-nodes :save-and-insert-node} outliner-op)]
(some #(when (and (or (and insert? (not (contains? cached-ids-set %)))
true)
(recursive-child? repo-url % block-id))
%) tx-block-ids)))]
(when first-changed-id
(or (get-prev-open-block db-before first-changed-id)
(get-prev-open-block current-db first-changed-id)))))
;; TODO: outliners ops should be merged to :save-nodes, :insert-nodes, ;; TODO: outliners ops should be merged to :save-nodes, :insert-nodes,
;; :delete-nodes and :move-nodes ;; :delete-nodes and :move-nodes
(defn- build-paginated-blocks-from-cache (defn- build-paginated-blocks-from-cache
"Notice: tx-report could be nil." "Notice: tx-report could be nil."
[repo-url tx-report result outliner-op page-id block-id tx-block-ids scoped-block-id] [repo-url tx-report result outliner-op page-id block-id tx-block-ids scoped-block-id]
(let [{:keys [db-before]} tx-report (let [{:keys [tx-meta]} tx-report
current-db (conn/get-conn repo-url) current-db (conn/get-conn repo-url)]
db-before (or db-before current-db)]
(cond (cond
(contains? #{:save-node :delete-node :delete-nodes} outliner-op) (contains? #{:save-node :delete-node :delete-nodes} outliner-op)
@result @result
(contains? #{:insert-node :insert-nodes :save-and-insert-node (contains? #{:insert-node :insert-nodes :save-and-insert-node
:collapse-expand-blocks :indent-outdent-nodes :move-subtree} outliner-op) :collapse-expand-blocks :indent-outdent-nodes :move-subtree} outliner-op)
(let [cached-ids (set (conj (map :db/id @result) page-id)) (when-let [start-id (get-start-id-for-pagination-query
move? (= :move-subtree outliner-op) repo-url current-db tx-report result outliner-op page-id block-id tx-block-ids)]
insert? (contains? #{:insert-node :insert-nodes :save-and-insert-node} outliner-op) (let [start-page? (:block/name (db-utils/entity start-id))]
first-changed-id (some #(when (and (or (and insert? (not (contains? cached-ids %)))
true)
(recursive-child? repo-url % block-id))
%) tx-block-ids)
start-id (if move? first-changed-id (or (get-prev-open-block db-before first-changed-id)
(get-prev-open-block current-db first-changed-id)))
start-page? (:block/name (db-utils/entity start-id))]
(when-not start-page? (when-not start-page?
(let [previous-blocks (take-while (fn [b] (not= start-id (:db/id b))) @result) (let [previous-blocks (take-while (fn [b] (not= start-id (:db/id b))) @result)
previous-count (count previous-blocks) previous-count (count previous-blocks)
limit (max step-loading-blocks (- initial-blocks-length previous-count)) limit 25
more (get-paginated-blocks-no-cache current-db start-id {:limit limit more (get-paginated-blocks-no-cache current-db start-id {:limit limit
:include-start? true :include-start? true
:scoped-block-id scoped-block-id})] :scoped-block-id scoped-block-id})]
(concat previous-blocks more)))) (concat previous-blocks more)))))
:else :else
nil))) nil)))

View File

@ -310,7 +310,6 @@
(when (or query query-fn) (when (or query query-fn)
(try (try
(let [f #(execute-query! repo-url db k tx cache)] (let [f #(execute-query! repo-url db k tx cache)]
(f)
;; Detects whether user is editing in a custom query, if so, execute the query immediately ;; Detects whether user is editing in a custom query, if so, execute the query immediately
(if (and custom? (if (and custom?
;; modifying during cards review need to be executed immediately ;; modifying during cards review need to be executed immediately

View File

@ -601,7 +601,9 @@
(let [root-page (:db/id (:block/page (:data root))) (let [root-page (:db/id (:block/page (:data root)))
target-page (:db/id (:block/page (:data target-node))) target-page (:db/id (:block/page (:data target-node)))
not-same-page? (not= root-page target-page) not-same-page? (not= root-page target-page)
opts (cond-> {:outliner-op :move-subtree} opts (cond-> {:outliner-op :move-subtree
:move-blocks [(:db/id (get-data root))]
:target (:db/id (get-data target-node))}
not-same-page? not-same-page?
(assoc :from-page root-page (assoc :from-page root-page
:target-page target-page))] :target-page target-page))]