@anaralabs/lector 3.12.8 → 3.12.9

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 +71 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2669,6 +2669,7 @@ var createTextSelectionManager = () => {
2669
2669
  var bindMouseEvents = createTextSelectionManager();
2670
2670
  var TEXT_BUILD_IDLE_MS = 300;
2671
2671
  var TEXT_SCROLL_SETTLE_MS = 160;
2672
+ var VIEWPORT_ATTACH_MAX_FRAMES = 300;
2672
2673
  var useTextLayer = () => {
2673
2674
  const textContainerRef = useRef(null);
2674
2675
  const textLayerRef = useRef(null);
@@ -2681,15 +2682,22 @@ var useTextLayer = () => {
2681
2682
  return;
2682
2683
  }
2683
2684
  let isCancelled = false;
2684
- const buildTimer = setTimeout(() => {
2685
- if (isCancelled || textContainerRef.current !== textContainer) return;
2685
+ let buildTimer = null;
2686
+ let unsubscribe = null;
2687
+ let built = false;
2688
+ let building = false;
2689
+ let buildGen = 0;
2690
+ const build = () => {
2691
+ building = true;
2692
+ const gen = ++buildGen;
2686
2693
  textContainer.innerHTML = "";
2687
2694
  if (textLayerRef.current) {
2688
2695
  textLayerRef.current.cancel();
2689
2696
  textLayerRef.current = null;
2690
2697
  }
2691
2698
  void import('pdfjs-dist/legacy/build/pdf.mjs').then(({ TextLayer: TextLayer2 }) => {
2692
- if (isCancelled || textContainerRef.current !== textContainer) return;
2699
+ if (isCancelled || gen !== buildGen || textContainerRef.current !== textContainer)
2700
+ return;
2693
2701
  const textLayer = new TextLayer2({
2694
2702
  textContentSource: pdfPageProxy.streamTextContent(),
2695
2703
  container: textContainer,
@@ -2698,22 +2706,61 @@ var useTextLayer = () => {
2698
2706
  textLayerRef.current = textLayer;
2699
2707
  return textLayer.render();
2700
2708
  }).then(() => {
2701
- if (isCancelled || textContainerRef.current !== textContainer) {
2709
+ if (isCancelled || gen !== buildGen || textContainerRef.current !== textContainer) {
2702
2710
  return;
2703
2711
  }
2704
2712
  const endOfContent = document.createElement("div");
2705
2713
  endOfContent.className = "endOfContent";
2706
2714
  textContainer.appendChild(endOfContent);
2707
2715
  bindMouseEvents(textContainer, endOfContent);
2716
+ built = true;
2717
+ building = false;
2718
+ unsubscribe?.();
2719
+ unsubscribe = null;
2708
2720
  }).catch((error) => {
2721
+ if (gen === buildGen) building = false;
2709
2722
  if (error instanceof Error && error.name !== "AbortException") {
2710
2723
  console.error("TextLayer rendering error:", error);
2711
2724
  }
2712
2725
  });
2713
- }, TEXT_BUILD_IDLE_MS);
2726
+ };
2727
+ const schedule = () => {
2728
+ if (built) return;
2729
+ if (building) {
2730
+ buildGen++;
2731
+ building = false;
2732
+ if (textLayerRef.current) {
2733
+ textLayerRef.current.cancel();
2734
+ textLayerRef.current = null;
2735
+ }
2736
+ textContainer.innerHTML = "";
2737
+ }
2738
+ if (buildTimer) clearTimeout(buildTimer);
2739
+ buildTimer = setTimeout(() => {
2740
+ buildTimer = null;
2741
+ if (isCancelled || textContainerRef.current !== textContainer) return;
2742
+ build();
2743
+ }, TEXT_BUILD_IDLE_MS);
2744
+ };
2745
+ let attachRaf = null;
2746
+ let attachAttempts = 0;
2747
+ const attachScrollSubscription = () => {
2748
+ attachRaf = null;
2749
+ const scrollViewport = viewportRef?.current;
2750
+ if (!scrollViewport) {
2751
+ if (++attachAttempts > VIEWPORT_ATTACH_MAX_FRAMES) return;
2752
+ attachRaf = requestAnimationFrame(attachScrollSubscription);
2753
+ return;
2754
+ }
2755
+ unsubscribe = subscribeToViewportInvalidation(scrollViewport, schedule);
2756
+ };
2757
+ attachScrollSubscription();
2758
+ schedule();
2714
2759
  return () => {
2715
2760
  isCancelled = true;
2716
- clearTimeout(buildTimer);
2761
+ if (buildTimer) clearTimeout(buildTimer);
2762
+ if (attachRaf !== null) cancelAnimationFrame(attachRaf);
2763
+ unsubscribe?.();
2717
2764
  if (textLayerRef.current) {
2718
2765
  textLayerRef.current.cancel();
2719
2766
  textLayerRef.current = null;
@@ -2723,11 +2770,10 @@ var useTextLayer = () => {
2723
2770
  delete textContainer._cleanupTextSelection;
2724
2771
  }
2725
2772
  };
2726
- }, [pdfPageProxy.streamTextContent, pdfPageProxy.getViewport]);
2773
+ }, [pdfPageProxy.streamTextContent, pdfPageProxy.getViewport, viewportRef]);
2727
2774
  useEffect(() => {
2728
2775
  const textContainer = textContainerRef.current;
2729
- const viewport = viewportRef?.current;
2730
- if (!textContainer || !viewport) return;
2776
+ if (!textContainer) return;
2731
2777
  let settleTimer = null;
2732
2778
  let pointerDown = false;
2733
2779
  let selecting = false;
@@ -2756,14 +2802,28 @@ var useTextLayer = () => {
2756
2802
  pointerDown = false;
2757
2803
  selecting = false;
2758
2804
  };
2759
- const unsubscribe = subscribeToViewportInvalidation(viewport, onScroll);
2805
+ let unsubscribe = null;
2806
+ let attachRaf = null;
2807
+ let attachAttempts = 0;
2808
+ const attachScrollSubscription = () => {
2809
+ attachRaf = null;
2810
+ const scrollViewport = viewportRef?.current;
2811
+ if (!scrollViewport) {
2812
+ if (++attachAttempts > VIEWPORT_ATTACH_MAX_FRAMES) return;
2813
+ attachRaf = requestAnimationFrame(attachScrollSubscription);
2814
+ return;
2815
+ }
2816
+ unsubscribe = subscribeToViewportInvalidation(scrollViewport, onScroll);
2817
+ };
2818
+ attachScrollSubscription();
2760
2819
  document.addEventListener("pointerdown", onPointerDown, true);
2761
2820
  document.addEventListener("pointerup", clearPointer, true);
2762
2821
  document.addEventListener("pointercancel", clearPointer, true);
2763
2822
  document.addEventListener("selectionchange", onSelectionChange);
2764
2823
  window.addEventListener("blur", clearPointer);
2765
2824
  return () => {
2766
- unsubscribe();
2825
+ if (attachRaf !== null) cancelAnimationFrame(attachRaf);
2826
+ unsubscribe?.();
2767
2827
  document.removeEventListener("pointerdown", onPointerDown, true);
2768
2828
  document.removeEventListener("pointerup", clearPointer, true);
2769
2829
  document.removeEventListener("pointercancel", clearPointer, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anaralabs/lector",
3
- "version": "3.12.8",
3
+ "version": "3.12.9",
4
4
  "description": "A headless PDF viewer for React",
5
5
  "author": "andrewdr (https://github.com/andrewdoro)",
6
6
  "license": "MIT",