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.
@@ -1,5 +1,5 @@
1
- import { useRef, useState, useEffect, Fragment, useCallback } from 'react';
2
- import { Image, Group, Rect, Text, Arrow, Path, RegularPolygon, Star, Circle, Line, Shape, Transformer, Stage, Layer } from 'react-konva';
1
+ import { useRef, useEffect, useState, Fragment, useCallback } from 'react';
2
+ import { Group, Image, Rect, Transformer, Text, Arrow, Path, RegularPolygon, Star, Circle, Line, Shape, Stage, Layer } from 'react-konva';
3
3
  import { observer } from 'mobx-react-lite';
4
4
  import { Box, Typography, IconButton, Button, TextField, FormGroup, FormControlLabel, Checkbox, RadioGroup, Radio } from '@mui/material';
5
5
  import CloseIcon from '@mui/icons-material/Close';
@@ -491,6 +491,166 @@ var ImageMaskPanel = observer(function (_ref) {
491
491
  });
492
492
  ImageMaskPanel.displayName = 'ImageMaskPanel';
493
493
 
494
+ // Cover-fit crop fractions (centered) for a box of given size over an image of natural size.
495
+ // Mirrors the existing auto-crop calculation in canvas/image-element.js.
496
+
497
+ // Clamp an image page-space offset so the image still fully covers rect at the given scale.
498
+ function clampImageOffset(offset, scale, rect, naturalWidth, naturalHeight) {
499
+ var imgW = naturalWidth * scale;
500
+ var imgH = naturalHeight * scale;
501
+ var minX = rect.x + rect.width - imgW;
502
+ var maxX = rect.x;
503
+ var minY = rect.y + rect.height - imgH;
504
+ var maxY = rect.y;
505
+ return {
506
+ x: Math.min(maxX, Math.max(minX, offset.x)),
507
+ y: Math.min(maxY, Math.max(minY, offset.y))
508
+ };
509
+ }
510
+
511
+ // src/canvas/crop-editor.js
512
+ var MIN_RECT_SIZE = 20;
513
+ var VIEWPORT_PAD = 400; // page units of dim area rendered around the crop rect
514
+
515
+ var CropEditor = observer(function (_ref) {
516
+ var element = _ref.element,
517
+ image = _ref.image;
518
+ var rectRef = useRef(null);
519
+ var transformerRef = useRef(null);
520
+ useEffect(function () {
521
+ if (transformerRef.current && rectRef.current) {
522
+ var _transformerRef$curre;
523
+ transformerRef.current.nodes([rectRef.current]);
524
+ (_transformerRef$curre = transformerRef.current.getLayer()) === null || _transformerRef$curre === void 0 || _transformerRef$curre.batchDraw();
525
+ }
526
+ });
527
+ if (!image || !element._cropRect || !element._cropImgOffset) return null;
528
+ var natW = image.naturalWidth || image.width;
529
+ var natH = image.naturalHeight || image.height;
530
+ var rect = element._cropRect;
531
+ var offset = element._cropImgOffset;
532
+ var scale = element._cropImgScale;
533
+ var handleImageDragMove = function handleImageDragMove(e) {
534
+ var node = e.target;
535
+ var clamped = clampImageOffset({
536
+ x: node.x(),
537
+ y: node.y()
538
+ }, scale, rect, natW, natH);
539
+ node.x(clamped.x);
540
+ node.y(clamped.y);
541
+ };
542
+ var handleImageDragEnd = function handleImageDragEnd(e) {
543
+ var node = e.target;
544
+ element.panCropImage({
545
+ x: node.x(),
546
+ y: node.y()
547
+ });
548
+ };
549
+
550
+ // Shared by both the live (onTransform) and final (onTransformEnd) handlers: reads the
551
+ // Konva node's current scale, bakes it into width/height, resets the node's scale back to
552
+ // 1 (required for the Transformer to keep computing subsequent ticks correctly within the
553
+ // same drag), and pushes the new rect through the model so the image/mask stay in sync.
554
+ var applyRectTransform = function applyRectTransform() {
555
+ var node = rectRef.current;
556
+ var scaleX = node.scaleX();
557
+ var scaleY = node.scaleY();
558
+ var newRect = {
559
+ x: node.x(),
560
+ y: node.y(),
561
+ width: Math.max(MIN_RECT_SIZE, rect.width * scaleX),
562
+ height: Math.max(MIN_RECT_SIZE, rect.height * scaleY)
563
+ };
564
+ node.scaleX(1);
565
+ node.scaleY(1);
566
+ node.width(newRect.width);
567
+ node.height(newRect.height);
568
+ element.setCropRectAndClamp(newRect);
569
+ };
570
+ var handleRectTransform = function handleRectTransform() {
571
+ return applyRectTransform();
572
+ };
573
+ var handleRectTransformEnd = function handleRectTransformEnd() {
574
+ return applyRectTransform();
575
+ };
576
+
577
+ // Dim mask: four rects framing the hole over `rect`.
578
+ var vx = rect.x - VIEWPORT_PAD;
579
+ var vy = rect.y - VIEWPORT_PAD;
580
+ var vw = rect.width + VIEWPORT_PAD * 2;
581
+ var vh = rect.height + VIEWPORT_PAD * 2;
582
+ return /*#__PURE__*/jsxs(Group, {
583
+ children: [/*#__PURE__*/jsx(Image, {
584
+ name: "display-only crop-interactive",
585
+ image: image,
586
+ x: offset.x,
587
+ y: offset.y,
588
+ width: natW * scale,
589
+ height: natH * scale,
590
+ draggable: true,
591
+ onDragMove: handleImageDragMove,
592
+ onDragEnd: handleImageDragEnd
593
+ }), /*#__PURE__*/jsx(Rect, {
594
+ name: "display-only",
595
+ x: vx,
596
+ y: vy,
597
+ width: vw,
598
+ height: rect.y - vy,
599
+ fill: "rgba(0,0,0,0.55)",
600
+ listening: false
601
+ }), /*#__PURE__*/jsx(Rect, {
602
+ name: "display-only",
603
+ x: vx,
604
+ y: rect.y + rect.height,
605
+ width: vw,
606
+ height: vy + vh - (rect.y + rect.height),
607
+ fill: "rgba(0,0,0,0.55)",
608
+ listening: false
609
+ }), /*#__PURE__*/jsx(Rect, {
610
+ name: "display-only",
611
+ x: vx,
612
+ y: rect.y,
613
+ width: rect.x - vx,
614
+ height: rect.height,
615
+ fill: "rgba(0,0,0,0.55)",
616
+ listening: false
617
+ }), /*#__PURE__*/jsx(Rect, {
618
+ name: "display-only",
619
+ x: rect.x + rect.width,
620
+ y: rect.y,
621
+ width: vx + vw - (rect.x + rect.width),
622
+ height: rect.height,
623
+ fill: "rgba(0,0,0,0.55)",
624
+ listening: false
625
+ }), /*#__PURE__*/jsx(Rect, {
626
+ name: "display-only crop-interactive",
627
+ ref: rectRef,
628
+ x: rect.x,
629
+ y: rect.y,
630
+ width: rect.width,
631
+ height: rect.height,
632
+ stroke: "#ffffff",
633
+ strokeWidth: 1.5,
634
+ listening: false,
635
+ onTransform: handleRectTransform,
636
+ onTransformEnd: handleRectTransformEnd
637
+ }), /*#__PURE__*/jsx(Transformer, {
638
+ ref: transformerRef,
639
+ rotateEnabled: false,
640
+ anchorSize: 8,
641
+ anchorCornerRadius: 2,
642
+ borderStroke: "#0d83cd",
643
+ anchorStroke: "#0d83cd",
644
+ anchorFill: "#ffffff",
645
+ boundBoxFunc: function boundBoxFunc(oldBox, newBox) {
646
+ if (newBox.width < MIN_RECT_SIZE || newBox.height < MIN_RECT_SIZE) return oldBox;
647
+ return newBox;
648
+ }
649
+ })]
650
+ });
651
+ });
652
+ CropEditor.displayName = 'CropEditor';
653
+
494
654
  var SepiaFilter = function SepiaFilter(imageData) {
495
655
  var data = imageData.data;
496
656
  for (var i = 0; i < data.length; i += 4) {
@@ -528,7 +688,8 @@ var ImageElementComponent = observer(function (_ref) {
528
688
  var onSelect = _ref.onSelect,
529
689
  onTransformEnd = _ref.onTransformEnd,
530
690
  onDragEnd = _ref.onDragEnd,
531
- groupChild = _ref.groupChild;
691
+ groupChild = _ref.groupChild,
692
+ store = _ref.store;
532
693
  var imageRef = useRef(null);
533
694
  var groupRef = useRef(null);
534
695
  var _useState = useState(null),
@@ -550,6 +711,7 @@ var ImageElementComponent = observer(function (_ref) {
550
711
  img.crossOrigin = 'anonymous';
551
712
  img.onload = function () {
552
713
  setImage(img);
714
+ element.setNaturalSize(img.naturalWidth, img.naturalHeight);
553
715
  };
554
716
  img.onerror = function () {
555
717
  console.warn('Failed to load image:', element.src.substring(0, 100));
@@ -584,6 +746,20 @@ var ImageElementComponent = observer(function (_ref) {
584
746
  }
585
747
  }, [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]);
586
748
  if (!element.visible) return null;
749
+ if (element._cropModeEnabled) {
750
+ return /*#__PURE__*/jsx(CropEditor, {
751
+ element: element,
752
+ image: image
753
+ });
754
+ }
755
+ var handleDblClick = function handleDblClick() {
756
+ if (!store || groupChild) return;
757
+ element.enterCropMode();
758
+ if (element._cropModeEnabled) {
759
+ store.selectElements([element.id]);
760
+ store.openToolbarPanel('crop');
761
+ }
762
+ };
587
763
  var handleDragEnd = function handleDragEnd(e) {
588
764
  var target = groupRef.current || e.target;
589
765
  element.set({
@@ -824,6 +1000,8 @@ var ImageElementComponent = observer(function (_ref) {
824
1000
  onDragEnd: handleDragEnd,
825
1001
  onTransform: handleTransform,
826
1002
  onTransformEnd: handleTransformEnd,
1003
+ onDblClick: handleDblClick,
1004
+ onDblTap: handleDblClick,
827
1005
  children: [imageNode, hasBorder && /*#__PURE__*/jsx(Rect, {
828
1006
  x: 0,
829
1007
  y: 0,
@@ -1624,7 +1802,8 @@ var ElementRenderer = observer(function (_ref) {
1624
1802
  onSelect = _ref.onSelect,
1625
1803
  onTransformEnd = _ref.onTransformEnd,
1626
1804
  onDragEnd = _ref.onDragEnd,
1627
- groupChild = _ref.groupChild;
1805
+ groupChild = _ref.groupChild,
1806
+ store = _ref.store;
1628
1807
  var Renderer = ELEMENT_RENDERERS[element.type];
1629
1808
  if (!Renderer) {
1630
1809
  console.warn("No renderer for element type: ".concat(element.type));
@@ -1641,7 +1820,8 @@ var ElementRenderer = observer(function (_ref) {
1641
1820
  onSelect: onSelect,
1642
1821
  onTransformEnd: onTransformEnd,
1643
1822
  onDragEnd: onDragEnd,
1644
- groupChild: groupChild
1823
+ groupChild: groupChild,
1824
+ store: store
1645
1825
  }, extra));
1646
1826
  });
1647
1827
  ElementRenderer.displayName = 'ElementRenderer';
@@ -2809,10 +2989,11 @@ var SelectionTransformer = observer(function (_ref5) {
2809
2989
  var transformerRef = useRef(null);
2810
2990
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
2811
2991
 
2812
- // Filter out line elements — they use custom endpoint handles instead
2992
+ // Filter out line elements (custom endpoint handles) and elements mid-crop
2993
+ // (CropEditor renders its own Transformer bound to the crop rect instead).
2813
2994
  var nonLineIds = selectedIds.filter(function (id) {
2814
2995
  var el = store.getElementById(id);
2815
- return el && el.type !== 'line';
2996
+ return el && el.type !== 'line' && !el._cropModeEnabled;
2816
2997
  });
2817
2998
  var allText = nonLineIds.length === 1 && function () {
2818
2999
  var el = store.getElementById(nonLineIds[0]);
@@ -2926,7 +3107,25 @@ var Workspace = observer(function (_ref6) {
2926
3107
  }
2927
3108
  }, [stageRef.current]);
2928
3109
  var handleStageMouseDown = useCallback(function (e) {
2929
- var _target$attrs, _e$evt, _e$evt2;
3110
+ var _store$_toolbarPanel, _target$attrs, _e$evt, _e$evt2;
3111
+ // Clicking anywhere outside the crop rect / pannable image / resize
3112
+ // handles while cropping finishes the crop (like Polotno), then falls
3113
+ // through to the click's normal handling (select whatever was actually
3114
+ // clicked, or deselect).
3115
+ if (((_store$_toolbarPanel = store._toolbarPanel) === null || _store$_toolbarPanel === void 0 ? void 0 : _store$_toolbarPanel.type) === 'crop') {
3116
+ var _e$target$getParent, _e$target;
3117
+ var croppingEl = store.getElementById(store._toolbarPanel.elementId);
3118
+ var isCropInteractive = typeof e.target.hasName === 'function' && e.target.hasName('crop-interactive');
3119
+ // Konva's Transformer adds its resize handles as direct children of
3120
+ // itself, so a handle's parent's className is 'Transformer' — this
3121
+ // catches drags on the crop rect's resize/rotate handles.
3122
+ 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';
3123
+ if (croppingEl && croppingEl._cropModeEnabled && !isCropInteractive && !isTransformerHandle) {
3124
+ croppingEl.commitCropMode();
3125
+ store.closeToolbarPanel();
3126
+ store.history.addUndoState();
3127
+ }
3128
+ }
2930
3129
  var target = e.target;
2931
3130
 
2932
3131
  // Click on empty area or background -> deselect
@@ -2975,12 +3174,21 @@ var Workspace = observer(function (_ref6) {
2975
3174
  // Keyboard shortcuts
2976
3175
  useEffect(function () {
2977
3176
  var handleKeyDown = function handleKeyDown(e) {
3177
+ var _store$_toolbarPanel2;
2978
3178
  // Only handle if stage container or body is focused
2979
3179
  var target = e.target;
2980
3180
  if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT') {
2981
3181
  return;
2982
3182
  }
2983
3183
 
3184
+ // Escape -> cancel crop mode if active
3185
+ if (e.key === 'Escape' && ((_store$_toolbarPanel2 = store._toolbarPanel) === null || _store$_toolbarPanel2 === void 0 ? void 0 : _store$_toolbarPanel2.type) === 'crop') {
3186
+ var el = store.getElementById(store._toolbarPanel.elementId);
3187
+ if (el && el.cancelCropMode) el.cancelCropMode();
3188
+ store.closeToolbarPanel();
3189
+ e.preventDefault();
3190
+ }
3191
+
2984
3192
  // Delete / Backspace
2985
3193
  if (e.key === 'Delete' || e.key === 'Backspace') {
2986
3194
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
@@ -3043,9 +3251,9 @@ var Workspace = observer(function (_ref6) {
3043
3251
  if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'G') {
3044
3252
  var _selectedIds3 = _toConsumableArray(store.selectedElementsIds);
3045
3253
  if (_selectedIds3.length === 1) {
3046
- var el = store.getElementById(_selectedIds3[0]);
3047
- if (el && el.type === 'group') {
3048
- store.ungroupElements([el.id]);
3254
+ var _el = store.getElementById(_selectedIds3[0]);
3255
+ if (_el && _el.type === 'group') {
3256
+ store.ungroupElements([_el.id]);
3049
3257
  store.history.addUndoState();
3050
3258
  e.preventDefault();
3051
3259
  }
@@ -3141,7 +3349,8 @@ var Workspace = observer(function (_ref6) {
3141
3349
  element: element,
3142
3350
  isSelected: store.selectedElementsIds.includes(element.id),
3143
3351
  onTransformEnd: handleTransformEnd,
3144
- onDragEnd: handleDragEnd
3352
+ onDragEnd: handleDragEnd,
3353
+ store: store
3145
3354
  }, element.id);
3146
3355
  }), /*#__PURE__*/jsx(PageOverlayFrame, {
3147
3356
  store: store