forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.ts
More file actions
403 lines (352 loc) · 8.07 KB
/
node.ts
File metadata and controls
403 lines (352 loc) · 8.07 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import {
DocumentModel as InnerDocumentModel,
Node as InnerNode,
getConvertedExtraKey,
} from '@alilc/lowcode-designer';
import { CompositeValue, NodeSchema, TransformStage } from '@alilc/lowcode-types';
import Prop from './prop';
import Props from './props';
import DocumentModel from './document-model';
import NodeChildren from './node-children';
import ComponentMeta from './component-meta';
import { documentSymbol, nodeSymbol } from './symbols';
const shellNodeSymbol = Symbol('shellNodeSymbol');
export default class Node {
private readonly [documentSymbol]: InnerDocumentModel;
private readonly [nodeSymbol]: InnerNode;
constructor(node: InnerNode) {
this[nodeSymbol] = node;
this[documentSymbol] = node.document;
}
static create(node: InnerNode | null | undefined) {
if (!node) return null;
// @ts-ignore 直接返回已挂载的 shell node 实例
if (node[shellNodeSymbol]) return node[shellNodeSymbol];
const shellNode = new Node(node);
// @ts-ignore 挂载 shell node 实例
node[shellNodeSymbol] = shellNode;
return shellNode;
}
/**
* 节点 id
*/
get id() {
return this[nodeSymbol].id;
}
/**
* 节点标题
*/
get title() {
return this[nodeSymbol].title;
}
/**
* 是否为「容器型」节点
*/
get isContainer() {
return this[nodeSymbol].isContainer();
}
/**
* 是否为根节点
*/
get isRoot() {
return this[nodeSymbol].isRoot();
}
/**
* 是否为空节点(无 children 或者 children 为空)
*/
get isEmpty() {
return this[nodeSymbol].isEmpty();
}
/**
* 是否为 Page 节点
*/
get isPage() {
return this[nodeSymbol].isPage();
}
/**
* 是否为 Component 节点
*/
get isComponent() {
return this[nodeSymbol].isComponent();
}
/**
* 是否为「模态框」节点
*/
get isModal() {
return this[nodeSymbol].isModal();
}
/**
* 是否为插槽节点
*/
get isSlot() {
return this[nodeSymbol].isSlot();
}
/**
* 是否为父类/分支节点
*/
get isParental() {
return this[nodeSymbol].isParental();
}
/**
* 是否为叶子节点
*/
get isLeaf() {
return this[nodeSymbol].isLeaf();
}
/**
* 下标
*/
get index() {
return this[nodeSymbol].index;
}
/**
* 图标
*/
get icon() {
return this[nodeSymbol].icon;
}
/**
* 节点所在树的层级深度,根节点深度为 0
*/
get zLevel() {
return this[nodeSymbol].zLevel;
}
/**
* 节点 componentName
*/
get componentName() {
return this[nodeSymbol].componentName;
}
/**
* 节点的物料元数据
*/
get componentMeta() {
return ComponentMeta.create(this[nodeSymbol].componentMeta);
}
/**
* 获取节点所属的文档模型对象
* @returns
*/
get document() {
return DocumentModel.create(this[documentSymbol]);
}
/**
* 获取当前节点的前一个兄弟节点
* @returns
*/
get prevSibling(): Node | null {
return Node.create(this[nodeSymbol].prevSibling);
}
/**
* 获取当前节点的后一个兄弟节点
* @returns
*/
get nextSibling(): Node | null {
return Node.create(this[nodeSymbol].nextSibling);
}
/**
* 获取当前节点的父亲节点
* @returns
*/
get parent(): Node | null {
return Node.create(this[nodeSymbol].parent);
}
/**
* 获取当前节点的孩子节点模型
* @returns
*/
get children() {
return NodeChildren.create(this[nodeSymbol].children);
}
/**
* 节点上挂载的插槽节点们
*/
get slots(): Node[] {
return this[nodeSymbol].slots.map((node: InnerNode) => Node.create(node)!);
}
/**
* 当前节点为插槽节点时,返回节点对应的属性实例
*/
get slotFor() {
return Prop.create(this[nodeSymbol].slotFor);
}
/**
* 返回节点的属性集
*/
get props() {
return Props.create(this[nodeSymbol].props);
}
/**
* 返回节点的属性集
*/
get propsData() {
return this[nodeSymbol].propsData;
}
/**
* @deprecated use .children instead
*/
getChildren() {
return this.children;
}
/**
* 获取节点实例对应的 dom 节点
*/
getDOMNode() {
return this[nodeSymbol].getDOMNode();
}
/**
* 返回节点的尺寸、位置信息
* @returns
*/
getRect() {
return this[nodeSymbol].getRect();
}
/**
* 是否有挂载插槽节点
* @returns
*/
hasSlots() {
return this[nodeSymbol].hasSlots();
}
/**
* 是否设定了渲染条件
* @returns
*/
hasCondition() {
return this[nodeSymbol].hasCondition();
}
/**
* 是否设定了循环数据
* @returns
*/
hasLoop() {
return this[nodeSymbol].hasLoop();
}
/**
* @deprecated use .props instead
*/
getProps() {
return this.props;
}
/**
* 获取指定 path 的属性模型实例
* @param path 属性路径,支持 a / a.b / a.0 等格式
* @returns
*/
getProp(path: string): Prop | null {
return Prop.create(this[nodeSymbol].getProp(path));
}
/**
* 获取指定 path 的属性模型实例值
* @param path 属性路径,支持 a / a.b / a.0 等格式
* @returns
*/
getPropValue(path: string) {
return this.getProp(path)?.getValue();
}
/**
* 获取指定 path 的属性模型实例,
* 注:导出时,不同于普通属性,该属性并不挂载在 props 之下,而是与 props 同级
* @param path 属性路径,支持 a / a.b / a.0 等格式
* @returns
*/
getExtraProp(path: string): Prop | null {
return Prop.create(this[nodeSymbol].getProp(getConvertedExtraKey(path)));
}
/**
* 获取指定 path 的属性模型实例,
* 注:导出时,不同于普通属性,该属性并不挂载在 props 之下,而是与 props 同级
* @param path 属性路径,支持 a / a.b / a.0 等格式
* @returns
*/
getExtraPropValue(path: string) {
return this.getExtraProp(path)?.getValue();
}
/**
* 设置指定 path 的属性模型实例值
* @param path 属性路径,支持 a / a.b / a.0 等格式
* @param value 值
* @returns
*/
setPropValue(path: string, value: CompositeValue) {
return this.getProp(path)?.setValue(value);
}
/**
* 设置指定 path 的属性模型实例值
* @param path 属性路径,支持 a / a.b / a.0 等格式
* @param value 值
* @returns
*/
setExtraPropValue(path: string, value: CompositeValue) {
return this.getExtraProp(path)?.setValue(value);
}
/**
* 导入节点数据
* @param data
*/
importSchema(data: NodeSchema) {
this[nodeSymbol].import(data);
}
/**
* 导出节点数据
* @param stage
* @param options
* @returns
*/
exportSchema(stage: TransformStage = TransformStage.Render, options?: any) {
return this[nodeSymbol].export(stage, options);
}
/**
* 在指定位置之前插入一个节点
* @param node
* @param ref
* @param useMutator
*/
insertBefore(node: Node, ref?: Node | undefined, useMutator?: boolean) {
this[nodeSymbol].insertBefore(node[nodeSymbol] || node, ref?.[nodeSymbol], useMutator);
}
/**
* 在指定位置之后插入一个节点
* @param node
* @param ref
* @param useMutator
*/
insertAfter(node: Node, ref?: Node | undefined, useMutator?: boolean) {
this[nodeSymbol].insertAfter(node[nodeSymbol] || node, ref?.[nodeSymbol], useMutator);
}
/**
* 替换指定节点
* @param node 待替换的子节点
* @param data 用作替换的节点对象或者节点描述
* @returns
*/
replaceChild(node: Node, data: any) {
return Node.create(this[nodeSymbol].replaceChild(node[nodeSymbol], data));
}
/**
* 将当前节点替换成指定节点描述
* @param schema
*/
replaceWith(schema: NodeSchema) {
this[nodeSymbol].replaceWith(schema);
}
/**
* 选中当前节点实例
*/
select() {
this[nodeSymbol].select();
}
/**
* 设置悬停态
* @param flag
*/
hover(flag = true) {
this[nodeSymbol].hover(flag);
}
/**
* 删除当前节点实例
*/
remove() {
this[nodeSymbol].remove();
}
}