action.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { defineStore } from "pinia";
  2. import { AlignEnum } from "@/enum/alignEnum";
  3. import { LayerEnum } from "@/enum/layerEnum";
  4. import { useProjectStore } from "@/store/modules/project";
  5. import { cloneDeep } from "lodash";
  6. import { CustomElement } from "#/project";
  7. export const useAcionStore = defineStore({
  8. id: 'action',
  9. state() {
  10. return {
  11. // 操作记录
  12. records: [],
  13. }
  14. },
  15. getters: {
  16. projectStore: () => useProjectStore(),
  17. },
  18. actions: {
  19. addRecord(record) {
  20. },
  21. actionUndo() {
  22. },
  23. actionRedo() {
  24. },
  25. actionClear() {
  26. },
  27. // 对齐
  28. actionAlign(type: AlignEnum) {
  29. const activeElements = this.projectStore.currentSelectedElements;
  30. switch (type) {
  31. case AlignEnum.Bottom: {
  32. const maxY = Math.max(...activeElements.map((item) => item.container.props.y + item.container.props.height));
  33. activeElements.forEach((item) => {
  34. this.projectStore.updateElement(item.key, 'container.props.y', maxY - item.container.props.height);
  35. });
  36. break;
  37. }
  38. case AlignEnum.HorizontalCenter: {
  39. const maxX = Math.max(...activeElements.map((item) => item.container.props.x + item.container.props.width));
  40. const minX = Math.min(...activeElements.map((item) => item.container.props.x));
  41. const centerX = minX + (maxX - minX) / 2;
  42. activeElements.forEach((item) => {
  43. this.projectStore.updateElement(item.key, 'container.props.x', centerX - item.container.props.width / 2);
  44. });
  45. break;
  46. }
  47. case AlignEnum.VerticalCenter: {
  48. const maxY = Math.max(...activeElements.map((item) => item.container.props.y + item.container.props.height));
  49. const minY = Math.min(...activeElements.map((item) => item.container.props.y));
  50. const centerY = minY + (maxY - minY) / 2;
  51. activeElements.forEach((item) => {
  52. this.projectStore.updateElement(item.key, 'container.props.y', centerY - item.container.props.height / 2);
  53. });
  54. break;
  55. }
  56. case AlignEnum.Left: {
  57. const minX = Math.min(...activeElements.map((item) => item.container.props.x));
  58. activeElements.forEach((item) => {
  59. this.projectStore.updateElement(item.key, 'container.props.x', minX);
  60. });
  61. break;
  62. }
  63. case AlignEnum.Right: {
  64. const maxX = Math.max(...activeElements.map((item) => item.container.props.x + item.container.props.width));
  65. activeElements.forEach((item) => {
  66. this.projectStore.updateElement(item.key, 'container.props.x', maxX - item.container.props.width);
  67. });
  68. break
  69. }
  70. case AlignEnum.Top: {
  71. const minY = Math.min(...activeElements.map((item) => item.container.props.y));
  72. console.log(this.projectStore.currentSelectedElements, minY)
  73. activeElements.forEach((item) => {
  74. this.projectStore.updateElement(item.key, 'container.props.y', minY);
  75. });
  76. break;
  77. }
  78. default:
  79. }
  80. },
  81. // 图层调整
  82. actionLayer(type: LayerEnum) {
  83. const activeElements = this.projectStore.currentSelectedElements;
  84. const elements = cloneDeep(this.projectStore.elements.sort((a, b) => a.zIndex - b.zIndex)) as CustomElement[];
  85. switch (type) {
  86. case LayerEnum.UP: {
  87. activeElements.forEach((item) => {
  88. const index = elements.findIndex((element) => element.key === item.key);
  89. if (item.zIndex === elements.length) return;
  90. elements.splice(index, 1);
  91. elements.splice(index + 1, 0, {...item});
  92. });
  93. elements.forEach((item, index) => { item.zIndex = index + 1});
  94. elements.forEach(item => {
  95. this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
  96. });
  97. break;
  98. }
  99. case LayerEnum.DOWN: {
  100. activeElements.forEach((item) => {
  101. const index = elements.findIndex((element) => element.key === item.key);
  102. if (item.zIndex === 1) return;
  103. elements.splice(index, 1);
  104. elements.splice(index - 1, 0, {...item});
  105. });
  106. elements.forEach((item, index) => { item.zIndex = index + 1});
  107. elements.forEach(item => {
  108. this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
  109. });
  110. break;
  111. }
  112. case LayerEnum.TOP: {
  113. activeElements.forEach((item) => {
  114. const index = elements.findIndex((element) => element.key === item.key);
  115. if (item.zIndex === elements.length) return;
  116. elements.splice(index, 1);
  117. elements.push({...item});
  118. });
  119. elements.forEach((item, index) => { item.zIndex = index + 1});
  120. elements.forEach(item => {
  121. this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
  122. });
  123. break;
  124. }
  125. case LayerEnum.BOTTOM: {
  126. activeElements.forEach((item) => {
  127. const index = elements.findIndex((element) => element.key === item.key);
  128. if (item.zIndex === 1) return;
  129. elements.splice(index, 1);
  130. elements.unshift({...item});
  131. });
  132. elements.forEach((item, index) => { item.zIndex = index + 1});
  133. elements.forEach(item => {
  134. this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
  135. });
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. })