@anaralabs/lector 3.10.1 → 3.10.2

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.
Files changed (2) hide show
  1. package/dist/index.js +55 -22
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1143,7 +1143,7 @@ var RenderCache = class {
1143
1143
  // Tracks invalidated documents so in-flight createImageBitmap calls
1144
1144
  // don't insert orphaned entries after invalidateDocument() runs.
1145
1145
  invalidatedDocs = /* @__PURE__ */ new Set();
1146
- constructor(maxEntries = 30) {
1146
+ constructor(maxEntries = 60) {
1147
1147
  this.maxEntries = maxEntries;
1148
1148
  }
1149
1149
  key(documentId, pageNumber, scale, background) {
@@ -1292,6 +1292,15 @@ var useCanvasLayer = ({ background }) => {
1292
1292
  const lastRenderedScaleRef = useRef(0);
1293
1293
  const lastBackgroundRef = useRef(background);
1294
1294
  const lastPageProxyRef = useRef(pdfPageProxy);
1295
+ const pageDimsRef = useRef(null);
1296
+ if (!pageDimsRef.current || pageDimsRef.current.proxy !== pdfPageProxy) {
1297
+ const vp = pdfPageProxy.getViewport({ scale: 1 });
1298
+ pageDimsRef.current = {
1299
+ width: vp.width,
1300
+ height: vp.height,
1301
+ proxy: pdfPageProxy
1302
+ };
1303
+ }
1295
1304
  useLayoutEffect(() => {
1296
1305
  if (!canvasRef.current) {
1297
1306
  return;
@@ -1301,9 +1310,7 @@ var useCanvasLayer = ({ background }) => {
1301
1310
  lastBackgroundRef.current = background;
1302
1311
  lastRenderedScaleRef.current = 0;
1303
1312
  }
1304
- const baseViewport = pdfPageProxy.getViewport({ scale: 1 });
1305
- const pageWidth = baseViewport.width;
1306
- const pageHeight = baseViewport.height;
1313
+ const { width: pageWidth, height: pageHeight } = pageDimsRef.current;
1307
1314
  const bgColor = background ?? "white";
1308
1315
  const targetBaseScale = dpr * Math.min(zoom, 1);
1309
1316
  const baseScale = clampScaleForPage(targetBaseScale, pageWidth, pageHeight);
@@ -1490,6 +1497,15 @@ var useDetailCanvasLayer = ({
1490
1497
  const pdfPageProxy = usePdf((state) => state.getPdfPageProxy(pageNumber));
1491
1498
  const viewportRef = usePdf((state) => state.viewportRef);
1492
1499
  const [zoom] = useDebounce(bouncyZoom, 200);
1500
+ const pageDimsRef = useRef(null);
1501
+ if (!pageDimsRef.current || pageDimsRef.current.proxy !== pdfPageProxy) {
1502
+ const vp = pdfPageProxy.getViewport({ scale: 1 });
1503
+ pageDimsRef.current = {
1504
+ width: vp.width,
1505
+ height: vp.height,
1506
+ proxy: pdfPageProxy
1507
+ };
1508
+ }
1493
1509
  useLayoutEffect(() => {
1494
1510
  const scrollContainer = viewportRef?.current;
1495
1511
  if (!scrollContainer) {
@@ -1515,9 +1531,7 @@ var useDetailCanvasLayer = ({
1515
1531
  hideDetailCanvas();
1516
1532
  return;
1517
1533
  }
1518
- const baseViewport = pdfPageProxy.getViewport({ scale: 1 });
1519
- const pageWidth = baseViewport.width;
1520
- const pageHeight = baseViewport.height;
1534
+ const { width: pageWidth, height: pageHeight } = pageDimsRef.current;
1521
1535
  const scrollX = scrollContainer.scrollLeft / zoom;
1522
1536
  const scrollY = scrollContainer.scrollTop / zoom;
1523
1537
  const viewportWidth = scrollContainer.clientWidth / zoom;
@@ -4930,7 +4944,7 @@ var useViewportContainer = ({
4930
4944
  elementWrapperRef,
4931
4945
  elementRef
4932
4946
  }) => {
4933
- const [origin, setOrigin] = useState([0, 0]);
4947
+ const originRef = useRef([0, 0]);
4934
4948
  const wheelInertia = useRef({
4935
4949
  active: false,
4936
4950
  lastTime: 0,
@@ -4950,28 +4964,46 @@ var useViewportContainer = ({
4950
4964
  translateY: 0,
4951
4965
  zoom
4952
4966
  });
4967
+ const zoomRafRef = useRef(null);
4968
+ const lastPushedZoomRef = useRef(zoom);
4953
4969
  const updateTransform = useCallback(
4954
4970
  (zoomUpdate) => {
4955
4971
  if (!elementRef.current || !containerRef.current || !elementWrapperRef.current) {
4956
4972
  return;
4957
4973
  }
4958
4974
  const { zoom: zoom2, translateX, translateY } = transformations.current;
4975
+ const naturalWidth = parseFloat(elementRef.current.style.width) || elementRef.current.offsetWidth;
4976
+ const naturalHeight = parseFloat(elementRef.current.style.height) || elementRef.current.offsetHeight;
4959
4977
  elementRef.current.style.transform = `scale3d(${zoom2}, ${zoom2}, 1)`;
4960
- elementRef.current.style.willChange = "scale3d";
4961
- const elementBoundingBox = elementRef.current.getBoundingClientRect();
4962
- const width = elementBoundingBox.width;
4963
- elementWrapperRef.current.style.width = `${width}px`;
4964
- elementWrapperRef.current.style.height = `${elementBoundingBox.height}px`;
4978
+ elementWrapperRef.current.style.width = `${naturalWidth * zoom2}px`;
4979
+ elementWrapperRef.current.style.height = `${naturalHeight * zoom2}px`;
4965
4980
  containerRef.current.scrollTop = translateY;
4966
4981
  containerRef.current.scrollLeft = translateX;
4967
- if (zoomUpdate) updateZoom(() => transformations.current.zoom);
4982
+ if (zoomUpdate && zoomRafRef.current === null) {
4983
+ zoomRafRef.current = requestAnimationFrame(() => {
4984
+ zoomRafRef.current = null;
4985
+ const z = transformations.current.zoom;
4986
+ lastPushedZoomRef.current = z;
4987
+ updateZoom(() => z);
4988
+ });
4989
+ }
4968
4990
  },
4969
4991
  [containerRef, elementRef, elementWrapperRef, updateZoom]
4970
4992
  );
4993
+ useEffect(() => {
4994
+ return () => {
4995
+ if (zoomRafRef.current !== null) {
4996
+ cancelAnimationFrame(zoomRafRef.current);
4997
+ }
4998
+ };
4999
+ }, []);
4971
5000
  useEffect(() => {
4972
5001
  if (transformations.current.zoom === zoom || !containerRef.current) {
4973
5002
  return;
4974
5003
  }
5004
+ if (zoom === lastPushedZoomRef.current) {
5005
+ return;
5006
+ }
4975
5007
  const prevZoom = transformations.current.zoom;
4976
5008
  if (!prevZoom || !Number.isFinite(prevZoom)) {
4977
5009
  transformations.current = {
@@ -5050,7 +5082,7 @@ var useViewportContainer = ({
5050
5082
  useGesture(
5051
5083
  {
5052
5084
  onPinch: (state) => {
5053
- const { origin: origin2, first, movement, delta, event, memo: memo7 } = state;
5085
+ const { origin, first, movement, delta, event, memo: memo7 } = state;
5054
5086
  const [ms] = movement;
5055
5087
  const [deltaScale] = delta;
5056
5088
  const currentElement = elementRef.current;
@@ -5061,17 +5093,17 @@ var useViewportContainer = ({
5061
5093
  const containerRect = currentContainer.getBoundingClientRect();
5062
5094
  const currentZoom = transformations.current.zoom;
5063
5095
  const contentPosition = [
5064
- origin2[0] - elementRect.left,
5065
- origin2[1] - elementRect.top
5096
+ origin[0] - elementRect.left,
5097
+ origin[1] - elementRect.top
5066
5098
  ];
5067
5099
  const containerPosition = [
5068
- origin2[0] - containerRect.left,
5069
- origin2[1] - containerRect.top
5100
+ origin[0] - containerRect.left,
5101
+ origin[1] - containerRect.top
5070
5102
  ];
5071
- setOrigin([
5103
+ originRef.current = [
5072
5104
  contentPosition[0] / currentZoom,
5073
5105
  contentPosition[1] / currentZoom
5074
- ]);
5106
+ ];
5075
5107
  return {
5076
5108
  contentPosition,
5077
5109
  containerPosition,
@@ -5120,7 +5152,7 @@ var useViewportContainer = ({
5120
5152
  }
5121
5153
  );
5122
5154
  return {
5123
- origin
5155
+ origin: originRef.current
5124
5156
  };
5125
5157
  };
5126
5158
  var selectLargestPageWidth = (state) => state.viewports.reduce((max, vp) => Math.max(max, vp.width), 0);
@@ -5280,6 +5312,7 @@ var Pages = ({
5280
5312
  alignItems: "center",
5281
5313
  flexDirection: "column",
5282
5314
  transformOrigin: "0 0",
5315
+ willChange: "transform",
5283
5316
  // width: "max-content",
5284
5317
  width: largestPageWidth,
5285
5318
  margin: "0 auto"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anaralabs/lector",
3
- "version": "3.10.1",
3
+ "version": "3.10.2",
4
4
  "description": "A headless PDF viewer for React",
5
5
  "author": "andrewdr (https://github.com/andrewdoro)",
6
6
  "license": "MIT",