forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument-model.ts
More file actions
381 lines (344 loc) · 10.3 KB
/
document-model.ts
File metadata and controls
381 lines (344 loc) · 10.3 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
import {
IDocumentModel as InnerDocumentModel,
INode as InnerNode,
} from '@alilc/lowcode-designer';
import {
IPublicEnumTransformStage,
IPublicTypeRootSchema,
GlobalEvent,
IPublicModelDocumentModel,
IPublicTypeOnChangeOptions,
IPublicTypeDragNodeObject,
IPublicTypeDragNodeDataObject,
IPublicModelNode,
IPublicModelSelection,
IPublicModelDetecting,
IPublicModelHistory,
IPublicApiProject,
IPublicModelModalNodesManager,
IPublicTypePropChangeOptions,
IPublicModelDropLocation,
IPublicApiCanvas,
IPublicTypeDisposable,
IPublicModelEditor,
IPublicTypeNodeSchema,
} from '@alilc/lowcode-types';
import { isDragNodeObject } from '@alilc/lowcode-utils';
import { Node as ShellNode } from './node';
import { Selection as ShellSelection } from './selection';
import { Detecting as ShellDetecting } from './detecting';
import { History as ShellHistory } from './history';
import { DropLocation as ShellDropLocation } from './drop-location';
import { Project as ShellProject, Canvas as ShellCanvas } from '../api';
import { Prop as ShellProp } from './prop';
import { ModalNodesManager } from './modal-nodes-manager';
import { documentSymbol, editorSymbol, nodeSymbol } from '../symbols';
const shellDocSymbol = Symbol('shellDocSymbol');
export class DocumentModel implements IPublicModelDocumentModel {
private readonly [documentSymbol]: InnerDocumentModel;
private readonly [editorSymbol]: IPublicModelEditor;
private _focusNode: IPublicModelNode | null;
selection: IPublicModelSelection;
detecting: IPublicModelDetecting;
history: IPublicModelHistory;
/**
* @deprecated use canvas API instead
*/
canvas: IPublicApiCanvas;
constructor(document: InnerDocumentModel) {
this[documentSymbol] = document;
this[editorSymbol] = document.designer?.editor as IPublicModelEditor;
this.selection = new ShellSelection(document);
this.detecting = new ShellDetecting(document);
this.history = new ShellHistory(document);
this.canvas = new ShellCanvas(this[editorSymbol]);
this._focusNode = ShellNode.create(this[documentSymbol].focusNode);
}
static create(document: InnerDocumentModel | undefined | null): IPublicModelDocumentModel | null {
if (!document) {
return null;
}
// @ts-ignore 直接返回已挂载的 shell doc 实例
if (document[shellDocSymbol]) {
return (document as any)[shellDocSymbol];
}
const shellDoc = new DocumentModel(document);
// @ts-ignore 直接返回已挂载的 shell doc 实例
document[shellDocSymbol] = shellDoc;
return shellDoc;
}
/**
* id
*/
get id(): string {
return this[documentSymbol].id;
}
set id(id) {
this[documentSymbol].id = id;
}
/**
* 获取当前文档所属的 project
* @returns
*/
get project(): IPublicApiProject {
return ShellProject.create(this[documentSymbol].project, true);
}
/**
* 获取文档的根节点
* root node of this documentModel
* @returns
*/
get root(): IPublicModelNode | null {
return ShellNode.create(this[documentSymbol].rootNode);
}
get focusNode(): IPublicModelNode | null {
return this._focusNode || this.root;
}
set focusNode(node: IPublicModelNode | null) {
this._focusNode = node;
this[editorSymbol].eventBus.emit(
'shell.document.focusNodeChanged',
{ document: this, focusNode: node },
);
}
/**
* 获取文档下所有节点 Map, key 为 nodeId
* get map of all nodes , using node.id as key
*/
get nodesMap(): Map<string, IPublicModelNode> {
const map = new Map<string, IPublicModelNode>();
for (let id of this[documentSymbol].nodesMap.keys()) {
map.set(id, this.getNodeById(id)!);
}
return map;
}
/**
* 模态节点管理
*/
get modalNodesManager(): IPublicModelModalNodesManager | null {
return ModalNodesManager.create(this[documentSymbol].modalNodesManager);
}
get dropLocation(): IPublicModelDropLocation | null {
return ShellDropLocation.create(this[documentSymbol].dropLocation);
}
set dropLocation(loc: IPublicModelDropLocation | null) {
this[documentSymbol].dropLocation = loc;
}
/**
* 根据 nodeId 返回 Node 实例
* get node instance by nodeId
* @param {string} nodeId
*/
getNodeById(nodeId: string): IPublicModelNode | null {
return ShellNode.create(this[documentSymbol].getNode(nodeId));
}
/**
* 导入 schema
* @param schema
*/
importSchema(schema: IPublicTypeRootSchema): void {
this[documentSymbol].import(schema);
this[editorSymbol].eventBus.emit('shell.document.importSchema', schema);
}
/**
* 导出 schema
* @param stage
* @returns
*/
exportSchema(stage: IPublicEnumTransformStage = IPublicEnumTransformStage.Render): IPublicTypeRootSchema | undefined {
return this[documentSymbol].export(stage);
}
/**
* 插入节点
* @param parent
* @param thing
* @param at
* @param copy
* @returns
*/
insertNode(
parent: IPublicModelNode,
thing: IPublicModelNode,
at?: number | null | undefined,
copy?: boolean | undefined,
): IPublicModelNode | null {
const node = this[documentSymbol].insertNode(
(parent as any)[nodeSymbol] ? (parent as any)[nodeSymbol] : parent,
(thing as any)?.[nodeSymbol] ? (thing as any)[nodeSymbol] : thing,
at,
copy,
);
return ShellNode.create(node);
}
/**
* 创建一个节点
* @param data
* @returns
*/
createNode<IPublicModelNode>(data: IPublicTypeNodeSchema): IPublicModelNode | null {
return ShellNode.create(this[documentSymbol].createNode(data));
}
/**
* 移除指定节点/节点id
* @param idOrNode
*/
removeNode(idOrNode: string | IPublicModelNode): void {
this[documentSymbol].removeNode(idOrNode as any);
}
/**
* componentsMap of documentModel
* @param extraComps
* @returns
*/
getComponentsMap(extraComps?: string[]): any {
return this[documentSymbol].getComponentsMap(extraComps);
}
/**
* 检查拖拽放置的目标节点是否可以放置该拖拽对象
* @param dropTarget 拖拽放置的目标节点
* @param dragObject 拖拽的对象
* @returns boolean 是否可以放置
*/
checkNesting(
dropTarget: IPublicModelNode,
dragObject: IPublicTypeDragNodeObject | IPublicTypeDragNodeDataObject,
): boolean {
let innerDragObject = dragObject;
if (isDragNodeObject(dragObject)) {
innerDragObject.nodes = innerDragObject.nodes?.map(
(node: IPublicModelNode) => ((node as any)[nodeSymbol] || node),
);
}
return this[documentSymbol].checkNesting(
((dropTarget as any)[nodeSymbol] || dropTarget) as any,
innerDragObject as any,
);
}
/**
* 当前 document 新增节点事件
*/
onAddNode(fn: (node: IPublicModelNode) => void): IPublicTypeDisposable {
return this[documentSymbol].onNodeCreate((node: InnerNode) => {
fn(ShellNode.create(node)!);
});
}
/**
* 当前 document 新增节点事件,此时节点已经挂载到 document 上
*/
onMountNode(fn: (payload: { node: IPublicModelNode }) => void): IPublicTypeDisposable {
return this[documentSymbol].onMountNode(({
node,
}) => {
fn({ node: ShellNode.create(node)! });
});
}
/**
* 当前 document 删除节点事件
*/
onRemoveNode(fn: (node: IPublicModelNode) => void): IPublicTypeDisposable {
return this[documentSymbol].onNodeDestroy((node: InnerNode) => {
fn(ShellNode.create(node)!);
});
}
/**
* 当前 document 的 hover 变更事件
*/
onChangeDetecting(fn: (node: IPublicModelNode) => void): IPublicTypeDisposable {
return this[documentSymbol].designer.detecting.onDetectingChange((node: InnerNode) => {
fn(ShellNode.create(node)!);
});
}
/**
* 当前 document 的选中变更事件
*/
onChangeSelection(fn: (ids: string[]) => void): IPublicTypeDisposable {
return this[documentSymbol].selection.onSelectionChange((ids: string[]) => {
fn(ids);
});
}
/**
* 当前 document 的节点显隐状态变更事件
* @param fn
*/
onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): IPublicTypeDisposable {
return this[documentSymbol].onChangeNodeVisible((node: InnerNode, visible: boolean) => {
fn(ShellNode.create(node)!, visible);
});
}
/**
* 当前 document 的节点 children 变更事件
* @param fn
*/
onChangeNodeChildren(fn: (info: IPublicTypeOnChangeOptions) => void): IPublicTypeDisposable {
return this[documentSymbol].onChangeNodeChildren((info?: IPublicTypeOnChangeOptions<InnerNode>) => {
if (!info) {
return;
}
fn({
type: info.type,
node: ShellNode.create(info.node)!,
});
});
}
/**
* 当前 document 节点属性修改事件
* @param fn
*/
onChangeNodeProp(fn: (info: IPublicTypePropChangeOptions) => void): IPublicTypeDisposable {
const callback = (info: GlobalEvent.Node.Prop.ChangeOptions) => {
fn({
key: info.key,
oldValue: info.oldValue,
newValue: info.newValue,
prop: ShellProp.create(info.prop)!,
node: ShellNode.create(info.node as any)!,
});
};
this[editorSymbol].on(
GlobalEvent.Node.Prop.InnerChange,
callback,
);
return () => {
this[editorSymbol].off(
GlobalEvent.Node.Prop.InnerChange,
callback,
);
};
}
/**
* import schema event
* @param fn
*/
onImportSchema(fn: (schema: IPublicTypeRootSchema) => void): IPublicTypeDisposable {
return this[editorSymbol].eventBus.on('shell.document.importSchema', fn as any);
}
isDetectingNode(node: IPublicModelNode): boolean {
return this.detecting.current === node;
}
onFocusNodeChanged(
fn: (doc: IPublicModelDocumentModel, focusNode: IPublicModelNode) => void,
): IPublicTypeDisposable {
if (!fn) {
return () => {};
}
return this[editorSymbol].eventBus.on(
'shell.document.focusNodeChanged',
(payload) => {
const { document, focusNode } = payload;
fn(document, focusNode);
},
);
}
onDropLocationChanged(fn: (doc: IPublicModelDocumentModel) => void): IPublicTypeDisposable {
if (!fn) {
return () => {};
}
return this[editorSymbol].eventBus.on(
'document.dropLocation.changed',
(payload) => {
const { document } = payload;
fn(document);
},
);
}
}