ace/lib/ace/mouse/multi_select_mouse_handler.js
2012-03-30 00:09:19 +04:00

173 lines
No EOL
5.9 KiB
JavaScript

/* vim:ts=4:sts=4:sw=4:
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Ajax.org Code Editor (ACE).
*
* The Initial Developer of the Original Code is
* Ajax.org B.V.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Harutyun Amirjanyan <amirjanyan AT gmail DOT com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
var event = require("../lib/event");
// mouse
function isSamePoint(p1, p2) {
return p1.row == p2.row && p1.column == p2.column
}
function onMouseDown(e) {
var ev = e.domEvent;
var alt = ev.altKey;
var shift = ev.shiftKey;
var ctrl = ev.ctrlKey;
var button = e.getButton();
if (!ctrl && !alt) {
if (e.editor.selection.rangeCount > 1) {
if (button == 0) {
e.editor.exitMultiSelectMode();
} else if (button == 2) {
var editor = e.editor;
var selectionEmpty = editor.selection.isEmpty();
editor.textInput.onContextMenu({x: e.clientX, y: e.clientY}, selectionEmpty);
event.capture(editor.container, function(){}, editor.textInput.onContextMenuClose);
e.stop();
}
}
return;
}
var editor = e.editor;
var selection = editor.selection;
var isMultiSelect = selection.rangeCount > 1
var pos = e.getDocumentPosition();
var rangeList = selection.rangeList;
var cursor = selection.getCursor()
var inSelection = e.inSelection() || (selection.isEmpty() && isSamePoint(pos, cursor));
if (ctrl && !shift && !alt && button == 0) {
if (!isMultiSelect && inSelection)
return // dragging
if (!isMultiSelect) {
selection.addRange(selection.toOrientedRange());
}
if (inSelection)
selection.clearSelection();
var helper = selection.toOrientedRange();
editor.addSelectionMarker(helper);
var oldRange = rangeList.rangeAtPoint(pos);
event.capture(editor.container, function(){}, function() {
editor.removeSelectionMarkers([helper]);
var tmpSel = selection.toOrientedRange();
if (oldRange && tmpSel.isEmpty() && isSamePoint(oldRange.cursor, tmpSel.cursor)) {
if (selection.rangeCount > 1) {
range = editor.selection.rangeList.substractPoint(tmpSel.cursor);
var range = editor.selection.rangeList.all[0];
if (range)
editor.selection.addRange(range);
return;
}
}
selection.addRange(tmpSel);
});
//e.stop()
} else if (!shift && alt && button == 0) {
e.stop()
var mouseX = e.pageX, mouseY = e.pageY;
var onMouseSelection = function(e) {
mouseX = event.getDocumentX(e);
mouseY = event.getDocumentY(e);
};
if (isMultiSelect && !ctrl) {
selection.single();
}
selection.moveCursorToPosition(pos);
selection.clearSelection();
var rectSel = [];
var session = editor.session;
var onMouseSelectionEnd = function(e) {
clearInterval(timerId);
editor.removeSelectionMarkers(rectSel);
for (var i = 0; i < rectSel.length; i++)
selection.addRange(rectSel[i])
};
var anchor = selection.getCursor();
var screenAnchor = editor.renderer.pixelToScreenCoordinates(mouseX, mouseY);
var clippedAnchor = session.documentToScreenPosition(anchor);
var screenCursor = screenAnchor;
var onSelectionInterval = function() {
var newCursor = editor.renderer.pixelToScreenCoordinates(mouseX, mouseY);
var cursor = session.screenToDocumentPosition(newCursor.row, newCursor.column);
if (isSamePoint(screenCursor, newCursor)
&& isSamePoint(cursor, selection.selectionLead))
return;
screenCursor = newCursor;
editor.selection.moveCursorToPosition(cursor);
editor.selection.clearSelection();
editor.renderer.scrollCursorIntoView();
editor.removeSelectionMarkers(rectSel);
rectSel = selection.rectangularRangeBlock(screenCursor, screenAnchor);
rectSel.forEach(editor.addSelectionMarker, editor);
editor.renderer.updateCursor();
editor.renderer.updateBackMarkers();
};
event.capture(editor.container, onMouseSelection, onMouseSelectionEnd);
var timerId = setInterval(onSelectionInterval, 20);
return e.preventDefault();
}
}
exports.onMouseDown = onMouseDown;
});