Copy and paste and basic selection support
This commit is contained in:
parent
828b4a184a
commit
f85c1b9758
6 changed files with 358 additions and 22 deletions
101
Editor.js
101
Editor.js
|
|
@ -39,12 +39,26 @@ function TextInput(parentNode, host) {
|
|||
host.onCompositionEnd();
|
||||
onTextInput();
|
||||
}
|
||||
|
||||
var onCopy = function() {
|
||||
text.value = host.getCopyText();
|
||||
text.select();
|
||||
}
|
||||
|
||||
var onCut = function() {
|
||||
text.value = host.getCopyText();
|
||||
host.onCut();
|
||||
text.select();
|
||||
}
|
||||
|
||||
addListener(text, "keypress", onTextInput, false);
|
||||
addListener(text, "textInput", onTextInput, false);
|
||||
addListener(text, "paste", onTextInput, false);
|
||||
addListener(text, "paste", onTextInput, false);
|
||||
addListener(text, "propertychange", onTextInput, false);
|
||||
|
||||
addListener(text, "copy", onCopy, false);
|
||||
addListener(text, "cut", onCut, false);
|
||||
|
||||
addListener(text, "compositionstart", onCompositionStart, false);
|
||||
addListener(text, "compositionupdate", onCompositionUpdate, false);
|
||||
addListener(text, "compositionend", onCompositionEnd, false);
|
||||
|
|
@ -84,6 +98,12 @@ function KeyBinding(element, host)
|
|||
addListener(element, "keydown", function(e)
|
||||
{
|
||||
var key = e.keyCode;
|
||||
|
||||
// TODO
|
||||
/*
|
||||
if (!e.shiftKey) {
|
||||
host.clearSelection();
|
||||
}*/
|
||||
|
||||
switch (key)
|
||||
{
|
||||
|
|
@ -96,11 +116,21 @@ function KeyBinding(element, host)
|
|||
return stopEvent(e);
|
||||
|
||||
case keys.LEFT:
|
||||
host.moveLeft();
|
||||
if (e.metaKey) {
|
||||
host.moveLineStart();
|
||||
} else {
|
||||
host.moveLeft();
|
||||
}
|
||||
return stopEvent(e);
|
||||
|
||||
case keys.RIGHT:
|
||||
host.moveRight();
|
||||
if (e.metaKey) {
|
||||
host.moveLineEnd();
|
||||
} else if (e.shiftKey) {
|
||||
host.selectRight();
|
||||
} else {
|
||||
host.moveRight();
|
||||
}
|
||||
return stopEvent(e);
|
||||
|
||||
case keys.POS1:
|
||||
|
|
@ -147,13 +177,17 @@ function Editor(doc, renderer)
|
|||
return preventDefault(e);
|
||||
});
|
||||
|
||||
this.cursor = {
|
||||
row: 0,
|
||||
column: 0
|
||||
}
|
||||
this.doc = doc;
|
||||
renderer.setDocument(doc);
|
||||
|
||||
this.cursor = {
|
||||
row: 0,
|
||||
column: 0
|
||||
};
|
||||
|
||||
this.selectionRange = null;
|
||||
this.selection = null;
|
||||
|
||||
this.draw();
|
||||
}
|
||||
|
||||
|
|
@ -179,6 +213,25 @@ Editor.prototype =
|
|||
this.renderer.visualizeBlur();
|
||||
},
|
||||
|
||||
getCopyText : function()
|
||||
{
|
||||
if (this.selectionRange) {
|
||||
return this.doc.getTextRange(this.selectionRange);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
},
|
||||
|
||||
onCut : function()
|
||||
{
|
||||
if (this.selectionRange)
|
||||
{
|
||||
this.cursor = this.doc.remove(this.selectionRange);
|
||||
this.clearSelection();
|
||||
this.draw();
|
||||
}
|
||||
},
|
||||
|
||||
placeCursorToMouse : function(pageX, pageY)
|
||||
{
|
||||
var pos = this.renderer.screenToTextCoordinates(pageX, pageY);
|
||||
|
|
@ -289,6 +342,40 @@ Editor.prototype =
|
|||
this.renderer.scrollCursorIntoView();
|
||||
},
|
||||
|
||||
clearSelection : function()
|
||||
{
|
||||
this.selectionRange = null;
|
||||
if (this.selection) {
|
||||
this.renderer.removeMarker(this.selection);
|
||||
this.selection = null;
|
||||
}
|
||||
},
|
||||
|
||||
selectRight : function()
|
||||
{
|
||||
if (!this.selectionRange) {
|
||||
this.selectionRange = {
|
||||
start: {
|
||||
row: this.cursor.row,
|
||||
column: this.cursor.column
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.moveRight();
|
||||
|
||||
this.selectionRange.end = {
|
||||
row: this.cursor.row,
|
||||
column: this.cursor.column
|
||||
}
|
||||
|
||||
if (this.selection) {
|
||||
this.renderer.updateMarker(this.selection, this.selectionRange);
|
||||
} else {
|
||||
this.selection = this.renderer.addMarker(this.selectionRange, "selection");
|
||||
}
|
||||
},
|
||||
|
||||
moveBy : function(rows, chars) {
|
||||
this.moveTo(this.cursor.row+rows, this.cursor.column+chars);
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue