Ver Fonte

enhance: clear unused editor state

Tienson Qin há 4 anos atrás
pai
commit
625ec882a9

+ 3 - 7
src/main/frontend/handler/editor.cljs

@@ -1415,8 +1415,7 @@
     (save-block-if-changed! block new-value
                             (merge
                              {:custom-properties properties}
-                             opts))
-    (prn "block saved!")))
+                             opts))))
 
 (defn save-block!
   [{:keys [format block id repo dummy?] :as state} value]
@@ -1424,14 +1423,12 @@
             dummy?)
     (save-block-aux! block value format {})))
 
-(defonce *auto-saving? (atom false))
 (defn save-current-block-when-idle!
   ([]
    (save-current-block-when-idle! {}))
   ([{:keys [check-idle? chan chan-callback]
      :or {check-idle? true}}]
-   (when-not @*auto-saving?
-     (reset! *auto-saving? true)
+   (when (nil? (state/get-editor-op))
      (when-let [repo (state/get-current-repo)]
        (when (and (if check-idle? (state/input-idle? repo) true)
                   (not (state/get-editor-show-page-search?))
@@ -1456,8 +1453,7 @@
                                                                          :chan-callback chan-callback})))
            (catch js/Error error
              (log/error :save-block-failed error)))
-         (state/set-editor-op! nil)))
-     (reset! *auto-saving? false))))
+         (state/set-editor-op! nil))))))
 
 (defn on-up-down
   [state e up?]

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

@@ -43,31 +43,32 @@
 
 (defn undo!
   []
-  (let [route (get-in (:route-match @state/state) [:data :name])]
-    (if (and (contains? #{:home :page :file} route)
-             (state/get-current-repo))
-      (let [repo (state/get-current-repo)
-            chan (async/promise-chan)
-            save-commited? (atom nil)
-            undo-fn (fn []
-                      (history/undo! repo file/alter-file restore-cursor!))]
-        (editor/save-current-block-when-idle! {:check-idle? false
-                                               :chan chan
-                                               :chan-callback (fn []
-                                                                (reset! save-commited? true))})
-        (if @save-commited?
-          (async/go
-            (let [_ (async/<! chan)]
-              ;; FIXME:
-              (js/setTimeout undo-fn 20)))
-          (undo-fn)))
-      (default-undo))))
+  (when-not (state/get-editor-op)
+    (let [route (get-in (:route-match @state/state) [:data :name])]
+      (if (and (contains? #{:home :page :file} route)
+               (state/get-current-repo))
+        (let [repo (state/get-current-repo)
+              chan (async/promise-chan)
+              save-commited? (atom nil)
+              undo-fn (fn []
+                        (history/undo! repo file/alter-file restore-cursor!))]
+          (editor/save-current-block-when-idle! {:check-idle? false
+                                                 :chan chan
+                                                 :chan-callback (fn []
+                                                                  (reset! save-commited? true))})
+          (if @save-commited?
+            (async/go
+              (let [_ (async/<! chan)]
+                (undo-fn)))
+            (undo-fn)))
+        (default-undo)))))
 
 (defn redo!
   []
-  (let [route (get-in (:route-match @state/state) [:data :name])]
-    (if (and (contains? #{:home :page :file} route)
-             (state/get-current-repo))
-      (let [repo (state/get-current-repo)]
-        (history/redo! repo file/alter-file restore-cursor!))
-      (default-redo))))
+  (when-not (state/get-editor-op)
+    (let [route (get-in (:route-match @state/state) [:data :name])]
+     (if (and (contains? #{:home :page :file} route)
+              (state/get-current-repo))
+       (let [repo (state/get-current-repo)]
+         (history/redo! repo file/alter-file restore-cursor!))
+       (default-redo)))))

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

@@ -32,10 +32,10 @@
                        (when (and old new)
                          (let [diffs (diff/diffs new old)
                               patches (diff/get-patches new old diffs)]
-                           [file patches]))))
+                           [file patches {:old old
+                                          :new new}]))))
                 (remove nil?))]
     (when (seq tx)
-      ;; FIXME: it's the new edit block instead of the previous one
       (let [last-edit-block (get @state/state :editor/last-edit-block)
             tx (if last-edit-block
                  {:data tx
@@ -59,6 +59,8 @@
         (let [idx (get @history-idx repo 0)
               idx' (inc idx)
               txs (vec (take idx' (get @history repo)))]
+          ;; TODO: auto-save the block and undo at the same time
+          ;; Should we use core.async channel to force serialization?
           (swap! history assoc repo (conj txs tx))
           (swap! history-idx assoc repo idx'))))))
 
@@ -72,7 +74,6 @@
             tx (get-in @history [repo idx'])
             {:keys [data]} tx
             _ (reset! *undoing? true)
-            ;; _ (state/clear-edit!)
             promises (for [[path patches] data]
                        (let [current-content (db/get-file-no-sub path)
                              original-content (diff/apply-patches! current-content patches)]
@@ -97,7 +98,6 @@
     (when (and (> (count txs) idx) (false? @*redoing?))
       (let [tx (get-in @history [repo idx])
             _ (reset! *redoing? true)
-            ;; _ (state/clear-edit!)
             promises (for [[path patches] (:data tx)]
                        (let [current-content (db/get-file-no-sub path)
                              reversed-patches (utils/reversePatch patches)