@meta2d/core
Version:
@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .
89 lines • 2.56 kB
JavaScript
import { default as mitt } from 'mitt';
import { CanvasLayer } from '../pen';
import { defaultOptions } from '../options';
import { defaultTheme } from '../theme';
import { globalStore } from './global';
export var EditType;
(function (EditType) {
EditType[EditType["Add"] = 0] = "Add";
EditType[EditType["Update"] = 1] = "Update";
EditType[EditType["Delete"] = 2] = "Delete";
EditType[EditType["Replace"] = 3] = "Replace";
})(EditType || (EditType = {}));
export const createStore = () => {
return {
data: {
x: 0,
y: 0,
scale: 1,
pens: [],
origin: { x: 0, y: 0 },
center: { x: 0, y: 0 },
paths: {},
// theme:'light'
},
histories: [],
pens: {},
path2dMap: new WeakMap(),
animateMap: new WeakMap(),
active: [],
animates: new Set(),
options: { ...defaultOptions },
theme: { ...defaultTheme },
emitter: mitt(),
bindDatas: {},
bind: {},
pensNetwork: {},
cacheDatas: [],
messageEvents: {},
templatePens: {},
globalTriggers: {},
};
};
// Return a data store, if not exists will create a store.
export const useStore = (id = 'default') => {
if (!globalStore[id]) {
globalStore[id] = createStore();
globalStore[id].id = id;
}
return globalStore[id];
};
export const clearStore = (store, template) => {
const isSame = store.data.template === template;
if (isSame) {
//模版一样
for (const pen of store.data.pens) {
// if (pen.template) {
if (pen.canvasLayer === CanvasLayer.CanvasTemplate) {
//TODO 不同图纸模板层切换
// store.templatePens[pen.id] = pen;
}
}
}
store.lastScale = store.data.scale;
store.data = {
x: 0,
y: 0,
scale: 1,
pens: [],
origin: { x: 0, y: 0 },
center: { x: 0, y: 0 },
paths: {},
template: isSame ? template : null,
lineAnimateDraws: {}
};
store.sameTemplate = isSame;
store.pens = {};
store.histories = [];
store.historyIndex = null;
store.path2dMap = new WeakMap();
store.animateMap = new WeakMap();
store.bindDatas = {};
store.bind = {};
store.pensNetwork = {};
store.active = [];
store.hover = undefined;
store.lastHover = undefined;
store.animates.clear();
};
//# sourceMappingURL=store.js.map