Skip to content

Commit 2af747a

Browse files
committed
web: Ignore IME composing events when inputting text
Basically, if we ignore composing input events, we can just accept the final composed text as key presses (not as text composition). This is the first step towards implementing IME on web. IME mechanics are not being used, no preview is available, but inserting text with IME should work.
1 parent c6b0fb5 commit 2af747a

File tree

1 file changed

+22
-5
lines changed
  • web/packages/core/src/internal/player

1 file changed

+22
-5
lines changed

web/packages/core/src/internal/player/inner.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export class InnerPlayer {
229229
"input",
230230
this.virtualKeyboardInput.bind(this),
231231
);
232+
this.virtualKeyboard.addEventListener(
233+
"compositionend",
234+
this.virtualKeyboardCompositionEnd.bind(this),
235+
);
232236
this.saveManager = this.shadow.getElementById(
233237
"save-manager",
234238
)! as HTMLDivElement;
@@ -1252,10 +1256,23 @@ export class InnerPlayer {
12521256
}
12531257
}
12541258

1255-
private virtualKeyboardInput() {
1256-
const input = this.virtualKeyboard;
1257-
const string = input.value;
1258-
for (const char of string) {
1259+
private virtualKeyboardInput(e: Event) {
1260+
const event = e as InputEvent;
1261+
if (!event || event.isComposing || event.inputType === 'insertCompositionText') {
1262+
// Ignore composing events, we'll get the composed text at the end
1263+
return;
1264+
}
1265+
1266+
this.virtualKeyboardInputText(event.data || "");
1267+
}
1268+
1269+
private virtualKeyboardCompositionEnd(e: Event) {
1270+
const event = e as CompositionEvent;
1271+
this.virtualKeyboardInputText(event.data || "");
1272+
}
1273+
1274+
private virtualKeyboardInputText(text: string) {
1275+
for (const char of text) {
12591276
for (const eventType of ["keydown", "keyup"]) {
12601277
this.element.dispatchEvent(
12611278
new KeyboardEvent(eventType, {
@@ -1265,7 +1282,7 @@ export class InnerPlayer {
12651282
);
12661283
}
12671284
}
1268-
input.value = "";
1285+
this.virtualKeyboard.value = "";
12691286
}
12701287

12711288
protected openVirtualKeyboard(): void {

0 commit comments

Comments
 (0)