forked from alibaba/lowcode-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
155 lines (144 loc) · 3.54 KB
/
utils.ts
File metadata and controls
155 lines (144 loc) · 3.54 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { material, project } from '@alilc/lowcode-engine';
import { Message } from '@alifd/next';
export const SAVE_KEY = 'projectSchema';
console.log(SAVE_KEY);
export const preview = () => {
saveSchema();
setTimeout(() => {
window.open('./preview.html');
}, 500);
};
export const saveSchema = () => {
window.localStorage.setItem(
SAVE_KEY,
JSON.stringify(project.exportSchema())
);
const packages = material.getAssets().packages;
window.localStorage.setItem(
'packages',
JSON.stringify(packages)
);
Message.success('成功保存到本地');
};
export const resetSchema = async () => {
const projectSchema = JSON.parse(
window.localStorage.getItem(SAVE_KEY) || `{
"componentName": "Page"
}`
);
projectSchema.componentsTree = [{
componentName: 'Page'
}];
window.localStorage.setItem(
SAVE_KEY,
JSON.stringify(projectSchema)
);
project.getCurrentDocument()?.importSchema({
componentName: 'Page',
fileName: 'sample',
});
project.simulator.rerender();
Message.success('成功重置页面');
}
export const getPackages = () => {
return JSON.parse(window.localStorage.getItem('packages') || '[]');
}
export const getPageSchemaFromLocalStorage = () => {
const schema = JSON.parse(
window.localStorage.getItem(SAVE_KEY) || '{}'
);
return schema;
}
export const getPageSchema = async (type) => {
const schema = JSON.parse(
window.localStorage.getItem(SAVE_KEY) || '{}'
);
const pageSchema = schema?.componentsTree?.[0];
if (pageSchema) {
return pageSchema;
}
const initialSchema = await request('./schema.json');
if (type === 'setter') {
initialSchema.children.push({
"componentName": "BuiltInComp",
})
}
return initialSchema;
};
function request(
dataAPI: string,
method = 'GET',
data?: object | string,
headers?: object,
otherProps?: any,
): Promise<any> {
return new Promise((resolve, reject): void => {
if (otherProps && otherProps.timeout) {
setTimeout((): void => {
reject(new Error('timeout'));
}, otherProps.timeout);
}
fetch(dataAPI, {
method,
credentials: 'include',
headers,
body: data,
...otherProps,
})
.then((response: Response): any => {
switch (response.status) {
case 200:
case 201:
case 202:
return response.json();
case 204:
if (method === 'DELETE') {
return {
success: true,
};
} else {
return {
__success: false,
code: response.status,
};
}
case 400:
case 401:
case 403:
case 404:
case 406:
case 410:
case 422:
case 500:
return response
.json()
.then((res: object): any => {
return {
__success: false,
code: response.status,
data: res,
};
})
.catch((): object => {
return {
__success: false,
code: response.status,
};
});
default:
return null;
}
})
.then((json: any): void => {
if (json && json.__success !== false) {
resolve(json);
} else {
delete json.__success;
reject(json);
}
})
.catch((err: Error): void => {
reject(err);
});
});
}