From 39433d0733f611db4e6519298ca68ccdedcd7f55 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 22 Apr 2010 19:02:38 +0200 Subject: [PATCH] only fire change events if selection/cursor really changes --- src/Selection.js | 34 ++++++++++++------- test/{TextDocumentTest.js => DocumentTest.js} | 0 test/NavigationTest.js | 2 +- test/SelectionTest.js | 30 ++++++++++++++++ 4 files changed, 52 insertions(+), 14 deletions(-) rename test/{TextDocumentTest.js => DocumentTest.js} (100%) diff --git a/src/Selection.js b/src/Selection.js index 6741e82a..f1048851 100644 --- a/src/Selection.js +++ b/src/Selection.js @@ -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() { diff --git a/test/TextDocumentTest.js b/test/DocumentTest.js similarity index 100% rename from test/TextDocumentTest.js rename to test/DocumentTest.js diff --git a/test/NavigationTest.js b/test/NavigationTest.js index 2d6470c4..37b84579 100644 --- a/test/NavigationTest.js +++ b/test/NavigationTest.js @@ -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()); diff --git a/test/SelectionTest.js b/test/SelectionTest.js index e27ce332..05f76be9 100644 --- a/test/SelectionTest.js +++ b/test/SelectionTest.js @@ -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); } }); \ No newline at end of file