File tree Expand file tree Collapse file tree 2 files changed +27
-7
lines changed Expand file tree Collapse file tree 2 files changed +27
-7
lines changed Original file line number Diff line number Diff line change @@ -291,6 +291,11 @@ type LocationKey = {
291
291
* (ie. chapter).
292
292
*/
293
293
position ?: number ;
294
+
295
+ /**
296
+ * Page index within the document.
297
+ */
298
+ pageIndex ?: number ;
294
299
} ;
295
300
296
301
/**
@@ -305,6 +310,7 @@ export function location(annotation: Annotation): LocationKey {
305
310
306
311
let cfi ;
307
312
let position ;
313
+ let pageIndex ;
308
314
309
315
// nb. We ignore the possibility of an annotation having multiple targets here.
310
316
// h and the client only support one.
@@ -313,10 +319,12 @@ export function location(annotation: Annotation): LocationKey {
313
319
position = selector . start ;
314
320
} else if ( selector . type === 'EPUBContentSelector' && selector . cfi ) {
315
321
cfi = selector . cfi ;
322
+ } else if ( selector . type === 'PageSelector' ) {
323
+ pageIndex = selector . index ;
316
324
}
317
325
}
318
326
319
- return { cfi, position } ;
327
+ return { cfi, position, pageIndex } ;
320
328
}
321
329
322
330
/**
Original file line number Diff line number Diff line change @@ -91,11 +91,23 @@ export const sorters = {
91
91
return 1 ;
92
92
}
93
93
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 ;
100
112
} ,
101
113
} satisfies Record < string , SortFunction > ;
You can’t perform that action at this time.
0 commit comments