pdfjs-reader-core 0.2.10 → 0.2.12

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 CHANGED
@@ -86,16 +86,28 @@ async function loadDocument(options) {
86
86
  onProgress,
87
87
  enableRangeRequests = true,
88
88
  enableStreaming = true,
89
- cacheDocument = true
89
+ cacheDocument = true,
90
+ signal
90
91
  } = options;
92
+ if (signal?.aborted) {
93
+ throw new DOMException("Aborted", "AbortError");
94
+ }
91
95
  await initializePDFJS({ workerSrc });
96
+ if (signal?.aborted) {
97
+ throw new DOMException("Aborted", "AbortError");
98
+ }
92
99
  const cacheKey = typeof src === "string" ? src : null;
93
100
  if (cacheKey && cacheDocument && documentCache.has(cacheKey)) {
94
101
  const cachedDoc = documentCache.get(cacheKey);
95
- return {
96
- document: cachedDoc,
97
- numPages: cachedDoc.numPages
98
- };
102
+ try {
103
+ const numPages = cachedDoc.numPages;
104
+ return {
105
+ document: cachedDoc,
106
+ numPages
107
+ };
108
+ } catch {
109
+ documentCache.delete(cacheKey);
110
+ }
99
111
  }
100
112
  const loadingParams = {
101
113
  password,
@@ -116,12 +128,37 @@ async function loadDocument(options) {
116
128
  loadingParams.data = src;
117
129
  }
118
130
  const loadingTask = pdfjsLib.getDocument(loadingParams);
131
+ let abortHandler = null;
132
+ if (signal) {
133
+ abortHandler = () => {
134
+ loadingTask.destroy();
135
+ };
136
+ signal.addEventListener("abort", abortHandler);
137
+ }
119
138
  if (onProgress) {
120
139
  loadingTask.onProgress = ({ loaded, total }) => {
121
140
  onProgress({ loaded, total });
122
141
  };
123
142
  }
124
- const document2 = await loadingTask.promise;
143
+ let document2;
144
+ try {
145
+ document2 = await loadingTask.promise;
146
+ } catch (error) {
147
+ if (signal && abortHandler) {
148
+ signal.removeEventListener("abort", abortHandler);
149
+ }
150
+ if (signal?.aborted) {
151
+ throw new DOMException("Aborted", "AbortError");
152
+ }
153
+ throw error;
154
+ }
155
+ if (signal && abortHandler) {
156
+ signal.removeEventListener("abort", abortHandler);
157
+ }
158
+ if (signal?.aborted) {
159
+ document2.destroy();
160
+ throw new DOMException("Aborted", "AbortError");
161
+ }
125
162
  if (cacheKey && cacheDocument) {
126
163
  documentCache.set(cacheKey, document2);
127
164
  }
@@ -7734,7 +7771,7 @@ function formatBytes(bytes) {
7734
7771
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
7735
7772
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
7736
7773
  }
7737
- var import_react33, import_jsx_runtime19, phaseMessages, PDFLoadingScreen;
7774
+ var import_react33, import_jsx_runtime19, phaseMessages, phaseIcons, PDFLoadingScreen;
7738
7775
  var init_PDFLoadingScreen = __esm({
7739
7776
  "src/components/PDFLoadingScreen/PDFLoadingScreen.tsx"() {
7740
7777
  "use strict";
@@ -7742,10 +7779,16 @@ var init_PDFLoadingScreen = __esm({
7742
7779
  init_utils();
7743
7780
  import_jsx_runtime19 = require("react/jsx-runtime");
7744
7781
  phaseMessages = {
7745
- initializing: "Initializing...",
7782
+ initializing: "Preparing viewer...",
7746
7783
  fetching: "Loading document...",
7747
7784
  parsing: "Processing pages...",
7748
- rendering: "Preparing view..."
7785
+ rendering: "Almost ready..."
7786
+ };
7787
+ phaseIcons = {
7788
+ initializing: "\u2699\uFE0F",
7789
+ fetching: "\u{1F4C4}",
7790
+ parsing: "\u{1F4D1}",
7791
+ rendering: "\u2728"
7749
7792
  };
7750
7793
  PDFLoadingScreen = (0, import_react33.memo)(function PDFLoadingScreen2({
7751
7794
  progress,
@@ -7757,70 +7800,102 @@ var init_PDFLoadingScreen = __esm({
7757
7800
  }) {
7758
7801
  const hasProgress = progress !== void 0 && progress >= 0;
7759
7802
  const hasBytes = bytesLoaded !== void 0 && totalBytes !== void 0 && totalBytes > 0;
7760
- return /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)(
7803
+ return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7761
7804
  "div",
7762
7805
  {
7763
7806
  className: cn(
7764
7807
  "pdf-loading-screen",
7765
7808
  "flex flex-col items-center justify-center",
7766
7809
  "w-full h-full min-h-[400px]",
7767
- "bg-slate-50 dark:bg-slate-800",
7810
+ "bg-gradient-to-b from-slate-50 to-slate-100",
7811
+ "dark:from-slate-900 dark:to-slate-800",
7768
7812
  className
7769
7813
  ),
7770
7814
  role: "status",
7771
7815
  "aria-live": "polite",
7772
7816
  "aria-label": phaseMessages[phase],
7773
- children: [
7774
- /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-skeleton", children: [
7817
+ children: /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-card", children: [
7818
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-icon-wrapper", children: [
7819
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("div", { className: "pdf-loading-icon-glow" }),
7775
7820
  /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("div", { className: "pdf-loading-icon", children: /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)(
7776
7821
  "svg",
7777
7822
  {
7778
- width: "48",
7779
- height: "56",
7780
- viewBox: "0 0 48 56",
7823
+ width: "64",
7824
+ height: "80",
7825
+ viewBox: "0 0 64 80",
7781
7826
  fill: "none",
7782
7827
  xmlns: "http://www.w3.org/2000/svg",
7783
7828
  className: "pdf-document-icon",
7784
7829
  children: [
7830
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7831
+ "ellipse",
7832
+ {
7833
+ cx: "32",
7834
+ cy: "76",
7835
+ rx: "24",
7836
+ ry: "4",
7837
+ className: "fill-slate-200 dark:fill-slate-700",
7838
+ opacity: "0.5"
7839
+ }
7840
+ ),
7785
7841
  /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7786
7842
  "path",
7787
7843
  {
7788
- d: "M4 4C4 1.79086 5.79086 0 8 0H30L44 14V52C44 54.2091 42.2091 56 40 56H8C5.79086 56 4 54.2091 4 52V4Z",
7844
+ d: "M8 4C8 1.79086 9.79086 0 12 0H40L56 16V72C56 74.2091 54.2091 76 52 76H12C9.79086 76 8 74.2091 8 72V4Z",
7789
7845
  className: "fill-white dark:fill-slate-700"
7790
7846
  }
7791
7847
  ),
7792
7848
  /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7793
7849
  "path",
7794
7850
  {
7795
- d: "M30 0L44 14H34C31.7909 14 30 12.2091 30 10V0Z",
7796
- className: "fill-slate-200 dark:fill-slate-600"
7851
+ d: "M40 0L56 16H44C41.7909 16 40 14.2091 40 12V0Z",
7852
+ className: "fill-blue-100 dark:fill-blue-900"
7797
7853
  }
7798
7854
  ),
7799
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("rect", { x: "10", y: "22", width: "24", height: "3", rx: "1.5", className: "fill-slate-200 dark:fill-slate-600" }),
7800
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("rect", { x: "10", y: "28", width: "20", height: "3", rx: "1.5", className: "fill-slate-200 dark:fill-slate-600" }),
7801
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("rect", { x: "10", y: "34", width: "22", height: "3", rx: "1.5", className: "fill-slate-200 dark:fill-slate-600" }),
7802
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("rect", { x: "10", y: "40", width: "16", height: "3", rx: "1.5", className: "fill-slate-200 dark:fill-slate-600" }),
7855
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7856
+ "rect",
7857
+ {
7858
+ x: "14",
7859
+ y: "28",
7860
+ width: "28",
7861
+ height: "12",
7862
+ rx: "2",
7863
+ className: "fill-blue-500 dark:fill-blue-400"
7864
+ }
7865
+ ),
7866
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7867
+ "text",
7868
+ {
7869
+ x: "28",
7870
+ y: "37",
7871
+ textAnchor: "middle",
7872
+ className: "fill-white text-[8px] font-bold",
7873
+ style: { fontSize: "8px", fontWeight: 700 },
7874
+ children: "PDF"
7875
+ }
7876
+ ),
7877
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("rect", { x: "14", y: "48", width: "28", height: "3", rx: "1.5", className: "pdf-line-1 fill-slate-200 dark:fill-slate-600" }),
7878
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("rect", { x: "14", y: "54", width: "22", height: "3", rx: "1.5", className: "pdf-line-2 fill-slate-200 dark:fill-slate-600" }),
7879
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("rect", { x: "14", y: "60", width: "18", height: "3", rx: "1.5", className: "pdf-line-3 fill-slate-200 dark:fill-slate-600" }),
7803
7880
  /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7804
7881
  "path",
7805
7882
  {
7806
- d: "M4 4C4 1.79086 5.79086 0 8 0H30L44 14V52C44 54.2091 42.2091 56 40 56H8C5.79086 56 4 54.2091 4 52V4Z",
7807
- className: "stroke-slate-300 dark:stroke-slate-500",
7808
- strokeWidth: "1",
7883
+ d: "M8 4C8 1.79086 9.79086 0 12 0H40L56 16V72C56 74.2091 54.2091 76 52 76H12C9.79086 76 8 74.2091 8 72V4Z",
7884
+ className: "stroke-slate-200 dark:stroke-slate-600",
7885
+ strokeWidth: "1.5",
7809
7886
  fill: "none"
7810
7887
  }
7811
7888
  )
7812
7889
  ]
7813
7890
  }
7814
- ) }),
7815
- /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-skeleton-lines", children: [
7816
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("div", { className: "pdf-skeleton-line pdf-skeleton-line-1" }),
7817
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("div", { className: "pdf-skeleton-line pdf-skeleton-line-2" }),
7818
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("div", { className: "pdf-skeleton-line pdf-skeleton-line-3" })
7819
- ] })
7891
+ ) })
7820
7892
  ] }),
7821
7893
  /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-info", children: [
7822
7894
  documentName && /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("p", { className: "pdf-loading-document-name", children: documentName }),
7823
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("p", { className: "pdf-loading-message", children: phaseMessages[phase] }),
7895
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-message-wrapper", children: [
7896
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("span", { className: "pdf-loading-phase-icon", children: phaseIcons[phase] }),
7897
+ /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("p", { className: "pdf-loading-message", children: phaseMessages[phase] })
7898
+ ] }),
7824
7899
  /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-progress-container", children: [
7825
7900
  /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("div", { className: "pdf-loading-progress-track", children: /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
7826
7901
  "div",
@@ -7832,7 +7907,7 @@ var init_PDFLoadingScreen = __esm({
7832
7907
  style: hasProgress ? { width: `${Math.min(100, progress)}%` } : void 0
7833
7908
  }
7834
7909
  ) }),
7835
- /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-progress-details", children: [
7910
+ (hasProgress || hasBytes) && /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "pdf-loading-progress-details", children: [
7836
7911
  hasProgress && /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("span", { className: "pdf-loading-progress-percent", children: [
7837
7912
  Math.round(progress),
7838
7913
  "%"
@@ -7845,7 +7920,7 @@ var init_PDFLoadingScreen = __esm({
7845
7920
  ] })
7846
7921
  ] })
7847
7922
  ] })
7848
- ]
7923
+ ] })
7849
7924
  }
7850
7925
  );
7851
7926
  });
@@ -10002,18 +10077,36 @@ var init_PDFViewerClient = __esm({
10002
10077
  viewerStore.getState().setError(null);
10003
10078
  setLoadState("idle");
10004
10079
  }, [viewerStore]);
10080
+ const abortControllerRef = (0, import_react41.useRef)(null);
10005
10081
  (0, import_react41.useEffect)(() => {
10006
10082
  mountedRef.current = true;
10007
10083
  return () => {
10008
10084
  mountedRef.current = false;
10085
+ if (abortControllerRef.current) {
10086
+ abortControllerRef.current.abort();
10087
+ abortControllerRef.current = null;
10088
+ }
10089
+ srcIdRef.current = null;
10090
+ const currentDoc = viewerStore.getState().document;
10091
+ if (currentDoc) {
10092
+ currentDoc.destroy();
10093
+ }
10094
+ viewerStore.getState().setDocument(null);
10095
+ viewerStore.getState().setLoading(false);
10096
+ viewerStore.getState().setError(null);
10009
10097
  };
10010
- }, []);
10098
+ }, [viewerStore]);
10011
10099
  (0, import_react41.useEffect)(() => {
10012
10100
  if (srcIdRef.current === srcId && viewerStore.getState().document) {
10013
10101
  return;
10014
10102
  }
10015
10103
  const loadId = srcId;
10016
10104
  srcIdRef.current = srcId;
10105
+ if (abortControllerRef.current) {
10106
+ abortControllerRef.current.abort();
10107
+ }
10108
+ const abortController = new AbortController();
10109
+ abortControllerRef.current = abortController;
10017
10110
  const currentDoc = viewerStore.getState().document;
10018
10111
  if (currentDoc) {
10019
10112
  currentDoc.destroy();
@@ -10023,14 +10116,15 @@ var init_PDFViewerClient = __esm({
10023
10116
  viewerStore.getState().setError(null);
10024
10117
  setLoadState("loading");
10025
10118
  const loadDoc = async () => {
10026
- if (!mountedRef.current) return;
10119
+ if (!mountedRef.current || abortController.signal.aborted) return;
10027
10120
  try {
10028
10121
  viewerStore.getState().setLoadingProgress({ phase: "fetching" });
10029
10122
  const { document: document2, numPages } = await loadDocument({
10030
10123
  src,
10031
10124
  workerSrc,
10125
+ signal: abortController.signal,
10032
10126
  onProgress: ({ loaded, total }) => {
10033
- if (mountedRef.current && srcIdRef.current === loadId) {
10127
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10034
10128
  const percent = total > 0 ? Math.round(loaded / total * 100) : void 0;
10035
10129
  viewerStore.getState().setLoadingProgress({
10036
10130
  phase: "fetching",
@@ -10041,10 +10135,10 @@ var init_PDFViewerClient = __esm({
10041
10135
  }
10042
10136
  }
10043
10137
  });
10044
- if (mountedRef.current && srcIdRef.current === loadId) {
10138
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10045
10139
  viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
10046
10140
  }
10047
- if (mountedRef.current && srcIdRef.current === loadId) {
10141
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10048
10142
  viewerStore.getState().setDocument(document2);
10049
10143
  setLoadState("loaded");
10050
10144
  if (initialPage !== 1) {
@@ -10062,6 +10156,13 @@ var init_PDFViewerClient = __esm({
10062
10156
  document2.destroy();
10063
10157
  }
10064
10158
  } catch (err) {
10159
+ if (err instanceof DOMException && err.name === "AbortError") {
10160
+ return;
10161
+ }
10162
+ const errorMessage = err instanceof Error ? err.message : String(err);
10163
+ if (abortController.signal.aborted || errorMessage.includes("network error") || errorMessage.includes("aborted")) {
10164
+ return;
10165
+ }
10065
10166
  if (mountedRef.current && srcIdRef.current === loadId) {
10066
10167
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
10067
10168
  viewerStore.getState().setError(error2);
@@ -10073,6 +10174,7 @@ var init_PDFViewerClient = __esm({
10073
10174
  };
10074
10175
  loadDoc();
10075
10176
  return () => {
10177
+ abortController.abort();
10076
10178
  };
10077
10179
  }, [srcId, src, workerSrc, initialPage, initialScale, viewerStore]);
10078
10180
  const prevPageRef = (0, import_react41.useRef)(currentPage);