95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { material, project } from '@alilc/lowcode-engine';
|
|
import { filterPackages } from '@alilc/lowcode-plugin-inject'
|
|
import { Message, Dialog } from '@alifd/next';
|
|
import { TransformStage } from '@alilc/lowcode-types';
|
|
import schema from './schema.json';
|
|
|
|
export const saveSchema = async (scenarioName: string = 'unknown') => {
|
|
setProjectSchemaToLocalStorage(scenarioName);
|
|
await setPackagesToLocalStorage(scenarioName);
|
|
Message.success('成功保存到本地');
|
|
};
|
|
|
|
export const resetSchema = async (scenarioName: string = 'unknown') => {
|
|
try {
|
|
await new Promise<void>((resolve, reject) => {
|
|
Dialog.confirm({
|
|
content: '确定要重置吗?您所有的修改都将消失!',
|
|
onOk: () => {
|
|
resolve();
|
|
},
|
|
onCancel: () => {
|
|
reject()
|
|
},
|
|
})
|
|
})
|
|
} catch(err) {
|
|
return;
|
|
}
|
|
|
|
let defaultSchema = schema || {
|
|
componentsTree: [{ componentName: 'Page', fileName: 'sample' }],
|
|
componentsMap: material.componentsMap,
|
|
version: '1.0.0',
|
|
i18n: {},
|
|
};
|
|
|
|
project.getCurrentDocument()?.importSchema(defaultSchema as any);
|
|
project.simulatorHost?.rerender();
|
|
|
|
setProjectSchemaToLocalStorage(scenarioName);
|
|
await setPackagesToLocalStorage(scenarioName);
|
|
Message.success('成功重置页面');
|
|
}
|
|
|
|
const getLSName = (scenarioName: string, ns: string = 'projectSchema') => `${scenarioName}:${ns}`;
|
|
|
|
export const getProjectSchemaFromLocalStorage = (scenarioName: string) => {
|
|
if (!scenarioName) {
|
|
console.error('scenarioName is required!');
|
|
return;
|
|
}
|
|
return JSON.parse(window.localStorage.getItem(getLSName(scenarioName)) || '{}');
|
|
}
|
|
|
|
const setProjectSchemaToLocalStorage = (scenarioName: string) => {
|
|
if (!scenarioName) {
|
|
console.error('scenarioName is required!');
|
|
return;
|
|
}
|
|
window.localStorage.setItem(
|
|
getLSName(scenarioName),
|
|
JSON.stringify(project.exportSchema(TransformStage.Save))
|
|
);
|
|
}
|
|
|
|
const setPackagesToLocalStorage = async (scenarioName: string) => {
|
|
if (!scenarioName) {
|
|
console.error('scenarioName is required!');
|
|
return;
|
|
}
|
|
const packages = await filterPackages(material.getAssets().packages);
|
|
window.localStorage.setItem(
|
|
getLSName(scenarioName, 'packages'),
|
|
JSON.stringify(packages),
|
|
);
|
|
}
|
|
|
|
export const getPackagesFromLocalStorage = (scenarioName: string) => {
|
|
if (!scenarioName) {
|
|
console.error('scenarioName is required!');
|
|
return;
|
|
}
|
|
return JSON.parse(window.localStorage.getItem(getLSName(scenarioName, 'packages')) || '{}');
|
|
}
|
|
|
|
export const getPageSchema = async (scenarioName: string = 'unknown') => {
|
|
const pageSchema = getProjectSchemaFromLocalStorage(scenarioName).componentsTree?.[0];
|
|
|
|
if (pageSchema) {
|
|
return pageSchema;
|
|
}
|
|
|
|
return schema;
|
|
};
|