fix(editor): refine file edting, fix state update

- update element state when switching files #3415
- notification when saved
- minor style change
- refine editor element id gen, use uuid
- add trailing newline to saved file

Fix #3415
This commit is contained in:
Andelf
2021-12-19 13:15:19 +08:00
committed by Tienson Qin
parent 6d80a1488f
commit 7024e9b4c3
4 changed files with 28 additions and 11 deletions

View File

@@ -132,6 +132,7 @@
[frontend.extensions.calc :as calc]
[frontend.handler.editor :as editor-handler]
[frontend.handler.file :as file-handler]
[frontend.handler.notification :as notification]
[frontend.state :as state]
[frontend.utf8 :as utf8]
[frontend.util :as util]
@@ -170,13 +171,17 @@
(:file-path config)
(let [path (:file-path config)
content (db/get-file-no-sub path)
value (some-> (gdom/getElement path)
[_ id _ _ _] (:rum/args state)
value (some-> (gdom/getElement id)
(gobj/get "value"))]
(when (and
(not (string/blank? value))
(not= (string/trim value) (string/trim content)))
(file-handler/alter-file (state/get-current-repo) path (string/trim value)
{:re-render-root? true})))
(file-handler/alter-file (state/get-current-repo)
path
(str (string/trim value) "\n")
{:re-render-root? true})
(notification/show! "Saved file!" :success)))
:else
nil))))
@@ -267,10 +272,16 @@
:did-mount (fn [state]
(load-and-render! state)
state)
:will-update (fn [state]
(when-let [editor @(:editor-atom state)]
(.toTextArea ^js editor)
(let [[_ _ _ code _] (:rum/args state)]
(when-let [textarea (rum/ref-node state textarea-ref-name)]
(gobj/set textarea "defaultValue" code)
(gobj/set textarea "value" code))))
state)
:did-update (fn [state]
(when-let [editor @(:editor-atom state)]
;; clear the previous instance
(.toTextArea ^js editor))
(load-and-render! state)
state)}
[state config id attr code theme options]