mirror of
https://github.com/embarklabs/neo-blessed.git
synced 2025-01-10 19:16:20 +00:00
cleanup textbox.
This commit is contained in:
parent
4f0f9e53d7
commit
5ef468c192
@ -763,8 +763,6 @@ function Element(options) {
|
||||
}
|
||||
|
||||
this.parseTags = options.parseTags || options.tags;
|
||||
// TODO: Maybe simply set _pcontent with _parseTags result.
|
||||
//this.content = this._parseTags(options.content || '');
|
||||
|
||||
this.setContent(options.content || '');
|
||||
|
||||
@ -807,8 +805,6 @@ function Element(options) {
|
||||
this.on('attach', function() {
|
||||
self.parseContent();
|
||||
});
|
||||
|
||||
//this.parseContent();
|
||||
}
|
||||
|
||||
Element.prototype.__proto__ = Node.prototype;
|
||||
@ -1268,7 +1264,7 @@ Box.prototype.render = function(stop) {
|
||||
, cell
|
||||
, attr
|
||||
, ch
|
||||
, content = this._pcontent || this.content
|
||||
, content = this._pcontent
|
||||
, ci = this.contentIndex || 0
|
||||
, cl = content.length
|
||||
, battr
|
||||
@ -1953,6 +1949,7 @@ function Textbox(options) {
|
||||
}
|
||||
Input.call(this, options);
|
||||
this.screen._listenKeys(this);
|
||||
this.value = options.value || '';
|
||||
}
|
||||
|
||||
Textbox.prototype.__proto__ = Input.prototype;
|
||||
@ -1973,7 +1970,7 @@ Textbox.prototype.setInput = function(callback) {
|
||||
this.screen.program.cup(
|
||||
this.top + 1 + (this.border ? 1 : 0),
|
||||
this.left + 1 + (this.border ? 1 : 0)
|
||||
+ this.content.length);
|
||||
+ this.value.length);
|
||||
this.screen.program.showCursor();
|
||||
this.screen.program.sgr('normal');
|
||||
|
||||
@ -2000,43 +1997,46 @@ Textbox.prototype.setInput = function(callback) {
|
||||
|
||||
Textbox.prototype._listener = function(ch, key) {
|
||||
var callback = this._callback
|
||||
, value = this.content;
|
||||
, value = this.value;
|
||||
|
||||
if (key.name === 'escape' || key.name === 'enter') {
|
||||
delete this._callback;
|
||||
this.setContent('');
|
||||
this.value = '';
|
||||
this.removeListener('keypress', this.__listener);
|
||||
delete this.__listener;
|
||||
callback(null, key.name === 'enter' ? value : null);
|
||||
} else if (key.name === 'backspace') {
|
||||
if (this.content.length) {
|
||||
this.setContent(this.content.slice(0, -1));
|
||||
if (this.content.length < this.width - (this.border ? 2 : 0) - 1) {
|
||||
if (this.value.length) {
|
||||
this.value = this.value.slice(0, -1);
|
||||
if (this.value.length < this.width - (this.border ? 2 : 0) - 1) {
|
||||
this.screen.program.cub();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (ch) {
|
||||
this.setContent(this.content + ch);
|
||||
if (this.content.length < this.width - (this.border ? 2 : 0)) {
|
||||
this.value += ch;
|
||||
if (this.value.length < this.width - (this.border ? 2 : 0)) {
|
||||
this.screen.program.cuf();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Maybe just use this instead of render hook:
|
||||
// Problem - user can't set .value willy nilly.
|
||||
// if (this.value !== value) {
|
||||
// this.setContent(this.value.slice(-(this.width - (this.border ? 2 : 0) - 1)));
|
||||
// }
|
||||
|
||||
this.screen.render();
|
||||
};
|
||||
|
||||
Textbox.prototype._render = Input.prototype.render;
|
||||
Textbox.prototype.render = function(stop) {
|
||||
var content = this.content;
|
||||
//this.content = this.content.slice(-(this.width - (this.border ? 2 : 0) - 1));
|
||||
this.setContent(content.slice(-(this.width - (this.border ? 2 : 0) - 1)), true);
|
||||
var ret = this._render(stop);
|
||||
//this.content = content;
|
||||
this.setContent(content, true);
|
||||
if (stop) return ret;
|
||||
return ret;
|
||||
// setContent is necessary to clear the area in case
|
||||
// .shrink is being used and the new content is smaller.
|
||||
// Could technically optimize this.
|
||||
this.setContent(this.value.slice(-(this.width - (this.border ? 2 : 0) - 1)));
|
||||
return this._render(stop);
|
||||
};
|
||||
|
||||
Textbox.prototype.setEditor = function(callback) {
|
||||
@ -2053,8 +2053,11 @@ Textbox.prototype.setEditor = function(callback) {
|
||||
self.screen.alloc();
|
||||
self.screen.render();
|
||||
if (err) return callback(err);
|
||||
value = value.replace(/[\r\n]/g, '');
|
||||
self.value = value;
|
||||
self.setContent(value);
|
||||
return callback(null, value);
|
||||
//return callback(null, value);
|
||||
return self.setInput(callback);
|
||||
});
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user