dragAndDropCommand.js 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. import { Range } from '../../common/core/range.js';
  6. import { Selection } from '../../common/core/selection.js';
  7. export class DragAndDropCommand {
  8. constructor(selection, targetPosition, copy) {
  9. this.selection = selection;
  10. this.targetPosition = targetPosition;
  11. this.copy = copy;
  12. this.targetSelection = null;
  13. }
  14. getEditOperations(model, builder) {
  15. let text = model.getValueInRange(this.selection);
  16. if (!this.copy) {
  17. builder.addEditOperation(this.selection, null);
  18. }
  19. builder.addEditOperation(new Range(this.targetPosition.lineNumber, this.targetPosition.column, this.targetPosition.lineNumber, this.targetPosition.column), text);
  20. if (this.selection.containsPosition(this.targetPosition) && !(this.copy && (this.selection.getEndPosition().equals(this.targetPosition) || this.selection.getStartPosition().equals(this.targetPosition)) // we allow users to paste content beside the selection
  21. )) {
  22. this.targetSelection = this.selection;
  23. return;
  24. }
  25. if (this.copy) {
  26. this.targetSelection = new Selection(this.targetPosition.lineNumber, this.targetPosition.column, this.selection.endLineNumber - this.selection.startLineNumber + this.targetPosition.lineNumber, this.selection.startLineNumber === this.selection.endLineNumber ?
  27. this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :
  28. this.selection.endColumn);
  29. return;
  30. }
  31. if (this.targetPosition.lineNumber > this.selection.endLineNumber) {
  32. // Drag the selection downwards
  33. this.targetSelection = new Selection(this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber, this.targetPosition.column, this.targetPosition.lineNumber, this.selection.startLineNumber === this.selection.endLineNumber ?
  34. this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :
  35. this.selection.endColumn);
  36. return;
  37. }
  38. if (this.targetPosition.lineNumber < this.selection.endLineNumber) {
  39. // Drag the selection upwards
  40. this.targetSelection = new Selection(this.targetPosition.lineNumber, this.targetPosition.column, this.targetPosition.lineNumber + this.selection.endLineNumber - this.selection.startLineNumber, this.selection.startLineNumber === this.selection.endLineNumber ?
  41. this.targetPosition.column + this.selection.endColumn - this.selection.startColumn :
  42. this.selection.endColumn);
  43. return;
  44. }
  45. // The target position is at the same line as the selection's end position.
  46. if (this.selection.endColumn <= this.targetPosition.column) {
  47. // The target position is after the selection's end position
  48. this.targetSelection = new Selection(this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber, this.selection.startLineNumber === this.selection.endLineNumber ?
  49. this.targetPosition.column - this.selection.endColumn + this.selection.startColumn :
  50. this.targetPosition.column - this.selection.endColumn + this.selection.startColumn, this.targetPosition.lineNumber, this.selection.startLineNumber === this.selection.endLineNumber ?
  51. this.targetPosition.column :
  52. this.selection.endColumn);
  53. }
  54. else {
  55. // 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.
  56. this.targetSelection = new Selection(this.targetPosition.lineNumber - this.selection.endLineNumber + this.selection.startLineNumber, this.targetPosition.column, this.targetPosition.lineNumber, this.targetPosition.column + this.selection.endColumn - this.selection.startColumn);
  57. }
  58. }
  59. computeCursorState(model, helper) {
  60. return this.targetSelection;
  61. }
  62. }