🐛 Keep text editing open when an IME cancels composition

Some IMEs (Fcitx Pinyin) end the composition before Escape is released,
so the key-up no longer looks like part of it and Escape left the text
edition. Remember when a key-down belongs to a composition and ignore
the Escape release that follows. Escape still leaves the edition or
closes the menu otherwise.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-16 16:38:42 +01:00
parent d6d911f690
commit 32548b1125
3 changed files with 51 additions and 5 deletions

View File

@ -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))))

View File

@ -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

View File

@ -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))))