pdfjs-reader-core 0.2.14 → 0.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +41 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +41 -22
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
- package/runkit-example.js +91 -0
package/dist/index.cjs
CHANGED
|
@@ -101,13 +101,14 @@ async function loadDocument(options) {
|
|
|
101
101
|
const cachedDoc = documentCache.get(cacheKey);
|
|
102
102
|
try {
|
|
103
103
|
const numPages = cachedDoc.numPages;
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
const isDestroyed = cachedDoc._transport?.destroyed || cachedDoc.destroyed;
|
|
105
|
+
if (numPages > 0 && !isDestroyed) {
|
|
106
106
|
return {
|
|
107
107
|
document: cachedDoc,
|
|
108
108
|
numPages
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
|
+
documentCache.delete(cacheKey);
|
|
111
112
|
} catch {
|
|
112
113
|
documentCache.delete(cacheKey);
|
|
113
114
|
}
|
|
@@ -10226,23 +10227,41 @@ var init_PDFViewerClient = __esm({
|
|
|
10226
10227
|
abortControllerRef.current = abortController;
|
|
10227
10228
|
const currentDoc = viewerStore.getState().document;
|
|
10228
10229
|
if (currentDoc) {
|
|
10229
|
-
|
|
10230
|
-
|
|
10230
|
+
try {
|
|
10231
|
+
currentDoc.destroy();
|
|
10232
|
+
} catch {
|
|
10233
|
+
}
|
|
10231
10234
|
}
|
|
10232
|
-
viewerStore.
|
|
10233
|
-
|
|
10235
|
+
viewerStore.setState({
|
|
10236
|
+
document: null,
|
|
10237
|
+
isLoading: true,
|
|
10238
|
+
loadingProgress: { phase: "fetching" },
|
|
10239
|
+
error: null
|
|
10240
|
+
});
|
|
10234
10241
|
setLoadState("loading");
|
|
10242
|
+
let lastProgressUpdate = 0;
|
|
10243
|
+
let lastPercent = -1;
|
|
10244
|
+
const PROGRESS_THROTTLE_MS = 100;
|
|
10245
|
+
const PROGRESS_MIN_CHANGE = 5;
|
|
10235
10246
|
const loadDoc = async () => {
|
|
10236
10247
|
if (!mountedRef.current || abortController.signal.aborted) return;
|
|
10237
10248
|
try {
|
|
10238
|
-
viewerStore.getState().setLoadingProgress({ phase: "fetching" });
|
|
10239
10249
|
const { document: document2, numPages } = await loadDocument({
|
|
10240
10250
|
src,
|
|
10241
10251
|
workerSrc,
|
|
10242
10252
|
signal: abortController.signal,
|
|
10243
10253
|
onProgress: ({ loaded, total }) => {
|
|
10244
|
-
if (mountedRef.current
|
|
10245
|
-
|
|
10254
|
+
if (!mountedRef.current || srcIdRef.current !== loadId || abortController.signal.aborted) {
|
|
10255
|
+
return;
|
|
10256
|
+
}
|
|
10257
|
+
const now = Date.now();
|
|
10258
|
+
const percent = total > 0 ? Math.round(loaded / total * 100) : 0;
|
|
10259
|
+
const timePassed = now - lastProgressUpdate >= PROGRESS_THROTTLE_MS;
|
|
10260
|
+
const percentChanged = Math.abs(percent - lastPercent) >= PROGRESS_MIN_CHANGE;
|
|
10261
|
+
const isComplete = percent >= 100;
|
|
10262
|
+
if (timePassed && percentChanged || isComplete) {
|
|
10263
|
+
lastProgressUpdate = now;
|
|
10264
|
+
lastPercent = percent;
|
|
10246
10265
|
viewerStore.getState().setLoadingProgress({
|
|
10247
10266
|
phase: "fetching",
|
|
10248
10267
|
percent,
|
|
@@ -10252,21 +10271,22 @@ var init_PDFViewerClient = __esm({
|
|
|
10252
10271
|
}
|
|
10253
10272
|
}
|
|
10254
10273
|
});
|
|
10255
|
-
if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
|
|
10256
|
-
viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
|
|
10257
|
-
}
|
|
10258
10274
|
if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
|
|
10259
10275
|
viewerStore.getState().setDocument(document2);
|
|
10260
10276
|
setLoadState("loaded");
|
|
10261
|
-
if (initialPage !== 1) {
|
|
10262
|
-
|
|
10263
|
-
|
|
10264
|
-
|
|
10265
|
-
|
|
10266
|
-
|
|
10267
|
-
|
|
10268
|
-
|
|
10269
|
-
|
|
10277
|
+
if (initialPage !== 1 || typeof initialScale === "number" || initialScale === "page-fit") {
|
|
10278
|
+
const updates = {};
|
|
10279
|
+
if (initialPage !== 1) {
|
|
10280
|
+
updates.currentPage = Math.max(1, Math.min(initialPage, numPages));
|
|
10281
|
+
}
|
|
10282
|
+
if (typeof initialScale === "number") {
|
|
10283
|
+
updates.scale = initialScale;
|
|
10284
|
+
} else if (initialScale === "page-fit") {
|
|
10285
|
+
updates.scale = 0.75;
|
|
10286
|
+
}
|
|
10287
|
+
if (Object.keys(updates).length > 0) {
|
|
10288
|
+
viewerStore.setState(updates);
|
|
10289
|
+
}
|
|
10270
10290
|
}
|
|
10271
10291
|
onDocumentLoadRef.current?.({ document: document2, numPages });
|
|
10272
10292
|
} else {
|
|
@@ -10283,7 +10303,6 @@ var init_PDFViewerClient = __esm({
|
|
|
10283
10303
|
if (mountedRef.current && srcIdRef.current === loadId) {
|
|
10284
10304
|
const error2 = err instanceof Error ? err : new Error("Failed to load document");
|
|
10285
10305
|
viewerStore.getState().setError(error2);
|
|
10286
|
-
viewerStore.getState().setLoading(false);
|
|
10287
10306
|
setLoadState("error");
|
|
10288
10307
|
onErrorRef.current?.(error2);
|
|
10289
10308
|
}
|