From a23068fec75894544f97a885e76edb9727bd0082 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 6 Sep 2023 23:45:37 +0800 Subject: [PATCH] fix: blocks rendering --- src/main/frontend/components/block.cljs | 48 +++++++++++++++++-------- src/main/frontend/handler/block.cljs | 2 +- src/main/frontend/handler/editor.cljs | 7 ++-- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 9564d3d1cf..536f046a24 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -81,7 +81,8 @@ [reitit.frontend.easy :as rfe] [rum.core :as rum] [shadow.loader :as loader] - [logseq.common.path :as path])) + [logseq.common.path :as path] + [clojure.data :as data])) @@ -3401,30 +3402,49 @@ (defn- get-hidden-atom [sub-id *ref {:keys [initial-value]}] - (let [*initial? (atom true)] + (let [*initial? (atom true) + *prev-scroll-top (atom @(:ui/main-container-scroll-top @state/state)) + *latest-value (atom nil)] (rum/derived-atom [(:ui/main-container-scroll-top @state/state)] [::lazy-display sub-id] - (fn [_top] - (if (and @*initial? (some? initial-value)) - (do - (reset! *initial? false) - initial-value) - (boolean (hide-block? @*ref))))))) + (fn [top] + (let [prev @*prev-scroll-top + minor-update? (< (abs (- prev top)) 64) + _ (reset! *prev-scroll-top top) + value (cond + (and @*initial? (some? initial-value)) + (do + (reset! *initial? false) + initial-value) + + (and minor-update? (some? @*latest-value)) + @*latest-value + + :else + (boolean (hide-block? @*ref)))] + value))))) (rum/defcs block-item < rum/reactive {:init (fn [state] (let [id (random-uuid) + disable-lazy? (:disable-lazy-load? (first (:rum/args state))) *ref (atom nil) - *hidden? (get-hidden-atom id *ref {:initial-value true})] + *hidden? (if disable-lazy? + (atom false) + (get-hidden-atom id *ref {:initial-value true}))] (assoc state ::sub-id id ::ref *ref ::hidden? *hidden?))) :should-update (fn [old-state new-state] (let [args-1 (:rum/args old-state) args-2 (:rum/args new-state)] - (not= [(first args-1) (last args-1)] - [(first args-2) (last args-2)]))) + (not= [(dissoc (first args-1) :query-result) + (last args-1)] + [(dissoc (first args-2) :query-result) + (last args-2)]))) :did-mount (fn [state] - (let [hide? (hide-block? @(::ref state))] - (reset! (::hidden? state) hide?) - state))} + (if (:disable-lazy-load? (first (:rum/args state))) + state + (let [hide? (hide-block? @(::ref state))] + (reset! (::hidden? state) hide?) + state)))} [state config item opts] (let [*hidden? (::hidden? state) hidden? (rum/react *hidden?) diff --git a/src/main/frontend/handler/block.cljs b/src/main/frontend/handler/block.cljs index 25d5ac8883..d10ff36d6d 100644 --- a/src/main/frontend/handler/block.cljs +++ b/src/main/frontend/handler/block.cljs @@ -373,7 +373,7 @@ [repo block content id text-range {:keys [direction retry-times] :or {retry-times 0} :as opts}] - (when (<= retry-times 6) + (when (<= retry-times 10) (let [block-id (:block/uuid block) edit-input-id (if (uuid? id) (get-edit-input-id-with-block-id id direction retry-times nil) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index d43f87401b..208f030f4a 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -2190,9 +2190,7 @@ (outliner-tx/transact! {:outliner-op :move-blocks :real-outliner-op :indent-outdent} - (outliner-core/move-blocks! [(:data node)] target true)) - (js/setTimeout #(edit-block! (:data node) (state/get-edit-pos) - (:block/uuid (:data node))) 20)))))) + (outliner-core/move-blocks! [(:data node)] target true))))))) (defn- last-top-level-child? [{:keys [id]} current-node] @@ -2777,8 +2775,7 @@ (outliner-tx/transact! {:outliner-op :move-blocks :real-outliner-op :indent-outdent} - (outliner-core/indent-outdent-blocks! [block] indent?)) - (js/setTimeout #(edit-block! block (state/get-edit-pos) (:block/uuid block)) 20)) + (outliner-core/indent-outdent-blocks! [block] indent?))) (state/set-editor-op! :nil))) (defn keydown-tab-handler