Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/dnd/browser/dragAndDropCommand.ts
4779 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Position } from '../../../common/core/position.js';
7
import { Range } from '../../../common/core/range.js';
8
import { Selection } from '../../../common/core/selection.js';
9
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../../../common/editorCommon.js';
10
import { ITextModel } from '../../../common/model.js';
11
12
13
export class DragAndDropCommand implements ICommand {
14
15
private readonly selection: Selection;
16
private readonly targetPosition: Position;
17
private targetSelection: Selection | null;
18
private readonly copy: boolean;
19
20
constructor(selection: Selection, targetPosition: Position, copy: boolean) {
21
this.selection = selection;
22
this.targetPosition = targetPosition;
23
this.copy = copy;
24
this.targetSelection = null;
25
}
26
27
public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {
28
const text = model.getValueInRange(this.selection);
29
if (!this.copy) {
30
builder.addEditOperation(this.selection, null);
31
}
32
builder.addEditOperation(new Range(this.targetPosition.lineNumber, this.targetPosition.column, this.targetPosition.lineNumber, this.targetPosition.column), text);
33
34
if (this.selection.containsPosition(this.targetPosition) && !(
35
this.copy && (
36
this.selection.getEndPosition().equals(this.targetPosition) || this.selection.getStartPosition().equals(this.targetPosition)
37
) // we allow users to paste content beside the selection
38
)) {
39
this.targetSelection = this.selection;
40
return;
41
}
42
43
if (this.copy) {
44
this.targetSelection = new Selection(
45
this.targetPosition.lineNumber,
46
this.targetPosition.column,
47
this.selection.endLineNumber - this.selection.startLineNumber + this.targetPosition.lineNumber,
48
this.selection.startLineNumber === this.selection.endLineNumber ?
49
this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :
50
this.selection.endColumn
51
);
52
return;
53
}
54
55
if (this.targetPosition.lineNumber > this.selection.endLineNumber) {
56
// Drag the selection downwards
57
this.targetSelection = new Selection(
58
this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber,
59
this.targetPosition.column,
60
this.targetPosition.lineNumber,
61
this.selection.startLineNumber === this.selection.endLineNumber ?
62
this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :
63
this.selection.endColumn
64
);
65
return;
66
}
67
68
if (this.targetPosition.lineNumber < this.selection.endLineNumber) {
69
// Drag the selection upwards
70
this.targetSelection = new Selection(
71
this.targetPosition.lineNumber,
72
this.targetPosition.column,
73
this.targetPosition.lineNumber + this.selection.endLineNumber - this.selection.startLineNumber,
74
this.selection.startLineNumber === this.selection.endLineNumber ?
75
this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :
76
this.selection.endColumn
77
);
78
return;
79
}
80
81
// The target position is at the same line as the selection's end position.
82
if (this.selection.endColumn <= this.targetPosition.column) {
83
// The target position is after the selection's end position
84
this.targetSelection = new Selection(
85
this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber,
86
this.selection.startLineNumber === this.selection.endLineNumber ?
87
this.targetPosition.column - this.selection.endColumn + this.selection.startColumn :
88
this.targetPosition.column - this.selection.endColumn + this.selection.startColumn,
89
this.targetPosition.lineNumber,
90
this.selection.startLineNumber === this.selection.endLineNumber ?
91
this.targetPosition.column :
92
this.selection.endColumn
93
);
94
} else {
95
// The target position is before the selection's end position. Since the selection doesn't contain the target position, the selection is one-line and target position is before this selection.
96
this.targetSelection = new Selection(
97
this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber,
98
this.targetPosition.column,
99
this.targetPosition.lineNumber,
100
this.targetPosition.column + this.selection.endColumn - this.selection.startColumn
101
);
102
}
103
}
104
105
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {
106
return this.targetSelection!;
107
}
108
}
109
110