Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): iOS 原生文本输入不在组字过程中被回写

liyize9b96618581fa
diff --git a/apps/mobile/src/__tests__/companionCreateNativeView.test.tsx b/apps/mobile/src/__tests__/companionCreateNativeView.test.tsx
new file mode 100644
index 0000000000..6bfbdabeb4
--- /dev/null
+++ b/apps/mobile/src/__tests__/companionCreateNativeView.test.tsx
@@ -0,0 +1,112 @@
+// @vitest-environment jsdom
+import React, { act } from 'react';
+import { createRoot } from 'react-dom/client';
+import { afterEach, expect, it, vi } from 'vitest';
+
+/**
+ * 创建伙伴的原生名称输入框必须由原生控件持有正在输入的文本。
+ * `onTextChange` 是异步事件:打字或输入法组字时原生控件已经比它新,若此时按 JS 值回写,
+ * SwiftUI 会重建字段、结束组字,未上屏的拼音字母被直接提交进输入框。
+ */
+const bridge = vi.hoisted(() => ({
+ field: null as any,
+ /** 每次 JS 写回原生控件的文本;输入过程中必须为空。 */
+ writes: [] as string[],
+}));
+
+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
+vi.mock('@/theme', () => ({ useTheme: () => ({ colors: { textSecondary: 'gray', inputCaret: 'blue' } }) }));
+vi.mock('@/session/ComposerSheet', () => ({
+ ComposerSheet: (p: any) => React.createElement('div', { 'data-testid': 'sheet' }, p.children),
+}));
+vi.mock('@/session/ComposerNativeSection', () => ({
+ ComposerNativeSection: (p: any) => React.createElement('section', null, p.children),
+}));
+vi.mock('@/session/CompanionPortraitPicker', () => ({ CompanionPortraitPicker: () => null }));
+vi.mock('@/session/CompanionNativeContent.ios', () => ({ CompanionNativeContent: (p: any) => p.children }));
+vi.mock('@expo/ui/swift-ui', async () => {
+ const React = await import('react');
+ const control = (p: any) => React.createElement('div', null, p.children);
+ return {
+ Button: (p: any) => React.createElement('div', { 'data-testid': p.testID }, p.children),
+ HStack: control,
+ Spacer: () => null,
+ ProgressView: () => null,
+ Text: (p: any) => React.createElement('span', null, p.children),
+ Section: control,
+ TextField: (p: any) => {
+ bridge.field = p;
+ return null;
+ },
+ // 原生持有文本:`get()` 读的是控件里的当前文本(打字、组字都会先更新它),
+ // `set()` 是 JS 侧唯一能改写控件的入口,这里逐次记录下来。
+ useNativeState: (initial: string) => {
+ const state = React.useRef({
+ value: initial,
+ get() { return this.value; },
+ set(value: string) { bridge.writes.push(value); this.value = value; },
+ });
+ return state.current;
+ },
+ };
+});
+vi.mock('@expo/ui/swift-ui/modifiers', () => Object.fromEntries(
+ ['accessibilityLabel', 'buttonStyle', 'contentShape', 'disabled', 'font', 'foregroundStyle', 'frame']
+ .map(key => [key, (value: any) => ({ [key]: value })])
+ .concat([['shapes', { rectangle: () => ({}) }] as any]),
+));
+
+Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
+
+import { CompanionCreateNativeView } from '@/session/CompanionCreateNativeView.ios';
+type Props = Parameters<typeof CompanionCreateNativeView>[0];
+
+const avatar = 'base64-portrait';
+let cleanup: Array<() => void> = [];
+afterEach(() => { cleanup.splice(0).forEach(fn => fn()); bridge.field = null; bridge.writes = []; });
+
+function mount(values: Props['values']) {
+ const root = createRoot(document.createElement('div'));
+ const onChange = vi.fn();
+ const panel = { id: 'create', text: '', action: { label: 'create' }, values: {} } as any;
+ const render = (next: Props['values']) => act(() => root.render(
+ <CompanionCreateNativeView panel={panel} values={next} onChange={onChange} visible online
+ deviceName="Mac" nameTaken={false} busy={false} loading={false} error={false} dirty={false}
+ onSubmit={() => {}} onRetry={() => {}} onClose={() => {}} />,
+ ));
+ render(values);
+ cleanup.push(() => act(() => root.unmount()));
+ return { render, onChange };
+}
+
+it('打字和组字期间不回写原生控件,未上屏的字母不会被提交', () => {
+ const { render } = mount({ name: '', avatarImageBase64: avatar });
+ // 拼音输入法:控件里已经是 "hai",而 JS 收到的是迟到一步的 "h"。
+ act(() => { bridge.field.text.value = 'hai'; bridge.field.onTextChange('h'); });
+ render({ name: 'h', avatarImageBase64: avatar });
+ expect(bridge.writes).toEqual([]);
+ // 组字继续:事件逐个追上原生,控件的文本一直由原生持有。
+ act(() => { bridge.field.text.value = 'hai'; bridge.field.onTextChange('hai'); });
+ render({ name: 'hai', avatarImageBase64: avatar });
+ // 选词上屏。
+ act(() => { bridge.field.text.value = '还'; bridge.field.onTextChange('还'); });
+ render({ name: '还', avatarImageBase64: avatar });
+ expect(bridge.writes).toEqual([]);
+});
+
+it('草稿被整体替换(重开表单)时才把新值同步回原生控件', () => {
+ const { render } = mount({ name: '', avatarImageBase64: avatar });
+ act(() => { bridge.field.text.value = '还'; bridge.field.onTextChange('还'); });
+ render({ name: '还', avatarImageBase64: avatar });
+ expect(bridge.writes).toEqual([]);
+ // 关闭后重开:宿主把草稿重置为空,控件里的旧名字必须跟着清掉。
+ render({ name: '', avatarImageBase64: avatar });
+ expect(bridge.writes).toEqual(['']);
+});
+
+it('上报的草稿始终是原生控件的当前文本', () => {
+ const { render, onChange } = mount({ name: '', avatarImageBase64: avatar });
+ act(() => { bridge.field.text.value = '还'; bridge.field.onTextChange('还'); });
+ render({ name: '还', avatarImageBase64: avatar });
+ expect(onChange).toHaveBeenLastCalledWith({ name: '还', avatarImageBase64: avatar });
+});
diff --git a/apps/mobile/src/session/CompanionCreateNativeView.ios.tsx b/apps/mobile/src/session/CompanionCreateNativeView.ios.tsx
index ba9804130b..d0a9f47575 100644
--- a/apps/mobile/src/session/CompanionCreateNativeView.ios.tsx
+++ b/apps/mobile/src/session/CompanionCreateNativeView.ios.tsx
@@ -1,6 +1,5 @@
-import { Button, HStack, Spacer, ProgressView, Text, TextField, useNativeState } from '@expo/ui/swift-ui';
+import { Button, HStack, Spacer, ProgressView, Text, TextField } from '@expo/ui/swift-ui';
import { accessibilityLabel, buttonStyle, contentShape, shapes, disabled, font, foregroundStyle, frame } from '@expo/ui/swift-ui/modifiers';
-import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useTheme } from '@/theme';
import { ComposerSheet } from './ComposerSheet';
@@ -8,18 +7,18 @@ import { ComposerNativeSection as Section } from './ComposerNativeSection';
import { CompanionPortraitPicker } from './CompanionPortraitPicker';
import { CompanionNativeContent } from './CompanionNativeContent.ios';
import type { CompanionCreateNativeViewProps } from './CompanionCreateNativeView';
+import { useNativeTextDraft } from './nativeTextDraft';
export function CompanionCreateNativeView(p: CompanionCreateNativeViewProps) {
const { t } = useTranslation(); const { colors } = useTheme();
const name = typeof p.values.name === 'string' ? p.values.name : '';
- const state = useNativeState(name);
- useEffect(() => { if (state.get() !== name) state.set(name); }, [name, state]);
+ const nameField = useNativeTextDraft(name, next => p.onChange({ ...p.values, name: next }));
const tr = (key: string) => t(`devices.companionProfile.${key}`, { deviceName: p.deviceName });
return <ComposerSheet visible={p.visible} onClose={p.onClose} onClosed={p.onClosed} title={tr('create')} nativeContent preventDismiss={p.dirty || p.busy}>
{!p.online || p.error || p.duplicate ? <Section><Text modifiers={[font({ textStyle: 'footnote' }), foregroundStyle(colors.textSecondary)]}>{tr(!p.online ? 'offline' : p.duplicate || p.nameTaken ? 'nameTaken' : 'createFailed')}</Text></Section> : null}
{p.loading ? <Section><ProgressView /></Section> : null}
{p.panel ? <>
- <Section title={tr('name')}><TextField text={state} onTextChange={name => p.onChange({ ...p.values, name })} maxLength={200} testID="companionCreate.name" modifiers={[accessibilityLabel(tr('name')), disabled(p.busy || !p.online || !!p.locked)]} /></Section>
+ <Section title={tr('name')}><TextField text={nameField.text} onTextChange={nameField.onTextChange} maxLength={200} testID="companionCreate.name" modifiers={[accessibilityLabel(tr('name')), disabled(p.busy || !p.online || !!p.locked)]} /></Section>
<Section title={tr('avatar')}><CompanionNativeContent><CompanionPortraitPicker value={String(p.values.avatarImageBase64 ?? '')} onChange={avatarImageBase64 => p.onChange({ ...p.values, avatarImageBase64 })} disabled={p.busy || !p.online || !!p.locked} /></CompanionNativeContent></Section>
{p.models ? <Section><CompanionNativeContent>{p.models}</CompanionNativeContent></Section> : null}
</> : null}
diff --git a/apps/mobile/src/session/CompanionProfileNativeView.ios.tsx b/apps/mobile/src/session/CompanionProfileNativeView.ios.tsx
index ba3ab79ebd..8ca5076f51 100644
--- a/apps/mobile/src/session/CompanionProfileNativeView.ios.tsx
+++ b/apps/mobile/src/session/CompanionProfileNativeView.ios.tsx
@@ -1,6 +1,6 @@
import { iconSize, spacing } from '@/theme';
-import { Fragment, useEffect } from 'react';
-import { Button, HStack, Image, Picker, ProgressView, RNHostView, Spacer, Text, TextField, Toggle, useNativeState } from '@expo/ui/swift-ui';
+import { Fragment } from 'react';
+import { Button, HStack, Image, Picker, ProgressView, RNHostView, Spacer, Text, TextField, Toggle } from '@expo/ui/swift-ui';
import { accessibilityLabel, alignmentGuide, buttonStyle, contentShape, shapes, disabled, font, foregroundStyle, frame, lineLimit, listRowInsets, pickerStyle, tag, tint } from '@expo/ui/swift-ui/modifiers';
import { useTranslation } from 'react-i18next';
import { resolveRemoteText, type RemoteActionField } from '@cindy/device-link';
@@ -11,6 +11,7 @@ import { ComposerSheet } from './ComposerSheet';
import { ComposerNativeSection as Section } from './ComposerNativeSection';
import type { CompanionProfileNativeViewProps } from './CompanionProfileNativeView';
import type { ProfilePanel, ProfileValues } from './companionProfileData';
+import { useNativeTextDraft } from './nativeTextDraft';
import { CompanionPortraitPicker } from './CompanionPortraitPicker';
import { CompanionNativeContent } from './CompanionNativeContent.ios';
import { saveCompanionReplyTargetPreference, useCompanionReplyTargetPreference } from './companionReplyTargetPreference';
@@ -20,15 +21,14 @@ export function CompanionNativeField({ field, values, onChange, busy }: { field:
const { colors } = useTheme();
const label = resolveRemoteText(field.label, i18n.language);
const value = typeof values[field.id] === 'string' ? values[field.id] as string : '';
- const text = useNativeState(value);
- useEffect(() => { if (text.get() !== value) text.set(value); }, [value, text]);
- const update = (value: string | boolean) => onChange({ ...values, [field.id]: value });
+ const update = (next: string | boolean) => onChange({ ...values, [field.id]: next });
+ const draft = useNativeTextDraft(value, next => update(next));
if (field.id === 'avatarImageBase64') return <Section title={label}><CompanionNativeContent><CompanionPortraitPicker value={value} onChange={update} disabled={busy} /></CompanionNativeContent></Section>;
if (field.kind === 'toggle') return <Toggle label={label} isOn={values[field.id] === true} onIsOnChange={update} modifiers={[disabled(busy), tint(colors.inputCaret)]} />;
if (field.kind === 'select') return <Picker label={label} selection={value} onSelectionChange={next => update(String(next))} modifiers={[pickerStyle('menu'), disabled(busy)]}>
{(field.options ?? []).map(option => <Text key={option.value} modifiers={[tag(option.value)]}>{resolveRemoteText(option.label, i18n.language)}</Text>)}
</Picker>;
- return <Section title={label}><TextField text={text} onTextChange={update} axis={field.kind === 'multiline' ? 'vertical' : 'horizontal'}
+ return <Section title={label}><TextField text={draft.text} onTextChange={draft.onTextChange} axis={field.kind === 'multiline' ? 'vertical' : 'horizontal'}
maxLength={field.id === 'name' || field.id === 'confirmName' ? 200 : field.id === 'body' ? 55000 : 12000} testID={`companionProfile.field.${field.id}`}
modifiers={[disabled(busy), accessibilityLabel(label), ...(field.kind === 'multiline' ? [lineLimit({ min: 3, max: 8 })] : [])]} /></Section>;
}
diff --git a/apps/mobile/src/session/ContextSheet.ios.tsx b/apps/mobile/src/session/ContextSheet.ios.tsx
index b7e0cef768..673b7706c7 100644
--- a/apps/mobile/src/session/ContextSheet.ios.tsx
+++ b/apps/mobile/src/session/ContextSheet.ios.tsx
@@ -2,13 +2,14 @@ import { useNativeGlassButtonStyle } from "@/platform/chrome/nativeGlassButtonSt
import { iconSize, useTheme } from '@/theme';
import { NativeErrorNotice } from '@/components/NativeErrorNotice.ios';
import { ComposerNativeSection as Section } from './ComposerNativeSection';
+import { useNativeTextDraft } from './nativeTextDraft';
import {
createContext,
useContext,
useEffect,
useRef,
} from "react";
-import { Button, HStack, Image, Picker, RNHostView, ProgressView, Spacer, Text, TextField, VStack, useNativeState } from '@expo/ui/swift-ui';
+import { Button, HStack, Image, Picker, RNHostView, ProgressView, Spacer, Text, TextField, VStack } from '@expo/ui/swift-ui';
import {
accessibilityAddTraits,
accessibilityHint,
@@ -261,12 +262,11 @@ export function ContextSheetSelectRow(props: ContextSheetSelectRowProps) {
}
export function ContextSheetTextField(props: ContextSheetTextFieldProps) {
- const text = useNativeState(props.value);
- useEffect(() => { if (text.get() !== props.value) text.set(props.value); }, [props.value, text]);
+ const draft = useNativeTextDraft(props.value, props.onChange);
return (
<TextField
- text={text}
- onTextChange={props.onChange}
+ text={draft.text}
+ onTextChange={draft.onTextChange}
axis={props.multiline ? 'vertical' : 'horizontal'}
placeholder={props.placeholder}
maxLength={props.maxLength}
diff --git a/apps/mobile/src/session/nativeTextDraft.ts b/apps/mobile/src/session/nativeTextDraft.ts
new file mode 100644
index 0000000000..0a24f0713e
--- /dev/null
+++ b/apps/mobile/src/session/nativeTextDraft.ts
@@ -0,0 +1,37 @@
+import { useEffect, useRef } from 'react';
+import { useNativeState } from '@expo/ui/swift-ui';
+
+/** 原生文本控件的可观察文本状态。`@expo/ui/swift-ui` 只导出 `useNativeState`,类型从它推导。 */
+export type NativeTextState = ReturnType<typeof useNativeState<string>>;
+
+/**
+ * 原生文本控件持有正在输入的文字,也包括输入法尚未上屏的组字(拼音、假名等)。
+ *
+ * `onTextChange` 是异步事件:打字或组字时原生控件已经比它新,按「JS 值不同就回写」同步
+ * 会把旧文本塞回控件。一次回写足以让 SwiftUI 重建字段、结束组字,未上屏的字母被直接
+ * 提交进输入框。因此输入过程中一律只由原生控件决定文本,只有草稿被整体替换(重开表单、
+ * 宿主重置、切换字段)时才把新值推给控件一次。
+ *
+ * 返回的 `onTextChange` 会先记下这次原生给出的文本,调用方再照常上报给 JS 草稿;随后的
+ * 重渲染会认出这个值来自输入本身而不是替换,从而跳过回写。
+ */
+export function useNativeTextDraft(value: string, emit: (text: string) => void): {
+ text: NativeTextState;
+ onTextChange: (text: string) => void;
+} {
+ const text = useNativeState(value);
+ // 最近一次由输入产生、已经存在于原生控件里的文本;与 `value` 相等即表示没有替换发生。
+ const typed = useRef(value);
+ useEffect(() => {
+ if (value === typed.current) return;
+ typed.current = value;
+ text.set(value);
+ }, [value, text]);
+ return {
+ text,
+ onTextChange: (next: string) => {
+ typed.current = next;
+ emit(next);
+ },
+ };
+}