diff --git a/lib/ace/editor.js b/lib/ace/editor.js index acef5313..bda90c34 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -46,6 +46,7 @@ var oop = require("pilot/oop"); var event = require("pilot/event"); var lang = require("pilot/lang"); var TextInput = require("ace/keyboard/textinput").TextInput; +var MouseHandler = require("ace/mouse_handler").MouseHandler; var KeyBinding = require("ace/keyboard/keybinding").KeyBinding; var EditSession = require("ace/edit_session").EditSession; var Search = require("ace/search").Search; @@ -60,20 +61,8 @@ var Editor =function(renderer, session) { this.textInput = new TextInput(renderer.getTextAreaContainer(), this); this.keyBinding = new KeyBinding(this); - var self = this; - event.addListener(container, "mousedown", function(e) { - self.focus(); - return event.preventDefault(e); - }); - event.addListener(container, "selectstart", function(e) { - return event.preventDefault(e); - }); - - var mouseTarget = 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.addMouseWheelListener(mouseTarget, this.onMouseWheel.bind(this)); + + this.$mouseHandler = new MouseHandler(this); this.$selectionMarker = null; this.$highlightLineMarker = null; @@ -350,100 +339,6 @@ var Editor =function(renderer, session) { this.renderer.setTokenizer(this.bgTokenizer); }; - - this.onMouseDown = function(e) { - var pageX = event.getDocumentX(e); - var pageY = event.getDocumentY(e); - - var pos = this.renderer.screenToTextCoordinates(pageX, pageY); - pos.row = Math.max(0, Math.min(pos.row, this.session.getLength()-1)); - - var button = event.getButton(e) - if (button != 0) { - var isEmpty = this.selection.isEmpty() - if (isEmpty) { - this.moveCursorToPosition(pos); - } - if(button == 2) { - this.textInput.onContextMenu({x: pageX, y: pageY}, isEmpty); - event.capture(this.container, function(){}, this.textInput.onContextMenuClose); - } - return; - } - - if (e.shiftKey) - this.selection.selectToPosition(pos) - else { - this.moveCursorToPosition(pos); - if (!this.$clickSelection) - this.selection.clearSelection(pos.row, pos.column); - } - - this.renderer.scrollCursorIntoView(); - - var self = this; - var mousePageX, mousePageY; - - var onMouseSelection = function(e) { - mousePageX = event.getDocumentX(e); - mousePageY = event.getDocumentY(e); - }; - - var onMouseSelectionEnd = function() { - clearInterval(timerId); - self.$clickSelection = null; - }; - - var onSelectionInterval = function() { - if (mousePageX === undefined || mousePageY === undefined) - return; - - var cursor = self.renderer.screenToTextCoordinates(mousePageX, mousePageY); - cursor.row = Math.max(0, Math.min(cursor.row, self.session.getLength()-1)); - - if (self.$clickSelection) { - if (self.$clickSelection.contains(cursor.row, cursor.column)) { - self.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; - } - self.selection.setSelectionAnchor(anchor.row, anchor.column); - self.selection.selectToPosition(cursor); - } - } - else { - self.selection.selectToPosition(cursor); - } - - self.renderer.scrollCursorIntoView(); - }; - - event.capture(this.container, onMouseSelection, onMouseSelectionEnd); - var timerId = setInterval(onSelectionInterval, 20); - - return event.preventDefault(e); - }; - - this.onMouseDoubleClick = function(e) { - this.selection.selectWord(); - this.$clickSelection = this.getSelectionRange(); - }; - - this.onMouseTripleClick = function(e) { - this.selection.selectLine(); - this.$clickSelection = this.getSelectionRange(); - }; - - this.onMouseWheel = function(e) { - var speed = this.$scrollSpeed * 2; - - this.renderer.scrollBy(e.wheelX * speed, e.wheelY * speed); - return event.preventDefault(e); - }; - this.getCopyText = function() { if (!this.selection.isEmpty()) { return this.session.getTextRange(this.getSelectionRange()); @@ -568,15 +463,13 @@ var Editor =function(renderer, session) { this.setOverwrite(!this.$overwrite); }; - - this.$scrollSpeed = 1; this.setScrollSpeed = function(speed) { - this.$scrollSpeed = speed; - } + this.$mouseHandler.setScrollSpeed(speed); + }; this.getScrollSpeed = function() { - return this.$scrollSpeed; - } + return this.$mouseHandler.getScrollSpeed() + }; this.$selectionStyle = "line"; this.setSelectionStyle = function(style) { @@ -587,7 +480,6 @@ var Editor =function(renderer, session) { this._dispatchEvent("changeSelectionStyle", {data: style}); }; - this.getSelectionStyle = function() { return this.$selectionStyle; }; diff --git a/lib/ace/mouse_handler.js b/lib/ace/mouse_handler.js new file mode 100644 index 00000000..4d4cc05a --- /dev/null +++ b/lib/ace/mouse_handler.js @@ -0,0 +1,168 @@ +/* ***** 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 + * + * 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 MouseHandler = function(editor) { + this.editor = editor; + 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.addMouseWheelListener(mouseTarget, this.onMouseWheel.bind(this)); +}; + +(function() { + + this.$scrollSpeed = 1; + this.setScrollSpeed = function(speed) { + this.$scrollSpeed = speed; + }; + + this.getScrollSpeed = function() { + return this.$scrollSpeed; + }; + + this.onMouseDown = function(e) { + var pageX = event.getDocumentX(e); + var pageY = event.getDocumentY(e); + var editor = this.editor; + + var pos = editor.renderer.screenToTextCoordinates(pageX, pageY); + pos.row = Math.max(0, Math.min(pos.row, editor.session.getLength()-1)); + + var button = event.getButton(e) + if (button != 0) { + var isEmpty = editor.selection.isEmpty() + if (isEmpty) { + editor.moveCursorToPosition(pos); + } + if(button == 2) { + editor.textInput.onContextMenu({x: pageX, y: pageY}, isEmpty); + event.capture(editor.container, function(){}, editor.textInput.onContextMenuClose); + } + return; + } + + if (e.shiftKey) + editor.selection.selectToPosition(pos) + else { + editor.moveCursorToPosition(pos); + if (!editor.$clickSelection) + editor.selection.clearSelection(pos.row, pos.column); + } + + editor.renderer.scrollCursorIntoView(); + + var self = this; + var mousePageX, mousePageY; + + var onMouseSelection = function(e) { + mousePageX = event.getDocumentX(e); + mousePageY = event.getDocumentY(e); + }; + + var onMouseSelectionEnd = function() { + clearInterval(timerId); + self.$clickSelection = null; + }; + + var onSelectionInterval = function() { + if (mousePageX === undefined || mousePageY === undefined) + return; + + 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)) { + self.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(); + }; + + event.capture(editor.container, onMouseSelection, onMouseSelectionEnd); + var timerId = setInterval(onSelectionInterval, 20); + + return event.preventDefault(e); + }; + + this.onMouseDoubleClick = function(e) { + this.editor.selection.selectWord(); + this.$clickSelection = this.editor.getSelectionRange(); + }; + + this.onMouseTripleClick = function(e) { + this.editor.selection.selectLine(); + this.$clickSelection = this.editor.getSelectionRange(); + }; + + this.onMouseWheel = function(e) { + var speed = this.$scrollSpeed * 2; + + this.editor.renderer.scrollBy(e.wheelX * speed, e.wheelY * speed); + return event.preventDefault(e); + }; + + +}).call(MouseHandler.prototype); + +exports.MouseHandler = MouseHandler; +}); \ No newline at end of file