fix: undo redo should wait for disk files get changed

This commit is contained in:
Tienson Qin
2021-01-09 10:03:28 +08:00
parent 3c327eb4ba
commit badff45ff8
2 changed files with 32 additions and 20 deletions

View File

@@ -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

View File

@@ -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))))))))