From 18014bcbd083e931f2acb972986da09bb0c15468 Mon Sep 17 00:00:00 2001 From: Steven Hermans Date: Sat, 9 May 2020 14:54:09 +0200 Subject: [PATCH 1/2] Upload folders via D&D in the browser workbench --- .../files/browser/views/explorerViewer.ts | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts b/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts index 7458682b6c6c3..ea373be4aad13 100644 --- a/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts +++ b/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts @@ -939,27 +939,51 @@ export class FileDragAndDrop implements ITreeDragAndDrop { } private async handleWebExternalDrop(data: DesktopDragAndDropData, target: ExplorerItem, originalEvent: DragEvent): Promise { - data.files.forEach(file => { + for (let item of originalEvent.dataTransfer.items) { + const entry = item.webkitGetAsEntry(); + await this.uploadFileEntry(entry, target.resource, target); + }); + } + + private async uploadFileEntry(entry: FileSystemEntry, parentResource: URI, target: ExplorerItem): Promise { + const resource = joinPath(parentResource, entry.name); + + if (entry.isFile) { + // Handle file upload + if (target && target.getChild(entry.name)) { + const { confirmed } = await this.dialogService.confirm(getFileOverwriteConfirm(name)); + if (!confirmed) { + return; + } + } + + const file = await new Promise((resolve, reject) => entry.file(resolve, reject)); const reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = async (event) => { const name = file.name; if (typeof name === 'string' && event.target?.result instanceof ArrayBuffer) { - if (target.getChild(name)) { - const { confirmed } = await this.dialogService.confirm(getFileOverwriteConfirm(name)); - if (!confirmed) { - return; - } - } - const resource = joinPath(target.resource, name); await this.fileService.writeFile(resource, VSBuffer.wrap(new Uint8Array(event.target.result))); if (data.files.length === 1) { await this.editorService.openEditor({ resource, options: { pinned: true } }); } } }; - }); + } else if (entry.isDirectory) { + // Handle folder upload + await this.fileService.createFolder(resource); + + // Recursive upload files in this directory + const folderTarget = target && target.getChild(entry.name) || undefined; + const dirReader = entry.createReader(); + const childEntries = await new Promise((resolve, reject) => { + dirReader.readEntries(resolve, reject); + }); + for (let childEntry of childEntries){ + await this.uploadFileEntry(childEntry, resource, folderTarget); + } + } } private async handleExternalDrop(data: DesktopDragAndDropData, target: ExplorerItem, originalEvent: DragEvent): Promise { From 2f01bd617ff9786a15ddf702f48126a2a842a5d5 Mon Sep 17 00:00:00 2001 From: Steven Hermans Date: Sat, 9 May 2020 17:03:13 +0000 Subject: [PATCH 2/2] Resolve compile issues --- .../files/browser/views/explorerViewer.ts | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts b/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts index ea373be4aad13..870da628a6a07 100644 --- a/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts +++ b/src/vs/workbench/contrib/files/browser/views/explorerViewer.ts @@ -939,25 +939,30 @@ export class FileDragAndDrop implements ITreeDragAndDrop { } private async handleWebExternalDrop(data: DesktopDragAndDropData, target: ExplorerItem, originalEvent: DragEvent): Promise { - for (let item of originalEvent.dataTransfer.items) { + const items = (originalEvent as any).dataTransfer.items; + for (let item of items) { const entry = item.webkitGetAsEntry(); await this.uploadFileEntry(entry, target.resource, target); - }); + + if (items.length === 1) { + await this.editorService.openEditor({ resource: joinPath(target.resource, entry.name), options: { pinned: true } }); + } + } } - private async uploadFileEntry(entry: FileSystemEntry, parentResource: URI, target: ExplorerItem): Promise { + private async uploadFileEntry(entry: any, parentResource: URI, target: ExplorerItem | undefined): Promise { const resource = joinPath(parentResource, entry.name); if (entry.isFile) { // Handle file upload if (target && target.getChild(entry.name)) { - const { confirmed } = await this.dialogService.confirm(getFileOverwriteConfirm(name)); + const { confirmed } = await this.dialogService.confirm(getFileOverwriteConfirm(resource.path)); if (!confirmed) { return; } } - const file = await new Promise((resolve, reject) => entry.file(resolve, reject)); + const file = await new Promise((resolve, reject) => entry.file(resolve, reject)); const reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = async (event) => { @@ -965,9 +970,6 @@ export class FileDragAndDrop implements ITreeDragAndDrop { if (typeof name === 'string' && event.target?.result instanceof ArrayBuffer) { await this.fileService.writeFile(resource, VSBuffer.wrap(new Uint8Array(event.target.result))); - if (data.files.length === 1) { - await this.editorService.openEditor({ resource, options: { pinned: true } }); - } } }; } else if (entry.isDirectory) { @@ -977,10 +979,10 @@ export class FileDragAndDrop implements ITreeDragAndDrop { // Recursive upload files in this directory const folderTarget = target && target.getChild(entry.name) || undefined; const dirReader = entry.createReader(); - const childEntries = await new Promise((resolve, reject) => { + const childEntries = await new Promise((resolve, reject) => { dirReader.readEntries(resolve, reject); }); - for (let childEntry of childEntries){ + for (let childEntry of childEntries) { await this.uploadFileEntry(childEntry, resource, folderTarget); } }