Add tree view sample

This commit is contained in:
Matt Bierner
2022-05-25 17:33:53 -07:00
parent 2e1659ae1b
commit c28bcd6507
4 changed files with 68 additions and 2 deletions

View File

@ -16,6 +16,9 @@
"categories": [
"Other"
],
"enabledApiProposals": [
"dataTransferFiles"
],
"activationEvents": [
"onView:nodeDependencies",
"onView:ftpExplorer",

View File

@ -1,9 +1,11 @@
import { TextDecoder } from 'util';
import * as vscode from 'vscode';
const decoder = new TextDecoder();
export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscode.TreeDragAndDropController<Node> {
dropMimeTypes = ['application/vnd.code.tree.testViewDragAndDrop'];
dropMimeTypes = ['application/vnd.code.tree.testViewDragAndDrop', 'text/uri-list'];
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, but the API for this is currently being finalized. Until it's finalized, use any.
@ -58,6 +60,25 @@ export class TestViewDragAndDrop implements vscode.TreeDataProvider<Node>, vscod
// Drag and drop controller
public async handleDrop(target: Node | undefined, sources: vscode.DataTransfer, token: vscode.CancellationToken): Promise<void> {
// Insert the file contents
const files: vscode.DataTransferFile[] = [];
sources.forEach((item) => {
const file = item.asFile();
if (file) {
files.push(file);
}
});
for (const file of files) {
const data = await file.data();
const text = decoder.decode(data);
const fileContentsPreview = text.slice(0, 100);
console.log(file.name + ' — ' + fileContentsPreview + '\n');
}
const transferItem = sources.get('application/vnd.code.tree.testViewDragAndDrop');
if (!transferItem) {
return;

View File

@ -0,0 +1,42 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
declare module 'vscode' {
// https://github.com/microsoft/vscode/issues/147481
/**
* A file associated with a {@linkcode DataTransferItem}.
*/
interface DataTransferFile {
/**
* The name of the file.
*/
readonly name: string;
/**
* The full file path of the file.
*
* May be undefined on web.
*/
readonly uri?: Uri;
/**
* The full file contents of the file.
*/
data(): Thenable<Uint8Array>;
}
export interface DataTransferItem {
/**
* Try getting the file associated with this data transfer item.
*
* Note that the file object is only valid for the scope of the drag and drop operation.
*
* @returns The file for the data transfer or `undefined` if the item is not a file.
*/
asFile(): DataTransferFile | undefined;
}
}