|
|
@@ -7,21 +7,24 @@ export const sortFn = (
|
|
|
b: Pick<DrawItem, "zIndex" | "createTime">
|
|
|
) => a.zIndex - b.zIndex || a.createTime - b.createTime;
|
|
|
|
|
|
+export type Proportion = { scale: number; unit: string };
|
|
|
export type StoreConfig = {
|
|
|
- proportion: { scale: number, unit: string }
|
|
|
+ proportion: Proportion;
|
|
|
+ strokeProportion: Proportion;
|
|
|
compass: {
|
|
|
rotation: number;
|
|
|
url: string;
|
|
|
};
|
|
|
};
|
|
|
export type StoreData = {
|
|
|
- layers: Record<string, DrawData>,
|
|
|
+ layers: Record<string, DrawData>;
|
|
|
config: StoreConfig;
|
|
|
__currentLayer: string;
|
|
|
};
|
|
|
const defConfig: StoreData["config"] = {
|
|
|
- compass: { rotation: 0, url: 'icons/edit_compass.svg' },
|
|
|
- proportion: {scale: 10, unit: 'mm'}
|
|
|
+ compass: { rotation: 0, url: "icons/edit_compass.svg" },
|
|
|
+ proportion: { scale: 1, unit: "px" },
|
|
|
+ strokeProportion: { scale: 1, unit: "px" }
|
|
|
};
|
|
|
|
|
|
export const getEmptyStoreData = (): StoreData => {
|
|
|
@@ -36,22 +39,22 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
state: () => ({ data: getEmptyStoreData() }),
|
|
|
getters: {
|
|
|
currentLayer() {
|
|
|
- const layer = (this as any).data.__currentLayer
|
|
|
- const layers = (this as any).layers
|
|
|
+ const layer = (this as any).data.__currentLayer;
|
|
|
+ const layers = (this as any).layers;
|
|
|
if (layers.includes(layer)) {
|
|
|
- return layer
|
|
|
+ return layer;
|
|
|
} else {
|
|
|
- return layers[0]
|
|
|
+ return layers[0];
|
|
|
}
|
|
|
},
|
|
|
layers() {
|
|
|
- const data = this.data as StoreData
|
|
|
- return Object.keys(data.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] || {}
|
|
|
+ const data = this.data as StoreData;
|
|
|
+ const layer = (this as any).currentLayer;
|
|
|
+ return data.layers[layer] || {};
|
|
|
},
|
|
|
items() {
|
|
|
return Object.values(this.typeItems).flat() as DrawItem[];
|
|
|
@@ -65,26 +68,26 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
},
|
|
|
actions: {
|
|
|
setStore(store: Partial<StoreData>) {
|
|
|
- const newStore = JSON.parse(JSON.stringify(store)) ;
|
|
|
+ const newStore = JSON.parse(JSON.stringify(store));
|
|
|
this.$patch((state) => {
|
|
|
state.data = {
|
|
|
...state.data,
|
|
|
...newStore,
|
|
|
config: {
|
|
|
...state.data.config,
|
|
|
- ...(newStore.config || {})
|
|
|
- }
|
|
|
+ ...(newStore.config || {}),
|
|
|
+ },
|
|
|
};
|
|
|
});
|
|
|
},
|
|
|
setLayerStore(layerStore: DrawData) {
|
|
|
- this.$patch(state => {
|
|
|
- state.data.layers[this.currentLayer] = layerStore
|
|
|
- })
|
|
|
+ this.$patch((state) => {
|
|
|
+ state.data.layers[this.currentLayer] = layerStore;
|
|
|
+ });
|
|
|
},
|
|
|
getItemNdx<T extends ShapeType>(type: T, id: string) {
|
|
|
const items = this.typeItems[type];
|
|
|
-
|
|
|
+
|
|
|
if (items) {
|
|
|
return items.findIndex((item) => item.id === id);
|
|
|
}
|
|
|
@@ -101,7 +104,7 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
items.forEach((item) => this.addItem(type, item));
|
|
|
},
|
|
|
addItem<T extends ShapeType>(type: T, item: DrawItem<T>) {
|
|
|
- const typeItems = this.typeItems
|
|
|
+ const typeItems = this.typeItems;
|
|
|
this.$patch(() => {
|
|
|
if (!(type in typeItems)) {
|
|
|
typeItems[type] = [];
|
|
|
@@ -111,7 +114,7 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
},
|
|
|
delItem<T extends ShapeType>(type: T, id: string) {
|
|
|
const ndx = this.getItemNdx(type, id);
|
|
|
- const typeItems = this.typeItems
|
|
|
+ const typeItems = this.typeItems;
|
|
|
if (~ndx) {
|
|
|
this.$patch(() => {
|
|
|
typeItems[type]!.splice(ndx, 1);
|
|
|
@@ -122,14 +125,14 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
type: T,
|
|
|
playData: { value: Partial<DrawItem<T>>; id: string }
|
|
|
) {
|
|
|
- const typeItems = this.typeItems
|
|
|
+ const typeItems = this.typeItems;
|
|
|
const ndx = this.getItemNdx(type, playData.id);
|
|
|
if (~ndx) {
|
|
|
this.$patch(() => {
|
|
|
- console.log()
|
|
|
- const old = typeItems[type]![ndx]
|
|
|
+ console.log();
|
|
|
+ const old = typeItems[type]![ndx];
|
|
|
Object.assign(typeItems[type]![ndx], playData.value);
|
|
|
- const newv = typeItems[type]![ndx]
|
|
|
+ const newv = typeItems[type]![ndx];
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
@@ -141,8 +144,8 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
}
|
|
|
},
|
|
|
getType(id: string) {
|
|
|
- const typeItems = this.typeItems
|
|
|
- const types = (Object.keys(typeItems)) as ShapeType[];
|
|
|
+ const typeItems = this.typeItems;
|
|
|
+ const types = Object.keys(typeItems) as ShapeType[];
|
|
|
for (const type of types) {
|
|
|
if (typeItems[type]?.some((item) => item.id === id)) {
|
|
|
return type;
|
|
|
@@ -162,25 +165,25 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
this.$patch((state) => {
|
|
|
state.data.config = {
|
|
|
...state.data.config,
|
|
|
- ...config
|
|
|
+ ...config,
|
|
|
};
|
|
|
});
|
|
|
},
|
|
|
setCurrentLayer(layer: string) {
|
|
|
if (!this.layers.includes(layer)) {
|
|
|
- throw `不存在${layer}层`
|
|
|
+ throw `不存在${layer}层`;
|
|
|
} else {
|
|
|
- this.data.__currentLayer = layer
|
|
|
+ this.data.__currentLayer = layer;
|
|
|
}
|
|
|
},
|
|
|
clear() {
|
|
|
- this.data.layers[this.currentLayer] = {}
|
|
|
+ this.data.layers[this.currentLayer] = {};
|
|
|
},
|
|
|
addLayer(layer: string) {
|
|
|
if (this.layers.includes(layer)) {
|
|
|
- throw `已存在${layer}层`
|
|
|
+ throw `已存在${layer}层`;
|
|
|
} else {
|
|
|
- this.data.layers[layer] = {}
|
|
|
+ this.data.layers[layer] = {};
|
|
|
}
|
|
|
},
|
|
|
delLayer(layer: string, translateLayer?: string) {
|
|
|
@@ -190,23 +193,20 @@ export const useStoreRaw = defineStore("draw-data", {
|
|
|
|
|
|
if (translateLayer) {
|
|
|
if (!this.layers.includes(translateLayer)) {
|
|
|
- throw `不存在${translateLayer}层`
|
|
|
+ throw `不存在${translateLayer}层`;
|
|
|
}
|
|
|
- const layerData = this.data.layers[layer]
|
|
|
- const tLayerData = this.data.layers[translateLayer]
|
|
|
- const keys = Object.keys(layerData) as ShapeType[]
|
|
|
+ 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
|
|
|
+ tLayerData[key] = [...tLayerData[key], ...layerData[key]!] as any;
|
|
|
} else {
|
|
|
- tLayerData[key] = layerData[key] as any
|
|
|
+ tLayerData[key] = layerData[key] as any;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
},
|
|
|
});
|