dnd.ts 8.2 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
	private readonly _editor: ICodeEditor;
36
	private _toUnhook: IDisposable[];
M
Matt Bierner 已提交
37
	private _dragSelection: Selection | null;
R
rebornix 已提交
38
	private _dndDecorationIds: string[];
39
	private _mouseDown: boolean;
K
khaled4vokalz 已提交
40
	private _modifierPressed: 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;
K
khaled4vokalz 已提交
59
		this._modifierPressed = false;
60
		this._dragSelection = null;
61 62
	}

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

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

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

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)) {
K
khaled4vokalz 已提交
92
			this._modifierPressed = false;
93 94
		}

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) {
M
Matt Bierner 已提交
118 119
			const selections = this._editor.getSelections() || [];
			let possibleSelections = selections.filter(selection => target.position && selection.containsPosition(target.position));
120 121
			if (possibleSelections.length === 1) {
				this._dragSelection = possibleSelections[0];
122 123
			} else {
				return;
124 125
			}
		}
126

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

M
Matt Bierner 已提交
137 138 139 140 141 142
		if (target.position) {
			if (this._dragSelection.containsPosition(target.position)) {
				this._removeDecoration();
			} else {
				this.showAt(target.position);
			}
143
		}
144 145 146
	}

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

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

R
rebornix 已提交
184 185 186 187
		this._editor.updateOptions({
			mouseStyle: 'text'
		});

R
rebornix 已提交
188
		this._removeDecoration();
189
		this._dragSelection = null;
190
		this._mouseDown = false;
191 192
	}

193
	private static readonly _DECORATION_OPTIONS = ModelDecorationOptions.register({
194 195 196
		className: 'dnd-target'
	});

R
rebornix 已提交
197
	public showAt(position: Position): void {
A
Alex Dima 已提交
198 199 200 201
		let newDecorations: IModelDeltaDecoration[] = [{
			range: new Range(position.lineNumber, position.column, position.lineNumber, position.column),
			options: DragAndDropController._DECORATION_OPTIONS
		}];
R
rebornix 已提交
202

A
Alex Dima 已提交
203
		this._dndDecorationIds = this._editor.deltaDecorations(this._dndDecorationIds, newDecorations);
204
		this._editor.revealPosition(position, editorCommon.ScrollType.Immediate);
R
rebornix 已提交
205 206 207
	}

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

211
	private _hitContent(target: IMouseTarget): boolean {
A
Alex Dima 已提交
212 213
		return target.type === MouseTargetType.CONTENT_TEXT ||
			target.type === MouseTargetType.CONTENT_EMPTY;
214 215 216
	}

	private _hitMargin(target: IMouseTarget): boolean {
A
Alex Dima 已提交
217 218 219
		return target.type === MouseTargetType.GUTTER_GLYPH_MARGIN ||
			target.type === MouseTargetType.GUTTER_LINE_NUMBERS ||
			target.type === MouseTargetType.GUTTER_LINE_DECORATIONS;
220 221
	}

222 223 224 225 226
	public getId(): string {
		return DragAndDropController.ID;
	}

	public dispose(): void {
R
rebornix 已提交
227
		this._removeDecoration();
228 229
		this._dragSelection = null;
		this._mouseDown = false;
K
khaled4vokalz 已提交
230
		this._modifierPressed = false;
231 232
		this._toUnhook = dispose(this._toUnhook);
	}
233 234 235
}

registerEditorContribution(DragAndDropController);