mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Update tree dnd sample (#567)
This commit is contained in:
@ -38,7 +38,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
// Drag and Drop proposed API sample
|
||||
// This check is for older versions of VS Code that don't have the most up-to-date tree drag and drop API proposal.
|
||||
if (typeof vscode.TreeDataTransferItem === 'function') {
|
||||
if (typeof vscode.DataTransferItem === 'function') {
|
||||
new TestViewDragAndDrop(context);
|
||||
}
|
||||
}
|
||||
@ -6,8 +6,8 @@ export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscod
|
||||
dropMimeTypes = ['application/vnd.code.tree.testViewDragAndDrop'];
|
||||
dragMimeTypes = ['text/uri-list'];
|
||||
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;
|
||||
// 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 = {
|
||||
'a': {
|
||||
'aa': {
|
||||
@ -57,7 +57,7 @@ export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscod
|
||||
|
||||
// Drag and drop controller
|
||||
|
||||
public async handleDrop(target: Node, sources: vscode.TreeDataTransfer, token: vscode.CancellationToken): Promise<void> {
|
||||
public async handleDrop(target: Node, sources: vscode.DataTransfer, token: vscode.CancellationToken): Promise<void> {
|
||||
const transferItem = sources.get('application/vnd.code.tree.testViewDragAndDrop');
|
||||
if (!transferItem) {
|
||||
return;
|
||||
@ -74,8 +74,8 @@ export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscod
|
||||
}
|
||||
}
|
||||
|
||||
public async handleDrag(source: Node[], treeDataTransfer: vscode.TreeDataTransfer, token: vscode.CancellationToken): Promise<void> {
|
||||
treeDataTransfer.set('application/vnd.code.tree.testViewDragAndDrop', new vscode.TreeDataTransferItem(source));
|
||||
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
|
||||
|
||||
@ -7,17 +7,6 @@ declare module 'vscode' {
|
||||
|
||||
// https://github.com/microsoft/vscode/issues/32592
|
||||
|
||||
/**
|
||||
* A data provider that provides tree data
|
||||
*/
|
||||
export interface TreeDataProvider<T> {
|
||||
/**
|
||||
* An optional event to signal that an element or root has changed.
|
||||
* This will trigger the view to update the changed element/root and its children recursively (if shown).
|
||||
* To signal that root has changed, do not pass any argument or pass `undefined` or `null`.
|
||||
*/
|
||||
onDidChangeTreeData2?: Event<T | T[] | undefined | null | void>;
|
||||
}
|
||||
|
||||
export interface TreeViewOptions<T> {
|
||||
/**
|
||||
@ -27,13 +16,12 @@ declare module 'vscode' {
|
||||
}
|
||||
|
||||
/**
|
||||
* A class for encapsulating data transferred during a tree drag and drop event.
|
||||
* A class for encapsulating data transferred during a drag and drop event.
|
||||
*
|
||||
* 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.
|
||||
* You can use the `value` of the `DataTransferItem` to get back the object you put into it
|
||||
* so long as the extension that created the `DataTransferItem` runs in the same extension host.
|
||||
*/
|
||||
export class TreeDataTransferItem {
|
||||
export class DataTransferItem {
|
||||
asString(): Thenable<string>;
|
||||
readonly value: any;
|
||||
constructor(value: any);
|
||||
@ -41,10 +29,11 @@ declare module 'vscode' {
|
||||
|
||||
/**
|
||||
* A map containing a mapping of the mime type of the corresponding transferred data.
|
||||
* Trees that support drag and drop can implement `DragAndDropController.handleDrag` to add additional mime types
|
||||
* when the drop occurs on an item in the same tree.
|
||||
* Drag and drop controllers that implement `handleDrag` can additional mime types to the data transfer
|
||||
* These additional mime types will only be included in the `handleDrop` when the the drag was initiated from
|
||||
* an element in the same drag and drop controller.
|
||||
*/
|
||||
export class TreeDataTransfer<T extends TreeDataTransferItem = TreeDataTransferItem> {
|
||||
export class DataTransfer<T extends DataTransferItem = DataTransferItem> {
|
||||
/**
|
||||
* Retrieves the data transfer item for a given mime type.
|
||||
* @param mimeType The mime type to get the data transfer item for.
|
||||
@ -105,7 +94,7 @@ declare module 'vscode' {
|
||||
* @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, token: CancellationToken): Thenable<void> | void;
|
||||
handleDrag?(source: T[], treeDataTransfer: DataTransfer, token: CancellationToken): Thenable<void> | void;
|
||||
|
||||
/**
|
||||
* Called when a drag and drop action results in a drop on the tree that this `DragAndDropController` belongs too.
|
||||
@ -116,6 +105,6 @@ declare module 'vscode' {
|
||||
* @param target The target tree element that the drop is occurring on.
|
||||
* @param token A cancellation token indicating that the drop has been cancelled.
|
||||
*/
|
||||
handleDrop(target: T, source: TreeDataTransfer, token: CancellationToken): Thenable<void> | void;
|
||||
handleDrop?(target: T, source: DataTransfer, token: CancellationToken): Thenable<void> | void;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user