Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cross-product FS-provided imports #133116

Merged
merged 8 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions src/vs/workbench/browser/dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,34 @@ export interface IDraggedResourceEditorInput extends IBaseTextResourceEditorInpu
isExternal?: boolean;
}

export function extractEditorsDropData(e: DragEvent, externalOnly?: boolean): Array<IDraggedResourceEditorInput> {
export function extractEditorsDropData(e: DragEvent): Array<IDraggedResourceEditorInput> {
const editors: IDraggedResourceEditorInput[] = [];
if (e.dataTransfer && e.dataTransfer.types.length > 0) {

// Check for window-to-window DND
if (!externalOnly) {

// Data Transfer: Code Editors
const rawEditorsData = e.dataTransfer.getData(CodeDataTransfers.EDITORS);
if (rawEditorsData) {
try {
editors.push(...parse(rawEditorsData));
} catch (error) {
// Invalid transfer
}
// Data Transfer: Code Editors
const rawEditorsData = e.dataTransfer.getData(CodeDataTransfers.EDITORS);
if (rawEditorsData) {
try {
editors.push(...parse(rawEditorsData));
} catch (error) {
// Invalid transfer
}
}

// Data Transfer: Resources
else {
try {
const rawResourcesData = e.dataTransfer.getData(DataTransfers.RESOURCES);
if (rawResourcesData) {
const resourcesRaw: string[] = JSON.parse(rawResourcesData);
for (const resourceRaw of resourcesRaw) {
if (resourceRaw.indexOf(':') > 0) { // mitigate https://github.com/microsoft/vscode/issues/124946
editors.push({ resource: URI.parse(resourceRaw) });
}
// Data Transfer: Resources
else {
try {
const rawResourcesData = e.dataTransfer.getData(DataTransfers.RESOURCES);
if (rawResourcesData) {
const resourcesRaw: string[] = JSON.parse(rawResourcesData);
for (const resourceRaw of resourcesRaw) {
if (resourceRaw.indexOf(':') > 0) { // mitigate https://github.com/microsoft/vscode/issues/124946
editors.push({ resource: URI.parse(resourceRaw) });
}
}
} catch (error) {
// Invalid transfer
}
} catch (error) {
// Invalid transfer
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/vs/workbench/contrib/files/browser/fileImportExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ export class BrowserFileUpload {

//#endregion

//#region Native File Import (drag and drop)
//#region External File Import (drag and drop)

export class NativeFileImport {
export class ExternalFileImport {

constructor(
@IFileService private readonly fileService: IFileService,
Expand Down Expand Up @@ -417,8 +417,12 @@ export class NativeFileImport {

private async doImport(target: ExplorerItem, source: DragEvent, token: CancellationToken): Promise<void> {

// Activate all providers for the resources dropped
const candidateFiles = coalesce(extractEditorsDropData(source).map(editor => editor.resource));
await Promise.all(candidateFiles.map(resource => this.fileService.activateProvider(resource.scheme)));

// Check for dropped external files to be folders
const files = coalesce(extractEditorsDropData(source, true).filter(editor => URI.isUri(editor.resource) && this.fileService.canHandleResource(editor.resource)).map(editor => editor.resource));
const files = coalesce(candidateFiles.filter(resource => this.fileService.canHandleResource(resource)));
const resolvedFiles = await this.fileService.resolveAll(files.map(file => ({ resource: file })));

if (token.isCancellationRequested) {
Expand Down
19 changes: 12 additions & 7 deletions src/vs/workbench/contrib/files/browser/views/explorerViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
import { ResourceFileEdit } from 'vs/editor/browser/services/bulkEditService';
import { IExplorerService } from 'vs/workbench/contrib/files/browser/files';
import { BrowserFileUpload, NativeFileImport, getMultipleFilesOverwriteConfirm } from 'vs/workbench/contrib/files/browser/fileImportExport';
import { BrowserFileUpload, ExternalFileImport, getMultipleFilesOverwriteConfirm } from 'vs/workbench/contrib/files/browser/fileImportExport';
import { toErrorMessage } from 'vs/base/common/errorMessage';

export class ExplorerDelegate implements IListVirtualDelegate<ExplorerItem> {
Expand Down Expand Up @@ -839,7 +839,7 @@ export class FileDragAndDrop implements ITreeDragAndDrop<ExplorerItem> {
if (!containsDragType(originalEvent, DataTransfers.FILES, CodeDataTransfers.FILES, DataTransfers.RESOURCES)) {
return false;
}
if (isWeb && originalEvent.dataTransfer?.types.indexOf('Files') === -1) {
if (isWeb && !containsDragType(originalEvent, 'Files')) {
// DnD from vscode to web is not supported #115535. Only if we are dragging from native finder / explorer then the "Files" data transfer will be set
return false;
}
Expand Down Expand Up @@ -980,14 +980,19 @@ export class FileDragAndDrop implements ITreeDragAndDrop<ExplorerItem> {

try {

// Desktop DND (Import file)
// External file DND (Import/Upload file)
if (data instanceof NativeDragAndDropData) {
if (isWeb) {
// Native OS file DND into Web
if (containsDragType(originalEvent, 'Files') && isWeb) {
const browserUpload = this.instantiationService.createInstance(BrowserFileUpload);
await browserUpload.upload(target, originalEvent);
} else {
const nativeImport = this.instantiationService.createInstance(NativeFileImport);
await nativeImport.import(resolvedTarget, originalEvent);
}
// 2 Cases handled for import:
// FS-Provided file DND into Web/Desktop
// Native OS file DND into Desktop
else {
const fileImport = this.instantiationService.createInstance(ExternalFileImport);
await fileImport.import(resolvedTarget, originalEvent);
}
}

Expand Down