Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): preserve IME composition in page search (#3769)
Stella-xixi57f3cfc3d4bd
diff --git a/apps/desktop/src/renderer/components/find-in-page/FindInPageBar.tsx b/apps/desktop/src/renderer/components/find-in-page/FindInPageBar.tsxindex b28e36507f..72f73a5f19 100644--- a/apps/desktop/src/renderer/components/find-in-page/FindInPageBar.tsx+++ b/apps/desktop/src/renderer/components/find-in-page/FindInPageBar.tsx@@ -25,6 +25,10 @@ export function FindInPageBar() {const [matches, setMatches] = useState(0);const [active, setActive] = useState(0);const inputRef = useRef<HTMLInputElement>(null);+ const isComposingRef = useRef(false);+ // Chromium may emit the final input/change after compositionend. Remember+ // that value so the committed IME text starts exactly one native search.+ const compositionCommitRef = useRef<string | null>(null);// Track the requestId returned by `findInPage` so we can ignore stale// result events from an earlier query (Chromium fires `found-in-page`// multiple times per request as the search progresses).@@ -53,6 +57,8 @@ export function FindInPageBar() {setText('');setMatches(0);setActive(0);+ isComposingRef.current = false;+ compositionCommitRef.current = null;lastRequestIdRef.current = null;window.electronAPI.stopFindInPage('clearSelection');}, []);@@ -73,8 +79,8 @@ export function FindInPageBar() {return true;});- // Run a search. `findNext=true` walks within the current term; false starts- // a fresh search (used when the text changes).+ // Electron uses `findNext=true` to start a new request and false to continue+ // walking the current search session.const runSearch = useCallback(async (nextText: string, opts: { forward?: boolean; findNext?: boolean } = {}) => {if (!nextText) {@@ -87,7 +93,7 @@ export function FindInPageBar() {const id = await window.electronAPI.findInPage({text: nextText,forward: opts.forward ?? true,- findNext: opts.findNext ?? false,+ findNext: opts.findNext ?? true,});if (typeof id === 'number') {lastRequestIdRef.current = id;@@ -123,16 +129,39 @@ export function FindInPageBar() {placeholder={t('findInPage.placeholder')}onChange={(e) => {const next = e.target.value;+ const nativeIsComposing =+ 'isComposing' in e.nativeEvent && e.nativeEvent.isComposing === true;setText(next);- void runSearch(next, { forward: true, findNext: false });+ if (isComposingRef.current || nativeIsComposing) return;+ if (compositionCommitRef.current !== null) {+ const committed = compositionCommitRef.current;+ compositionCommitRef.current = null;+ if (next === committed) return;+ }+ void runSearch(next, { forward: true, findNext: true });+ }}+ onCompositionStart={() => {+ isComposingRef.current = true;+ compositionCommitRef.current = null;+ }}+ onCompositionEnd={(e) => {+ const committed = e.currentTarget.value;+ isComposingRef.current = false;+ compositionCommitRef.current = committed;+ setText(committed);+ void runSearch(committed, { forward: true, findNext: true });}}onKeyDown={(e) => {if (e.key === 'Escape') {e.preventDefault();close();- } else if (e.key === 'Enter' && !e.nativeEvent.isComposing) {+ } else if (+ e.key === 'Enter' &&+ !e.nativeEvent.isComposing &&+ e.nativeEvent.keyCode !== 229+ ) {e.preventDefault();- if (text) void runSearch(text, { forward: !e.shiftKey, findNext: true });+ if (text) void runSearch(text, { forward: !e.shiftKey, findNext: false });}}}className={cn(@@ -152,7 +181,7 @@ export function FindInPageBar() {type="button"aria-label={t('findInPage.previous')}disabled={!text || matches === 0}- onClick={() => void runSearch(text, { forward: false, findNext: true })}+ onClick={() => void runSearch(text, { forward: false, findNext: false })}className={cn('flex h-6 w-6 items-center justify-center rounded','hover:bg-titlebar-button-hover',@@ -166,7 +195,7 @@ export function FindInPageBar() {type="button"aria-label={t('findInPage.next')}disabled={!text || matches === 0}- onClick={() => void runSearch(text, { forward: true, findNext: true })}+ onClick={() => void runSearch(text, { forward: true, findNext: false })}className={cn('flex h-6 w-6 items-center justify-center rounded','hover:bg-titlebar-button-hover',diff --git a/apps/desktop/src/renderer/components/find-in-page/__tests__/FindInPageBar.test.tsx b/apps/desktop/src/renderer/components/find-in-page/__tests__/FindInPageBar.test.tsxnew file mode 100644index 0000000000..5eac837fdd--- /dev/null+++ b/apps/desktop/src/renderer/components/find-in-page/__tests__/FindInPageBar.test.tsx@@ -0,0 +1,118 @@+// @vitest-environment jsdom++import { act, cleanup, fireEvent, render, waitFor } from '@testing-library/react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++const shortcut = vi.hoisted(() => ({ handler: null as null | (() => boolean) }));++vi.mock('@/hooks/useAppShortcut', () => ({+ useAppShortcut: (_id: string, handler: () => boolean) => {+ shortcut.handler = handler;+ },+}));++vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));++import { FindInPageBar } from '../FindInPageBar';++describe('FindInPageBar', () => {+ const findInPage = vi.fn<(params: {+ text: string;+ forward?: boolean;+ findNext?: boolean;+ }) => Promise<number>>();+ const stopFindInPage = vi.fn();++ beforeEach(() => {+ findInPage.mockReset();+ findInPage.mockResolvedValue(1);+ stopFindInPage.mockReset();+ shortcut.handler = null;+ (window as unknown as { electronAPI: unknown }).electronAPI = {+ findInPage,+ stopFindInPage,+ onFindInPageResult: vi.fn(() => vi.fn()),+ };+ });++ afterEach(cleanup);++ async function openFindBar() {+ const view = render(<FindInPageBar />);+ expect(shortcut.handler).not.toBeNull();+ await act(async () => {+ shortcut.handler?.();+ });+ return { view, input: view.getByRole('textbox') as HTMLInputElement };+ }++ it('waits for IME composition to finish before searching the committed text', async () => {+ const { input } = await openFindBar();++ fireEvent.compositionStart(input);+ fireEvent.change(input, { target: { value: 'n' } });+ fireEvent.change(input, { target: { value: 'ni' } });+ expect(findInPage).not.toHaveBeenCalled();++ fireEvent.change(input, { target: { value: '你' } });+ fireEvent.compositionEnd(input);++ await waitFor(() => {+ expect(findInPage).toHaveBeenCalledTimes(1);+ expect(findInPage).toHaveBeenCalledWith({ text: '你', forward: true, findNext: true });+ });++ // Some Chromium versions emit one final change after compositionend.+ fireEvent.change(input, { target: { value: '你' } });+ expect(findInPage).toHaveBeenCalledTimes(1);+ });++ it('keeps IME composition intact after an earlier search', async () => {+ const { input } = await openFindBar();++ fireEvent.change(input, { target: { value: 'a' } });+ await waitFor(() => expect(findInPage).toHaveBeenCalledTimes(1));+ findInPage.mockClear();++ fireEvent.compositionStart(input);+ fireEvent.change(input, { target: { value: 'z' } });+ fireEvent.change(input, { target: { value: 'zhong' } });+ expect(findInPage).not.toHaveBeenCalled();+ fireEvent.change(input, { target: { value: '中' } });+ fireEvent.compositionEnd(input);++ await waitFor(() =>+ expect(findInPage).toHaveBeenCalledWith({ text: '中', forward: true, findNext: true }),+ );+ });++ it('continues the current Electron search session for Enter navigation', async () => {+ const { input } = await openFindBar();++ fireEvent.change(input, { target: { value: 'term' } });+ await waitFor(() => expect(findInPage).toHaveBeenCalledTimes(1));+ findInPage.mockClear();++ fireEvent.keyDown(input, { key: 'Enter' });+ expect(findInPage).toHaveBeenCalledWith({ text: 'term', forward: true, findNext: false });+ fireEvent.keyDown(input, { key: 'Enter', shiftKey: true });+ expect(findInPage).toHaveBeenLastCalledWith({+ text: 'term',+ forward: false,+ findNext: false,+ });+ });++ it('does not treat a Windows IME confirmation key as find-next', async () => {+ const { input } = await openFindBar();++ fireEvent.change(input, { target: { value: 'term' } });+ await waitFor(() => expect(findInPage).toHaveBeenCalledTimes(1));+ findInPage.mockClear();++ fireEvent.keyDown(input, { key: 'Enter', keyCode: 229 });+ expect(findInPage).not.toHaveBeenCalled();+ });+});