fix: code block editing

This commit is contained in:
Tienson Qin
2021-05-16 21:57:17 +08:00
parent c9943eb680
commit 8d6b937415
10 changed files with 95 additions and 113 deletions

View File

@@ -51,42 +51,37 @@
[editor textarea config state]
(.save editor)
(let [value (gobj/get textarea "value")
default-value (gobj/get textarea "defaultValue")
pos-meta (:pos-meta state)]
default-value (gobj/get textarea "defaultValue")]
(when (not= value default-value)
(cond
(:block/uuid config)
(let [block (db/pull [:block/uuid (:block/uuid config)])
format (:block/format block)
content (:block/content block)
{:keys [start_pos end_pos]} @pos-meta
prev-content (utf8/substring (utf8/encode content)
0 start_pos)
value (str (if (not= "\n" (last prev-content))
"\n")
(string/trimr value)
"\n")
content' (utf8/insert! content start_pos end_pos value)]
(editor-handler/save-block-if-changed! block content')
(let [new-pos-meta {:start_pos start_pos
:end_pos (+ start_pos
(utf8/length (utf8/encode value)))}
old-pos-meta @pos-meta]
(reset! pos-meta new-pos-meta)))
(:block/uuid config)
(let [block (db/pull [:block/uuid (:block/uuid config)])
format (:block/format block)
content (:block/content block)
full-content (:full_content (last (:rum/args state)))]
(when (and full-content (string/includes? content full-content))
(let [lines (string/split-lines full-content)
fl (first lines)
ll (last lines)]
(when (and fl ll)
(let [value' (str fl "\n" value "\n" ll)
;; FIXME: What if there're multiple code blocks with the same value?
content' (string/replace-first content full-content value')]
(editor-handler/save-block-if-changed! block content'))))))
(:file-path config)
(let [path (:file-path config)
content (db/get-file-no-sub path)
value (some-> (gdom/getElement path)
(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-path config)
(let [path (:file-path config)
content (db/get-file-no-sub path)
value (some-> (gdom/getElement path)
(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})))
:else
nil))))
:else
nil))))
(defn- text->cm-mode
[text]
@@ -114,7 +109,10 @@
(let [editor-atom (:editor-atom state)
esc-pressed? (atom nil)]
(if @editor-atom
@editor-atom
(let [editor @editor-atom
doc (.getDoc editor)
code (nth (:rum/args state) 3)]
(.setValue doc code))
(let [[config id attr code] (:rum/args state)
original-mode (get attr :data-lang)
mode (or original-mode "javascript")
@@ -133,16 +131,12 @@
:lineNumbers true
:styleActiveLine true
:extraKeys #js {"Esc" (fn [cm]
(let [save! #(save-file-or-block-when-blur-or-esc! cm textarea config state)]
(if-let [block-id (:block/uuid config)]
(let [block (db/pull [:block/uuid block-id])
value (.getValue cm)
textarea-value (gobj/get textarea "value")
changed? (not= value textarea-value)]
(if changed?
(save!)
(editor-handler/edit-block! block :max (:block/format block) block-id)))
(save!)))
(save-file-or-block-when-blur-or-esc! cm textarea config state)
(when-let [block-id (:block/uuid config)]
(let [block (db/pull [:block/uuid block-id])
value (.getValue cm)
textarea-value (gobj/get textarea "value")]
(editor-handler/edit-block! block :max (:block/format block) block-id)))
;; TODO: return "handled" or false doesn't always prevent event bubbles
(reset! esc-pressed? true)
(js/setTimeout #(reset! esc-pressed? false) 10))}})))]
@@ -150,38 +144,35 @@
(let [element (.getWrapperElement editor)]
(.on editor "blur" (fn [_cm e]
(util/stop e)
(state/set-block-component-editing-mode! false)
(when-not @esc-pressed?
(save-file-or-block-when-blur-or-esc! editor textarea config state))))
(.addEventListener element "click"
(.addEventListener element "mousedown"
(fn [e]
(util/stop e)))
(state/clear-selection!)
(util/stop e)
(state/set-block-component-editing-mode! true)))
(.save editor)
(.refresh editor)))
editor))))
(defn- load-and-render!
[state]
(let [editor-atom (:editor-atom state)]
(let [editor (render! state)]
(reset! editor-atom editor))))
(let [editor-atom (:editor-atom state)
editor (render! state)]
(reset! editor-atom editor)))
(rum/defcs editor < rum/reactive
{:init (fn [state]
(assoc state
:pos-meta (atom (last (:rum/args state)))
:editor-atom (atom nil)))
(assoc state :editor-atom (atom nil)))
:did-mount (fn [state]
(load-and-render! state)
state)
:did-remount (fn [old_state state]
(load-and-render! state)
state)}
[state config id attr code pos-meta]
:did-update (fn [state]
(load-and-render! state)
state)}
[state config id attr code options]
[:div.extensions__code
{:on-mouse-down (fn [e]
(util/stop e)
(state/set-block-component-editing-mode! true))
:on-blur #(state/set-block-component-editing-mode! false)}
[:div.extensions__code-lang
(let [mode (string/lower-case (get attr :data-lang "javascript"))]
(if (= mode "text/x-clojure")