forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.ts
More file actions
307 lines (273 loc) · 8.09 KB
/
project.ts
File metadata and controls
307 lines (273 loc) · 8.09 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { EventEmitter } from 'events';
import { obx, computed, makeObservable, action } from '@alilc/lowcode-editor-core';
import { Designer } from '../designer';
import { DocumentModel, isDocumentModel, isPageSchema } from '../document';
import { ProjectSchema, RootSchema, TransformStage } from '@alilc/lowcode-types';
import { ISimulatorHost } from '../simulator';
export class Project {
private emitter = new EventEmitter();
@obx.shallow readonly documents: DocumentModel[] = [];
private data: ProjectSchema = { version: '1.0.0', componentsMap: [], componentsTree: [], i18n: {} };
private _simulator?: ISimulatorHost;
/**
* 模拟器
*/
get simulator(): ISimulatorHost | null {
return this._simulator || null;
}
// TODO: 考虑项目级别 History
constructor(readonly designer: Designer, schema?: ProjectSchema) {
makeObservable(this);
this.load(schema);
}
@computed get currentDocument() {
return this.documents.find((doc) => doc.active);
}
@obx private _config: any = {};
@computed get config(): any {
// TODO: parse layout Component
return this._config;
}
set config(value: any) {
this._config = value;
}
@obx.ref private _i18n: any = {};
get i18n(): any {
return this._i18n;
}
set i18n(value: any) {
this._i18n = value || {};
}
/**
* 获取项目整体 schema
*/
getSchema(stage: TransformStage = TransformStage.Save): ProjectSchema {
return {
...this.data,
// TODO: future change this filter
componentsMap: this.currentDocument?.getComponentsMap(),
componentsTree: this.documents.filter((doc) => !doc.isBlank()).map((doc) => doc.export(stage)),
i18n: this.i18n,
};
}
/**
* 替换当前document的schema,并触发渲染器的render
* @param schema
*/
setSchema(schema?: ProjectSchema) {
// FIXME: 这里的行为和 getSchema 并不对等,感觉不太对
const doc = this.documents.find((doc) => doc.active);
doc && doc.import(schema?.componentsTree[0]);
this.simulator?.rerender();
}
/**
* 整体设置项目 schema
*
* @param autoOpen true 自动打开文档 string 指定打开的文件
*/
@action
load(schema?: ProjectSchema, autoOpen?: boolean | string) {
this.unload();
// load new document
this.data = {
version: '1.0.0',
componentsMap: [],
componentsTree: [],
i18n: {},
...schema,
};
this.config = schema?.config || this.config;
this.i18n = schema?.i18n || this.i18n;
if (autoOpen) {
if (autoOpen === true) {
// auto open first document or open a blank page
// this.open(this.data.componentsTree[0]);
const documentInstances = this.data.componentsTree.map((data) => this.createDocument(data));
// TODO: 暂时先读 config tabBar 里的值,后面看整个 layout 结构是否能作为引擎规范
if (this.config?.layout?.props?.tabBar?.items?.length > 0) {
// slice(1)这个贼不雅,默认任务fileName 是类'/fileName'的形式
documentInstances.find((i) => i.fileName === this.config.layout.props.tabBar.items[0].path?.slice(1))?.open();
} else {
documentInstances[0].open();
}
} else {
// auto open should be string of fileName
this.open(autoOpen);
}
}
}
/**
* 卸载当前项目数据
*/
unload() {
if (this.documents.length < 1) {
return;
}
for (let i = this.documents.length - 1; i >= 0; i--) {
this.documents[i].remove();
}
}
removeDocument(doc: DocumentModel) {
const index = this.documents.indexOf(doc);
if (index < 0) {
return;
}
this.documents.splice(index, 1);
this.documentsMap.delete(doc.id);
}
/**
* 分字段设置储存数据,不记录操作记录
*/
set(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
key:
| 'version'
| 'componentsTree'
| 'componentsMap'
| 'utils'
| 'constants'
| 'i18n'
| 'css'
| 'dataSource'
| string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
value: any,
): void {
if (key === 'config') {
this.config = value;
}
if (key === 'i18n') {
this.i18n = value;
}
Object.assign(this.data, { [key]: value });
}
/**
* 分字段设置储存数据
*/
get(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
key:
| 'version'
| 'componentsTree'
| 'componentsMap'
| 'utils'
| 'constants'
| 'i18n'
| 'css'
| 'dataSource'
| 'config'
| string,
): any {
if (key === 'config') {
return this.config;
}
if (key === 'i18n') {
return this.i18n;
}
return Reflect.get(this.data, key);
}
private documentsMap = new Map<string, DocumentModel>();
getDocument(id: string): DocumentModel | null {
// 此处不能使用 this.documentsMap.get(id),因为在乐高 rollback 场景,document.id 会被改成其他值
return this.documents.find(doc => doc.id === id) || null;
}
getDocumentByFileName(fileName: string): DocumentModel | null {
return this.documents.find(doc => doc.fileName === fileName) || null;
}
@action
createDocument(data?: RootSchema): DocumentModel {
const doc = new DocumentModel(this, data || this?.data?.componentsTree?.[0]);
this.documents.push(doc);
this.documentsMap.set(doc.id, doc);
return doc;
}
open(doc?: string | DocumentModel | RootSchema): DocumentModel | null {
if (!doc) {
const got = this.documents.find((item) => item.isBlank());
if (got) {
return got.open();
}
doc = this.createDocument();
return doc.open();
}
if (typeof doc === 'string') {
const got = this.documents.find((item) => item.fileName === doc);
if (got) {
return got.open();
}
const data = this.data.componentsTree.find((data) => data.fileName === doc);
if (data) {
doc = this.createDocument(data);
return doc.open();
}
return null;
} else if (isDocumentModel(doc)) {
return doc.open();
}
// else if (isPageSchema(doc)) {
// 暂时注释掉,影响了 diff 功能
// const foundDoc = this.documents.find(curDoc => curDoc?.rootNode?.id && curDoc?.rootNode?.id === doc?.id);
// if (foundDoc) {
// foundDoc.remove();
// }
// }
doc = this.createDocument(doc);
return doc.open();
}
checkExclusive(activeDoc: DocumentModel) {
this.documents.forEach((doc) => {
if (doc !== activeDoc) {
doc.suspense();
}
});
this.emitter.emit('current-document.change', activeDoc);
}
closeOthers(opened: DocumentModel) {
this.documents.forEach((doc) => {
if (doc !== opened) {
doc.close();
}
});
}
/**
* 提供给模拟器的参数
*/
@computed get simulatorProps(): object {
let { simulatorProps } = this.designer;
if (typeof simulatorProps === 'function') {
simulatorProps = simulatorProps(this);
}
return {
...simulatorProps,
project: this,
onMount: this.mountSimulator.bind(this),
};
}
private mountSimulator(simulator: ISimulatorHost) {
// TODO: 多设备 simulator 支持
this._simulator = simulator;
this.designer.editor.set('simulator', simulator);
this.emitter.emit('lowcode_engine_simulator_ready', simulator);
}
setRendererReady(renderer: any) {
this.emitter.emit('lowcode_engine_renderer_ready', renderer);
}
onSimulatorReady(fn: (args: any) => void): () => void {
this.emitter.on('lowcode_engine_simulator_ready', fn);
return () => {
this.emitter.removeListener('lowcode_engine_simulator_ready', fn);
};
}
onRendererReady(fn: (args: any) => void): () => void {
this.emitter.on('lowcode_engine_renderer_ready', fn);
return () => {
this.emitter.removeListener('lowcode_engine_renderer_ready', fn);
};
}
onCurrentDocumentChange(fn: (doc: DocumentModel) => void): () => void {
this.emitter.on('current-document.change', fn);
return () => {
this.emitter.removeListener('current-document.change', fn);
};
}
}