Skip to content

Commit

Permalink
EPUB: Reliably navigate on load
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek committed Aug 27, 2024
1 parent 931ad0d commit ef5e90f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/dom/epub/epub-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
return new EpubCFI(rangeOrNode, section.cfiBase);
}

getRange(cfi: EpubCFI | string, mount = false): Range | null {
getRange(cfi: EpubCFI | string, mount = false): PersistentRange | null {
if (!this._sectionViews.length) {
// The book isn't loaded yet -- don't spam the console
return null;
Expand All @@ -315,16 +315,17 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
view.mount();
}
if (this._rangeCache.has(cfiString)) {
return this._rangeCache.get(cfiString)!.toRange();
return this._rangeCache.get(cfiString)!;
}
try {
let range = cfi.toRange(view.container.ownerDocument, undefined, view.container);
if (!range) {
console.error('Unable to get range for CFI', cfiString);
return null;
}
this._rangeCache.set(cfiString, new PersistentRange(range));
return range;
let persistentRange = new PersistentRange(range);
this._rangeCache.set(cfiString, persistentRange);
return persistentRange;
}
catch (e) {
console.error('Unable to get range for CFI', cfiString, e);
Expand Down Expand Up @@ -362,7 +363,7 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
if (sectionIndex === null || !this._sectionViews[sectionIndex].mounted) {
return null;
}
return range;
return range.toRange();
}
default:
// No other selector types supported on EPUBs
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 @@ -121,11 +121,11 @@ class PageMapping {
return this.tree.minKey()?.toRange() ?? null;
}

getRange(pageLabel: string): Range | null {
getRange(pageLabel: string): PersistentRange | null {
// This is slow, but only needs to be called when manually navigating to a physical page number
for (let [key, value] of this.tree.entries()) {
if (value === pageLabel) {
return key.toRange();
return key;
}
}
return null;
Expand Down
11 changes: 8 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,23 @@ function generateReaderConfig(build) {
module: {
rules: [
{
test: /\.(js|jsx)$/,
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', { useBuiltIns: false }],
['@babel/preset-env', {
useBuiltIns: false,
targets: build === 'zotero' || build === 'dev'
? { firefox: 115, chrome: 128 }
: undefined
}],
],
},
},
},
{
build === 'dev' && {
test: /\.tsx?$/,
exclude: /node_modules/,
use: 'ts-loader',
Expand Down

0 comments on commit ef5e90f

Please sign in to comment.