Skip to content

Commit

Permalink
EPUB: Page mapping and outline optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek committed Nov 26, 2024
1 parent 4adfaf3 commit aff6e1c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/dom/common/lib/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class PersistentRange {
this.endOffset = range.endOffset;
}

compareBoundaryPoints(how: number, other: Range | PersistentRange): number {
return this.toRange().compareBoundaryPoints(how, other instanceof PersistentRange ? other.toRange() : other);
}

toRange(): Range {
let range = new Range();
range.setStart(this.startContainer, this.startOffset);
Expand Down
13 changes: 10 additions & 3 deletions src/dom/epub/epub-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {

private readonly _rangeCache = new Map<string, PersistentRange>();

private readonly _hrefTargetCache = new Map<string, HTMLElement>();

private _pageMappingJSON!: string;

constructor(options: DOMViewOptions<EPUBViewState, EPUBViewData>) {
Expand Down Expand Up @@ -688,6 +690,10 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
}

protected _getHrefTarget(href: string): HTMLElement | null {
if (this._hrefTargetCache.has(href)) {
return this._hrefTargetCache.get(href)!;
}

let [pathname, hash] = this._splitHref(href);
let section = this.book.spine.get(pathname);
if (!section) {
Expand All @@ -708,6 +714,7 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
console.warn('Unable to resolve hash', hashTarget);
}
}
this._hrefTargetCache.set(href, target);
return target;
}

Expand Down Expand Up @@ -1290,7 +1297,7 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
return parseInt(elem.getAttribute('data-section-index')!);
}

private static _compareSectionIndices(a: Range | Node, b: Range | Node): number {
private static _compareSectionIndices(a: Range | PersistentRange | Node, b: Range | PersistentRange | Node): number {
let aSectionIndex = this.getContainingSectionIndex(a);
if (aSectionIndex === null) {
throw new Error('a is not inside a section');
Expand All @@ -1302,11 +1309,11 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
return aSectionIndex - bSectionIndex;
}

static compareBoundaryPoints(how: number, a: Range, b: Range): number {
static compareBoundaryPoints(how: number, a: Range | PersistentRange, b: Range | PersistentRange): number {
if (a.startContainer.getRootNode() !== b.startContainer.getRootNode()) {
return this._compareSectionIndices(a, b) || -1;
}
return a.compareBoundaryPoints(how, b);
return a.compareBoundaryPoints(how, b as Range);
}

static compareRangeToPoint(a: Range, b: Node, bOffset: number): number {
Expand Down
4 changes: 2 additions & 2 deletions src/dom/epub/lib/page-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class PageMapping {

readonly tree = new BTree<PersistentRange, string>(
undefined,
(a, b) => EPUBView.compareBoundaryPoints(Range.START_TO_START, a.toRange(), b.toRange())
|| EPUBView.compareBoundaryPoints(Range.END_TO_END, a.toRange(), b.toRange())
(a, b) => EPUBView.compareBoundaryPoints(Range.START_TO_START, a, b)
|| EPUBView.compareBoundaryPoints(Range.END_TO_END, a, b)
);

private _isPhysical = false;
Expand Down

0 comments on commit aff6e1c

Please sign in to comment.