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.js
CHANGED
|
@@ -78,13 +78,14 @@ async function loadDocument(options) {
|
|
|
78
78
|
const cachedDoc = documentCache.get(cacheKey);
|
|
79
79
|
try {
|
|
80
80
|
const numPages = cachedDoc.numPages;
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
const isDestroyed = cachedDoc._transport?.destroyed || cachedDoc.destroyed;
|
|
82
|
+
if (numPages > 0 && !isDestroyed) {
|
|
83
83
|
return {
|
|
84
84
|
document: cachedDoc,
|
|
85
85
|
numPages
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
|
+
documentCache.delete(cacheKey);
|
|
88
89
|
} catch {
|
|
89
90
|
documentCache.delete(cacheKey);
|
|
90
91
|
}
|
|
@@ -10196,23 +10197,41 @@ var init_PDFViewerClient = __esm({
|
|
|
10196
10197
|
abortControllerRef.current = abortController;
|
|
10197
10198
|
const currentDoc = viewerStore.getState().document;
|
|
10198
10199
|
if (currentDoc) {
|
|
10199
|
-
|
|
10200
|
-
|
|
10200
|
+
try {
|
|
10201
|
+
currentDoc.destroy();
|
|
10202
|
+
} catch {
|
|
10203
|
+
}
|
|
10201
10204
|
}
|
|
10202
|
-
viewerStore.
|
|
10203
|
-
|
|
10205
|
+
viewerStore.setState({
|
|
10206
|
+
document: null,
|
|
10207
|
+
isLoading: true,
|
|
10208
|
+
loadingProgress: { phase: "fetching" },
|
|
10209
|
+
error: null
|
|
10210
|
+
});
|
|
10204
10211
|
setLoadState("loading");
|
|
10212
|
+
let lastProgressUpdate = 0;
|
|
10213
|
+
let lastPercent = -1;
|
|
10214
|
+
const PROGRESS_THROTTLE_MS = 100;
|
|
10215
|
+
const PROGRESS_MIN_CHANGE = 5;
|
|
10205
10216
|
const loadDoc = async () => {
|
|
10206
10217
|
if (!mountedRef.current || abortController.signal.aborted) return;
|
|
10207
10218
|
try {
|
|
10208
|
-
viewerStore.getState().setLoadingProgress({ phase: "fetching" });
|
|
10209
10219
|
const { document: document2, numPages } = await loadDocument({
|
|
10210
10220
|
src,
|
|
10211
10221
|
workerSrc,
|
|
10212
10222
|
signal: abortController.signal,
|
|
10213
10223
|
onProgress: ({ loaded, total }) => {
|
|
10214
|
-
if (mountedRef.current
|
|
10215
|
-
|
|
10224
|
+
if (!mountedRef.current || srcIdRef.current !== loadId || abortController.signal.aborted) {
|
|
10225
|
+
return;
|
|
10226
|
+
}
|
|
10227
|
+
const now = Date.now();
|
|
10228
|
+
const percent = total > 0 ? Math.round(loaded / total * 100) : 0;
|
|
10229
|
+
const timePassed = now - lastProgressUpdate >= PROGRESS_THROTTLE_MS;
|
|
10230
|
+
const percentChanged = Math.abs(percent - lastPercent) >= PROGRESS_MIN_CHANGE;
|
|
10231
|
+
const isComplete = percent >= 100;
|
|
10232
|
+
if (timePassed && percentChanged || isComplete) {
|
|
10233
|
+
lastProgressUpdate = now;
|
|
10234
|
+
lastPercent = percent;
|
|
10216
10235
|
viewerStore.getState().setLoadingProgress({
|
|
10217
10236
|
phase: "fetching",
|
|
10218
10237
|
percent,
|
|
@@ -10222,21 +10241,22 @@ var init_PDFViewerClient = __esm({
|
|
|
10222
10241
|
}
|
|
10223
10242
|
}
|
|
10224
10243
|
});
|
|
10225
|
-
if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
|
|
10226
|
-
viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
|
|
10227
|
-
}
|
|
10228
10244
|
if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
|
|
10229
10245
|
viewerStore.getState().setDocument(document2);
|
|
10230
10246
|
setLoadState("loaded");
|
|
10231
|
-
if (initialPage !== 1) {
|
|
10232
|
-
|
|
10233
|
-
|
|
10234
|
-
|
|
10235
|
-
|
|
10236
|
-
|
|
10237
|
-
|
|
10238
|
-
|
|
10239
|
-
|
|
10247
|
+
if (initialPage !== 1 || typeof initialScale === "number" || initialScale === "page-fit") {
|
|
10248
|
+
const updates = {};
|
|
10249
|
+
if (initialPage !== 1) {
|
|
10250
|
+
updates.currentPage = Math.max(1, Math.min(initialPage, numPages));
|
|
10251
|
+
}
|
|
10252
|
+
if (typeof initialScale === "number") {
|
|
10253
|
+
updates.scale = initialScale;
|
|
10254
|
+
} else if (initialScale === "page-fit") {
|
|
10255
|
+
updates.scale = 0.75;
|
|
10256
|
+
}
|
|
10257
|
+
if (Object.keys(updates).length > 0) {
|
|
10258
|
+
viewerStore.setState(updates);
|
|
10259
|
+
}
|
|
10240
10260
|
}
|
|
10241
10261
|
onDocumentLoadRef.current?.({ document: document2, numPages });
|
|
10242
10262
|
} else {
|
|
@@ -10253,7 +10273,6 @@ var init_PDFViewerClient = __esm({
|
|
|
10253
10273
|
if (mountedRef.current && srcIdRef.current === loadId) {
|
|
10254
10274
|
const error2 = err instanceof Error ? err : new Error("Failed to load document");
|
|
10255
10275
|
viewerStore.getState().setError(error2);
|
|
10256
|
-
viewerStore.getState().setLoading(false);
|
|
10257
10276
|
setLoadState("error");
|
|
10258
10277
|
onErrorRef.current?.(error2);
|
|
10259
10278
|
}
|