react-pdf-highlighter-plus 1.1.1 → 1.1.3

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.
@@ -1694,6 +1694,8 @@ interface LeftPanelProps {
1694
1694
  linkService?: PDFLinkService | unknown | null;
1695
1695
  /** Event bus for page change events */
1696
1696
  eventBus?: EventBus$1 | unknown | null;
1697
+ /** Function to navigate to a page (from pdfHighlighterUtils.goToPage) */
1698
+ goToPage?: (pageNumber: number) => void;
1697
1699
  /** Whether panel is open */
1698
1700
  isOpen?: boolean;
1699
1701
  /** Callback when open state changes */
@@ -1829,6 +1831,7 @@ interface UseDocumentOutlineOptions {
1829
1831
  linkService?: {
1830
1832
  goToDestination: (dest: unknown) => void;
1831
1833
  } | unknown | null;
1834
+ goToPage?: (pageNumber: number) => void;
1832
1835
  }
1833
1836
  interface UseDocumentOutlineResult {
1834
1837
  outline: ProcessedOutlineItem[] | null;
package/dist/esm/index.js CHANGED
@@ -1484,7 +1484,63 @@ var PdfHighlighter = ({
1484
1484
  getLinkService: () => linkServiceRef.current,
1485
1485
  getEventBus: () => eventBusRef.current,
1486
1486
  goToPage: (pageNumber) => {
1487
- viewerRef.current?.scrollPageIntoView({ pageNumber });
1487
+ console.log("[PdfHighlighter] goToPage called with page:", pageNumber);
1488
+ const viewer = viewerRef.current;
1489
+ if (!viewer) {
1490
+ console.log("[PdfHighlighter] goToPage: viewer not available");
1491
+ return;
1492
+ }
1493
+ const container = viewer.container;
1494
+ console.log("[PdfHighlighter] goToPage: container:", !!container, "offsetParent:", !!container?.offsetParent);
1495
+ if (container && container.offsetParent) {
1496
+ try {
1497
+ console.log("[PdfHighlighter] goToPage: using viewer.scrollPageIntoView");
1498
+ viewer.scrollPageIntoView({ pageNumber });
1499
+ return;
1500
+ } catch (e) {
1501
+ console.log("[PdfHighlighter] goToPage: scrollPageIntoView threw error:", e);
1502
+ }
1503
+ }
1504
+ const pageElement = container?.querySelector(`.page[data-page-number="${pageNumber}"]`);
1505
+ console.log("[PdfHighlighter] goToPage: DOM fallback, pageElement found:", !!pageElement);
1506
+ if (pageElement && container) {
1507
+ const styleTop = pageElement.style.top;
1508
+ const scrollTarget = styleTop ? parseInt(styleTop, 10) : 0;
1509
+ console.log("[PdfHighlighter] goToPage: style.top =", styleTop, "scrollTarget =", scrollTarget);
1510
+ if (scrollTarget > 0) {
1511
+ container.scrollTo({
1512
+ top: scrollTarget,
1513
+ behavior: "smooth"
1514
+ });
1515
+ } else {
1516
+ const containerRect = container.getBoundingClientRect();
1517
+ const pageRect = pageElement.getBoundingClientRect();
1518
+ const scrollTop = container.scrollTop + (pageRect.top - containerRect.top);
1519
+ console.log("[PdfHighlighter] goToPage: using getBoundingClientRect, scrollTop =", scrollTop);
1520
+ container.scrollTo({
1521
+ top: scrollTop,
1522
+ behavior: "smooth"
1523
+ });
1524
+ }
1525
+ } else {
1526
+ const docPageElement = document.querySelector(`.page[data-page-number="${pageNumber}"]`);
1527
+ console.log("[PdfHighlighter] goToPage: document-wide search, found:", !!docPageElement);
1528
+ if (docPageElement) {
1529
+ const styleTop = docPageElement.style.top;
1530
+ const scrollTarget = styleTop ? parseInt(styleTop, 10) : 0;
1531
+ const scrollContainer = docPageElement.closest(".pdfViewer")?.parentElement;
1532
+ if (scrollContainer && scrollTarget > 0) {
1533
+ console.log("[PdfHighlighter] goToPage: document search, scrolling to style.top =", scrollTarget);
1534
+ scrollContainer.scrollTo({
1535
+ top: scrollTarget,
1536
+ behavior: "smooth"
1537
+ });
1538
+ } else {
1539
+ console.log("[PdfHighlighter] goToPage: using scrollIntoView fallback");
1540
+ docPageElement.scrollIntoView({ behavior: "smooth", block: "start" });
1541
+ }
1542
+ }
1543
+ }
1488
1544
  }
1489
1545
  };
1490
1546
  const utilsRefCalledRef = useRef5(false);
@@ -3590,7 +3646,7 @@ async function exportPdf(pdfSource, highlights, options = {}) {
3590
3646
  }
3591
3647
 
3592
3648
  // src/components/leftpanel/LeftPanel.tsx
3593
- import React21, { useState as useState17, useMemo as useMemo3, useCallback as useCallback8 } from "react";
3649
+ import React21, { useState as useState17, useMemo as useMemo3, useCallback as useCallback8, useRef as useRef19, useEffect as useEffect18 } from "react";
3594
3650
  import { List, FileText, ChevronLeft, ChevronRight as ChevronRight2 } from "lucide-react";
3595
3651
 
3596
3652
  // src/contexts/LeftPanelContext.ts
@@ -3621,7 +3677,6 @@ async function processOutlineItems(pdfDocument, items, level) {
3621
3677
  pageNumber = pageIndex + 1;
3622
3678
  }
3623
3679
  } catch {
3624
- console.log(`Failed to resolve destination for outline item: ${item.title}`);
3625
3680
  }
3626
3681
  }
3627
3682
  const children = item.items?.length ? await processOutlineItems(pdfDocument, item.items, level + 1) : [];
@@ -3649,7 +3704,7 @@ function flattenOutline(items) {
3649
3704
  return result;
3650
3705
  }
3651
3706
  function useDocumentOutline(options) {
3652
- const { pdfDocument, linkService } = options;
3707
+ const { pdfDocument, goToPage } = options;
3653
3708
  const [outline, setOutline] = useState12(null);
3654
3709
  const [isLoading, setIsLoading] = useState12(true);
3655
3710
  const [error, setError] = useState12(null);
@@ -3660,7 +3715,6 @@ function useDocumentOutline(options) {
3660
3715
  setIsLoading(true);
3661
3716
  setError(null);
3662
3717
  const rawOutline = await pdfDocument.getOutline();
3663
- console.log("Raw outline from PDF:", rawOutline);
3664
3718
  if (cancelled) return;
3665
3719
  if (!rawOutline || rawOutline.length === 0) {
3666
3720
  setOutline([]);
@@ -3672,11 +3726,9 @@ function useDocumentOutline(options) {
3672
3726
  0
3673
3727
  );
3674
3728
  if (cancelled) return;
3675
- console.log("Processed outline:", processedOutline);
3676
3729
  setOutline(processedOutline);
3677
3730
  } catch (err) {
3678
3731
  if (cancelled) return;
3679
- console.error("Failed to load outline:", err);
3680
3732
  setError(err instanceof Error ? err : new Error("Failed to load outline"));
3681
3733
  } finally {
3682
3734
  if (!cancelled) {
@@ -3691,12 +3743,11 @@ function useDocumentOutline(options) {
3691
3743
  }, [pdfDocument]);
3692
3744
  const navigateToItem = useCallback5(
3693
3745
  (item) => {
3694
- console.log("Navigating to outline item:", item);
3695
- if (linkService && item.dest && typeof linkService === "object" && "goToDestination" in linkService) {
3696
- linkService.goToDestination(item.dest);
3746
+ if (goToPage && item.pageNumber) {
3747
+ goToPage(item.pageNumber);
3697
3748
  }
3698
3749
  },
3699
- [linkService]
3750
+ [goToPage]
3700
3751
  );
3701
3752
  const flatOutline = useMemo2(() => {
3702
3753
  if (!outline) return [];
@@ -3903,7 +3954,9 @@ function usePageNavigation(options) {
3903
3954
  const viewerRef = useRef16(viewer);
3904
3955
  viewerRef.current = viewer;
3905
3956
  useEffect15(() => {
3906
- if (!eventBus || !isEventBus(eventBus)) return;
3957
+ if (!eventBus || !isEventBus(eventBus)) {
3958
+ return;
3959
+ }
3907
3960
  const handlePageChange = (evt) => {
3908
3961
  setCurrentPage(evt.pageNumber);
3909
3962
  };
@@ -3912,11 +3965,11 @@ function usePageNavigation(options) {
3912
3965
  eventBus.off("pagechanging", handlePageChange);
3913
3966
  };
3914
3967
  }, [eventBus]);
3915
- const syncedRef = useRef16(false);
3968
+ const prevViewerRef = useRef16(viewer);
3916
3969
  useEffect15(() => {
3917
- if (viewer && isViewer(viewer) && viewer.currentPageNumber && !syncedRef.current) {
3918
- setCurrentPage(viewer.currentPageNumber);
3919
- syncedRef.current = true;
3970
+ if (viewer !== prevViewerRef.current) {
3971
+ setCurrentPage(1);
3972
+ prevViewerRef.current = viewer;
3920
3973
  }
3921
3974
  }, [viewer]);
3922
3975
  const goToPage = useCallback7(
@@ -3925,10 +3978,13 @@ function usePageNavigation(options) {
3925
3978
  if (v && isViewer(v)) {
3926
3979
  const totalPages2 = v.pagesCount || 0;
3927
3980
  if (pageNumber >= 1 && pageNumber <= totalPages2) {
3928
- try {
3929
- v.scrollPageIntoView({ pageNumber });
3930
- return;
3931
- } catch (e) {
3981
+ const container = v.container;
3982
+ if (container && container.offsetParent) {
3983
+ try {
3984
+ v.scrollPageIntoView({ pageNumber });
3985
+ return;
3986
+ } catch {
3987
+ }
3932
3988
  }
3933
3989
  }
3934
3990
  }
@@ -4576,6 +4632,7 @@ var LeftPanel = ({
4576
4632
  viewer = null,
4577
4633
  linkService = null,
4578
4634
  eventBus = null,
4635
+ goToPage: goToPageProp,
4579
4636
  isOpen: controlledIsOpen,
4580
4637
  onOpenChange,
4581
4638
  defaultTab = "thumbnails",
@@ -4616,30 +4673,38 @@ var LeftPanel = ({
4616
4673
  },
4617
4674
  [onOpenChange]
4618
4675
  );
4619
- const {
4620
- outline,
4621
- isLoading: isOutlineLoading,
4622
- hasOutline,
4623
- navigateToItem
4624
- } = useDocumentOutline({
4625
- pdfDocument,
4626
- linkService
4627
- });
4628
4676
  const { thumbnails, loadThumbnail, totalPages } = useThumbnails({
4629
4677
  pdfDocument,
4630
4678
  thumbnailWidth
4631
4679
  });
4632
- const { currentPage, goToPage } = usePageNavigation({
4680
+ const { currentPage, goToPage: goToPageFromHook } = usePageNavigation({
4633
4681
  viewer,
4634
4682
  eventBus
4635
4683
  });
4684
+ const goToPagePropRef = useRef19(goToPageProp);
4685
+ useEffect18(() => {
4686
+ goToPagePropRef.current = goToPageProp;
4687
+ }, [goToPageProp]);
4636
4688
  const handlePageSelect = useCallback8(
4637
4689
  (pageNumber) => {
4638
- goToPage(pageNumber);
4690
+ if (goToPagePropRef.current) {
4691
+ goToPagePropRef.current(pageNumber);
4692
+ } else {
4693
+ goToPageFromHook(pageNumber);
4694
+ }
4639
4695
  onPageSelect?.(pageNumber);
4640
4696
  },
4641
- [goToPage, onPageSelect]
4697
+ [goToPageFromHook, onPageSelect]
4642
4698
  );
4699
+ const {
4700
+ outline,
4701
+ isLoading: isOutlineLoading,
4702
+ hasOutline,
4703
+ navigateToItem
4704
+ } = useDocumentOutline({
4705
+ pdfDocument,
4706
+ goToPage: handlePageSelect
4707
+ });
4643
4708
  const handleOutlineNavigate = useCallback8(
4644
4709
  (item) => {
4645
4710
  navigateToItem(item);