From f7bc9549f7e8be52c70c916d2544c13d17cb564e Mon Sep 17 00:00:00 2001 From: Paul Reichert Date: Tue, 18 May 2021 22:01:18 +0200 Subject: [PATCH 1/3] fix #559: undo after pair character input --- src/cljs/athens/views/blocks/textarea_keydown.cljs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cljs/athens/views/blocks/textarea_keydown.cljs b/src/cljs/athens/views/blocks/textarea_keydown.cljs index a2c0d04a5e..a6da0b6b36 100644 --- a/src/cljs/athens/views/blocks/textarea_keydown.cljs +++ b/src/cljs/athens/views/blocks/textarea_keydown.cljs @@ -578,7 +578,10 @@ (= 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) + (.. 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)) @@ -592,7 +595,10 @@ (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) + (.. 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)) From c047812ecd761995dfb8b56b0d9aca7c90f189c1 Mon Sep 17 00:00:00 2001 From: Paul Reichert Date: Wed, 19 May 2021 14:39:14 +0200 Subject: [PATCH 2/3] add comments: execCommand is obsolete --- src/cljs/athens/views/blocks/textarea_keydown.cljs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cljs/athens/views/blocks/textarea_keydown.cljs b/src/cljs/athens/views/blocks/textarea_keydown.cljs index a6da0b6b36..c7dc2b510d 100644 --- a/src/cljs/athens/views/blocks/textarea_keydown.cljs +++ b/src/cljs/athens/views/blocks/textarea_keydown.cljs @@ -578,6 +578,9 @@ (= selection "") (let [new-str (str head key close-pair tail) new-idx (inc start)] (swap! state assoc :string/local 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 @@ -595,6 +598,9 @@ (not= selection "") (let [surround-selection (surround selection key) new-str (str head surround-selection tail)] (swap! state assoc :string/local 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 From ac2126222176efb4e5721062110e8054ef008a78 Mon Sep 17 00:00:00 2001 From: Paul Reichert Date: Wed, 19 May 2021 14:55:11 +0200 Subject: [PATCH 3/3] replace "or" clause with "some" to remove duplication --- src/cljs/athens/views/blocks/textarea_keydown.cljs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cljs/athens/views/blocks/textarea_keydown.cljs b/src/cljs/athens/views/blocks/textarea_keydown.cljs index c7dc2b510d..4ff59cbab5 100644 --- a/src/cljs/athens/views/blocks/textarea_keydown.cljs +++ b/src/cljs/athens/views/blocks/textarea_keydown.cljs @@ -566,14 +566,12 @@ 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)]