|
@@ -1,5 +1,6 @@
|
|
|
import { defineStore } from "pinia";
|
|
|
import { DrawData, DrawItem, ShapeType } from "../components";
|
|
|
+import { defaultLayer } from "@/constant";
|
|
|
|
|
|
const sortFn = (
|
|
|
a: Pick<DrawItem, "zIndex" | "createTime">,
|
|
@@ -10,25 +11,48 @@ export type StoreConfig = {
|
|
|
compass: {
|
|
|
rotation: number;
|
|
|
url?: string;
|
|
|
- }
|
|
|
+ };
|
|
|
};
|
|
|
export type StoreData = {
|
|
|
- typeItems: DrawData;
|
|
|
+ layers: Record<string, DrawData>,
|
|
|
config: StoreConfig;
|
|
|
+ __currentLayer: string;
|
|
|
};
|
|
|
const defConfig: StoreData["config"] = {
|
|
|
compass: { rotation: 0 },
|
|
|
};
|
|
|
|
|
|
export const getEmptyStoreData = (): StoreData => {
|
|
|
- return { typeItems: {}, config: { ...defConfig } };
|
|
|
+ return {
|
|
|
+ layers: {},
|
|
|
+ config: { ...defConfig },
|
|
|
+ __currentLayer: defaultLayer,
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
export const useStoreRaw = defineStore("draw-data", {
|
|
|
state: () => ({ data: getEmptyStoreData() }),
|
|
|
getters: {
|
|
|
+ currentLayer() {
|
|
|
+ const layer = (this as any).data.__currentLayer
|
|
|
+ const layers = (this as any).layers
|
|
|
+ if (layers.includes(layer)) {
|
|
|
+ return layer
|
|
|
+ } else {
|
|
|
+ return layers[0]
|
|
|
+ }
|
|
|
+ },
|
|
|
+ layers() {
|
|
|
+ const data = this.data as StoreData
|
|
|
+ return Object.keys(data.layers)
|
|
|
+ },
|
|
|
+ typeItems() {
|
|
|
+ const data = this.data as StoreData
|
|
|
+ const layer = (this as any).currentLayer
|
|
|
+ return data.layers[layer] || {}
|
|
|
+ },
|
|
|
items() {
|
|
|
- return Object.values((this as any).data.typeItems).flat() as DrawItem[];
|
|
|
+ return Object.values(this.typeItems).flat() as DrawItem[];
|
|
|
},
|
|
|
sortItems() {
|
|
|
return (this.items as any).sort(sortFn) as DrawItem[];
|
|
@@ -38,48 +62,55 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
},
|
|
|
},
|
|
|
actions: {
|
|
|
- repStore(store: StoreData) {
|
|
|
- const newStore = JSON.parse(JSON.stringify(store));
|
|
|
- this.$patch((state) => {
|
|
|
- state.data = newStore;
|
|
|
- });
|
|
|
- },
|
|
|
setStore(store: Partial<StoreData>) {
|
|
|
- const newStore = JSON.parse(JSON.stringify(store));
|
|
|
+ const newStore = JSON.parse(JSON.stringify(store)) ;
|
|
|
this.$patch((state) => {
|
|
|
- state.data = newStore;
|
|
|
+ console.log(state.data)
|
|
|
+ state.data = {
|
|
|
+ ...state.data,
|
|
|
+ ...newStore
|
|
|
+ };
|
|
|
+ console.log(state.data)
|
|
|
});
|
|
|
},
|
|
|
+ setLayerStore(layerStore: DrawData) {
|
|
|
+ this.$patch(state => {
|
|
|
+ state.data.layers[this.currentLayer] = layerStore
|
|
|
+ })
|
|
|
+ },
|
|
|
getItemNdx<T extends ShapeType>(type: T, id: string) {
|
|
|
- const items = this.data.typeItems[type];
|
|
|
+ const items = this.typeItems[type];
|
|
|
+
|
|
|
if (items) {
|
|
|
return items.findIndex((item) => item.id === id);
|
|
|
}
|
|
|
return -1;
|
|
|
},
|
|
|
getTypeItems<T extends ShapeType>(type: T): DrawItem<T>[] {
|
|
|
- return (this.data.typeItems[type] as DrawItem<T>[]) || [];
|
|
|
+ return (this.typeItems[type] as DrawItem<T>[]) || [];
|
|
|
},
|
|
|
getItem<T extends ShapeType>(type: T, id: string) {
|
|
|
const ndx = this.getItemNdx(type, id);
|
|
|
- if (~ndx) return this.data.typeItems[type]![ndx];
|
|
|
+ if (~ndx) return this.typeItems[type]![ndx];
|
|
|
},
|
|
|
addItems<T extends ShapeType>(type: T, items: DrawItem<T>[]) {
|
|
|
items.forEach((item) => this.addItem(type, item));
|
|
|
},
|
|
|
addItem<T extends ShapeType>(type: T, item: DrawItem<T>) {
|
|
|
- this.$patch((state) => {
|
|
|
- if (!(type in state.data.typeItems)) {
|
|
|
- state.data.typeItems[type] = [];
|
|
|
+ const typeItems = this.typeItems
|
|
|
+ this.$patch(() => {
|
|
|
+ if (!(type in typeItems)) {
|
|
|
+ typeItems[type] = [];
|
|
|
}
|
|
|
- state.data.typeItems[type]!.push(item as any);
|
|
|
+ typeItems[type]!.push(item as any);
|
|
|
});
|
|
|
},
|
|
|
delItem<T extends ShapeType>(type: T, id: string) {
|
|
|
const ndx = this.getItemNdx(type, id);
|
|
|
+ const typeItems = this.typeItems
|
|
|
if (~ndx) {
|
|
|
- this.$patch((state) => {
|
|
|
- state.data.typeItems[type]!.splice(ndx, 1);
|
|
|
+ this.$patch(() => {
|
|
|
+ typeItems[type]!.splice(ndx, 1);
|
|
|
});
|
|
|
}
|
|
|
},
|
|
@@ -87,10 +118,11 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
type: T,
|
|
|
playData: { value: Partial<DrawItem<T>>; id: string }
|
|
|
) {
|
|
|
+ const typeItems = this.typeItems
|
|
|
const ndx = this.getItemNdx(type, playData.id);
|
|
|
if (~ndx) {
|
|
|
- this.$patch((state) => {
|
|
|
- Object.assign(state.data.typeItems[type]![ndx], playData.value);
|
|
|
+ this.$patch(() => {
|
|
|
+ Object.assign(typeItems[type]![ndx], playData.value);
|
|
|
});
|
|
|
}
|
|
|
},
|
|
@@ -102,17 +134,18 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
}
|
|
|
},
|
|
|
getType(id: string) {
|
|
|
- const types = Object.keys(this.data.typeItems) as ShapeType[];
|
|
|
+ const typeItems = this.typeItems
|
|
|
+ const types = Object.keys(typeItems) as ShapeType[];
|
|
|
for (const type of types) {
|
|
|
- if (this.data.typeItems[type]?.some((item) => item.id === id)) {
|
|
|
+ if (typeItems[type]?.some((item) => item.id === id)) {
|
|
|
return type;
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
getItemById(id: string) {
|
|
|
- const types = Object.keys(this.data.typeItems) as ShapeType[];
|
|
|
+ const types = Object.keys(this.typeItems) as ShapeType[];
|
|
|
for (const type of types) {
|
|
|
- const item = this.data.typeItems[type]?.find((item) => item.id === id);
|
|
|
+ const item = this.typeItems[type]?.find((item) => item.id === id);
|
|
|
if (item) {
|
|
|
return item;
|
|
|
}
|
|
@@ -123,5 +156,44 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
state.data.config = config;
|
|
|
});
|
|
|
},
|
|
|
+ setCurrentLayer(layer: string) {
|
|
|
+ if (!this.layers.includes(layer)) {
|
|
|
+ throw `不存在${layer}层`
|
|
|
+ } else {
|
|
|
+ this.data.__currentLayer = layer
|
|
|
+ }
|
|
|
+ },
|
|
|
+ addLayer(layer: string) {
|
|
|
+ if (this.layers.includes(layer)) {
|
|
|
+ throw `已存在${layer}层`
|
|
|
+ } else {
|
|
|
+ this.data.layers[layer] = {}
|
|
|
+ }
|
|
|
+ },
|
|
|
+ delLayer(layer: string, translateLayer?: string) {
|
|
|
+ if (!this.layers.includes(layer)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (translateLayer) {
|
|
|
+ if (!this.layers.includes(translateLayer)) {
|
|
|
+ throw `不存在${translateLayer}层`
|
|
|
+ }
|
|
|
+ const layerData = this.data.layers[layer]
|
|
|
+ const tLayerData = this.data.layers[translateLayer]
|
|
|
+ const keys = Object.keys(layerData) as ShapeType[]
|
|
|
+
|
|
|
+ for (const key of keys) {
|
|
|
+ if (key in tLayerData && tLayerData[key]) {
|
|
|
+ tLayerData[key] = [
|
|
|
+ ...tLayerData[key],
|
|
|
+ ...layerData[key]!
|
|
|
+ ] as any
|
|
|
+ } else {
|
|
|
+ tLayerData[key] = layerData[key] as any
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
},
|
|
|
});
|