|
@@ -1,9 +1,10 @@
|
|
|
-import { defineStore } from "pinia";
|
|
|
import type { ProjectInfo, Page, ReferLine, CustomElement } from "#/project";
|
|
|
import type { ComponentType } from "@/components";
|
|
|
-import componentAll from '@/components';
|
|
|
+import { defineStore } from "pinia";
|
|
|
+import componentAll from "@/components";
|
|
|
import { ScreenFillEnum } from "@/enum/screenFillEnum";
|
|
|
-import { update } from "lodash";
|
|
|
+import { update, defaultsDeep } from "lodash";
|
|
|
+import { getNormalizedContainer } from "@/utils/common";
|
|
|
|
|
|
type ProjectState = {
|
|
|
projectInfo: ProjectInfo;
|
|
@@ -12,48 +13,48 @@ type ProjectState = {
|
|
|
key: number;
|
|
|
name: string;
|
|
|
componentType: ComponentType;
|
|
|
- position: {
|
|
|
- x: number;
|
|
|
- y: number;
|
|
|
+ container: {
|
|
|
+ style: Record<string, any>;
|
|
|
+ props: Record<string, any>;
|
|
|
};
|
|
|
} | null;
|
|
|
- mode: 'edit' | 'preview';
|
|
|
+ mode: "edit" | "preview";
|
|
|
selectedElementKeys: number[];
|
|
|
-}
|
|
|
+};
|
|
|
const defaultPage: Page = {
|
|
|
- name: '页面1',
|
|
|
+ name: "页面1",
|
|
|
background: {
|
|
|
- type: 'color',
|
|
|
- color: '#0b074b',
|
|
|
- image: '',
|
|
|
- fillType: ''
|
|
|
+ type: "color",
|
|
|
+ color: "#0b074b",
|
|
|
+ image: "",
|
|
|
+ fillType: "",
|
|
|
},
|
|
|
elements: [],
|
|
|
- referLines: []
|
|
|
-}
|
|
|
-const CURRENT_PROJECT = 'currentProject';
|
|
|
+ referLines: [],
|
|
|
+};
|
|
|
+const CURRENT_PROJECT = "currentProject";
|
|
|
|
|
|
export const useProjectStore = defineStore({
|
|
|
- id: 'project',
|
|
|
+ id: "project",
|
|
|
state: (): ProjectState => ({
|
|
|
// 项目信息
|
|
|
projectInfo: {
|
|
|
- name: '',
|
|
|
- description: '',
|
|
|
- sizeType: '',
|
|
|
+ name: "",
|
|
|
+ description: "",
|
|
|
+ sizeType: "",
|
|
|
width: 0,
|
|
|
height: 0,
|
|
|
fillType: ScreenFillEnum.Auto,
|
|
|
- pages: [{ ...defaultPage}]
|
|
|
+ pages: [{ ...defaultPage }],
|
|
|
},
|
|
|
// 当前编辑页面索引
|
|
|
activePageIndex: 0,
|
|
|
// 添加组件临时数据
|
|
|
addCompData: null,
|
|
|
// 视图模式
|
|
|
- mode: 'edit',
|
|
|
+ mode: "edit",
|
|
|
// 选中的元素
|
|
|
- selectedElementKeys: []
|
|
|
+ selectedElementKeys: [],
|
|
|
}),
|
|
|
getters: {
|
|
|
referLines(state) {
|
|
@@ -64,7 +65,7 @@ export const useProjectStore = defineStore({
|
|
|
},
|
|
|
currentPage(state) {
|
|
|
return state.projectInfo.pages[state.activePageIndex];
|
|
|
- }
|
|
|
+ },
|
|
|
},
|
|
|
actions: {
|
|
|
setProjectInfo(info: any) {
|
|
@@ -72,7 +73,7 @@ export const useProjectStore = defineStore({
|
|
|
localStorage.setItem(CURRENT_PROJECT, JSON.stringify(info));
|
|
|
},
|
|
|
getCurrentProjectInfo(): ProjectInfo | undefined {
|
|
|
- const info = JSON.parse(localStorage.getItem(CURRENT_PROJECT) || '');
|
|
|
+ const info = JSON.parse(localStorage.getItem(CURRENT_PROJECT) || "");
|
|
|
this.setProjectInfo(info as unknown as ProjectInfo);
|
|
|
return info;
|
|
|
},
|
|
@@ -81,7 +82,11 @@ export const useProjectStore = defineStore({
|
|
|
},
|
|
|
removeReferLine(key: number) {
|
|
|
const index = this.referLines.findIndex((line) => line.key === key);
|
|
|
- index !== -1 && this.projectInfo.pages[this.activePageIndex].referLines.splice(index, 1);
|
|
|
+ index !== -1 &&
|
|
|
+ this.projectInfo.pages[this.activePageIndex].referLines.splice(
|
|
|
+ index,
|
|
|
+ 1
|
|
|
+ );
|
|
|
},
|
|
|
updateReferLine(line: ReferLine) {
|
|
|
const index = this.referLines.findIndex((l) => l.key === line.key);
|
|
@@ -90,37 +95,61 @@ export const useProjectStore = defineStore({
|
|
|
}
|
|
|
},
|
|
|
// 添加组件
|
|
|
- async addElement(element: CustomElement) {
|
|
|
+ async addElement(element: any) {
|
|
|
this.addCompData = null;
|
|
|
- if(!element) return;
|
|
|
+ if (!element) return;
|
|
|
|
|
|
const elements = this.projectInfo.pages[this.activePageIndex].elements;
|
|
|
- // 获取组件默认属性
|
|
|
- const { defaultPropsValue } = await componentAll[element.componentType]?.() || {};
|
|
|
- const index = elements.filter(item => item.componentType === element.componentType).length + 1;
|
|
|
- const width = defaultPropsValue?.width || 400;
|
|
|
- const height = defaultPropsValue?.height || 260;
|
|
|
+ // 获取每个自定义组件暴露出来的属性
|
|
|
+ const { defaultPropsValue } =
|
|
|
+ (await componentAll[element.componentType as ComponentType]?.()) || {};
|
|
|
+ const { width = 400, height = 260 } = defaultPropsValue?.container?.props || {};
|
|
|
+ const { props: containerProps = {}, style = {} } = defaultPropsValue?.container || {};
|
|
|
|
|
|
+ const index =
|
|
|
+ elements.filter((item) => item.componentType === element.componentType)
|
|
|
+ .length + 1;
|
|
|
+
|
|
|
+ const { x, y } = element.container.props;
|
|
|
+ const container = getNormalizedContainer({
|
|
|
+ style,
|
|
|
+ props: {
|
|
|
+ ...containerProps,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ x: x - width / 2,
|
|
|
+ y: y - height / 2,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // 添加组件
|
|
|
this.projectInfo.pages[this.activePageIndex].elements.push({
|
|
|
- ...element,
|
|
|
+ ...defaultsDeep(element, defaultPropsValue),
|
|
|
name: element.name + index,
|
|
|
zIndex: elements.length + 1,
|
|
|
- props: defaultPropsValue,
|
|
|
visible: true,
|
|
|
locked: false,
|
|
|
- position: {
|
|
|
- x: element.position.x - (width / 2),
|
|
|
- y: element.position.y - (height / 2)
|
|
|
- }
|
|
|
+ container,
|
|
|
});
|
|
|
|
|
|
this.selectedElementKeys = [element.key];
|
|
|
},
|
|
|
// 更新组件
|
|
|
updateElement(key: number, path: string, payload: any) {
|
|
|
- const element = this.projectInfo.pages[this.activePageIndex].elements.find((item) => item.key === key);
|
|
|
+ const element = this.projectInfo.pages[
|
|
|
+ this.activePageIndex
|
|
|
+ ].elements.find((item) => item.key === key);
|
|
|
// 如果是锁定状态不能修改宽高 位置
|
|
|
- if(element && element.locked && ['props.width', 'props.height', 'position'].includes(path)) return;
|
|
|
+ if (
|
|
|
+ element &&
|
|
|
+ element.locked &&
|
|
|
+ [
|
|
|
+ "container.props.width",
|
|
|
+ "container.props.height",
|
|
|
+ "container.props.x",
|
|
|
+ "container.props.y",
|
|
|
+ ].includes(path)
|
|
|
+ )
|
|
|
+ return;
|
|
|
|
|
|
if (element) {
|
|
|
update(element, path, () => payload);
|
|
@@ -128,7 +157,9 @@ export const useProjectStore = defineStore({
|
|
|
},
|
|
|
// 删除组件
|
|
|
removeElement(key: number) {
|
|
|
- const index = this.projectInfo.pages[this.activePageIndex].elements.findIndex((item) => item.key === key);
|
|
|
+ const index = this.projectInfo.pages[
|
|
|
+ this.activePageIndex
|
|
|
+ ].elements.findIndex((item) => item.key === key);
|
|
|
if (index !== -1) {
|
|
|
this.projectInfo.pages[this.activePageIndex].elements.splice(index, 1);
|
|
|
}
|
|
@@ -141,7 +172,7 @@ export const useProjectStore = defineStore({
|
|
|
clearAddCompData() {
|
|
|
this.addCompData = null;
|
|
|
},
|
|
|
- setMode(mode: 'edit' | 'preview') {
|
|
|
+ setMode(mode: "edit" | "preview") {
|
|
|
this.mode = mode;
|
|
|
},
|
|
|
// 设置选中的元素
|
|
@@ -152,5 +183,5 @@ export const useProjectStore = defineStore({
|
|
|
clearAllSelectedElement() {
|
|
|
this.selectedElementKeys = [];
|
|
|
},
|
|
|
- }
|
|
|
-});
|
|
|
+ },
|
|
|
+});
|