labellife-design-tool 2.3.7 → 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
@@ -31,6 +31,7 @@ import NumbersIcon from '@mui/icons-material/Numbers';
31
31
  import RadioButtonCheckedIcon from '@mui/icons-material/RadioButtonChecked';
32
32
  import FormatListBulletedIcon from '@mui/icons-material/FormatListBulleted';
33
33
  import AddIcon from '@mui/icons-material/Add';
34
+ import '@mui/icons-material/AspectRatio';
34
35
  import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
35
36
  import FileUploadIcon from '@mui/icons-material/FileUpload';
36
37
  import UndoIcon from '@mui/icons-material/Undo';
@@ -44,7 +45,7 @@ import FormatAlignLeftIcon from '@mui/icons-material/FormatAlignLeft';
44
45
  import FormatAlignCenterIcon from '@mui/icons-material/FormatAlignCenter';
45
46
  import FormatAlignRightIcon from '@mui/icons-material/FormatAlignRight';
46
47
  import OpacityIcon from '@mui/icons-material/Opacity';
47
- import '@mui/icons-material/Crop';
48
+ import CropIcon from '@mui/icons-material/Crop';
48
49
  import AutoAwesomeIcon from '@mui/icons-material/AutoAwesome';
49
50
  import SwapHorizIcon from '@mui/icons-material/SwapHoriz';
50
51
  import SwapVertIcon from '@mui/icons-material/SwapVert';
@@ -60,7 +61,7 @@ import VerticalAlignBottomIcon from '@mui/icons-material/VerticalAlignBottom';
60
61
  import ZoomInIcon from '@mui/icons-material/ZoomIn';
61
62
  import ZoomOutIcon from '@mui/icons-material/ZoomOut';
62
63
  import FitScreenIcon from '@mui/icons-material/FitScreen';
63
- 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';
64
65
  import Konva from 'konva';
65
66
  import ArrowBackIcon from '@mui/icons-material/ArrowBack';
66
67
 
@@ -630,6 +631,84 @@ var Shape = Node.named('Shape').props({
630
631
  };
631
632
  });
632
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
+
633
712
  var ImageElement = Shape.named('ImageElement').props({
634
713
  type: types.optional(types.string, 'image'),
635
714
  src: types.optional(types.string, ''),
@@ -663,12 +742,132 @@ var ImageElement = Shape.named('ImageElement').props({
663
742
  return s;
664
743
  })["volatile"](function () {
665
744
  return {
666
- _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
667
756
  };
668
757
  }).actions(function (self) {
669
758
  return {
670
- toggleCropMode: function toggleCropMode(flag) {
671
- 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;
672
871
  }
673
872
  };
674
873
  });
@@ -1763,10 +1962,22 @@ var Store = types.model('Store', {
1763
1962
  if (self._toolbarPanel) {
1764
1963
  var stillSelected = ids.includes(self._toolbarPanel.elementId);
1765
1964
  if (!stillSelected) {
1965
+ self._cancelActiveCrop();
1766
1966
  self._toolbarPanel = null;
1767
1967
  }
1768
1968
  }
1769
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
+ },
1770
1981
  toggleBleed: function toggleBleed(bleedVisible) {
1771
1982
  self.bleedVisible = bleedVisible !== undefined ? bleedVisible : !self.bleedVisible;
1772
1983
  },
@@ -1783,6 +1994,7 @@ var Store = types.model('Store', {
1783
1994
  openToolbarPanel: function openToolbarPanel(type) {
1784
1995
  var el = self.selectedElements.length === 1 ? self.selectedElements[0] : null;
1785
1996
  if (el) {
1997
+ self._cancelActiveCrop();
1786
1998
  self._toolbarPanel = {
1787
1999
  type: type,
1788
2000
  elementId: el.id
@@ -1790,6 +2002,7 @@ var Store = types.model('Store', {
1790
2002
  }
1791
2003
  },
1792
2004
  closeToolbarPanel: function closeToolbarPanel() {
2005
+ self._cancelActiveCrop();
1793
2006
  self._toolbarPanel = null;
1794
2007
  },
1795
2008
  setScale: function setScale(scale) {
@@ -6275,6 +6488,115 @@ var ImageMaskPanel = observer(function (_ref) {
6275
6488
  });
6276
6489
  ImageMaskPanel.displayName = 'ImageMaskPanel';
6277
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
+
6278
6600
  var SliderRow = function SliderRow(_ref) {
6279
6601
  var label = _ref.label,
6280
6602
  value = _ref.value,
@@ -6800,6 +7122,8 @@ var SidePanel = observer(function (_ref) {
6800
7122
  });
6801
7123
  }() : store._toolbarPanel.type === 'mask' ? /*#__PURE__*/jsx(ImageMaskPanel, {
6802
7124
  store: store
7125
+ }) : store._toolbarPanel.type === 'crop' ? /*#__PURE__*/jsx(ImageCropPanel, {
7126
+ store: store
6803
7127
  }) : null : (/* Normal panel content */
6804
7128
  activeSectionObj && /*#__PURE__*/jsx(Box, {
6805
7129
  sx: {
@@ -8155,6 +8479,265 @@ var InputFieldsSection = {
8155
8479
  Panel: InputFieldsPanelContent
8156
8480
  };
8157
8481
 
8482
+ function toPx(value, unit) {
8483
+ var dpi = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 96;
8484
+ var v = Number(value);
8485
+ if (!v) return 0;
8486
+ switch ((unit || 'px').toLowerCase()) {
8487
+ case 'mm':
8488
+ return Math.round(v * dpi / 25.4);
8489
+ case 'cm':
8490
+ return Math.round(v * dpi / 2.54);
8491
+ case 'in':
8492
+ return Math.round(v * dpi);
8493
+ default:
8494
+ return Math.round(v);
8495
+ }
8496
+ }
8497
+ function scaleElements(children, ratioX, ratioY) {
8498
+ children.forEach(function (el) {
8499
+ var updates = {
8500
+ x: el.x * ratioX,
8501
+ y: el.y * ratioY,
8502
+ width: el.width * ratioX,
8503
+ height: el.height * ratioY
8504
+ };
8505
+ if (el.type === 'text') {
8506
+ updates.fontSize = el.fontSize * Math.min(ratioX, ratioY);
8507
+ }
8508
+ el.set(updates);
8509
+ if (el.type === 'group' && Array.isArray(el.children)) {
8510
+ scaleElements(el.children, ratioX, ratioY);
8511
+ }
8512
+ });
8513
+ }
8514
+ observer(function (_ref2) {
8515
+ var store = _ref2.store,
8516
+ fetchSizes = _ref2.fetchSizes;
8517
+ var _useState = useState(String(store.width)),
8518
+ _useState2 = _slicedToArray(_useState, 2),
8519
+ width = _useState2[0],
8520
+ setWidth = _useState2[1];
8521
+ var _useState3 = useState(String(store.height)),
8522
+ _useState4 = _slicedToArray(_useState3, 2),
8523
+ height = _useState4[0],
8524
+ setHeight = _useState4[1];
8525
+ var _useState5 = useState(false),
8526
+ _useState6 = _slicedToArray(_useState5, 2),
8527
+ scaleContent = _useState6[0],
8528
+ setScaleContent = _useState6[1];
8529
+ var _useState7 = useState([]),
8530
+ _useState8 = _slicedToArray(_useState7, 2),
8531
+ sizes = _useState8[0],
8532
+ setSizes = _useState8[1];
8533
+ var _useState9 = useState(false),
8534
+ _useState0 = _slicedToArray(_useState9, 2),
8535
+ loadingSizes = _useState0[0],
8536
+ setLoadingSizes = _useState0[1];
8537
+ var _useState1 = useState(false),
8538
+ _useState10 = _slicedToArray(_useState1, 2),
8539
+ sizesError = _useState10[0],
8540
+ setSizesError = _useState10[1];
8541
+
8542
+ // Keep inputs in sync when store dimensions change externally
8543
+ useEffect(function () {
8544
+ setWidth(String(store.width));
8545
+ setHeight(String(store.height));
8546
+ }, [store.width, store.height]);
8547
+ useEffect(function () {
8548
+ if (!fetchSizes) return;
8549
+ setLoadingSizes(true);
8550
+ setSizesError(false);
8551
+ fetchSizes().then(function (res) {
8552
+ var _res$data;
8553
+ return setSizes(((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.sizes) || []);
8554
+ })["catch"](function () {
8555
+ return setSizesError(true);
8556
+ })["finally"](function () {
8557
+ return setLoadingSizes(false);
8558
+ });
8559
+ }, []);
8560
+ var wNum = parseInt(width, 10);
8561
+ var hNum = parseInt(height, 10);
8562
+ var isValid = wNum > 0 && hNum > 0 && Number.isFinite(wNum) && Number.isFinite(hNum);
8563
+ var handleResize = function handleResize() {
8564
+ if (!isValid || !store.activePage) return;
8565
+ if (scaleContent) {
8566
+ var ratioX = wNum / store.width;
8567
+ var ratioY = hNum / store.height;
8568
+ scaleElements(store.activePage.children, ratioX, ratioY);
8569
+ }
8570
+ store.setSize(wNum, hNum);
8571
+ };
8572
+ var handlePresetClick = function handlePresetClick(size) {
8573
+ var dpi = size.dpi || 96;
8574
+ var w = toPx(size.width, size.unit, dpi);
8575
+ var h = toPx(size.height, size.unit, dpi);
8576
+ if (w > 0) setWidth(String(w));
8577
+ if (h > 0) setHeight(String(h));
8578
+ };
8579
+ return /*#__PURE__*/jsxs(Box, {
8580
+ sx: {
8581
+ p: 2,
8582
+ display: 'flex',
8583
+ flexDirection: 'column',
8584
+ gap: 2
8585
+ },
8586
+ children: [/*#__PURE__*/jsxs(Box, {
8587
+ children: [/*#__PURE__*/jsx(Typography, {
8588
+ sx: {
8589
+ fontWeight: 600,
8590
+ fontSize: 14,
8591
+ mb: 0.5
8592
+ },
8593
+ children: "Resize Canvas"
8594
+ }), /*#__PURE__*/jsxs(Typography, {
8595
+ sx: {
8596
+ fontSize: 12,
8597
+ color: '#888'
8598
+ },
8599
+ children: ["Current: ", store.width, " \xD7 ", store.height, " px"]
8600
+ })]
8601
+ }), /*#__PURE__*/jsxs(Box, {
8602
+ sx: {
8603
+ display: 'flex',
8604
+ gap: 1.5
8605
+ },
8606
+ children: [/*#__PURE__*/jsx(TextField, {
8607
+ label: "Width (px)",
8608
+ type: "number",
8609
+ size: "small",
8610
+ value: width,
8611
+ onChange: function onChange(e) {
8612
+ return setWidth(e.target.value);
8613
+ },
8614
+ inputProps: {
8615
+ min: 1,
8616
+ step: 1
8617
+ },
8618
+ error: !!width && (isNaN(wNum) || wNum <= 0),
8619
+ sx: {
8620
+ flex: 1
8621
+ }
8622
+ }), /*#__PURE__*/jsx(TextField, {
8623
+ label: "Height (px)",
8624
+ type: "number",
8625
+ size: "small",
8626
+ value: height,
8627
+ onChange: function onChange(e) {
8628
+ return setHeight(e.target.value);
8629
+ },
8630
+ inputProps: {
8631
+ min: 1,
8632
+ step: 1
8633
+ },
8634
+ error: !!height && (isNaN(hNum) || hNum <= 0),
8635
+ sx: {
8636
+ flex: 1
8637
+ }
8638
+ })]
8639
+ }), /*#__PURE__*/jsx(FormControlLabel, {
8640
+ control: /*#__PURE__*/jsx(Switch, {
8641
+ checked: scaleContent,
8642
+ onChange: function onChange(e) {
8643
+ return setScaleContent(e.target.checked);
8644
+ },
8645
+ size: "small"
8646
+ }),
8647
+ label: /*#__PURE__*/jsx(Typography, {
8648
+ sx: {
8649
+ fontSize: 13
8650
+ },
8651
+ children: "Scale content"
8652
+ })
8653
+ }), /*#__PURE__*/jsx(Button, {
8654
+ variant: "contained",
8655
+ disabled: !isValid || !store.activePage,
8656
+ onClick: handleResize,
8657
+ sx: {
8658
+ textTransform: 'uppercase',
8659
+ fontWeight: 700,
8660
+ letterSpacing: 1,
8661
+ backgroundColor: '#0d83cd',
8662
+ '&:hover': {
8663
+ backgroundColor: '#0a6fad'
8664
+ },
8665
+ '&.Mui-disabled': {
8666
+ opacity: 0.5
8667
+ }
8668
+ },
8669
+ children: "Resize"
8670
+ }), /*#__PURE__*/jsx(Divider, {}), /*#__PURE__*/jsx(Typography, {
8671
+ sx: {
8672
+ fontWeight: 600,
8673
+ fontSize: 13,
8674
+ color: '#555'
8675
+ },
8676
+ children: "Preset sizes"
8677
+ }), loadingSizes && /*#__PURE__*/jsx(Box, {
8678
+ sx: {
8679
+ display: 'flex',
8680
+ justifyContent: 'center',
8681
+ py: 2
8682
+ },
8683
+ children: /*#__PURE__*/jsx(CircularProgress, {
8684
+ size: 22
8685
+ })
8686
+ }), sizesError && /*#__PURE__*/jsx(Typography, {
8687
+ sx: {
8688
+ fontSize: 12,
8689
+ color: '#c44'
8690
+ },
8691
+ children: "Could not load sizes."
8692
+ }), !loadingSizes && !sizesError && sizes.length === 0 && /*#__PURE__*/jsx(Typography, {
8693
+ sx: {
8694
+ fontSize: 12,
8695
+ color: '#aaa'
8696
+ },
8697
+ children: "No sizes available."
8698
+ }), !loadingSizes && !sizesError && sizes.map(function (size) {
8699
+ var dpi = size.dpi || 96;
8700
+ toPx(size.width, size.unit, dpi);
8701
+ toPx(size.height, size.unit, dpi);
8702
+ var dimLabel = size.width && size.height ? "".concat(size.width, " \xD7 ").concat(size.height, " ").concat((size.unit || 'px').toUpperCase()) : null;
8703
+ return /*#__PURE__*/jsxs(Box, {
8704
+ onClick: function onClick() {
8705
+ return handlePresetClick(size);
8706
+ },
8707
+ sx: {
8708
+ display: 'flex',
8709
+ alignItems: 'center',
8710
+ justifyContent: 'space-between',
8711
+ px: 1.5,
8712
+ py: 0.75,
8713
+ borderRadius: '6px',
8714
+ cursor: 'pointer',
8715
+ border: '1px solid #e8e8e8',
8716
+ '&:hover': {
8717
+ backgroundColor: '#f0f7fd',
8718
+ borderColor: '#0d83cd'
8719
+ },
8720
+ transition: 'all 0.12s ease'
8721
+ },
8722
+ children: [/*#__PURE__*/jsx(Typography, {
8723
+ sx: {
8724
+ fontSize: 13,
8725
+ fontWeight: 500
8726
+ },
8727
+ children: size.name
8728
+ }), dimLabel && /*#__PURE__*/jsx(Typography, {
8729
+ sx: {
8730
+ fontSize: 11,
8731
+ color: '#888',
8732
+ fontFamily: 'monospace'
8733
+ },
8734
+ children: dimLabel
8735
+ })]
8736
+ }, size.id);
8737
+ })]
8738
+ });
8739
+ });
8740
+
8158
8741
  var GOOGLE_FONTS_API = 'https://www.googleapis.com/webfonts/v1/webfonts';
8159
8742
  var cachedFonts = null;
8160
8743
  function fetchGoogleFonts(_x) {
@@ -8619,6 +9202,13 @@ var ImageToolbar = observer(function (_ref5) {
8619
9202
  store.history.addUndoState();
8620
9203
  setFlipAnchor(null);
8621
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
+ };
8622
9212
  var handleFitToPage = function handleFitToPage() {
8623
9213
  element.set({
8624
9214
  x: 0,
@@ -8748,6 +9338,16 @@ var ImageToolbar = observer(function (_ref5) {
8748
9338
  } : {}),
8749
9339
  onClick: handleMask,
8750
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')
8751
9351
  })]
8752
9352
  });
8753
9353
  });
@@ -9917,6 +10517,159 @@ var ZoomButtons = observer(function (_ref) {
9917
10517
  });
9918
10518
  ZoomButtons.displayName = 'ZoomButtons';
9919
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
+
9920
10673
  var SepiaFilter = function SepiaFilter(imageData) {
9921
10674
  var data = imageData.data;
9922
10675
  for (var i = 0; i < data.length; i += 4) {
@@ -9954,7 +10707,8 @@ var ImageElementComponent = observer(function (_ref) {
9954
10707
  var onSelect = _ref.onSelect,
9955
10708
  onTransformEnd = _ref.onTransformEnd,
9956
10709
  onDragEnd = _ref.onDragEnd,
9957
- groupChild = _ref.groupChild;
10710
+ groupChild = _ref.groupChild,
10711
+ store = _ref.store;
9958
10712
  var imageRef = useRef(null);
9959
10713
  var groupRef = useRef(null);
9960
10714
  var _useState = useState(null),
@@ -9976,6 +10730,7 @@ var ImageElementComponent = observer(function (_ref) {
9976
10730
  img.crossOrigin = 'anonymous';
9977
10731
  img.onload = function () {
9978
10732
  setImage(img);
10733
+ element.setNaturalSize(img.naturalWidth, img.naturalHeight);
9979
10734
  };
9980
10735
  img.onerror = function () {
9981
10736
  console.warn('Failed to load image:', element.src.substring(0, 100));
@@ -10010,6 +10765,20 @@ var ImageElementComponent = observer(function (_ref) {
10010
10765
  }
10011
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]);
10012
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
+ };
10013
10782
  var handleDragEnd = function handleDragEnd(e) {
10014
10783
  var target = groupRef.current || e.target;
10015
10784
  element.set({
@@ -10250,6 +11019,8 @@ var ImageElementComponent = observer(function (_ref) {
10250
11019
  onDragEnd: handleDragEnd,
10251
11020
  onTransform: handleTransform,
10252
11021
  onTransformEnd: handleTransformEnd,
11022
+ onDblClick: handleDblClick,
11023
+ onDblTap: handleDblClick,
10253
11024
  children: [imageNode, hasBorder && /*#__PURE__*/jsx(Rect, {
10254
11025
  x: 0,
10255
11026
  y: 0,
@@ -11050,7 +11821,8 @@ var ElementRenderer = observer(function (_ref) {
11050
11821
  onSelect = _ref.onSelect,
11051
11822
  onTransformEnd = _ref.onTransformEnd,
11052
11823
  onDragEnd = _ref.onDragEnd,
11053
- groupChild = _ref.groupChild;
11824
+ groupChild = _ref.groupChild,
11825
+ store = _ref.store;
11054
11826
  var Renderer = ELEMENT_RENDERERS[element.type];
11055
11827
  if (!Renderer) {
11056
11828
  console.warn("No renderer for element type: ".concat(element.type));
@@ -11067,7 +11839,8 @@ var ElementRenderer = observer(function (_ref) {
11067
11839
  onSelect: onSelect,
11068
11840
  onTransformEnd: onTransformEnd,
11069
11841
  onDragEnd: onDragEnd,
11070
- groupChild: groupChild
11842
+ groupChild: groupChild,
11843
+ store: store
11071
11844
  }, extra));
11072
11845
  });
11073
11846
  ElementRenderer.displayName = 'ElementRenderer';
@@ -12235,10 +13008,11 @@ var SelectionTransformer = observer(function (_ref5) {
12235
13008
  var transformerRef = useRef(null);
12236
13009
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
12237
13010
 
12238
- // 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).
12239
13013
  var nonLineIds = selectedIds.filter(function (id) {
12240
13014
  var el = store.getElementById(id);
12241
- return el && el.type !== 'line';
13015
+ return el && el.type !== 'line' && !el._cropModeEnabled;
12242
13016
  });
12243
13017
  var allText = nonLineIds.length === 1 && function () {
12244
13018
  var el = store.getElementById(nonLineIds[0]);
@@ -12352,7 +13126,25 @@ var Workspace = observer(function (_ref6) {
12352
13126
  }
12353
13127
  }, [stageRef.current]);
12354
13128
  var handleStageMouseDown = useCallback(function (e) {
12355
- 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
+ }
12356
13148
  var target = e.target;
12357
13149
 
12358
13150
  // Click on empty area or background -> deselect
@@ -12401,12 +13193,21 @@ var Workspace = observer(function (_ref6) {
12401
13193
  // Keyboard shortcuts
12402
13194
  useEffect(function () {
12403
13195
  var handleKeyDown = function handleKeyDown(e) {
13196
+ var _store$_toolbarPanel2;
12404
13197
  // Only handle if stage container or body is focused
12405
13198
  var target = e.target;
12406
13199
  if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT') {
12407
13200
  return;
12408
13201
  }
12409
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
+
12410
13211
  // Delete / Backspace
12411
13212
  if (e.key === 'Delete' || e.key === 'Backspace') {
12412
13213
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
@@ -12469,9 +13270,9 @@ var Workspace = observer(function (_ref6) {
12469
13270
  if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'G') {
12470
13271
  var _selectedIds3 = _toConsumableArray(store.selectedElementsIds);
12471
13272
  if (_selectedIds3.length === 1) {
12472
- var el = store.getElementById(_selectedIds3[0]);
12473
- if (el && el.type === 'group') {
12474
- store.ungroupElements([el.id]);
13273
+ var _el = store.getElementById(_selectedIds3[0]);
13274
+ if (_el && _el.type === 'group') {
13275
+ store.ungroupElements([_el.id]);
12475
13276
  store.history.addUndoState();
12476
13277
  e.preventDefault();
12477
13278
  }
@@ -12567,7 +13368,8 @@ var Workspace = observer(function (_ref6) {
12567
13368
  element: element,
12568
13369
  isSelected: store.selectedElementsIds.includes(element.id),
12569
13370
  onTransformEnd: handleTransformEnd,
12570
- onDragEnd: handleDragEnd
13371
+ onDragEnd: handleDragEnd,
13372
+ store: store
12571
13373
  }, element.id);
12572
13374
  }), /*#__PURE__*/jsx(PageOverlayFrame, {
12573
13375
  store: store