forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.ts
More file actions
118 lines (105 loc) · 2.63 KB
/
selection.ts
File metadata and controls
118 lines (105 loc) · 2.63 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
import {
IDocumentModel as InnerDocumentModel,
INode as InnerNode,
ISelection,
} from '@alilc/lowcode-designer';
import { Node as ShellNode } from './node';
import { selectionSymbol } from '../symbols';
import { IPublicModelSelection, IPublicModelNode, IPublicTypeDisposable } from '@alilc/lowcode-types';
export class Selection implements IPublicModelSelection {
private readonly [selectionSymbol]: ISelection;
constructor(document: InnerDocumentModel) {
this[selectionSymbol] = document.selection;
}
/**
* 返回选中的节点 id
*/
get selected(): string[] {
return this[selectionSymbol].selected;
}
/**
* return selected Node instance
*/
get node(): IPublicModelNode | null {
const nodes = this.getNodes();
return nodes && nodes.length > 0 ? nodes[0] : null;
}
/**
* 选中指定节点(覆盖方式)
* @param id
*/
select(id: string): void {
this[selectionSymbol].select(id);
}
/**
* 批量选中指定节点们
* @param ids
*/
selectAll(ids: string[]): void {
this[selectionSymbol].selectAll(ids);
}
/**
* 移除选中的指定节点
* @param id
*/
remove(id: string): void {
this[selectionSymbol].remove(id);
}
/**
* 清除所有选中节点
*/
clear(): void {
this[selectionSymbol].clear();
}
/**
* 判断是否选中了指定节点
* @param id
* @returns
*/
has(id: string): boolean {
return this[selectionSymbol].has(id);
}
/**
* 选中指定节点(增量方式)
* @param id
*/
add(id: string): void {
this[selectionSymbol].add(id);
}
/**
* 获取选中的节点实例
* @returns
*/
getNodes(): IPublicModelNode[] {
const innerNodes = this[selectionSymbol].getNodes();
const nodes: IPublicModelNode[] = [];
innerNodes.forEach((node: InnerNode) => {
const shellNode = ShellNode.create(node);
if (shellNode) {
nodes.push(shellNode);
}
});
return nodes;
}
/**
* 获取选区的顶层节点
* for example:
* getNodes() returns [A, subA, B], then
* getTopNodes() will return [A, B], subA will be removed
* @returns
*/
getTopNodes(includeRoot: boolean = false): IPublicModelNode[] {
const innerNodes = this[selectionSymbol].getTopNodes(includeRoot);
const nodes: IPublicModelNode[] = [];
innerNodes.forEach((node: InnerNode) => {
const shellNode = ShellNode.create(node);
if (shellNode) {
nodes.push(shellNode);
}
});
return nodes;
}
onSelectionChange(fn: (ids: string[]) => void): IPublicTypeDisposable {
return this[selectionSymbol].onSelectionChange(fn);
}
}