2014-10-23 05:41:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const TextView = require('./text_view.js').TextView;
|
|
|
|
const miscUtil = require('./misc_util.js');
|
|
|
|
const strUtil = require('./string_util.js');
|
2015-04-28 02:19:17 +00:00
|
|
|
|
2022-04-08 21:19:00 +00:00
|
|
|
const VIEW_SPECIAL_KEY_MAP_DEFAULT = require('./view').VIEW_SPECIAL_KEY_MAP_DEFAULT;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.EditTextView = EditTextView;
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2022-04-08 21:19:00 +00:00
|
|
|
const EDIT_TEXT_VIEW_KEY_MAP = Object.assign({}, VIEW_SPECIAL_KEY_MAP_DEFAULT, {
|
|
|
|
delete : [ 'delete', 'ctrl + d' ], // https://www.tecmint.com/linux-command-line-bash-shortcut-keys/
|
|
|
|
});
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function EditTextView(options) {
|
2018-06-23 03:26:46 +00:00
|
|
|
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
|
|
|
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
|
|
|
|
options.cursorStyle = miscUtil.valueWithDefault(options.cursorStyle, 'steady block');
|
|
|
|
options.resizable = false;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-04-08 21:19:00 +00:00
|
|
|
if(!_.isObject(options.specialKeyMap)) {
|
|
|
|
options.specialKeyMap = EDIT_TEXT_VIEW_KEY_MAP;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
TextView.call(this, options);
|
2015-04-08 05:15:34 +00:00
|
|
|
|
2018-06-24 04:23:22 +00:00
|
|
|
this.initDefaultWidth();
|
2018-06-22 05:15:04 +00:00
|
|
|
this.cursorPos = { row : 0, col : 0 };
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.clientBackspace = function() {
|
2022-04-08 21:19:00 +00:00
|
|
|
this.text = this.text.substr(0, this.text.length - 1);
|
|
|
|
|
|
|
|
if(this.text.length >= this.dimens.width) {
|
|
|
|
this.redraw();
|
|
|
|
} else {
|
|
|
|
this.cursorPos.col -= 1;
|
|
|
|
if(this.cursorPos.col >= 0) {
|
|
|
|
const fillCharSGR = this.getStyleSGR(1) || this.getSGR();
|
|
|
|
this.client.term.write(`\b${fillCharSGR}${this.fillChar}\b${this.getFocusSGR()}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 05:41:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 23:17:13 +00:00
|
|
|
require('util').inherits(EditTextView, TextView);
|
2014-10-23 05:41:00 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
EditTextView.prototype.onKeyPress = function(ch, key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(key) {
|
|
|
|
if(this.isKeyMapped('backspace', key.name)) {
|
|
|
|
if(this.text.length > 0) {
|
2022-04-08 21:19:00 +00:00
|
|
|
this.clientBackspace();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return EditTextView.super_.prototype.onKeyPress.call(this, ch, key);
|
2022-04-08 21:19:00 +00:00
|
|
|
} else if (this.isKeyMapped('delete', key.name)) {
|
|
|
|
// Some (mostly older) terms send 'delete' for Backspace.
|
|
|
|
// if we're at the end of the line, go ahead and treat them the same
|
|
|
|
if (this.text.length > 0 && this.cursorPos.col === this.text.length) {
|
|
|
|
this.clientBackspace();
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
} else if(this.isKeyMapped('clearLine', key.name)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
this.text = '';
|
|
|
|
this.cursorPos.col = 0;
|
|
|
|
this.setFocus(true); // resetting focus will redraw & adjust cursor
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return EditTextView.super_.prototype.onKeyPress.call(this, ch, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ch && strUtil.isPrintable(ch)) {
|
|
|
|
if(this.text.length < this.maxLength) {
|
|
|
|
ch = strUtil.stylizeString(ch, this.textStyle);
|
|
|
|
|
|
|
|
this.text += ch;
|
|
|
|
|
|
|
|
if(this.text.length > this.dimens.width) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// no shortcuts - redraw the view
|
2018-06-22 05:15:04 +00:00
|
|
|
this.redraw();
|
|
|
|
} else {
|
|
|
|
this.cursorPos.col += 1;
|
|
|
|
|
|
|
|
if(_.isString(this.textMaskChar)) {
|
|
|
|
if(this.textMaskChar.length > 0) {
|
|
|
|
this.client.term.write(this.textMaskChar);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.client.term.write(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditTextView.super_.prototype.onKeyPress.call(this, ch, key);
|
2015-06-05 22:20:26 +00:00
|
|
|
};
|
|
|
|
|
2015-06-29 04:31:12 +00:00
|
|
|
EditTextView.prototype.setText = function(text) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// draw & set |text|
|
2018-06-22 05:15:04 +00:00
|
|
|
EditTextView.super_.prototype.setText.call(this, text);
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// adjust local cursor tracking
|
2018-06-22 05:15:04 +00:00
|
|
|
this.cursorPos = { row : 0, col : text.length };
|
2015-06-05 22:20:26 +00:00
|
|
|
};
|