From f79e4a3111088fc9957ebfbc66cc53ade9ab137b Mon Sep 17 00:00:00 2001 From: Mihai Sucan Date: Mon, 14 Feb 2011 23:31:51 +0200 Subject: [PATCH] Started work on fixing issue 42. Code's not working as desired, yet. Work in progress. :) --- lib/ace/edit_session.js | 9 +++++- lib/ace/editor.js | 7 +++++ lib/ace/mouse_handler.js | 59 ++++++++++++++++++++++++++++++++-------- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index d8cb4e30..b5d234ff 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -1,4 +1,5 @@ -/* ***** BEGIN LICENSE BLOCK ***** +/* 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 @@ -658,6 +659,12 @@ var EditSession = function(text, mode) { return addedRows; }; + this.moveText = function(range, toPosition) { + var removed = this.getTextRange(range); + this.doc.remove(range); + this.doc.insert(toPosition, removed); + }; + this.$clipRowToDocument = function(row) { return Math.max(0, Math.min(row, this.doc.getLength()-1)); }; diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 676fe8ff..bc3a5aec 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -762,6 +762,13 @@ var Editor =function(renderer, session) { }); }; + this.moveText = function(range, toPosition) { + if (this.$readOnly) + return; + + this.session.moveText(range, toPosition); + }; + this.copyLinesUp = function() { if (this.$readOnly) return; diff --git a/lib/ace/mouse_handler.js b/lib/ace/mouse_handler.js index b3a16833..72f6c5f5 100644 --- a/lib/ace/mouse_handler.js +++ b/lib/ace/mouse_handler.js @@ -1,4 +1,5 @@ -/* ***** BEGIN LICENSE BLOCK ***** +/* 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 @@ -38,6 +39,7 @@ define(function(require, exports, module) { var event = require("pilot/event"); +var Range = require("ace/range").Range; var MouseHandler = function(editor) { this.editor = editor; @@ -74,6 +76,8 @@ var MouseHandler = function(editor) { var pos = editor.renderer.screenToTextCoordinates(pageX, pageY); pos.row = Math.max(0, Math.min(pos.row, editor.session.getLength()-1)); + + var inSelection = false; var button = event.getButton(e) if (button != 0) { @@ -86,16 +90,19 @@ var MouseHandler = function(editor) { event.capture(editor.container, function(){}, editor.textInput.onContextMenuClose); } return; - } + } else + inSelection = editor.getSelectionRange().contains(pos.row, pos.column); - if (e.shiftKey) - editor.selection.selectToPosition(pos) - else { - editor.moveCursorToPosition(pos); - if (!editor.$clickSelection) - editor.selection.clearSelection(pos.row, pos.column); + if (!inSelection) { + 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; @@ -110,6 +117,20 @@ var MouseHandler = function(editor) { clearInterval(timerId); self.$clickSelection = null; }; + + var onMouseDragSelectionEnd = function() { + clearInterval(timerId); + + var cursor = editor.getCursorPosition(); + var selection = editor.getSelectionRange(); + editor.moveText(selection, cursor); + + var rows = selection.end.row - selection.start.row; + var columns = selection.end.column - selection.end.column; + + var newSelection = new Range(cursor.row, cursor.column, cursor.row + rows, cursor.column + columns); + editor.selection.setSelectionRange(newSelection); + }; var onSelectionInterval = function() { if (mousePageX === undefined || mousePageY === undefined) @@ -137,9 +158,25 @@ var MouseHandler = function(editor) { editor.renderer.scrollCursorIntoView(); }; + + var onDragSelectionInterval = function() { + if (mousePageX === undefined || mousePageY === undefined) + return; - event.capture(editor.container, onMouseSelection, onMouseSelectionEnd); - var timerId = setInterval(onSelectionInterval, 20); + var cursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY); + cursor.row = Math.max(0, Math.min(cursor.row, editor.session.getLength()-1)); + + editor.moveCursorToPosition(cursor); + editor.renderer.scrollCursorIntoView(); + }; + + if (inSelection) { + event.capture(editor.container, onMouseSelection, onMouseDragSelectionEnd); + var timerId = setInterval(onDragSelectionInterval, 20); + } else { + event.capture(editor.container, onMouseSelection, onMouseSelectionEnd); + var timerId = setInterval(onSelectionInterval, 20); + } return event.preventDefault(e); };