Completed work. Selected text is now draggable.
This commit is contained in:
parent
f79e4a3111
commit
3f24b7a553
6 changed files with 123 additions and 34 deletions
|
|
@ -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
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||||
*
|
*
|
||||||
* The contents of this file are subject to the Mozilla Public License Version
|
* The contents of this file are subject to the Mozilla Public License Version
|
||||||
|
|
@ -70,6 +71,9 @@ var Anchor = exports.Anchor = function(doc, row, column) {
|
||||||
|
|
||||||
this.onChange = function(e) {
|
this.onChange = function(e) {
|
||||||
var delta = e.data;
|
var delta = e.data;
|
||||||
|
if (delta.isMeta)
|
||||||
|
return;
|
||||||
|
|
||||||
var range = delta.range;
|
var range = delta.range;
|
||||||
|
|
||||||
if (range.start.row == range.end.row && range.start.row != this.row)
|
if (range.start.row == range.end.row && range.start.row != this.row)
|
||||||
|
|
|
||||||
|
|
@ -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
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||||
*
|
*
|
||||||
* The contents of this file are subject to the Mozilla Public License Version
|
* The contents of this file are subject to the Mozilla Public License Version
|
||||||
|
|
@ -20,6 +21,7 @@
|
||||||
*
|
*
|
||||||
* Contributor(s):
|
* Contributor(s):
|
||||||
* Fabian Jakobs <fabian AT ajax DOT org>
|
* 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
|
* 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
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||||
|
|
@ -363,9 +365,59 @@ var Document = function(text) {
|
||||||
return end;
|
return end;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move a range of text from the given range to the given position.
|
||||||
|
*
|
||||||
|
* @param fromRange {Range} The range of text you want moved within the
|
||||||
|
* document.
|
||||||
|
* @param toPosition {Object} The location (row and column) where you want
|
||||||
|
* to move the text to.
|
||||||
|
* @return {Range} The new range where the text was moved to.
|
||||||
|
*/
|
||||||
|
this.move = function(fromRange, toPosition) {
|
||||||
|
var text = this.getTextRange(fromRange);
|
||||||
|
this.remove(fromRange);
|
||||||
|
|
||||||
|
var toRow = toPosition.row;
|
||||||
|
var toColumn = toPosition.column;
|
||||||
|
|
||||||
|
// Make sure to update the insert location, when text is removed in
|
||||||
|
// front of the chosen point of insertion.
|
||||||
|
if (!fromRange.isMultiLine() && fromRange.start.row == toRow &&
|
||||||
|
fromRange.end.column < toColumn)
|
||||||
|
toColumn -= text.length;
|
||||||
|
|
||||||
|
if (fromRange.isMultiLine() && fromRange.end.row < toRow) {
|
||||||
|
var lines = this.$split(text);
|
||||||
|
toRow -= lines.length - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var endRow = toRow + fromRange.end.row - fromRange.start.row;
|
||||||
|
var endColumn = fromRange.isMultiLine() ?
|
||||||
|
fromRange.end.column :
|
||||||
|
toColumn + fromRange.end.column - fromRange.start.column;
|
||||||
|
|
||||||
|
var toRange = new Range(toRow, toColumn, endRow, endColumn);
|
||||||
|
|
||||||
|
this.insert(toRange.start, text);
|
||||||
|
|
||||||
|
var delta = {
|
||||||
|
action: "move",
|
||||||
|
fromRange: fromRange,
|
||||||
|
toRange: toRange,
|
||||||
|
isMeta: true,
|
||||||
|
};
|
||||||
|
this._dispatchEvent("change", { data: delta });
|
||||||
|
|
||||||
|
return toRange;
|
||||||
|
};
|
||||||
|
|
||||||
this.applyDeltas = function(deltas) {
|
this.applyDeltas = function(deltas) {
|
||||||
for (var i=0; i<deltas.length; i++) {
|
for (var i=0; i<deltas.length; i++) {
|
||||||
var delta = deltas[i];
|
var delta = deltas[i];
|
||||||
|
if (delta.isMeta)
|
||||||
|
continue;
|
||||||
|
|
||||||
var range = Range.fromPoints(delta.range.start, delta.range.end);
|
var range = Range.fromPoints(delta.range.start, delta.range.end);
|
||||||
|
|
||||||
if (delta.action == "insertLines")
|
if (delta.action == "insertLines")
|
||||||
|
|
@ -382,6 +434,9 @@ var Document = function(text) {
|
||||||
this.revertDeltas = function(deltas) {
|
this.revertDeltas = function(deltas) {
|
||||||
for (var i=deltas.length-1; i>=0; i--) {
|
for (var i=deltas.length-1; i>=0; i--) {
|
||||||
var delta = deltas[i];
|
var delta = deltas[i];
|
||||||
|
if (delta.isMeta)
|
||||||
|
continue;
|
||||||
|
|
||||||
var range = Range.fromPoints(delta.range.start, delta.range.end);
|
var range = Range.fromPoints(delta.range.start, delta.range.end);
|
||||||
|
|
||||||
if (delta.action == "insertLines")
|
if (delta.action == "insertLines")
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@
|
||||||
*
|
*
|
||||||
* Contributor(s):
|
* Contributor(s):
|
||||||
* Fabian Jakobs <fabian AT ajax DOT org>
|
* 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
|
* 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
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||||
|
|
@ -587,7 +588,7 @@ var EditSession = function(text, mode) {
|
||||||
action.start = delta.range.start;
|
action.start = delta.range.start;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update selection based on last operation
|
// update selection based on last operation
|
||||||
this.selection.clearSelection();
|
this.selection.clearSelection();
|
||||||
var action = actions[actions.length-1];
|
var action = actions[actions.length-1];
|
||||||
|
|
@ -659,12 +660,6 @@ var EditSession = function(text, mode) {
|
||||||
return addedRows;
|
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) {
|
this.$clipRowToDocument = function(row) {
|
||||||
return Math.max(0, Math.min(row, this.doc.getLength()-1));
|
return Math.max(0, Math.min(row, this.doc.getLength()-1));
|
||||||
};
|
};
|
||||||
|
|
@ -756,7 +751,7 @@ var EditSession = function(text, mode) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$updateWrapDataOnChange = function(e) {
|
this.$updateWrapDataOnChange = function(e) {
|
||||||
if (!this.$useWrapMode) {
|
if (!this.$useWrapMode || e.data.isMeta) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -271,6 +271,9 @@ var Editor =function(renderer, session) {
|
||||||
|
|
||||||
this.onDocumentChange = function(e) {
|
this.onDocumentChange = function(e) {
|
||||||
var delta = e.data;
|
var delta = e.data;
|
||||||
|
if (delta.isMeta)
|
||||||
|
return;
|
||||||
|
|
||||||
var range = delta.range;
|
var range = delta.range;
|
||||||
|
|
||||||
this.bgTokenizer.start(range.start.row);
|
this.bgTokenizer.start(range.start.row);
|
||||||
|
|
@ -764,9 +767,9 @@ var Editor =function(renderer, session) {
|
||||||
|
|
||||||
this.moveText = function(range, toPosition) {
|
this.moveText = function(range, toPosition) {
|
||||||
if (this.$readOnly)
|
if (this.$readOnly)
|
||||||
return;
|
return null;
|
||||||
|
|
||||||
this.session.moveText(range, toPosition);
|
return this.session.doc.move(range, toPosition);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.copyLinesUp = function() {
|
this.copyLinesUp = function() {
|
||||||
|
|
|
||||||
|
|
@ -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
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||||
*
|
*
|
||||||
* The contents of this file are subject to the Mozilla Public License Version
|
* The contents of this file are subject to the Mozilla Public License Version
|
||||||
|
|
@ -128,6 +129,9 @@ oop.inherits(Mode, TextMode);
|
||||||
worker.call("setValue", [doc.getValue()]);
|
worker.call("setValue", [doc.getValue()]);
|
||||||
|
|
||||||
doc.on("change", function(e) {
|
doc.on("change", function(e) {
|
||||||
|
if (e.data.isMeta)
|
||||||
|
return;
|
||||||
|
|
||||||
e.range = {
|
e.range = {
|
||||||
start: e.data.range.start,
|
start: e.data.range.start,
|
||||||
end: e.data.range.end
|
end: e.data.range.end
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@
|
||||||
*
|
*
|
||||||
* Contributor(s):
|
* Contributor(s):
|
||||||
* Fabian Jakobs <fabian AT ajax DOT org>
|
* 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
|
* 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
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||||
|
|
@ -39,7 +40,6 @@
|
||||||
define(function(require, exports, module) {
|
define(function(require, exports, module) {
|
||||||
|
|
||||||
var event = require("pilot/event");
|
var event = require("pilot/event");
|
||||||
var Range = require("ace/range").Range;
|
|
||||||
|
|
||||||
var MouseHandler = function(editor) {
|
var MouseHandler = function(editor) {
|
||||||
this.editor = editor;
|
this.editor = editor;
|
||||||
|
|
@ -68,31 +68,40 @@ var MouseHandler = function(editor) {
|
||||||
this.getScrollSpeed = function() {
|
this.getScrollSpeed = function() {
|
||||||
return this.$scrollSpeed;
|
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.onMouseDown = function(e) {
|
this.onMouseDown = function(e) {
|
||||||
var pageX = event.getDocumentX(e);
|
var pageX = event.getDocumentX(e);
|
||||||
var pageY = event.getDocumentY(e);
|
var pageY = event.getDocumentY(e);
|
||||||
|
var pos = this.$getEventPosition(e);
|
||||||
var editor = this.editor;
|
var editor = this.editor;
|
||||||
|
var selectionRange = editor.getSelectionRange();
|
||||||
var pos = editor.renderer.screenToTextCoordinates(pageX, pageY);
|
var selectionEmpty = selectionRange.isEmpty();
|
||||||
pos.row = Math.max(0, Math.min(pos.row, editor.session.getLength()-1));
|
|
||||||
|
|
||||||
var inSelection = false;
|
var inSelection = false;
|
||||||
|
|
||||||
var button = event.getButton(e)
|
var button = event.getButton(e)
|
||||||
if (button != 0) {
|
if (button != 0) {
|
||||||
var isEmpty = editor.selection.isEmpty()
|
if (selectionEmpty) {
|
||||||
if (isEmpty) {
|
|
||||||
editor.moveCursorToPosition(pos);
|
editor.moveCursorToPosition(pos);
|
||||||
}
|
}
|
||||||
if(button == 2) {
|
if(button == 2) {
|
||||||
editor.textInput.onContextMenu({x: pageX, y: pageY}, isEmpty);
|
editor.textInput.onContextMenu({x: pageX, y: pageY}, selectionEmpty);
|
||||||
event.capture(editor.container, function(){}, editor.textInput.onContextMenuClose);
|
event.capture(editor.container, function(){}, editor.textInput.onContextMenuClose);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else
|
} else
|
||||||
inSelection = editor.getSelectionRange().contains(pos.row, pos.column);
|
inSelection = !this.$clickSelection &&
|
||||||
|
!editor.getReadOnly() &&
|
||||||
|
!selectionEmpty &&
|
||||||
|
selectionRange.contains(pos.row, pos.column);
|
||||||
|
|
||||||
if (!inSelection) {
|
if (!inSelection) {
|
||||||
if (e.shiftKey)
|
if (e.shiftKey)
|
||||||
editor.selection.selectToPosition(pos)
|
editor.selection.selectToPosition(pos)
|
||||||
|
|
@ -107,6 +116,8 @@ var MouseHandler = function(editor) {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var mousePageX, mousePageY;
|
var mousePageX, mousePageY;
|
||||||
|
var overwrite = editor.getOverwrite();
|
||||||
|
var dragCursor = null;
|
||||||
|
|
||||||
var onMouseSelection = function(e) {
|
var onMouseSelection = function(e) {
|
||||||
mousePageX = event.getDocumentX(e);
|
mousePageX = event.getDocumentX(e);
|
||||||
|
|
@ -120,16 +131,28 @@ var MouseHandler = function(editor) {
|
||||||
|
|
||||||
var onMouseDragSelectionEnd = function() {
|
var onMouseDragSelectionEnd = function() {
|
||||||
clearInterval(timerId);
|
clearInterval(timerId);
|
||||||
|
if (!self.$clickSelection) {
|
||||||
|
self.$clickSelection = null;
|
||||||
|
if (!dragCursor) {
|
||||||
|
editor.moveCursorToPosition(pos);
|
||||||
|
editor.selection.clearSelection(pos.row, pos.column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dragCursor)
|
||||||
|
return;
|
||||||
|
|
||||||
var cursor = editor.getCursorPosition();
|
|
||||||
var selection = editor.getSelectionRange();
|
var selection = editor.getSelectionRange();
|
||||||
editor.moveText(selection, cursor);
|
if (selection.contains(dragCursor.row, dragCursor.column))
|
||||||
|
return;
|
||||||
|
|
||||||
var rows = selection.end.row - selection.start.row;
|
editor.clearSelection();
|
||||||
var columns = selection.end.column - selection.end.column;
|
var newRange = editor.moveText(selection, dragCursor);
|
||||||
|
if (!newRange)
|
||||||
|
return;
|
||||||
|
|
||||||
var newSelection = new Range(cursor.row, cursor.column, cursor.row + rows, cursor.column + columns);
|
editor.selection.setSelectionRange(newRange);
|
||||||
editor.selection.setSelectionRange(newSelection);
|
dragCursor = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
var onSelectionInterval = function() {
|
var onSelectionInterval = function() {
|
||||||
|
|
@ -163,10 +186,11 @@ var MouseHandler = function(editor) {
|
||||||
if (mousePageX === undefined || mousePageY === undefined)
|
if (mousePageX === undefined || mousePageY === undefined)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var cursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
|
dragCursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
|
||||||
cursor.row = Math.max(0, Math.min(cursor.row, editor.session.getLength()-1));
|
dragCursor.row = Math.max(0, Math.min(dragCursor.row,
|
||||||
|
editor.session.getLength() - 1));
|
||||||
editor.moveCursorToPosition(cursor);
|
|
||||||
|
editor.renderer.updateCursor(dragCursor, overwrite);
|
||||||
editor.renderer.scrollCursorIntoView();
|
editor.renderer.scrollCursorIntoView();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -182,11 +206,15 @@ var MouseHandler = function(editor) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onMouseDoubleClick = function(e) {
|
this.onMouseDoubleClick = function(e) {
|
||||||
|
var pos = this.$getEventPosition(e);
|
||||||
|
this.editor.moveCursorToPosition(pos);
|
||||||
this.editor.selection.selectWord();
|
this.editor.selection.selectWord();
|
||||||
this.$clickSelection = this.editor.getSelectionRange();
|
this.$clickSelection = this.editor.getSelectionRange();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onMouseTripleClick = function(e) {
|
this.onMouseTripleClick = function(e) {
|
||||||
|
var pos = this.$getEventPosition(e);
|
||||||
|
this.editor.moveCursorToPosition(pos);
|
||||||
this.editor.selection.selectLine();
|
this.editor.selection.selectLine();
|
||||||
this.$clickSelection = this.editor.getSelectionRange();
|
this.$clickSelection = this.editor.getSelectionRange();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue