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): 修复听写停止后的输入光标位置

DavidShenfadf2a2c5b07
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index 8419a5cba6..d7effce003 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -698,6 +698,7 @@ export default function NewRemoteSessionScreen() {
const userTouchedWorkspaceRef = useRef(false);
const firstMessageRef = useRef(draft.firstMessage);
const firstMessageSelectionRef = useRef({ start: draft.firstMessage.length, end: draft.firstMessage.length });
+ const [firstMessageSelection, setFirstMessageSelection] = useState(firstMessageSelectionRef.current);
const firstMessageInputRef = useRef<NativeTextInput>(null);
const voiceDraftScrollRef = useRef<ScrollView>(null);
const voiceRecordingActiveRef = useRef(false);
@@ -3219,7 +3220,10 @@ export default function NewRemoteSessionScreen() {
localVoiceInputHistory,
readCurrentDraft: () => firstMessageRef.current,
onDraftChanged: (text, selection) => {
- if (selection) firstMessageSelectionRef.current = selection;
+ if (selection) {
+ firstMessageSelectionRef.current = selection;
+ setFirstMessageSelection(selection);
+ }
setFirstMessageDraft(text);
},
onStateChanged: setVoiceState,
@@ -5866,7 +5870,9 @@ export default function NewRemoteSessionScreen() {
onChangeText={setFirstMessageDraft}
onSelectionChange={(event) => {
if (!voiceRecordingActiveRef.current && !voiceStopInFlightRef.current) {
- firstMessageSelectionRef.current = event.nativeEvent.selection;
+ const selection = event.nativeEvent.selection;
+ firstMessageSelectionRef.current = selection;
+ setFirstMessageSelection(selection);
}
}}
onContentSizeChange={handleFirstMessageInputContentSizeChange}
@@ -5881,6 +5887,7 @@ export default function NewRemoteSessionScreen() {
placeholderTextColor={colors.textTertiary}
resizeHandle={composerCardActive ? renderComposerResizeHandle() : null}
scrollEnabled={composerInputScrollEnabled}
+ selection={firstMessageSelection}
selectionColor={colors.inputCaret}
testID="newSession.actions"
toolbar={renderComposerToolbar()}
diff --git a/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx b/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx
index 63d7910582..d00d220533 100644
--- a/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx
+++ b/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx
@@ -64,7 +64,7 @@ const hiddenStyle = findOne((node): node is ts.PropertyAssignment =>
ts.isPropertyAssignment(node) && node.name.getText(source) === 'inputVoiceHidden');
const composer = findOne((node): node is ts.JsxSelfClosingElement =>
ts.isJsxSelfClosingElement(node) && node.tagName.getText(source) === 'MobileComposerInputRow');
-const propNames = ['inputStyle', 'caretHidden', 'value', 'placeholder', 'onPressIn'];
+const propNames = ['inputStyle', 'caretHidden', 'value', 'placeholder', 'selection', 'onPressIn'];
const expressions = propNames.map((name) => {
const prop = composer.attributes.properties.find((node) =>
ts.isJsxAttribute(node) && node.name.getText(source) === name);
@@ -75,7 +75,7 @@ const expressions = propNames.map((name) => {
return `${name}: ${prop.initializer.expression.getText(source)}`;
});
const compiled = ts.transpileModule(`function pageProps(bindings) {
- const { Platform, voiceIsListening, draft, finishVoiceRecording, composerPlaceholder } = bindings;
+ const { Platform, voiceIsListening, draft, finishVoiceRecording, composerPlaceholder, firstMessageSelection } = bindings;
const styles = { inputVoiceHidden: ${hiddenStyle.initializer.getText(source)} };
return { ${expressions.join(',\n')} };
}`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
@@ -91,14 +91,15 @@ function mountInput() {
const ref = createRef<HTMLTextAreaElement>();
const finishVoiceRecording = vi.fn();
root = createRoot(container);
- function render(listening: boolean, draft: string) {
+ function render(listening: boolean, draft: string, selection = { start: draft.length, end: draft.length }) {
const colors = palettes[native.mode];
act(() => root!.render(createElement(MobileComposerInputRow, {
accessibilityLabel: 'Draft', inputTestID: 'draft', inputRef: ref,
placeholder: 'Draft', placeholderTextColor: colors.textTertiary,
value: draft, onChangeText: vi.fn(), onPasteImages: vi.fn(),
...pageProps({ Platform: { OS: native.platform }, voiceIsListening: listening,
- draft: { firstMessage: draft }, finishVoiceRecording, composerPlaceholder: 'Draft' }),
+ draft: { firstMessage: draft }, firstMessageSelection: selection,
+ finishVoiceRecording, composerPlaceholder: 'Draft' }),
inputOverlay: listening ? createElement('span', { 'data-testid': 'preview' }, draft) : null,
})));
}
@@ -128,6 +129,7 @@ describe.each(['light', 'dark'] as const)('new-session dictation input (%s)', (m
expect(input.inputStyle().display).not.toBe('none');
expect(native.props.caretHidden).toBe(true);
expect(native.props.placeholder).toBe('');
+ expect(native.props.selection).toEqual({ start: draft.length, end: draft.length });
expect(input.container.querySelector('[data-testid="preview"]')?.textContent).toBe(draft);
}
act(() => original!.dispatchEvent(new MouseEvent('mousedown', { bubbles: true })));
@@ -139,6 +141,7 @@ describe.each(['light', 'dark'] as const)('new-session dictation input (%s)', (m
expect(input.inputStyle().opacity ?? 1).toBe(1);
expect(input.inputStyle().color).toBe(palettes[mode].textPrimary);
expect(native.props.caretHidden).toBe(false);
+ expect(native.props.selection).toEqual({ start: 'before hello\nworld suffix'.length, end: 'before hello\nworld suffix'.length });
expect(input.container.querySelector('[data-testid="preview"]')).toBeNull();
act(() => original!.dispatchEvent(new MouseEvent('mousedown', { bubbles: true })));
expect(input.finishVoiceRecording).toHaveBeenCalledOnce();
@@ -154,4 +157,14 @@ describe.each(['light', 'dark'] as const)('new-session dictation input (%s)', (m
input.render(false, 'dictation');
expect(input.inputStyle().color).toBe(palettes[mode].textPrimary);
});
+
+ it('keeps the post-dictation insertion point when Android returns to typing', () => {
+ native.platform = 'android';
+ native.mode = mode;
+ const input = mountInput();
+ input.render(true, '甲新词乙', { start: 4, end: 4 });
+ expect(native.props.selection).toEqual({ start: 4, end: 4 });
+ input.render(false, '甲新词乙', { start: 4, end: 4 });
+ expect(native.props.selection).toEqual({ start: 4, end: 4 });
+ });
});
diff --git a/apps/mobile/src/session/MobileComposerInputRow.tsx b/apps/mobile/src/session/MobileComposerInputRow.tsx
index c5d70f1b33..dcef7feec4 100644
--- a/apps/mobile/src/session/MobileComposerInputRow.tsx
+++ b/apps/mobile/src/session/MobileComposerInputRow.tsx
@@ -154,6 +154,7 @@ export interface MobileComposerInputRowProps {
onPressIn?: TextInputProps['onPressIn'];
placeholder: string;
placeholderTextColor: string;
+ selection?: TextInputProps['selection'];
/** 顶部居中的拖拽调高 grabber(ComposerResizeGrabber),absolute 定位不占布局空间。 */
resizeHandle?: ReactNode;
rowStyle?: StyleProp<ViewStyle>;
@@ -215,6 +216,7 @@ export function MobileComposerInputRow({
onPressIn,
placeholder,
placeholderTextColor,
+ selection,
resizeHandle,
rowStyle,
scrollEnabled,
@@ -271,6 +273,7 @@ export function MobileComposerInputRow({
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
scrollEnabled={scrollEnabled}
+ selection={selection}
selectionColor={selectionColor}
style={[
styles.input,