mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Update tree drag and drop sample
This commit is contained in:
@ -1,27 +1,10 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
// TestTreeDataTransferItem is for older versions of VS Code that don't have the most up-to-date tree drag and drop API proposal.
|
||||
const TestTreeDataTransferItem = (typeof vscode.TreeDataTransferItem === 'function') ? vscode.TreeDataTransferItem : class MockItem {
|
||||
async asString(): Promise<string> {
|
||||
return 'mock';
|
||||
}
|
||||
|
||||
constructor(_value: any) { /* this is a mock constructor */ }
|
||||
};
|
||||
|
||||
|
||||
class TestViewObjectTransferItem extends TestTreeDataTransferItem {
|
||||
constructor(private _nodes: Node[]) {
|
||||
super(_nodes);
|
||||
}
|
||||
|
||||
asObject(): Node[] {
|
||||
return this._nodes;
|
||||
}
|
||||
}
|
||||
|
||||
export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscode.TreeDragAndDropController<Node> {
|
||||
supportedMimeTypes = ['text/treeitems'];
|
||||
dropMimeTypes = ['text/treeitems'];
|
||||
dragMimeTypes = [];
|
||||
private _onDidChangeTreeData: vscode.EventEmitter<Node[] | undefined> = new vscode.EventEmitter<Node[] | undefined>();
|
||||
// We want to use an array as the event type, so we use the proposed onDidChangeTreeData2.
|
||||
public onDidChangeTreeData2: vscode.Event<Node[] | undefined> = this._onDidChangeTreeData.event;
|
||||
@ -74,12 +57,12 @@ export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscod
|
||||
|
||||
// Drag and drop controller
|
||||
|
||||
public async handleDrop(sources: vscode.TreeDataTransfer, target: Node): Promise<void> {
|
||||
public async handleDrop(sources: vscode.TreeDataTransfer, target: Node, token: vscode.CancellationToken): Promise<void> {
|
||||
const transferItem = sources.get('text/treeitems');
|
||||
if (!transferItem || !(transferItem instanceof TestViewObjectTransferItem)) {
|
||||
if (!transferItem) {
|
||||
return;
|
||||
}
|
||||
const treeItems = transferItem.asObject();
|
||||
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));
|
||||
@ -91,8 +74,8 @@ export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscod
|
||||
}
|
||||
}
|
||||
|
||||
public async handleDrag(source: Node[], treeDataTransfer: vscode.TreeDataTransfer): Promise<void> {
|
||||
treeDataTransfer.set('text/treeitems', new TestViewObjectTransferItem(source));
|
||||
public async handleDrag(source: Node[], treeDataTransfer: vscode.TreeDataTransfer, token: vscode.CancellationToken): Promise<void> {
|
||||
treeDataTransfer.set('text/treeitems', new vscode.TreeDataTransferItem(source));
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
@ -29,24 +29,13 @@ declare module 'vscode' {
|
||||
/**
|
||||
* A class for encapsulating data transferred during a tree drag and drop event.
|
||||
*
|
||||
* If your `DragAndDropController` implements `handleDrag`, you can extend `TreeDataTransferItem` and return
|
||||
* an instance of your new class for easy access to the source tree items.
|
||||
*
|
||||
* ```ts
|
||||
* class TestViewObjectTransferItem extends vscode.TreeDataTransferItem {
|
||||
* constructor(private _nodes: Node[]) {
|
||||
* super(_nodes);
|
||||
* }
|
||||
*
|
||||
* asObject(): Node[] {
|
||||
* return this._nodes;
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* If your `DragAndDropController` implements `handleDrag`, you can use the `value` of the `TreeDataTransferItem`
|
||||
* to get back the object you put into it so long as the extension that created the `TreeDataTransferItem` runs in the same
|
||||
* extension host.
|
||||
*/
|
||||
export class TreeDataTransferItem {
|
||||
asString(): Thenable<string>;
|
||||
|
||||
readonly value: any;
|
||||
constructor(value: any);
|
||||
}
|
||||
|
||||
@ -82,8 +71,8 @@ declare module 'vscode' {
|
||||
export interface TreeDragAndDropController<T> {
|
||||
|
||||
/**
|
||||
* The mime types that the `drop` method of this `DragAndDropController` supports. This could be well-defined, existing, mime types,
|
||||
* and also mime types defined by the extension that are returned in the `TreeDataTransfer` from `handleDrag`.
|
||||
* The mime types that the `handleDrop` method of this `DragAndDropController` supports.
|
||||
* This could be well-defined, existing, mime types, and also mime types defined by the extension.
|
||||
*
|
||||
* Each tree will automatically support drops from it's own `DragAndDropController`. To support drops from other trees,
|
||||
* you will need to add the mime type of that tree. The mime type of a tree is of the format `tree/treeidlowercase`.
|
||||
@ -93,7 +82,13 @@ declare module 'vscode' {
|
||||
* 2. Use the Developer: Set Log Level... command to set the level to "Debug"
|
||||
* 3. Open the developer tools and drag the item with unknown mime type over your tree. The mime types will be logged to the developer console
|
||||
*/
|
||||
readonly supportedMimeTypes: string[];
|
||||
readonly dropMimeTypes: string[];
|
||||
|
||||
/**
|
||||
* The mime types that the `handleDrag` method of this `TreeDragAndDropController` may add to the tree data transfer.
|
||||
* This could be well-defined, existing, mime types, and also mime types defined by the extension.
|
||||
*/
|
||||
readonly dragMimeTypes: string[];
|
||||
|
||||
/**
|
||||
* When the user starts dragging items from this `DragAndDropController`, `handleDrag` will be called.
|
||||
@ -102,10 +97,15 @@ declare module 'vscode' {
|
||||
* When the items are dropped on **another tree item** in **the same tree**, your `TreeDataTransferItem` objects
|
||||
* will be preserved. See the documentation for `TreeDataTransferItem` for how best to take advantage of this.
|
||||
*
|
||||
* To add a data transfer item that can be dragged into the editor, use the application specific mime type "resourceurls".
|
||||
* The data for "resourceurls" should be an array of `toString()`ed Uris. To specify a cursor position in the file,
|
||||
* set the Uri's fragment to `L3,5`, where 3 is the line number and 5 is the column number.
|
||||
*
|
||||
* @param source The source items for the drag and drop operation.
|
||||
* @param treeDataTransfer The data transfer associated with this drag.
|
||||
* @param token A cancellation token indicating that drag has been cancelled.
|
||||
*/
|
||||
handleDrag?(source: T[], treeDataTransfer: TreeDataTransfer): Thenable<void> | void;
|
||||
handleDrag?(source: T[], treeDataTransfer: TreeDataTransfer, token: CancellationToken): Thenable<void> | void;
|
||||
|
||||
/**
|
||||
* Called when a drag and drop action results in a drop on the tree that this `DragAndDropController` belongs too.
|
||||
@ -113,8 +113,9 @@ declare module 'vscode' {
|
||||
* Extensions should fire `TreeDataProvider.onDidChangeTreeData` for any elements that need to be refreshed.
|
||||
*
|
||||
* @param source The data transfer items of the source of the drag.
|
||||
* @param target The target tree element that the drop is occuring on.
|
||||
* @param target The target tree element that the drop is occurring on.
|
||||
* @param token TODO @alexr00: When would this operation actually be cancelled?
|
||||
*/
|
||||
handleDrop(source: TreeDataTransfer, target: T): Thenable<void> | void;
|
||||
handleDrop(source: TreeDataTransfer, target: T, token: CancellationToken): Thenable<void> | void;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user