Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

fix: don't submit on IME composition Enter (Chinese / Japanese / Korean input)

nash_su7b46463ad373
diff --git a/src/components/chat/chat-input.tsx b/src/components/chat/chat-input.tsx
index 12e13da6..74851167 100644
--- a/src/components/chat/chat-input.tsx
+++ b/src/components/chat/chat-input.tsx
@@ -1,6 +1,7 @@
import { useRef, useState, useCallback } from "react"
import { Send, Square } from "lucide-react"
import { Button } from "@/components/ui/button"
+import { isImeComposing } from "@/lib/keyboard-utils"
interface ChatInputProps {
onSend: (text: string) => void
@@ -32,6 +33,11 @@ export function ChatInput({ onSend, onStop, isStreaming, placeholder }: ChatInpu
const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
+ // Don't submit on the Enter that commits an IME candidate —
+ // the user is mid-composition (Chinese / Japanese / Korean
+ // input method picking an English word or phrase) and would
+ // see the message fire before they finished typing.
+ if (isImeComposing(e)) return
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault()
handleSend()
diff --git a/src/components/layout/research-panel.tsx b/src/components/layout/research-panel.tsx
index daa689de..459f3dcc 100644
--- a/src/components/layout/research-panel.tsx
+++ b/src/components/layout/research-panel.tsx
@@ -14,6 +14,7 @@ import { useWikiStore } from "@/stores/wiki-store"
import { readFile } from "@/commands/fs"
import { queueResearch } from "@/lib/deep-research"
import { normalizePath } from "@/lib/path-utils"
+import { isImeComposing } from "@/lib/keyboard-utils"
export function ResearchPanel() {
const tasks = useResearchStore((s) => s.tasks)
@@ -64,7 +65,10 @@ export function ResearchPanel() {
<input
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
- onKeyDown={(e) => { if (e.key === "Enter") handleStartResearch() }}
+ onKeyDown={(e) => {
+ if (isImeComposing(e)) return
+ if (e.key === "Enter") handleStartResearch()
+ }}
placeholder="Enter a research topic..."
className="flex-1 rounded border bg-background px-2 py-1 text-xs outline-none placeholder:text-muted-foreground focus:ring-1 focus:ring-ring"
/>
diff --git a/src/components/search/search-view.tsx b/src/components/search/search-view.tsx
index ddf2bd2f..d4a44a98 100644
--- a/src/components/search/search-view.tsx
+++ b/src/components/search/search-view.tsx
@@ -7,6 +7,7 @@ import { useTranslation } from "react-i18next"
import { normalizePath } from "@/lib/path-utils"
import { resolveMarkdownImageSrc } from "@/lib/markdown-image-resolver"
import { findRawSourceForImage, imageUrlToAbsolute } from "@/lib/raw-source-resolver"
+import { isImeComposing } from "@/lib/keyboard-utils"
/**
* One image hit displayed in the Images section.
@@ -193,7 +194,10 @@ export function SearchView() {
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
- onKeyDown={(e) => { if (e.key === "Enter") doSearch(query) }}
+ onKeyDown={(e) => {
+ if (isImeComposing(e)) return
+ if (e.key === "Enter") doSearch(query)
+ }}
placeholder={t("search.placeholder") + " (Enter to search)"}
autoFocus
className="w-full rounded-md border bg-background py-2 pl-9 pr-3 text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring"
diff --git a/src/lib/keyboard-utils.test.ts b/src/lib/keyboard-utils.test.ts
new file mode 100644
index 00000000..0eb99bbc
--- /dev/null
+++ b/src/lib/keyboard-utils.test.ts
@@ -0,0 +1,44 @@
+import { describe, it, expect } from "vitest"
+import { isImeComposing } from "./keyboard-utils"
+
+// Build a minimal stand-in for a React KeyboardEvent. Vitest's jsdom
+// environment doesn't fire real composition events, so we synthesize
+// the two signals isImeComposing inspects.
+function ke(opts: {
+ isComposing?: boolean
+ keyCode?: number
+ key?: string
+}): React.KeyboardEvent {
+ return {
+ key: opts.key ?? "Enter",
+ keyCode: opts.keyCode ?? 13,
+ nativeEvent: { isComposing: opts.isComposing ?? false } as unknown as KeyboardEvent,
+ } as unknown as React.KeyboardEvent
+}
+
+describe("isImeComposing", () => {
+ it("is false for a plain Enter press with no IME activity", () => {
+ expect(isImeComposing(ke({ key: "Enter", keyCode: 13 }))).toBe(false)
+ })
+
+ it("is true when the W3C `isComposing` flag is set", () => {
+ expect(isImeComposing(ke({ isComposing: true, keyCode: 229 }))).toBe(true)
+ })
+
+ it("is true when keyCode === 229 even after isComposing has flipped back", () => {
+ // The commit-press itself: Chromium reports keyCode 229 but
+ // isComposing has already cleared. Without this branch the
+ // commit Enter leaks through as a submit.
+ expect(isImeComposing(ke({ isComposing: false, keyCode: 229 }))).toBe(true)
+ })
+
+ it("is true for non-Enter keys during composition", () => {
+ // Defensive: arrow keys, escape, etc. during composition should
+ // also be treated as IME-owned by callers that care.
+ expect(isImeComposing(ke({ key: "ArrowDown", isComposing: true, keyCode: 229 }))).toBe(true)
+ })
+
+ it("is false for Shift+Enter (no IME)", () => {
+ expect(isImeComposing(ke({ key: "Enter", keyCode: 13 }))).toBe(false)
+ })
+})
diff --git a/src/lib/keyboard-utils.ts b/src/lib/keyboard-utils.ts
new file mode 100644
index 00000000..17fa5519
--- /dev/null
+++ b/src/lib/keyboard-utils.ts
@@ -0,0 +1,24 @@
+/**
+ * Returns true when a keydown event is part of an IME composition
+ * (Chinese / Japanese / Korean input methods). When the user is
+ * composing — e.g. typing English letters under a Chinese input
+ * method to pick a candidate — pressing Enter commits the
+ * candidate, but the same Enter keydown ALSO bubbles up to the
+ * input element and gets misread by `if (e.key === "Enter")`
+ * handlers as a "submit" intent. Result: the message sends
+ * before the user actually finished typing.
+ *
+ * Both signals are required because no single one is reliable:
+ * - `nativeEvent.isComposing` — W3C standard, true while the
+ * IME is composing. Cleared by the time the commit-press
+ * fires in some browsers.
+ * - `keyCode === 229` — the legacy "IME activity" signal that
+ * Chromium continues to emit on the commit-press itself,
+ * after `isComposing` has already flipped back to false.
+ *
+ * Use this in every Enter-as-submit handler on a text input so
+ * IME composition Enter never leaks through as a submit.
+ */
+export function isImeComposing(e: React.KeyboardEvent): boolean {
+ return e.nativeEvent.isComposing || e.keyCode === 229
+}