diff --git a/src/main/frontend/handler/file.cljs b/src/main/frontend/handler/file.cljs index 937de62fb5..4dc1cec664 100644 --- a/src/main/frontend/handler/file.cljs +++ b/src/main/frontend/handler/file.cljs @@ -190,7 +190,7 @@ :path-params {:path path}})))))) (defn alter-files - [repo files {:keys [add-history? update-status? git-add-cb reset? update-db? chan chan-callback] + [repo files {:keys [add-history? update-status? git-add-cb reset? update-db? chan chan-callback resolved-handler] :or {add-history? true update-status? true reset? false diff --git a/src/main/frontend/history.cljs b/src/main/frontend/history.cljs index 90788be221..c6f0e0ee98 100644 --- a/src/main/frontend/history.cljs +++ b/src/main/frontend/history.cljs @@ -1,6 +1,7 @@ (ns frontend.history (:require [frontend.diff :as diff] [frontend.db :as db] + [promesa.core :as p] ["/frontend/utils" :as utils])) ;; Undo && Redo that works with files @@ -38,31 +39,42 @@ (swap! history assoc repo (conj txs tx)) (swap! history-idx assoc repo idx')))))) +(defonce *undoing? (atom false)) + (defn undo! [repo alter-file] (let [idx (get @history-idx repo 0)] - (when (> idx 0) + (when (and (> idx 0) (false? @*undoing?)) (let [idx' (dec idx) - tx (get-in @history [repo idx'])] - (doseq [[path patches] tx] - (let [current-content (db/get-file-no-sub path) - original-content (diff/apply-patches! current-content patches)] - (alter-file repo path original-content - {:add-history? false - :re-render-root? true}))) - (swap! history-idx assoc repo idx'))))) + tx (get-in @history [repo idx']) + _ (reset! *undoing? true) + promises (for [[path patches] tx] + (let [current-content (db/get-file-no-sub path) + original-content (diff/apply-patches! current-content patches)] + (alter-file repo path original-content + {:add-history? false + :re-render-root? true})))] + (-> (p/all promises) + (p/then (fn [] + (swap! history-idx assoc repo idx') + (reset! *undoing? false)))))))) +(defonce *redoing? (atom false)) (defn redo! [repo alter-file] (let [idx (get @history-idx repo 0) txs (get @history repo)] - (when (> (count txs) idx) - (let [tx (get-in @history [repo idx])] - (doseq [[path patches] tx] - (let [current-content (db/get-file-no-sub path) - reversed-patches (utils/reversePatch patches) - content (diff/apply-patches! current-content reversed-patches)] - (alter-file repo path content - {:add-history? false - :re-render-root? true}))) - (swap! history-idx assoc repo (inc idx)))))) + (when (and (> (count txs) idx) (false? @*redoing?)) + (let [tx (get-in @history [repo idx]) + _ (reset! *redoing? true) + promises (for [[path patches] tx] + (let [current-content (db/get-file-no-sub path) + reversed-patches (utils/reversePatch patches) + content (diff/apply-patches! current-content reversed-patches)] + (alter-file repo path content + {:add-history? false + :re-render-root? true})))] + (-> (p/all promises) + (p/then (fn [] + (swap! history-idx assoc repo (inc idx)) + (reset! *redoing? false))))))))