Browse Source

fix: pause history when restoring cursor and state

Tienson Qin 1 year ago
parent
commit
972e90604b

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

@@ -30,11 +30,13 @@
 
 (defn- restore-cursor-and-state!
   [result]
+  (state/set-state! :history/paused? true)
   (let [{:keys [ui-state-str undo?] :as data} (ldb/read-transit-str result)]
     (if ui-state-str
       (let [{:keys [old-state new-state]} (ldb/read-transit-str ui-state-str)]
         (if undo? (restore-app-state! old-state) (restore-app-state! new-state)))
-      (restore-cursor! data))))
+      (restore-cursor! data)))
+  (state/set-state! :history/paused? false))
 
 (let [*last-request (atom nil)]
   (defn undo!

+ 10 - 9
src/main/frontend/persist_db/browser.cljs

@@ -51,15 +51,16 @@
   (add-watch state/state
              :sync-ui-state
              (fn [_ _ prev current]
-               (let [f (fn [state]
-                         (-> (select-keys state [:ui/sidebar-open? :ui/sidebar-collapsed-blocks :sidebar/blocks])
-                             (assoc :route-data (get-route-data (:route-match state)))))
-                     old-state (f prev)
-                     new-state (f current)]
-                 (when (not= new-state old-state)
-                   (.sync-ui-state worker (state/get-current-repo)
-                                   (ldb/write-transit-str {:old-state old-state
-                                                           :new-state new-state})))))))
+               (when-not @(:history/paused? @state/state)
+                 (let [f (fn [state]
+                           (-> (select-keys state [:ui/sidebar-open? :ui/sidebar-collapsed-blocks :sidebar/blocks])
+                               (assoc :route-data (get-route-data (:route-match state)))))
+                       old-state (f prev)
+                       new-state (f current)]
+                   (when (not= new-state old-state)
+                     (.sync-ui-state worker (state/get-current-repo)
+                                     (ldb/write-transit-str {:old-state old-state
+                                                             :new-state new-state}))))))))
 
 (defn transact!
   [^js worker repo tx-data tx-meta]

+ 1 - 14
src/main/frontend/state.cljs

@@ -153,6 +153,7 @@
       :editor/code-block-context             {}
 
       :db/properties-changed-pages           {}
+      :history/paused?                       (atom false)
       :editor/cursor-range                   (atom nil)
       :editor/container-id                   (atom nil)
       :editor/next-edit-block                (atom nil)
@@ -1342,20 +1343,6 @@ Similar to re-frame subscriptions"
     (doseq [item items]
       (set-state! [:ui/sidebar-collapsed-blocks item] collapsed?))))
 
-(defn get-current-edit-block-and-position
-  []
-  (let [edit-input-id (get-edit-input-id)
-        edit-block (get-edit-block)
-        block-element (when edit-input-id (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block")))
-        container (when block-element
-                    (util/get-block-container block-element))]
-    (when container
-      {:last-edit-block edit-block
-       :container       (gobj/get container "id")
-       :pos             @(:editor/start-pos @state)
-       :end-pos         (or (cursor/pos (gdom/getElement edit-input-id))
-                            (count (:block/content edit-block)))})))
-
 (defn clear-edit!
   []
   (set-state! :editor/editing? {})