-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathtestViewDragAndDrop.ts
215 lines (193 loc) · 6.23 KB
/
testViewDragAndDrop.ts
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
import * as vscode from 'vscode';
export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscode.TreeDragAndDropController<Node> {
dropMimeTypes = ['application/vnd.code.tree.testViewDragAndDrop'];
dragMimeTypes = ['text/uri-list'];
private _onDidChangeTreeData: vscode.EventEmitter<(Node | undefined)[] | undefined> = new vscode.EventEmitter<Node[] | undefined>();
// We want to use an array as the event type, but the API for this is currently being finalized. Until it's finalized, use any.
public onDidChangeTreeData: vscode.Event<any> = this._onDidChangeTreeData.event;
public tree: any = {
'a': {
'aa': {
'aaa': {
'aaaa': {
'aaaaa': {
'aaaaaa': {
}
}
}
}
},
'ab': {}
},
'b': {
'ba': {},
'bb': {}
}
};
// Keep track of any nodes we create so that we can re-use the same objects.
private nodes: any = {};
constructor(context: vscode.ExtensionContext) {
const view = vscode.window.createTreeView('testViewDragAndDrop', { treeDataProvider: this, showCollapseAll: true, canSelectMany: true, dragAndDropController: this });
context.subscriptions.push(view);
}
// Tree data provider
public getChildren(element: Node): Node[] {
return this._getChildren(element ? element.key : undefined).map(key => this._getNode(key));
}
public getTreeItem(element: Node): vscode.TreeItem {
const treeItem = this._getTreeItem(element.key);
treeItem.id = element.key;
return treeItem;
}
public getParent(element: Node): Node {
return this._getParent(element.key);
}
dispose(): void {
// nothing to dispose
}
// Drag and drop controller
public async handleDrop(target: Node | undefined, sources: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> {
const transferItem = sources.get('application/vnd.code.tree.testViewDragAndDrop');
if (!transferItem) {
return;
}
const treeItems: Node[] = transferItem.value;
let roots = this._getLocalRoots(treeItems);
// Remove nodes that are already target's parent nodes
roots = roots.filter(r => !this._isChild(this._getTreeElement(r.key), target));
if (roots.length > 0) {
// Reload parents of the moving elements
const parents = roots.map(r => this.getParent(r));
roots.forEach(r => this._reparentNode(r, target));
this._onDidChangeTreeData.fire([...parents, target]);
}
}
public async handleDrag(source: Node[], treeDataTransfer: vscode.DataTransfer, _token: vscode.CancellationToken): Promise<void> {
treeDataTransfer.set('application/vnd.code.tree.testViewDragAndDrop', new vscode.DataTransferItem(source));
}
// Helper methods
_isChild(node: Node, child: Node | undefined): boolean {
if (!child) {
return false;
}
for (const prop in node) {
if (prop === child.key) {
return true;
} else {
const isChild = this._isChild((node as any)[prop], child);
if (isChild) {
return isChild;
}
}
}
return false;
}
// From the given nodes, filter out all nodes who's parent is already in the the array of Nodes.
_getLocalRoots(nodes: Node[]): Node[] {
const localRoots = [];
for (const node of nodes) {
const parent = this.getParent(node);
if (parent) {
const isInList = nodes.find(n => n.key === parent.key);
if (isInList === undefined) {
localRoots.push(node);
}
} else {
localRoots.push(node);
}
}
return localRoots;
}
// Remove node from current position and add node to new target element
_reparentNode(node: Node, target: Node | undefined): void {
const element: any = {};
element[node.key] = this._getTreeElement(node.key);
const elementCopy = { ...element };
this._removeNode(node);
const targetElement = this._getTreeElement(target?.key);
if (Object.keys(element).length === 0) {
targetElement[node.key] = {};
} else {
Object.assign(targetElement, elementCopy);
}
}
// Remove node from tree
_removeNode(element: Node, tree?: any): void {
const subTree = tree ? tree : this.tree;
for (const prop in subTree) {
if (prop === element.key) {
const parent = this.getParent(element);
if (parent) {
const parentObject = this._getTreeElement(parent.key);
delete parentObject[prop];
} else {
delete this.tree[prop];
}
} else {
this._removeNode(element, subTree[prop]);
}
}
}
_getChildren(key: string | undefined): string[] {
if (!key) {
return Object.keys(this.tree);
}
const treeElement = this._getTreeElement(key);
if (treeElement) {
return Object.keys(treeElement);
}
return [];
}
_getTreeItem(key: string): vscode.TreeItem {
const treeElement = this._getTreeElement(key);
// An example of how to use codicons in a MarkdownString in a tree item tooltip.
const tooltip = new vscode.MarkdownString(`$(zap) Tooltip for ${key}`, true);
return {
label: /**vscode.TreeItemLabel**/{ label: key, highlights: key.length > 1 ? [[key.length - 2, key.length - 1]] : void 0 } as any,
tooltip,
collapsibleState: treeElement && Object.keys(treeElement).length ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None,
resourceUri: vscode.Uri.parse(`/tmp/${key}`),
};
}
_getTreeElement(element: string | undefined, tree?: any): any {
if (!element) {
return this.tree;
}
const currentNode = tree ?? this.tree;
for (const prop in currentNode) {
if (prop === element) {
return currentNode[prop];
} else {
const treeElement = this._getTreeElement(element, currentNode[prop]);
if (treeElement) {
return treeElement;
}
}
}
}
_getParent(element: string, parent?: string, tree?: any): any {
const currentNode = tree ?? this.tree;
for (const prop in currentNode) {
if (prop === element && parent) {
return this._getNode(parent);
} else {
const parent = this._getParent(element, prop, currentNode[prop]);
if (parent) {
return parent;
}
}
}
}
_getNode(key: string): Node {
if (!this.nodes[key]) {
this.nodes[key] = new Key(key);
}
return this.nodes[key];
}
}
interface Node {
key: string;
}
class Key {
constructor(readonly key: string) { }
}