From 9ee77b62054766caf38450080d825ad583ecfb08 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 8 Apr 2010 13:14:32 +0200 Subject: [PATCH] triple click selects the whole line --- Editor.js | 21 +++++++++++++++++++-- experiments/triple_click.html | 28 ++++++++++++++++++++++++++++ lib.js | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 experiments/triple_click.html diff --git a/Editor.js b/Editor.js index 228280ce..fe08dc5c 100644 --- a/Editor.js +++ b/Editor.js @@ -118,6 +118,7 @@ function Editor(doc, renderer) addListener(container, "mousedown", bind(this.onMouseDown, this)); addListener(container, "mousewheel", bind(this.onMouseWheel, this)); + addTripleClickListener(container, bind(this.selectCurrentLine, this)); this.doc = doc; doc.addChangeListener(function(startRow, endRow) { @@ -457,14 +458,30 @@ Editor.prototype = this._moveSelection(this.moveLineEnd); }, + selectCurrentLine : function() + { + this.setSelectionAnchor(this.cursor.row, 0); + this._moveSelection(function() { + this.moveTo(this.cursor.row+1, 0); + }); + }, + moveBy : function(rows, chars) { this.moveTo(this.cursor.row+rows, this.cursor.column+chars); }, moveTo : function(row, column) { - this.cursor.row = Math.min(this.doc.getLength()-1, Math.max(0, row)); - this.cursor.column = Math.min(this.doc.getLine(this.cursor.row).length, Math.max(0, column)); + if (row >= this.doc.getLength()) { + this.cursor.row = this.doc.getLength()-1; + this.cursor.column = this.doc.getLine(this.cursor.row).length; + } else if (row < 0) { + this.cursor.row = 0; + this.cursor.column = 0; + } else { + this.cursor.row = row; + this.cursor.column = Math.min(this.doc.getLine(this.cursor.row).length, Math.max(0, column)); + } this.updateCursor(); } } \ No newline at end of file diff --git a/experiments/triple_click.html b/experiments/triple_click.html new file mode 100644 index 00000000..bdc3caa7 --- /dev/null +++ b/experiments/triple_click.html @@ -0,0 +1,28 @@ + + + + + + triple_click + + + + + +
+ Juhu Kinners +
+ + + + + + diff --git a/lib.js b/lib.js index 5db700ff..2a2301be 100644 --- a/lib.js +++ b/lib.js @@ -3,9 +3,20 @@ function addListener(elem, type, callback) { return elem.addEventListener(type, callback, false); } if (elem.attachEvent) { - elem.attachEvent("on" + type, function() { + var wrapper = function() { callback(window.event); - }); + } + callback.$$wrapper = wrapper; + elem.attachEvent("on" + type, wrapper); + } +} + +function removeListener(elem, type, callback) { + if (elem.removeEventListener) { + return elem.removeEventListener(type, callback, false); + } + if (elem.detachEvent) { + elem.detachEvent("on" + type, callback.$$wrapper || callback); } } @@ -117,4 +128,24 @@ capture = function(el, eventHandler, releaseCaptureHandler) document.addEventListener("mousemove", onMouseMove, true); document.addEventListener("mouseup", onMouseUp, true); +} + +function addTripleClickListener(el, callback) +{ + addListener(el, "dblclick", function() + { + var listener = function(e) + { + clearTimeout(timeoutId); + remove(); + callback(e); + } + + var remove = function() { + removeListener(el, "click", listener); + }; + + addListener(el, "click", listener); + var timeoutId = setTimeout(remove, 300); + }); } \ No newline at end of file