mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
Allow explicit PDF.js resource paths for bundler compatibility
Bundlers like Vite cannot resolve directory paths (cmaps/, standard_fonts/) through the new URL(..., import.meta.url) pattern. Add optional config to makePDF, makeBook, and View.open so consumers can provide explicit URLs. When options are not provided, falls back to import.meta.url resolution (preserving existing behavior for direct module usage).
This commit is contained in:
@@ -129,7 +129,12 @@ const makeTOCItem = (item) => ({
|
|||||||
subitems: item.items.length ? item.items.map(makeTOCItem) : null,
|
subitems: item.items.length ? item.items.map(makeTOCItem) : null,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const makePDF = async (file) => {
|
export const makePDF = async (file, options = {}) => {
|
||||||
|
const cMapUrl = options.cMapUrl ?? pdfjsPath("cmaps/");
|
||||||
|
const standardFontDataUrl = options.standardFontDataUrl ?? pdfjsPath("standard_fonts/");
|
||||||
|
if (options.workerSrc) {
|
||||||
|
pdfjsLib.GlobalWorkerOptions.workerSrc = options.workerSrc;
|
||||||
|
}
|
||||||
const transport = new pdfjsLib.PDFDataRangeTransport(file.size, []);
|
const transport = new pdfjsLib.PDFDataRangeTransport(file.size, []);
|
||||||
transport.requestDataRange = (begin, end) => {
|
transport.requestDataRange = (begin, end) => {
|
||||||
file
|
file
|
||||||
@@ -141,8 +146,8 @@ export const makePDF = async (file) => {
|
|||||||
};
|
};
|
||||||
const pdf = await pdfjsLib.getDocument({
|
const pdf = await pdfjsLib.getDocument({
|
||||||
range: transport,
|
range: transport,
|
||||||
cMapUrl: pdfjsPath("cmaps/"),
|
cMapUrl,
|
||||||
standardFontDataUrl: pdfjsPath("standard_fonts/"),
|
standardFontDataUrl,
|
||||||
isEvalSupported: false,
|
isEvalSupported: false,
|
||||||
}).promise;
|
}).promise;
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ const fetchFile = async url => {
|
|||||||
return new File([await res.blob()], new URL(res.url).pathname)
|
return new File([await res.blob()], new URL(res.url).pathname)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const makeBook = async file => {
|
export const makeBook = async (file, options = {}) => {
|
||||||
if (typeof file === 'string') file = await fetchFile(file)
|
if (typeof file === 'string') file = await fetchFile(file)
|
||||||
let book
|
let book
|
||||||
if (file.isDirectory) {
|
if (file.isDirectory) {
|
||||||
@@ -105,7 +105,7 @@ export const makeBook = async file => {
|
|||||||
}
|
}
|
||||||
else if (await isPDF(file)) {
|
else if (await isPDF(file)) {
|
||||||
const { makePDF } = await import('./pdf.js')
|
const { makePDF } = await import('./pdf.js')
|
||||||
book = await makePDF(file)
|
book = await makePDF(file, options.pdf)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const { isMOBI, MOBI } = await import('./mobi.js')
|
const { isMOBI, MOBI } = await import('./mobi.js')
|
||||||
@@ -230,10 +230,10 @@ export class View extends HTMLElement {
|
|||||||
this.renderer.goTo(resolved)
|
this.renderer.goTo(resolved)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
async open(book) {
|
async open(book, options = {}) {
|
||||||
if (typeof book === 'string'
|
if (typeof book === 'string'
|
||||||
|| typeof book.arrayBuffer === 'function'
|
|| typeof book.arrayBuffer === 'function'
|
||||||
|| book.isDirectory) book = await makeBook(book)
|
|| book.isDirectory) book = await makeBook(book, options)
|
||||||
this.book = book
|
this.book = book
|
||||||
this.language = languageInfo(book.metadata?.language)
|
this.language = languageInfo(book.metadata?.language)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user