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.
@@ -556,6 +556,84 @@ var Shape = Node.named('Shape').props({
556
556
  };
557
557
  });
558
558
 
559
+ // Cover-fit crop fractions (centered) for a box of given size over an image of natural size.
560
+ // Mirrors the existing auto-crop calculation in canvas/image-element.js.
561
+ function coverFitCrop(boxWidth, boxHeight, naturalWidth, naturalHeight) {
562
+ var boxAspect = boxWidth / boxHeight;
563
+ var imgAspect = naturalWidth / naturalHeight;
564
+ if (imgAspect > boxAspect) {
565
+ var _cw = naturalHeight * boxAspect;
566
+ return {
567
+ cropX: (naturalWidth - _cw) / 2 / naturalWidth,
568
+ cropY: 0,
569
+ cropWidth: _cw / naturalWidth,
570
+ cropHeight: 1
571
+ };
572
+ }
573
+ var ch = naturalWidth / boxAspect;
574
+ return {
575
+ cropX: 0,
576
+ cropY: (naturalHeight - ch) / 2 / naturalHeight,
577
+ cropWidth: 1,
578
+ cropHeight: ch / naturalHeight
579
+ };
580
+ }
581
+
582
+ // Whether stored crop fields represent an explicit (non-default) manual crop.
583
+ function hasManualCrop(cropX, cropY, cropWidth, cropHeight) {
584
+ return cropX !== 0 || cropY !== 0 || cropWidth !== 1 || cropHeight !== 1;
585
+ }
586
+
587
+ // Convert a crop (fractions of natural image size) + target rect (page units) into an
588
+ // image position/scale such that the crop region renders exactly under the rect.
589
+ function cropToImageTransform(crop, rect, naturalWidth, naturalHeight) {
590
+ var naturalX = crop.cropX * naturalWidth;
591
+ var naturalY = crop.cropY * naturalHeight;
592
+ var naturalW = crop.cropWidth * naturalWidth;
593
+ var scale = naturalW > 0 ? rect.width / naturalW : 1;
594
+ return {
595
+ scale: scale,
596
+ offset: {
597
+ x: rect.x - naturalX * scale,
598
+ y: rect.y - naturalY * scale
599
+ }
600
+ };
601
+ }
602
+
603
+ // Inverse: given the image's page-space position/scale and a rect, compute crop fractions.
604
+ function rectToCrop(rect, imgOffset, imgScale, naturalWidth, naturalHeight) {
605
+ var naturalX = (rect.x - imgOffset.x) / imgScale;
606
+ var naturalY = (rect.y - imgOffset.y) / imgScale;
607
+ var naturalW = rect.width / imgScale;
608
+ var naturalH = rect.height / imgScale;
609
+ return {
610
+ cropX: naturalX / naturalWidth,
611
+ cropY: naturalY / naturalHeight,
612
+ cropWidth: naturalW / naturalWidth,
613
+ cropHeight: naturalH / naturalHeight
614
+ };
615
+ }
616
+
617
+ // Minimum scale at which the image (rendered at naturalWidth*scale x naturalHeight*scale)
618
+ // still fully covers rect.
619
+ function minScaleForRect(rect, naturalWidth, naturalHeight) {
620
+ return Math.max(rect.width / naturalWidth, rect.height / naturalHeight);
621
+ }
622
+
623
+ // Clamp an image page-space offset so the image still fully covers rect at the given scale.
624
+ function clampImageOffset(offset, scale, rect, naturalWidth, naturalHeight) {
625
+ var imgW = naturalWidth * scale;
626
+ var imgH = naturalHeight * scale;
627
+ var minX = rect.x + rect.width - imgW;
628
+ var maxX = rect.x;
629
+ var minY = rect.y + rect.height - imgH;
630
+ var maxY = rect.y;
631
+ return {
632
+ x: Math.min(maxX, Math.max(minX, offset.x)),
633
+ y: Math.min(maxY, Math.max(minY, offset.y))
634
+ };
635
+ }
636
+
559
637
  var ImageElement = Shape.named('ImageElement').props({
560
638
  type: types.optional(types.string, 'image'),
561
639
  src: types.optional(types.string, ''),
@@ -589,12 +667,132 @@ var ImageElement = Shape.named('ImageElement').props({
589
667
  return s;
590
668
  })["volatile"](function () {
591
669
  return {
592
- _cropModeEnabled: false
670
+ // Set once, when the image finishes loading (see canvas/image-element.js).
671
+ _naturalWidth: 0,
672
+ _naturalHeight: 0,
673
+ // Crop-mode editing state — all null/default when not cropping.
674
+ _cropModeEnabled: false,
675
+ _cropEntrySnapshot: null,
676
+ _cropRect: null,
677
+ _cropImgOffset: null,
678
+ _cropImgScale: 1,
679
+ _cropNaturalWidth: 0,
680
+ _cropNaturalHeight: 0
593
681
  };
594
682
  }).actions(function (self) {
595
683
  return {
596
- toggleCropMode: function toggleCropMode(flag) {
597
- self._cropModeEnabled = flag !== undefined ? flag : !self._cropModeEnabled;
684
+ setNaturalSize: function setNaturalSize(width, height) {
685
+ self._naturalWidth = width;
686
+ self._naturalHeight = height;
687
+ },
688
+ enterCropMode: function enterCropMode() {
689
+ var naturalWidth = self._naturalWidth;
690
+ var naturalHeight = self._naturalHeight;
691
+ if (!naturalWidth || !naturalHeight) return;
692
+ if (self.rotation !== 0 || self.flipX || self.flipY) return;
693
+ self._cropEntrySnapshot = {
694
+ x: self.x,
695
+ y: self.y,
696
+ width: self.width,
697
+ height: self.height,
698
+ cropX: self.cropX,
699
+ cropY: self.cropY,
700
+ cropWidth: self.cropWidth,
701
+ cropHeight: self.cropHeight
702
+ };
703
+ var rect = {
704
+ x: self.x,
705
+ y: self.y,
706
+ width: self.width,
707
+ height: self.height
708
+ };
709
+ var crop = hasManualCrop(self.cropX, self.cropY, self.cropWidth, self.cropHeight) ? {
710
+ cropX: self.cropX,
711
+ cropY: self.cropY,
712
+ cropWidth: self.cropWidth,
713
+ cropHeight: self.cropHeight
714
+ } : coverFitCrop(rect.width, rect.height, naturalWidth, naturalHeight);
715
+ var _cropToImageTransform = cropToImageTransform(crop, rect, naturalWidth, naturalHeight),
716
+ scale = _cropToImageTransform.scale,
717
+ offset = _cropToImageTransform.offset;
718
+ self._cropRect = rect;
719
+ self._cropImgOffset = offset;
720
+ self._cropImgScale = scale;
721
+ self._cropNaturalWidth = naturalWidth;
722
+ self._cropNaturalHeight = naturalHeight;
723
+ self._cropModeEnabled = true;
724
+ },
725
+ setCropRectAndClamp: function setCropRectAndClamp(rect) {
726
+ // Defensive guard (not in the original brief text): without this, a caller invoking
727
+ // this action while crop mode is inactive (e.g. enterCropMode() no-op'd because the
728
+ // image hadn't loaded yet) would hit minScaleForRect/clampImageOffset with
729
+ // _cropNaturalWidth/_cropNaturalHeight still at their 0 defaults, producing
730
+ // Infinity/NaN. Crop mode is only ever active with a nonzero natural size (see
731
+ // enterCropMode's guard above), so gating on _cropModeEnabled here is equivalent to
732
+ // gating on nonzero natural size, without changing behavior for the normal case.
733
+ if (!self._cropModeEnabled) return;
734
+ self._cropRect = rect;
735
+ var minScale = minScaleForRect(rect, self._cropNaturalWidth, self._cropNaturalHeight);
736
+ if (self._cropImgScale < minScale) {
737
+ self._cropImgScale = minScale;
738
+ }
739
+ self._cropImgOffset = clampImageOffset(self._cropImgOffset || {
740
+ x: rect.x,
741
+ y: rect.y
742
+ }, self._cropImgScale, rect, self._cropNaturalWidth, self._cropNaturalHeight);
743
+ },
744
+ panCropImage: function panCropImage(offset) {
745
+ if (!self._cropRect) return;
746
+ self._cropImgOffset = clampImageOffset(offset, self._cropImgScale, self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight);
747
+ },
748
+ setCropZoom: function setCropZoom(scale) {
749
+ if (!self._cropRect) return;
750
+ var minScale = minScaleForRect(self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight);
751
+ var clampedScale = Math.max(minScale, scale);
752
+ self._cropImgScale = clampedScale;
753
+ self._cropImgOffset = clampImageOffset(self._cropImgOffset || {
754
+ x: self._cropRect.x,
755
+ y: self._cropRect.y
756
+ }, clampedScale, self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight);
757
+ },
758
+ resetCropToDefault: function resetCropToDefault() {
759
+ if (!self._cropRect) return;
760
+ var crop = coverFitCrop(self._cropRect.width, self._cropRect.height, self._cropNaturalWidth, self._cropNaturalHeight);
761
+ var _cropToImageTransform2 = cropToImageTransform(crop, self._cropRect, self._cropNaturalWidth, self._cropNaturalHeight),
762
+ scale = _cropToImageTransform2.scale,
763
+ offset = _cropToImageTransform2.offset;
764
+ self._cropImgScale = scale;
765
+ self._cropImgOffset = offset;
766
+ },
767
+ cancelCropMode: function cancelCropMode() {
768
+ if (self._cropEntrySnapshot) {
769
+ self.set(self._cropEntrySnapshot);
770
+ }
771
+ self._cropEntrySnapshot = null;
772
+ self._cropRect = null;
773
+ self._cropImgOffset = null;
774
+ self._cropImgScale = 1;
775
+ self._cropModeEnabled = false;
776
+ },
777
+ commitCropMode: function commitCropMode() {
778
+ if (self._cropRect && self._cropImgOffset) {
779
+ var crop = rectToCrop(self._cropRect, self._cropImgOffset, self._cropImgScale, self._cropNaturalWidth, self._cropNaturalHeight);
780
+ self.set({
781
+ x: self._cropRect.x,
782
+ y: self._cropRect.y,
783
+ width: self._cropRect.width,
784
+ height: self._cropRect.height,
785
+ cropX: crop.cropX,
786
+ cropY: crop.cropY,
787
+ cropWidth: crop.cropWidth,
788
+ cropHeight: crop.cropHeight
789
+ });
790
+ }
791
+ self._cropEntrySnapshot = null;
792
+ self._cropRect = null;
793
+ self._cropImgOffset = null;
794
+ self._cropImgScale = 1;
795
+ self._cropModeEnabled = false;
598
796
  }
599
797
  };
600
798
  });
@@ -1689,10 +1887,22 @@ var Store = types.model('Store', {
1689
1887
  if (self._toolbarPanel) {
1690
1888
  var stillSelected = ids.includes(self._toolbarPanel.elementId);
1691
1889
  if (!stillSelected) {
1890
+ self._cancelActiveCrop();
1692
1891
  self._toolbarPanel = null;
1693
1892
  }
1694
1893
  }
1695
1894
  },
1895
+ // Single choke-point for "cancel whatever element is mid-crop, if any" — must be
1896
+ // called before _toolbarPanel is overwritten/cleared by any path (panel switch,
1897
+ // panel close, or selection change) so an element can never be left stuck in
1898
+ // crop mode with no matching exit trigger.
1899
+ _cancelActiveCrop: function _cancelActiveCrop() {
1900
+ var _self$_toolbarPanel;
1901
+ if (((_self$_toolbarPanel = self._toolbarPanel) === null || _self$_toolbarPanel === void 0 ? void 0 : _self$_toolbarPanel.type) === 'crop') {
1902
+ var el = self.getElementById(self._toolbarPanel.elementId);
1903
+ if (el && el._cropModeEnabled && el.cancelCropMode) el.cancelCropMode();
1904
+ }
1905
+ },
1696
1906
  toggleBleed: function toggleBleed(bleedVisible) {
1697
1907
  self.bleedVisible = bleedVisible !== undefined ? bleedVisible : !self.bleedVisible;
1698
1908
  },
@@ -1709,6 +1919,7 @@ var Store = types.model('Store', {
1709
1919
  openToolbarPanel: function openToolbarPanel(type) {
1710
1920
  var el = self.selectedElements.length === 1 ? self.selectedElements[0] : null;
1711
1921
  if (el) {
1922
+ self._cancelActiveCrop();
1712
1923
  self._toolbarPanel = {
1713
1924
  type: type,
1714
1925
  elementId: el.id
@@ -1716,6 +1927,7 @@ var Store = types.model('Store', {
1716
1927
  }
1717
1928
  },
1718
1929
  closeToolbarPanel: function closeToolbarPanel() {
1930
+ self._cancelActiveCrop();
1719
1931
  self._toolbarPanel = null;
1720
1932
  },
1721
1933
  setScale: function setScale(scale) {