forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmockService.ts
More file actions
116 lines (101 loc) · 3.64 KB
/
mockService.ts
File metadata and controls
116 lines (101 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { material, project } from '@felce/lowcode-engine';
import { filterPackages } from '@felce/lowcode-plugin-inject';
import { Message, Dialog } from '@alifd/next';
import { IPublicTypeProjectSchema, IPublicEnumTransformStage } from '@felce/lowcode-types';
import DefaultPageSchema from './defaultPageSchema.json';
import DefaultI18nSchema from './defaultI18nSchema.json';
const generateProjectSchema = (pageSchema: any, i18nSchema: any): IPublicTypeProjectSchema => {
return {
componentsTree: [pageSchema],
componentsMap: material.componentsMap as any,
version: '1.0.0',
i18n: i18nSchema,
};
};
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;
}
const defaultSchema = generateProjectSchema(DefaultPageSchema, DefaultI18nSchema);
project.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;
}
const localValue = window.localStorage.getItem(getLSName(scenarioName));
if (localValue) {
return JSON.parse(localValue);
}
return undefined;
};
const setProjectSchemaToLocalStorage = (scenarioName: string) => {
if (!scenarioName) {
console.error('scenarioName is required!');
return;
}
window.localStorage.setItem(
getLSName(scenarioName),
JSON.stringify(project.exportSchema(IPublicEnumTransformStage.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 getProjectSchema = async (
scenarioName: string = 'unknown',
): Promise<IPublicTypeProjectSchema> => {
const pageSchema = await getPageSchema(scenarioName);
return generateProjectSchema(pageSchema, DefaultI18nSchema);
};
export const getPageSchema = async (scenarioName: string = 'unknown') => {
const pageSchema = getProjectSchemaFromLocalStorage(scenarioName)?.componentsTree?.[0];
if (pageSchema) {
return pageSchema;
}
return DefaultPageSchema;
};
export const getPreviewLocale = (scenarioName: string) => {
const key = getLSName(scenarioName, 'previewLocale');
return window.localStorage.getItem(key) || 'zh-CN';
};
export const setPreviewLocale = (scenarioName: string, locale: string) => {
const key = getLSName(scenarioName, 'previewLocale');
window.localStorage.setItem(key, locale || 'zh-CN');
window.location.reload();
};