mirror of
https://github.com/atom/atom.git
synced 2026-01-23 22:08:08 -05:00
320 lines
11 KiB
JavaScript
320 lines
11 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):
|
|
* Fabian Jakobs <fabian AT ajax DOT org>
|
|
* Mihai Sucan <mihai DOT sucan 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("pilot/event");
|
|
var dom = require("pilot/dom");
|
|
var BrowserFocus = require("pilot/browser_focus").BrowserFocus;
|
|
|
|
var STATE_UNKNOWN = 0;
|
|
var STATE_SELECT = 1;
|
|
var STATE_DRAG = 2;
|
|
|
|
var DRAG_TIMER = 250; // milliseconds
|
|
var DRAG_OFFSET = 5; // pixels
|
|
|
|
var MouseHandler = function(editor) {
|
|
this.editor = editor;
|
|
|
|
this.browserFocus = new BrowserFocus();
|
|
event.addListener(editor.container, "mousedown", function(e) {
|
|
editor.focus();
|
|
return event.preventDefault(e);
|
|
});
|
|
event.addListener(editor.container, "selectstart", function(e) {
|
|
return event.preventDefault(e);
|
|
});
|
|
|
|
var mouseTarget = editor.renderer.getMouseEventTarget();
|
|
event.addListener(mouseTarget, "mousedown", this.onMouseDown.bind(this));
|
|
event.addMultiMouseDownListener(mouseTarget, 0, 2, 500, this.onMouseDoubleClick.bind(this));
|
|
event.addMultiMouseDownListener(mouseTarget, 0, 3, 600, this.onMouseTripleClick.bind(this));
|
|
event.addMultiMouseDownListener(mouseTarget, 0, 4, 600, this.onMouseQuadClick.bind(this));
|
|
event.addMouseWheelListener(editor.container, this.onMouseWheel.bind(this));
|
|
};
|
|
|
|
(function() {
|
|
|
|
this.$scrollSpeed = 1;
|
|
this.setScrollSpeed = function(speed) {
|
|
this.$scrollSpeed = speed;
|
|
};
|
|
|
|
this.getScrollSpeed = function() {
|
|
return this.$scrollSpeed;
|
|
};
|
|
|
|
this.$getEventPosition = function(e) {
|
|
var pageX = event.getDocumentX(e);
|
|
var pageY = event.getDocumentY(e);
|
|
var pos = this.editor.renderer.screenToTextCoordinates(pageX, pageY);
|
|
pos.row = Math.max(0, Math.min(pos.row, this.editor.session.getLength()-1));
|
|
return pos;
|
|
};
|
|
|
|
this.$distance = function(ax, ay, bx, by) {
|
|
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
|
|
};
|
|
|
|
this.onMouseDown = function(e) {
|
|
var pageX = event.getDocumentX(e);
|
|
var pageY = event.getDocumentY(e);
|
|
|
|
var pos = this.$getEventPosition(e);
|
|
|
|
var editor = this.editor;
|
|
var self = this;
|
|
var selectionRange = editor.getSelectionRange();
|
|
var selectionEmpty = selectionRange.isEmpty();
|
|
var inSelection = !editor.getReadOnly()
|
|
&& !selectionEmpty
|
|
&& selectionRange.contains(pos.row, pos.column);
|
|
|
|
var state = STATE_UNKNOWN;
|
|
|
|
// if this click caused the editor to be focused should not clear the
|
|
// selection
|
|
if (
|
|
inSelection && (
|
|
!this.browserFocus.isFocused()
|
|
|| new Date().getTime() - this.browserFocus.lastFocus < 20
|
|
|| !this.editor.isFocused()
|
|
)
|
|
) {
|
|
this.editor.focus();
|
|
return;
|
|
}
|
|
|
|
var button = event.getButton(e);
|
|
if (button !== 0) {
|
|
if (selectionEmpty) {
|
|
editor.moveCursorToPosition(pos);
|
|
}
|
|
if(button == 2) {
|
|
editor.textInput.onContextMenu({x: pageX, y: pageY}, selectionEmpty);
|
|
event.capture(editor.container, function(){}, editor.textInput.onContextMenuClose);
|
|
}
|
|
return;
|
|
} else {
|
|
// Select the fold as the user clicks it.
|
|
var fold = editor.session.getFoldAt(pos.row, pos.column, 1);
|
|
if (fold) {
|
|
editor.selection.setSelectionRange(fold.range);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!inSelection) {
|
|
// Directly pick STATE_SELECT, since the user is not clicking inside
|
|
// a selection.
|
|
onStartSelect(pos);
|
|
}
|
|
|
|
var mousePageX, mousePageY;
|
|
var overwrite = editor.getOverwrite();
|
|
var mousedownTime = (new Date()).getTime();
|
|
var dragCursor, dragRange;
|
|
|
|
var onMouseSelection = function(e) {
|
|
mousePageX = event.getDocumentX(e);
|
|
mousePageY = event.getDocumentY(e);
|
|
};
|
|
|
|
var onMouseSelectionEnd = function() {
|
|
clearInterval(timerId);
|
|
if (state == STATE_UNKNOWN)
|
|
onStartSelect(pos);
|
|
else if (state == STATE_DRAG)
|
|
onMouseDragSelectionEnd();
|
|
|
|
self.$clickSelection = null;
|
|
state = STATE_UNKNOWN;
|
|
};
|
|
|
|
var onMouseDragSelectionEnd = function() {
|
|
dom.removeCssClass(editor.container, "ace_dragging");
|
|
editor.session.removeMarker(dragSelectionMarker);
|
|
|
|
if (!self.$clickSelection) {
|
|
if (!dragCursor) {
|
|
editor.moveCursorToPosition(pos);
|
|
editor.selection.clearSelection(pos.row, pos.column);
|
|
}
|
|
}
|
|
|
|
if (!dragCursor)
|
|
return;
|
|
|
|
if (dragRange.contains(dragCursor.row, dragCursor.column)) {
|
|
dragCursor = null;
|
|
return;
|
|
}
|
|
|
|
editor.clearSelection();
|
|
var newRange = editor.moveText(dragRange, dragCursor);
|
|
if (!newRange) {
|
|
dragCursor = null;
|
|
return;
|
|
}
|
|
|
|
editor.selection.setSelectionRange(newRange);
|
|
};
|
|
|
|
var onSelectionInterval = function() {
|
|
if (mousePageX === undefined || mousePageY === undefined)
|
|
return;
|
|
|
|
if (state == STATE_UNKNOWN) {
|
|
var distance = self.$distance(pageX, pageY, mousePageX, mousePageY);
|
|
var time = (new Date()).getTime();
|
|
|
|
|
|
if (distance > DRAG_OFFSET) {
|
|
state = STATE_SELECT;
|
|
var cursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
|
|
cursor.row = Math.max(0, Math.min(cursor.row, editor.session.getLength()-1));
|
|
onStartSelect(cursor);
|
|
} else if ((time - mousedownTime) > DRAG_TIMER) {
|
|
state = STATE_DRAG;
|
|
dragRange = editor.getSelectionRange();
|
|
var style = editor.getSelectionStyle();
|
|
dragSelectionMarker = editor.session.addMarker(dragRange, "ace_selection", style);
|
|
editor.clearSelection();
|
|
dom.addCssClass(editor.container, "ace_dragging");
|
|
}
|
|
|
|
}
|
|
|
|
if (state == STATE_DRAG)
|
|
onDragSelectionInterval();
|
|
else if (state == STATE_SELECT)
|
|
onUpdateSelectionInterval();
|
|
};
|
|
|
|
function onStartSelect(pos) {
|
|
if (e.shiftKey)
|
|
editor.selection.selectToPosition(pos)
|
|
else {
|
|
if (!self.$clickSelection) {
|
|
editor.moveCursorToPosition(pos);
|
|
editor.selection.clearSelection(pos.row, pos.column);
|
|
}
|
|
}
|
|
state = STATE_SELECT;
|
|
}
|
|
|
|
var onUpdateSelectionInterval = function() {
|
|
var cursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
|
|
cursor.row = Math.max(0, Math.min(cursor.row, editor.session.getLength()-1));
|
|
|
|
if (self.$clickSelection) {
|
|
if (self.$clickSelection.contains(cursor.row, cursor.column)) {
|
|
editor.selection.setSelectionRange(self.$clickSelection);
|
|
} else {
|
|
if (self.$clickSelection.compare(cursor.row, cursor.column) == -1) {
|
|
var anchor = self.$clickSelection.end;
|
|
} else {
|
|
var anchor = self.$clickSelection.start;
|
|
}
|
|
editor.selection.setSelectionAnchor(anchor.row, anchor.column);
|
|
editor.selection.selectToPosition(cursor);
|
|
}
|
|
}
|
|
else {
|
|
editor.selection.selectToPosition(cursor);
|
|
}
|
|
|
|
editor.renderer.scrollCursorIntoView();
|
|
};
|
|
|
|
var onDragSelectionInterval = function() {
|
|
dragCursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
|
|
dragCursor.row = Math.max(0, Math.min(dragCursor.row, editor.session.getLength() - 1));
|
|
|
|
editor.moveCursorToPosition(dragCursor);
|
|
};
|
|
|
|
event.capture(editor.container, onMouseSelection, onMouseSelectionEnd);
|
|
var timerId = setInterval(onSelectionInterval, 20);
|
|
|
|
return event.preventDefault(e);
|
|
};
|
|
|
|
this.onMouseDoubleClick = function(e) {
|
|
var editor = this.editor;
|
|
var pos = this.$getEventPosition(e);
|
|
|
|
// If the user dclicked on a fold, then expand it.
|
|
var fold = editor.session.getFoldAt(pos.row, pos.column, 1);
|
|
if (fold) {
|
|
editor.session.expandFold(fold);
|
|
} else {
|
|
editor.moveCursorToPosition(pos);
|
|
editor.selection.selectWord();
|
|
this.$clickSelection = editor.getSelectionRange();
|
|
}
|
|
};
|
|
|
|
this.onMouseTripleClick = function(e) {
|
|
var pos = this.$getEventPosition(e);
|
|
this.editor.moveCursorToPosition(pos);
|
|
this.editor.selection.selectLine();
|
|
this.$clickSelection = this.editor.getSelectionRange();
|
|
};
|
|
|
|
this.onMouseQuadClick = function(e) {
|
|
this.editor.selectAll();
|
|
this.$clickSelection = this.editor.getSelectionRange();
|
|
};
|
|
|
|
this.onMouseWheel = function(e) {
|
|
var speed = this.$scrollSpeed * 2;
|
|
|
|
this.editor.renderer.scrollBy(e.wheelX * speed, e.wheelY * speed);
|
|
if (this.editor.renderer.isScrollableBy(e.wheelX, e.wheelY))
|
|
return event.preventDefault(e);
|
|
};
|
|
|
|
|
|
}).call(MouseHandler.prototype);
|
|
|
|
exports.MouseHandler = MouseHandler;
|
|
});
|