дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
Fixes #7386. macOS Korean 2-Set commits a Space typed after Hangul as a one-character composition, and typedImeCommit (#6608) removed it as a Pinyin syllable space, in the editor and the terminal. Only spaces between non-space characters now count as syllable separators, so ni hao still becomes nihao and a lone Space is kept. Co-authored-by: kmsdoit <[email protected]>
42 рядки
2.1 KiB
TypeScript
42 рядки
2.1 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);
|
|
}
|
|
|
|
/**
|
|
* The text a composition commits, without the syllable spaces an IME displays when it commits
|
|
* its raw letters (the commit is the composition text itself and plain ASCII): switching macOS
|
|
* Pinyin to English mid-composition commits `ni hao` for `nihao`. The user never typed those
|
|
* spaces: Space picks a candidate while composing. Candidate commits are returned unchanged.
|
|
* Only spaces between letters are syllable separators: macOS Korean 2-Set commits a Space typed
|
|
* after Hangul as a composition of its own (` `), and the user did type that one.
|
|
*/
|
|
export function typedImeCommit(commit: string, composition: string): string {
|
|
if (commit !== composition || !/^[\x20-\x7e]+$/.test(commit)) return commit;
|
|
return commit.replace(/(\S) +(?=\S)/g, "$1");
|
|
}
|