scale.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * 对选中的组件进行缩放
  3. * 如果操作的是group组件,递归缩放子元素
  4. *
  5. * @param projectStore 项目store
  6. * @param type 缩放类型
  7. * @param moveX x轴移动距离
  8. * @param moveY y轴移动距离
  9. * @param pathPrefix 路径前缀
  10. * @param elements 选中的组件
  11. * @param parentKey 父组件key
  12. * @returns void
  13. *
  14. */
  15. import { CustomElement } from "#/project";
  16. export const scaleAction = ({
  17. projectStore,
  18. type,
  19. moveX,
  20. moveY,
  21. pathPrefix = "",
  22. elements,
  23. parentKey,
  24. }: {
  25. projectStore: any;
  26. type: string;
  27. moveX: number;
  28. moveY: number;
  29. elements: CustomElement[];
  30. pathPrefix?: string;
  31. parentKey?: string;
  32. }) => {
  33. // 对选中的组件进行缩放
  34. elements.forEach((item, index) => {
  35. let { x, y, width, height } = item.container.props || {};
  36. switch (type) {
  37. case "top-left":
  38. width -= moveX;
  39. height -= moveY;
  40. if (!parentKey) {
  41. x += moveX;
  42. y += moveY;
  43. }
  44. break;
  45. case "top-center":
  46. height -= moveY;
  47. if (!parentKey) {
  48. y += moveY;
  49. }
  50. break;
  51. case "top-right":
  52. width += moveX;
  53. height -= moveY;
  54. if (!parentKey) {
  55. y += moveY;
  56. }
  57. break;
  58. case "left-center":
  59. width -= moveX;
  60. if (!parentKey) {
  61. x += moveX;
  62. }
  63. break;
  64. case "right-center":
  65. width += moveX;
  66. break;
  67. case "bottom-left":
  68. width -= moveX;
  69. height += moveY;
  70. if (!parentKey) {
  71. x += moveX;
  72. }
  73. break;
  74. case "bottom-center":
  75. height += moveY;
  76. break;
  77. case "bottom-right":
  78. width += moveX;
  79. height += moveY;
  80. break;
  81. }
  82. if (width < 10 || height < 10) return;
  83. const prefix = pathPrefix ? `${pathPrefix}[${index}].` : "";
  84. projectStore.updateElement(
  85. parentKey || item.key,
  86. prefix + "container.props.x",
  87. Math.round(x)
  88. );
  89. projectStore.updateElement(
  90. parentKey || item.key,
  91. prefix + "container.props.y",
  92. Math.round(y)
  93. );
  94. projectStore.updateElement(
  95. parentKey || item.key,
  96. prefix + "container.props.width",
  97. Math.round(width)
  98. );
  99. projectStore.updateElement(
  100. parentKey || item.key,
  101. prefix + "container.props.height",
  102. Math.round(height)
  103. );
  104. // 如果是group组件,递归缩放子元素
  105. if (item.componentType === "group") {
  106. scaleAction({
  107. projectStore,
  108. type,
  109. moveX,
  110. moveY,
  111. pathPrefix: `children`,
  112. elements: item.children || [],
  113. parentKey: item.key,
  114. });
  115. }
  116. });
  117. };