浏览代码

Switch back to blocks tree instead of virtual list for now

Tienson Qin 3 年之前
父节点
当前提交
b0999efa47

+ 24 - 17
src/main/frontend/components/block.cljs

@@ -2238,8 +2238,7 @@
                     (when pre-block? " pre-block")
                     (when (and card? (not review-cards?)) " shadow-xl"))
         :blockid (str uuid)
-        :haschild (str has-child?)
-        :style {:padding-left (* (- level 1) 24)}}
+        :haschild (str has-child?)}
 
        level
        (assoc :level level)
@@ -2275,7 +2274,7 @@
 
       (block-content-or-editor config block edit-input-id block-id heading-level edit?)]
 
-     ;; (block-children config children collapsed?)
+     (block-children config children collapsed?)
 
      (dnd-separator-wrapper block block-id slide? false false)]))
 
@@ -2877,20 +2876,28 @@
 (rum/defcs lazy-blocks < rum/reactive
   {:init (fn [state]
            (assoc state ::id (str (random-uuid))))}
-  [state config blocks blocks->vec-tree]
-  (let [db-id (:db/id config)]
-    (if (or (:custom-query? config) (not db-id))
-      (let [blocks (blocks->vec-tree blocks)]
-        (block-list config blocks))
-      (let [blocks (tree/blocks-with-level blocks)
-            bottom-reached (fn [] (load-more-blocks! config blocks))]
-        (ui/virtual-list {:style {:height "calc(100vh - 180px)"}
-                          :data (bean/->js blocks)
-                          :end-reached bottom-reached
-                          :overscan 200
-                          :item-content (fn [idx block]
-                                          (let [block (bean/->clj block)]
-                                            (block-item config blocks idx block)))})))))
+  [state config flat-blocks blocks->vec-tree]
+  (let [db-id (:db/id config)
+        blocks (blocks->vec-tree flat-blocks)]
+    (if (:custom-query? config)
+      (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))
+                               (load-more-blocks! config flat-blocks)))
+            dom-id (str "lazy-blocks-" (::id state))]
+        [:div {:id dom-id}
+         (ui/infinite-list
+          "main-content-container"
+          (block-list config blocks)
+          {:on-load bottom-reached
+           :bottom-reached (fn []
+                             (when-let [node (gdom/getElement dom-id)]
+                               (ui/bottom-reached? node 1000)))
+           :has-more true
+           :more "More"})]))))
 
 (rum/defcs blocks-container <
   {:init (fn [state]

+ 17 - 9
src/main/frontend/db/model.cljs

@@ -19,6 +19,13 @@
             [frontend.db.default :as default-db]
             [frontend.util.drawer :as drawer]))
 
+;; lazy loading
+
+(def initial-blocks-length 100)
+
+(def step-loading-blocks 50)
+
+
 ;; TODO: extract to specific models and move data transform logic to the
 ;; corresponding handlers.
 
@@ -555,27 +562,27 @@
 
 ;; TODO: outliners ops should be merged to :save-nodes, :insert-nodes,
 ;; :delete-nodes and :move-nodes
-(defn- build-panigated-blocks-from-cache
+(defn- build-paginated-blocks-from-cache
   [repo-url result outliner-op page-id block-id tx-block-ids scoped-block-id]
   (cond
     (contains? #{:save-node :delete-node :delete-nodes} outliner-op)
     @result
 
-    (contains? #{:insert-node :insert-nodes :save-and-insert-node :collapse-expand-blocks :indent-outdent-nodes
-                 ;; :move-subtree
-                 } outliner-op)
+    (contains? #{:insert-node :insert-nodes :save-and-insert-node
+                 :collapse-expand-blocks :indent-outdent-nodes :move-subtree} outliner-op)
     (let [cached-ids (set (conj (map :db/id @result) page-id))
+          move? (= :move-subtree outliner-op)
           insert? (contains? #{:insert-node :insert-nodes :save-and-insert-node} outliner-op)
           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 (get-prev-open-block first-changed-id)
+          start-id (if move? first-changed-id (get-prev-open-block first-changed-id))
           start-page? (:block/name (db-utils/entity start-id))]
       (when-not start-page?
         (let [previous-blocks (take-while (fn [b] (not= start-id (:db/id b))) @result)
               previous-count (count previous-blocks)
-              limit (max 10 (- 50 previous-count))
+              limit (max step-loading-blocks (- initial-blocks-length previous-count))
               more (get-paginated-blocks-no-cache start-id {:limit limit
                                                             :include-start? true
                                                             :scoped-block-id scoped-block-id})]
@@ -591,7 +598,7 @@
    (get-paginated-blocks repo-url block-id {}))
   ([repo-url block-id {:keys [pull-keys start-block limit use-cache? scoped-block-id]
                        :or {pull-keys '[* :block/_refs]
-                            limit 50
+                            limit initial-blocks-length
                             use-cache? true
                             scoped-block-id nil}}]
    (assert (integer? block-id))
@@ -622,13 +629,14 @@
                                                                 (zipmap (mapv :db/id @result) @result)]))
                            limit (if (and result @result) (+ (count @result) 5) limit)
                            outliner-op (get-in tx-report [:tx-meta :outliner-op])
-                           blocks (build-panigated-blocks-from-cache repo-url result outliner-op page-id block-id tx-block-ids scoped-block-id)
+                           blocks (build-paginated-blocks-from-cache repo-url result outliner-op page-id block-id tx-block-ids scoped-block-id)
                            blocks (or blocks
                                       (get-paginated-blocks-no-cache block-id {:limit limit
                                                                                :include-start? (not page?)
                                                                                :scoped-block-id scoped-block-id}))
                            block-eids (map :db/id blocks)
-                           blocks (if (seq tx-id->block)
+                           blocks (if (and (seq tx-id->block)
+                                           (not (contains? #{:indent-outdent-nodes :move-subtree} outliner-op)))
                                     (map (fn [id]
                                            (or (get tx-id->block id)
                                                (get cached-id->block id)

+ 2 - 9
src/main/frontend/fs/node.cljs

@@ -63,20 +63,13 @@
          (not (contains? #{"excalidraw" "edn" "css"} ext))
          (not (string/includes? path "/.recycle/"))
          (zero? pending-writes))
-        (do
-          (when (util/electron?)
-            (debug/set-ack-step! path :saved-successfully)
-            (debug/ack-file-write! path))
-          (p/let [disk-content (encrypt/decrypt disk-content)]
-            (state/pub-event! [:file/not-matched-from-disk path disk-content content])))
+        (p/let [disk-content (encrypt/decrypt disk-content)]
+          (state/pub-event! [:file/not-matched-from-disk path disk-content content]))
 
         :else
         (->
          (p/let [result (ipc/ipc "writeFile" repo path content)
                  mtime (gobj/get result "mtime")]
-           (when (util/electron?)
-             (debug/set-ack-step! path :saved-successfully)
-             (debug/ack-file-write! path))
            (db/set-file-last-modified-at! repo path mtime)
            (p/let [content (if (encrypt/encrypted-db? (state/get-current-repo))
                              (encrypt/decrypt content)

+ 2 - 7
src/main/frontend/handler/block.cljs

@@ -8,17 +8,12 @@
             [frontend.format.block :as block]
             [frontend.util :as util]))
 
-;; lazy loading
-
-(def initial-blocks-length 50)
-
-(def step-loading-blocks 50)
 
 ;;  Fns
 
 (defn long-page?
   [repo page-id]
-  (>= (db/get-page-blocks-count repo page-id) initial-blocks-length))
+  (>= (db/get-page-blocks-count repo page-id) db-model/initial-blocks-length))
 
 (defn get-block-refs-with-children
   [block]
@@ -109,7 +104,7 @@
             :frontend.db.react/block-and-children
             :frontend.db.react/page-blocks)
         query-k [repo k db-id]
-        option (cond-> {:limit step-loading-blocks}
+        option (cond-> {:limit db-model/step-loading-blocks}
                  block?
                  (assoc :scoped-block-id db-id))
         more-data (->> (db-model/get-paginated-blocks-no-cache start-id option)

+ 4 - 11
src/main/frontend/handler/editor.cljs

@@ -544,22 +544,15 @@
                                                   :block/page :block/journal?]) new-m)
                        (wrap-parse-block))
         sibling? (when block-self? false)]
-    (profile
-     "outliner insert block"
-     (outliner-insert-block! config current-block next-block {:sibling? sibling?}))
+    (outliner-insert-block! config current-block next-block {:sibling? sibling?})
     ;; WORKAROUND: The block won't refresh itself even if the content is empty.
     (when block-self?
       (gobj/set input "value" ""))
-    (profile "ok handler" (ok-handler next-block))))
+    (ok-handler next-block)))
 
 (defn clear-when-saved!
   []
-  (state/set-editor-show-input! nil)
-  (state/set-editor-show-zotero! false)
-  (state/set-editor-show-date-picker! false)
-  (state/set-editor-show-page-search! false)
-  (state/set-editor-show-block-search! false)
-  (state/set-editor-show-template-search! false)
+  (state/clear-editor-show-state!)
   (commands/restore-state true))
 
 (defn get-state
@@ -1836,7 +1829,7 @@
                        (let [nodes (mapv outliner-core/block blocks)]
                          (outliner-core/move-nodes nodes up?)
                          (rehighlight-selected-nodes)
-                         (let [block-node (util/get-first-block-by-id (:block/uuid (first blocks)))]
+                         (when-let [block-node (util/get-first-block-by-id (:block/uuid (first blocks)))]
                            (.scrollIntoView block-node #js {:behavior "smooth" :block "nearest"}))))]
       (if edit-block-id
         (when-let [block (db/pull [:block/uuid edit-block-id])]

+ 1 - 5
src/main/frontend/handler/file.cljs

@@ -131,7 +131,7 @@
 
                 (and (mobile/native-android?) (not= "/" (first file)))
                 file
-                
+
                 (and (mobile/native-ios?) (not= "/" (first file)))
                 file
 
@@ -244,7 +244,6 @@
                           (-> (p/let [_ (or
                                          (util/electron?)
                                          (nfs/check-directory-permission! repo))]
-                                (debug/set-ack-step! path :write-file)
                                 (fs/write-file! repo (config/get-repo-dir repo) path content
                                                 {:old-content original-content}))
                               (p/catch (fn [error]
@@ -301,9 +300,6 @@
       (let [args (async/<! chan)
             files (second args)]
 
-        (doseq [path (map first files)]
-          (debug/set-ack-step! path :start-write-file))
-
         ;; return a channel
         (try
           (<p! (apply alter-files-handler! args))

+ 0 - 2
src/main/frontend/modules/file/core.cljs

@@ -107,8 +107,6 @@
     (assert (some? chan) "File write chan shouldn't be nil")
     (let [chan-callback (:chan-callback opts)]
       (async/put! chan [repo files opts])
-      (doseq [file (map first files)]
-        (debug/set-ack-step! file :pushed-to-channel))
       (when chan-callback
         (chan-callback)))))
 

+ 6 - 7
src/main/frontend/modules/outliner/file.cljs

@@ -53,13 +53,12 @@
 
 (defn sync-to-file
   [{page-db-id :db/id}]
-  ;; (if (nil? page-db-id)
-  ;;   (notification/show!
-  ;;    "Write file failed, can't find the current page!"
-  ;;    :error)
-  ;;   (when-let [repo (state/get-current-repo)]
-  ;;     (async/put! write-chan [repo page-db-id])))
-  )
+  (if (nil? page-db-id)
+    (notification/show!
+     "Write file failed, can't find the current page!"
+     :error)
+    (when-let [repo (state/get-current-repo)]
+      (async/put! write-chan [repo page-db-id]))))
 
 (util/batch write-chan
             batch-write-interval

+ 0 - 7
src/main/frontend/modules/outliner/pipeline.cljs

@@ -7,13 +7,6 @@
 
 (defn updated-page-hook
   [page]
-  (let [page (db/entity (:db/id page))
-        path (:file/path (:block/file page))
-        page-title (or (:block/original-name page)
-                       (:block/name page))]
-    (when (util/electron?)
-      (debug/set-ack-step! path :start-writing)
-      (debug/wait-for-write-ack! page-title path)))
   (file/sync-to-file page))
 
 (defn invoke-hooks

+ 13 - 0
src/main/frontend/state.cljs

@@ -637,6 +637,19 @@
   [value]
   (set-state! :editor/show-zotero value))
 
+;; TODO: refactor, use one state
+(defn clear-editor-show-state!
+  []
+  (swap! state (fn [state]
+                 (assoc state
+                        :editor/show-input nil
+                        :editor/show-zotero false
+                        :editor/show-date-picker? false
+                        :editor/show-block-search? false
+                        :editor/show-template-search? false
+                        :editor/show-page-search? false
+                        :editor/show-page-search-hashtag? false))))
+
 (defn set-edit-input-id!
   [input-id]
   (swap! state update :editor/editing?

+ 0 - 2
src/main/frontend/ui.cljs

@@ -24,7 +24,6 @@
             ["react-textarea-autosize" :as TextareaAutosize]
             ["react-tippy" :as react-tippy]
             ["react-transition-group" :refer [CSSTransition TransitionGroup]]
-            ["react-virtuoso" :refer [Virtuoso]]
             ["@logseq/react-tweet-embed" :as react-tweet-embed]
             [rum.core :as rum]
             [frontend.db-mixins :as db-mixins]
@@ -34,7 +33,6 @@
 (defonce transition-group (r/adapt-class TransitionGroup))
 (defonce css-transition (r/adapt-class CSSTransition))
 (defonce textarea (r/adapt-class (gobj/get TextareaAutosize "default")))
-(defonce virtual-list (r/adapt-class Virtuoso))
 (def resize-provider (r/adapt-class (gobj/get Resize "ResizeProvider")))
 (def resize-consumer (r/adapt-class (gobj/get Resize "ResizeConsumer")))
 (def Tippy (r/adapt-class (gobj/get react-tippy "Tooltip")))