@tbib-image-viewer/react 0.1.1 → 0.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.
package/dist/index.mjs CHANGED
@@ -52,7 +52,17 @@ var DownloadMenu = ({
52
52
  }) => {
53
53
  const [isOpen, setIsOpen] = useState(false);
54
54
  const [isExporting, setIsExporting] = useState(false);
55
+ const [popoverPos, setPopoverPos] = useState({ top: 0, right: 0 });
55
56
  const containerRef = useRef(null);
57
+ useEffect(() => {
58
+ if (isOpen && containerRef.current) {
59
+ const rect = containerRef.current.getBoundingClientRect();
60
+ setPopoverPos({
61
+ top: rect.bottom + 8,
62
+ right: Math.max(12, window.innerWidth - rect.right)
63
+ });
64
+ }
65
+ }, [isOpen]);
56
66
  useEffect(() => {
57
67
  const handleOutsideClick = (e) => {
58
68
  if (containerRef.current && !containerRef.current.contains(e.target)) {
@@ -61,9 +71,11 @@ var DownloadMenu = ({
61
71
  };
62
72
  if (isOpen) {
63
73
  document.addEventListener("mousedown", handleOutsideClick);
74
+ document.addEventListener("touchstart", handleOutsideClick);
64
75
  }
65
76
  return () => {
66
77
  document.removeEventListener("mousedown", handleOutsideClick);
78
+ document.removeEventListener("touchstart", handleOutsideClick);
67
79
  };
68
80
  }, [isOpen]);
69
81
  const handleAnnotated = async () => {
@@ -145,11 +157,12 @@ var DownloadMenu = ({
145
157
  {
146
158
  className: "tbib-iv-popover",
147
159
  style: {
160
+ position: "fixed",
161
+ top: `${popoverPos.top}px`,
162
+ right: `${popoverPos.right}px`,
148
163
  flexDirection: "column",
149
164
  minWidth: "210px",
150
- right: 0,
151
- left: "auto",
152
- zIndex: 100
165
+ zIndex: 9999
153
166
  },
154
167
  children: [
155
168
  /* @__PURE__ */ jsxs(
@@ -513,12 +526,20 @@ var AnnotationCanvas = ({
513
526
  activeTool,
514
527
  color,
515
528
  strokeWidth,
529
+ currentScale = 1,
516
530
  onStrokeComplete,
517
- onEraseStroke
531
+ onEraseStroke,
532
+ onPinchZoom,
533
+ onPinchPan
518
534
  }) => {
519
535
  const [currentPoints, setCurrentPoints] = useState2(null);
520
536
  const isDrawingRef = useRef2(false);
521
537
  const svgRef = useRef2(null);
538
+ const activePointersRef = useRef2(/* @__PURE__ */ new Map());
539
+ const pinchStartDistRef = useRef2(null);
540
+ const pinchStartScaleRef = useRef2(1);
541
+ const pinchLastMidpointRef = useRef2(null);
542
+ const isMultiTouchRef = useRef2(false);
522
543
  const isDrawableTool = activeTool === "pen" || activeTool === "highlighter";
523
544
  const isEraser = activeTool === "eraser";
524
545
  const getNormalizedPoint = useCallback(
@@ -539,6 +560,24 @@ var AnnotationCanvas = ({
539
560
  const handlePointerDown = (e) => {
540
561
  if (!isDrawableTool && !isEraser) return;
541
562
  e.stopPropagation();
563
+ activePointersRef.current.set(e.pointerId, { clientX: e.clientX, clientY: e.clientY });
564
+ if (activePointersRef.current.size >= 2) {
565
+ isMultiTouchRef.current = true;
566
+ isDrawingRef.current = false;
567
+ setCurrentPoints(null);
568
+ const pts = Array.from(activePointersRef.current.values());
569
+ const dist = Math.hypot(pts[0].clientX - pts[1].clientX, pts[0].clientY - pts[1].clientY);
570
+ pinchStartDistRef.current = dist > 0 ? dist : 1;
571
+ pinchStartScaleRef.current = currentScale || 1;
572
+ pinchLastMidpointRef.current = {
573
+ x: (pts[0].clientX + pts[1].clientX) / 2,
574
+ y: (pts[0].clientY + pts[1].clientY) / 2
575
+ };
576
+ return;
577
+ }
578
+ isMultiTouchRef.current = false;
579
+ pinchStartDistRef.current = null;
580
+ pinchLastMidpointRef.current = null;
542
581
  e.target.setPointerCapture?.(e.pointerId);
543
582
  const pt = getNormalizedPoint(e);
544
583
  if (!pt) return;
@@ -553,8 +592,28 @@ var AnnotationCanvas = ({
553
592
  }
554
593
  };
555
594
  const handlePointerMove = (e) => {
556
- if (!isDrawingRef.current) return;
557
595
  e.stopPropagation();
596
+ if (activePointersRef.current.has(e.pointerId)) {
597
+ activePointersRef.current.set(e.pointerId, { clientX: e.clientX, clientY: e.clientY });
598
+ }
599
+ if (isMultiTouchRef.current || activePointersRef.current.size >= 2) {
600
+ if (activePointersRef.current.size >= 2 && pinchStartDistRef.current) {
601
+ const pts = Array.from(activePointersRef.current.values());
602
+ const currentDist = Math.hypot(pts[0].clientX - pts[1].clientX, pts[0].clientY - pts[1].clientY);
603
+ const ratio = currentDist / pinchStartDistRef.current;
604
+ onPinchZoom?.(pinchStartScaleRef.current, ratio);
605
+ const midX = (pts[0].clientX + pts[1].clientX) / 2;
606
+ const midY = (pts[0].clientY + pts[1].clientY) / 2;
607
+ if (pinchLastMidpointRef.current) {
608
+ const dx = midX - pinchLastMidpointRef.current.x;
609
+ const dy = midY - pinchLastMidpointRef.current.y;
610
+ onPinchPan?.(dx, dy);
611
+ }
612
+ pinchLastMidpointRef.current = { x: midX, y: midY };
613
+ }
614
+ return;
615
+ }
616
+ if (!isDrawingRef.current) return;
558
617
  const pt = getNormalizedPoint(e);
559
618
  if (!pt) return;
560
619
  if (isEraser) {
@@ -566,8 +625,19 @@ var AnnotationCanvas = ({
566
625
  }
567
626
  };
568
627
  const handlePointerUp = (e) => {
569
- if (!isDrawingRef.current) return;
570
628
  e.stopPropagation();
629
+ activePointersRef.current.delete(e.pointerId);
630
+ if (isMultiTouchRef.current) {
631
+ if (activePointersRef.current.size === 0) {
632
+ isMultiTouchRef.current = false;
633
+ pinchStartDistRef.current = null;
634
+ pinchLastMidpointRef.current = null;
635
+ }
636
+ isDrawingRef.current = false;
637
+ setCurrentPoints(null);
638
+ return;
639
+ }
640
+ if (!isDrawingRef.current) return;
571
641
  isDrawingRef.current = false;
572
642
  if (isDrawableTool && currentPoints && currentPoints.length > 0) {
573
643
  const isHighlighter = activeTool === "highlighter";
@@ -1009,6 +1079,12 @@ function useImageTransform() {
1009
1079
  translateY: prev.translateY + dy
1010
1080
  }));
1011
1081
  }, []);
1082
+ const pinchZoomByRatio = useCallback2((startScale, ratio) => {
1083
+ setTransform((prev) => ({
1084
+ ...prev,
1085
+ scale: clampScale(startScale * ratio)
1086
+ }));
1087
+ }, []);
1012
1088
  const handleWheel = useCallback2((e) => {
1013
1089
  e.preventDefault();
1014
1090
  const factor = e.deltaY < 0 ? 1.15 : 1 / 1.15;
@@ -1049,6 +1125,7 @@ function useImageTransform() {
1049
1125
  resetTransform,
1050
1126
  rotateClockwise,
1051
1127
  panBy,
1128
+ pinchZoomByRatio,
1052
1129
  handleWheel,
1053
1130
  handleTouchStart,
1054
1131
  handleTouchMove,
@@ -1117,6 +1194,7 @@ var ImageViewer = ({
1117
1194
  resetTransform,
1118
1195
  rotateClockwise,
1119
1196
  panBy,
1197
+ pinchZoomByRatio,
1120
1198
  handleWheel,
1121
1199
  handleTouchStart,
1122
1200
  handleTouchMove,
@@ -1221,16 +1299,18 @@ var ImageViewer = ({
1221
1299
  return () => window.removeEventListener("keydown", handleKeyDown);
1222
1300
  }, [handleUndo, handleRedo, zoomIn, zoomOut, resetTransform]);
1223
1301
  const handleDownloadAnnotated = async () => {
1224
- if (!imgRef.current) return;
1225
- await downloadAnnotatedImage(imgRef.current, strokes, {
1302
+ const targetSource = effectiveSrc || imgRef.current?.src || "";
1303
+ if (!targetSource) return;
1304
+ await downloadAnnotatedImage(targetSource, strokes, {
1226
1305
  format: "image/png",
1227
1306
  includeAnnotations: true,
1228
1307
  fileName: `tbib-annotated-${Date.now()}.png`
1229
1308
  });
1230
1309
  };
1231
1310
  const handleDownloadClean = async () => {
1232
- if (!imgRef.current) return;
1233
- await downloadAnnotatedImage(imgRef.current, [], {
1311
+ const targetSource = effectiveSrc || imgRef.current?.src || "";
1312
+ if (!targetSource) return;
1313
+ await downloadAnnotatedImage(targetSource, [], {
1234
1314
  format: "image/png",
1235
1315
  includeAnnotations: false,
1236
1316
  fileName: `tbib-clean-${Date.now()}.png`
@@ -1326,8 +1406,11 @@ var ImageViewer = ({
1326
1406
  activeTool,
1327
1407
  color,
1328
1408
  strokeWidth,
1409
+ currentScale: transform.scale,
1329
1410
  onStrokeComplete: handleStrokeComplete,
1330
- onEraseStroke: handleEraseStroke
1411
+ onEraseStroke: handleEraseStroke,
1412
+ onPinchZoom: pinchZoomByRatio,
1413
+ onPinchPan: panBy
1331
1414
  }
1332
1415
  )
1333
1416
  ]
@@ -1493,6 +1576,7 @@ var ImageGalleryLightbox = ({
1493
1576
  resetTransform,
1494
1577
  rotateClockwise,
1495
1578
  panBy,
1579
+ pinchZoomByRatio,
1496
1580
  handleWheel,
1497
1581
  handleTouchStart,
1498
1582
  handleTouchMove,
@@ -1655,16 +1739,18 @@ var ImageGalleryLightbox = ({
1655
1739
  }
1656
1740
  };
1657
1741
  const handleDownloadAnnotated = async () => {
1658
- if (!imgRef.current) return;
1659
- await downloadAnnotatedImage2(imgRef.current, strokes, {
1742
+ const targetSource = effectiveSrc || currentItem?.src || imgRef.current?.src || "";
1743
+ if (!targetSource) return;
1744
+ await downloadAnnotatedImage2(targetSource, strokes, {
1660
1745
  format: "image/png",
1661
1746
  includeAnnotations: true,
1662
1747
  fileName: `tbib-gallery-${currentIndex + 1}-annotated.png`
1663
1748
  });
1664
1749
  };
1665
1750
  const handleDownloadClean = async () => {
1666
- if (!imgRef.current) return;
1667
- await downloadAnnotatedImage2(imgRef.current, [], {
1751
+ const targetSource = effectiveSrc || currentItem?.src || imgRef.current?.src || "";
1752
+ if (!targetSource) return;
1753
+ await downloadAnnotatedImage2(targetSource, [], {
1668
1754
  format: "image/png",
1669
1755
  includeAnnotations: false,
1670
1756
  fileName: `tbib-gallery-${currentIndex + 1}-clean.png`
@@ -1790,8 +1876,11 @@ var ImageGalleryLightbox = ({
1790
1876
  activeTool,
1791
1877
  color,
1792
1878
  strokeWidth,
1879
+ currentScale: transform.scale,
1793
1880
  onStrokeComplete: handleStrokeComplete,
1794
- onEraseStroke: handleEraseStroke
1881
+ onEraseStroke: handleEraseStroke,
1882
+ onPinchZoom: pinchZoomByRatio,
1883
+ onPinchPan: panBy
1795
1884
  }
1796
1885
  )
1797
1886
  ]