Skip to content

Commit a00d15d

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 c55d624 commit a00d15d

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;
@@ -1256,10 +1260,23 @@ export class InnerPlayer {
12561260
}
12571261
}
12581262

1259-
private virtualKeyboardInput() {
1260-
const input = this.virtualKeyboard;
1261-
const string = input.value;
1262-
for (const char of string) {
1263+
private virtualKeyboardInput(e: Event) {
1264+
const event = e as InputEvent;
1265+
if (!event || event.isComposing || event.inputType === 'insertCompositionText') {
1266+
// Ignore composing events, we'll get the composed text at the end
1267+
return;
1268+
}
1269+
1270+
this.virtualKeyboardInputText(event.data || "");
1271+
}
1272+
1273+
private virtualKeyboardCompositionEnd(e: Event) {
1274+
const event = e as CompositionEvent;
1275+
this.virtualKeyboardInputText(event.data || "");
1276+
}
1277+
1278+
private virtualKeyboardInputText(text: string) {
1279+
for (const char of text) {
12631280
for (const eventType of ["keydown", "keyup"]) {
12641281
this.element.dispatchEvent(
12651282
new KeyboardEvent(eventType, {
@@ -1269,7 +1286,7 @@ export class InnerPlayer {
12691286
);
12701287
}
12711288
}
1272-
input.value = "";
1289+
this.virtualKeyboard.value = "";
12731290
}
12741291

12751292
protected openVirtualKeyboard(): void {

0 commit comments

Comments
 (0)