File size: 16,694 Bytes
5a321ab dac2ca2 49a03ba dac2ca2 49a03ba 5f30692 49a03ba dac2ca2 49a03ba dac2ca2 bef5045 49a03ba dac2ca2 e6e90f2 dac2ca2 76d6c6d dac2ca2 bef5045 1dc63a4 49a03ba dac2ca2 49a03ba dac2ca2 76d6c6d dac2ca2 76d6c6d dac2ca2 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba 6c8fd46 49a03ba dac2ca2 49a03ba dac2ca2 49a03ba 5a321ab 6d6c14d 5a321ab 49a03ba 6c8fd46 49a03ba 01ab508 bec35e9 01ab508 49a03ba 01ab508 bec35e9 01ab508 49a03ba dac2ca2 49a03ba dac2ca2 1dc63a4 dac2ca2 49a03ba dac2ca2 08bd7b1 5f30692 86eb73d dac2ca2 5f30692 49a03ba dac2ca2 49a03ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | /** @param {unknown} original */
function originalImageFallbackExtension(original) {
const extension = typeof original === "string" ? original.toLowerCase() : "";
return ["jpg", "jpeg", "png", "gif", "bmp", "webp"].includes(extension) ? extension : "";
}
/** @param {import('../../../typings/reader-controller-strict').ReaderFormatControllerContext} readerContext */
export function createReaderFormatController(readerContext) {
/** @param {string} mode */
function disposeFormatResources(mode) {
if (mode === "epub-chapters") {
readerContext.chapterManifestCleanup?.();
readerContext.chapterManifestCleanup = null;
}
if (mode === "swf") {
readerContext.swfPlayer?.remove();
readerContext.swfPlayer = null;
}
if (["pdf", "pdf-pages", "image-pages"].includes(mode)) {
readerContext.fullSearchController.resetAvailability();
readerContext.pdfDocument = null;
readerContext.pdfPageManifest = null;
readerContext.pdfOcrManifest = null;
readerContext.pdfOcrManifestPromise = null;
readerContext.pdfV3Repository?.dispose(); readerContext.pdfTextLayerRepository?.dispose();
readerContext.pdfV3Repository = readerContext.pdfTextLayerRepository = null;
readerContext.pdfV3Manifest = readerContext.pdfV3Map = null;
readerContext.pdfTextContentCache.clear();
readerContext.pdfTextContentPromises.clear();
readerContext.pdfTextContentCacheBytes = 0;
readerContext.pdfOcrPageCache.clear();
readerContext.pdfOcrPageCacheBytes = 0;
readerContext.clearPdfManifestPrefetches();
readerContext.pdfBookSearchClient?.dispose();
readerContext.pdfBookSearchClient = null;
readerContext.pdfShellWindow?.dispose();
readerContext.pdfFirstPagePreload?.removeAttribute("src");
readerContext.pdfFirstPagePreload = null;
for (const canvas of /** @type {NodeListOf<HTMLCanvasElement>} */ (readerContext.content.querySelectorAll(".reader-page canvas"))) {
canvas.width = 0;
canvas.height = 0;
}
for (const image of readerContext.content.querySelectorAll(".reader-page img")) image.removeAttribute("src");
}
if (mode === "html") {
readerContext.htmlFrame?.remove();
readerContext.htmlFrame = null;
readerContext.content.classList.remove("reader-content--spreadsheet");
}
if (["audio", "video"].includes(mode) && readerContext.mediaElement) {
readerContext.mediaElement.pause();
readerContext.mediaElement.removeAttribute("src");
readerContext.mediaElement.load();
readerContext.mediaElement = null;
}
}
function registerReaderFormatAdapters() {
/** @template T @param {() => T | PromiseLike<T>} open @param {(value: Awaited<T>) => unknown} render */
function pair(open, render) {
return { open, render: /** @param {unknown} value */ (value) => render(/** @type {Awaited<T>} */ (value)) };
}
// Media rendering assigns the source directly to the native player.
const openMedia = () => null;
const formats = {
"pdf-pages": pair(() => readerContext.fetchReaderResponse(), readerContext.renderPdfPages),
"image-pages": pair(() => readerContext.fetchReaderResponse(), readerContext.renderImagePages),
pdf: pair(() => {
if (readerContext.extension !== "djvu") return readerContext.loadPdfDocument();
readerContext.releasePdfWorker?.();
// Keep the loader separate: its worker URL is relative to the loader module.
const loaderUrl = "/static/reader/pdf/reader-djvu-loader.mjs";
return import(loaderUrl).then(/** @param {typeof import('../pdf/reader-djvu-loader.mjs')} module */ module => module.openNativeDjvu(readerContext));
}, readerContext.renderPdf),
image: pair(readerContext.loadImageDocument, readerContext.renderImageDocument),
text: pair(readerContext.loadTextDocument, readerContext.renderTextDocument),
markdown: pair(readerContext.loadMarkdownDocument, readerContext.renderMarkdownDocument),
html: pair(readerContext.loadHtmlDocument, readerContext.renderHtml),
"epub-chapters": pair(readerContext.loadChapterManifestDocument, readerContext.renderChapterManifest),
audio: pair(openMedia, () => readerContext.renderMedia("audio")),
video: pair(openMedia, () => readerContext.renderMedia("video")),
swf: pair(readerContext.loadSwfDocument, readerContext.renderSwf)
};
for (const [mode, {open, render}] of Object.entries(formats))
readerContext.formatAdapters.register(mode, {
open,
render,
restore: (entry, generation) => readerContext.restoreFormat(mode, entry, generation),
dispose: () => readerContext.disposeFormatResources(mode)
});
}
async function resolveReaderSource() {
if (!readerContext.readerId || readerContext.sourceUrl) return;
readerContext.setReaderStage("resolve");
let resolved = readerContext.cachedReaderData;
if (!resolved?.url) {
try {
resolved = await readerContext.resolveReaderId(readerContext.readerId);
} catch (error) {
readerContext.assertReaderActive();
if (!readerContext.localReaderData?.url) throw error;
resolved = readerContext.localReaderData;
}
}
readerContext.assertReaderActive();
if (!resolved) throw new Error("Invalid reader metadata");
if (typeof resolved.original_extension === "string" && resolved.original_extension.toLowerCase().replace(/^\./,'') === 'djvu' &&
typeof resolved.extension === "string" && resolved.extension.toLowerCase() === 'pdf-pages') {
// Links saved before the direct reader may still resolve to an old page
// stream. Use their original download URL, never the legacy page images.
if (typeof resolved.download !== "string" || !readerContext.validSource(resolved.download) || !/\.djvu$/i.test(new URL(resolved.download).pathname))
throw new Error('DJVU_ORIGINAL_SOURCE_REQUIRED');
resolved = {...resolved,url:resolved.download,extension:'djvu'};
}
readerContext.sourceUrl = readerContext.normalizeSourceUrl(typeof resolved.url === "string" ? resolved.url : "");
readerContext.contentUrl = readerContext.readerContentUrl(readerContext.sourceUrl);
readerContext.downloadUrl = typeof resolved.download === "string" && resolved.download || readerContext.sourceUrl;
readerContext.chapterManifestUrl = typeof resolved.chapter_manifest === "string" ? resolved.chapter_manifest : "";
if (typeof resolved.extension === "string" && resolved.extension) readerContext.extension = resolved.extension.toLowerCase();
readerContext.capability = readerContext.readerRuntime.negotiate(VoiceOfMLReader.capability(readerContext.extension));
if (readerContext.capability.mode !== "pdf" && !readerContext.pdfPrimarySource()) readerContext.releasePdfWorker?.();
readerContext.readerRuntime.update("source", {
url: readerContext.sourceUrl,
contentUrl: readerContext.contentUrl,
downloadUrl: readerContext.downloadUrl,
extension: readerContext.extension,
metadata: resolved
});
readerContext.applyReaderMetadata(resolved);
}
async function start() {
const generation = readerContext.readerRuntime.currentGeneration("navigation");
// A PDF link already tells us which engine is needed. Start its import while
// an ID-only Reader link is resolving its source URL. The promise is reused
// by loadPdfDocument, so this does not create a second module request.
if (readerContext.extension === "pdf" || /\.pdf(?:$|[?#])/i.test(readerContext.sourceUrl)) readerContext.preloadPdfEngine().catch(() => {});
readerContext.readerSourcePromise = readerContext.measurePdf(readerContext.resolveReaderSource(), "resolve");
await readerContext.readerSourcePromise;
readerContext.assertReaderActive();
readerContext.syncCapabilityControls();
/** @type {HTMLAnchorElement} */ (readerContext.downloadButton).href = readerContext.readerDownloadUrl(readerContext.documentState.title, readerContext.downloadUrl);
if (
(!readerContext.validSource(readerContext.sourceUrl) && !readerContext.validSource(readerContext.chapterManifestUrl) && !readerContext.readerId) ||
readerContext.capability.readerMode === VoiceOfMLReader.ReaderMode.UNSUPPORTED
)
return readerContext.fail("此文件暂不支持在线阅读,请下载原文件。", "READER_UNSUPPORTED");
readerContext.formatAdapters.activate(readerContext.capability.mode);
readerContext.applyReaderMetadata(readerContext.resolvedReaderData);
try {
readerContext.setReaderStage("prepare");
const restorationPromise = readerContext.loadReaderRestoration();
readerContext.readerRestorationPromise = restorationPromise;
const prepareResources = new Set(readerContext.readerResources);
let pagedImageFallback = false;
try {
const active = readerContext.formatAdapters.active;
if (!active) throw new Error("READER_ADAPTER_MISSING");
const prepared = await readerContext.awaitReader(active.open());
readerContext.assertReaderActive();
const renderer = readerContext.formatAdapters.active;
if (!renderer) throw new Error("READER_ADAPTER_MISSING");
await renderer.render(prepared);
} catch (error) {
readerContext.assertReaderActive();
const originalExtension = originalImageFallbackExtension(readerContext.resolvedReaderData?.original_extension);
const mode = readerContext.capability.mode;
if ((mode !== "image-pages" && (mode !== "image" || readerContext.sourceUrl === readerContext.downloadUrl)) ||
!readerContext.validSource(readerContext.downloadUrl) || !originalExtension) throw error;
for (const cleanup of [...readerContext.readerResources]) {
if (prepareResources.has(cleanup)) continue;
cleanup();
readerContext.readerResources.delete(cleanup);
}
// The original can be viewed, but its single-image coordinates cannot
// replace the converted document's history or consume its bookmark.
pagedImageFallback = mode === "image-pages";
readerContext.formatAdapters.dispose();
readerContext.content.replaceChildren();
readerContext.extension = originalExtension;
readerContext.capability = readerContext.readerRuntime.negotiate(VoiceOfMLReader.capability(readerContext.extension));
readerContext.readerRuntime.update("source", { extension: readerContext.extension });
readerContext.updateDocumentState({ page: 1, pageCount: 0 });
readerContext.formatAdapters.activate(readerContext.capability.mode);
readerContext.syncCapabilityControls();
await readerContext.renderImageDocument(await readerContext.loadImageDocument(readerContext.downloadUrl));
}
readerContext.assertReaderActive();
readerContext.markReaderContentReady();
const lookup = restorationPromise.then(entry => ({ entry }));
const history = pagedImageFallback ? null : await readerContext.awaitReader(Promise.race([
lookup, readerContext.waitForReader(150).then(() => null)
]));
/** @param {Awaited<typeof restorationPromise>} restored */
const restore = async restored => {
// Capture the match before restoration consumes the bookmark parameters.
const explicitBookmark = readerContext.capability.mode === "markdown" && restored?.id && restored.id === readerContext.params.get("bookmark") &&
restored.url === readerContext.params.get("bookmark_source");
await readerContext.applyInitialRestoration(restored, generation);
readerContext.assertReaderActive();
// A Markdown anchor overrides ordinary progress, but not a matched bookmark.
if (readerContext.capability.mode === "markdown" && !explicitBookmark && location.hash &&
readerContext.isReaderGenerationCurrent("navigation", generation)) {
try {
const id = decodeURIComponent(location.hash.slice(1));
const article = readerContext.content.querySelector(".reader-markdown");
const target = [...(article?.querySelectorAll("[id], [name]") || [])]
.find(element => element.id === id || element.getAttribute("name") === id);
target?.scrollIntoView({ block: "start" });
} catch (_) {}
}
};
if (history) await restore(history.entry);
if (!readerContext.setReaderPhase("ready")) return;
const enableSaving = () => {
readerContext.updateDocumentState({ restorationReady: !readerContext.restorationFailed });
readerContext.syncHeadingLocation();
readerContext.updateProgressTools();
readerContext.scheduleSave();
};
if (history) enableSaving();
else if (pagedImageFallback) readerContext.updateDocumentState({ restorationReady: false });
else {
// Keep the UI usable even if IndexedDB never responds. The old
// position is protected until the read is known to have succeeded.
void readerContext.awaitReader(lookup).then(async ({ entry }) => {
await restore(entry);
enableSaving();
}).catch(error => {
if (!readerContext.readerAbortController.signal.aborted)
readerContext.reportNavigationError(error, generation);
});
}
readerContext.schedulePdfBookSearchPrefetch();
readerContext.updateProgressTools();
if (readerContext.fullSearchInput.value.trim()) readerContext.runFullSearch();
} catch (error) {
const detail = error !== null && (typeof error === "object" || typeof error === "function") ? error : null;
if (readerContext.readerAbortController.signal.aborted || (detail && "name" in detail && detail.name === "AbortError")) return;
console.error(error);
const message = detail && "code" in detail && detail.code === "READER_PDF_RATE_LIMIT" ? "请求过于频繁,请稍后重试加载。" :
detail && "message" in detail && typeof detail.message === "string" && detail.message ? detail.message : "原文件加载失败";
const fallback = "READER_PARSE";
const code = readerContext.classifyReaderError(error, fallback);
readerContext.fail(message, typeof code === "string" ? code : fallback);
}
}
function syncCapabilityControls() {
readerContext.content.dataset.mode = readerContext.capability.mode || "unsupported";
/** @type {HTMLElement} */ (readerContext.pageInvertToggle).hidden = !["pdf", "pdf-pages"].includes(readerContext.capability.mode || "unsupported");
const features = readerContext.capability.features;
/** @type {HTMLElement} */ (document.querySelector(".page-controls")).hidden = !("pagination" in features && features.pagination);
/** @type {HTMLElement} */ (document.querySelector(".zoom-controls")).hidden = !("zoom" in features && features.zoom);
/** @type {HTMLElement} */ (readerContext.mediaTab).hidden = !("media" in features && features.media);
const noNativeText = !!readerContext.pdfDocument?.readerNoSearchableText && !readerContext.ocrManifestUrl;
const searchOpen = readerContext.panelState.selected === "full-search";
if (noNativeText && !searchOpen && document.activeElement === readerContext.fullSearchButton)
/** @type {HTMLElement} */ (document.querySelector("#history")).focus({ preventScroll: true });
/** @type {HTMLElement} */ (readerContext.fullSearchButton).hidden = !("search" in features && features.search) || (noNativeText && !searchOpen) || (readerContext.capability.mode === "pdf-pages" && !readerContext.ocrManifestUrl);
/** @type {HTMLElement} */ (readerContext.bookmarkRibbon).hidden = !("bookmarks" in features && features.bookmarks);
/** @type {HTMLElement} */ (readerContext.readingProgressSummary.querySelector(".reader-progress-bookmark")).hidden =
!("bookmarks" in features && features.bookmarks);
if (readerContext.panelState.selected === "full-search" && /** @type {HTMLElement} */ (readerContext.fullSearchButton).hidden)
readerContext.closeFullSearchView();
if (
(readerContext.panelState.selected === "media" && /** @type {HTMLElement} */ (readerContext.mediaTab).hidden) ||
(readerContext.panelState.selected === "full-search" && /** @type {HTMLElement} */ (readerContext.fullSearchButton).hidden)
)
readerContext.selectPanel("toc");
}
return {
disposeFormatResources,
registerReaderFormatAdapters,
resolveReaderSource,
start,
syncCapabilityControls
};
}
|