diff --git a/drop-on-document/src/extension.ts b/drop-on-document/src/extension.ts index d637d5dd..1d949c7e 100644 --- a/drop-on-document/src/extension.ts +++ b/drop-on-document/src/extension.ts @@ -70,7 +70,7 @@ class FileNameListOnDropProvider implements vscode.DocumentOnDropEditProvider { const data = await file.data(); const text = decoder.decode(data); const fileContentsPreview = text.slice(0, 100); - snippet.appendText(file.name + '—' + fileContentsPreview + '\n'); + snippet.appendText(file.name + ' — ' + fileContentsPreview + '\n'); } return new vscode.DocumentDropEdit(snippet); diff --git a/tree-view-sample/package.json b/tree-view-sample/package.json index 8a4c0c3d..c995f391 100644 --- a/tree-view-sample/package.json +++ b/tree-view-sample/package.json @@ -16,6 +16,9 @@ "categories": [ "Other" ], + "enabledApiProposals": [ + "dataTransferFiles" + ], "activationEvents": [ "onView:nodeDependencies", "onView:ftpExplorer", diff --git a/tree-view-sample/src/testViewDragAndDrop.ts b/tree-view-sample/src/testViewDragAndDrop.ts index 34cdd2e1..acbaa44b 100644 --- a/tree-view-sample/src/testViewDragAndDrop.ts +++ b/tree-view-sample/src/testViewDragAndDrop.ts @@ -1,9 +1,11 @@ +import { TextDecoder } from 'util'; import * as vscode from 'vscode'; +const decoder = new TextDecoder(); export class TestViewDragAndDrop implements vscode.TreeDataProvider, vscode.TreeDragAndDropController { - dropMimeTypes = ['application/vnd.code.tree.testViewDragAndDrop']; + dropMimeTypes = ['application/vnd.code.tree.testViewDragAndDrop', 'text/uri-list']; dragMimeTypes = ['text/uri-list']; private _onDidChangeTreeData: vscode.EventEmitter = new vscode.EventEmitter(); // 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, vscod // Drag and drop controller public async handleDrop(target: Node | undefined, sources: vscode.DataTransfer, token: vscode.CancellationToken): Promise { + // 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; diff --git a/tree-view-sample/src/vscode.proposed.dataTransferFiles.d.ts b/tree-view-sample/src/vscode.proposed.dataTransferFiles.d.ts new file mode 100644 index 00000000..24baea79 --- /dev/null +++ b/tree-view-sample/src/vscode.proposed.dataTransferFiles.d.ts @@ -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; + } + + 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; + } +}