Файли
openidea/src/platform/ime.ts
T
ntbang0901 84cfac9c17 fix(editor): keep IME keys away from every text field's key handlers (#2364)
The Enter that commits an input-method candidate (keyCode 229 / isComposing) reached text-field handlers as a plain Enter, so Find in Files, Run Anything and several popups acted on it. Only handlers that called isImeKey themselves were safe. One capture-phase listener now stops IME keys from reaching any <input>/<textarea> handler, without cancelling the default action; xterm's textarea and CodeMirror's contenteditable keep their own IME handling.

Co-authored-by: ntbang0901 <[email protected]>
2026-11-10 03:14:20 +08:00

29 рядки
1.4 KiB
TypeScript

/**
* True while an input method is composing, including the key that ends it: WebKit sends the
* Enter / Space that commits a candidate after `compositionend`, with `isComposing` already
* false and keyCode 229, so a handler checking `isComposing` alone runs that Enter.
*/
export function isImeKey(e: KeyboardEvent): boolean {
return e.isComposing || e.keyCode === 229;
}
function isPlainTextField(target: EventTarget | null): boolean {
if (!(target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement)) return false;
// xterm's hidden textarea has its own IME handling (features/terminal/imeInput.ts).
return !target.closest(".xterm");
}
/**
* Keeps IME keys away from every handler of an `<input>` / `<textarea>`. Each handler that
* reacts to Enter, arrows or Escape must otherwise guard with {@link isImeKey} itself, and
* the ones that forget run the key that picks a candidate (Replace, run a line, check out a
* branch, commit a cell edit). Default actions are untouched, so the input method still works.
*/
export function installImeKeyGuard(target: Window = window): () => void {
const onKeyDown = (e: KeyboardEvent) => {
if (isImeKey(e) && isPlainTextField(e.target)) e.stopImmediatePropagation();
};
target.addEventListener("keydown", onKeyDown, true);
return () => target.removeEventListener("keydown", onKeyDown, true);
}