diff --git a/apps/readest-app/src/__tests__/app/reader/hooks/useAnnotationEditor.test.ts b/apps/readest-app/src/__tests__/app/reader/hooks/useAnnotationEditor.test.ts index 28e915f89..3888a1b3f 100644 --- a/apps/readest-app/src/__tests__/app/reader/hooks/useAnnotationEditor.test.ts +++ b/apps/readest-app/src/__tests__/app/reader/hooks/useAnnotationEditor.test.ts @@ -109,12 +109,14 @@ describe('useAnnotationEditor applyAnnotationRange', () => { await result.current.applyAnnotationRange(range, 2, false, false); - // The next push anchors the new range for both readers again. + // The next push anchors the new range for both readers again. Cleared as + // null, not dropped, so the server forgets the old range even when the + // new one can't be anchored. const [, [saved]] = h.updateBooknotes.mock.calls.at(-1) as unknown as [string, BookNote[]]; expect(saved).toMatchObject({ cfi: 'new-cfi', text: 'edited text' }); - expect(saved!.xpointer0).toBeUndefined(); - expect(saved!.xpointer1).toBeUndefined(); - expect(saved!.crosspoint).toBeUndefined(); + expect(saved!.xpointer0).toBeNull(); + expect(saved!.xpointer1).toBeNull(); + expect(saved!.crosspoint).toBeNull(); }); // #6390: a range in the footnote popup's document means nothing to the main diff --git a/apps/readest-app/src/__tests__/utils/transform.test.ts b/apps/readest-app/src/__tests__/utils/transform.test.ts index 565d8f33b..b5eb1a9ca 100644 --- a/apps/readest-app/src/__tests__/utils/transform.test.ts +++ b/apps/readest-app/src/__tests__/utils/transform.test.ts @@ -85,6 +85,12 @@ describe('transformBookNoteToDB with a CrossPoint anchor', () => { expect('crosspoint' in pulled).toBe(false); expect({ ...highlight, ...pulled }.crosspoint).toEqual(highlight.crosspoint); }); + + it('sends anchors cleared by a moved range as null, so the server forgets them', () => { + const moved = { ...highlight, xpointer0: null, xpointer1: null, crosspoint: null }; + const db = JSON.parse(JSON.stringify(transformBookNoteToDB(moved, 'user1'))); + expect(db).toMatchObject({ xpointer0: null, xpointer1: null, crosspoint: null }); + }); }); describe('transformBookNoteToDB with xpointer fields', () => { diff --git a/apps/readest-app/src/app/reader/hooks/useAnnotationEditor.ts b/apps/readest-app/src/app/reader/hooks/useAnnotationEditor.ts index 819fcd17c..410a271f7 100644 --- a/apps/readest-app/src/app/reader/hooks/useAnnotationEditor.ts +++ b/apps/readest-app/src/app/reader/hooks/useAnnotationEditor.ts @@ -83,9 +83,11 @@ export const useAnnotationEditor = ({ cfi: newCfi, text: newText, // Anchors of the old range: the next push sets them for this one. - xpointer0: undefined, - xpointer1: undefined, - crosspoint: undefined, + // Null, not undefined, which the push would drop: the server must + // forget the old range even if the new one can't be anchored. + xpointer0: null, + xpointer1: null, + crosspoint: null, updatedAt: nextBooknoteStamp(existingAnnotation), }; diff --git a/apps/readest-app/src/types/book.ts b/apps/readest-app/src/types/book.ts index 887a19cf6..033302ced 100644 --- a/apps/readest-app/src/types/book.ts +++ b/apps/readest-app/src/types/book.ts @@ -211,8 +211,8 @@ export interface BookNote { id: string; type: BookNoteType; cfi: string; // Canonicalized CFI for the note location - xpointer0?: string; // Start XPointer for the note location - xpointer1?: string; // End XPointer for the note location + xpointer0?: string | null; // Start XPointer for the note location + xpointer1?: string | null; // End XPointer for the note location page?: number; text?: string; style?: HighlightStyle; @@ -237,7 +237,7 @@ export interface BookNote { * KOReader's. Set on a clipping a reader synced, or by a Readest app for a * highlight in an EPUB. See utils/crosspointClipping. */ - crosspoint?: CrossPointAnchor; + crosspoint?: CrossPointAnchor | null; createdAt: number; updatedAt: number; diff --git a/apps/readest-app/src/types/records.ts b/apps/readest-app/src/types/records.ts index 4e34d9b6e..ad889430a 100644 --- a/apps/readest-app/src/types/records.ts +++ b/apps/readest-app/src/types/records.ts @@ -53,8 +53,8 @@ export interface DBBookNote { id: string; type: string; cfi?: string; - xpointer0?: string; - xpointer1?: string; + xpointer0?: string | null; + xpointer1?: string | null; page?: number; text?: string; style?: string;