@anaralabs/lector 3.12.5 → 3.12.6

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({
@@ -1244,9 +1250,11 @@ function setCachedBitmap(docId, proxy, scale, background, bitmap) {
1244
1250
  }
1245
1251
  var useCanvasLayer = ({ background }) => {
1246
1252
  const canvasRef = useRef(null);
1253
+ const hasContentRef = useRef(false);
1247
1254
  const pageNumber = usePDFPageNumber();
1248
1255
  const dpr = useDpr();
1249
1256
  const bouncyZoom = usePdf((state) => state.zoom);
1257
+ const isResizing = usePdf((state) => state.isResizing);
1250
1258
  const docId = usePdf((state) => state.pdfDocumentProxy.fingerprints[0] ?? "");
1251
1259
  const pdfPageProxy = usePdf((state) => state.getPdfPageProxy(pageNumber));
1252
1260
  const markPageRendered = usePdf((state) => state.markPageRendered);
@@ -1258,6 +1266,13 @@ var useCanvasLayer = ({ background }) => {
1258
1266
  const baseViewport = pdfPageProxy.getViewport({ scale: 1 });
1259
1267
  const pageWidth = baseViewport.width;
1260
1268
  const pageHeight = baseViewport.height;
1269
+ if (isResizing && hasContentRef.current) {
1270
+ baseCanvas.style.visibility = "";
1271
+ baseCanvas.style.width = `${pageWidth}px`;
1272
+ baseCanvas.style.height = `${pageHeight}px`;
1273
+ return;
1274
+ }
1275
+ hasContentRef.current = false;
1261
1276
  const targetBaseScale = dpr * Math.min(zoom, 1);
1262
1277
  const baseScale = clampScaleForPage(targetBaseScale, pageWidth, pageHeight);
1263
1278
  baseCanvas.width = Math.floor(pageWidth * baseScale);
@@ -1278,6 +1293,7 @@ var useCanvasLayer = ({ background }) => {
1278
1293
  if (cached) {
1279
1294
  context.drawImage(cached, 0, 0);
1280
1295
  markPageRendered(pageNumber);
1296
+ hasContentRef.current = true;
1281
1297
  return;
1282
1298
  }
1283
1299
  context.setTransform(1, 0, 0, 1, 0, 0);
@@ -1313,6 +1329,7 @@ var useCanvasLayer = ({ background }) => {
1313
1329
  }
1314
1330
  baseCanvas.style.visibility = "";
1315
1331
  markPageRendered(pageNumber);
1332
+ hasContentRef.current = true;
1316
1333
  if (typeof createImageBitmap !== "undefined") {
1317
1334
  createImageBitmap(renderCanvas).then((bitmap) => {
1318
1335
  if (cancelled) {
@@ -1347,6 +1364,7 @@ var useCanvasLayer = ({ background }) => {
1347
1364
  background,
1348
1365
  dpr,
1349
1366
  zoom,
1367
+ isResizing,
1350
1368
  pageNumber,
1351
1369
  markPageRendered,
1352
1370
  docId
@@ -2987,32 +3005,57 @@ var TotalPages = ({ ...props }) => {
2987
3005
  const pages = usePdf((state) => state.pdfDocumentProxy.numPages);
2988
3006
  return /* @__PURE__ */ jsx("div", { ...props, children: pages });
2989
3007
  };
3008
+ var RESIZE_QUIET_MS = 200;
2990
3009
  var useFitWidth = ({ viewportRef }) => {
2991
3010
  const viewports = usePdf((state) => state.viewports);
2992
3011
  const zoomOptions = usePdf((state) => state.zoomOptions);
2993
3012
  const updateZoom = usePdf((state) => state.updateZoom);
3013
+ const setIsResizing = usePdf((state) => state.setIsResizing);
2994
3014
  const store = PDFStore.useContext();
3015
+ const pendingFrameRef = useRef(null);
3016
+ const latestWidthRef = useRef(null);
2995
3017
  useLayoutEffect(() => {
2996
3018
  if (viewportRef.current === null) return;
3019
+ let quietTimer = null;
2997
3020
  const resizeObserver = new ResizeObserver((entries) => {
2998
3021
  for (const entry of entries) {
2999
- const isFitWidth = store.getState().isZoomFitWidth;
3000
- if (entry.target === viewportRef.current && isFitWidth) {
3001
- const containerWidth = entry.contentRect.width;
3022
+ if (entry.target !== viewportRef.current) continue;
3023
+ const prevWidth = latestWidthRef.current;
3024
+ latestWidthRef.current = entry.contentRect.width;
3025
+ if (store.getState().isZoomFitWidth && prevWidth !== null && prevWidth !== entry.contentRect.width) {
3026
+ if (!store.getState().isResizing) setIsResizing(true);
3027
+ if (quietTimer) clearTimeout(quietTimer);
3028
+ quietTimer = setTimeout(() => {
3029
+ setIsResizing(false);
3030
+ quietTimer = null;
3031
+ }, RESIZE_QUIET_MS);
3032
+ }
3033
+ if (pendingFrameRef.current !== null) continue;
3034
+ pendingFrameRef.current = requestAnimationFrame(() => {
3035
+ pendingFrameRef.current = null;
3036
+ const containerWidth = latestWidthRef.current;
3037
+ if (containerWidth === null) return;
3038
+ if (!store.getState().isZoomFitWidth) return;
3002
3039
  const newZoom = getFitWidthZoom(
3003
3040
  containerWidth,
3004
3041
  viewports,
3005
3042
  zoomOptions
3006
3043
  );
3007
3044
  updateZoom(newZoom, true);
3008
- }
3045
+ });
3009
3046
  }
3010
3047
  });
3011
3048
  resizeObserver.observe(viewportRef.current);
3012
3049
  return () => {
3050
+ if (quietTimer) clearTimeout(quietTimer);
3013
3051
  resizeObserver.disconnect();
3052
+ if (pendingFrameRef.current !== null) {
3053
+ cancelAnimationFrame(pendingFrameRef.current);
3054
+ pendingFrameRef.current = null;
3055
+ }
3056
+ if (store.getState().isResizing) setIsResizing(false);
3014
3057
  };
3015
- }, [store, updateZoom, viewportRef, viewports, zoomOptions]);
3058
+ }, [store, updateZoom, setIsResizing, viewportRef, viewports, zoomOptions]);
3016
3059
  return null;
3017
3060
  };
3018
3061
  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.6",
4
4
  "description": "A headless PDF viewer for React",
5
5
  "author": "andrewdr (https://github.com/andrewdoro)",
6
6
  "license": "MIT",