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
241 lines (219 loc) · 5.59 KB
/
document-model.ts
File metadata and controls
241 lines (219 loc) · 5.59 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
import { Editor } from '@alilc/lowcode-editor-core';
import {
DocumentModel as InnerDocumentModel,
Node as InnerNode,
ParentalNode,
IOnChangeOptions as InnerIOnChangeOptions,
PropChangeOptions as InnerPropChangeOptions,
} from '@alilc/lowcode-designer';
import { TransformStage, RootSchema, NodeSchema, NodeData, GlobalEvent } from '@alilc/lowcode-types';
import Node from './node';
import Selection from './selection';
import Detecting from './detecting';
import History from './history';
import Project from './project';
import Prop from './prop';
import Canvas from './canvas';
import ModalNodesManager from './modal-nodes-manager';
import { documentSymbol, editorSymbol, nodeSymbol } from './symbols';
type IOnChangeOptions = {
type: string;
node: Node;
};
type PropChangeOptions = {
key?: string | number;
prop?: Prop;
node: Node;
newValue: any;
oldValue: any;
};
export default class DocumentModel {
private readonly [documentSymbol]: InnerDocumentModel;
private readonly [editorSymbol]: Editor;
public selection: Selection;
public detecting: Detecting;
public history: History;
public canvas: Canvas;
constructor(document: InnerDocumentModel) {
this[documentSymbol] = document;
this[editorSymbol] = document.designer.editor as Editor;
this.selection = new Selection(document);
this.detecting = new Detecting(document);
this.history = new History(document.getHistory());
this.canvas = new Canvas(document.designer);
}
static create(document: InnerDocumentModel | undefined | null) {
if (document == undefined) return null;
return new DocumentModel(document);
}
/**
* 获取当前文档所属的 project
* @returns
*/
get project() {
return Project.create(this[documentSymbol].project);
}
/**
* 获取文档的根节点
* @returns
*/
get root(): Node | null {
return Node.create(this[documentSymbol].getRoot());
}
/**
* 获取文档下所有节点
* @returns
*/
get nodesMap() {
const map = new Map<string, Node>();
for (let id of this[documentSymbol].nodesMap.keys()) {
map.set(id, this.getNodeById(id)!);
}
return map;
}
/**
* 模态节点管理
*/
get modalNodesManager() {
return ModalNodesManager.create(this[documentSymbol].modalNodesManager);
}
/**
* 根据 nodeId 返回 Node 实例
* @param nodeId
* @returns
*/
getNodeById(nodeId: string) {
return Node.create(this[documentSymbol].getNode(nodeId));
}
/**
* 导入 schema
* @param schema
*/
importSchema(schema: RootSchema) {
this[documentSymbol].import(schema);
}
/**
* 导出 schema
* @param stage
* @returns
*/
exportSchema(stage: TransformStage = TransformStage.Render) {
return this[documentSymbol].export(stage);
}
/**
* 插入节点
* @param parent
* @param thing
* @param at
* @param copy
* @returns
*/
insertNode(
parent: Node,
thing: Node,
at?: number | null | undefined,
copy?: boolean | undefined,
) {
const node = this[documentSymbol].insertNode(
parent[nodeSymbol] as any,
thing?.[nodeSymbol],
at,
copy,
);
return Node.create(node);
}
/**
* 创建一个节点
* @param data
* @returns
*/
createNode(data: any) {
return Node.create(this[documentSymbol].createNode(data));
}
/**
* 移除指定节点/节点id
* @param idOrNode
*/
removeNode(idOrNode: string | Node) {
this[documentSymbol].removeNode(idOrNode as any);
}
/**
* 当前 document 新增节点事件
*/
onAddNode(fn: (node: Node) => void) {
this[documentSymbol].onNodeCreate((node: InnerNode) => {
fn(Node.create(node)!);
});
}
/**
* 当前 document 删除节点事件
*/
onRemoveNode(fn: (node: Node) => void) {
this[documentSymbol].onNodeDestroy((node: InnerNode) => {
fn(Node.create(node)!);
});
}
/**
* 当前 document 的 hover 变更事件
*/
onChangeDetecting(fn: (node: Node) => void) {
this[documentSymbol].designer.detecting.onDetectingChange((node: InnerNode) => {
fn(Node.create(node)!);
});
}
/**
* 当前 document 的选中变更事件
*/
onChangeSelection(fn: (ids: string[]) => void) {
this[documentSymbol].selection.onSelectionChange((ids: string[]) => {
fn(ids);
});
}
/**
* 当前 document 的节点显隐状态变更事件
* @param fn
*/
onChangeNodeVisible(fn: (node: Node, visible: boolean) => void) {
// TODO: history 变化时需要重新绑定
this[documentSymbol].nodesMap.forEach((node) => {
node.onVisibleChange((flag: boolean) => {
fn(Node.create(node)!, flag);
});
});
}
/**
* 当前 document 的节点 children 变更事件
* @param fn
*/
onChangeNodeChildren(fn: (info?: IOnChangeOptions) => void) {
// TODO: history 变化时需要重新绑定
this[documentSymbol].nodesMap.forEach((node) => {
node.onChildrenChange((info?: InnerIOnChangeOptions) => {
return info
? fn({
type: info.type,
node: Node.create(node)!,
})
: fn();
});
});
}
/**
* 当前 document 节点属性修改事件
* @param fn
*/
onChangeNodeProp(fn: (info: PropChangeOptions) => void) {
this[editorSymbol].on(
GlobalEvent.Node.Prop.InnerChange,
(info: GlobalEvent.Node.Prop.ChangeOptions) => {
fn({
key: info.key,
oldValue: info.oldValue,
newValue: info.newValue,
prop: Prop.create(info.prop)!,
node: Node.create(info.node as any)!,
});
},
);
}
}