Path: blob/main/src/vs/editor/contrib/dnd/browser/dragAndDropCommand.ts
4779 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Position } from '../../../common/core/position.js';6import { Range } from '../../../common/core/range.js';7import { Selection } from '../../../common/core/selection.js';8import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from '../../../common/editorCommon.js';9import { ITextModel } from '../../../common/model.js';101112export class DragAndDropCommand implements ICommand {1314private readonly selection: Selection;15private readonly targetPosition: Position;16private targetSelection: Selection | null;17private readonly copy: boolean;1819constructor(selection: Selection, targetPosition: Position, copy: boolean) {20this.selection = selection;21this.targetPosition = targetPosition;22this.copy = copy;23this.targetSelection = null;24}2526public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {27const text = model.getValueInRange(this.selection);28if (!this.copy) {29builder.addEditOperation(this.selection, null);30}31builder.addEditOperation(new Range(this.targetPosition.lineNumber, this.targetPosition.column, this.targetPosition.lineNumber, this.targetPosition.column), text);3233if (this.selection.containsPosition(this.targetPosition) && !(34this.copy && (35this.selection.getEndPosition().equals(this.targetPosition) || this.selection.getStartPosition().equals(this.targetPosition)36) // we allow users to paste content beside the selection37)) {38this.targetSelection = this.selection;39return;40}4142if (this.copy) {43this.targetSelection = new Selection(44this.targetPosition.lineNumber,45this.targetPosition.column,46this.selection.endLineNumber - this.selection.startLineNumber + this.targetPosition.lineNumber,47this.selection.startLineNumber === this.selection.endLineNumber ?48this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :49this.selection.endColumn50);51return;52}5354if (this.targetPosition.lineNumber > this.selection.endLineNumber) {55// Drag the selection downwards56this.targetSelection = new Selection(57this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber,58this.targetPosition.column,59this.targetPosition.lineNumber,60this.selection.startLineNumber === this.selection.endLineNumber ?61this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :62this.selection.endColumn63);64return;65}6667if (this.targetPosition.lineNumber < this.selection.endLineNumber) {68// Drag the selection upwards69this.targetSelection = new Selection(70this.targetPosition.lineNumber,71this.targetPosition.column,72this.targetPosition.lineNumber + this.selection.endLineNumber - this.selection.startLineNumber,73this.selection.startLineNumber === this.selection.endLineNumber ?74this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :75this.selection.endColumn76);77return;78}7980// The target position is at the same line as the selection's end position.81if (this.selection.endColumn <= this.targetPosition.column) {82// The target position is after the selection's end position83this.targetSelection = new Selection(84this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber,85this.selection.startLineNumber === this.selection.endLineNumber ?86this.targetPosition.column - this.selection.endColumn + this.selection.startColumn :87this.targetPosition.column - this.selection.endColumn + this.selection.startColumn,88this.targetPosition.lineNumber,89this.selection.startLineNumber === this.selection.endLineNumber ?90this.targetPosition.column :91this.selection.endColumn92);93} else {94// 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.95this.targetSelection = new Selection(96this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber,97this.targetPosition.column,98this.targetPosition.lineNumber,99this.targetPosition.column + this.selection.endColumn - this.selection.startColumn100);101}102}103104public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {105return this.targetSelection!;106}107}108109110