pdfjs-reader-core 0.1.2 → 0.1.4
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 +103 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +121 -73
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -338,13 +338,21 @@ function createViewerStore(initialOverrides = {}) {
|
|
|
338
338
|
...initialOverrides,
|
|
339
339
|
// Document actions
|
|
340
340
|
setDocument: (document2) => {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
341
|
+
if (document2) {
|
|
342
|
+
set({
|
|
343
|
+
document: document2,
|
|
344
|
+
numPages: document2.numPages,
|
|
345
|
+
isLoading: false,
|
|
346
|
+
error: null,
|
|
347
|
+
currentPage: 1
|
|
348
|
+
});
|
|
349
|
+
} else {
|
|
350
|
+
set({
|
|
351
|
+
document: null,
|
|
352
|
+
numPages: 0,
|
|
353
|
+
isLoading: false
|
|
354
|
+
});
|
|
355
|
+
}
|
|
348
356
|
},
|
|
349
357
|
setLoading: (isLoading) => {
|
|
350
358
|
set({ isLoading });
|
|
@@ -8777,6 +8785,17 @@ var PDFViewerClient_exports = {};
|
|
|
8777
8785
|
__export(PDFViewerClient_exports, {
|
|
8778
8786
|
PDFViewerClient: () => PDFViewerClient
|
|
8779
8787
|
});
|
|
8788
|
+
function getSrcIdentifier(src) {
|
|
8789
|
+
if (typeof src === "string") {
|
|
8790
|
+
return src;
|
|
8791
|
+
}
|
|
8792
|
+
const data = src instanceof ArrayBuffer ? new Uint8Array(src) : src;
|
|
8793
|
+
const len = data.byteLength;
|
|
8794
|
+
if (len === 0) return "empty";
|
|
8795
|
+
const first = Array.from(data.slice(0, 4)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
8796
|
+
const last = Array.from(data.slice(-4)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
8797
|
+
return `binary:${len}:${first}:${last}`;
|
|
8798
|
+
}
|
|
8780
8799
|
var import_react39, import_jsx_runtime25, PDFViewerInner, PDFViewerClient;
|
|
8781
8800
|
var init_PDFViewerClient = __esm({
|
|
8782
8801
|
"src/components/PDFViewer/PDFViewerClient.tsx"() {
|
|
@@ -8808,68 +8827,97 @@ var init_PDFViewerClient = __esm({
|
|
|
8808
8827
|
className
|
|
8809
8828
|
}) {
|
|
8810
8829
|
const { viewerStore } = usePDFViewerStores();
|
|
8811
|
-
const
|
|
8812
|
-
const
|
|
8830
|
+
const mountedRef = (0, import_react39.useRef)(true);
|
|
8831
|
+
const [, setLoadState] = (0, import_react39.useState)("idle");
|
|
8832
|
+
const onDocumentLoadRef = (0, import_react39.useRef)(onDocumentLoad);
|
|
8833
|
+
const onErrorRef = (0, import_react39.useRef)(onError);
|
|
8834
|
+
const onPageChangeRef = (0, import_react39.useRef)(onPageChange);
|
|
8835
|
+
const onScaleChangeRef = (0, import_react39.useRef)(onScaleChange);
|
|
8836
|
+
onDocumentLoadRef.current = onDocumentLoad;
|
|
8837
|
+
onErrorRef.current = onError;
|
|
8838
|
+
onPageChangeRef.current = onPageChange;
|
|
8839
|
+
onScaleChangeRef.current = onScaleChange;
|
|
8840
|
+
const srcIdRef = (0, import_react39.useRef)(null);
|
|
8813
8841
|
const currentPage = useViewerStore((s) => s.currentPage);
|
|
8814
8842
|
const scale = useViewerStore((s) => s.scale);
|
|
8815
8843
|
const theme = useViewerStore((s) => s.theme);
|
|
8816
8844
|
const isLoading = useViewerStore((s) => s.isLoading);
|
|
8817
8845
|
const error = useViewerStore((s) => s.error);
|
|
8818
8846
|
const sidebarOpen = useViewerStore((s) => s.sidebarOpen);
|
|
8819
|
-
const
|
|
8820
|
-
|
|
8821
|
-
|
|
8822
|
-
|
|
8823
|
-
|
|
8824
|
-
|
|
8825
|
-
const { document: document2, numPages } = await loadDocument({
|
|
8826
|
-
src,
|
|
8827
|
-
workerSrc
|
|
8828
|
-
});
|
|
8829
|
-
if (srcRef.current === src) {
|
|
8830
|
-
viewerStore.getState().setDocument(document2);
|
|
8831
|
-
if (initialPage !== 1) {
|
|
8832
|
-
viewerStore.getState().goToPage(initialPage);
|
|
8833
|
-
}
|
|
8834
|
-
if (typeof initialScale === "number" && initialScale !== 1) {
|
|
8835
|
-
viewerStore.getState().setScale(initialScale);
|
|
8836
|
-
}
|
|
8837
|
-
onDocumentLoad?.({ document: document2, numPages });
|
|
8838
|
-
} else {
|
|
8839
|
-
document2.destroy();
|
|
8840
|
-
}
|
|
8841
|
-
} catch (err) {
|
|
8842
|
-
if (srcRef.current === src) {
|
|
8843
|
-
const error2 = err instanceof Error ? err : new Error("Failed to load document");
|
|
8844
|
-
viewerStore.getState().setError(error2);
|
|
8845
|
-
onError?.(error2);
|
|
8846
|
-
}
|
|
8847
|
-
} finally {
|
|
8848
|
-
loadingRef.current = false;
|
|
8849
|
-
}
|
|
8850
|
-
}, [src, workerSrc, initialPage, initialScale, onDocumentLoad, onError, viewerStore]);
|
|
8847
|
+
const srcId = getSrcIdentifier(src);
|
|
8848
|
+
const handleRetry = (0, import_react39.useCallback)(() => {
|
|
8849
|
+
srcIdRef.current = null;
|
|
8850
|
+
viewerStore.getState().setError(null);
|
|
8851
|
+
setLoadState("idle");
|
|
8852
|
+
}, [viewerStore]);
|
|
8851
8853
|
(0, import_react39.useEffect)(() => {
|
|
8852
|
-
|
|
8854
|
+
mountedRef.current = true;
|
|
8855
|
+
return () => {
|
|
8856
|
+
mountedRef.current = false;
|
|
8857
|
+
};
|
|
8858
|
+
}, []);
|
|
8859
|
+
(0, import_react39.useEffect)(() => {
|
|
8860
|
+
if (srcIdRef.current === srcId && viewerStore.getState().document) {
|
|
8861
|
+
return;
|
|
8862
|
+
}
|
|
8863
|
+
const loadId = srcId;
|
|
8864
|
+
srcIdRef.current = srcId;
|
|
8853
8865
|
const currentDoc = viewerStore.getState().document;
|
|
8854
8866
|
if (currentDoc) {
|
|
8855
|
-
|
|
8867
|
+
currentDoc.destroy();
|
|
8868
|
+
viewerStore.getState().setDocument(null);
|
|
8856
8869
|
}
|
|
8857
|
-
loadDoc()
|
|
8858
|
-
|
|
8859
|
-
|
|
8870
|
+
const loadDoc = async () => {
|
|
8871
|
+
if (!mountedRef.current) return;
|
|
8872
|
+
try {
|
|
8873
|
+
viewerStore.getState().setLoading(true);
|
|
8874
|
+
viewerStore.getState().setError(null);
|
|
8875
|
+
setLoadState("loading");
|
|
8876
|
+
const { document: document2, numPages } = await loadDocument({
|
|
8877
|
+
src,
|
|
8878
|
+
workerSrc
|
|
8879
|
+
});
|
|
8880
|
+
if (mountedRef.current && srcIdRef.current === loadId) {
|
|
8881
|
+
viewerStore.getState().setDocument(document2);
|
|
8882
|
+
setLoadState("loaded");
|
|
8883
|
+
if (initialPage !== 1) {
|
|
8884
|
+
viewerStore.getState().goToPage(initialPage);
|
|
8885
|
+
}
|
|
8886
|
+
if (typeof initialScale === "number" && initialScale !== 1) {
|
|
8887
|
+
viewerStore.getState().setScale(initialScale);
|
|
8888
|
+
}
|
|
8889
|
+
onDocumentLoadRef.current?.({ document: document2, numPages });
|
|
8890
|
+
} else {
|
|
8891
|
+
document2.destroy();
|
|
8892
|
+
}
|
|
8893
|
+
} catch (err) {
|
|
8894
|
+
if (mountedRef.current && srcIdRef.current === loadId) {
|
|
8895
|
+
const error2 = err instanceof Error ? err : new Error("Failed to load document");
|
|
8896
|
+
viewerStore.getState().setError(error2);
|
|
8897
|
+
viewerStore.getState().setLoading(false);
|
|
8898
|
+
setLoadState("error");
|
|
8899
|
+
onErrorRef.current?.(error2);
|
|
8900
|
+
}
|
|
8901
|
+
}
|
|
8860
8902
|
};
|
|
8861
|
-
|
|
8862
|
-
(0, import_react39.useEffect)(() => {
|
|
8903
|
+
loadDoc();
|
|
8863
8904
|
return () => {
|
|
8864
|
-
viewerStore.getState().reset();
|
|
8865
8905
|
};
|
|
8866
|
-
}, [viewerStore]);
|
|
8906
|
+
}, [srcId, src, workerSrc, initialPage, initialScale, viewerStore]);
|
|
8907
|
+
const prevPageRef = (0, import_react39.useRef)(currentPage);
|
|
8867
8908
|
(0, import_react39.useEffect)(() => {
|
|
8868
|
-
|
|
8869
|
-
|
|
8909
|
+
if (prevPageRef.current !== currentPage) {
|
|
8910
|
+
prevPageRef.current = currentPage;
|
|
8911
|
+
onPageChangeRef.current?.(currentPage);
|
|
8912
|
+
}
|
|
8913
|
+
}, [currentPage]);
|
|
8914
|
+
const prevScaleRef = (0, import_react39.useRef)(scale);
|
|
8870
8915
|
(0, import_react39.useEffect)(() => {
|
|
8871
|
-
|
|
8872
|
-
|
|
8916
|
+
if (prevScaleRef.current !== scale) {
|
|
8917
|
+
prevScaleRef.current = scale;
|
|
8918
|
+
onScaleChangeRef.current?.(scale);
|
|
8919
|
+
}
|
|
8920
|
+
}, [scale]);
|
|
8873
8921
|
const themeClass = theme === "dark" ? "dark" : "";
|
|
8874
8922
|
if (error) {
|
|
8875
8923
|
return /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(
|
|
@@ -8888,7 +8936,7 @@ var init_PDFViewerClient = __esm({
|
|
|
8888
8936
|
/* @__PURE__ */ (0, import_jsx_runtime25.jsx)(
|
|
8889
8937
|
"button",
|
|
8890
8938
|
{
|
|
8891
|
-
onClick:
|
|
8939
|
+
onClick: handleRetry,
|
|
8892
8940
|
className: "mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600",
|
|
8893
8941
|
children: "Retry"
|
|
8894
8942
|
}
|