move () on Selection. Add support for moveDown/Up when in wrapMode

This commit is contained in:
Julian Viereck 2011-01-09 01:28:03 +01:00
commit bc4c9c6060
3 changed files with 33 additions and 39 deletions

View file

@ -768,7 +768,7 @@ var Document = function(text, mode) {
(function() { (function() {
this.$wrapLimit = 12; this.$wrapLimit = 24;
this.$useWrapMode = false; this.$useWrapMode = false;
this.setUseWrapMode = function(useWrapMode) { this.setUseWrapMode = function(useWrapMode) {
var _self = this; var _self = this;
@ -894,6 +894,10 @@ var Document = function(text, mode) {
var docColumn = column + var docColumn = column +
(docRow < linesCount ? wrapData[docRow][row - 1] || 0 : 0); (docRow < linesCount ? wrapData[docRow][row - 1] || 0 : 0);
if (this.lines[docRow]) {
docColumn = Math.min(docColumn, this.lines[docRow].length);
}
return { return {
row: docRow, row: docRow,
column: docColumn column: docColumn

View file

@ -368,13 +368,11 @@ var Editor =function(renderer, doc) {
this.onMouseDoubleClick = function(e) { this.onMouseDoubleClick = function(e) {
this.selection.selectWord(); this.selection.selectWord();
this.$clickSelection = this.getSelectionRange(); this.$clickSelection = this.getSelectionRange();
this.$updateDesiredColumn();
}; };
this.onMouseTripleClick = function(e) { this.onMouseTripleClick = function(e) {
this.selection.selectLine(); this.selection.selectLine();
this.$clickSelection = this.getSelectionRange(); this.$clickSelection = this.getSelectionRange();
this.$updateDesiredColumn();
}; };
this.onMouseWheel = function(e) { this.onMouseWheel = function(e) {
@ -633,7 +631,6 @@ var Editor =function(renderer, doc) {
var range = this.doc.outdentRows(selection.getRange()); var range = this.doc.outdentRows(selection.getRange());
selection.setSelectionRange(range, selection.isBackwards()); selection.setSelectionRange(range, selection.isBackwards());
this.$updateDesiredColumn();
}; };
this.toggleCommentLines = function() { this.toggleCommentLines = function() {
@ -825,17 +822,14 @@ var Editor =function(renderer, doc) {
this.clearSelection = function() { this.clearSelection = function() {
this.selection.clearSelection(); this.selection.clearSelection();
this.$updateDesiredColumn();
}; };
this.moveCursorTo = function(row, column) { this.moveCursorTo = function(row, column) {
this.selection.moveCursorTo(row, column); this.selection.moveCursorTo(row, column);
this.$updateDesiredColumn();
}; };
this.moveCursorToPosition = function(pos) { this.moveCursorToPosition = function(pos) {
this.selection.moveCursorToPosition(pos); this.selection.moveCursorToPosition(pos);
this.$updateDesiredColumn();
}; };
@ -854,35 +848,18 @@ var Editor =function(renderer, doc) {
this.navigateTo = function(row, column) { this.navigateTo = function(row, column) {
this.clearSelection(); this.clearSelection();
this.moveCursorTo(row, column); this.moveCursorTo(row, column);
this.$updateDesiredColumn(column);
}; };
this.navigateUp = function() { this.navigateUp = function() {
this.selection.clearSelection(); this.selection.clearSelection();
this.selection.moveCursorBy(-1, 0); this.selection.moveCursorBy(-1, 0);
if (this.$desiredColumn) {
var cursor = this.getCursorPosition();
var column = this.doc.screenToDocumentColumn(cursor.row, this.$desiredColumn);
this.selection.moveCursorTo(cursor.row, column);
}
}; };
this.navigateDown = function() { this.navigateDown = function() {
this.selection.clearSelection(); this.selection.clearSelection();
this.selection.moveCursorBy(1, 0); this.selection.moveCursorBy(1, 0);
if (this.$desiredColumn) {
var cursor = this.getCursorPosition();
var column = this.doc.screenToDocumentColumn(cursor.row, this.$desiredColumn);
this.selection.moveCursorTo(cursor.row, column);
}
}; };
this.$updateDesiredColumn = function() {
var cursor = this.getCursorPosition();
this.$desiredColumn = this.doc.documentToScreenColumn(cursor.row, cursor.column);
};
this.navigateLeft = function() { this.navigateLeft = function() {
if (!this.selection.isEmpty()) { if (!this.selection.isEmpty()) {
@ -944,7 +921,6 @@ var Editor =function(renderer, doc) {
this.$tryReplace(range, replacement); this.$tryReplace(range, replacement);
if (range !== null) if (range !== null)
this.selection.setSelectionRange(range); this.selection.setSelectionRange(range);
this.$updateDesiredColumn();
}, },
this.replaceAll = function(replacement, options) { this.replaceAll = function(replacement, options) {
@ -963,7 +939,6 @@ var Editor =function(renderer, doc) {
this.$tryReplace(ranges[i], replacement); this.$tryReplace(ranges[i], replacement);
if (ranges[0] !== null) if (ranges[0] !== null)
this.selection.setSelectionRange(ranges[0]); this.selection.setSelectionRange(ranges[0]);
this.$updateDesiredColumn();
}, },
this.$tryReplace = function(range, replacement) { this.$tryReplace = function(range, replacement) {
@ -1016,7 +991,6 @@ var Editor =function(renderer, doc) {
var range = this.$search.find(this.doc); var range = this.$search.find(this.doc);
if (range) { if (range) {
this.gotoLine(range.end.row+1, range.end.column); this.gotoLine(range.end.row+1, range.end.column);
this.$updateDesiredColumn();
this.selection.setSelectionRange(range); this.selection.setSelectionRange(range);
} }
}; };

View file

@ -163,6 +163,15 @@ var Selection = function(doc) {
this.setSelectionAnchor(range.start.row, range.start.column); this.setSelectionAnchor(range.start.row, range.start.column);
this.selectTo(range.end.row, range.end.column); this.selectTo(range.end.row, range.end.column);
} }
this.$updateDesiredColumn();
};
this.$updateDesiredColumn = function() {
var cursor = this.getCursor();
if (cursor) {
this.$desiredColumn = this.doc.documentToScreenPosition(cursor.row, cursor.column).column;
console.log("selection.$updateDesiredColumn", this.$desiredColumn);
}
}; };
this.$moveSelection = function(mover) { this.$moveSelection = function(mover) {
@ -265,7 +274,7 @@ var Selection = function(doc) {
this.moveCursorLeft = function() { this.moveCursorLeft = function() {
if (this.selectionLead.column == 0) { if (this.selectionLead.column == 0) {
// cursor is a line start // cursor is a line (start
if (this.selectionLead.row > 0) { if (this.selectionLead.row > 0) {
this.moveCursorTo(this.selectionLead.row - 1, this.doc this.moveCursorTo(this.selectionLead.row - 1, this.doc
.getLine(this.selectionLead.row - 1).length); .getLine(this.selectionLead.row - 1).length);
@ -380,7 +389,17 @@ var Selection = function(doc) {
}; };
this.moveCursorBy = function(rows, chars) { this.moveCursorBy = function(rows, chars) {
this.moveCursorTo(this.selectionLead.row + rows, this.selectionLead.column + chars); if (this.doc.getUseWrapMode()) {
var pos = this.doc.documentToScreenPosition(
this.selectionLead.row, this.selectionLead.column);
pos = this.doc.screenToDocumentPosition(
pos.row + rows, (this.$desiredColumn || pos.column));
this.moveCursorTo(pos.row, pos.column + chars, chars == 0);
} else {
this.moveCursorTo(this.selectionLead.row + rows,
(this.$desiredColumn || this.selectionLead.column) + chars,
chars == 0);
}
}; };
@ -388,20 +407,17 @@ var Selection = function(doc) {
this.moveCursorTo(position.row, position.column); this.moveCursorTo(position.row, position.column);
}; };
this.moveCursorTo = function(row, column) { this.moveCursorTo = function(row, column, preventUpdateDesiredColumn) {
var cursor = this.$clipPositionToDocument(row, column); var cursor = this.$clipPositionToDocument(row, column);
// only dispatch change if the cursor actually changed // only dispatch change if the cursor actually changed
if (cursor.row !== this.selectionLead.row || cursor.column !== this.selectionLead.column) { if (cursor.row !== this.selectionLead.row || cursor.column !== this.selectionLead.column) {
this.selectionLead = cursor; this.selectionLead = cursor;
!preventUpdateDesiredColumn && this.$updateDesiredColumn(column);
this._dispatchEvent("changeCursor", { data: this.getCursor() }); this._dispatchEvent("changeCursor", { data: this.getCursor() });
} }
}; };
this.moveCursorUp = function() {
this.moveCursorBy(-1, 0);
};
this.$clipPositionToDocument = function(row, column) { this.$clipPositionToDocument = function(row, column) {
var pos = {}; var pos = {};