Skip to content

Commit 5e8b766

Browse files
committed
Extend Location sorting to support page index
1 parent df9d852 commit 5e8b766

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/sidebar/helpers/annotation-metadata.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ type LocationKey = {
291291
* (ie. chapter).
292292
*/
293293
position?: number;
294+
295+
/**
296+
* Page index within the document.
297+
*/
298+
pageIndex?: number;
294299
};
295300

296301
/**
@@ -305,6 +310,7 @@ export function location(annotation: Annotation): LocationKey {
305310

306311
let cfi;
307312
let position;
313+
let pageIndex;
308314

309315
// nb. We ignore the possibility of an annotation having multiple targets here.
310316
// h and the client only support one.
@@ -313,10 +319,12 @@ export function location(annotation: Annotation): LocationKey {
313319
position = selector.start;
314320
} else if (selector.type === 'EPUBContentSelector' && selector.cfi) {
315321
cfi = selector.cfi;
322+
} else if (selector.type === 'PageSelector') {
323+
pageIndex = selector.index;
316324
}
317325
}
318326

319-
return { cfi, position };
327+
return { cfi, position, pageIndex };
320328
}
321329

322330
/**

src/sidebar/helpers/thread-sorters.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,23 @@ export const sorters = {
9191
return 1;
9292
}
9393

94-
// If the chapter number is the same or for other document types, compare
95-
// the text position instead. Missing positions sort after any present
96-
// positions.
97-
const aPos = aLocation.position ?? Number.MAX_SAFE_INTEGER;
98-
const bPos = bLocation.position ?? Number.MAX_SAFE_INTEGER;
99-
return Math.sign(aPos - bPos);
94+
// If the chapter number is the same or for other document types, and
95+
// at least one of the annotations has text position info, compare
96+
// that instead. Missing positions sort after any present positions.
97+
if (typeof aLocation.position === 'number' || typeof bLocation.position === 'number') {
98+
const aPos = aLocation.position ?? Number.MAX_SAFE_INTEGER;
99+
const bPos = bLocation.position ?? Number.MAX_SAFE_INTEGER;
100+
return Math.sign(aPos - bPos);
101+
}
102+
103+
// If text positions aren't specified, check for page numbers.
104+
if (typeof aLocation.pageIndex === 'number' || typeof bLocation.pageIndex === 'number') {
105+
const aPage = aLocation.pageIndex ?? Number.MAX_SAFE_INTEGER;
106+
const bPage = bLocation.pageIndex ?? Number.MAX_SAFE_INTEGER;
107+
return Math.sign(aPage - bPage);
108+
}
109+
110+
// If nothing matches
111+
return 0;
100112
},
101113
} satisfies Record<string, SortFunction>;

0 commit comments

Comments
 (0)