@tobyt/expo-pdf-markup 1.1.3 → 1.2.0

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 (73) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +7 -1
  3. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookPager.kt +66 -0
  4. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookTurnPolicy.kt +117 -0
  5. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +370 -57
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
  7. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +96 -5
  8. package/build/ExpoPdfMarkup.types.d.ts +12 -0
  9. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  10. package/build/ExpoPdfMarkup.types.js.map +1 -1
  11. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  12. package/build/ExpoPdfMarkupView.web.js +369 -44
  13. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  14. package/build/web/bookMode.d.ts +77 -0
  15. package/build/web/bookMode.d.ts.map +1 -0
  16. package/build/web/bookMode.js +169 -0
  17. package/build/web/bookMode.js.map +1 -0
  18. package/build/web/layout.d.ts +8 -0
  19. package/build/web/layout.d.ts.map +1 -0
  20. package/build/web/layout.js +22 -0
  21. package/build/web/layout.js.map +1 -0
  22. package/build/web/types.d.ts +2 -0
  23. package/build/web/types.d.ts.map +1 -1
  24. package/build/web/types.js.map +1 -1
  25. package/docs/agents/domain.md +51 -0
  26. package/docs/agents/issue-tracker.md +46 -0
  27. package/docs/agents/triage-labels.md +15 -0
  28. package/ios/AnnotationSerializer.swift +36 -7
  29. package/ios/ExpoPdfMarkupModule.swift +4 -0
  30. package/ios/ExpoPdfMarkupView+Book.swift +455 -0
  31. package/ios/ExpoPdfMarkupView+Ink.swift +10 -2
  32. package/ios/ExpoPdfMarkupView+Move.swift +3 -4
  33. package/ios/ExpoPdfMarkupView.swift +49 -6
  34. package/ios/Tests/AnnotationSerializerTests.swift +0 -22
  35. package/ios/Tests/BookModeTests.swift +534 -0
  36. package/ios/Tests/book-sample.pdf +207 -0
  37. package/ios/Tests/single-page.pdf +52 -0
  38. package/metro.js +32 -14
  39. package/package.json +4 -3
  40. package/src/ExpoPdfMarkup.types.ts +13 -0
  41. package/src/ExpoPdfMarkupView.web.tsx +443 -52
  42. package/src/web/bookMode.ts +186 -0
  43. package/src/web/layout.ts +30 -0
  44. package/src/web/types.ts +3 -0
  45. package/tsconfig.test.json +10 -0
  46. package/docs/.nojekyll +0 -1
  47. package/docs/assets/hierarchy.js +0 -1
  48. package/docs/assets/highlight.css +0 -113
  49. package/docs/assets/icons.js +0 -18
  50. package/docs/assets/icons.svg +0 -1
  51. package/docs/assets/main.js +0 -60
  52. package/docs/assets/navigation.js +0 -1
  53. package/docs/assets/search.js +0 -1
  54. package/docs/assets/style.css +0 -1648
  55. package/docs/functions/ExpoPdfMarkupView.html +0 -42
  56. package/docs/functions/setPdfJsWorkerSrc.html +0 -42
  57. package/docs/hierarchy.html +0 -29
  58. package/docs/index.html +0 -148
  59. package/docs/modules.html +0 -40
  60. package/docs/types/Annotation.html +0 -33
  61. package/docs/types/AnnotationBounds.html +0 -73
  62. package/docs/types/AnnotationMode.html +0 -33
  63. package/docs/types/AnnotationPoint.html +0 -61
  64. package/docs/types/AnnotationsData.html +0 -61
  65. package/docs/types/ExpoPdfMarkupViewProps.html +0 -184
  66. package/docs/types/HighlightAnnotation.html +0 -91
  67. package/docs/types/InkAnnotation.html +0 -91
  68. package/docs/types/StampAnnotation.html +0 -95
  69. package/docs/types/StampDefinition.html +0 -72
  70. package/docs/types/TextAnnotation.html +0 -110
  71. package/docs/types/TextInputRequest.html +0 -73
  72. package/docs/types/UnderlineAnnotation.html +0 -85
  73. package/ios/InkPDFAnnotation.swift +0 -45
@@ -2,6 +2,8 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import * as pdfjs from 'pdfjs-dist';
3
3
  import * as React from 'react';
4
4
  import { clampAnnotationTranslation, canvasRectToPdfBounds, canvasToPdf, drawAnnotationsOnCanvas, hitTestAnnotation, newAnnotationId, parseAnnotations, serializeAnnotations, translateAnnotation, } from './web/annotationUtils';
5
+ import { BOUNCE_PX, EDGE_RESISTANCE, TAP_SLOP_PX, isViewShown, ownsArrowKeys, keyTurnFor, canGestureTurn, classifySwipe, classifyTap, releaseVelocityX, resolveTurn, slideOffsetFor, } from './web/bookMode';
6
+ import { computePageMetas } from './web/layout';
5
7
  // Configure pdfjs worker once at module level (consumers can override before mounting)
6
8
  if (!pdfjs.GlobalWorkerOptions.workerSrc) {
7
9
  // Default: served from public/ by withPdfMarkup() in metro.js.
@@ -325,7 +327,7 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
325
327
  : annotationMode === 'stamp'
326
328
  ? 'copy'
327
329
  : 'crosshair';
328
- return (_jsxs("div", { ref: containerRef, style: { position: 'relative', width: cssWidth, height: cssHeight, margin: '0 auto' }, children: [_jsx("canvas", { ref: pdfCanvasRef, style: {
330
+ return (_jsxs("div", { ref: containerRef, "data-pdf-page": pageIndex, style: { position: 'relative', width: cssWidth, height: cssHeight, margin: '0 auto' }, children: [_jsx("canvas", { ref: pdfCanvasRef, style: {
329
331
  position: 'absolute',
330
332
  top: 0,
331
333
  left: 0,
@@ -345,12 +347,16 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
345
347
  // ---------------------------------------------------------------------------
346
348
  // Main component
347
349
  // ---------------------------------------------------------------------------
350
+ /** Duration of the book mode slide, in ms. */
351
+ const TURN_DURATION_MS = 250;
348
352
  export default function ExpoPdfMarkupView(props) {
349
- const { source, page: propPage, backgroundColor = '#f0f0f0', annotations: annotationsProp, annotationMode = 'none', annotationColor = '#FF0000', annotationLineWidth = 2, annotationFontFamily, stampText, stampSize = 48, onPageChanged, onLoadComplete, onError, onAnnotationsChanged, onTextInputRequested, style, } = props;
353
+ const { source, displayMode = 'continuous', page: propPage, backgroundColor = '#f0f0f0', annotations: annotationsProp, annotationMode = 'none', annotationColor = '#FF0000', annotationLineWidth = 2, annotationFontFamily, stampText, stampSize = 48, onPageChanged, onLoadComplete, onError, onAnnotationsChanged, onTextInputRequested, style, } = props;
354
+ const isBook = displayMode === 'book';
350
355
  const scrollContainerRef = React.useRef(null);
351
- const [containerWidth, setContainerWidth] = React.useState(0);
356
+ const [containerSize, setContainerSize] = React.useState({ width: 0, height: 0 });
357
+ const containerWidth = containerSize.width;
352
358
  const [doc, setDoc] = React.useState(null);
353
- const [pageMetas, setPageMetas] = React.useState([]);
359
+ const [pageSizes, setPageSizes] = React.useState([]);
354
360
  const [annotations, setAnnotations] = React.useState([]);
355
361
  const lastAnnotationsJsonRef = React.useRef(undefined);
356
362
  const pageContainerRefs = React.useRef([]);
@@ -358,7 +364,40 @@ export default function ExpoPdfMarkupView(props) {
358
364
  const loadingSourceRef = React.useRef(null);
359
365
  const [zoomLevel, setZoomLevel] = React.useState(1);
360
366
  const zoomRef = React.useRef(1);
361
- // ResizeObserver → containerWidth
367
+ // The page the reader is on, shared by both modes so switching mode keeps it. The ref is read
368
+ // by the turn path, so quick turns in a row see the latest page before React re-renders.
369
+ const [currentPage, setCurrentPage] = React.useState(0);
370
+ const currentPageRef = React.useRef(0);
371
+ const lastReportedPageRef = React.useRef(-1);
372
+ // Book mode slide: horizontal offset of the page strip, and whether it animates there.
373
+ const [slide, setSlide] = React.useState({ offset: 0, animated: false });
374
+ const slideOffsetRef = React.useRef(0);
375
+ const turnTimerRef = React.useRef(null);
376
+ const turnFrameRef = React.useRef(null);
377
+ const pendingTurnRef = React.useRef(null);
378
+ const gestureRef = React.useRef(null);
379
+ const activePointersRef = React.useRef(new Set());
380
+ const dpr = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
381
+ // Continuous mode ignores the view's height, so a height change doesn't re-lay it out.
382
+ const fitHeight = isBook ? containerSize.height : 0;
383
+ const pageMetas = React.useMemo(() => computePageMetas(pageSizes, containerWidth, fitHeight, displayMode, dpr), [pageSizes, containerWidth, fitHeight, displayMode, dpr]);
384
+ const pageCount = doc ? pageMetas.length : 0;
385
+ /** Records the reader's page and fires `onPageChanged` once per change. */
386
+ const reportPage = React.useCallback((page) => {
387
+ currentPageRef.current = page;
388
+ setCurrentPage(page);
389
+ if (!doc || page === lastReportedPageRef.current)
390
+ return;
391
+ lastReportedPageRef.current = page;
392
+ onPageChanged?.({
393
+ nativeEvent: {
394
+ page,
395
+ pageCount: doc.numPages,
396
+ pageWidth: pageSizes[page]?.pdfWidth ?? 0,
397
+ pageHeight: pageSizes[page]?.pdfHeight ?? 0,
398
+ },
399
+ });
400
+ }, [doc, pageSizes, onPageChanged]);
362
401
  const applyAnnotations = React.useCallback((updater) => {
363
402
  setAnnotations((prev) => {
364
403
  const next = updater(prev);
@@ -368,13 +407,16 @@ export default function ExpoPdfMarkupView(props) {
368
407
  return next;
369
408
  });
370
409
  }, [onAnnotationsChanged]);
410
+ // ResizeObserver → containerSize
371
411
  React.useEffect(() => {
372
412
  const el = scrollContainerRef.current;
373
413
  if (!el)
374
414
  return;
375
415
  const ro = new ResizeObserver((entries) => {
376
- const width = entries[0]?.contentRect.width ?? 0;
377
- setContainerWidth(width);
416
+ const rect = entries[0]?.contentRect;
417
+ const width = rect?.width ?? 0;
418
+ const height = rect?.height ?? 0;
419
+ setContainerSize((prev) => prev.width === width && prev.height === height ? prev : { width, height });
378
420
  });
379
421
  ro.observe(el);
380
422
  return () => ro.disconnect();
@@ -450,9 +492,13 @@ export default function ExpoPdfMarkupView(props) {
450
492
  el.removeEventListener('touchcancel', onTouchEnd);
451
493
  };
452
494
  }, []);
453
- // Load PDF when source or containerWidth changes
495
+ // Load the PDF once the view has a size. Page layout is derived from the page sizes, so
496
+ // resizing the view re-lays out the pages without reloading the document, even when it comes
497
+ // back from zero size (e.g. a screen that was display: none in a back stack).
498
+ const hasSize = containerWidth > 0;
499
+ const loadedSourceRef = React.useRef(null);
454
500
  React.useEffect(() => {
455
- if (!source || containerWidth <= 0)
501
+ if (!source || !hasSize || loadedSourceRef.current === source)
456
502
  return;
457
503
  let cancelled = false;
458
504
  loadingSourceRef.current = source;
@@ -464,8 +510,7 @@ export default function ExpoPdfMarkupView(props) {
464
510
  loadedDoc.loadingTask.destroy();
465
511
  return;
466
512
  }
467
- const metas = [];
468
- const dpr = window.devicePixelRatio ?? 1;
513
+ const sizes = [];
469
514
  for (let i = 1; i <= loadedDoc.numPages; i++) {
470
515
  const pg = await loadedDoc.getPage(i);
471
516
  if (cancelled) {
@@ -474,21 +519,17 @@ export default function ExpoPdfMarkupView(props) {
474
519
  return;
475
520
  }
476
521
  const viewport = pg.getViewport({ scale: 1 });
477
- const scale = containerWidth / viewport.width;
478
- const cssHeight = viewport.height * scale;
479
- metas.push({
480
- pdfWidth: viewport.width,
481
- pdfHeight: viewport.height,
482
- scale,
483
- canvasWidth: Math.round(containerWidth * dpr),
484
- canvasHeight: Math.round(cssHeight * dpr),
485
- });
522
+ sizes.push({ pdfWidth: viewport.width, pdfHeight: viewport.height });
486
523
  pg.cleanup();
487
524
  }
488
525
  docRef.current?.loadingTask.destroy();
489
526
  docRef.current = loadedDoc;
527
+ loadedSourceRef.current = source;
528
+ currentPageRef.current = 0;
529
+ lastReportedPageRef.current = -1;
530
+ setCurrentPage(0);
490
531
  setDoc(loadedDoc);
491
- setPageMetas(metas);
532
+ setPageSizes(sizes);
492
533
  pageContainerRefs.current = new Array(loadedDoc.numPages).fill(null);
493
534
  onLoadComplete?.({ nativeEvent: { pageCount: loadedDoc.numPages } });
494
535
  }
@@ -504,7 +545,24 @@ export default function ExpoPdfMarkupView(props) {
504
545
  return () => {
505
546
  cancelled = true;
506
547
  };
507
- }, [source, containerWidth]); // intentionally omit callbacks — they should not re-trigger a PDF load
548
+ }, [source, hasSize]); // intentionally omit callbacks — they should not re-trigger a PDF load
549
+ // Continuous mode keeps the events it had in 1.1.4, when a width change reloaded the document:
550
+ // `onLoadComplete` fires again and the page in view is reported afresh. The document now loads
551
+ // once, so those events are replayed here instead. Book mode, and switching mode, fire neither.
552
+ const laidOutRef = React.useRef({
553
+ doc: null,
554
+ width: 0,
555
+ });
556
+ React.useEffect(() => {
557
+ const last = laidOutRef.current;
558
+ laidOutRef.current = { doc, width: containerWidth };
559
+ if (!doc || isBook || last.doc !== doc)
560
+ return;
561
+ if (containerWidth <= 0 || containerWidth === last.width)
562
+ return;
563
+ lastReportedPageRef.current = -1;
564
+ onLoadComplete?.({ nativeEvent: { pageCount: doc.numPages } });
565
+ }, [doc, containerWidth, isBook]); // intentionally omit callbacks — only a resize fires these
508
566
  // Sync annotations prop → state (guard round-trips)
509
567
  React.useEffect(() => {
510
568
  if (annotationsProp === lastAnnotationsJsonRef.current)
@@ -513,21 +571,171 @@ export default function ExpoPdfMarkupView(props) {
513
571
  const parsed = parseAnnotations(annotationsProp);
514
572
  setAnnotations(parsed.annotations);
515
573
  }, [annotationsProp]);
516
- // Scroll to propPage
574
+ // -------------------------------------------------------------------------
575
+ // Book mode turns
576
+ // -------------------------------------------------------------------------
577
+ const setSlideOffset = React.useCallback((offset, animated) => {
578
+ slideOffsetRef.current = offset;
579
+ setSlide((prev) => prev.offset === offset && prev.animated === animated ? prev : { offset, animated });
580
+ }, []);
581
+ const clearTurnTimer = React.useCallback(() => {
582
+ if (turnTimerRef.current != null)
583
+ clearTimeout(turnTimerRef.current);
584
+ turnTimerRef.current = null;
585
+ if (turnFrameRef.current != null)
586
+ cancelAnimationFrame(turnFrameRef.current);
587
+ turnFrameRef.current = null;
588
+ }, []);
589
+ const resetZoom = React.useCallback(() => {
590
+ if (zoomRef.current === 1)
591
+ return;
592
+ zoomRef.current = 1;
593
+ setZoomLevel(1);
594
+ const el = scrollContainerRef.current;
595
+ if (el) {
596
+ el.scrollLeft = 0;
597
+ el.scrollTop = 0;
598
+ }
599
+ }, []);
600
+ /** Shows `page` straight away, with no slide, and reports it. */
601
+ const showBookPage = React.useCallback((page) => {
602
+ clearTurnTimer();
603
+ pendingTurnRef.current = null;
604
+ setSlideOffset(0, false);
605
+ reportPage(page);
606
+ }, [clearTurnTimer, reportPage, setSlideOffset]);
607
+ /** Lands a slide that is still running, so a new turn starts from the page it was going to. */
608
+ const finishPendingTurn = React.useCallback(() => {
609
+ const pending = pendingTurnRef.current;
610
+ if (pending != null)
611
+ showBookPage(pending);
612
+ else
613
+ clearTurnTimer();
614
+ }, [clearTurnTimer, showBookPage]);
615
+ /** Slides the page back to rest. From rest it nudges towards `direction` first: the bounce. */
616
+ const bounce = React.useCallback((direction) => {
617
+ clearTurnTimer();
618
+ if (slideOffsetRef.current === 0) {
619
+ setSlideOffset(slideOffsetFor(direction, BOUNCE_PX), true);
620
+ turnTimerRef.current = setTimeout(() => {
621
+ setSlideOffset(0, true);
622
+ turnTimerRef.current = null;
623
+ }, TURN_DURATION_MS / 2);
624
+ }
625
+ else {
626
+ setSlideOffset(0, true);
627
+ }
628
+ }, [clearTurnTimer, setSlideOffset]);
629
+ /**
630
+ * The one turn path for book mode. Swipes and edge taps call it once the turn policy allows
631
+ * them; hardware keys can call it directly. It resets zoom to fit, slides the page in
632
+ * `direction` (or bounces at either end) and fires `onPageChanged` once the turn lands.
633
+ */
634
+ const turnPage = React.useCallback((direction) => {
635
+ if (!isBook || pageCount === 0)
636
+ return;
637
+ finishPendingTurn();
638
+ const wasZoomed = zoomRef.current !== 1;
639
+ resetZoom();
640
+ const target = resolveTurn(currentPageRef.current, direction, pageCount);
641
+ if (target == null) {
642
+ bounce(direction);
643
+ return;
644
+ }
645
+ pendingTurnRef.current = target;
646
+ const slideAway = () => {
647
+ turnFrameRef.current = null;
648
+ setSlideOffset(slideOffsetFor(direction, containerWidth), true);
649
+ turnTimerRef.current = setTimeout(() => showBookPage(target), TURN_DURATION_MS);
650
+ };
651
+ if (!wasZoomed) {
652
+ slideAway();
653
+ return;
654
+ }
655
+ // A zoomed page shows on its own, so the strip of neighbours only comes back with this
656
+ // render. Let it paint at rest first, or the slide would jump instead of animating.
657
+ setSlideOffset(0, false);
658
+ turnFrameRef.current = requestAnimationFrame(() => {
659
+ turnFrameRef.current = requestAnimationFrame(slideAway);
660
+ });
661
+ }, [
662
+ isBook,
663
+ pageCount,
664
+ containerWidth,
665
+ finishPendingTurn,
666
+ resetZoom,
667
+ bounce,
668
+ setSlideOffset,
669
+ showBookPage,
670
+ ]);
671
+ React.useEffect(() => clearTurnTimer, [clearTurnTimer]);
672
+ // Hardware keys and page-turner pedals turn the page in book mode.
673
+ React.useEffect(() => {
674
+ if (!isBook)
675
+ return;
676
+ function onKeyDown(e) {
677
+ // A modified arrow belongs to the browser or the host app (Alt+Left is Back, Shift selects).
678
+ if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey)
679
+ return;
680
+ const direction = keyTurnFor(e.key);
681
+ if (!direction)
682
+ return;
683
+ // Keys pressed in a text field or an arrow-key control anywhere on the page are its own.
684
+ if (ownsArrowKeys(e.target) || ownsArrowKeys(document.activeElement)) {
685
+ return;
686
+ }
687
+ // A view that isn't on screen (hidden, or a screen left mounted in a back stack) keeps out.
688
+ if (!isViewShown(scrollContainerRef.current, containerSize.width, containerSize.height)) {
689
+ return;
690
+ }
691
+ e.preventDefault();
692
+ turnPage(direction);
693
+ }
694
+ document.addEventListener('keydown', onKeyDown);
695
+ return () => document.removeEventListener('keydown', onKeyDown);
696
+ }, [isBook, turnPage, containerSize]);
697
+ // Switching mode keeps the reader's page and starts it at fit.
698
+ React.useLayoutEffect(() => {
699
+ if (!doc)
700
+ return;
701
+ finishPendingTurn();
702
+ resetZoom();
703
+ if (!isBook) {
704
+ pageContainerRefs.current[currentPageRef.current]?.scrollIntoView({
705
+ behavior: 'instant',
706
+ });
707
+ }
708
+ // Only a mode switch should move the page; the other dependencies are stable per document.
709
+ }, [isBook]);
710
+ // In book mode, report the page shown when the document opens.
711
+ React.useEffect(() => {
712
+ if (isBook && doc && pageCount > 0)
713
+ reportPage(currentPageRef.current);
714
+ }, [isBook, doc, pageCount, reportPage]);
715
+ // Go to propPage
517
716
  React.useEffect(() => {
518
717
  if (propPage == null || !doc)
519
718
  return;
719
+ if (isBook) {
720
+ if (propPage < 0 || propPage >= pageSizes.length)
721
+ return;
722
+ // `page` is a one-shot command, so acting on it here is the event, not derived state.
723
+ resetZoom();
724
+ // eslint-disable-next-line react-hooks/set-state-in-effect
725
+ showBookPage(propPage);
726
+ return;
727
+ }
520
728
  const el = pageContainerRefs.current[propPage];
521
729
  el?.scrollIntoView({ behavior: 'instant' });
730
+ // Only a new `page` (or document) is a jump; switching mode keeps the current page instead.
522
731
  }, [propPage, doc]);
523
- // IntersectionObserver → onPageChanged
732
+ // IntersectionObserver → onPageChanged (continuous mode)
524
733
  React.useEffect(() => {
525
- if (!doc || pageMetas.length === 0)
734
+ if (isBook || !doc || pageMetas.length === 0)
526
735
  return;
527
736
  const els = pageContainerRefs.current.filter(Boolean);
528
737
  if (els.length === 0)
529
738
  return;
530
- let lastReported = -1;
531
739
  const io = new IntersectionObserver((entries) => {
532
740
  let topPage = -1;
533
741
  let topY = Infinity;
@@ -540,22 +748,103 @@ export default function ExpoPdfMarkupView(props) {
540
748
  }
541
749
  }
542
750
  }
543
- if (topPage !== -1 && topPage !== lastReported) {
544
- lastReported = topPage;
545
- onPageChanged?.({
546
- nativeEvent: {
547
- page: topPage,
548
- pageCount: doc.numPages,
549
- pageWidth: pageMetas[topPage]?.pdfWidth ?? 0,
550
- pageHeight: pageMetas[topPage]?.pdfHeight ?? 0,
551
- },
552
- });
553
- }
751
+ if (topPage !== -1)
752
+ reportPage(topPage);
554
753
  }, { root: scrollContainerRef.current, threshold: 0.1 });
555
754
  for (const el of els)
556
755
  io.observe(el);
557
756
  return () => io.disconnect();
558
- }, [doc, pageMetas, onPageChanged]);
757
+ }, [isBook, doc, pageMetas, reportPage]);
758
+ // -------------------------------------------------------------------------
759
+ // Book mode gestures: swipes and edge taps
760
+ // -------------------------------------------------------------------------
761
+ function cancelBookGesture() {
762
+ const gesture = gestureRef.current;
763
+ gestureRef.current = null;
764
+ if (gesture?.kind === 'turn' && slideOffsetRef.current !== 0 && turnTimerRef.current == null) {
765
+ setSlideOffset(0, true);
766
+ }
767
+ }
768
+ function handleBookPointerDown(e) {
769
+ activePointersRef.current.add(e.pointerId);
770
+ if (activePointersRef.current.size > 1) {
771
+ // A second finger means a pinch, never a turn.
772
+ cancelBookGesture();
773
+ return;
774
+ }
775
+ if (e.button !== 0 || annotationMode !== 'none')
776
+ return;
777
+ const kind = canGestureTurn(annotationMode, zoomRef.current) ? 'turn' : 'pan';
778
+ if (kind === 'turn')
779
+ finishPendingTurn();
780
+ gestureRef.current = {
781
+ startX: e.clientX,
782
+ startY: e.clientY,
783
+ lastX: e.clientX,
784
+ lastY: e.clientY,
785
+ kind,
786
+ moved: false,
787
+ samples: [{ x: e.clientX, t: e.timeStamp }],
788
+ };
789
+ try {
790
+ e.currentTarget.setPointerCapture(e.pointerId);
791
+ }
792
+ catch {
793
+ // The pointer may already be gone (e.g. a synthetic event); the drag still works without capture.
794
+ }
795
+ }
796
+ function handleBookPointerMove(e) {
797
+ const gesture = gestureRef.current;
798
+ if (!gesture)
799
+ return;
800
+ const dx = e.clientX - gesture.startX;
801
+ const dy = e.clientY - gesture.startY;
802
+ if (!gesture.moved && Math.hypot(dx, dy) >= TAP_SLOP_PX)
803
+ gesture.moved = true;
804
+ if (gesture.kind === 'pan') {
805
+ const el = scrollContainerRef.current;
806
+ if (el) {
807
+ el.scrollLeft -= e.clientX - gesture.lastX;
808
+ el.scrollTop -= e.clientY - gesture.lastY;
809
+ }
810
+ }
811
+ else if (gesture.moved) {
812
+ // Past either end the page follows the finger only part of the way.
813
+ const blocked = resolveTurn(currentPageRef.current, dx < 0 ? 'next' : 'previous', pageCount);
814
+ setSlideOffset(blocked == null ? dx * EDGE_RESISTANCE : dx, false);
815
+ }
816
+ gesture.lastX = e.clientX;
817
+ gesture.lastY = e.clientY;
818
+ gesture.samples.push({ x: e.clientX, t: e.timeStamp });
819
+ if (gesture.samples.length > 20)
820
+ gesture.samples.shift();
821
+ }
822
+ function handleBookPointerUp(e) {
823
+ activePointersRef.current.delete(e.pointerId);
824
+ const gesture = gestureRef.current;
825
+ gestureRef.current = null;
826
+ if (!gesture || gesture.kind === 'pan')
827
+ return;
828
+ const dx = e.clientX - gesture.startX;
829
+ const dy = e.clientY - gesture.startY;
830
+ if (!gesture.moved) {
831
+ const rect = e.currentTarget.getBoundingClientRect();
832
+ const direction = classifyTap(e.clientX - rect.left, containerWidth);
833
+ if (direction)
834
+ turnPage(direction);
835
+ return;
836
+ }
837
+ gesture.samples.push({ x: e.clientX, t: e.timeStamp });
838
+ const direction = classifySwipe(dx, dy, containerWidth, releaseVelocityX(gesture.samples));
839
+ if (direction)
840
+ turnPage(direction);
841
+ else if (slideOffsetRef.current !== 0)
842
+ setSlideOffset(0, true);
843
+ }
844
+ function handleBookPointerCancel(e) {
845
+ activePointersRef.current.delete(e.pointerId);
846
+ cancelBookGesture();
847
+ }
559
848
  const handleAnnotationAdded = React.useCallback((annotation) => {
560
849
  applyAnnotations((prev) => [...prev, annotation]);
561
850
  }, [applyAnnotations]);
@@ -608,15 +897,51 @@ export default function ExpoPdfMarkupView(props) {
608
897
  onTextInputRequested,
609
898
  ]);
610
899
  const styleObj = style;
611
- return (_jsx("div", { ref: scrollContainerRef, style: {
612
- overflowY: 'scroll',
613
- overflowX: 'auto',
900
+ const pageViewProps = {
901
+ annotations,
902
+ annotationMode,
903
+ annotationColor,
904
+ annotationLineWidth,
905
+ stampText,
906
+ stampSize,
907
+ zoomLevel,
908
+ onAnnotationAdded: handleAnnotationAdded,
909
+ onAnnotationRemoved: handleAnnotationRemoved,
910
+ onAnnotationUpdated: handleAnnotationUpdated,
911
+ onTextInputRequested: handleTextInputRequested,
912
+ };
913
+ const { width, height } = containerSize;
914
+ const zoomed = zoomLevel > 1;
915
+ // Book mode: one slot per page, each the size of the view with the page centred in it. At fit,
916
+ // the current page sits between its neighbours on a strip that slides sideways. Zoomed in,
917
+ // only the current page is shown, and the view scrolls over it.
918
+ const slotStyle = {
919
+ width,
920
+ height,
921
+ flex: 'none',
922
+ display: 'flex',
923
+ alignItems: 'center',
924
+ justifyContent: 'center',
925
+ };
926
+ const bookSlots = zoomed ? [currentPage] : [currentPage - 1, currentPage, currentPage + 1];
927
+ // The root element stays the same across modes, so the size and zoom listeners stay attached.
928
+ return (_jsx("div", { ref: scrollContainerRef, "data-display-mode": displayMode, "data-page": isBook ? currentPage : undefined, style: {
614
929
  backgroundColor,
615
930
  height: '100%',
931
+ ...(isBook
932
+ ? { overflow: zoomed ? 'auto' : 'hidden', touchAction: 'none', userSelect: 'none' }
933
+ : { overflowY: 'scroll', overflowX: 'auto' }),
616
934
  ...styleObj,
617
- }, children: _jsx("div", { style: { zoom: zoomLevel }, children: doc &&
618
- pageMetas.map((meta, i) => (_jsxs(React.Fragment, { children: [_jsx(PageView, { doc: doc, pageIndex: i, meta: meta, annotations: annotations, annotationMode: annotationMode, annotationColor: annotationColor, annotationLineWidth: annotationLineWidth, stampText: stampText, stampSize: stampSize, zoomLevel: zoomLevel, onAnnotationAdded: handleAnnotationAdded, onAnnotationRemoved: handleAnnotationRemoved, onAnnotationUpdated: handleAnnotationUpdated, onTextInputRequested: handleTextInputRequested, containerRef: (el) => {
935
+ }, onPointerDown: isBook ? handleBookPointerDown : undefined, onPointerMove: isBook ? handleBookPointerMove : undefined, onPointerUp: isBook ? handleBookPointerUp : undefined, onPointerCancel: isBook ? handleBookPointerCancel : undefined, children: _jsx("div", { style: { zoom: zoomLevel }, children: isBook ? (_jsx("div", { style: {
936
+ display: 'flex',
937
+ width: width * bookSlots.length,
938
+ transform: zoomed ? undefined : `translateX(${slide.offset - width}px)`,
939
+ transition: slide.animated ? `transform ${TURN_DURATION_MS}ms ease-out` : 'none',
940
+ }, children: bookSlots.map((i) => (_jsx("div", { style: slotStyle, "aria-hidden": i === currentPage ? undefined : true, "data-book-slot": i, children: doc && pageMetas[i] && (_jsx(PageView, { ...pageViewProps, doc: doc, pageIndex: i, meta: pageMetas[i], containerRef: (el) => {
941
+ pageContainerRefs.current[i] = el;
942
+ } })) }, i))) })) : (doc &&
943
+ pageMetas.map((meta, i) => (_jsxs(React.Fragment, { children: [_jsx(PageView, { ...pageViewProps, doc: doc, pageIndex: i, meta: meta, containerRef: (el) => {
619
944
  pageContainerRefs.current[i] = el;
620
- } }), i < pageMetas.length - 1 && _jsx("div", { style: { height: 8 }, "aria-hidden": true })] }, i))) }) }));
945
+ } }), i < pageMetas.length - 1 && _jsx("div", { style: { height: 8 }, "aria-hidden": true })] }, i)))) }) }));
621
946
  }
622
947
  //# sourceMappingURL=ExpoPdfMarkupView.web.js.map