dnd.ts 7.6 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

R
rebornix 已提交
8
import 'vs/css!./dnd';
9
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
10
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
11
import { isMacintosh } from 'vs/base/common/platform';
12
import { KeyCode } from 'vs/base/common/keyCodes';
A
Alex Dima 已提交
13
import { ICodeEditor, IEditorMouseEvent, IMouseTarget, MouseTargetType } from 'vs/editor/browser/editorBrowser';
14
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
15 16
import * as editorCommon from 'vs/editor/common/editorCommon';
import { Position } from 'vs/editor/common/core/position';
R
rebornix 已提交
17
import { Range } from 'vs/editor/common/core/range';
18
import { Selection } from 'vs/editor/common/core/selection';
19
import { DragAndDropCommand } from 'vs/editor/contrib/dnd/dragAndDropCommand';
A
Alex Dima 已提交
20
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
A
Alex Dima 已提交
21
import { IModelDeltaDecoration } from 'vs/editor/common/model';
A
Alex Dima 已提交
22 23 24 25 26 27 28 29 30
import { IMouseEvent } from 'vs/base/browser/mouseEvent';

function hasTriggerModifier(e: IKeyboardEvent | IMouseEvent): boolean {
	if (isMacintosh) {
		return e.altKey;
	} else {
		return e.ctrlKey;
	}
}
31 32 33

export class DragAndDropController implements editorCommon.IEditorContribution {

34
	private static readonly ID = 'editor.contrib.dragAndDrop';
35 36 37

	private _editor: ICodeEditor;
	private _toUnhook: IDisposable[];
38
	private _dragSelection: Selection;
R
rebornix 已提交
39
	private _dndDecorationIds: string[];
40
	private _mouseDown: boolean;
41
	private _modiferPressed: boolean;
42
	static TRIGGER_KEY_VALUE = isMacintosh ? KeyCode.Alt : KeyCode.Ctrl;
43

44
	static get(editor: ICodeEditor): DragAndDropController {
45 46 47 48 49 50
		return editor.getContribution<DragAndDropController>(DragAndDropController.ID);
	}

	constructor(editor: ICodeEditor) {
		this._editor = editor;
		this._toUnhook = [];
51 52
		this._toUnhook.push(this._editor.onMouseDown((e: IEditorMouseEvent) => this._onEditorMouseDown(e)));
		this._toUnhook.push(this._editor.onMouseUp((e: IEditorMouseEvent) => this._onEditorMouseUp(e)));
53
		this._toUnhook.push(this._editor.onMouseDrag((e: IEditorMouseEvent) => this._onEditorMouseDrag(e)));
54
		this._toUnhook.push(this._editor.onMouseDrop((e: IEditorMouseEvent) => this._onEditorMouseDrop(e)));
55 56
		this._toUnhook.push(this._editor.onKeyDown((e: IKeyboardEvent) => this.onEditorKeyDown(e)));
		this._toUnhook.push(this._editor.onKeyUp((e: IKeyboardEvent) => this.onEditorKeyUp(e)));
R
rebornix 已提交
57
		this._dndDecorationIds = [];
58
		this._mouseDown = false;
59
		this._modiferPressed = false;
60
		this._dragSelection = null;
61 62
	}

63
	private onEditorKeyDown(e: IKeyboardEvent): void {
64 65 66 67
		if (!this._editor.getConfiguration().dragAndDrop) {
			return;
		}

A
Alex Dima 已提交
68
		if (hasTriggerModifier(e)) {
69 70 71
			this._modiferPressed = true;
		}

A
Alex Dima 已提交
72
		if (this._mouseDown && hasTriggerModifier(e)) {
73 74 75
			this._editor.updateOptions({
				mouseStyle: 'copy'
			});
R
rebornix 已提交
76
		}
77
	}
R
rebornix 已提交
78

79
	private onEditorKeyUp(e: IKeyboardEvent): void {
80 81 82 83
		if (!this._editor.getConfiguration().dragAndDrop) {
			return;
		}

A
Alex Dima 已提交
84
		if (hasTriggerModifier(e)) {
85 86 87
			this._modiferPressed = false;
		}

88 89 90 91
		if (this._mouseDown && e.keyCode === DragAndDropController.TRIGGER_KEY_VALUE) {
			this._editor.updateOptions({
				mouseStyle: 'default'
			});
R
rebornix 已提交
92
		}
93
	}
R
rebornix 已提交
94

95 96 97 98 99 100
	private _onEditorMouseDown(mouseEvent: IEditorMouseEvent): void {
		this._mouseDown = true;
	}

	private _onEditorMouseUp(mouseEvent: IEditorMouseEvent): void {
		this._mouseDown = false;
R
rebornix 已提交
101 102 103 104
		// Whenever users release the mouse, the drag and drop operation should finish and the cursor should revert to text.
		this._editor.updateOptions({
			mouseStyle: 'text'
		});
R
rebornix 已提交
105 106
	}

107 108
	private _onEditorMouseDrag(mouseEvent: IEditorMouseEvent): void {
		let target = mouseEvent.target;
109

110
		if (this._dragSelection === null) {
111 112 113
			let possibleSelections = this._editor.getSelections().filter(selection => selection.containsPosition(target.position));
			if (possibleSelections.length === 1) {
				this._dragSelection = possibleSelections[0];
114 115
			} else {
				return;
116 117
			}
		}
118

A
Alex Dima 已提交
119
		if (hasTriggerModifier(mouseEvent.event)) {
R
rebornix 已提交
120 121 122 123 124 125 126 127 128
			this._editor.updateOptions({
				mouseStyle: 'copy'
			});
		} else {
			this._editor.updateOptions({
				mouseStyle: 'default'
			});
		}

129 130 131 132 133
		if (this._dragSelection.containsPosition(target.position)) {
			this._removeDecoration();
		} else {
			this.showAt(target.position);
		}
134 135 136
	}

	private _onEditorMouseDrop(mouseEvent: IEditorMouseEvent): void {
137
		if (mouseEvent.target && (this._hitContent(mouseEvent.target) || this._hitMargin(mouseEvent.target)) && mouseEvent.target.position) {
R
rebornix 已提交
138
			let newCursorPosition = new Position(mouseEvent.target.position.lineNumber, mouseEvent.target.position.column);
139

140
			if (this._dragSelection === null) {
141 142 143 144 145 146 147 148 149 150 151 152 153 154
				if (mouseEvent.event.shiftKey) {
					let primarySelection = this._editor.getSelection();
					let { startLineNumber, startColumn } = primarySelection;
					this._editor.setSelections([new Selection(startLineNumber, startColumn, newCursorPosition.lineNumber, newCursorPosition.column)]);
				} else {
					let newSelections = this._editor.getSelections().map(selection => {
						if (selection.containsPosition(newCursorPosition)) {
							return new Selection(newCursorPosition.lineNumber, newCursorPosition.column, newCursorPosition.lineNumber, newCursorPosition.column);
						} else {
							return selection;
						}
					});
					this._editor.setSelections(newSelections);
				}
155 156 157
			} else if (!this._dragSelection.containsPosition(newCursorPosition) ||
				(
					(
A
Alex Dima 已提交
158
						hasTriggerModifier(mouseEvent.event) ||
159 160 161 162 163
						this._modiferPressed
					) && (
						this._dragSelection.getEndPosition().equals(newCursorPosition) || this._dragSelection.getStartPosition().equals(newCursorPosition)
					) // we allow users to paste content beside the selection
				)) {
A
Alex Dima 已提交
164
				this._editor.pushUndoStop();
A
Alex Dima 已提交
165
				this._editor.executeCommand(DragAndDropController.ID, new DragAndDropCommand(this._dragSelection, newCursorPosition, hasTriggerModifier(mouseEvent.event) || this._modiferPressed));
A
Alex Dima 已提交
166
				this._editor.pushUndoStop();
167
			}
168 169
		}

R
rebornix 已提交
170 171 172 173
		this._editor.updateOptions({
			mouseStyle: 'text'
		});

R
rebornix 已提交
174
		this._removeDecoration();
175
		this._dragSelection = null;
176
		this._mouseDown = false;
177 178
	}

179
	private static readonly _DECORATION_OPTIONS = ModelDecorationOptions.register({
180 181 182
		className: 'dnd-target'
	});

R
rebornix 已提交
183
	public showAt(position: Position): void {
A
Alex Dima 已提交
184 185 186 187
		let newDecorations: IModelDeltaDecoration[] = [{
			range: new Range(position.lineNumber, position.column, position.lineNumber, position.column),
			options: DragAndDropController._DECORATION_OPTIONS
		}];
R
rebornix 已提交
188

A
Alex Dima 已提交
189
		this._dndDecorationIds = this._editor.deltaDecorations(this._dndDecorationIds, newDecorations);
190
		this._editor.revealPosition(position, editorCommon.ScrollType.Immediate);
R
rebornix 已提交
191 192 193
	}

	private _removeDecoration(): void {
A
Alex Dima 已提交
194
		this._dndDecorationIds = this._editor.deltaDecorations(this._dndDecorationIds, []);
195 196
	}

197
	private _hitContent(target: IMouseTarget): boolean {
A
Alex Dima 已提交
198 199
		return target.type === MouseTargetType.CONTENT_TEXT ||
			target.type === MouseTargetType.CONTENT_EMPTY;
200 201 202
	}

	private _hitMargin(target: IMouseTarget): boolean {
A
Alex Dima 已提交
203 204 205
		return target.type === MouseTargetType.GUTTER_GLYPH_MARGIN ||
			target.type === MouseTargetType.GUTTER_LINE_NUMBERS ||
			target.type === MouseTargetType.GUTTER_LINE_DECORATIONS;
206 207
	}

208 209 210 211 212
	public getId(): string {
		return DragAndDropController.ID;
	}

	public dispose(): void {
R
rebornix 已提交
213
		this._removeDecoration();
214 215
		this._dragSelection = null;
		this._mouseDown = false;
216
		this._modiferPressed = false;
217 218
		this._toUnhook = dispose(this._toUnhook);
	}
219 220 221
}

registerEditorContribution(DragAndDropController);