action.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. import { uuid } from "@/utils";
  8. // import { recoverRecord } from "@/utils/recover";
  9. // type RecordItem = {
  10. // info: Record<string, any>;
  11. // type: "add" | "update" | "delete" | "init";
  12. // snapshot: ProjectInfo;
  13. // };
  14. type ActionState = {
  15. // 操作记录--最大记录10条
  16. records: string[];
  17. // 当前操作索引
  18. activeIndex: number;
  19. appKey: number;
  20. copyCache: any;
  21. };
  22. export const useAcionStore = defineStore({
  23. id: "action",
  24. state(): ActionState {
  25. return {
  26. records: [],
  27. activeIndex: -1,
  28. appKey: 0,
  29. copyCache: null,
  30. };
  31. },
  32. getters: {
  33. projectStore: () => useProjectStore(),
  34. undoDisabled: (state) => state.activeIndex <= 0,
  35. redoDisabled: (state) => state.activeIndex === state.records.length - 1,
  36. },
  37. actions: {
  38. initRecord() {
  39. this.records = [JSON.stringify(this.projectStore.projectInfo)];
  40. this.activeIndex = 0;
  41. },
  42. // addRecord({type, info }: RecordItem & { snapshot?: ProjectInfo}) {
  43. addRecord() {
  44. // 新增如果当前索引不是最后一条, 覆盖后面的记录
  45. if (this.activeIndex < this.records.length - 1) {
  46. this.records.splice(this.activeIndex + 1, this.records.length);
  47. }
  48. this.records.push(JSON.stringify(this.projectStore.projectInfo));
  49. // 新增如果超过10条记录,删除最早的一条
  50. if (this.records.length > 10) {
  51. this.records.shift();
  52. this.activeIndex--;
  53. }
  54. this.activeIndex = this.records.length - 1;
  55. },
  56. /* 撤销 */
  57. actionUndo() {
  58. if (this.activeIndex <= 0) return;
  59. --this.activeIndex;
  60. const projectInfo = JSON.parse(this.records[this.activeIndex]);
  61. this.projectStore.updateProjectInfo(projectInfo);
  62. this.appKey++;
  63. },
  64. /* 重做 */
  65. actionRedo() {
  66. ++this.activeIndex;
  67. const projectInfo = JSON.parse(this.records[this.activeIndex]);
  68. this.projectStore.updateProjectInfo(projectInfo);
  69. this.appKey++;
  70. },
  71. actionClear() {},
  72. /* 对齐 */
  73. actionAlign(type: AlignEnum) {
  74. const activeElements = this.projectStore.currentSelectedElements;
  75. switch (type) {
  76. case AlignEnum.Bottom: {
  77. const maxY = Math.max(
  78. ...activeElements.map(
  79. (item) => item.container.props.y + item.container.props.height
  80. )
  81. );
  82. activeElements.forEach((item) => {
  83. this.projectStore.updateElement(
  84. item.key,
  85. "container.props.y",
  86. maxY - item.container.props.height
  87. );
  88. });
  89. break;
  90. }
  91. case AlignEnum.HorizontalCenter: {
  92. const maxX = Math.max(
  93. ...activeElements.map(
  94. (item) => item.container.props.x + item.container.props.width
  95. )
  96. );
  97. const minX = Math.min(
  98. ...activeElements.map((item) => item.container.props.x)
  99. );
  100. const centerX = minX + (maxX - minX) / 2;
  101. activeElements.forEach((item) => {
  102. this.projectStore.updateElement(
  103. item.key,
  104. "container.props.x",
  105. centerX - item.container.props.width / 2
  106. );
  107. });
  108. break;
  109. }
  110. case AlignEnum.VerticalCenter: {
  111. const maxY = Math.max(
  112. ...activeElements.map(
  113. (item) => item.container.props.y + item.container.props.height
  114. )
  115. );
  116. const minY = Math.min(
  117. ...activeElements.map((item) => item.container.props.y)
  118. );
  119. const centerY = minY + (maxY - minY) / 2;
  120. activeElements.forEach((item) => {
  121. this.projectStore.updateElement(
  122. item.key,
  123. "container.props.y",
  124. centerY - item.container.props.height / 2
  125. );
  126. });
  127. break;
  128. }
  129. case AlignEnum.Left: {
  130. const minX = Math.min(
  131. ...activeElements.map((item) => item.container.props.x)
  132. );
  133. activeElements.forEach((item) => {
  134. this.projectStore.updateElement(
  135. item.key,
  136. "container.props.x",
  137. minX
  138. );
  139. });
  140. break;
  141. }
  142. case AlignEnum.Right: {
  143. const maxX = Math.max(
  144. ...activeElements.map(
  145. (item) => item.container.props.x + item.container.props.width
  146. )
  147. );
  148. activeElements.forEach((item) => {
  149. this.projectStore.updateElement(
  150. item.key,
  151. "container.props.x",
  152. maxX - item.container.props.width
  153. );
  154. });
  155. break;
  156. }
  157. case AlignEnum.Top: {
  158. const minY = Math.min(
  159. ...activeElements.map((item) => item.container.props.y)
  160. );
  161. activeElements.forEach((item) => {
  162. this.projectStore.updateElement(
  163. item.key,
  164. "container.props.y",
  165. minY
  166. );
  167. });
  168. break;
  169. }
  170. default:
  171. }
  172. this.addRecord();
  173. },
  174. /* 图层调整 */
  175. actionLayer(type: LayerEnum) {
  176. const activeElements = this.projectStore.currentSelectedElements;
  177. const elements = cloneDeep(
  178. this.projectStore.elements.sort((a, b) => a.zIndex - b.zIndex)
  179. ) as CustomElement[];
  180. switch (type) {
  181. case LayerEnum.UP: {
  182. activeElements.forEach((item) => {
  183. const index = elements.findIndex(
  184. (element) => element.key === item.key
  185. );
  186. if (item.zIndex === elements.length) return;
  187. elements.splice(index, 1);
  188. elements.splice(index + 1, 0, { ...item });
  189. });
  190. elements.forEach((item, index) => {
  191. item.zIndex = index + 1;
  192. });
  193. elements.forEach((item) => {
  194. this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
  195. });
  196. break;
  197. }
  198. case LayerEnum.DOWN: {
  199. activeElements.forEach((item) => {
  200. const index = elements.findIndex(
  201. (element) => element.key === item.key
  202. );
  203. if (item.zIndex === 1) return;
  204. elements.splice(index, 1);
  205. elements.splice(index - 1, 0, { ...item });
  206. });
  207. elements.forEach((item, index) => {
  208. item.zIndex = index + 1;
  209. });
  210. elements.forEach((item) => {
  211. this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
  212. });
  213. break;
  214. }
  215. case LayerEnum.TOP: {
  216. activeElements.forEach((item) => {
  217. const index = elements.findIndex(
  218. (element) => element.key === item.key
  219. );
  220. if (item.zIndex === elements.length) return;
  221. elements.splice(index, 1);
  222. elements.push({ ...item });
  223. });
  224. elements.forEach((item, index) => {
  225. item.zIndex = index + 1;
  226. });
  227. elements.forEach((item) => {
  228. this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
  229. });
  230. break;
  231. }
  232. case LayerEnum.BOTTOM: {
  233. activeElements.forEach((item) => {
  234. const index = elements.findIndex(
  235. (element) => element.key === item.key
  236. );
  237. if (item.zIndex === 1) return;
  238. elements.splice(index, 1);
  239. elements.unshift({ ...item });
  240. });
  241. elements.forEach((item, index) => {
  242. item.zIndex = index + 1;
  243. });
  244. elements.forEach((item) => {
  245. this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
  246. });
  247. break;
  248. }
  249. }
  250. this.addRecord();
  251. },
  252. /* 添加组合 */
  253. actionGroup() {
  254. const elements = this.projectStore.currentSelectedElements;
  255. const key = uuid();
  256. // 1、移除元素
  257. elements.forEach((element) => {
  258. this.projectStore.removeElement(element.key);
  259. });
  260. const minX = Math.min(...elements.map((item) => item.container.props.x));
  261. const minY = Math.min(...elements.map((item) => item.container.props.y));
  262. const maxX = Math.max(...elements.map((item) => item.container.props.x + item.container.props.width));
  263. const maxY = Math.max(...elements.map((item) => item.container.props.y + item.container.props.height));
  264. const maxZIndex = Math.max(...elements.map((item) => item.zIndex));
  265. const groupIndex = this.projectStore.elements.filter((item) => item.componentType === "group").length + 1;
  266. // 重新计算子元素位置
  267. elements.forEach((item) => {
  268. item.container.props.x -= minX;
  269. item.container.props.y -= minY;
  270. item.parentKey = key;
  271. });
  272. const group: CustomElement = {
  273. key,
  274. name: "组合" + groupIndex,
  275. componentType: "group",
  276. visible: true,
  277. locked: false,
  278. zIndex: maxZIndex,
  279. container: {
  280. style: {},
  281. props: {
  282. width: maxX - minX,
  283. height: maxY - minY,
  284. x: minX,
  285. y: minY,
  286. },
  287. },
  288. children: elements,
  289. collapsed: false,
  290. events: [],
  291. animations: [],
  292. props: {}
  293. }
  294. // 2、添加组合元素
  295. this.projectStore.addElement(group);
  296. },
  297. /* 拆分组合元素 */
  298. actionUngroup() {
  299. const group = this.projectStore.currentSelectedElements[0];
  300. // 1、取出子元素
  301. const elements = group.children?.map((item) => {
  302. // 2、计算子元素位置
  303. item.container.props.x += group.container.props.x;
  304. item.container.props.y += group.container.props.y;
  305. delete item.parentKey;
  306. return item;
  307. });
  308. // 3、移除组
  309. this.projectStore.removeElement(group.key);
  310. // 4、添加子元素
  311. elements?.forEach((item) => {
  312. this.projectStore.addElement(item, undefined, true);
  313. });
  314. },
  315. /* 复制 */
  316. actionCopy() {
  317. const elements = this.projectStore.currentSelectedElements;
  318. this.copyCache = JSON.stringify(elements);
  319. },
  320. /* 粘贴 */
  321. actionPaste() {
  322. try {
  323. const elements = JSON.parse(this.copyCache);
  324. const offsetX = 10;
  325. const offsetY = 10;
  326. elements.forEach((element: CustomElement) => {
  327. element.key = uuid();
  328. element.container.props.x += offsetX;
  329. element.container.props.y += offsetY;
  330. this.projectStore.addElement(element);
  331. });
  332. } catch (error) {
  333. console.log(error);
  334. }
  335. }
  336. },
  337. });