Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(docs): keep a space typed at the end of the title across autosave (#10935)
pseudoyuaa15cd6bada4
diff --git a/packages/views/docs/components/use-doc-draft.test.tsx b/packages/views/docs/components/use-doc-draft.test.tsxindex 36c7917df8..a92daf24ca 100644--- a/packages/views/docs/components/use-doc-draft.test.tsx+++ b/packages/views/docs/components/use-doc-draft.test.tsx@@ -360,3 +360,53 @@ it("keeps showing the saved content while the doc prop catches up", async () =>baseRevision: 2,});});++it("keeps a space typed at the end of the title after the server saves it trimmed", async () => {+ // The API trims titles. Pausing after a space must not drop it from the+ // field, or the next word typed joins the previous one.+ vi.mocked(api.updateDoc).mockImplementation(async (_id, data) => ({+ ...doc,+ ...data,+ title: (data.title ?? doc.title).trim(),+ revision: data.baseRevision + 1,+ versionCount: data.baseRevision + 1,+ }));+ const view = mount();+ act(() => view.result.current.change({ title: "Release " }));+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1600);+ });+ expect(api.updateDoc).toHaveBeenCalledTimes(1);+ expect(view.result.current.draft.title).toBe("Release ");+ expect(view.result.current.dirty).toBe(false);++ // The trimmed title is saved: nothing is sent again while the field rests.+ await act(async () => {+ await vi.advanceTimersByTimeAsync(5000);+ });+ expect(api.updateDoc).toHaveBeenCalledTimes(1);++ // A body edit keeps the space too.+ act(() => view.result.current.change({ content: "Body" }));+ expect(view.result.current.draft.title).toBe("Release ");+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1600);+ });+ expect(api.updateDoc).toHaveBeenLastCalledWith(doc.id, {+ title: "Release",+ content: "Body",+ baseRevision: 2,+ });+ expect(view.result.current.draft.title).toBe("Release ");++ act(() => view.result.current.change({ title: "Release notes" }));+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1600);+ });+ expect(api.updateDoc).toHaveBeenLastCalledWith(doc.id, {+ title: "Release notes",+ content: "Body",+ baseRevision: 3,+ });+ expect(view.result.current.draft.title).toBe("Release notes");+});diff --git a/packages/views/docs/components/use-doc-draft.ts b/packages/views/docs/components/use-doc-draft.tsindex ef75dd0bc4..0e3f3f6f90 100644--- a/packages/views/docs/components/use-doc-draft.ts+++ b/packages/views/docs/components/use-doc-draft.ts@@ -38,6 +38,9 @@ export function useDocDraft(doc: Doc) {// before the parent passes the refreshed `doc`, so falling back to the prop// would briefly hand the editor the pre-save content and reset its caret.const savedDoc = useRef<Doc | null>(null);+ // The server trims titles. Keep what was typed in the field, so a pause+ // after a space does not join the next word to the previous one.+ const typedTitle = useRef<{ typed: string; saved: string } | null>(null);const latestDoc =savedDoc.current && savedDoc.current.revision > doc.revision? savedDoc.current@@ -73,9 +76,13 @@ export function useDocDraft(doc: Doc) {setDocDraft(key, { ...fromDoc(doc), backup: current.backup });}}, [doc, key]);- const value = draft ?? fromDoc(latestDoc);+ const stored = draft ?? fromDoc(latestDoc);const dirty =- value.title !== value.baseTitle || value.content !== value.baseContent;+ stored.title !== stored.baseTitle || stored.content !== stored.baseContent;+ const value =+ typedTitle.current?.saved === stored.title+ ? { ...stored, title: typedTitle.current.typed }+ : stored;const save = async () => {const sent = useDocDraftStore.getState().draft.entries[key];if (@@ -102,6 +109,8 @@ export function useDocDraft(doc: Doc) {}qc.setQueryData(docKeys.detail(wsId, doc.id), updated);savedDoc.current = updated;+ if (current.title === sent.title && updated.title !== sent.title)+ typedTitle.current = { typed: sent.title, saved: updated.title };const next = {...current,title: current.title === sent.title ? updated.title : current.title,@@ -145,6 +154,7 @@ export function useDocDraft(doc: Doc) {if (!inScope()) return;const current =useDocDraftStore.getState().draft.entries[key] ?? fromDoc(latestDoc);+ if (patch.title !== undefined) typedTitle.current = null;setDocDraft(key, { ...current, ...patch });};const reload = async () => {