Skip to content

Commit

Permalink
enhance: restore both route and right sidebar when undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Apr 12, 2023
1 parent 95149e1 commit 593526f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
30 changes: 26 additions & 4 deletions src/main/frontend/handler/history.cljs
Original file line number Diff line number Diff line change
@@ -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!
Expand All @@ -17,21 +19,41 @@
(: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!
[e]
(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))
7 changes: 6 additions & 1 deletion src/main/frontend/modules/editor/undo_redo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

0 comments on commit 593526f

Please sign in to comment.