Files
workinf_Blender_Wasm/web/protocol/ime-composition.ts
mes123456 380cbed4ff
Some checks are pending
M6 deployable RC / quick (push) Waiting to run
M6 deployable RC / chromium (push) Blocked by required conditions
M6 deployable RC / release (push) Blocked by required conditions
Checkpoint web parity through Chromium input tasks
2026-08-19 10:39:03 -04:00

35 lines
1.7 KiB
TypeScript

export const IME_COMPOSITION_SCHEMA_VERSION = 1 as const;
export interface IMECompositionState {
schemaVersion: typeof IME_COMPOSITION_SCHEMA_VERSION;
composing: boolean;
revision: number;
pendingText: string;
lastEvent: "IDLE" | "START" | "UPDATE" | "END";
}
export type IMECompositionEvent =
| { type: "compositionstart"; data?: string }
| { type: "compositionupdate"; data?: string }
| { type: "compositionend"; data?: string };
export function createIMECompositionState(): IMECompositionState {
return { schemaVersion: IME_COMPOSITION_SCHEMA_VERSION, composing: false, revision: 0, pendingText: "", lastEvent: "IDLE" };
}
export function reduceIMEComposition(state: IMECompositionState, event: IMECompositionEvent): IMECompositionState {
if (!state || state.schemaVersion !== IME_COMPOSITION_SCHEMA_VERSION) throw new Error("IME_STATE_INVALID");
const text = typeof event.data === "string" ? event.data : "";
if (event.type === "compositionstart") return { schemaVersion: 1, composing: true, revision: state.revision + 1, pendingText: text, lastEvent: "START" };
if (event.type === "compositionupdate") {
if (!state.composing) return state;
return { schemaVersion: 1, composing: true, revision: state.revision + 1, pendingText: text, lastEvent: "UPDATE" };
}
return { schemaVersion: 1, composing: false, revision: state.revision + 1, pendingText: text, lastEvent: "END" };
}
export function shouldBlockOperatorShortcuts(state: IMECompositionState, eventIsComposing = false): boolean {
if (!state || state.schemaVersion !== IME_COMPOSITION_SCHEMA_VERSION) throw new Error("IME_STATE_INVALID");
return state.composing || eventIsComposing;
}