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.
@@ -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,176 @@ 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
+ var handleRectDragEnd = function handleRectDragEnd(e) {
550
+ var node = e.target;
551
+ element.setCropRectAndClamp({
552
+ x: node.x(),
553
+ y: node.y(),
554
+ width: rect.width,
555
+ height: rect.height
556
+ });
557
+ };
558
+
559
+ // Shared by both the live (onTransform) and final (onTransformEnd) handlers: reads the
560
+ // Konva node's current scale, bakes it into width/height, resets the node's scale back to
561
+ // 1 (required for the Transformer to keep computing subsequent ticks correctly within the
562
+ // same drag), and pushes the new rect through the model so the image/mask stay in sync.
563
+ var applyRectTransform = function applyRectTransform() {
564
+ var node = rectRef.current;
565
+ var scaleX = node.scaleX();
566
+ var scaleY = node.scaleY();
567
+ var newRect = {
568
+ x: node.x(),
569
+ y: node.y(),
570
+ width: Math.max(MIN_RECT_SIZE, rect.width * scaleX),
571
+ height: Math.max(MIN_RECT_SIZE, rect.height * scaleY)
572
+ };
573
+ node.scaleX(1);
574
+ node.scaleY(1);
575
+ node.width(newRect.width);
576
+ node.height(newRect.height);
577
+ element.setCropRectAndClamp(newRect);
578
+ };
579
+ var handleRectTransform = function handleRectTransform() {
580
+ return applyRectTransform();
581
+ };
582
+ var handleRectTransformEnd = function handleRectTransformEnd() {
583
+ return applyRectTransform();
584
+ };
585
+
586
+ // Dim mask: four rects framing the hole over `rect`.
587
+ var vx = rect.x - VIEWPORT_PAD;
588
+ var vy = rect.y - VIEWPORT_PAD;
589
+ var vw = rect.width + VIEWPORT_PAD * 2;
590
+ var vh = rect.height + VIEWPORT_PAD * 2;
591
+ return /*#__PURE__*/jsxs(Group, {
592
+ children: [/*#__PURE__*/jsx(Image, {
593
+ name: "display-only crop-interactive",
594
+ image: image,
595
+ x: offset.x,
596
+ y: offset.y,
597
+ width: natW * scale,
598
+ height: natH * scale,
599
+ draggable: true,
600
+ onDragMove: handleImageDragMove,
601
+ onDragEnd: handleImageDragEnd
602
+ }), /*#__PURE__*/jsx(Rect, {
603
+ name: "display-only",
604
+ x: vx,
605
+ y: vy,
606
+ width: vw,
607
+ height: rect.y - vy,
608
+ fill: "rgba(0,0,0,0.55)",
609
+ listening: false
610
+ }), /*#__PURE__*/jsx(Rect, {
611
+ name: "display-only",
612
+ x: vx,
613
+ y: rect.y + rect.height,
614
+ width: vw,
615
+ height: vy + vh - (rect.y + rect.height),
616
+ fill: "rgba(0,0,0,0.55)",
617
+ listening: false
618
+ }), /*#__PURE__*/jsx(Rect, {
619
+ name: "display-only",
620
+ x: vx,
621
+ y: rect.y,
622
+ width: rect.x - vx,
623
+ height: rect.height,
624
+ fill: "rgba(0,0,0,0.55)",
625
+ listening: false
626
+ }), /*#__PURE__*/jsx(Rect, {
627
+ name: "display-only",
628
+ x: rect.x + rect.width,
629
+ y: rect.y,
630
+ width: vx + vw - (rect.x + rect.width),
631
+ height: rect.height,
632
+ fill: "rgba(0,0,0,0.55)",
633
+ listening: false
634
+ }), /*#__PURE__*/jsx(Rect, {
635
+ name: "display-only crop-interactive",
636
+ ref: rectRef,
637
+ x: rect.x,
638
+ y: rect.y,
639
+ width: rect.width,
640
+ height: rect.height,
641
+ stroke: "#ffffff",
642
+ strokeWidth: 1.5,
643
+ draggable: true,
644
+ onDragEnd: handleRectDragEnd,
645
+ onTransform: handleRectTransform,
646
+ onTransformEnd: handleRectTransformEnd
647
+ }), /*#__PURE__*/jsx(Transformer, {
648
+ ref: transformerRef,
649
+ rotateEnabled: false,
650
+ anchorSize: 8,
651
+ anchorCornerRadius: 2,
652
+ borderStroke: "#0d83cd",
653
+ anchorStroke: "#0d83cd",
654
+ anchorFill: "#ffffff",
655
+ boundBoxFunc: function boundBoxFunc(oldBox, newBox) {
656
+ if (newBox.width < MIN_RECT_SIZE || newBox.height < MIN_RECT_SIZE) return oldBox;
657
+ return newBox;
658
+ }
659
+ })]
660
+ });
661
+ });
662
+ CropEditor.displayName = 'CropEditor';
663
+
494
664
  var SepiaFilter = function SepiaFilter(imageData) {
495
665
  var data = imageData.data;
496
666
  for (var i = 0; i < data.length; i += 4) {
@@ -528,7 +698,8 @@ var ImageElementComponent = observer(function (_ref) {
528
698
  var onSelect = _ref.onSelect,
529
699
  onTransformEnd = _ref.onTransformEnd,
530
700
  onDragEnd = _ref.onDragEnd,
531
- groupChild = _ref.groupChild;
701
+ groupChild = _ref.groupChild,
702
+ store = _ref.store;
532
703
  var imageRef = useRef(null);
533
704
  var groupRef = useRef(null);
534
705
  var _useState = useState(null),
@@ -550,6 +721,7 @@ var ImageElementComponent = observer(function (_ref) {
550
721
  img.crossOrigin = 'anonymous';
551
722
  img.onload = function () {
552
723
  setImage(img);
724
+ element.setNaturalSize(img.naturalWidth, img.naturalHeight);
553
725
  };
554
726
  img.onerror = function () {
555
727
  console.warn('Failed to load image:', element.src.substring(0, 100));
@@ -584,6 +756,20 @@ var ImageElementComponent = observer(function (_ref) {
584
756
  }
585
757
  }, [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
758
  if (!element.visible) return null;
759
+ if (element._cropModeEnabled) {
760
+ return /*#__PURE__*/jsx(CropEditor, {
761
+ element: element,
762
+ image: image
763
+ });
764
+ }
765
+ var handleDblClick = function handleDblClick() {
766
+ if (!store || groupChild) return;
767
+ element.enterCropMode();
768
+ if (element._cropModeEnabled) {
769
+ store.selectElements([element.id]);
770
+ store.openToolbarPanel('crop');
771
+ }
772
+ };
587
773
  var handleDragEnd = function handleDragEnd(e) {
588
774
  var target = groupRef.current || e.target;
589
775
  element.set({
@@ -824,6 +1010,8 @@ var ImageElementComponent = observer(function (_ref) {
824
1010
  onDragEnd: handleDragEnd,
825
1011
  onTransform: handleTransform,
826
1012
  onTransformEnd: handleTransformEnd,
1013
+ onDblClick: handleDblClick,
1014
+ onDblTap: handleDblClick,
827
1015
  children: [imageNode, hasBorder && /*#__PURE__*/jsx(Rect, {
828
1016
  x: 0,
829
1017
  y: 0,
@@ -1624,7 +1812,8 @@ var ElementRenderer = observer(function (_ref) {
1624
1812
  onSelect = _ref.onSelect,
1625
1813
  onTransformEnd = _ref.onTransformEnd,
1626
1814
  onDragEnd = _ref.onDragEnd,
1627
- groupChild = _ref.groupChild;
1815
+ groupChild = _ref.groupChild,
1816
+ store = _ref.store;
1628
1817
  var Renderer = ELEMENT_RENDERERS[element.type];
1629
1818
  if (!Renderer) {
1630
1819
  console.warn("No renderer for element type: ".concat(element.type));
@@ -1641,7 +1830,8 @@ var ElementRenderer = observer(function (_ref) {
1641
1830
  onSelect: onSelect,
1642
1831
  onTransformEnd: onTransformEnd,
1643
1832
  onDragEnd: onDragEnd,
1644
- groupChild: groupChild
1833
+ groupChild: groupChild,
1834
+ store: store
1645
1835
  }, extra));
1646
1836
  });
1647
1837
  ElementRenderer.displayName = 'ElementRenderer';
@@ -2809,10 +2999,11 @@ var SelectionTransformer = observer(function (_ref5) {
2809
2999
  var transformerRef = useRef(null);
2810
3000
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
2811
3001
 
2812
- // Filter out line elements — they use custom endpoint handles instead
3002
+ // Filter out line elements (custom endpoint handles) and elements mid-crop
3003
+ // (CropEditor renders its own Transformer bound to the crop rect instead).
2813
3004
  var nonLineIds = selectedIds.filter(function (id) {
2814
3005
  var el = store.getElementById(id);
2815
- return el && el.type !== 'line';
3006
+ return el && el.type !== 'line' && !el._cropModeEnabled;
2816
3007
  });
2817
3008
  var allText = nonLineIds.length === 1 && function () {
2818
3009
  var el = store.getElementById(nonLineIds[0]);
@@ -2926,7 +3117,25 @@ var Workspace = observer(function (_ref6) {
2926
3117
  }
2927
3118
  }, [stageRef.current]);
2928
3119
  var handleStageMouseDown = useCallback(function (e) {
2929
- var _target$attrs, _e$evt, _e$evt2;
3120
+ var _store$_toolbarPanel, _target$attrs, _e$evt, _e$evt2;
3121
+ // Clicking anywhere outside the crop rect / pannable image / resize
3122
+ // handles while cropping finishes the crop (like Polotno), then falls
3123
+ // through to the click's normal handling (select whatever was actually
3124
+ // clicked, or deselect).
3125
+ if (((_store$_toolbarPanel = store._toolbarPanel) === null || _store$_toolbarPanel === void 0 ? void 0 : _store$_toolbarPanel.type) === 'crop') {
3126
+ var _e$target$getParent, _e$target;
3127
+ var croppingEl = store.getElementById(store._toolbarPanel.elementId);
3128
+ var isCropInteractive = typeof e.target.hasName === 'function' && e.target.hasName('crop-interactive');
3129
+ // Konva's Transformer adds its resize handles as direct children of
3130
+ // itself, so a handle's parent's className is 'Transformer' — this
3131
+ // catches drags on the crop rect's resize/rotate handles.
3132
+ 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';
3133
+ if (croppingEl && croppingEl._cropModeEnabled && !isCropInteractive && !isTransformerHandle) {
3134
+ croppingEl.commitCropMode();
3135
+ store.closeToolbarPanel();
3136
+ store.history.addUndoState();
3137
+ }
3138
+ }
2930
3139
  var target = e.target;
2931
3140
 
2932
3141
  // Click on empty area or background -> deselect
@@ -2975,12 +3184,21 @@ var Workspace = observer(function (_ref6) {
2975
3184
  // Keyboard shortcuts
2976
3185
  useEffect(function () {
2977
3186
  var handleKeyDown = function handleKeyDown(e) {
3187
+ var _store$_toolbarPanel2;
2978
3188
  // Only handle if stage container or body is focused
2979
3189
  var target = e.target;
2980
3190
  if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.tagName === 'SELECT') {
2981
3191
  return;
2982
3192
  }
2983
3193
 
3194
+ // Escape -> cancel crop mode if active
3195
+ if (e.key === 'Escape' && ((_store$_toolbarPanel2 = store._toolbarPanel) === null || _store$_toolbarPanel2 === void 0 ? void 0 : _store$_toolbarPanel2.type) === 'crop') {
3196
+ var el = store.getElementById(store._toolbarPanel.elementId);
3197
+ if (el && el.cancelCropMode) el.cancelCropMode();
3198
+ store.closeToolbarPanel();
3199
+ e.preventDefault();
3200
+ }
3201
+
2984
3202
  // Delete / Backspace
2985
3203
  if (e.key === 'Delete' || e.key === 'Backspace') {
2986
3204
  var selectedIds = _toConsumableArray(store.selectedElementsIds);
@@ -3043,9 +3261,9 @@ var Workspace = observer(function (_ref6) {
3043
3261
  if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'G') {
3044
3262
  var _selectedIds3 = _toConsumableArray(store.selectedElementsIds);
3045
3263
  if (_selectedIds3.length === 1) {
3046
- var el = store.getElementById(_selectedIds3[0]);
3047
- if (el && el.type === 'group') {
3048
- store.ungroupElements([el.id]);
3264
+ var _el = store.getElementById(_selectedIds3[0]);
3265
+ if (_el && _el.type === 'group') {
3266
+ store.ungroupElements([_el.id]);
3049
3267
  store.history.addUndoState();
3050
3268
  e.preventDefault();
3051
3269
  }
@@ -3141,7 +3359,8 @@ var Workspace = observer(function (_ref6) {
3141
3359
  element: element,
3142
3360
  isSelected: store.selectedElementsIds.includes(element.id),
3143
3361
  onTransformEnd: handleTransformEnd,
3144
- onDragEnd: handleDragEnd
3362
+ onDragEnd: handleDragEnd,
3363
+ store: store
3145
3364
  }, element.id);
3146
3365
  }), /*#__PURE__*/jsx(PageOverlayFrame, {
3147
3366
  store: store