Skip to content

Commit 4e7bda0

Browse files
committed
fix(input): broken accents in safari
turns out safari uses a completely different event for composition ending
1 parent f86f253 commit 4e7bda0

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

frontend/src/ts/input/helpers/input-type.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
export type InsertInputType =
22
| "insertText"
33
| "insertCompositionText"
4+
| "insertFromComposition"
45
| "insertLineBreak";
5-
66
export type DeleteInputType = "deleteWordBackward" | "deleteContentBackward";
77

8-
export type SupportedInputType = InsertInputType | DeleteInputType;
9-
8+
type SupportedInputType = InsertInputType | DeleteInputType;
109
const SUPPORTED_INPUT_TYPES: Set<SupportedInputType> = new Set([
1110
"insertText",
1211
"insertCompositionText",
12+
"insertFromComposition",
1313
"insertLineBreak",
1414
"deleteWordBackward",
1515
"deleteContentBackward",

frontend/src/ts/input/listeners/input.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { onDelete } from "../handlers/delete";
22
import { onInsertText } from "../handlers/insert-text";
3-
import {
4-
isSupportedInputType,
5-
SupportedInputType,
6-
} from "../helpers/input-type";
3+
import { isSupportedInputType } from "../helpers/input-type";
74
import { getInputElement } from "../input-element";
85
import {
96
getLastInsertCompositionTextData,
@@ -56,7 +53,10 @@ inputEl.addEventListener("beforeinput", async (event) => {
5653
inputType === "deleteContentBackward"
5754
) {
5855
onBeforeDelete(event);
59-
} else if (inputType === "insertCompositionText") {
56+
} else if (
57+
inputType === "insertCompositionText" ||
58+
inputType === "insertFromComposition"
59+
) {
6060
// firefox fires this extra event which we dont want to handle
6161
if (!event.isComposing) {
6262
event.preventDefault();
@@ -80,11 +80,16 @@ inputEl.addEventListener("input", async (event) => {
8080
value: (event.target as HTMLInputElement).value,
8181
});
8282

83+
// this shouldnt be neccesary because beforeinput already prevents default
84+
// but some browsers (LIKE SAFARI) seem to ignore that, so just double checking here
85+
if (!isSupportedInputType(event.inputType)) {
86+
event.preventDefault();
87+
return;
88+
}
89+
8390
const now = performance.now();
8491

85-
// this is ok to cast because we are preventing default
86-
// in the input listener for unsupported input types
87-
const inputType = event.inputType as SupportedInputType;
92+
const inputType = event.inputType;
8893

8994
if (
9095
(inputType === "insertText" && event.data !== null) ||
@@ -105,7 +110,10 @@ inputEl.addEventListener("input", async (event) => {
105110
inputType === "deleteContentBackward"
106111
) {
107112
onDelete(inputType);
108-
} else if (inputType === "insertCompositionText") {
113+
} else if (
114+
inputType === "insertCompositionText" ||
115+
inputType === "insertFromComposition"
116+
) {
109117
// in case the data is the same as the last one, just ignore it
110118
if (getLastInsertCompositionTextData() !== event.data) {
111119
setLastInsertCompositionTextData(event.data ?? "");

0 commit comments

Comments
 (0)