only fire change events if selection/cursor really changes

This commit is contained in:
Fabian Jakobs 2010-04-22 19:02:38 +02:00
commit 39433d0733
4 changed files with 52 additions and 14 deletions

View file

@ -16,14 +16,6 @@ ace.Selection = function(doc) {
ace.implement(this, ace.MEventEmitter);
this.updateCursor = function() {
this.$dispatchEvent("changeCursor", { data: this.getCursor() });
};
this.updateSelection = function() {
this.$dispatchEvent("changeSelection", {});
};
this.isEmpty = function() {
return (this.selectionAnchor == null);
};
@ -94,8 +86,10 @@ ace.Selection = function(doc) {
};
this.clearSelection = function() {
this.selectionAnchor = null;
this.updateSelection();
if (this.selectionAnchor) {
this.selectionAnchor = null;
this.$dispatchEvent("changeSelection", {});
}
};
@ -109,12 +103,22 @@ ace.Selection = function(doc) {
};
this.$moveSelection = function(mover) {
var changed = false;
if (!this.selectionAnchor) {
changed = true;
this.selectionAnchor = this.$clone(this.selectionLead);
}
var cursor = this.$clone(this.selectionLead);
mover.call(this);
this.updateSelection();
if (cursor.row !== this.selectionLead.row || cursor.column !== this.selectionLead.column) {
changed = true;
}
if (changed)
this.$dispatchEvent("changeSelection", {});
};
this.selectTo = function(row, column) {
@ -349,8 +353,12 @@ ace.Selection = function(doc) {
};
this.moveCursorTo = function(row, column) {
this.selectionLead = this.$clipPositionToDocument(row, column);
this.updateCursor();
var cursor = this.$clipPositionToDocument(row, column);
if (cursor.row !== this.selectionLead.row || cursor.column !== this.selectionLead.column) {
this.selectionLead = cursor;
this.$dispatchEvent("changeCursor", { data: this.getCursor() });
}
};
this.moveCursorUp = function() {

View file

@ -21,7 +21,7 @@ var NavigationTest = TestCase("NavigationTest",
var doc = this.createTextDocument(200, 10);
var editor = new ace.Editor(new MockRenderer(), doc);
editor.scrollToRow(editor.getLastVisibleRow() + 20);
editor.moveCursorTo(editor.getLastVisibleRow() + 20);
editor.navigateFileStart();
assertEquals(0, editor.getFirstVisibleRow());

View file

@ -216,5 +216,35 @@ var SelectionTest = TestCase("SelectionTest",
var range = selection.getRange();
assertPosition(0, 4, range.start);
assertPosition(0, 6, range.end);
},
"test: moving cursor should fire a 'changeCursor' event" : function() {
var doc = new ace.Document("Juhu Kinners");
var selection = doc.getSelection();
selection.moveCursorTo(0, 5);
var called = false;
selection.addEventListener("changeCursor", function() {
called = true;
});
selection.moveCursorTo(0, 6);
assertTrue(called);
},
"test: calling setCursor with the same position should not fire an event": function() {
var doc = new ace.Document("Juhu Kinners");
var selection = doc.getSelection();
selection.moveCursorTo(0, 5);
var called = false;
selection.addEventListener("changeCursor", function() {
called = true;
});
selection.moveCursorTo(0, 5);
assertFalse(called);
}
});