multi_select_handler.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. var event = require("../lib/event");
  32. // mouse
  33. function isSamePoint(p1, p2) {
  34. return p1.row == p2.row && p1.column == p2.column;
  35. }
  36. function onMouseDown(e) {
  37. var ev = e.domEvent;
  38. var alt = ev.altKey;
  39. var shift = ev.shiftKey;
  40. var ctrl = e.getAccelKey();
  41. var button = e.getButton();
  42. if (e.editor.inMultiSelectMode && button == 2) {
  43. e.editor.textInput.onContextMenu(e.domEvent);
  44. return;
  45. }
  46. if (!ctrl && !alt) {
  47. if (button == 0 && e.editor.inMultiSelectMode)
  48. e.editor.exitMultiSelectMode();
  49. return;
  50. }
  51. var editor = e.editor;
  52. var selection = editor.selection;
  53. var isMultiSelect = editor.inMultiSelectMode;
  54. var pos = e.getDocumentPosition();
  55. var cursor = selection.getCursor();
  56. var inSelection = e.inSelection() || (selection.isEmpty() && isSamePoint(pos, cursor));
  57. var mouseX = e.x, mouseY = e.y;
  58. var onMouseSelection = function(e) {
  59. mouseX = e.clientX;
  60. mouseY = e.clientY;
  61. };
  62. var blockSelect = function() {
  63. var newCursor = editor.renderer.pixelToScreenCoordinates(mouseX, mouseY);
  64. var cursor = session.screenToDocumentPosition(newCursor.row, newCursor.column);
  65. if (isSamePoint(screenCursor, newCursor)
  66. && isSamePoint(cursor, selection.selectionLead))
  67. return;
  68. screenCursor = newCursor;
  69. editor.selection.moveCursorToPosition(cursor);
  70. editor.selection.clearSelection();
  71. editor.renderer.scrollCursorIntoView();
  72. editor.removeSelectionMarkers(rectSel);
  73. rectSel = selection.rectangularRangeBlock(screenCursor, screenAnchor);
  74. rectSel.forEach(editor.addSelectionMarker, editor);
  75. editor.updateSelectionMarkers();
  76. };
  77. var session = editor.session;
  78. var screenAnchor = editor.renderer.pixelToScreenCoordinates(mouseX, mouseY);
  79. var screenCursor = screenAnchor;
  80. if (ctrl && !shift && !alt && button == 0) {
  81. if (!isMultiSelect && inSelection)
  82. return; // dragging
  83. if (!isMultiSelect) {
  84. var range = selection.toOrientedRange();
  85. editor.addSelectionMarker(range);
  86. }
  87. var oldRange = selection.rangeList.rangeAtPoint(pos);
  88. editor.once("mouseup", function() {
  89. var tmpSel = selection.toOrientedRange();
  90. if (oldRange && tmpSel.isEmpty() && isSamePoint(oldRange.cursor, tmpSel.cursor))
  91. selection.substractPoint(tmpSel.cursor);
  92. else {
  93. if (range) {
  94. editor.removeSelectionMarker(range);
  95. selection.addRange(range);
  96. }
  97. selection.addRange(tmpSel);
  98. }
  99. });
  100. } else if (alt && button == 0) {
  101. e.stop();
  102. if (isMultiSelect && !ctrl)
  103. selection.toSingleRange();
  104. else if (!isMultiSelect && ctrl)
  105. selection.addRange();
  106. var rectSel = [];
  107. if (shift) {
  108. screenAnchor = session.documentToScreenPosition(selection.lead);
  109. blockSelect();
  110. } else {
  111. selection.moveCursorToPosition(pos);
  112. selection.clearSelection();
  113. }
  114. var onMouseSelectionEnd = function(e) {
  115. clearInterval(timerId);
  116. editor.removeSelectionMarkers(rectSel);
  117. for (var i = 0; i < rectSel.length; i++)
  118. selection.addRange(rectSel[i]);
  119. };
  120. var onSelectionInterval = blockSelect;
  121. event.capture(editor.container, onMouseSelection, onMouseSelectionEnd);
  122. var timerId = setInterval(function() {onSelectionInterval();}, 20);
  123. return e.preventDefault();
  124. }
  125. }
  126. exports.onMouseDown = onMouseDown;
  127. });