Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(mobile): focus note body on create

tianma-ifd4835b0ee692
diff --git a/apps/mobile/src/components/LocalTiptapEditor.tsx b/apps/mobile/src/components/LocalTiptapEditor.tsx
index b8d1fc71..ee10ed7d 100644
--- a/apps/mobile/src/components/LocalTiptapEditor.tsx
+++ b/apps/mobile/src/components/LocalTiptapEditor.tsx
@@ -466,8 +466,8 @@ function LocalTiptapEditorImpl(props: LocalTiptapEditorProps) {
try {
const parsed = JSON.parse(contentJsonSerialized) as EditorDoc;
const next = resolveImageSources(parsed, props.baseUrl);
- // Do not focus here: draft restore / template inject must not steal the native
- // title TextInput IME. Callers that need caret at end use focusEnd().
+ // Do not focus while replacing content. Callers decide when the editor should
+ // take focus and place the caret via focusEnd().
editor.commands.setContent(next, { emitUpdate: !isViewer });
} catch {
// Ignore malformed payloads from the native bridge.
@@ -1950,4 +1950,3 @@ const getEditorStyles = (theme: "light" | "dark", options?: { viewer?: boolean }
.edgeever-editor-content hr { margin: 24px 0; border: 0; border-top: 1px solid #cbd5e1; }
`;
};
-
diff --git a/apps/mobile/src/screens/WorkspaceScreen.tsx b/apps/mobile/src/screens/WorkspaceScreen.tsx
index 3c8f157a..d2e3a18d 100644
--- a/apps/mobile/src/screens/WorkspaceScreen.tsx
+++ b/apps/mobile/src/screens/WorkspaceScreen.tsx
@@ -1,4 +1,4 @@
-import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type ComponentRef, type ReactNode } from "react";
+import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient, type UseMutationResult } from "@tanstack/react-query";
import * as Clipboard from "expo-clipboard";
import Constants from "expo-constants";
@@ -1733,20 +1733,6 @@ const CreateMemoModal = ({
const focusTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const keyboardTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
- const titleFocusTimersRef = useRef<Array<ReturnType<typeof setTimeout>>>([]);
- const titleInputRef = useRef<ComponentRef<typeof TextInput>>(null);
- // Blank create: focus native title so the soft keyboard shows immediately while DomWebView
- // cold-starts. Template / share drafts already have structure — focus the body instead.
- const preferTitleFocus = useMemo(() => {
- if (!initialDraft) {
- return true;
- }
- const seedTitle = initialDraft.title?.trim() ?? "";
- const seedBody = initialDraft.contentMarkdown?.trim() ?? "";
- return !seedTitle && !seedBody;
- }, [initialDraft]);
- const preferTitleFocusRef = useRef(preferTitleFocus);
- preferTitleFocusRef.current = preferTitleFocus;
const pushBodyToEditor = useCallback((doc: TiptapDoc) => {
safeDomCall(() => editorRef.current?.setContent(JSON.stringify(doc)));
@@ -1761,22 +1747,8 @@ const CreateMemoModal = ({
clearTimeout(keyboardTimerRef.current);
keyboardTimerRef.current = null;
}
- for (const timer of titleFocusTimersRef.current) {
- clearTimeout(timer);
- }
- titleFocusTimersRef.current = [];
}, []);
- const scheduleTitleFocus = useCallback((delaysMs: number[] = [0, 80, 220, 480]) => {
- clearFocusTimers();
- // DomWebView often steals focus when it attaches; reclaim the native title a few times.
- titleFocusTimersRef.current = delaysMs.map((delayMs) =>
- setTimeout(() => {
- titleInputRef.current?.focus();
- }, delayMs)
- );
- }, [clearFocusTimers]);
-
const scheduleBodyKeyboard = useCallback((delayMs = 160) => {
clearFocusTimers();
// Full-tree create only mounts the editor DomWebView, so native IME show is safe again.
@@ -2125,8 +2097,7 @@ const CreateMemoModal = ({
const editorElement = useMemo(() => draftLoaded && baseUrl ? (
<LocalTiptapEditor
- // Blank create keeps focus on the native title field; template/share focus the body.
- autoFocus={!preferTitleFocus}
+ autoFocus
baseUrl={baseUrl}
content={contentJsonRef.current}
dom={{
@@ -2152,18 +2123,13 @@ const CreateMemoModal = ({
onReady={async (elapsedMs) => {
setEditorReady(true);
recordEditorStartup(elapsedMs);
- if (preferTitleFocusRef.current) {
- // DomWebView may steal focus on attach — reclaim the title (and soft keyboard).
- scheduleTitleFocus();
- return;
- }
scheduleBodyKeyboard(60);
}}
ref={editorRef}
locale={resolvedLocale}
theme={resolvedTheme}
/>
- ) : null, [baseUrl, draftLoaded, loadEditorResource, preferTitleFocus, resolvedLocale, resolvedTheme, scheduleBodyKeyboard, scheduleTitleFocus, selectResource]);
+ ) : null, [baseUrl, draftLoaded, loadEditorResource, resolvedLocale, resolvedTheme, scheduleBodyKeyboard, selectResource]);
return (
<SafeAreaView edges={["top", "left", "right", "bottom"]} style={styles.createMemoSafeArea}>
@@ -2198,7 +2164,6 @@ const CreateMemoModal = ({
<View style={styles.createMemoMain}>
<TextInput
autoCorrect
- autoFocus={preferTitleFocus}
accessibilityLabel="笔记标题"
onChangeText={(value) => {
setTitle(value);
@@ -2206,7 +2171,6 @@ const CreateMemoModal = ({
}}
placeholder={DEFAULT_MEMO_TITLE}
placeholderTextColor="#94a3b8"
- ref={titleInputRef}
style={styles.createMemoTitleInput}
value={title}
/>
@@ -2262,11 +2226,7 @@ const CreateMemoModal = ({
client={client}
onClose={() => {
setTemplatePickerOpen(false);
- if (preferTitleFocusRef.current) {
- scheduleTitleFocus([80, 200]);
- } else {
- scheduleBodyKeyboard(80);
- }
+ scheduleBodyKeyboard(80);
}}
onSelect={requestApplyTemplateSeed}
presentation="overlay"
diff --git a/tests/mobile-app-scope.test.ts b/tests/mobile-app-scope.test.ts
index 61cf9f64..a635c4b8 100644
--- a/tests/mobile-app-scope.test.ts
+++ b/tests/mobile-app-scope.test.ts
@@ -90,6 +90,22 @@ describe("mobile app scope", () => {
expect(workspaceSource).toContain("createPendingRef.current || imageOperationRef.current");
});
+ test("focuses the note body instead of the title when creating a note", () => {
+ const createMemoSource = workspaceSource.slice(
+ workspaceSource.indexOf("const CreateMemoModal ="),
+ workspaceSource.indexOf("const RichEditorModal =")
+ );
+ const titleInput = createMemoSource.match(
+ /<TextInput\s+autoCorrect\s+accessibilityLabel="笔记标题"[\s\S]*?\/>/
+ )?.[0];
+
+ expect(createMemoSource).toMatch(/<LocalTiptapEditor\s+autoFocus\s/);
+ expect(createMemoSource).toContain("scheduleBodyKeyboard(60)");
+ expect(titleInput).toBeDefined();
+ expect(titleInput).not.toContain("autoFocus");
+ expect(createMemoSource).not.toContain("scheduleTitleFocus");
+ });
+
test("declares iOS privacy strings and full-screen phone-on-iPad presentation", () => {
const infoPlist = appJson.expo.ios?.infoPlist ?? {};
expect(appJson.expo.ios?.supportsTablet).toBe(false);