editorAreaDropHandler.ts 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import { IDraggedResource, IDraggedEditor, extractResources } from 'vs/workbench/browser/editor';
import { WORKSPACE_EXTENSION, IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
import { extname } from 'vs/base/common/paths';
import { IFileService } from 'vs/platform/files/common/files';
import { IWindowsService, IWindowService } from 'vs/platform/windows/common/windows';
import URI from 'vs/base/common/uri';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
15
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
16 17 18 19 20 21 22
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
import { TPromise } from 'vs/base/common/winjs.base';
import { Schemas } from 'vs/base/common/network';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { Position } from 'vs/platform/editor/common/editor';
import { onUnexpectedError } from 'vs/base/common/errors';
23 24
import { DefaultEndOfLine } from 'vs/editor/common/model';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

/**
 * Shared function across some editor components to handle drag & drop of external resources. E.g. of folders and workspace files
 * to open them in the window instead of the editor or to handle dirty editors being dropped between instances of Code.
 */
export class EditorAreaDropHandler {

	constructor(
		@IFileService private fileService: IFileService,
		@IWindowsService private windowsService: IWindowsService,
		@IWindowService private windowService: IWindowService,
		@IWorkspacesService private workspacesService: IWorkspacesService,
		@ITextFileService private textFileService: ITextFileService,
		@IBackupFileService private backupFileService: IBackupFileService,
		@IEditorGroupService private groupService: IEditorGroupService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
42
		@IConfigurationService private configurationService: IConfigurationService
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	) {
	}

	public handleDrop(event: DragEvent, afterDrop: () => void, targetPosition: Position, targetIndex?: number): void {
		const resources = extractResources(event).filter(r => r.resource.scheme === Schemas.file || r.resource.scheme === Schemas.untitled);
		if (!resources.length) {
			return;
		}

		return this.doHandleDrop(resources).then(isWorkspaceOpening => {
			if (isWorkspaceOpening) {
				return void 0; // return early if the drop operation resulted in this window changing to a workspace
			}

			// Add external ones to recently open list unless dropped resource is a workspace
			const externalResources = resources.filter(d => d.isExternal).map(d => d.resource);
			if (externalResources.length) {
				this.windowsService.addRecentlyOpened(externalResources.map(resource => resource.fsPath));
			}

			// Open in Editor
			return this.windowService.focusWindow()
				.then(() => this.editorService.openEditors(resources.map(r => {
					return {
						input: {
							resource: r.resource,
							options: {
								pinned: true,
								index: targetIndex,
								viewState: (r as IDraggedEditor).viewState
							}
						},
						position: targetPosition
					};
				}))).then(() => {

					// Finish with provided function
					afterDrop();
				});
		}).done(null, onUnexpectedError);
	}

	private doHandleDrop(resources: (IDraggedResource | IDraggedEditor)[]): TPromise<boolean> {

		// Check for dirty editor being dropped
		if (resources.length === 1 && !resources[0].isExternal && (resources[0] as IDraggedEditor).backupResource) {
			return this.handleDirtyEditorDrop(resources[0]);
		}

		// Check for workspace file being dropped
		if (resources.some(r => r.isExternal)) {
			return this.handleWorkspaceFileDrop(resources);
		}

		return TPromise.as(false);
	}

	private handleDirtyEditorDrop(droppedDirtyEditor: IDraggedEditor): TPromise<boolean> {

		// Untitled: always ensure that we open a new untitled for each file we drop
		if (droppedDirtyEditor.resource.scheme === Schemas.untitled) {
			droppedDirtyEditor.resource = this.untitledEditorService.createOrGet().getResource();
		}

		// Return early if the resource is already dirty in target or opened already
		if (this.textFileService.isDirty(droppedDirtyEditor.resource) || this.groupService.getStacksModel().isOpen(droppedDirtyEditor.resource)) {
			return TPromise.as(false);
		}

		// Resolve the contents of the dropped dirty resource from source
113
		return this.backupFileService.resolveBackupContent(droppedDirtyEditor.backupResource).then(content => {
114 115

			// Set the contents of to the resource to the target
116
			return this.backupFileService.backupResource(droppedDirtyEditor.resource, content.create(this.getDefaultEOL()).createSnapshot(true));
117 118 119
		}).then(() => false, () => false /* ignore any error */);
	}

120 121 122 123 124 125 126 127 128
	private getDefaultEOL(): DefaultEndOfLine {
		const eol = this.configurationService.getValue('files.eol');
		if (eol === '\r\n') {
			return DefaultEndOfLine.CRLF;
		}

		return DefaultEndOfLine.LF;
	}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	private handleWorkspaceFileDrop(resources: (IDraggedResource | IDraggedEditor)[]): TPromise<boolean> {
		const externalResources = resources.filter(d => d.isExternal).map(d => d.resource);

		const externalWorkspaceResources: { workspaces: URI[], folders: URI[] } = {
			workspaces: [],
			folders: []
		};

		return TPromise.join(externalResources.map(resource => {

			// Check for Workspace
			if (extname(resource.fsPath) === `.${WORKSPACE_EXTENSION}`) {
				externalWorkspaceResources.workspaces.push(resource);

				return void 0;
			}

			// Check for Folder
			return this.fileService.resolveFile(resource).then(stat => {
				if (stat.isDirectory) {
					externalWorkspaceResources.folders.push(stat.resource);
				}
			}, error => void 0);
		})).then(_ => {
			const { workspaces, folders } = externalWorkspaceResources;

			// Return early if no external resource is a folder or workspace
			if (workspaces.length === 0 && folders.length === 0) {
				return false;
			}

			// Pass focus to window
			this.windowService.focusWindow();

			let workspacesToOpen: TPromise<string[]>;

			// Open in separate windows if we drop workspaces or just one folder
			if (workspaces.length > 0 || folders.length === 1) {
				workspacesToOpen = TPromise.as([...workspaces, ...folders].map(resources => resources.fsPath));
			}

			// Multiple folders: Create new workspace with folders and open
			else if (folders.length > 1) {
				workspacesToOpen = this.workspacesService.createWorkspace(folders.map(folder => ({ uri: folder }))).then(workspace => [workspace.configPath]);
			}

			// Open
			workspacesToOpen.then(workspaces => {
				this.windowsService.openWindow(workspaces, { forceReuseWindow: true });
			});

			return true;
		});
	}
}