@mce/bigesj 0.15.5 → 0.15.7

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.
@@ -0,0 +1,15 @@
1
+ export interface BigeCropping {
2
+ imageWidth: number;
3
+ imageHeight: number;
4
+ maskWidth?: number;
5
+ maskHeight?: number;
6
+ translateX: number;
7
+ translateY: number;
8
+ zoom?: number;
9
+ }
10
+ export declare function croppingToCropRect(cropping: BigeCropping, width: number, height: number): {
11
+ left: number;
12
+ top: number;
13
+ right: number;
14
+ bottom: number;
15
+ };
@@ -1,5 +1,6 @@
1
1
  export * from './animation';
2
2
  export * from './background';
3
+ export * from './cropping';
3
4
  export * from './doc';
4
5
  export * from './element';
5
6
  export * from './image';
@@ -7,4 +8,5 @@ export * from './layout';
7
8
  export * from './style';
8
9
  export * from './svg';
9
10
  export * from './text';
11
+ export * from './transform';
10
12
  export * from './types';
@@ -1 +1 @@
1
- export declare function getStyle(el: Record<string, any>): any;
1
+ export declare function getStyle(el: Record<string, any>, clone?: boolean): any;
@@ -0,0 +1,15 @@
1
+ export interface BigeTransform {
2
+ imageWidth: number;
3
+ imageHeight: number;
4
+ originWidth?: number;
5
+ originHeight?: number;
6
+ translateX: number;
7
+ translateY: number;
8
+ zoom?: number;
9
+ }
10
+ export declare function transformToCropRect(transform: BigeTransform, width: number, height: number): {
11
+ left: number;
12
+ top: number;
13
+ right: number;
14
+ bottom: number;
15
+ };
package/dist/index.js CHANGED
@@ -585,10 +585,30 @@ function parseAnimations(el) {
585
585
  animations: animations2.filter((v) => !!v?.keyframes)
586
586
  };
587
587
  }
588
+ function transformToCropRect(transform, width, height) {
589
+ const {
590
+ imageWidth,
591
+ imageHeight,
592
+ originWidth: _originWidth = width,
593
+ originHeight: _originHeight = height,
594
+ translateX,
595
+ translateY,
596
+ zoom = 1
597
+ } = transform;
598
+ const left = -(imageWidth * (zoom - 1) / 2) + translateX * zoom;
599
+ const top = -(imageHeight * (zoom - 1) / 2) + translateY * zoom;
600
+ const right = left + imageWidth * zoom;
601
+ const bottom = top + imageHeight * zoom;
602
+ return {
603
+ left: -left / width,
604
+ top: -top / height,
605
+ right: -(width - right) / width,
606
+ bottom: -(height - bottom) / height
607
+ };
608
+ }
588
609
  function convertBackground(el) {
589
610
  let background;
590
611
  const backgroundColor = el.backgroundColor ?? el.background?.color;
591
- const backgroundImage = el.backgroundImage ?? el.background?.image;
592
612
  if (backgroundColor) {
593
613
  if (isGradient(backgroundColor ?? "")) {
594
614
  background = {
@@ -600,13 +620,49 @@ function convertBackground(el) {
600
620
  };
601
621
  }
602
622
  }
623
+ const backgroundImage = el.backgroundImage ?? el.background?.image;
603
624
  if (backgroundImage) {
604
625
  background = {
605
- image: backgroundImage
626
+ image: backgroundImage.match(/url\((.+)\)/)?.[1] ?? backgroundImage
606
627
  };
607
628
  }
629
+ const backgroundTransform = el.backgroundTransform ?? el.background?.transform;
630
+ if (background && backgroundTransform) {
631
+ background.cropRect = transformToCropRect(
632
+ backgroundTransform,
633
+ el.style.width,
634
+ el.style.height
635
+ );
636
+ }
608
637
  return background;
609
638
  }
639
+ function croppingToCropRect(cropping, width, height) {
640
+ const {
641
+ imageWidth,
642
+ imageHeight,
643
+ maskWidth = width,
644
+ maskHeight = height,
645
+ translateX,
646
+ translateY,
647
+ zoom = 1
648
+ } = cropping;
649
+ const cvsWidth = imageWidth * zoom;
650
+ const cvsHeight = imageHeight * zoom;
651
+ const distX = (cvsWidth - maskWidth) / 2 - translateX;
652
+ const distY = (cvsHeight - maskHeight) / 2 - translateY;
653
+ const originX = distX + maskWidth / 2;
654
+ const originY = distY + maskHeight / 2;
655
+ const left = -(width / 2 - originX);
656
+ const top = -(height / 2 - originY);
657
+ const right = cvsWidth - (left + width);
658
+ const bottom = cvsHeight - (top + height);
659
+ return {
660
+ left: left / cvsWidth,
661
+ top: top / cvsHeight,
662
+ right: right / cvsWidth,
663
+ bottom: bottom / cvsHeight
664
+ };
665
+ }
610
666
  async function convertImageElementToUrl(el) {
611
667
  const {
612
668
  transform = {},
@@ -1168,8 +1224,23 @@ function convertSvgLinearGradient(value) {
1168
1224
  return `linear-gradient(${args.join(", ")})`;
1169
1225
  }
1170
1226
  }
1171
- function getStyle(el) {
1172
- return el.style ?? el;
1227
+ function getStyle(el, clone = false) {
1228
+ let style = el.style ?? el;
1229
+ if (clone) {
1230
+ style = { ...style };
1231
+ delete style.right;
1232
+ delete style.bottom;
1233
+ delete style.backgroundColor;
1234
+ delete style.backgroundImage;
1235
+ delete style.backgroundImageOpacity;
1236
+ delete style.backgroundPosition;
1237
+ delete style.backgroundSize;
1238
+ delete style.backgroundTransform;
1239
+ delete style.backgroundUrl;
1240
+ delete style.elements;
1241
+ delete style.imageTransform;
1242
+ }
1243
+ return style;
1173
1244
  }
1174
1245
  async function convertSvgElementToUrl(el) {
1175
1246
  const {
@@ -1388,9 +1459,8 @@ async function convertTextContent(el, isByWord = false) {
1388
1459
  }
1389
1460
  const percentageToPx = (per) => (Number.parseFloat(per) || 0) / 100;
1390
1461
  async function convertElement(el, parent, context) {
1391
- const style = { ...getStyle(el) };
1392
- delete style.bottom;
1393
- delete style.right;
1462
+ const oldStyle = getStyle(el);
1463
+ const style = getStyle(el, true);
1394
1464
  const { children: _children, ...raw } = el;
1395
1465
  const meta = {
1396
1466
  raw,
@@ -1399,19 +1469,25 @@ async function convertElement(el, parent, context) {
1399
1469
  };
1400
1470
  const element = {
1401
1471
  id: idGenerator(),
1402
- name: el.name ?? el.title ?? el.id,
1472
+ name: el.name ?? el.title,
1403
1473
  style,
1404
1474
  meta,
1405
- children: []
1475
+ children: [],
1476
+ background: convertBackground(el)
1406
1477
  };
1407
- if (style.borderRadius) {
1408
- style.borderRadius = style.borderRadius * 0.01 * Math.max(style.height, style.width);
1478
+ if (oldStyle.borderRadius) {
1479
+ style.borderRadius = oldStyle.borderRadius * 0.01 * Math.max(oldStyle.height, oldStyle.width);
1409
1480
  }
1410
- if (parent && el.groupStyle) {
1411
- style.width = parent.style.width * percentageToPx(el.groupStyle.width);
1412
- style.height = parent.style.height * percentageToPx(el.groupStyle.height);
1413
- style.left = parent.style.width * percentageToPx(el.groupStyle.left);
1414
- style.top = parent.style.height * percentageToPx(el.groupStyle.top);
1481
+ if (parent) {
1482
+ if (el.groupStyle) {
1483
+ style.width = parent.style.width * percentageToPx(el.groupStyle.width);
1484
+ style.height = parent.style.height * percentageToPx(el.groupStyle.height);
1485
+ style.left = parent.style.width * percentageToPx(el.groupStyle.left);
1486
+ style.top = parent.style.height * percentageToPx(el.groupStyle.top);
1487
+ } else {
1488
+ style.left = oldStyle.left - parent.left;
1489
+ style.top = oldStyle.top - parent.top;
1490
+ }
1415
1491
  }
1416
1492
  if (el.editable === false) {
1417
1493
  style.visibility = "hidden";
@@ -1447,33 +1523,17 @@ async function convertElement(el, parent, context) {
1447
1523
  meta.rawForegroundImage = el.url;
1448
1524
  }
1449
1525
  if (el.cropping) {
1450
- const width = el.style.width;
1451
- const height = el.style.height;
1452
- const {
1453
- imageWidth,
1454
- imageHeight,
1455
- maskWidth = width,
1456
- maskHeight = height,
1457
- translateX,
1458
- translateY,
1459
- zoom = 1
1460
- } = el.cropping;
1461
- const cvsWidth = imageWidth * zoom;
1462
- const cvsHeight = imageHeight * zoom;
1463
- const distX = (cvsWidth - maskWidth) / 2 - translateX;
1464
- const distY = (cvsHeight - maskHeight) / 2 - translateY;
1465
- const originX = distX + maskWidth / 2;
1466
- const originY = distY + maskHeight / 2;
1467
- const left = -(width / 2 - originX);
1468
- const top = -(height / 2 - originY);
1469
- const right = cvsWidth - (left + width);
1470
- const bottom = cvsHeight - (top + height);
1471
- element.foreground.cropRect = {
1472
- left: left / cvsWidth,
1473
- top: top / cvsHeight,
1474
- right: right / cvsWidth,
1475
- bottom: bottom / cvsHeight
1476
- };
1526
+ element.foreground.cropRect = croppingToCropRect(
1527
+ el.cropping,
1528
+ el.style.width,
1529
+ el.style.height
1530
+ );
1531
+ } else if (el.transform) {
1532
+ element.foreground.cropRect = transformToCropRect(
1533
+ el.transform,
1534
+ el.style.width,
1535
+ el.style.height
1536
+ );
1477
1537
  }
1478
1538
  break;
1479
1539
  case "svg": {
@@ -1562,9 +1622,7 @@ async function convertElement(el, parent, context) {
1562
1622
  }
1563
1623
  async function convertLayout(layout, isFrame = true, context) {
1564
1624
  const id = idGenerator();
1565
- const style = { ...getStyle(layout) };
1566
- delete style.right;
1567
- delete style.bottom;
1625
+ const style = getStyle(layout, true);
1568
1626
  if (isFrame) {
1569
1627
  style.overflow = "hidden";
1570
1628
  }
@@ -1813,11 +1871,13 @@ export {
1813
1871
  convertTextContent,
1814
1872
  convertTextEffects,
1815
1873
  convertTextStyle,
1874
+ croppingToCropRect,
1816
1875
  plugin as default,
1817
1876
  getStyle,
1818
1877
  getTextContents,
1819
1878
  parseAnimations,
1820
1879
  plugin,
1880
+ transformToCropRect,
1821
1881
  useFonts,
1822
1882
  useSharedTextAssets
1823
1883
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mce/bigesj",
3
3
  "type": "module",
4
- "version": "0.15.5",
4
+ "version": "0.15.7",
5
5
  "description": "Plugin for mce",
6
6
  "author": "wxm",
7
7
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "modern-openxml": "^1.10.1"
50
50
  },
51
51
  "devDependencies": {
52
- "mce": "0.15.5"
52
+ "mce": "0.15.7"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "mce": "^0"