Browse Source

fix: lazy loading in block embeds

Tienson Qin 3 years ago
parent
commit
b6e02e18b1

+ 13 - 9
src/main/frontend/components/block.cljs

@@ -577,13 +577,15 @@
 (rum/defc block-embed < rum/reactive db-mixins/query
   [config uuid]
   (when-let [block (db/entity [:block/uuid uuid])]
-    (let [blocks (db/get-paginated-blocks (state/get-current-repo) (:db/id block))]
+    (let [blocks (db/get-paginated-blocks (state/get-current-repo) (:db/id block)
+                                          {:scoped-block-id (:db/id block)})]
       [:div.color-level.embed-block.bg-base-2
        {:style {:z-index 2}
         :on-double-click #(edit-parent-block % config)
         :on-mouse-down (fn [e] (.stopPropagation e))}
        [:div.px-3.pt-1.pb-2
         (blocks-container blocks (assoc config
+                                        :db/id (:db/id block)
                                         :id (str uuid)
                                         :embed-id uuid
                                         :embed? true
@@ -2877,12 +2879,12 @@
   [config flat-blocks]
   (when-let [db-id (:db/id config)]
     (let [last-block-id (:db/id (last flat-blocks))]
-      (block-handler/load-more! (:block? config)
-                               db-id
-                               last-block-id))))
+      (block-handler/load-more! db-id last-block-id))))
 
 (rum/defcs lazy-blocks < rum/reactive
-  {:did-remount (fn [_old-state new-state]
+  {:init (fn [state]
+           (assoc state ::id (str (random-uuid))))
+   :did-remount (fn [_old-state new-state]
                   ;; Loading more when pressing Enter, expand blocks or paste
                   (let [[config flat-blocks _] (:rum/args new-state)]
                     (when-not (:db/id config)
@@ -2906,15 +2908,17 @@
                            (load-more-blocks! config flat-blocks)
                            (reset! *last-idx idx)))
         has-more? (if db-id
-                    (not= last-block-id (model/get-block-last-child db-id))
-                    (>= (count flat-blocks) (inc idx)))]
-    [:div#lazy-blocks
+                    (and (not= last-block-id (model/get-block-last-child db-id))
+                         (not= last-block-id db-id))
+                    (>= (count flat-blocks) (inc idx)))
+        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 "lazy-blocks")]
+                         (when-let [node (gdom/getElement dom-id)]
                            (ui/bottom-reached? node 1000)))
        :has-more has-more?
        :more (if (or (:preview? config) (:sidebar? config))

+ 10 - 9
src/main/frontend/components/journal.cljs

@@ -68,15 +68,16 @@
 (rum/defc journals < rum/reactive
   [latest-journals]
   [:div#journals
-   (ui/infinite-list "main-content-container"
-                     (for [{:block/keys [name format]} latest-journals]
-                       [:div.journal-item.content {:key name}
-                        (journal-cp [name format])])
-                     {:has-more (page-handler/has-more-journals?)
-                      :more-class "text-4xl"
-                      :on-top-reached page-handler/create-today-journal!
-                      :on-load (fn []
-                                 (page-handler/load-more-journals!))})])
+   (ui/infinite-list
+    "main-content-container"
+    (for [{:block/keys [name format]} latest-journals]
+      [:div.journal-item.content {:key name}
+       (journal-cp [name format])])
+    {:has-more (page-handler/has-more-journals?)
+     :more-class "text-4xl"
+     :on-top-reached page-handler/create-today-journal!
+     :on-load (fn []
+                (page-handler/load-more-journals!))})])
 
 (rum/defc all-journals < rum/reactive db-mixins/query
   []

+ 2 - 1
src/main/frontend/extensions/srs.cljs

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

+ 3 - 1
src/main/frontend/handler/block.cljs

@@ -101,8 +101,10 @@
   (get-timestamp block "Deadline"))
 
 (defn load-more!
-  [block? db-id start-id]
+  [db-id start-id]
   (let [repo (state/get-current-repo)
+        block (db/entity repo db-id)
+        block? (not (:block/name block))
         k (if block?
             :frontend.db.react/block-and-children
             :frontend.db.react/page-blocks)