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
246 lines (221 loc) · 6.62 KB
/
project.ts
File metadata and controls
246 lines (221 loc) · 6.62 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
import {
BuiltinSimulatorHost,
IProject as InnerProject,
} from '@alilc/lowcode-designer';
import { globalContext } from '@alilc/lowcode-editor-core';
import {
IPublicTypeRootSchema,
IPublicTypeProjectSchema,
IPublicModelEditor,
IPublicApiProject,
IPublicApiSimulatorHost,
IPublicModelDocumentModel,
IPublicTypePropsTransducer,
IPublicEnumTransformStage,
IPublicTypeDisposable,
IPublicTypeAppConfig,
} from '@alilc/lowcode-types';
import { DocumentModel as ShellDocumentModel } from '../model';
import { SimulatorHost } from './simulator-host';
import { editorSymbol, projectSymbol, simulatorHostSymbol, documentSymbol } from '../symbols';
import { getLogger } from '@alilc/lowcode-utils';
const logger = getLogger({ level: 'warn', bizName: 'shell-project' });
const innerProjectSymbol = Symbol('innerProject');
export class Project implements IPublicApiProject {
private readonly [innerProjectSymbol]: InnerProject;
private [simulatorHostSymbol]: BuiltinSimulatorHost;
get [projectSymbol](): InnerProject {
if (this.workspaceMode) {
return this[innerProjectSymbol];
}
const workspace = globalContext.get('workspace');
if (workspace.isActive) {
if (!workspace.window?.innerProject) {
logger.error('project api 调用时机出现问题,请检查');
return this[innerProjectSymbol];
}
return workspace.window.innerProject;
}
return this[innerProjectSymbol];
}
get [editorSymbol](): IPublicModelEditor {
return this[projectSymbol]?.designer.editor;
}
constructor(project: InnerProject, public workspaceMode: boolean = false) {
this[innerProjectSymbol] = project;
}
static create(project: InnerProject, workspaceMode: boolean = false) {
return new Project(project, workspaceMode);
}
/**
* 获取当前的 document
* @returns
*/
get currentDocument(): IPublicModelDocumentModel | null {
return this.getCurrentDocument();
}
/**
* 获取当前 project 下所有 documents
* @returns
*/
get documents(): IPublicModelDocumentModel[] {
return this[projectSymbol].documents.map((doc) => ShellDocumentModel.create(doc)!);
}
/**
* 获取模拟器的 host
*/
get simulatorHost(): IPublicApiSimulatorHost | null {
return SimulatorHost.create(this[projectSymbol].simulator as any || this[simulatorHostSymbol]);
}
/**
* @deprecated use .simulatorHost instead.
*/
get simulator() {
return this.simulatorHost;
}
/**
* 打开一个 document
* @param doc
* @returns
*/
openDocument(doc?: string | IPublicTypeRootSchema | undefined) {
const documentModel = this[projectSymbol].open(doc);
if (!documentModel) {
return null;
}
return ShellDocumentModel.create(documentModel);
}
/**
* 创建一个 document
* @param data
* @returns
*/
createDocument(data?: IPublicTypeRootSchema): IPublicModelDocumentModel | null {
const doc = this[projectSymbol].createDocument(data);
return ShellDocumentModel.create(doc);
}
/**
* 删除一个 document
* @param doc
*/
removeDocument(doc: IPublicModelDocumentModel) {
this[projectSymbol].removeDocument((doc as any)[documentSymbol]);
}
/**
* 根据 fileName 获取 document
* @param fileName
* @returns
*/
getDocumentByFileName(fileName: string): IPublicModelDocumentModel | null {
const innerDocumentModel = this[projectSymbol].getDocumentByFileName(fileName);
return ShellDocumentModel.create(innerDocumentModel);
}
/**
* 根据 id 获取 document
* @param id
* @returns
*/
getDocumentById(id: string): IPublicModelDocumentModel | null {
return ShellDocumentModel.create(this[projectSymbol].getDocument(id));
}
/**
* 导出 project
* @returns
*/
exportSchema(stage: IPublicEnumTransformStage = IPublicEnumTransformStage.Render) {
return this[projectSymbol].getSchema(stage);
}
/**
* 导入 project
* @param schema 待导入的 project 数据
*/
importSchema(schema?: IPublicTypeProjectSchema): void {
this[projectSymbol].load(schema, true);
}
/**
* 获取当前的 document
* @returns
*/
getCurrentDocument(): IPublicModelDocumentModel | null {
return ShellDocumentModel.create(this[projectSymbol].currentDocument);
}
/**
* 增加一个属性的管道处理函数
* @param transducer
* @param stage
*/
addPropsTransducer(
transducer: IPublicTypePropsTransducer,
stage: IPublicEnumTransformStage,
): void {
this[projectSymbol].designer.addPropsReducer(transducer, stage);
}
/**
* 绑定删除文档事件
* @param fn
* @returns
*/
onRemoveDocument(fn: (data: { id: string}) => void): IPublicTypeDisposable {
return this[editorSymbol].eventBus.on(
'designer.document.remove',
(data: { id: string }) => fn(data),
);
}
/**
* 当前 project 内的 document 变更事件
*/
onChangeDocument(fn: (doc: IPublicModelDocumentModel) => void): IPublicTypeDisposable {
const offFn = this[projectSymbol].onCurrentDocumentChange((originalDoc) => {
fn(ShellDocumentModel.create(originalDoc)!);
});
if (this[projectSymbol].currentDocument) {
fn(ShellDocumentModel.create(this[projectSymbol].currentDocument)!);
}
return offFn;
}
/**
* 当前 project 的模拟器 ready 事件
*/
onSimulatorHostReady(fn: (host: IPublicApiSimulatorHost) => void): IPublicTypeDisposable {
const offFn = this[projectSymbol].onSimulatorReady((simulator: BuiltinSimulatorHost) => {
fn(SimulatorHost.create(simulator)!);
});
return offFn;
}
/**
* 当前 project 的渲染器 ready 事件
*/
onSimulatorRendererReady(fn: () => void): IPublicTypeDisposable {
const offFn = this[projectSymbol].onRendererReady(() => {
fn();
});
return offFn;
}
/**
* 设置多语言语料
* 数据格式参考 https://github.com/alibaba/lowcode-engine/blob/main/specs/lowcode-spec.md#2434%E5%9B%BD%E9%99%85%E5%8C%96%E5%A4%9A%E8%AF%AD%E8%A8%80%E7%B1%BB%E5%9E%8Baa
* @param value object
* @returns
*/
setI18n(value: object): void {
this[projectSymbol].set('i18n', value);
}
/**
* 设置项目配置
* @param value object
* @returns
*/
setConfig<T extends keyof IPublicTypeAppConfig>(key: T, value: IPublicTypeAppConfig[T]): void;
setConfig(value: IPublicTypeAppConfig): void;
setConfig(...params: any[]): void {
if (params.length === 2) {
const oldConfig = this[projectSymbol].get('config');
this[projectSymbol].set('config', {
...oldConfig,
[params[0]]: params[1],
});
} else {
this[projectSymbol].set('config', params[0]);
}
}
}