Skip to content

Commit

Permalink
fix(undo): undo after pair character input (athensresearch#1194)
Browse files Browse the repository at this point in the history
* fix athensresearch#559: undo after pair character input

* add comments: execCommand is obsolete

* replace "or" clause with "some" to remove duplication

Co-authored-by: jeff <[email protected]>
  • Loading branch information
2 people authored and korlaism committed Jul 19, 2021
1 parent 48e22bf commit 8f7f6e2
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/cljs/athens/views/blocks/textarea_keydown.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -566,19 +566,23 @@
close-pair (get PAIR-CHARS key)
lookbehind-char (nth value start nil)]
(.. e preventDefault)

(cond
;; when close char, increment caret index without writing more
(or (= ")" key lookbehind-char)
(= "}" key lookbehind-char)
(= "\"" key lookbehind-char)
(= "]" key lookbehind-char)) (do (setStart target (inc start))
(swap! state assoc :search/type nil))
(some #(= % key lookbehind-char)
[")" "}" "\"" "]"]) (do (setStart target (inc start))
(swap! state assoc :search/type nil))

(= selection "") (let [new-str (str head key close-pair tail)
new-idx (inc start)]
(swap! state assoc :string/local new-str)
(set! (.-value target) new-str)
;; execCommand is obsolete:
;; be wary before updating electron - as chromium might drop support for execCommand
;; electron 11 - uses chromium < 90(latest) which supports execCommand
(.. js/document (execCommand
"insertText"
false
(str key close-pair)))
(set-cursor-position target new-idx)
(when (>= (count (:string/local @state)) 4)
(let [four-char (subs (:string/local @state) (dec start) (+ start 3))
Expand All @@ -592,7 +596,13 @@
(not= selection "") (let [surround-selection (surround selection key)
new-str (str head surround-selection tail)]
(swap! state assoc :string/local new-str)
(set! (.-value target) new-str)
;; execCommand is obsolete:
;; be wary before updating electron - as chromium might drop support for execCommand
;; electron 11 - uses chromium < 90(latest) which supports execCommand
(.. js/document (execCommand
"insertText"
false
surround-selection))
(set! (.-selectionStart target) (inc start))
(set! (.-selectionEnd target) (inc end))
(let [four-char (str (subs (:string/local @state) (dec start) (inc start))
Expand Down

0 comments on commit 8f7f6e2

Please sign in to comment.