pdfjs-reader-core 0.1.2 → 0.1.3
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 +66 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +84 -56
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -8810,66 +8810,94 @@ var init_PDFViewerClient = __esm({
|
|
|
8810
8810
|
const { viewerStore } = usePDFViewerStores();
|
|
8811
8811
|
const loadingRef = (0, import_react39.useRef)(false);
|
|
8812
8812
|
const srcRef = (0, import_react39.useRef)(src);
|
|
8813
|
+
const onDocumentLoadRef = (0, import_react39.useRef)(onDocumentLoad);
|
|
8814
|
+
const onErrorRef = (0, import_react39.useRef)(onError);
|
|
8815
|
+
const onPageChangeRef = (0, import_react39.useRef)(onPageChange);
|
|
8816
|
+
const onScaleChangeRef = (0, import_react39.useRef)(onScaleChange);
|
|
8817
|
+
onDocumentLoadRef.current = onDocumentLoad;
|
|
8818
|
+
onErrorRef.current = onError;
|
|
8819
|
+
onPageChangeRef.current = onPageChange;
|
|
8820
|
+
onScaleChangeRef.current = onScaleChange;
|
|
8813
8821
|
const currentPage = useViewerStore((s) => s.currentPage);
|
|
8814
8822
|
const scale = useViewerStore((s) => s.scale);
|
|
8815
8823
|
const theme = useViewerStore((s) => s.theme);
|
|
8816
8824
|
const isLoading = useViewerStore((s) => s.isLoading);
|
|
8817
8825
|
const error = useViewerStore((s) => s.error);
|
|
8818
8826
|
const sidebarOpen = useViewerStore((s) => s.sidebarOpen);
|
|
8819
|
-
const
|
|
8820
|
-
|
|
8821
|
-
loadingRef.current =
|
|
8822
|
-
|
|
8823
|
-
|
|
8824
|
-
|
|
8825
|
-
|
|
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]);
|
|
8827
|
+
const [retryTrigger, setRetryTrigger] = (0, import_react39.useState)(0);
|
|
8828
|
+
const handleRetry = (0, import_react39.useCallback)(() => {
|
|
8829
|
+
loadingRef.current = false;
|
|
8830
|
+
srcRef.current = null;
|
|
8831
|
+
viewerStore.getState().setError(null);
|
|
8832
|
+
setRetryTrigger((c) => c + 1);
|
|
8833
|
+
}, [viewerStore, src]);
|
|
8851
8834
|
(0, import_react39.useEffect)(() => {
|
|
8835
|
+
if (loadingRef.current) {
|
|
8836
|
+
return;
|
|
8837
|
+
}
|
|
8838
|
+
if (srcRef.current === src && viewerStore.getState().document) {
|
|
8839
|
+
return;
|
|
8840
|
+
}
|
|
8852
8841
|
srcRef.current = src;
|
|
8842
|
+
loadingRef.current = true;
|
|
8853
8843
|
const currentDoc = viewerStore.getState().document;
|
|
8854
8844
|
if (currentDoc) {
|
|
8855
8845
|
viewerStore.getState().reset();
|
|
8856
8846
|
}
|
|
8847
|
+
const loadDoc = async () => {
|
|
8848
|
+
try {
|
|
8849
|
+
viewerStore.getState().setLoading(true);
|
|
8850
|
+
viewerStore.getState().setError(null);
|
|
8851
|
+
const { document: document2, numPages } = await loadDocument({
|
|
8852
|
+
src,
|
|
8853
|
+
workerSrc
|
|
8854
|
+
});
|
|
8855
|
+
if (srcRef.current === src) {
|
|
8856
|
+
viewerStore.getState().setDocument(document2);
|
|
8857
|
+
if (initialPage !== 1) {
|
|
8858
|
+
viewerStore.getState().goToPage(initialPage);
|
|
8859
|
+
}
|
|
8860
|
+
if (typeof initialScale === "number" && initialScale !== 1) {
|
|
8861
|
+
viewerStore.getState().setScale(initialScale);
|
|
8862
|
+
}
|
|
8863
|
+
onDocumentLoadRef.current?.({ document: document2, numPages });
|
|
8864
|
+
} else {
|
|
8865
|
+
document2.destroy();
|
|
8866
|
+
}
|
|
8867
|
+
} catch (err) {
|
|
8868
|
+
if (srcRef.current === src) {
|
|
8869
|
+
const error2 = err instanceof Error ? err : new Error("Failed to load document");
|
|
8870
|
+
viewerStore.getState().setError(error2);
|
|
8871
|
+
onErrorRef.current?.(error2);
|
|
8872
|
+
}
|
|
8873
|
+
} finally {
|
|
8874
|
+
loadingRef.current = false;
|
|
8875
|
+
}
|
|
8876
|
+
};
|
|
8857
8877
|
loadDoc();
|
|
8858
8878
|
return () => {
|
|
8859
8879
|
srcRef.current = null;
|
|
8860
8880
|
};
|
|
8861
|
-
}, [src,
|
|
8881
|
+
}, [src, workerSrc, initialPage, initialScale, viewerStore, retryTrigger]);
|
|
8862
8882
|
(0, import_react39.useEffect)(() => {
|
|
8863
8883
|
return () => {
|
|
8864
8884
|
viewerStore.getState().reset();
|
|
8865
8885
|
};
|
|
8866
8886
|
}, [viewerStore]);
|
|
8887
|
+
const prevPageRef = (0, import_react39.useRef)(currentPage);
|
|
8867
8888
|
(0, import_react39.useEffect)(() => {
|
|
8868
|
-
|
|
8869
|
-
|
|
8889
|
+
if (prevPageRef.current !== currentPage) {
|
|
8890
|
+
prevPageRef.current = currentPage;
|
|
8891
|
+
onPageChangeRef.current?.(currentPage);
|
|
8892
|
+
}
|
|
8893
|
+
}, [currentPage]);
|
|
8894
|
+
const prevScaleRef = (0, import_react39.useRef)(scale);
|
|
8870
8895
|
(0, import_react39.useEffect)(() => {
|
|
8871
|
-
|
|
8872
|
-
|
|
8896
|
+
if (prevScaleRef.current !== scale) {
|
|
8897
|
+
prevScaleRef.current = scale;
|
|
8898
|
+
onScaleChangeRef.current?.(scale);
|
|
8899
|
+
}
|
|
8900
|
+
}, [scale]);
|
|
8873
8901
|
const themeClass = theme === "dark" ? "dark" : "";
|
|
8874
8902
|
if (error) {
|
|
8875
8903
|
return /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(
|
|
@@ -8888,7 +8916,7 @@ var init_PDFViewerClient = __esm({
|
|
|
8888
8916
|
/* @__PURE__ */ (0, import_jsx_runtime25.jsx)(
|
|
8889
8917
|
"button",
|
|
8890
8918
|
{
|
|
8891
|
-
onClick:
|
|
8919
|
+
onClick: handleRetry,
|
|
8892
8920
|
className: "mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600",
|
|
8893
8921
|
children: "Retry"
|
|
8894
8922
|
}
|