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.d.cts CHANGED
@@ -327,7 +327,7 @@ interface ViewerState {
327
327
  isPresentationMode: boolean;
328
328
  }
329
329
  interface ViewerActions {
330
- setDocument: (doc: PDFDocumentProxy) => void;
330
+ setDocument: (doc: PDFDocumentProxy | null) => void;
331
331
  setLoading: (loading: boolean) => void;
332
332
  setError: (error: Error | null) => void;
333
333
  setCurrentPage: (page: number) => void;
package/dist/index.d.ts CHANGED
@@ -327,7 +327,7 @@ interface ViewerState {
327
327
  isPresentationMode: boolean;
328
328
  }
329
329
  interface ViewerActions {
330
- setDocument: (doc: PDFDocumentProxy) => void;
330
+ setDocument: (doc: PDFDocumentProxy | null) => void;
331
331
  setLoading: (loading: boolean) => void;
332
332
  setError: (error: Error | null) => void;
333
333
  setCurrentPage: (page: number) => void;
package/dist/index.js CHANGED
@@ -316,13 +316,21 @@ function createViewerStore(initialOverrides = {}) {
316
316
  ...initialOverrides,
317
317
  // Document actions
318
318
  setDocument: (document2) => {
319
- set({
320
- document: document2,
321
- numPages: document2.numPages,
322
- isLoading: false,
323
- error: null,
324
- currentPage: 1
325
- });
319
+ if (document2) {
320
+ set({
321
+ document: document2,
322
+ numPages: document2.numPages,
323
+ isLoading: false,
324
+ error: null,
325
+ currentPage: 1
326
+ });
327
+ } else {
328
+ set({
329
+ document: null,
330
+ numPages: 0,
331
+ isLoading: false
332
+ });
333
+ }
326
334
  },
327
335
  setLoading: (isLoading) => {
328
336
  set({ isLoading });
@@ -8740,8 +8748,19 @@ var PDFViewerClient_exports = {};
8740
8748
  __export(PDFViewerClient_exports, {
8741
8749
  PDFViewerClient: () => PDFViewerClient
8742
8750
  });
8743
- import { useEffect as useEffect22, useCallback as useCallback32, memo as memo24, useRef as useRef19 } from "react";
8751
+ import { useEffect as useEffect22, useCallback as useCallback32, memo as memo24, useRef as useRef19, useState as useState21 } from "react";
8744
8752
  import { jsx as jsx25, jsxs as jsxs21 } from "react/jsx-runtime";
8753
+ function getSrcIdentifier(src) {
8754
+ if (typeof src === "string") {
8755
+ return src;
8756
+ }
8757
+ const data = src instanceof ArrayBuffer ? new Uint8Array(src) : src;
8758
+ const len = data.byteLength;
8759
+ if (len === 0) return "empty";
8760
+ const first = Array.from(data.slice(0, 4)).map((b) => b.toString(16).padStart(2, "0")).join("");
8761
+ const last = Array.from(data.slice(-4)).map((b) => b.toString(16).padStart(2, "0")).join("");
8762
+ return `binary:${len}:${first}:${last}`;
8763
+ }
8745
8764
  var PDFViewerInner, PDFViewerClient;
8746
8765
  var init_PDFViewerClient = __esm({
8747
8766
  "src/components/PDFViewer/PDFViewerClient.tsx"() {
@@ -8771,68 +8790,97 @@ var init_PDFViewerClient = __esm({
8771
8790
  className
8772
8791
  }) {
8773
8792
  const { viewerStore } = usePDFViewerStores();
8774
- const loadingRef = useRef19(false);
8775
- const srcRef = useRef19(src);
8793
+ const mountedRef = useRef19(true);
8794
+ const [, setLoadState] = useState21("idle");
8795
+ const onDocumentLoadRef = useRef19(onDocumentLoad);
8796
+ const onErrorRef = useRef19(onError);
8797
+ const onPageChangeRef = useRef19(onPageChange);
8798
+ const onScaleChangeRef = useRef19(onScaleChange);
8799
+ onDocumentLoadRef.current = onDocumentLoad;
8800
+ onErrorRef.current = onError;
8801
+ onPageChangeRef.current = onPageChange;
8802
+ onScaleChangeRef.current = onScaleChange;
8803
+ const srcIdRef = useRef19(null);
8776
8804
  const currentPage = useViewerStore((s) => s.currentPage);
8777
8805
  const scale = useViewerStore((s) => s.scale);
8778
8806
  const theme = useViewerStore((s) => s.theme);
8779
8807
  const isLoading = useViewerStore((s) => s.isLoading);
8780
8808
  const error = useViewerStore((s) => s.error);
8781
8809
  const sidebarOpen = useViewerStore((s) => s.sidebarOpen);
8782
- const loadDoc = useCallback32(async () => {
8783
- if (loadingRef.current) return;
8784
- loadingRef.current = true;
8785
- try {
8786
- viewerStore.getState().setLoading(true);
8787
- viewerStore.getState().setError(null);
8788
- const { document: document2, numPages } = await loadDocument({
8789
- src,
8790
- workerSrc
8791
- });
8792
- if (srcRef.current === src) {
8793
- viewerStore.getState().setDocument(document2);
8794
- if (initialPage !== 1) {
8795
- viewerStore.getState().goToPage(initialPage);
8796
- }
8797
- if (typeof initialScale === "number" && initialScale !== 1) {
8798
- viewerStore.getState().setScale(initialScale);
8799
- }
8800
- onDocumentLoad?.({ document: document2, numPages });
8801
- } else {
8802
- document2.destroy();
8803
- }
8804
- } catch (err) {
8805
- if (srcRef.current === src) {
8806
- const error2 = err instanceof Error ? err : new Error("Failed to load document");
8807
- viewerStore.getState().setError(error2);
8808
- onError?.(error2);
8809
- }
8810
- } finally {
8811
- loadingRef.current = false;
8812
- }
8813
- }, [src, workerSrc, initialPage, initialScale, onDocumentLoad, onError, viewerStore]);
8810
+ const srcId = getSrcIdentifier(src);
8811
+ const handleRetry = useCallback32(() => {
8812
+ srcIdRef.current = null;
8813
+ viewerStore.getState().setError(null);
8814
+ setLoadState("idle");
8815
+ }, [viewerStore]);
8814
8816
  useEffect22(() => {
8815
- srcRef.current = src;
8817
+ mountedRef.current = true;
8818
+ return () => {
8819
+ mountedRef.current = false;
8820
+ };
8821
+ }, []);
8822
+ useEffect22(() => {
8823
+ if (srcIdRef.current === srcId && viewerStore.getState().document) {
8824
+ return;
8825
+ }
8826
+ const loadId = srcId;
8827
+ srcIdRef.current = srcId;
8816
8828
  const currentDoc = viewerStore.getState().document;
8817
8829
  if (currentDoc) {
8818
- viewerStore.getState().reset();
8830
+ currentDoc.destroy();
8831
+ viewerStore.getState().setDocument(null);
8819
8832
  }
8820
- loadDoc();
8821
- return () => {
8822
- srcRef.current = null;
8833
+ const loadDoc = async () => {
8834
+ if (!mountedRef.current) return;
8835
+ try {
8836
+ viewerStore.getState().setLoading(true);
8837
+ viewerStore.getState().setError(null);
8838
+ setLoadState("loading");
8839
+ const { document: document2, numPages } = await loadDocument({
8840
+ src,
8841
+ workerSrc
8842
+ });
8843
+ if (mountedRef.current && srcIdRef.current === loadId) {
8844
+ viewerStore.getState().setDocument(document2);
8845
+ setLoadState("loaded");
8846
+ if (initialPage !== 1) {
8847
+ viewerStore.getState().goToPage(initialPage);
8848
+ }
8849
+ if (typeof initialScale === "number" && initialScale !== 1) {
8850
+ viewerStore.getState().setScale(initialScale);
8851
+ }
8852
+ onDocumentLoadRef.current?.({ document: document2, numPages });
8853
+ } else {
8854
+ document2.destroy();
8855
+ }
8856
+ } catch (err) {
8857
+ if (mountedRef.current && srcIdRef.current === loadId) {
8858
+ const error2 = err instanceof Error ? err : new Error("Failed to load document");
8859
+ viewerStore.getState().setError(error2);
8860
+ viewerStore.getState().setLoading(false);
8861
+ setLoadState("error");
8862
+ onErrorRef.current?.(error2);
8863
+ }
8864
+ }
8823
8865
  };
8824
- }, [src, loadDoc, viewerStore]);
8825
- useEffect22(() => {
8866
+ loadDoc();
8826
8867
  return () => {
8827
- viewerStore.getState().reset();
8828
8868
  };
8829
- }, [viewerStore]);
8869
+ }, [srcId, src, workerSrc, initialPage, initialScale, viewerStore]);
8870
+ const prevPageRef = useRef19(currentPage);
8830
8871
  useEffect22(() => {
8831
- onPageChange?.(currentPage);
8832
- }, [currentPage, onPageChange]);
8872
+ if (prevPageRef.current !== currentPage) {
8873
+ prevPageRef.current = currentPage;
8874
+ onPageChangeRef.current?.(currentPage);
8875
+ }
8876
+ }, [currentPage]);
8877
+ const prevScaleRef = useRef19(scale);
8833
8878
  useEffect22(() => {
8834
- onScaleChange?.(scale);
8835
- }, [scale, onScaleChange]);
8879
+ if (prevScaleRef.current !== scale) {
8880
+ prevScaleRef.current = scale;
8881
+ onScaleChangeRef.current?.(scale);
8882
+ }
8883
+ }, [scale]);
8836
8884
  const themeClass = theme === "dark" ? "dark" : "";
8837
8885
  if (error) {
8838
8886
  return /* @__PURE__ */ jsx25(
@@ -8851,7 +8899,7 @@ var init_PDFViewerClient = __esm({
8851
8899
  /* @__PURE__ */ jsx25(
8852
8900
  "button",
8853
8901
  {
8854
- onClick: loadDoc,
8902
+ onClick: handleRetry,
8855
8903
  className: "mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600",
8856
8904
  children: "Retry"
8857
8905
  }
@@ -8973,7 +9021,7 @@ init_AnnotationToolbar2();
8973
9021
 
8974
9022
  // src/components/Annotations/StickyNote.tsx
8975
9023
  init_utils();
8976
- import { memo as memo26, useState as useState21, useRef as useRef20, useEffect as useEffect23, useCallback as useCallback33 } from "react";
9024
+ import { memo as memo26, useState as useState22, useRef as useRef20, useEffect as useEffect23, useCallback as useCallback33 } from "react";
8977
9025
  import { jsx as jsx27, jsxs as jsxs23 } from "react/jsx-runtime";
8978
9026
  var NOTE_COLORS = [
8979
9027
  "#fef08a",
@@ -9000,8 +9048,8 @@ var StickyNote = memo26(function StickyNote2({
9000
9048
  onDragStart,
9001
9049
  className
9002
9050
  }) {
9003
- const [isExpanded, setIsExpanded] = useState21(false);
9004
- const [localContent, setLocalContent] = useState21(note.content);
9051
+ const [isExpanded, setIsExpanded] = useState22(false);
9052
+ const [localContent, setLocalContent] = useState22(note.content);
9005
9053
  const textareaRef = useRef20(null);
9006
9054
  const noteRef = useRef20(null);
9007
9055
  useEffect23(() => {
@@ -9190,7 +9238,7 @@ var StickyNote = memo26(function StickyNote2({
9190
9238
 
9191
9239
  // src/components/Annotations/DrawingCanvas.tsx
9192
9240
  init_utils();
9193
- import { memo as memo27, useRef as useRef21, useCallback as useCallback34, useState as useState22 } from "react";
9241
+ import { memo as memo27, useRef as useRef21, useCallback as useCallback34, useState as useState23 } from "react";
9194
9242
  import { jsx as jsx28 } from "react/jsx-runtime";
9195
9243
  function pointsToSvgPath(points) {
9196
9244
  if (points.length === 0) return "";
@@ -9240,8 +9288,8 @@ var DrawingCanvas = memo27(function DrawingCanvas2({
9240
9288
  className
9241
9289
  }) {
9242
9290
  const svgRef = useRef21(null);
9243
- const [isDrawing, setIsDrawing] = useState22(false);
9244
- const [currentPath, setCurrentPath] = useState22([]);
9291
+ const [isDrawing, setIsDrawing] = useState23(false);
9292
+ const [currentPath, setCurrentPath] = useState23([]);
9245
9293
  const getPoint = useCallback34((e) => {
9246
9294
  if (!svgRef.current) return null;
9247
9295
  const svg = svgRef.current;
@@ -9324,7 +9372,7 @@ var DrawingCanvas = memo27(function DrawingCanvas2({
9324
9372
 
9325
9373
  // src/components/Annotations/ShapeRenderer.tsx
9326
9374
  init_utils();
9327
- import { memo as memo28, useCallback as useCallback35, useState as useState23, useRef as useRef22 } from "react";
9375
+ import { memo as memo28, useCallback as useCallback35, useState as useState24, useRef as useRef22 } from "react";
9328
9376
  import { jsx as jsx29, jsxs as jsxs24 } from "react/jsx-runtime";
9329
9377
  var ShapeRenderer = memo28(function ShapeRenderer2({
9330
9378
  shape,
@@ -9336,9 +9384,9 @@ var ShapeRenderer = memo28(function ShapeRenderer2({
9336
9384
  onDelete: _onDelete,
9337
9385
  className
9338
9386
  }) {
9339
- const [_isDragging, setIsDragging] = useState23(false);
9340
- const [_isResizing, setIsResizing] = useState23(false);
9341
- const [activeHandle, setActiveHandle] = useState23(null);
9387
+ const [_isDragging, setIsDragging] = useState24(false);
9388
+ const [_isResizing, setIsResizing] = useState24(false);
9389
+ const [activeHandle, setActiveHandle] = useState24(null);
9342
9390
  const startPosRef = useRef22({ x: 0, y: 0 });
9343
9391
  const startShapeRef = useRef22({ x: 0, y: 0, width: 0, height: 0 });
9344
9392
  const { shapeType, x, y, width, height, color, strokeWidth, id: _id } = shape;
@@ -9629,7 +9677,7 @@ var ShapePreview = memo28(function ShapePreview2({
9629
9677
 
9630
9678
  // src/components/Annotations/QuickNoteButton.tsx
9631
9679
  init_utils();
9632
- import { memo as memo29, useCallback as useCallback36, useState as useState24 } from "react";
9680
+ import { memo as memo29, useCallback as useCallback36, useState as useState25 } from "react";
9633
9681
  import { jsx as jsx30 } from "react/jsx-runtime";
9634
9682
  var QuickNoteButton = memo29(function QuickNoteButton2({
9635
9683
  pageNumber,
@@ -9639,7 +9687,7 @@ var QuickNoteButton = memo29(function QuickNoteButton2({
9639
9687
  className,
9640
9688
  visible = true
9641
9689
  }) {
9642
- const [isHovered, setIsHovered] = useState24(false);
9690
+ const [isHovered, setIsHovered] = useState25(false);
9643
9691
  const handleClick = useCallback36(
9644
9692
  (e) => {
9645
9693
  e.stopPropagation();
@@ -9691,7 +9739,7 @@ var QuickNoteButton = memo29(function QuickNoteButton2({
9691
9739
 
9692
9740
  // src/components/Annotations/QuickNotePopover.tsx
9693
9741
  init_utils();
9694
- import { memo as memo30, useCallback as useCallback37, useState as useState25, useRef as useRef23, useEffect as useEffect24 } from "react";
9742
+ import { memo as memo30, useCallback as useCallback37, useState as useState26, useRef as useRef23, useEffect as useEffect24 } from "react";
9695
9743
  import { jsx as jsx31, jsxs as jsxs25 } from "react/jsx-runtime";
9696
9744
  var QuickNotePopover = memo30(function QuickNotePopover2({
9697
9745
  visible,
@@ -9702,10 +9750,10 @@ var QuickNotePopover = memo30(function QuickNotePopover2({
9702
9750
  onCancel,
9703
9751
  className
9704
9752
  }) {
9705
- const [content, setContent] = useState25(initialContent);
9753
+ const [content, setContent] = useState26(initialContent);
9706
9754
  const textareaRef = useRef23(null);
9707
9755
  const popoverRef = useRef23(null);
9708
- const [adjustedPosition, setAdjustedPosition] = useState25(position);
9756
+ const [adjustedPosition, setAdjustedPosition] = useState26(position);
9709
9757
  useEffect24(() => {
9710
9758
  if (visible && textareaRef.current) {
9711
9759
  textareaRef.current.focus();
@@ -9957,7 +10005,7 @@ var AskAboutOverlay = memo31(function AskAboutOverlay2({
9957
10005
 
9958
10006
  // src/components/AskAbout/AskAboutTrigger.tsx
9959
10007
  init_utils();
9960
- import { memo as memo32, useCallback as useCallback38, useState as useState26, useRef as useRef24, useEffect as useEffect25 } from "react";
10008
+ import { memo as memo32, useCallback as useCallback38, useState as useState27, useRef as useRef24, useEffect as useEffect25 } from "react";
9961
10009
  import { jsx as jsx33, jsxs as jsxs27 } from "react/jsx-runtime";
9962
10010
  var AskAboutTrigger = memo32(function AskAboutTrigger2({
9963
10011
  position,
@@ -9967,7 +10015,7 @@ var AskAboutTrigger = memo32(function AskAboutTrigger2({
9967
10015
  autoHideDelay = 5e3,
9968
10016
  className
9969
10017
  }) {
9970
- const [adjustedPosition, setAdjustedPosition] = useState26(position);
10018
+ const [adjustedPosition, setAdjustedPosition] = useState27(position);
9971
10019
  const triggerRef = useRef24(null);
9972
10020
  useEffect25(() => {
9973
10021
  if (!visible || !triggerRef.current) return;