Implements Ctrl+X cut support

This commit is contained in:
Dan Kaplun 2014-04-25 17:15:21 -05:00
parent c37f58b358
commit e6dc7dc005

View File

@ -86,9 +86,10 @@ Editor.prototype._initHandlers = function () {
self.cursor(key.name === 'home' ? 0 : Infinity, self.cursor().y);
}
}
if (key.full === 'C-c') {
var text = self.select().text;
if (text) { clipboard.copy(text); }
if (key.full === 'C-c' || key.full === 'C-x') {
var selection = self.select();
if (selection.text) { clipboard.copy(selection.text); }
if (key.full === 'C-x') { self.delete(); }
}
});
@ -132,19 +133,24 @@ Editor.prototype._initHandlers = function () {
self.scroll(scroll);
});
self.on('text', function (text) {
self.on('lines', function (text) {
self._editorRender();
});
self.on('scroll', function (scroll) {
self._editorRender();
});
self.on('startSelection', function (selection) {
self._editorRender();
});
};
Editor.prototype.text = util.getterSetter('text', null, function (text) {
text = text.toString();
this._lines = text.split(/\n/).map(function (line) { return new Line(line); });
return text;
Editor.prototype.text = util.getterSetter('lines', null, function (text) {
this.emit('text', text);
return text.toString()
.split(/\n/)
.map(function (line) { return new Line(line); });
});
Editor.prototype.line = function (n) {
return this._lines[arguments.length ? Math.max(Math.min(n, this._lines.length - 1), 0) : this.cursor().y];
@ -221,6 +227,7 @@ Editor.prototype.startSelection = util.getterSetter('startSelection', function (
Editor.prototype.select = function (start, end) {
if (arguments.length) {
if (arguments.length === 1) {
if (start === null) { return this.startSelection(start); }
end = start;
start = this.cursor();
}
@ -228,10 +235,8 @@ Editor.prototype.select = function (start, end) {
.startSelection(start)
.cursor(end);
} else {
var startSelection = this.startSelection();
if (!startSelection) { return null; }
var selectionBounds = [startSelection, this.cursor()];
var cursor = this.cursor();
var selectionBounds = [this.startSelection() || cursor, cursor];
selectionBounds.sort(coordinate.linear.cmp);
return {
start: selectionBounds[0],
@ -266,6 +271,20 @@ Editor.prototype.size = function () {
};
};
Editor.prototype.delete = function (start, end) {
if (arguments.length < 2) {
if (!arguments.length) { start = this.select(); }
end = start.end; start = start.start;
}
this._lines[start.y] = new Line(
this._lines[start.y].text().slice(0, start.x) +
this._lines[end.y].text().slice(end.x));
this._lines.splice(start.y + 1, end.y - start.y);
return this.select(null, start);
}
var markupRegex = /{(\/?)([\w\-,;!#]*)}/;
Editor._realIndex = function (markup, index) {
var i = 0;