瀏覽代碼

enhance: restore both route and right sidebar when undo/redo

Tienson Qin 2 年之前
父節點
當前提交
593526f578
共有 2 個文件被更改,包括 32 次插入5 次删除
  1. 26 4
      src/main/frontend/handler/history.cljs
  2. 6 1
      src/main/frontend/modules/editor/undo_redo.cljs

+ 26 - 4
src/main/frontend/handler/history.cljs

@@ -1,9 +1,11 @@
 (ns ^:no-doc frontend.handler.history
   (:require [frontend.db :as db]
+            [frontend.state :as state]
             [frontend.handler.editor :as editor]
             [frontend.modules.editor.undo-redo :as undo-redo]
             [frontend.state :as state]
             [frontend.util :as util]
+            [frontend.handler.route :as route-handler]
             [goog.dom :as gdom]))
 
 (defn restore-cursor!
@@ -17,14 +19,33 @@
                               (:block/uuid block)
                               {:custom-content (:block/content block)}))))))
 
+(defn- get-route-data
+  [route-match]
+  (when (seq route-match)
+    {:to (get-in route-match [:data :name])
+     :path-params (:path-params route-match)
+     :query-params (:query-params route-match)}))
+
+(defn restore-app-state!
+  [state]
+  (let [route-match (:route-match state)
+        current-route (:route-match @state/state)
+        prev-route-data (get-route-data route-match)
+        current-route-data (get-route-data current-route)]
+    (when (and (not= prev-route-data current-route-data)
+               prev-route-data)
+      (route-handler/redirect! prev-route-data))
+    (swap! state/state merge state)))
+
 (defn undo!
   [e]
   (util/stop e)
   (state/set-editor-op! :undo)
   (state/clear-editor-action!)
   (editor/save-current-block!)
-  (let [{:keys [editor-cursor]} (undo-redo/undo)]
-    (restore-cursor! editor-cursor))
+  (let [{:keys [editor-cursor app-state]} (undo-redo/undo)]
+    (restore-cursor! editor-cursor)
+    (restore-app-state! app-state))
   (state/set-editor-op! nil))
 
 (defn redo!
@@ -32,6 +53,7 @@
   (util/stop e)
   (state/set-editor-op! :redo)
   (state/clear-editor-action!)
-  (let [{:keys [editor-cursor]} (undo-redo/redo)]
-    (restore-cursor! editor-cursor))
+  (let [{:keys [editor-cursor app-state]} (undo-redo/redo)]
+    (restore-cursor! editor-cursor)
+    (restore-app-state! app-state))
   (state/set-editor-op! nil))

+ 6 - 1
src/main/frontend/modules/editor/undo_redo.cljs

@@ -176,5 +176,10 @@
                     :txs tx-data
                     :tx-meta tx-meta
                     :editor-cursor (:editor-cursor tx-meta)
-                    :pagination-blocks-range (get-in [:ui/pagination-blocks-range (get-in tx-report [:db-after :max-tx])] @state/state)}]
+                    :pagination-blocks-range (get-in [:ui/pagination-blocks-range (get-in tx-report [:db-after :max-tx])] @state/state)
+                    :app-state (select-keys @state/state
+                                            [:route-match
+                                             :ui/sidebar-open?
+                                             :ui/sidebar-collapsed-blocks
+                                             :sidebar/blocks])}]
         (push-undo entity)))))