diff --git a/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs b/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs index aeae20d242..dfb2f3a556 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs @@ -150,6 +150,13 @@ (or (.-isComposing native) (= 229 (.-keyCode event))))) +(defn- editor-escape? + [^js event ime-key?] + (and (kbd/esc? event) + (not ime-key?) + (not (.-isComposing event)) + (not= 229 (.-keyCode event)))) + (defn- secondary-button? "True for a secondary click, which opens the context menu: the right button, or the macOS Ctrl+Click that stands in for it and reports button 0." @@ -196,6 +203,10 @@ ;; and shortcuts wait until the IME commits or cancels. composing-ref (mf/use-ref false) + ;; Native IMEs can end composition before the cancelling key's key-up. + ;; Remember its key-down so that Escape only cancels the preedit. + ime-key-ref (mf/use-ref false) + ;; Number of characters the browser is about to replace via marked text ;; (macOS press-and-hold accent menu). Set on `beforeinput`, consumed on ;; `input`. See on-before-input / on-input. @@ -300,6 +311,9 @@ on-key-down (mf/use-fn (fn [^js event] + (mf/set-ref-val! ime-key-ref + (or (mf/ref-val composing-ref) + (composing-event? event))) (when (and (text-editor/text-editor-has-focus?) (not (mf/ref-val composing-ref)) (not (composing-event? event))) @@ -635,11 +649,14 @@ (let [on-key-up (fn [event] (when (kbd/esc? event) (dom/stop-propagation event) - ;; With the menu open, Escape only closes it (checked - ;; in the DOM: the store may already be cleared). - (if (some? (dom/query menu-selector)) - (st/emit! dw/hide-context-menu) - (st/emit! (dw/clear-edition-mode)))))] + (when (editor-escape? event + (or (mf/ref-val composing-ref) + (mf/ref-val ime-key-ref))) + ;; With the menu open, Escape only closes it (checked + ;; in the DOM: the store may already be cleared). + (if (some? (dom/query menu-selector)) + (st/emit! dw/hide-context-menu) + (st/emit! (dw/clear-edition-mode))))))] (.addEventListener js/document "keyup" on-key-up) #(.removeEventListener js/document "keyup" on-key-up)))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 611fa0dfff..8fa06ed9bb 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -184,6 +184,7 @@ [frontend-tests.ui.subscription-test] [frontend-tests.ui.team-container-test] [frontend-tests.ui.text-attrs-multiple-test] + [frontend-tests.ui.text-v3-editor-test] [frontend-tests.ui.viewport-top-bar-test] [frontend-tests.ui.workspace-libraries-test] [frontend-tests.util-clipboard-test] @@ -406,6 +407,7 @@ 'frontend-tests.ui.subscription-test 'frontend-tests.ui.team-container-test 'frontend-tests.ui.text-attrs-multiple-test + 'frontend-tests.ui.text-v3-editor-test 'frontend-tests.ui.viewport-top-bar-test 'frontend-tests.ui.workspace-libraries-test 'frontend-tests.util-clipboard-test diff --git a/frontend/test/frontend_tests/ui/text_v3_editor_test.cljs b/frontend/test/frontend_tests/ui/text_v3_editor_test.cljs new file mode 100644 index 0000000000..845b64ffa6 --- /dev/null +++ b/frontend/test/frontend_tests/ui/text_v3_editor_test.cljs @@ -0,0 +1,27 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC Sucursal en EspaƱa SL + +(ns frontend-tests.ui.text-v3-editor-test + "Keys of the WASM text editor input." + (:require + [app.main.ui.workspace.shapes.text.v3-editor :as v3-editor] + [cljs.test :as t :include-macros true])) + +(t/deftest escape-that-cancels-native-composition-keeps-text-editing + ;; Fcitx Pinyin ends composition before releasing Escape. That key-up no + ;; longer reports isComposing, so remember that its key-down belonged to IME. + (t/is (false? (@#'v3-editor/editor-escape? + #js {:key "Escape" :isComposing false :keyCode 27} true))) + (t/is (false? (@#'v3-editor/editor-escape? + #js {:key "Escape" :isComposing true :keyCode 27} false))) + (t/is (false? (@#'v3-editor/editor-escape? + #js {:key "Escape" :isComposing false :keyCode 229} false)))) + +(t/deftest ordinary-escape-still-exits-text-editing + (t/is (true? (@#'v3-editor/editor-escape? + #js {:key "Escape" :isComposing false :keyCode 27} false))) + (t/is (false? (@#'v3-editor/editor-escape? + #js {:key "Enter" :isComposing false :keyCode 13} false))))