|
@@ -4,144 +4,255 @@ import { LayerEnum } from "@/enum/layerEnum";
|
|
|
import { useProjectStore } from "@/store/modules/project";
|
|
|
import { cloneDeep } from "lodash";
|
|
|
import { CustomElement } from "#/project";
|
|
|
+// import { recoverRecord } from "@/utils/recover";
|
|
|
|
|
|
+// type RecordItem = {
|
|
|
+// info: Record<string, any>;
|
|
|
+// type: "add" | "update" | "delete" | "init";
|
|
|
+// snapshot: ProjectInfo;
|
|
|
+// };
|
|
|
+type ActionState = {
|
|
|
+ // 操作记录--最大记录10条
|
|
|
+ records: string[];
|
|
|
+ // 当前操作索引
|
|
|
+ activeIndex: number;
|
|
|
+ appKey: number;
|
|
|
+};
|
|
|
export const useAcionStore = defineStore({
|
|
|
- id: 'action',
|
|
|
- state() {
|
|
|
+ id: "action",
|
|
|
+ state(): ActionState {
|
|
|
return {
|
|
|
- // 操作记录
|
|
|
records: [],
|
|
|
- }
|
|
|
+ activeIndex: -1,
|
|
|
+ appKey: 0,
|
|
|
+ };
|
|
|
},
|
|
|
getters: {
|
|
|
projectStore: () => useProjectStore(),
|
|
|
+ undoDisabled: (state) => state.activeIndex <= 0,
|
|
|
+ redoDisabled: (state) => state.activeIndex === state.records.length - 1,
|
|
|
},
|
|
|
actions: {
|
|
|
- addRecord(record) {
|
|
|
+ initRecord() {
|
|
|
+ this.records = [JSON.stringify(this.projectStore.projectInfo)];
|
|
|
+ this.activeIndex = 0;
|
|
|
+ },
|
|
|
+ // addRecord({type, info }: RecordItem & { snapshot?: ProjectInfo}) {
|
|
|
+ addRecord() {
|
|
|
+ // 新增如果当前索引不是最后一条, 覆盖后面的记录
|
|
|
+ if (this.activeIndex < this.records.length - 1) {
|
|
|
+ this.records.splice(this.activeIndex + 1, this.records.length);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.records.push(JSON.stringify(this.projectStore.projectInfo));
|
|
|
+
|
|
|
+ // 新增如果超过10条记录,删除最早的一条
|
|
|
+ if (this.records.length > 10) {
|
|
|
+ this.records.shift();
|
|
|
+ this.activeIndex--;
|
|
|
+ }
|
|
|
|
|
|
+ this.activeIndex = this.records.length - 1;
|
|
|
+ console.log(this.activeIndex, this.records);
|
|
|
},
|
|
|
+ // 撤销
|
|
|
actionUndo() {
|
|
|
-
|
|
|
+ if (this.activeIndex <= 0) return;
|
|
|
+ --this.activeIndex;
|
|
|
+ const projectInfo = JSON.parse(this.records[this.activeIndex]);
|
|
|
+ this.projectStore.updateProjectInfo(projectInfo);
|
|
|
+ this.appKey++;
|
|
|
},
|
|
|
+ // 重做
|
|
|
actionRedo() {
|
|
|
-
|
|
|
- },
|
|
|
- actionClear() {
|
|
|
-
|
|
|
+ if (this.activeIndex >= this.records.length - 1) return;
|
|
|
+ ++this.activeIndex;
|
|
|
+ const projectInfo = JSON.parse(this.records[this.activeIndex]);
|
|
|
+ this.projectStore.updateProjectInfo(projectInfo);
|
|
|
+ this.appKey++;
|
|
|
},
|
|
|
+ actionClear() {},
|
|
|
// 对齐
|
|
|
actionAlign(type: AlignEnum) {
|
|
|
const activeElements = this.projectStore.currentSelectedElements;
|
|
|
switch (type) {
|
|
|
case AlignEnum.Bottom: {
|
|
|
- const maxY = Math.max(...activeElements.map((item) => item.container.props.y + item.container.props.height));
|
|
|
+ const maxY = Math.max(
|
|
|
+ ...activeElements.map(
|
|
|
+ (item) => item.container.props.y + item.container.props.height
|
|
|
+ )
|
|
|
+ );
|
|
|
activeElements.forEach((item) => {
|
|
|
- this.projectStore.updateElement(item.key, 'container.props.y', maxY - item.container.props.height);
|
|
|
+ this.projectStore.updateElement(
|
|
|
+ item.key,
|
|
|
+ "container.props.y",
|
|
|
+ maxY - item.container.props.height
|
|
|
+ );
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
case AlignEnum.HorizontalCenter: {
|
|
|
- const maxX = Math.max(...activeElements.map((item) => item.container.props.x + item.container.props.width));
|
|
|
- const minX = Math.min(...activeElements.map((item) => item.container.props.x));
|
|
|
+ const maxX = Math.max(
|
|
|
+ ...activeElements.map(
|
|
|
+ (item) => item.container.props.x + item.container.props.width
|
|
|
+ )
|
|
|
+ );
|
|
|
+ const minX = Math.min(
|
|
|
+ ...activeElements.map((item) => item.container.props.x)
|
|
|
+ );
|
|
|
const centerX = minX + (maxX - minX) / 2;
|
|
|
activeElements.forEach((item) => {
|
|
|
- this.projectStore.updateElement(item.key, 'container.props.x', centerX - item.container.props.width / 2);
|
|
|
+ this.projectStore.updateElement(
|
|
|
+ item.key,
|
|
|
+ "container.props.x",
|
|
|
+ centerX - item.container.props.width / 2
|
|
|
+ );
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
case AlignEnum.VerticalCenter: {
|
|
|
- const maxY = Math.max(...activeElements.map((item) => item.container.props.y + item.container.props.height));
|
|
|
- const minY = Math.min(...activeElements.map((item) => item.container.props.y));
|
|
|
+ const maxY = Math.max(
|
|
|
+ ...activeElements.map(
|
|
|
+ (item) => item.container.props.y + item.container.props.height
|
|
|
+ )
|
|
|
+ );
|
|
|
+ const minY = Math.min(
|
|
|
+ ...activeElements.map((item) => item.container.props.y)
|
|
|
+ );
|
|
|
const centerY = minY + (maxY - minY) / 2;
|
|
|
activeElements.forEach((item) => {
|
|
|
- this.projectStore.updateElement(item.key, 'container.props.y', centerY - item.container.props.height / 2);
|
|
|
+ this.projectStore.updateElement(
|
|
|
+ item.key,
|
|
|
+ "container.props.y",
|
|
|
+ centerY - item.container.props.height / 2
|
|
|
+ );
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
case AlignEnum.Left: {
|
|
|
- const minX = Math.min(...activeElements.map((item) => item.container.props.x));
|
|
|
+ const minX = Math.min(
|
|
|
+ ...activeElements.map((item) => item.container.props.x)
|
|
|
+ );
|
|
|
activeElements.forEach((item) => {
|
|
|
- this.projectStore.updateElement(item.key, 'container.props.x', minX);
|
|
|
+ this.projectStore.updateElement(
|
|
|
+ item.key,
|
|
|
+ "container.props.x",
|
|
|
+ minX
|
|
|
+ );
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
case AlignEnum.Right: {
|
|
|
- const maxX = Math.max(...activeElements.map((item) => item.container.props.x + item.container.props.width));
|
|
|
+ const maxX = Math.max(
|
|
|
+ ...activeElements.map(
|
|
|
+ (item) => item.container.props.x + item.container.props.width
|
|
|
+ )
|
|
|
+ );
|
|
|
activeElements.forEach((item) => {
|
|
|
- this.projectStore.updateElement(item.key, 'container.props.x', maxX - item.container.props.width);
|
|
|
+ this.projectStore.updateElement(
|
|
|
+ item.key,
|
|
|
+ "container.props.x",
|
|
|
+ maxX - item.container.props.width
|
|
|
+ );
|
|
|
});
|
|
|
- break
|
|
|
+ break;
|
|
|
}
|
|
|
case AlignEnum.Top: {
|
|
|
- const minY = Math.min(...activeElements.map((item) => item.container.props.y));
|
|
|
- console.log(this.projectStore.currentSelectedElements, minY)
|
|
|
+ const minY = Math.min(
|
|
|
+ ...activeElements.map((item) => item.container.props.y)
|
|
|
+ );
|
|
|
+ console.log(this.projectStore.currentSelectedElements, minY);
|
|
|
activeElements.forEach((item) => {
|
|
|
- this.projectStore.updateElement(item.key, 'container.props.y', minY);
|
|
|
+ this.projectStore.updateElement(
|
|
|
+ item.key,
|
|
|
+ "container.props.y",
|
|
|
+ minY
|
|
|
+ );
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
- default:
|
|
|
+ default:
|
|
|
}
|
|
|
+ this.addRecord();
|
|
|
},
|
|
|
// 图层调整
|
|
|
actionLayer(type: LayerEnum) {
|
|
|
const activeElements = this.projectStore.currentSelectedElements;
|
|
|
- const elements = cloneDeep(this.projectStore.elements.sort((a, b) => a.zIndex - b.zIndex)) as CustomElement[];
|
|
|
+ const elements = cloneDeep(
|
|
|
+ this.projectStore.elements.sort((a, b) => a.zIndex - b.zIndex)
|
|
|
+ ) as CustomElement[];
|
|
|
|
|
|
switch (type) {
|
|
|
case LayerEnum.UP: {
|
|
|
activeElements.forEach((item) => {
|
|
|
- const index = elements.findIndex((element) => element.key === item.key);
|
|
|
+ const index = elements.findIndex(
|
|
|
+ (element) => element.key === item.key
|
|
|
+ );
|
|
|
if (item.zIndex === elements.length) return;
|
|
|
elements.splice(index, 1);
|
|
|
- elements.splice(index + 1, 0, {...item});
|
|
|
+ elements.splice(index + 1, 0, { ...item });
|
|
|
+ });
|
|
|
+ elements.forEach((item, index) => {
|
|
|
+ item.zIndex = index + 1;
|
|
|
});
|
|
|
- elements.forEach((item, index) => { item.zIndex = index + 1});
|
|
|
- elements.forEach(item => {
|
|
|
- this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
|
|
|
+ elements.forEach((item) => {
|
|
|
+ this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
case LayerEnum.DOWN: {
|
|
|
activeElements.forEach((item) => {
|
|
|
- const index = elements.findIndex((element) => element.key === item.key);
|
|
|
+ const index = elements.findIndex(
|
|
|
+ (element) => element.key === item.key
|
|
|
+ );
|
|
|
if (item.zIndex === 1) return;
|
|
|
elements.splice(index, 1);
|
|
|
- elements.splice(index - 1, 0, {...item});
|
|
|
+ elements.splice(index - 1, 0, { ...item });
|
|
|
});
|
|
|
- elements.forEach((item, index) => { item.zIndex = index + 1});
|
|
|
- elements.forEach(item => {
|
|
|
- this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
|
|
|
+ elements.forEach((item, index) => {
|
|
|
+ item.zIndex = index + 1;
|
|
|
+ });
|
|
|
+ elements.forEach((item) => {
|
|
|
+ this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
case LayerEnum.TOP: {
|
|
|
activeElements.forEach((item) => {
|
|
|
- const index = elements.findIndex((element) => element.key === item.key);
|
|
|
+ const index = elements.findIndex(
|
|
|
+ (element) => element.key === item.key
|
|
|
+ );
|
|
|
if (item.zIndex === elements.length) return;
|
|
|
elements.splice(index, 1);
|
|
|
- elements.push({...item});
|
|
|
+ elements.push({ ...item });
|
|
|
+ });
|
|
|
+ elements.forEach((item, index) => {
|
|
|
+ item.zIndex = index + 1;
|
|
|
});
|
|
|
- elements.forEach((item, index) => { item.zIndex = index + 1});
|
|
|
- elements.forEach(item => {
|
|
|
- this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
|
|
|
+ elements.forEach((item) => {
|
|
|
+ this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
case LayerEnum.BOTTOM: {
|
|
|
activeElements.forEach((item) => {
|
|
|
- const index = elements.findIndex((element) => element.key === item.key);
|
|
|
+ const index = elements.findIndex(
|
|
|
+ (element) => element.key === item.key
|
|
|
+ );
|
|
|
if (item.zIndex === 1) return;
|
|
|
elements.splice(index, 1);
|
|
|
- elements.unshift({...item});
|
|
|
+ elements.unshift({ ...item });
|
|
|
});
|
|
|
- elements.forEach((item, index) => { item.zIndex = index + 1});
|
|
|
- elements.forEach(item => {
|
|
|
- this.projectStore.updateElement(item.key, 'zIndex', item.zIndex);
|
|
|
+ elements.forEach((item, index) => {
|
|
|
+ item.zIndex = index + 1;
|
|
|
+ });
|
|
|
+ elements.forEach((item) => {
|
|
|
+ this.projectStore.updateElement(item.key, "zIndex", item.zIndex);
|
|
|
});
|
|
|
break;
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-})
|
|
|
+ }
|
|
|
+ this.addRecord();
|
|
|
+ },
|
|
|
+ },
|
|
|
+});
|