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
176 lines (157 loc) · 4.33 KB
/
project.ts
File metadata and controls
176 lines (157 loc) · 4.33 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
import {
BuiltinSimulatorHost,
Project as InnerProject,
PropsReducer as PropsTransducer,
TransformStage,
} from '@alilc/lowcode-designer';
import { RootSchema, ProjectSchema } from '@alilc/lowcode-types';
import DocumentModel from './document-model';
import SimulatorHost from './simulator-host';
import { projectSymbol, simulatorHostSymbol, simulatorRendererSymbol, documentSymbol } from './symbols';
export default class Project {
private readonly [projectSymbol]: InnerProject;
private [simulatorHostSymbol]: BuiltinSimulatorHost;
private [simulatorRendererSymbol]: any;
constructor(project: InnerProject) {
this[projectSymbol] = project;
}
static create(project: InnerProject) {
return new Project(project);
}
/**
* 获取当前的 document
* @returns
*/
get currentDocument(): DocumentModel | null {
return this.getCurrentDocument();
}
/**
* 获取当前 project 下所有 documents
* @returns
*/
get documents(): DocumentModel[] {
return this[projectSymbol].documents.map((doc) => DocumentModel.create(doc)!);
}
/**
* 获取模拟器的 host
*/
get simulatorHost() {
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 | RootSchema | undefined) {
const documentModel = this[projectSymbol].open(doc);
if (!documentModel) return null;
return DocumentModel.create(documentModel);
}
/**
* 创建一个 document
* @param data
* @returns
*/
createDocument(data?: RootSchema): DocumentModel | null {
const doc = this[projectSymbol].createDocument(data);
return DocumentModel.create(doc);
}
/**
* 删除一个 document
* @param doc
*/
removeDocument(doc: DocumentModel) {
this[projectSymbol].removeDocument(doc[documentSymbol]);
}
/**
* 根据 fileName 获取 document
* @param fileName
* @returns
*/
getDocumentByFileName(fileName: string): DocumentModel | null {
return DocumentModel.create(this[projectSymbol].getDocumentByFileName(fileName));
}
/**
* 根据 id 获取 document
* @param id
* @returns
*/
getDocumentById(id: string): DocumentModel | null {
return DocumentModel.create(this[projectSymbol].getDocument(id));
}
/**
* 导出 project
* @returns
*/
exportSchema(stage: TransformStage = TransformStage.Render) {
return this[projectSymbol].getSchema(stage);
}
/**
* 导入 project
* @param schema 待导入的 project 数据
*/
importSchema(schema?: ProjectSchema) {
this[projectSymbol].load(schema, true);
}
/**
* 获取当前的 document
* @returns
*/
getCurrentDocument(): DocumentModel | null {
return DocumentModel.create(this[projectSymbol].currentDocument);
}
/**
* 增加一个属性的管道处理函数
* @param transducer
* @param stage
*/
addPropsTransducer(transducer: PropsTransducer, stage: TransformStage) {
this[projectSymbol].designer.addPropsReducer(transducer, stage);
}
/**
* 当前 project 内的 document 变更事件
*/
onChangeDocument(fn: (doc: DocumentModel) => void) {
if (this[projectSymbol].currentDocument) {
fn(DocumentModel.create(this[projectSymbol].currentDocument)!);
return () => {};
}
return this[projectSymbol].onCurrentDocumentChange((originalDoc) => {
fn(DocumentModel.create(originalDoc)!);
});
}
/**
* 当前 project 的模拟器 ready 事件
*/
onSimulatorHostReady(fn: (host: SimulatorHost) => void) {
if (this[simulatorHostSymbol]) {
fn(SimulatorHost.create(this[simulatorHostSymbol])!);
return () => {};
}
return this[projectSymbol].onSimulatorReady((simulator: BuiltinSimulatorHost) => {
this[simulatorHostSymbol] = simulator;
fn(SimulatorHost.create(simulator)!);
});
}
/**
* 当前 project 的渲染器 ready 事件
*/
onSimulatorRendererReady(fn: () => void) {
if (this[simulatorRendererSymbol]) {
fn();
return () => {};
}
// TODO: 补充 renderer 实例
return this[projectSymbol].onRendererReady((renderer: any) => {
this[simulatorRendererSymbol] = renderer;
fn();
});
}
}