labellife-design-tool 2.3.8 → 2.4.0

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