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

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

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

export class DragAndDropController implements editorCommon.IEditorContribution {

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

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

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

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

R
rebornix 已提交
63 64 65 66 67 68 69
	private onEditorBlur() {
		this._removeDecoration();
		this._dragSelection = null;
		this._mouseDown = false;
		this._modiferPressed = false;
	}

70
	private onEditorKeyDown(e: IKeyboardEvent): void {
71 72 73 74
		if (!this._editor.getConfiguration().dragAndDrop) {
			return;
		}

A
Alex Dima 已提交
75
		if (hasTriggerModifier(e)) {
76 77 78
			this._modiferPressed = true;
		}

A
Alex Dima 已提交
79
		if (this._mouseDown && hasTriggerModifier(e)) {
80 81 82
			this._editor.updateOptions({
				mouseStyle: 'copy'
			});
R
rebornix 已提交
83
		}
84
	}
R
rebornix 已提交
85

86
	private onEditorKeyUp(e: IKeyboardEvent): void {
87 88 89 90
		if (!this._editor.getConfiguration().dragAndDrop) {
			return;
		}

A
Alex Dima 已提交
91
		if (hasTriggerModifier(e)) {
92 93 94
			this._modiferPressed = false;
		}

95 96 97 98
		if (this._mouseDown && e.keyCode === DragAndDropController.TRIGGER_KEY_VALUE) {
			this._editor.updateOptions({
				mouseStyle: 'default'
			});
R
rebornix 已提交
99
		}
100
	}
R
rebornix 已提交
101

102 103 104 105 106 107
	private _onEditorMouseDown(mouseEvent: IEditorMouseEvent): void {
		this._mouseDown = true;
	}

	private _onEditorMouseUp(mouseEvent: IEditorMouseEvent): void {
		this._mouseDown = false;
R
rebornix 已提交
108 109 110 111
		// 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 已提交
112 113
	}

114 115
	private _onEditorMouseDrag(mouseEvent: IEditorMouseEvent): void {
		let target = mouseEvent.target;
116

117
		if (this._dragSelection === null) {
118 119 120
			let possibleSelections = this._editor.getSelections().filter(selection => selection.containsPosition(target.position));
			if (possibleSelections.length === 1) {
				this._dragSelection = possibleSelections[0];
121 122
			} else {
				return;
123 124
			}
		}
125

A
Alex Dima 已提交
126
		if (hasTriggerModifier(mouseEvent.event)) {
R
rebornix 已提交
127 128 129 130 131 132 133 134 135
			this._editor.updateOptions({
				mouseStyle: 'copy'
			});
		} else {
			this._editor.updateOptions({
				mouseStyle: 'default'
			});
		}

136 137 138 139 140
		if (this._dragSelection.containsPosition(target.position)) {
			this._removeDecoration();
		} else {
			this.showAt(target.position);
		}
141 142 143
	}

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

147
			if (this._dragSelection === null) {
A
Alex Dima 已提交
148
				let newSelections: Selection[] = null;
149 150
				if (mouseEvent.event.shiftKey) {
					let primarySelection = this._editor.getSelection();
151
					let { selectionStartLineNumber, selectionStartColumn } = primarySelection;
A
Alex Dima 已提交
152
					newSelections = [new Selection(selectionStartLineNumber, selectionStartColumn, newCursorPosition.lineNumber, newCursorPosition.column)];
153
				} else {
A
Alex Dima 已提交
154
					newSelections = this._editor.getSelections().map(selection => {
155 156 157 158 159 160 161
						if (selection.containsPosition(newCursorPosition)) {
							return new Selection(newCursorPosition.lineNumber, newCursorPosition.column, newCursorPosition.lineNumber, newCursorPosition.column);
						} else {
							return selection;
						}
					});
				}
A
Alex Dima 已提交
162 163
				// Use `mouse` as the source instead of `api`.
				(<CodeEditorWidget>this._editor).setSelections(newSelections, 'mouse');
164 165 166
			} else if (!this._dragSelection.containsPosition(newCursorPosition) ||
				(
					(
A
Alex Dima 已提交
167
						hasTriggerModifier(mouseEvent.event) ||
168 169 170 171 172
						this._modiferPressed
					) && (
						this._dragSelection.getEndPosition().equals(newCursorPosition) || this._dragSelection.getStartPosition().equals(newCursorPosition)
					) // we allow users to paste content beside the selection
				)) {
A
Alex Dima 已提交
173
				this._editor.pushUndoStop();
A
Alex Dima 已提交
174
				this._editor.executeCommand(DragAndDropController.ID, new DragAndDropCommand(this._dragSelection, newCursorPosition, hasTriggerModifier(mouseEvent.event) || this._modiferPressed));
A
Alex Dima 已提交
175
				this._editor.pushUndoStop();
176
			}
177 178
		}

R
rebornix 已提交
179 180 181 182
		this._editor.updateOptions({
			mouseStyle: 'text'
		});

R
rebornix 已提交
183
		this._removeDecoration();
184
		this._dragSelection = null;
185
		this._mouseDown = false;
186 187
	}

188
	private static readonly _DECORATION_OPTIONS = ModelDecorationOptions.register({
189 190 191
		className: 'dnd-target'
	});

R
rebornix 已提交
192
	public showAt(position: Position): void {
A
Alex Dima 已提交
193 194 195 196
		let newDecorations: IModelDeltaDecoration[] = [{
			range: new Range(position.lineNumber, position.column, position.lineNumber, position.column),
			options: DragAndDropController._DECORATION_OPTIONS
		}];
R
rebornix 已提交
197

A
Alex Dima 已提交
198
		this._dndDecorationIds = this._editor.deltaDecorations(this._dndDecorationIds, newDecorations);
199
		this._editor.revealPosition(position, editorCommon.ScrollType.Immediate);
R
rebornix 已提交
200 201 202
	}

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

206
	private _hitContent(target: IMouseTarget): boolean {
A
Alex Dima 已提交
207 208
		return target.type === MouseTargetType.CONTENT_TEXT ||
			target.type === MouseTargetType.CONTENT_EMPTY;
209 210 211
	}

	private _hitMargin(target: IMouseTarget): boolean {
A
Alex Dima 已提交
212 213 214
		return target.type === MouseTargetType.GUTTER_GLYPH_MARGIN ||
			target.type === MouseTargetType.GUTTER_LINE_NUMBERS ||
			target.type === MouseTargetType.GUTTER_LINE_DECORATIONS;
215 216
	}

217 218 219 220 221
	public getId(): string {
		return DragAndDropController.ID;
	}

	public dispose(): void {
R
rebornix 已提交
222
		this._removeDecoration();
223 224
		this._dragSelection = null;
		this._mouseDown = false;
225
		this._modiferPressed = false;
226 227
		this._toUnhook = dispose(this._toUnhook);
	}
228 229 230
}

registerEditorContribution(DragAndDropController);