import {reaction} from 'mobx';
|
import {applySnapshot, getEnv, getSnapshot, types} from 'mobx-state-tree';
|
import {getLowCodeJson} from '../api/admin';
|
import {getPageId} from '../utils/request';
|
import {PageStore} from './Page';
|
|
let pagIndex = 1;
|
export const MainStore = types
|
.model('MainStore', {
|
pages: types.optional(types.array(PageStore), [
|
{
|
id: `${pagIndex}`,
|
path: 'hello-world',
|
label: 'Hello world',
|
icon: 'fa fa-file',
|
schema: {
|
type: 'page',
|
title: 'Hello world',
|
body: '初始页面'
|
}
|
}
|
]),
|
theme: 'cxd',
|
asideFixed: true,
|
asideFolded: false,
|
offScreen: false,
|
addPageIsOpen: false,
|
preview: false,
|
isMobile: false,
|
schema: types.frozen()
|
})
|
.views(self => ({
|
get fetcher() {
|
return getEnv(self).fetcher;
|
},
|
get notify() {
|
return getEnv(self).notify;
|
},
|
get alert() {
|
return getEnv(self).alert;
|
},
|
get copy() {
|
return getEnv(self).copy;
|
}
|
}))
|
.actions(self => {
|
function toggleAsideFolded() {
|
self.asideFolded = !self.asideFolded;
|
}
|
|
function toggleAsideFixed() {
|
self.asideFixed = !self.asideFixed;
|
}
|
|
function toggleOffScreen() {
|
self.offScreen = !self.offScreen;
|
}
|
|
function setAddPageIsOpen(isOpened: boolean) {
|
self.addPageIsOpen = isOpened;
|
}
|
|
function addPage(data: {
|
label: string;
|
path: string;
|
icon?: string;
|
schema?: any;
|
}) {
|
self.pages.push(
|
PageStore.create({
|
...data,
|
id: `${++pagIndex}`
|
})
|
);
|
}
|
|
function removePageAt(index: number) {
|
self.pages.splice(index, 1);
|
}
|
|
function updatePageSchemaAt(index: number) {
|
if (index === -1) return;
|
self.pages[index].updateSchema(self.schema);
|
}
|
|
function updateSchema(value: any) {
|
self.schema = value;
|
}
|
|
function setPreview(value: boolean) {
|
self.preview = value;
|
}
|
|
function setIsMobile(value: boolean) {
|
self.isMobile = value;
|
}
|
|
return {
|
toggleAsideFolded,
|
toggleAsideFixed,
|
toggleOffScreen,
|
setAddPageIsOpen,
|
addPage,
|
removePageAt,
|
updatePageSchemaAt,
|
updateSchema,
|
setPreview,
|
setIsMobile,
|
async afterCreate() {
|
// persist store
|
if (typeof window !== 'undefined' && window.localStorage) {
|
// const storeData = window.localStorage.getItem('store') as any;
|
|
const currentId = getPageId();
|
// 先清空,防止接口权限过期,请求到旧数据
|
window.localStorage.removeItem('store');
|
|
const json = await getLowCodeJson({
|
id: currentId
|
});
|
// const codeJsonList = (await Promise.all(codeJson)).map(item => {
|
// return item?.amis_json;
|
// });
|
const storeDataObj = {
|
pages: [
|
{
|
id: currentId,
|
icon: '',
|
label: currentId,
|
path: currentId,
|
schema: json?.amis_json
|
}
|
]
|
};
|
const storeData = JSON.stringify(storeDataObj);
|
|
window.localStorage.setItem('store', storeData);
|
if (storeData) applySnapshot(self, storeDataObj);
|
|
reaction(
|
() => getSnapshot(self),
|
json => {
|
window.localStorage.setItem('store', JSON.stringify(json));
|
}
|
);
|
}
|
}
|
};
|
});
|
|
export type IMainStore = typeof MainStore.Type;
|