labellife-design-tool 2.3.8 → 2.3.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.
package/dist/esm/index.js CHANGED
@@ -45,7 +45,7 @@ import FormatAlignLeftIcon from '@mui/icons-material/FormatAlignLeft';
45
45
  import FormatAlignCenterIcon from '@mui/icons-material/FormatAlignCenter';
46
46
  import FormatAlignRightIcon from '@mui/icons-material/FormatAlignRight';
47
47
  import OpacityIcon from '@mui/icons-material/Opacity';
48
- import '@mui/icons-material/Crop';
48
+ import CropIcon from '@mui/icons-material/Crop';
49
49
  import AutoAwesomeIcon from '@mui/icons-material/AutoAwesome';
50
50
  import SwapHorizIcon from '@mui/icons-material/SwapHoriz';
51
51
  import SwapVertIcon from '@mui/icons-material/SwapVert';
@@ -61,7 +61,7 @@ import VerticalAlignBottomIcon from '@mui/icons-material/VerticalAlignBottom';
61
61
  import ZoomInIcon from '@mui/icons-material/ZoomIn';
62
62
  import ZoomOutIcon from '@mui/icons-material/ZoomOut';
63
63
  import FitScreenIcon from '@mui/icons-material/FitScreen';
64
- import { Image as Image$1, Group, Rect, Text, Arrow, Path, RegularPolygon, Star, Circle, Line, Shape as Shape$1, Transformer, Stage, Layer } from 'react-konva';
64
+ import { Group, Image as Image$1, Rect, Transformer, Text, Arrow, Path, RegularPolygon, Star, Circle, Line, Shape as Shape$1, Stage, Layer } from 'react-konva';
65
65
  import Konva from 'konva';
66
66
  import ArrowBackIcon from '@mui/icons-material/ArrowBack';
67
67
 
@@ -631,6 +631,84 @@ var Shape = Node.named('Shape').props({
631
631
  };
632
632
  });
633
633
 
634
+ // Cover-fit crop fractions (centered) for a box of given size over an image of natural size.
635
+ // Mirrors the existing auto-crop calculation in canvas/image-element.js.
636
+ function coverFitCrop(boxWidth, boxHeight, naturalWidth, naturalHeight) {
637
+ var boxAspect = boxWidth / boxHeight;
638
+ var imgAspect = naturalWidth / naturalHeight;
639
+ if (imgAspect > boxAspect) {
640
+ var _cw = naturalHeight * boxAspect;
641
+ return {
642
+ cropX: (naturalWidth - _cw) / 2 / naturalWidth,
643
+ cropY: 0,
644
+ cropWidth: _cw / naturalWidth,
645
+ cropHeight: 1
646
+ };
647
+ }
648
+ var ch = naturalWidth / boxAspect;
649
+ return {
650
+ cropX: 0,
651
+ cropY: (naturalHeight - ch) / 2 / naturalHeight,
652
+ cropWidth: 1,
653
+ cropHeight: ch / naturalHeight
654
+ };
655
+ }
656
+
657
+ // Whether stored crop fields represent an explicit (non-default) manual crop.
658
+ function hasManualCrop(cropX, cropY, cropWidth, cropHeight) {
659
+ return cropX !== 0 || cropY !== 0 || cropWidth !== 1 || cropHeight !== 1;
660
+ }
661
+
662
+ // Convert a crop (fractions of natural image size) + target rect (page units) into an
663
+ // image position/scale such that the crop region renders exactly under the rect.
664
+ function cropToImageTransform(crop, rect, naturalWidth, naturalHeight) {
665
+ var naturalX = crop.cropX * naturalWidth;
666
+ var naturalY = crop.cropY * naturalHeight;
667
+ var naturalW = crop.cropWidth * naturalWidth;
668
+ var scale = naturalW > 0 ? rect.width / naturalW : 1;
669
+ return {
670
+ scale: scale,
671
+ offset: {
672
+ x: rect.x - naturalX * scale,
673
+ y: rect.y - naturalY * scale
674
+ }
675
+ };
676
+ }
677
+
678
+ // Inverse: given the image's page-space position/scale and a rect, compute crop fractions.
679
+ function rectToCrop(rect, imgOffset, imgScale, naturalWidth, naturalHeight) {
680
+ var naturalX = (rect.x - imgOffset.x) / imgScale;
681
+ var naturalY = (rect.y - imgOffset.y) / imgScale;
682
+ var naturalW = rect.width / imgScale;
683
+ var naturalH = rect.height / imgScale;
684
+ return {
685
+ cropX: naturalX / naturalWidth,
686
+ cropY: naturalY / naturalHeight,
687
+ cropWidth: naturalW / naturalWidth,
688
+ cropHeight: naturalH / naturalHeight
689
+ };
690
+ }
691
+
692
+ // Minimum scale at which the image (rendered at naturalWidth*scale x naturalHeight*scale)
693
+ // still fully covers rect.
694
+ function minScaleForRect(rect, naturalWidth, naturalHeight) {
695
+ return Math.max(rect.width / naturalWidth, rect.height / naturalHeight);
696
+ }
697
+
698
+ // Clamp an image page-space offset so the image still fully covers rect at the given scale.
699
+ function clampImageOffset(offset, scale, rect, naturalWidth, naturalHeight) {
700
+ var imgW = naturalWidth * scale;
701
+ var imgH = naturalHeight * scale;
702
+ var minX = rect.x + rect.width - imgW;
703
+ var maxX = rect.x;
704
+ var minY = rect.y + rect.height - imgH;
705
+ var maxY = rect.y;
706
+ return {
707
+ x: Math.min(maxX, Math.max(minX, offset.x)),
708
+ y: Math.min(maxY, Math.max(minY, offset.y))
709
+ };
710
+ }
711
+
634
712
  var ImageElement = Shape.named('ImageElement').props({
635
713
  type: types.optional(types.string, 'image'),
636
714
  src: types.optional(types.string, ''),
@@ -664,12 +742,132 @@ var ImageElement = Shape.named('ImageElement').props({
664
742
  return s;
665
743
  })["volatile"](function () {
666
744
  return {
667
- _cropModeEnabled: false
745
+ // Set once, when the image finishes loading (see canvas/image-element.js).
746
+ _naturalWidth: 0,
747
+ _naturalHeight: 0,
748
+ // Crop-mode editing state — all null/default when not cropping.
749
+ _cropModeEnabled: false,
750
+ _cropEntrySnapshot: null,
751
+ _cropRect: null,
752
+ _cropImgOffset: null,
753
+ _cropImgScale: 1,
754
+ _cropNaturalWidth: 0,
755
+ _cropNaturalHeight: 0
668
756
  };
669
757
  }).actions(function (self) {
670
758
  return {
671
- toggleCropMode: function toggleCropMode(flag) {
672
- self._cropModeEnabled = flag !== undefined ? flag : !self._cropModeEnabled;
759
+ setNaturalSize: function setNaturalSize(width, height) {
760
+ self._naturalWidth = width;
761
+ self._naturalHeight = height;
762
+ },
763
+ enterCropMode: function enterCropMode() {
764
+ var naturalWidth = self._naturalWidth;
765
+ var naturalHeight = self._naturalHeight;
766
+ if (!naturalWidth || !naturalHeight) return;
767
+ if (self.rotation !== 0 || self.flipX || self.flipY) return;
768
+ self._cropEntrySnapshot = {
769
+ x: self.x,
770
+ y: self.y,
771
+ width: self.width,
772
+ height: self.height,
773
+ cropX: self.cropX,
774
+ cropY: self.cropY,
775
+ cropWidth: self.cropWidth,
776
+ cropHeight: self.cropHeight
777
+ };
778
+ var rect = {
779
+ x: self.x,
780
+ y: self.y,
781
+ width: self.width,
782
+ height: self.height
783
+ };
784
+ var crop = hasManualCrop(self.cropX, self.cropY, self.cropWidth, self.cropHeight) ? {
785
+ cropX: self.cropX,
786
+ cropY: self.cropY,
787
+ cropWidth: self.cropWidth,
788
+ cropHeight: self.cropHeight
789
+ } : coverFitCrop(rect.width, rect.height, naturalWidth, naturalHeight);
790
+ var _cropToImageTransform = cropToImageTransform(crop, rect, naturalWidth, naturalHeight),
791
+ scale = _cropToImageTransform.scale,
792
+ offset = _cropToImageTransform.offset;
793
+ self._cropRect = rect;
794
+ self._cropImgOffset = offset;
795
+ self._cropImgScale = scale;
796
+ self._cropNaturalWidth = naturalWidth;
797
+ self._cropNaturalHeight = naturalHeight;
798
+ self._cropModeEnabled = true;
799
+ },
800
+ setCropRectAndClamp: function setCropRectAndClamp(rect) {
801
+ // Defensive guard (not in the original brief text): without this, a caller invoking
802
+ // this action while crop mode is inactive (e.g. enterCropMode() no-op'd because the
803
+ // image hadn't loaded yet) would hit minScaleForRect/clampImageOffset with
804
+ // _cropNaturalWidth/_cropNaturalHeight still at their 0 defaults, producing
805
+ // Infinity/NaN. Crop mode is only ever active with a nonzero natural size (see
806
+ // enterCropMode's guard above), so gating on _cropModeEnabled here is equivalent to
807
+ // gating on nonzero natural size, without changing behavior for the normal case.
808
+ if (!self._cropModeEnabled) return;
809
+ self._cropRect = rect;
810
+ var minScale = minScaleForRect(rect, self._cropNaturalWidth, self._cropNaturalHeight);
811
+ if (self._cropImgScale < minScale) {
812
+ self._cropImgScale = minScale;
813
+ }
814
+ self._cropImgOffset = clampImageOffset(self._cropImgOffset || {
815
+ x: rect.x,
816
+ y: rect.y
817
+ }, self._cropImgScale, rect, self._cropNaturalWidth, self._cropNaturalHeight);
818
+ },
819
+ panCropImage: function panCropImage(offset) {
820
+ if (!self._cropRect) return;
821
+ self._cropImgOffset = clampImageOffset(offset, self._cropImgScale, self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight);
822
+ },
823
+ setCropZoom: function setCropZoom(scale) {
824
+ if (!self._cropRect) return;
825
+ var minScale = minScaleForRect(self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight);
826
+ var clampedScale = Math.max(minScale, scale);
827
+ self._cropImgScale = clampedScale;
828
+ self._cropImgOffset = clampImageOffset(self._cropImgOffset || {
829
+ x: self._cropRect.x,
830
+ y: self._cropRect.y
831
+ }, clampedScale, self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight);
832
+ },
833
+ resetCropToDefault: function resetCropToDefault() {
834
+ if (!self._cropRect) return;
835
+ var crop = coverFitCrop(self._cropRect.width, self._cropRect.height, self._cropNaturalWidth, self._cropNaturalHeight);
836
+ var _cropToImageTransform2 = cropToImageTransform(crop, self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight),
837
+ scale = _cropToImageTransform2.scale,
838
+ offset = _cropToImageTransform2.offset;
839
+ self._cropImgScale = scale;
840
+ self._cropImgOffset = offset;
841
+ },
842
+ cancelCropMode: function cancelCropMode() {
843
+ if (self._cropEntrySnapshot) {
844
+ self.set(self._cropEntrySnapshot);
845
+ }
846
+ self._cropEntrySnapshot = null;
847
+ self._cropRect = null;
848
+ self._cropImgOffset = null;
849
+ self._cropImgScale = 1;
850
+ self._cropModeEnabled = false;
851
+ },
852
+ commitCropMode: function commitCropMode() {
853
+ if (self._cropRect && self._cropImgOffset) {
854
+ var crop = rectToCrop(self._cropRect, self._cropImgOffset, self._cropImgScale, self._cropNaturalWidth, self._cropNaturalHeight);
855
+ self.set({
856
+ x: self._cropRect.x,
857
+ y: self._cropRect.y,
858
+ width: self._cropRect.width,
859
+ height: self._cropRect.height,
860
+ cropX: crop.cropX,
861
+ cropY: crop.cropY,
862
+ cropWidth: crop.cropWidth,
863
+ cropHeight: crop.cropHeight
864
+ });
865
+ }
866
+ self._cropEntrySnapshot = null;
867
+ self._cropRect = null;
868
+ self._cropImgOffset = null;
869
+ self._cropImgScale = 1;
870
+ self._cropModeEnabled = false;
673
871
  }
674
872
  };
675
873
  });
@@ -1764,10 +1962,22 @@ var Store = types.model('Store', {
1764
1962
  if (self._toolbarPanel) {
1765
1963
  var stillSelected = ids.includes(self._toolbarPanel.elementId);
1766
1964
  if (!stillSelected) {
1965
+ self._cancelActiveCrop();
1767
1966
  self._toolbarPanel = null;
1768
1967
  }
1769
1968
  }
1770
1969
  },
1970
+ // Single choke-point for "cancel whatever element is mid-crop, if any" — must be
1971
+ // called before _toolbarPanel is overwritten/cleared by any path (panel switch,
1972
+ // panel close, or selection change) so an element can never be left stuck in
1973
+ // crop mode with no matching exit trigger.
1974
+ _cancelActiveCrop: function _cancelActiveCrop() {
1975
+ var _self$_toolbarPanel;
1976
+ if (((_self$_toolbarPanel = self._toolbarPanel) === null || _self$_toolbarPanel === void 0 ? void 0 : _self$_toolbarPanel.type) === 'crop') {
1977
+ var el = self.getElementById(self._toolbarPanel.elementId);
1978
+ if (el && el._cropModeEnabled && el.cancelCropMode) el.cancelCropMode();
1979
+ }
1980
+ },
1771
1981
  toggleBleed: function toggleBleed(bleedVisible) {
1772
1982
  self.bleedVisible = bleedVisible !== undefined ? bleedVisible : !self.bleedVisible;
1773
1983
  },
@@ -1784,6 +1994,7 @@ var Store = types.model('Store', {
1784
1994
  openToolbarPanel: function openToolbarPanel(type) {
1785
1995
  var el = self.selectedElements.length === 1 ? self.selectedElements[0] : null;
1786
1996
  if (el) {
1997
+ self._cancelActiveCrop();
1787
1998
  self._toolbarPanel = {
1788
1999
  type: type,
1789
2000
  elementId: el.id
@@ -1791,6 +2002,7 @@ var Store = types.model('Store', {
1791
2002
  }
1792
2003
  },
1793
2004
  closeToolbarPanel: function closeToolbarPanel() {
2005
+ self._cancelActiveCrop();
1794
2006
  self._toolbarPanel = null;
1795
2007
  },
1796
2008
  setScale: function setScale(scale) {
@@ -6276,6 +6488,115 @@ var ImageMaskPanel = observer(function (_ref) {
6276
6488
  });
6277
6489
  ImageMaskPanel.displayName = 'ImageMaskPanel';
6278
6490
 
6491
+ var ImageCropPanel = observer(function (_ref) {
6492
+ var store = _ref.store;
6493
+ var panel = store._toolbarPanel;
6494
+ if (!panel || panel.type !== 'crop') return null;
6495
+ var element = store.getElementById(panel.elementId);
6496
+ if (!element || !element._cropRect) return null;
6497
+ var handleCancel = function handleCancel() {
6498
+ element.cancelCropMode();
6499
+ store.closeToolbarPanel();
6500
+ };
6501
+ var handleDone = function handleDone() {
6502
+ element.commitCropMode();
6503
+ store.closeToolbarPanel();
6504
+ store.history.addUndoState();
6505
+ };
6506
+ var handleReset = function handleReset() {
6507
+ element.resetCropToDefault();
6508
+ };
6509
+ var minScale = minScaleForRect(element._cropRect, element._cropNaturalWidth, element._cropNaturalHeight);
6510
+ var maxScale = minScale * 4;
6511
+ return /*#__PURE__*/jsxs(Box, {
6512
+ sx: {
6513
+ width: 280,
6514
+ height: '100%',
6515
+ overflowY: 'auto',
6516
+ overflowX: 'hidden',
6517
+ backgroundColor: '#fff',
6518
+ borderRight: '1px solid #e0e0e0',
6519
+ flexShrink: 0,
6520
+ p: 2
6521
+ },
6522
+ children: [/*#__PURE__*/jsxs(Box, {
6523
+ sx: {
6524
+ display: 'flex',
6525
+ alignItems: 'center',
6526
+ justifyContent: 'space-between',
6527
+ mb: 2
6528
+ },
6529
+ children: [/*#__PURE__*/jsx(Typography, {
6530
+ variant: "subtitle1",
6531
+ sx: {
6532
+ fontWeight: 600
6533
+ },
6534
+ children: t('toolbar.crop', 'Crop')
6535
+ }), /*#__PURE__*/jsx(IconButton, {
6536
+ size: "small",
6537
+ onClick: handleCancel,
6538
+ children: /*#__PURE__*/jsx(CloseIcon, {
6539
+ fontSize: "small"
6540
+ })
6541
+ })]
6542
+ }), /*#__PURE__*/jsx(Typography, {
6543
+ variant: "body2",
6544
+ sx: {
6545
+ fontWeight: 500,
6546
+ mb: 0.5
6547
+ },
6548
+ children: t('toolbar.cropZoom', 'Zoom')
6549
+ }), /*#__PURE__*/jsx(Slider, {
6550
+ size: "small",
6551
+ value: element._cropImgScale,
6552
+ onChange: function onChange(_, v) {
6553
+ return element.setCropZoom(v);
6554
+ },
6555
+ min: minScale,
6556
+ max: maxScale,
6557
+ step: (maxScale - minScale) / 100 || 0.01,
6558
+ sx: {
6559
+ mb: 3
6560
+ }
6561
+ }), /*#__PURE__*/jsx(Button, {
6562
+ fullWidth: true,
6563
+ variant: "outlined",
6564
+ size: "small",
6565
+ onClick: handleReset,
6566
+ sx: {
6567
+ textTransform: 'none',
6568
+ mb: 3
6569
+ },
6570
+ children: t('toolbar.cropReset', 'Reset')
6571
+ }), /*#__PURE__*/jsxs(Box, {
6572
+ sx: {
6573
+ display: 'flex',
6574
+ gap: 1
6575
+ },
6576
+ children: [/*#__PURE__*/jsx(Button, {
6577
+ fullWidth: true,
6578
+ variant: "outlined",
6579
+ size: "small",
6580
+ onClick: handleCancel,
6581
+ sx: {
6582
+ textTransform: 'none'
6583
+ },
6584
+ children: t('toolbar.cropCancel', 'Cancel')
6585
+ }), /*#__PURE__*/jsx(Button, {
6586
+ fullWidth: true,
6587
+ variant: "contained",
6588
+ size: "small",
6589
+ onClick: handleDone,
6590
+ sx: {
6591
+ textTransform: 'none'
6592
+ },
6593
+ children: t('toolbar.cropDone', 'Done')
6594
+ })]
6595
+ })]
6596
+ });
6597
+ });
6598
+ ImageCropPanel.displayName = 'ImageCropPanel';
6599
+
6279
6600
  var SliderRow = function SliderRow(_ref) {
6280
6601
  var label = _ref.label,
6281
6602
  value = _ref.value,
@@ -6801,6 +7122,8 @@ var SidePanel = observer(function (_ref) {
6801
7122
  });
6802
7123
  }() : store._toolbarPanel.type === 'mask' ? /*#__PURE__*/jsx(ImageMaskPanel, {
6803
7124
  store: store
7125
+ }) : store._toolbarPanel.type === 'crop' ? /*#__PURE__*/jsx(ImageCropPanel, {
7126
+ store: store
6804
7127
  }) : null : (/* Normal panel content */
6805
7128
  activeSectionObj && /*#__PURE__*/jsx(Box, {
6806
7129
  sx: {
@@ -8879,6 +9202,13 @@ var ImageToolbar = observer(function (_ref5) {
8879
9202
  store.history.addUndoState();
8880
9203
  setFlipAnchor(null);
8881
9204
  };
9205
+ var handleCrop = function handleCrop() {
9206
+ if (!element._naturalWidth || !element._naturalHeight) return;
9207
+ element.enterCropMode();
9208
+ if (element._cropModeEnabled) {
9209
+ store.openToolbarPanel('crop');
9210
+ }
9211
+ };
8882
9212
  var handleFitToPage = function handleFitToPage() {
8883
9213
  element.set({
8884
9214
  x: 0,
@@ -9008,6 +9338,16 @@ var ImageToolbar = observer(function (_ref5) {
9008
9338
  } : {}),
9009
9339
  onClick: handleMask,
9010
9340
  children: hasMask ? t('toolbar.removeMask', 'Remove mask') : t('toolbar.clip', 'Apply mask')
9341
+ }), /*#__PURE__*/jsx(Button, {
9342
+ size: "small",
9343
+ sx: toolbarBtnSx,
9344
+ startIcon: /*#__PURE__*/jsx(CropIcon, {
9345
+ sx: {
9346
+ fontSize: '16px !important'
9347
+ }
9348
+ }),
9349
+ onClick: handleCrop,
9350
+ children: t('toolbar.crop', 'Crop')
9011
9351
  })]
9012
9352
  });
9013
9353
  });
@@ -10177,6 +10517,159 @@ var ZoomButtons = observer(function (_ref) {
10177
10517
  });
10178
10518
  ZoomButtons.displayName = 'ZoomButtons';
10179
10519
 
10520
+ // src/canvas/crop-editor.js
10521
+ var MIN_RECT_SIZE = 20;
10522
+ var VIEWPORT_PAD = 400; // page units of dim area rendered around the crop rect
10523
+
10524
+ var CropEditor = observer(function (_ref) {
10525
+ var element = _ref.element,
10526
+ image = _ref.image;
10527
+ var rectRef = useRef(null);
10528
+ var transformerRef = useRef(null);
10529
+ useEffect(function () {
10530
+ if (transformerRef.current && rectRef.current) {
10531
+ var _transformerRef$curre;
10532
+ transformerRef.current.nodes([rectRef.current]);
10533
+ (_transformerRef$curre = transformerRef.current.getLayer()) === null || _transformerRef$curre === void 0 || _transformerRef$curre.batchDraw();
10534
+ }
10535
+ });
10536
+ if (!image || !element._cropRect || !element._cropImgOffset) return null;
10537
+ var natW = image.naturalWidth || image.width;
10538
+ var natH = image.naturalHeight || image.height;
10539
+ var rect = element._cropRect;
10540
+ var offset = element._cropImgOffset;
10541
+ var scale = element._cropImgScale;
10542
+ var handleImageDragMove = function handleImageDragMove(e) {
10543
+ var node = e.target;
10544
+ var clamped = clampImageOffset({
10545
+ x: node.x(),
10546
+ y: node.y()
10547
+ }, scale, rect, natW, natH);
10548
+ node.x(clamped.x);
10549
+ node.y(clamped.y);
10550
+ };
10551
+ var handleImageDragEnd = function handleImageDragEnd(e) {
10552
+ var node = e.target;
10553
+ element.panCropImage({
10554
+ x: node.x(),
10555
+ y: node.y()
10556
+ });
10557
+ };
10558
+ var handleRectDragEnd = function handleRectDragEnd(e) {
10559
+ var node = e.target;
10560
+ element.setCropRectAndClamp({
10561
+ x: node.x(),
10562
+ y: node.y(),
10563
+ width: rect.width,
10564
+ height: rect.height
10565
+ });
10566
+ };
10567
+
10568
+ // Shared by both the live (onTransform) and final (onTransformEnd) handlers: reads the
10569
+ // Konva node's current scale, bakes it into width/height, resets the node's scale back to
10570
+ // 1 (required for the Transformer to keep computing subsequent ticks correctly within the
10571
+ // same drag), and pushes the new rect through the model so the image/mask stay in sync.
10572
+ var applyRectTransform = function applyRectTransform() {
10573
+ var node = rectRef.current;
10574
+ var scaleX = node.scaleX();
10575
+ var scaleY = node.scaleY();
10576
+ var newRect = {
10577
+ x: node.x(),
10578
+ y: node.y(),
10579
+ width: Math.max(MIN_RECT_SIZE, rect.width * scaleX),
10580
+ height: Math.max(MIN_RECT_SIZE, rect.height * scaleY)
10581
+ };
10582
+ node.scaleX(1);
10583
+ node.scaleY(1);
10584
+ node.width(newRect.width);
10585
+ node.height(newRect.height);
10586
+ element.setCropRectAndClamp(newRect);
10587
+ };
10588
+ var handleRectTransform = function handleRectTransform() {
10589
+ return applyRectTransform();
10590
+ };
10591
+ var handleRectTransformEnd = function handleRectTransformEnd() {
10592
+ return applyRectTransform();
10593
+ };
10594
+
10595
+ // Dim mask: four rects framing the hole over `rect`.
10596
+ var vx = rect.x - VIEWPORT_PAD;
10597
+ var vy = rect.y - VIEWPORT_PAD;
10598
+ var vw = rect.width + VIEWPORT_PAD * 2;
10599
+ var vh = rect.height + VIEWPORT_PAD * 2;
10600
+ return /*#__PURE__*/jsxs(Group, {
10601
+ children: [/*#__PURE__*/jsx(Image$1, {
10602
+ name: "display-only crop-interactive",
10603
+ image: image,
10604
+ x: offset.x,
10605
+ y: offset.y,
10606
+ width: natW * scale,
10607
+ height: natH * scale,
10608
+ draggable: true,
10609
+ onDragMove: handleImageDragMove,
10610
+ onDragEnd: handleImageDragEnd
10611
+ }), /*#__PURE__*/jsx(Rect, {
10612
+ name: "display-only",
10613
+ x: vx,
10614
+ y: vy,
10615
+ width: vw,
10616
+ height: rect.y - vy,
10617
+ fill: "rgba(0,0,0,0.55)",
10618
+ listening: false
10619
+ }), /*#__PURE__*/jsx(Rect, {
10620
+ name: "display-only",
10621
+ x: vx,
10622
+ y: rect.y + rect.height,
10623
+ width: vw,
10624
+ height: vy + vh - (rect.y + rect.height),
10625
+ fill: "rgba(0,0,0,0.55)",
10626
+ listening: false
10627
+ }), /*#__PURE__*/jsx(Rect, {
10628
+ name: "display-only",
10629
+ x: vx,
10630
+ y: rect.y,
10631
+ width: rect.x - vx,
10632
+ height: rect.height,
10633
+ fill: "rgba(0,0,0,0.55)",
10634
+ listening: false
10635
+ }), /*#__PURE__*/jsx(Rect, {
10636
+ name: "display-only",
10637
+ x: rect.x + rect.width,
10638
+ y: rect.y,
10639
+ width: vx + vw - (rect.x + rect.width),
10640
+ height: rect.height,
10641
+ fill: "rgba(0,0,0,0.55)",
10642
+ listening: false
10643
+ }), /*#__PURE__*/jsx(Rect, {
10644
+ name: "display-only crop-interactive",
10645
+ ref: rectRef,
10646
+ x: rect.x,
10647
+ y: rect.y,
10648
+ width: rect.width,
10649
+ height: rect.height,
10650
+ stroke: "#ffffff",
10651
+ strokeWidth: 1.5,
10652
+ draggable: true,
10653
+ onDragEnd: handleRectDragEnd,
10654
+ onTransform: handleRectTransform,
10655
+ onTransformEnd: handleRectTransformEnd
10656
+ }), /*#__PURE__*/jsx(Transformer, {
10657
+ ref: transformerRef,
10658
+ rotateEnabled: false,
10659
+ anchorSize: 8,
10660
+ anchorCornerRadius: 2,
10661
+ borderStroke: "#0d83cd",
10662
+ anchorStroke: "#0d83cd",
10663
+ anchorFill: "#ffffff",
10664
+ boundBoxFunc: function boundBoxFunc(oldBox, newBox) {
10665
+ if (newBox.width < MIN_RECT_SIZE || newBox.height < MIN_RECT_SIZE) return oldBox;
10666
+ return newBox;
10667
+ }
10668
+ })]
10669
+ });
10670
+ });
10671
+ CropEditor.displayName = 'CropEditor';
10672
+
10180
10673
  var SepiaFilter = function SepiaFilter(imageData) {
10181
10674
  var data = imageData.data;
10182
10675
  for (var i = 0; i < data.length; i += 4) {
@@ -10214,7 +10707,8 @@ var ImageElementComponent = observer(function (_ref) {
10214
10707
  var onSelect = _ref.onSelect,
10215
10708
  onTransformEnd = _ref.onTransformEnd,
10216
10709
  onDragEnd = _ref.onDragEnd,
10217
- groupChild = _ref.groupChild;
10710
+ groupChild = _ref.groupChild,
10711
+ store = _ref.store;
10218
10712
  var imageRef = useRef(null);
10219
10713
  var groupRef = useRef(null);
10220
10714
  var _useState = useState(null),
@@ -10236,6 +10730,7 @@ var ImageElementComponent = observer(function (_ref) {
10236
10730
  img.crossOrigin = 'anonymous';
10237
10731
  img.onload = function () {
10238
10732
  setImage(img);
10733
+ element.setNaturalSize(img.naturalWidth, img.naturalHeight);
10239
10734
  };
10240
10735
  img.onerror = function () {
10241
10736
  console.warn('Failed to load image:', element.src.substring(0, 100));
@@ -10270,6 +10765,20 @@ var ImageElementComponent = observer(function (_ref) {
10270
10765
  }
10271
10766
  }, [image, needsCache, element.blurEnabled, element.blurRadius, element.brightnessEnabled, element.brightness, element.grayscaleEnabled, element.sepiaEnabled, element.warmEnabled, element.coldEnabled, element.cornerRadius, element.shadowEnabled, element.shadowBlur, element.shadowOffsetX, element.shadowOffsetY, element.shadowColor]);
10272
10767
  if (!element.visible) return null;
10768
+ if (element._cropModeEnabled) {
10769
+ return /*#__PURE__*/jsx(CropEditor, {
10770
+ element: element,
10771
+ image: image
10772
+ });
10773
+ }
10774
+ var handleDblClick = function handleDblClick() {
10775
+ if (!store || groupChild) return;
10776
+ element.enterCropMode();
10777
+ if (element._cropModeEnabled) {
10778
+ store.selectElements([element.id]);
10779
+ store.openToolbarPanel('crop');
10780
+ }
10781
+ };
10273
10782
  var handleDragEnd = function handleDragEnd(e) {
10274
10783
  var target = groupRef.current || e.target;
10275
10784
  element.set({
@@ -10510,6 +11019,8 @@ var ImageElementComponent = observer(function (_ref) {
10510
11019
  onDragEnd: handleDragEnd,
10511
11020
  onTransform: handleTransform,
10512
11021
  onTransformEnd: handleTransformEnd,
11022
+ onDblClick: handleDblClick,
11023
+ onDblTap: handleDblClick,
10513
11024
  children: [imageNode, hasBorder && /*#__PURE__*/jsx(Rect, {
10514
11025
  x: 0,
10515
11026
  y: 0,
@@ -11310,7 +11821,8 @@ var ElementRenderer = observer(function (_ref) {
11310
11821
  onSelect = _ref.onSelect,
11311
11822
  onTransformEnd = _ref.onTransformEnd,
11312
11823
  onDragEnd = _ref.onDragEnd,
11313
- groupChild = _ref.groupChild;
11824
+ groupChild = _ref.groupChild,
11825
+ store = _ref.store;
11314
11826
  var Renderer = ELEMENT_RENDERERS[element.type];
11315
11827
  if (!Renderer) {
11316
11828
  console.warn("No renderer for element type: ".concat(element.type));
@@ -11327,7 +11839,8 @@ var ElementRenderer = observer(function (_ref) {
11327
11839
  onSelect: onSelect,
11328
11840
  onTransformEnd: onTransformEnd,
11329
11841
  onDragEnd: onDragEnd,
11330
- groupChild: groupChild
11842
+ groupChild: groupChild,
11843
+ store: store
11331
11844
  }, extra));
11332
11845
  });
11333
11846
  ElementRenderer.displayName = 'ElementRenderer';
@@ -12495,10 +13008,11 @@ var SelectionTransformer = observer(function (_ref5) {
12495
13008
  var transformerRef = useRef(null);
12496
13009
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
12497
13010
 
12498
- // Filter out line elements — they use custom endpoint handles instead
13011
+ // Filter out line elements (custom endpoint handles) and elements mid-crop
13012
+ // (CropEditor renders its own Transformer bound to the crop rect instead).
12499
13013
  var nonLineIds = selectedIds.filter(function (id) {
12500
13014
  var el = store.getElementById(id);
12501
- return el && el.type !== 'line';
13015
+ return el && el.type !== 'line' && !el._cropModeEnabled;
12502
13016
  });
12503
13017
  var allText = nonLineIds.length === 1 && function () {
12504
13018
  var el = store.getElementById(nonLineIds[0]);
@@ -12612,7 +13126,25 @@ var Workspace = observer(function (_ref6) {
12612
13126
  }
12613
13127
  }, [stageRef.current]);
12614
13128
  var handleStageMouseDown = useCallback(function (e) {
12615
- var _target$attrs, _e$evt, _e$evt2;
13129
+ var _store$_toolbarPanel, _target$attrs, _e$evt, _e$evt2;
13130
+ // Clicking anywhere outside the crop rect / pannable image / resize
13131
+ // handles while cropping finishes the crop (like Polotno), then falls
13132
+ // through to the click's normal handling (select whatever was actually
13133
+ // clicked, or deselect).
13134
+ if (((_store$_toolbarPanel = store._toolbarPanel) === null || _store$_toolbarPanel === void 0 ? void 0 : _store$_toolbarPanel.type) === 'crop') {
13135
+ var _e$target$getParent, _e$target;
13136
+ var croppingEl = store.getElementById(store._toolbarPanel.elementId);
13137
+ var isCropInteractive = typeof e.target.hasName === 'function' && e.target.hasName('crop-interactive');
13138
+ // Konva's Transformer adds its resize handles as direct children of
13139
+ // itself, so a handle's parent's className is 'Transformer' — this
13140
+ // catches drags on the crop rect's resize/rotate handles.
13141
+ var isTransformerHandle = ((_e$target$getParent = (_e$target = e.target).getParent) === null || _e$target$getParent === void 0 || (_e$target$getParent = _e$target$getParent.call(_e$target)) === null || _e$target$getParent === void 0 ? void 0 : _e$target$getParent.className) === 'Transformer';
13142
+ if (croppingEl && croppingEl._cropModeEnabled && !isCropInteractive && !isTransformerHandle) {
13143
+ croppingEl.commitCropMode();
13144
+ store.closeToolbarPanel();
13145
+ store.history.addUndoState();
13146
+ }
13147
+ }
12616
13148
  var target = e.target;
12617
13149
 
12618
13150
  // Click on empty area or background -> deselect
@@ -12661,12 +13193,21 @@ var Workspace = observer(function (_ref6) {
12661
13193
  // Keyboard shortcuts
12662
13194
  useEffect(function () {
12663
13195
  var handleKeyDown = function handleKeyDown(e) {
13196
+ var _store$_toolbarPanel2;
12664
13197
  // Only handle if stage container or body is focused
12665
13198
  var target = e.target;
12666
13199
  if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT') {
12667
13200
  return;
12668
13201
  }
12669
13202
 
13203
+ // Escape -> cancel crop mode if active
13204
+ if (e.key === 'Escape' && ((_store$_toolbarPanel2 = store._toolbarPanel) === null || _store$_toolbarPanel2 === void 0 ? void 0 : _store$_toolbarPanel2.type) === 'crop') {
13205
+ var el = store.getElementById(store._toolbarPanel.elementId);
13206
+ if (el && el.cancelCropMode) el.cancelCropMode();
13207
+ store.closeToolbarPanel();
13208
+ e.preventDefault();
13209
+ }
13210
+
12670
13211
  // Delete / Backspace
12671
13212
  if (e.key === 'Delete' || e.key === 'Backspace') {
12672
13213
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
@@ -12729,9 +13270,9 @@ var Workspace = observer(function (_ref6) {
12729
13270
  if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'G') {
12730
13271
  var _selectedIds3 = _toConsumableArray(store.selectedElementsIds);
12731
13272
  if (_selectedIds3.length === 1) {
12732
- var el = store.getElementById(_selectedIds3[0]);
12733
- if (el && el.type === 'group') {
12734
- store.ungroupElements([el.id]);
13273
+ var _el = store.getElementById(_selectedIds3[0]);
13274
+ if (_el && _el.type === 'group') {
13275
+ store.ungroupElements([_el.id]);
12735
13276
  store.history.addUndoState();
12736
13277
  e.preventDefault();
12737
13278
  }
@@ -12827,7 +13368,8 @@ var Workspace = observer(function (_ref6) {
12827
13368
  element: element,
12828
13369
  isSelected: store.selectedElementsIds.includes(element.id),
12829
13370
  onTransformEnd: handleTransformEnd,
12830
- onDragEnd: handleDragEnd
13371
+ onDragEnd: handleDragEnd,
13372
+ store: store
12831
13373
  }, element.id);
12832
13374
  }), /*#__PURE__*/jsx(PageOverlayFrame, {
12833
13375
  store: store