triple click selects the whole line

This commit is contained in:
Fabian Jakobs 2010-04-08 13:14:32 +02:00
commit 9ee77b6205
3 changed files with 80 additions and 4 deletions

View file

@ -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();
}
}

View file

@ -0,0 +1,28 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>triple_click</title>
<meta name="author" content="Fabian Jakobs">
</head>
<body>
<div id="juhu">
Juhu Kinners
</div>
<script src="../lib.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var el = document.getElementById("juhu");
addTripleClickListener(el, function() {
console.log("triple");
})
</script>
</body>
</html>

35
lib.js
View file

@ -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);
});
}