Browse Source

Merge pull request #215 from logseq/refactor/remove-editor-ugly-timers

Remove editor's ugly timers
Tienson Qin 5 years ago
parent
commit
b249234664
2 changed files with 50 additions and 63 deletions
  1. 2 1
      src/main/frontend/components/editor.cljs
  2. 48 62
      src/main/frontend/handler/editor.cljs

+ 2 - 1
src/main/frontend/components/editor.cljs

@@ -650,7 +650,8 @@
                              (page-handler/rename-when-alter-title-propertiy! old-page-name path format content value)
                              (file/alter-file (state/get-current-repo) path (string/trim value)
                                               {:re-render-root? true}))))
-                       (editor-handler/save-block! (get-state state) value)))
+                       (when-not (contains? #{:insert :indent-outdent} (state/get-editor-op))
+                         (editor-handler/save-block! (get-state state) value))))
                    state)}
   [state {:keys [on-hide dummy? node format block block-parent-id]
           :or {dummy? false}

+ 48 - 62
src/main/frontend/handler/editor.cljs

@@ -46,7 +46,6 @@
 (defonce *image-uploading? (atom false))
 (defonce *image-uploading-process (atom 0))
 (defonce *selected-text (atom nil))
-(defonce *async-insert-start (atom false))
 
 (defn modified-time-tx
   [page file]
@@ -623,12 +622,10 @@
                                             {:key :block/change
                                              :data (map (fn [block] (assoc block :block/page page)) blocks)}
                                             [[file-path new-content]])
-                                           (reset! *async-insert-start false))]
+                                           (state/set-editor-op! nil))]
                          ;; Replace with batch transactions
                          (state/add-tx! transact-fn)
 
-                         (reset! *async-insert-start true)
-
                          (let [blocks (remove (fn [block]
                                                 (nil? (:block/content block))) blocks)
                                page-blocks-atom (db/get-page-blocks-cache-atom repo (:db/id page))
@@ -703,7 +700,8 @@
                 ;; Continue to edit the last block
                 (let [blocks (db/get-page-blocks repo (:page/name page))
                       last-block (last blocks)]
-                  (edit-last-block-for-new-page! last-block 0)))))))
+                  (edit-last-block-for-new-page! last-block 0))))))
+        (state/set-editor-op! nil))
 
       file
       (let [file-path (:file/path file)
@@ -753,46 +751,40 @@
       (assoc properties new-marker (util/time-ms))
       properties)))
 
-;; FIXME: temporal fix
-(defonce *skip-save-block? (atom false))
-
 (defn insert-new-block!
   [state]
-  (reset! *skip-save-block? true)
-  (let [aux-fn (fn [] (when-not config/publishing?
-                        (let [{:keys [block value format id config]} (get-state state)
-                              block-id (:block/uuid block)
-                              block (or (db/pull [:block/uuid block-id])
-                                        block)
-                              collapsed? (:block/collapsed? block)
-                              repo (or (:block/repo block) (state/get-current-repo))
-                              last-child (and collapsed?
-                                              (last (db/get-block-and-children-no-cache repo (:block/uuid block))))
-                              last-child (when (not= (:block/uuid last-child)
-                                                     (:block/uuid block))
-                                           last-child)
-                              new-block (or last-child block)
-                              new-value (if last-child (:block/content last-child) value)
-                              properties (with-timetracking-properties new-block new-value)]
-                          ;; save the current block and insert a new block
-                          (insert-new-block-aux!
-                           (assoc new-block :block/properties properties)
-                           new-value
-                           {:create-new-block? true
-                            :ok-handler
-                            (fn [[_first-block last-block _new-block-content]]
-                              (let [last-id (:block/uuid last-block)]
-                                (edit-block! last-block 0 format id)
-                                (clear-when-saved!)))
-                            :with-level? (if last-child true false)
-                            :new-level (and last-child (:block/level block))
-                            :blocks-container-id (:id config)
-                            :current-page (state/get-current-page)})
-
-                          (js/setTimeout #(reset! *skip-save-block? false) 10))))]
-    (if @*async-insert-start
-      (js/setTimeout aux-fn 20)
-      (aux-fn))))
+  (when (and (not config/publishing?)
+             ;; skip this operation if it's inserting
+             (not= :insert (state/get-editor-op)))
+    (state/set-editor-op! :insert)
+    (let [{:keys [block value format id config]} (get-state state)
+          block-id (:block/uuid block)
+          block (or (db/pull [:block/uuid block-id])
+                    block)
+          collapsed? (:block/collapsed? block)
+          repo (or (:block/repo block) (state/get-current-repo))
+          last-child (and collapsed?
+                          (last (db/get-block-and-children-no-cache repo (:block/uuid block))))
+          last-child (when (not= (:block/uuid last-child)
+                                 (:block/uuid block))
+                       last-child)
+          new-block (or last-child block)
+          new-value (if last-child (:block/content last-child) value)
+          properties (with-timetracking-properties new-block new-value)]
+      ;; save the current block and insert a new block
+      (insert-new-block-aux!
+       (assoc new-block :block/properties properties)
+       new-value
+       {:create-new-block? true
+        :ok-handler
+        (fn [[_first-block last-block _new-block-content]]
+          (let [last-id (:block/uuid last-block)]
+            (edit-block! last-block 0 format id)
+            (clear-when-saved!)))
+        :with-level? (if last-child true false)
+        :new-level (and last-child (:block/level block))
+        :blocks-container-id (:id config)
+        :current-page (state/get-current-page)}))))
 
 (defn insert-new-block-without-save-previous!
   [config last-block]
@@ -1324,16 +1316,15 @@
 
 (defn save-block!
   [{:keys [format block id repo dummy?] :as state} value]
-  (when-not @*skip-save-block?
-    (when (or (:db/id (db/entity repo [:block/uuid (:block/uuid block)]))
-              dummy?)
-      (let [value (text/remove-level-spaces value format true)
-            new-value (block/with-levels value format block)
-            properties (with-timetracking-properties block value)]
-        ;; FIXME: somehow frontend.components.editor's will-unmount event will loop forever
-        ;; maybe we shouldn't save the block/file in "will-unmount" event?
-        (save-block-if-changed! block new-value
-                                {:custom-properties properties})))))
+  (when (or (:db/id (db/entity repo [:block/uuid (:block/uuid block)]))
+            dummy?)
+    (let [value (text/remove-level-spaces value format true)
+          new-value (block/with-levels value format block)
+          properties (with-timetracking-properties block value)]
+      ;; FIXME: somehow frontend.components.editor's will-unmount event will loop forever
+      ;; maybe we shouldn't save the block/file in "will-unmount" event?
+      (save-block-if-changed! block new-value
+                              {:custom-properties properties}))))
 
 (defn on-up-down
   [state e up?]
@@ -1598,8 +1589,8 @@
 
 (defn adjust-block-level!
   [state direction]
-  (let [aux-fn (fn []
-                 (let [{:keys [block block-parent-id value config]} (get-state state)
+  (state/set-editor-op! :indent-outdent)
+  (let [{:keys [block block-parent-id value config]} (get-state state)
                        start-level (:start-level config)
                        format (:block/format block)
                        block-pattern (config/get-block-pattern format)
@@ -1625,14 +1616,9 @@
                                      (util/uuid-string? (get config :id)))
                                     (<= final-level start-level)))
                           (<= (- final-level previous-level) 1))
-                     (reset! *skip-save-block? true)
                      (save-block-if-changed! block new-value
-                                             {:indent-left? (= direction :left)})
-                     (js/setTimeout #(reset! *skip-save-block? false) 10))))]
-    ;; TODO: Not a universal solution
-    (if @*async-insert-start
-      (js/setTimeout aux-fn 20)
-      (aux-fn))))
+                                             {:indent-left? (= direction :left)})))
+  (state/set-editor-op! nil))
 
 (defn adjust-blocks-level!
   [blocks direction])