@anaralabs/lector 3.12.5 → 3.12.7

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.
@@ -326,6 +326,8 @@ declare interface PDFState {
326
326
  zoomFitWidth: () => void;
327
327
  isPinching: boolean;
328
328
  setIsPinching: (isPinching: boolean) => void;
329
+ isResizing: boolean;
330
+ setIsResizing: (isResizing: boolean) => void;
329
331
  currentPage: number;
330
332
  setCurrentPage: (pageNumber: number) => void;
331
333
  viewports: Array<PageViewport>;
package/dist/index.js CHANGED
@@ -86,6 +86,12 @@ var PDFStore = createZustandContext(
86
86
  isPinching: val
87
87
  });
88
88
  },
89
+ isResizing: false,
90
+ setIsResizing: (val) => {
91
+ set({
92
+ isResizing: val
93
+ });
94
+ },
89
95
  virtualizer: null,
90
96
  setVirtualizer: (val) => {
91
97
  set({
@@ -993,7 +999,12 @@ var defaultAnnotationLayerParams = {
993
999
  var useAnnotationLayer = (params) => {
994
1000
  const mergedParams = useMemo(() => {
995
1001
  return { ...defaultAnnotationLayerParams, ...params };
996
- }, [params]);
1002
+ }, [
1003
+ params.renderForms,
1004
+ params.externalLinksEnabled,
1005
+ params.jumpOptions?.behavior,
1006
+ params.jumpOptions?.align
1007
+ ]);
997
1008
  const annotationLayerRef = useRef(null);
998
1009
  const annotationLayerObjectRef = useRef(null);
999
1010
  const linkService = usePDFLinkService();
@@ -1244,9 +1255,11 @@ function setCachedBitmap(docId, proxy, scale, background, bitmap) {
1244
1255
  }
1245
1256
  var useCanvasLayer = ({ background }) => {
1246
1257
  const canvasRef = useRef(null);
1258
+ const hasContentRef = useRef(false);
1247
1259
  const pageNumber = usePDFPageNumber();
1248
1260
  const dpr = useDpr();
1249
1261
  const bouncyZoom = usePdf((state) => state.zoom);
1262
+ const isResizing = usePdf((state) => state.isResizing);
1250
1263
  const docId = usePdf((state) => state.pdfDocumentProxy.fingerprints[0] ?? "");
1251
1264
  const pdfPageProxy = usePdf((state) => state.getPdfPageProxy(pageNumber));
1252
1265
  const markPageRendered = usePdf((state) => state.markPageRendered);
@@ -1258,6 +1271,13 @@ var useCanvasLayer = ({ background }) => {
1258
1271
  const baseViewport = pdfPageProxy.getViewport({ scale: 1 });
1259
1272
  const pageWidth = baseViewport.width;
1260
1273
  const pageHeight = baseViewport.height;
1274
+ if (isResizing && hasContentRef.current) {
1275
+ baseCanvas.style.visibility = "";
1276
+ baseCanvas.style.width = `${pageWidth}px`;
1277
+ baseCanvas.style.height = `${pageHeight}px`;
1278
+ return;
1279
+ }
1280
+ hasContentRef.current = false;
1261
1281
  const targetBaseScale = dpr * Math.min(zoom, 1);
1262
1282
  const baseScale = clampScaleForPage(targetBaseScale, pageWidth, pageHeight);
1263
1283
  baseCanvas.width = Math.floor(pageWidth * baseScale);
@@ -1278,6 +1298,7 @@ var useCanvasLayer = ({ background }) => {
1278
1298
  if (cached) {
1279
1299
  context.drawImage(cached, 0, 0);
1280
1300
  markPageRendered(pageNumber);
1301
+ hasContentRef.current = true;
1281
1302
  return;
1282
1303
  }
1283
1304
  context.setTransform(1, 0, 0, 1, 0, 0);
@@ -1313,6 +1334,7 @@ var useCanvasLayer = ({ background }) => {
1313
1334
  }
1314
1335
  baseCanvas.style.visibility = "";
1315
1336
  markPageRendered(pageNumber);
1337
+ hasContentRef.current = true;
1316
1338
  if (typeof createImageBitmap !== "undefined") {
1317
1339
  createImageBitmap(renderCanvas).then((bitmap) => {
1318
1340
  if (cancelled) {
@@ -1347,6 +1369,7 @@ var useCanvasLayer = ({ background }) => {
1347
1369
  background,
1348
1370
  dpr,
1349
1371
  zoom,
1372
+ isResizing,
1350
1373
  pageNumber,
1351
1374
  markPageRendered,
1352
1375
  docId
@@ -2987,32 +3010,57 @@ var TotalPages = ({ ...props }) => {
2987
3010
  const pages = usePdf((state) => state.pdfDocumentProxy.numPages);
2988
3011
  return /* @__PURE__ */ jsx("div", { ...props, children: pages });
2989
3012
  };
3013
+ var RESIZE_QUIET_MS = 200;
2990
3014
  var useFitWidth = ({ viewportRef }) => {
2991
3015
  const viewports = usePdf((state) => state.viewports);
2992
3016
  const zoomOptions = usePdf((state) => state.zoomOptions);
2993
3017
  const updateZoom = usePdf((state) => state.updateZoom);
3018
+ const setIsResizing = usePdf((state) => state.setIsResizing);
2994
3019
  const store = PDFStore.useContext();
3020
+ const pendingFrameRef = useRef(null);
3021
+ const latestWidthRef = useRef(null);
2995
3022
  useLayoutEffect(() => {
2996
3023
  if (viewportRef.current === null) return;
3024
+ let quietTimer = null;
2997
3025
  const resizeObserver = new ResizeObserver((entries) => {
2998
3026
  for (const entry of entries) {
2999
- const isFitWidth = store.getState().isZoomFitWidth;
3000
- if (entry.target === viewportRef.current && isFitWidth) {
3001
- const containerWidth = entry.contentRect.width;
3027
+ if (entry.target !== viewportRef.current) continue;
3028
+ const prevWidth = latestWidthRef.current;
3029
+ latestWidthRef.current = entry.contentRect.width;
3030
+ if (store.getState().isZoomFitWidth && prevWidth !== null && prevWidth !== entry.contentRect.width) {
3031
+ if (!store.getState().isResizing) setIsResizing(true);
3032
+ if (quietTimer) clearTimeout(quietTimer);
3033
+ quietTimer = setTimeout(() => {
3034
+ setIsResizing(false);
3035
+ quietTimer = null;
3036
+ }, RESIZE_QUIET_MS);
3037
+ }
3038
+ if (pendingFrameRef.current !== null) continue;
3039
+ pendingFrameRef.current = requestAnimationFrame(() => {
3040
+ pendingFrameRef.current = null;
3041
+ const containerWidth = latestWidthRef.current;
3042
+ if (containerWidth === null) return;
3043
+ if (!store.getState().isZoomFitWidth) return;
3002
3044
  const newZoom = getFitWidthZoom(
3003
3045
  containerWidth,
3004
3046
  viewports,
3005
3047
  zoomOptions
3006
3048
  );
3007
3049
  updateZoom(newZoom, true);
3008
- }
3050
+ });
3009
3051
  }
3010
3052
  });
3011
3053
  resizeObserver.observe(viewportRef.current);
3012
3054
  return () => {
3055
+ if (quietTimer) clearTimeout(quietTimer);
3013
3056
  resizeObserver.disconnect();
3057
+ if (pendingFrameRef.current !== null) {
3058
+ cancelAnimationFrame(pendingFrameRef.current);
3059
+ pendingFrameRef.current = null;
3060
+ }
3061
+ if (store.getState().isResizing) setIsResizing(false);
3014
3062
  };
3015
- }, [store, updateZoom, viewportRef, viewports, zoomOptions]);
3063
+ }, [store, updateZoom, setIsResizing, viewportRef, viewports, zoomOptions]);
3016
3064
  return null;
3017
3065
  };
3018
3066
  var supportsScrollend = typeof window === "undefined" ? true : "onscrollend" in window;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anaralabs/lector",
3
- "version": "3.12.5",
3
+ "version": "3.12.7",
4
4
  "description": "A headless PDF viewer for React",
5
5
  "author": "andrewdr (https://github.com/andrewdoro)",
6
6
  "license": "MIT",