2015-06-01 03:18:23 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const View = require('./view.js').View;
|
|
|
|
const strUtil = require('./string_util.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const wordWrapText = require('./word_wrap.js').wordWrapText;
|
|
|
|
const ansiPrep = require('./ansi_prep.js');
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const _ = require('lodash');
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Determine CTRL-* keys for various things
|
|
|
|
// See http://www.bbsdocumentary.com/library/PROGRAMS/GRAPHICS/ANSI/bansi.txt
|
|
|
|
// http://wiki.synchro.net/howto:editor:slyedit#edit_mode
|
|
|
|
// http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/reference/keyboard_shortcuts_win.html
|
2015-06-05 22:20:26 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
/* Mystic
|
2018-06-23 03:26:46 +00:00
|
|
|
[^B] Reformat Paragraph [^O] Show this help file
|
2015-06-05 22:20:26 +00:00
|
|
|
[^I] Insert tab space [^Q] Enter quote mode
|
|
|
|
[^K] Cut current line of text [^V] Toggle insert/overwrite
|
|
|
|
[^U] Paste previously cut text [^Y] Delete current line
|
|
|
|
|
|
|
|
|
|
|
|
BASIC MOVEMENT COMMANDS
|
|
|
|
|
|
|
|
UP/^E LEFT/^S PGUP/^R HOME/^F
|
|
|
|
DOWN/^X RIGHT/^D PGDN/^C END/^G
|
|
|
|
*/
|
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Some other interesting implementations, resources, etc.
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Editors - BBS
|
|
|
|
// * https://github.com/M-griffin/Enthral/blob/master/src/msg_fse.cpp
|
2016-06-27 07:29:17 +00:00
|
|
|
//
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Editors - Other
|
|
|
|
// * http://joe-editor.sourceforge.net/
|
|
|
|
// * http://www.jbox.dk/downloads/edit.c
|
|
|
|
// * https://github.com/dominictarr/hipster
|
2015-07-13 19:41:21 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Implementations - Word Wrap
|
|
|
|
// * https://github.com/protomouse/synchronet/blob/93b01c55b3102ebc3c4f4793c3a45b8c13d0dc2a/src/sbbs3/wordwrap.c
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Misc notes
|
|
|
|
// * https://github.com/dominictarr/hipster/issues/15 (Deleting lines/etc.)
|
2015-07-02 02:35:03 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Blessed
|
|
|
|
// insertLine: CSR(top, bottom) + CUP(y, 0) + IL(1) + CSR(0, height)
|
|
|
|
// deleteLine: CSR(top, bottom) + CUP(y, 0) + DL(1) + CSR(0, height)
|
|
|
|
// Quick Ansi -- update only what was changed:
|
|
|
|
// https://github.com/dominictarr/quickansi
|
2015-07-02 02:35:03 +00:00
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// To-Do
|
2016-06-27 07:29:17 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// * Index pos % for emit scroll events
|
|
|
|
// * Some of this should be async'd where there is lots of processing (e.g. word wrap)
|
|
|
|
// * Fix backspace when col=0 (e.g. bs to prev line)
|
|
|
|
// * Add word delete (CTRL+????)
|
|
|
|
// *
|
2015-07-02 02:35:03 +00:00
|
|
|
|
2017-09-01 23:42:47 +00:00
|
|
|
const SPECIAL_KEY_MAP_DEFAULT = {
|
2022-06-05 20:04:25 +00:00
|
|
|
'line feed': ['return'],
|
|
|
|
exit: ['esc'],
|
|
|
|
backspace: ['backspace', 'ctrl + d'], // https://www.tecmint.com/linux-command-line-bash-shortcut-keys/
|
|
|
|
delete: ['delete'],
|
|
|
|
tab: ['tab'],
|
|
|
|
up: ['up arrow'],
|
|
|
|
down: ['down arrow'],
|
|
|
|
end: ['end'],
|
|
|
|
home: ['home'],
|
|
|
|
left: ['left arrow'],
|
|
|
|
right: ['right arrow'],
|
|
|
|
'delete line': ['ctrl + y', 'ctrl + u'], // https://en.wikipedia.org/wiki/Backspace
|
|
|
|
'page up': ['page up'],
|
|
|
|
'page down': ['page down'],
|
|
|
|
insert: ['insert', 'ctrl + v'],
|
2015-06-04 04:00:33 +00:00
|
|
|
};
|
2015-06-03 04:18:00 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.MultiLineEditTextView = MultiLineEditTextView;
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
function MultiLineEditTextView(options) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isBoolean(options.acceptsFocus)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.acceptsFocus = true;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isBoolean(this.acceptsInput)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.acceptsInput = true;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(options.specialKeyMap)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.specialKeyMap = SPECIAL_KEY_MAP_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
View.call(this, options);
|
|
|
|
|
2018-06-24 04:23:22 +00:00
|
|
|
this.initDefaultWidth();
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// ANSI seems to want tabs to default to 8 characters. See the following:
|
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs2/control_chars/
|
|
|
|
// * http://www.bbsdocumentary.com/library/PROGRAMS/GRAPHICS/ANSI/bansi.txt
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// This seems overkill though, so let's default to 4 :)
|
|
|
|
// :TODO: what shoudl this really be? Maybe 8 is OK
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
this.tabWidth = _.isNumber(options.tabWidth) ? options.tabWidth : 4;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.textLines = [];
|
|
|
|
this.topVisibleIndex = 0;
|
|
|
|
this.mode = options.mode || 'edit'; // edit | preview | read-only
|
2023-02-04 18:51:47 +00:00
|
|
|
this.maxLength = 0; // no max by default
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if ('preview' === this.mode) {
|
|
|
|
this.autoScroll = options.autoScroll || true;
|
|
|
|
this.tabSwitchesView = true;
|
|
|
|
} else {
|
|
|
|
this.autoScroll = options.autoScroll || false;
|
|
|
|
this.tabSwitchesView = options.tabSwitchesView || false;
|
|
|
|
}
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// cursorPos represents zero-based row, col positions
|
|
|
|
// within the editor itself
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
this.cursorPos = { col: 0, row: 0 };
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getSGRFor = function (sgrFor) {
|
|
|
|
return (
|
|
|
|
{
|
|
|
|
text: self.getSGR(),
|
|
|
|
}[sgrFor] || self.getSGR()
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.isEditMode = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return 'edit' === self.mode;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.isPreviewMode = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return 'preview' === self.mode;
|
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Most of the calls to this could be avoided via incrementRow(), decrementRow() that keeps track or such
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getTextLinesIndex = function (row) {
|
|
|
|
if (!_.isNumber(row)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
row = self.cursorPos.row;
|
|
|
|
}
|
|
|
|
var index = self.topVisibleIndex + row;
|
|
|
|
return index;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getRemainingLinesBelowRow = function (row) {
|
|
|
|
if (!_.isNumber(row)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
row = self.cursorPos.row;
|
|
|
|
}
|
|
|
|
return self.textLines.length - (self.topVisibleIndex + row) - 1;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getNextEndOfLineIndex = function (startIndex) {
|
|
|
|
for (var i = startIndex; i < self.textLines.length; i++) {
|
|
|
|
if (self.textLines[i].eol) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.textLines.length;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.toggleTextCursor = function (action) {
|
|
|
|
self.client.term.rawWrite(
|
|
|
|
`${self.getSGRFor('text')}${
|
|
|
|
'hide' === action ? ansi.hideCursor() : ansi.showCursor()
|
|
|
|
}`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.redrawRows = function (startRow, endRow) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.toggleTextCursor('hide');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const startIndex = self.getTextLinesIndex(startRow);
|
|
|
|
const endIndex = Math.min(self.getTextLinesIndex(endRow), self.textLines.length);
|
|
|
|
const absPos = self.getAbsolutePosition(startRow, 0);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
for (let i = startIndex; i < endIndex; ++i) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//${self.getSGRFor('text')}
|
|
|
|
self.client.term.write(
|
|
|
|
`${ansi.goto(absPos.row++, absPos.col)}${self.getRenderText(i)}`,
|
2022-06-05 20:04:25 +00:00
|
|
|
false // convertLineFeeds
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.toggleTextCursor('show');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return absPos.row - self.position.row; // row we ended on
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.eraseRows = function (startRow, endRow) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.toggleTextCursor('hide');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const absPos = self.getAbsolutePosition(startRow, 0);
|
|
|
|
const absPosEnd = self.getAbsolutePosition(endRow, 0);
|
|
|
|
const eraseFiller = ' '.repeat(self.dimens.width); //new Array(self.dimens.width).join(' ');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
while (absPos.row < absPosEnd.row) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.term.write(
|
|
|
|
`${ansi.goto(absPos.row++, absPos.col)}${eraseFiller}`,
|
2022-06-05 20:04:25 +00:00
|
|
|
false // convertLineFeeds
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.toggleTextCursor('show');
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.redrawVisibleArea = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(self.topVisibleIndex <= self.textLines.length);
|
|
|
|
const lastRow = self.redrawRows(0, self.dimens.height);
|
|
|
|
|
|
|
|
self.eraseRows(lastRow, self.dimens.height);
|
|
|
|
/*
|
2015-06-19 04:17:51 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TOOD: create eraseRows(startRow, endRow)
|
|
|
|
if(lastRow < self.dimens.height) {
|
|
|
|
var absPos = self.getAbsolutePosition(lastRow, 0);
|
|
|
|
var empty = new Array(self.dimens.width).join(' ');
|
|
|
|
while(lastRow++ < self.dimens.height) {
|
|
|
|
self.client.term.write(ansi.goto(absPos.row++, absPos.col));
|
|
|
|
self.client.term.write(empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getVisibleText = function (index) {
|
|
|
|
if (!_.isNumber(index)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
index = self.getTextLinesIndex();
|
|
|
|
}
|
|
|
|
return self.textLines[index].text.replace(/\t/g, ' ');
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getText = function (index) {
|
|
|
|
if (!_.isNumber(index)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
index = self.getTextLinesIndex();
|
|
|
|
}
|
|
|
|
return self.textLines.length > index ? self.textLines[index].text : '';
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getTextLength = function (index) {
|
|
|
|
if (!_.isNumber(index)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
index = self.getTextLinesIndex();
|
|
|
|
}
|
|
|
|
return self.textLines.length > index ? self.textLines[index].text.length : 0;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getCharacter = function (index, col) {
|
|
|
|
if (!_.isNumber(col)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
col = self.cursorPos.col;
|
|
|
|
}
|
|
|
|
return self.getText(index).charAt(col);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.isTab = function (index, col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return '\t' === self.getCharacter(index, col);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getTextEndOfLineColumn = function (index) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return Math.max(0, self.getTextLength(index));
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getRenderText = function (index) {
|
|
|
|
let text = self.getVisibleText(index);
|
|
|
|
const remain = self.dimens.width - strUtil.renderStringLength(text);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (remain > 0) {
|
|
|
|
text += ' '.repeat(remain); // + 1);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getTextLines = function (startIndex, endIndex) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var lines;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (startIndex === endIndex) {
|
|
|
|
lines = [self.textLines[startIndex]];
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
lines = self.textLines.slice(startIndex, endIndex + 1); // "slice extracts up to but not including end."
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getOutputText = function (startIndex, endIndex, eolMarker, options) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const lines = self.getTextLines(startIndex, endIndex);
|
2022-06-05 20:04:25 +00:00
|
|
|
let text = '';
|
|
|
|
const re = new RegExp('\\t{1,' + self.tabWidth + '}', 'g');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
lines.forEach(line => {
|
|
|
|
text += line.text.replace(re, '\t');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.forceLineTerms || (eolMarker && line.eol)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
text += eolMarker;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getContiguousText = function (startIndex, endIndex, includeEol) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var lines = self.getTextLines(startIndex, endIndex);
|
|
|
|
var text = '';
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var i = 0; i < lines.length; ++i) {
|
2018-06-22 05:15:04 +00:00
|
|
|
text += lines[i].text;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (includeEol && lines[i].eol) {
|
2018-06-22 05:15:04 +00:00
|
|
|
text += '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2023-02-04 18:51:47 +00:00
|
|
|
this.getCharacterLength = function () {
|
|
|
|
// :TODO: FSE needs re-write anyway, but this should just be known all the time vs calc. Too much of a mess right now...
|
|
|
|
let len = 0;
|
|
|
|
this.textLines.forEach(tl => {
|
|
|
|
len += tl.text.length;
|
|
|
|
});
|
|
|
|
return len;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.replaceCharacterInText = function (c, index, col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.textLines[index].text = strUtil.replaceAt(
|
2022-06-05 20:04:25 +00:00
|
|
|
self.textLines[index].text,
|
|
|
|
col,
|
|
|
|
c
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
this.editTextAtPosition = function(editAction, text, index, col) {
|
|
|
|
switch(editAction) {
|
|
|
|
case 'insert' :
|
|
|
|
self.insertCharactersInText(text, index, col);
|
|
|
|
break;
|
2015-06-15 03:19:25 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
case 'deleteForward' :
|
|
|
|
break;
|
2015-06-15 03:19:25 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
case 'deleteBack' :
|
|
|
|
break;
|
2015-06-15 03:19:25 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
case 'replace' :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
2015-06-15 21:54:33 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.updateTextWordWrap = function (index) {
|
|
|
|
const nextEolIndex = self.getNextEndOfLineIndex(index);
|
|
|
|
const wrapped = self.wordWrapSingleLine(
|
|
|
|
self.getContiguousText(index, nextEolIndex),
|
|
|
|
'tabsIntact'
|
|
|
|
);
|
|
|
|
const newLines = wrapped.wrapped.map(l => {
|
|
|
|
return { text: l };
|
|
|
|
});
|
2015-06-16 06:27:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
newLines[newLines.length - 1].eol = true;
|
2015-06-16 06:27:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
Array.prototype.splice.apply(
|
|
|
|
self.textLines,
|
2022-06-05 20:04:25 +00:00
|
|
|
[index, nextEolIndex - index + 1].concat(newLines)
|
|
|
|
);
|
2015-06-16 06:27:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return wrapped.firstWrapRange;
|
|
|
|
};
|
2015-06-16 06:27:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.removeCharactersFromText = function (index, col, operation, count) {
|
|
|
|
if ('delete' === operation) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.textLines[index].text =
|
2018-06-23 03:26:46 +00:00
|
|
|
self.textLines[index].text.slice(0, col) +
|
|
|
|
self.textLines[index].text.slice(col + count);
|
2015-06-16 04:53:49 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.updateTextWordWrap(index);
|
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
self.moveClientCursorToCursorPos();
|
|
|
|
} else if ('backspace' === operation) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: method for splicing text
|
2018-06-22 05:15:04 +00:00
|
|
|
self.textLines[index].text =
|
2018-06-23 03:26:46 +00:00
|
|
|
self.textLines[index].text.slice(0, col - (count - 1)) +
|
|
|
|
self.textLines[index].text.slice(col + 1);
|
2015-06-16 04:53:49 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.cursorPos.col -= count - 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.updateTextWordWrap(index);
|
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
|
|
|
|
self.moveClientCursorToCursorPos();
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('delete line' === operation) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Delete a visible line. Note that this is *not* the "physical" line, or
|
|
|
|
// 1:n entries up to eol! This is to keep consistency with home/end, and
|
|
|
|
// some other text editors such as nano. Sublime for example want to
|
|
|
|
// treat all of these things using the physical approach, but this seems
|
|
|
|
// a bit odd in this context.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
var isLastLine = index === self.textLines.length - 1;
|
|
|
|
var hadEol = self.textLines[index].eol;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.textLines.splice(index, 1);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (hadEol && self.textLines.length > index && !self.textLines[index].eol) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.textLines[index].eol = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Create a empty edit buffer if necessary
|
|
|
|
// :TODO: Make this a method
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.textLines.length < 1) {
|
|
|
|
self.textLines = [{ text: '', eol: true }];
|
2018-06-23 03:26:46 +00:00
|
|
|
isLastLine = false; // resetting
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.cursorPos.col = 0;
|
|
|
|
|
|
|
|
var lastRow = self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
self.eraseRows(lastRow, self.dimens.height);
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If we just deleted the last line in the buffer, move up
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (isLastLine) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorEndOfPreviousLine();
|
|
|
|
} else {
|
|
|
|
self.moveClientCursorToCursorPos();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.insertCharactersInText = function (c, index, col) {
|
|
|
|
const prevTextLength = self.getTextLength(index);
|
|
|
|
let editingEol = self.cursorPos.col === prevTextLength;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.textLines[index].text = [
|
|
|
|
self.textLines[index].text.slice(0, col),
|
|
|
|
c,
|
2022-06-05 20:04:25 +00:00
|
|
|
self.textLines[index].text.slice(col),
|
2018-06-22 05:15:04 +00:00
|
|
|
].join('');
|
|
|
|
|
|
|
|
self.cursorPos.col += c.length;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.getTextLength(index) > self.dimens.width) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Update word wrapping and |cursorOffset| if the cursor
|
|
|
|
// was within the bounds of the wrapped text
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
let cursorOffset;
|
2022-06-05 20:04:25 +00:00
|
|
|
const lastCol = self.cursorPos.col - c.length;
|
|
|
|
const firstWrapRange = self.updateTextWordWrap(index);
|
|
|
|
if (lastCol >= firstWrapRange.start && lastCol <= firstWrapRange.end) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cursorOffset = self.cursorPos.col - firstWrapRange.start;
|
2022-06-05 20:04:25 +00:00
|
|
|
editingEol = true; //override
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
cursorOffset = firstWrapRange.end;
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// redraw from current row to end of visible area
|
2018-06-22 05:15:04 +00:00
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// If we're editing mid, we're done here. Else, we need to
|
|
|
|
// move the cursor to the new editing position after a wrap
|
2022-06-05 20:04:25 +00:00
|
|
|
if (editingEol) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorBeginOfNextLine();
|
|
|
|
self.cursorPos.col += cursorOffset;
|
|
|
|
self.client.term.rawWrite(ansi.right(cursorOffset));
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// adjust cursor after drawing new rows
|
2022-06-05 20:04:25 +00:00
|
|
|
const absPos = self.getAbsolutePosition(
|
|
|
|
self.cursorPos.row,
|
|
|
|
self.cursorPos.col
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.term.rawWrite(ansi.goto(absPos.row, absPos.col));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We must only redraw from col -> end of current visible line
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
const absPos = self.getAbsolutePosition(
|
|
|
|
self.cursorPos.row,
|
|
|
|
self.cursorPos.col
|
|
|
|
);
|
|
|
|
const renderText = self
|
|
|
|
.getRenderText(index)
|
|
|
|
.slice(self.cursorPos.col - c.length);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.client.term.write(
|
2022-06-05 20:04:25 +00:00
|
|
|
`${ansi.hideCursor()}${self.getSGRFor('text')}${renderText}${ansi.goto(
|
|
|
|
absPos.row,
|
|
|
|
absPos.col
|
|
|
|
)}${ansi.showCursor()}`,
|
|
|
|
false // convertLineFeeds
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getRemainingTabWidth = function (col) {
|
|
|
|
if (!_.isNumber(col)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
col = self.cursorPos.col;
|
|
|
|
}
|
|
|
|
return self.tabWidth - (col % self.tabWidth);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.calculateTabStops = function () {
|
|
|
|
self.tabStops = [0];
|
2018-06-22 05:15:04 +00:00
|
|
|
var col = 0;
|
2022-06-05 20:04:25 +00:00
|
|
|
while (col < self.dimens.width) {
|
2018-06-22 05:15:04 +00:00
|
|
|
col += self.getRemainingTabWidth(col);
|
|
|
|
self.tabStops.push(col);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getNextTabStop = function (col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var i = self.tabStops.length;
|
2022-06-05 20:04:25 +00:00
|
|
|
while (self.tabStops[--i] > col);
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.tabStops[++i];
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getPrevTabStop = function (col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var i = self.tabStops.length;
|
2022-06-05 20:04:25 +00:00
|
|
|
while (self.tabStops[--i] >= col);
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.tabStops[i];
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.expandTab = function (col, expandChar) {
|
2018-06-22 05:15:04 +00:00
|
|
|
expandChar = expandChar || ' ';
|
|
|
|
return new Array(self.getRemainingTabWidth(col)).join(expandChar);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.wordWrapSingleLine = function (line, tabHandling = 'expand') {
|
|
|
|
return wordWrapText(line, {
|
|
|
|
width: self.dimens.width,
|
|
|
|
tabHandling: tabHandling,
|
|
|
|
tabWidth: self.tabWidth,
|
|
|
|
tabChar: '\t',
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.setTextLines = function (lines, index, termWithEol) {
|
|
|
|
if (
|
|
|
|
0 === index &&
|
|
|
|
(0 === self.textLines.length ||
|
|
|
|
(self.textLines.length === 1 && '' === self.textLines[0].text))
|
|
|
|
) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// quick path: just set the things
|
2022-06-05 20:04:25 +00:00
|
|
|
self.textLines = lines
|
|
|
|
.slice(0, -1)
|
|
|
|
.map(l => {
|
|
|
|
return { text: l };
|
|
|
|
})
|
|
|
|
.concat({ text: lines[lines.length - 1], eol: termWithEol });
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// insert somewhere in textLines...
|
2022-06-05 20:04:25 +00:00
|
|
|
if (index > self.textLines.length) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// fill with empty
|
2018-06-22 05:15:04 +00:00
|
|
|
self.textLines.splice(
|
|
|
|
self.textLines.length,
|
|
|
|
0,
|
2022-06-05 20:04:25 +00:00
|
|
|
...Array.from({ length: index - self.textLines.length }).map(() => {
|
|
|
|
return { text: '' };
|
|
|
|
})
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const newLines = lines
|
|
|
|
.slice(0, -1)
|
|
|
|
.map(l => {
|
|
|
|
return { text: l };
|
|
|
|
})
|
|
|
|
.concat({ text: lines[lines.length - 1], eol: termWithEol });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.textLines.splice(index, 0, ...newLines);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.setAnsiWithOptions = function (ansi, options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
function setLines(text) {
|
|
|
|
text = strUtil.splitTextAtTerms(text);
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
text.forEach(line => {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.setTextLines([line], index, true); // true=termWithEol
|
2018-06-22 05:15:04 +00:00
|
|
|
index += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
self.cursorStartOfDocument();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.prepped) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return setLines(ansi);
|
|
|
|
}
|
|
|
|
|
|
|
|
ansiPrep(
|
|
|
|
ansi,
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
termWidth: options.termWidth || this.client.term.termWidth,
|
|
|
|
termHeight: options.termHeight || this.client.term.termHeight,
|
|
|
|
cols: this.dimens.width,
|
|
|
|
rows: 'auto',
|
|
|
|
startCol: this.position.col,
|
|
|
|
forceLineTerm: options.forceLineTerm,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
(err, preppedAnsi) => {
|
|
|
|
return setLines(err ? ansi : preppedAnsi);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.insertRawText = function (text, index, col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Perform the following on |text|:
|
|
|
|
// * Normalize various line feed formats -> \n
|
|
|
|
// * Remove some control characters (e.g. \b)
|
|
|
|
// * Word wrap lines such that they fit in the visible workspace.
|
|
|
|
// Each actual line will then take 1:n elements in textLines[].
|
|
|
|
// * Each tab will be appropriately expanded and take 1:n \t
|
|
|
|
// characters. This allows us to know when we're in tab space
|
|
|
|
// when doing cursor movement/etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Try to handle any possible newline that can be fed to us.
|
|
|
|
// See http://stackoverflow.com/questions/5034781/js-regex-to-split-by-line
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: support index/col insertion point
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(index)) {
|
|
|
|
if (_.isNumber(col)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Modify text to have information from index
|
|
|
|
// before and and after column
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Need to clean this string (e.g. collapse tabs)
|
2018-06-22 05:15:04 +00:00
|
|
|
text = self.textLines;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Remove original line @ index
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index = self.textLines.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
text = strUtil.splitTextAtTerms(text);
|
|
|
|
|
|
|
|
let wrapped;
|
|
|
|
text.forEach(line => {
|
|
|
|
wrapped = self.wordWrapSingleLine(line, 'expand').wrapped;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.setTextLines(wrapped, index, true); // true=termWithEol
|
2018-06-22 05:15:04 +00:00
|
|
|
index += wrapped.length;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getAbsolutePosition = function (row, col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
row: self.position.row + row,
|
|
|
|
col: self.position.col + col,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.moveClientCursorToCursorPos = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
var absPos = self.getAbsolutePosition(self.cursorPos.row, self.cursorPos.col);
|
|
|
|
self.client.term.rawWrite(ansi.goto(absPos.row, absPos.col));
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressCharacter = function (c) {
|
2023-02-04 18:51:47 +00:00
|
|
|
if (this.maxLength > 0 && this.getCharacterLength() + 1 >= this.maxLength) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
var index = self.getTextLinesIndex();
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: stuff that needs to happen
|
|
|
|
// * Break up into smaller methods
|
|
|
|
// * Even in overtype mode, word wrapping must apply if past bounds
|
|
|
|
// * A lot of this can be used for backspacing also
|
|
|
|
// * See how Sublime treats tabs in *non* overtype mode... just overwrite them?
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.overtypeMode) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: special handing for insert over eol mark?
|
2018-06-22 05:15:04 +00:00
|
|
|
self.replaceCharacterInText(c, index, self.cursorPos.col);
|
|
|
|
self.cursorPos.col++;
|
|
|
|
self.client.term.write(c);
|
|
|
|
} else {
|
|
|
|
self.insertCharactersInText(c, index, self.cursorPos.col);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressUp = function () {
|
|
|
|
if (self.cursorPos.row > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorPos.row--;
|
|
|
|
self.client.term.rawWrite(ansi.up());
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.adjustCursorToNextTab('up')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.adjustCursorIfPastEndOfLine(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.scrollDocumentDown();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressDown = function () {
|
|
|
|
var lastVisibleRow =
|
|
|
|
Math.min(self.dimens.height, self.textLines.length - self.topVisibleIndex) -
|
|
|
|
1;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.cursorPos.row < lastVisibleRow) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorPos.row++;
|
|
|
|
self.client.term.rawWrite(ansi.down());
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.adjustCursorToNextTab('down')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.adjustCursorIfPastEndOfLine(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.scrollDocumentUp();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressLeft = function () {
|
|
|
|
if (self.cursorPos.col > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var prevCharIsTab = self.isTab();
|
|
|
|
|
|
|
|
self.cursorPos.col--;
|
|
|
|
self.client.term.rawWrite(ansi.left());
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (prevCharIsTab) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.adjustCursorToNextTab('left');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.cursorEndOfPreviousLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressRight = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
var eolColumn = self.getTextEndOfLineColumn();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.cursorPos.col < eolColumn) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var prevCharIsTab = self.isTab();
|
|
|
|
|
|
|
|
self.cursorPos.col++;
|
|
|
|
self.client.term.rawWrite(ansi.right());
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (prevCharIsTab) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.adjustCursorToNextTab('right');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.cursorBeginOfNextLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressHome = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
var firstNonWhitespace = self.getVisibleText().search(/\S/);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (-1 !== firstNonWhitespace) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorPos.col = firstNonWhitespace;
|
|
|
|
} else {
|
|
|
|
self.cursorPos.col = 0;
|
|
|
|
}
|
|
|
|
self.moveClientCursorToCursorPos();
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressEnd = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorPos.col = self.getTextEndOfLineColumn();
|
|
|
|
self.moveClientCursorToCursorPos();
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressPageUp = function () {
|
|
|
|
if (self.topVisibleIndex > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.topVisibleIndex = Math.max(0, self.topVisibleIndex - self.dimens.height);
|
|
|
|
self.redraw();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
|
|
|
} else {
|
|
|
|
self.cursorPos.row = 0;
|
2018-06-23 03:26:46 +00:00
|
|
|
self.moveClientCursorToCursorPos(); // :TODO: ajust if eol, etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressPageDown = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (linesBelow > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.topVisibleIndex += Math.min(linesBelow, self.dimens.height);
|
|
|
|
self.redraw();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressLineFeed = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Break up text from cursor position, redraw, and update cursor
|
|
|
|
// position to start of next line
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
var index = self.getTextLinesIndex();
|
|
|
|
var nextEolIndex = self.getNextEndOfLineIndex(index);
|
|
|
|
var text = self.getContiguousText(index, nextEolIndex);
|
|
|
|
const newLines = self.wordWrapSingleLine(
|
|
|
|
text.slice(self.cursorPos.col),
|
|
|
|
'tabsIntact'
|
|
|
|
).wrapped;
|
|
|
|
|
|
|
|
newLines.unshift({ text: text.slice(0, self.cursorPos.col), eol: true });
|
|
|
|
for (var i = 1; i < newLines.length; ++i) {
|
|
|
|
newLines[i] = { text: newLines[i] };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
newLines[newLines.length - 1].eol = true;
|
|
|
|
|
|
|
|
Array.prototype.splice.apply(
|
|
|
|
self.textLines,
|
2022-06-05 20:04:25 +00:00
|
|
|
[index, nextEolIndex - index + 1].concat(newLines)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// redraw from current row to end of visible area
|
2018-06-22 05:15:04 +00:00
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
self.cursorBeginOfNextLine();
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressInsert = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.toggleTextEditMode();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressTab = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
var index = self.getTextLinesIndex();
|
2022-06-05 20:04:25 +00:00
|
|
|
self.insertCharactersInText(
|
|
|
|
self.expandTab(self.cursorPos.col, '\t') + '\t',
|
|
|
|
index,
|
|
|
|
self.cursorPos.col
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressBackspace = function () {
|
|
|
|
if (self.cursorPos.col >= 1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Don't want to delete character at cursor, but rather the character
|
|
|
|
// to the left of the cursor!
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
self.cursorPos.col -= 1;
|
|
|
|
|
|
|
|
var index = self.getTextLinesIndex();
|
|
|
|
var count;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.isTab()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var col = self.cursorPos.col;
|
|
|
|
var prevTabStop = self.getPrevTabStop(self.cursorPos.col);
|
2022-06-05 20:04:25 +00:00
|
|
|
while (col >= prevTabStop) {
|
|
|
|
if (!self.isTab(index, col)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
--col;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
count = self.cursorPos.col - col;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.removeCharactersFromText(index, self.cursorPos.col, 'backspace', count);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Delete character at end of line previous.
|
|
|
|
// * This may be a eol marker
|
|
|
|
// * Word wrapping will need re-applied
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: apply word wrapping such that text can be re-adjusted if it can now fit on prev
|
2022-06-05 20:04:25 +00:00
|
|
|
self.keyPressLeft(); // same as hitting left - jump to previous line
|
2018-06-22 05:15:04 +00:00
|
|
|
//self.keyPressBackspace();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressDelete = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
const lineIndex = self.getTextLinesIndex();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
0 === self.cursorPos.col &&
|
|
|
|
0 === self.textLines[lineIndex].text.length &&
|
|
|
|
self.textLines.length > 0
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Start of line and nothing left. Just delete the line
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
self.removeCharactersFromText(lineIndex, 0, 'delete line');
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.removeCharactersFromText(lineIndex, self.cursorPos.col, 'delete', 1);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.keyPressDeleteLine = function () {
|
|
|
|
if (self.textLines.length > 0) {
|
|
|
|
self.removeCharactersFromText(self.getTextLinesIndex(), 0, 'delete line');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.emitEditPosition();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.adjustCursorIfPastEndOfLine = function (forceUpdate) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var eolColumn = self.getTextEndOfLineColumn();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.cursorPos.col > eolColumn) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorPos.col = eolColumn;
|
|
|
|
forceUpdate = true;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (forceUpdate) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.adjustCursorToNextTab = function (direction) {
|
|
|
|
if (self.isTab()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var move;
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (direction) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Next tabstop to the right
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'right':
|
2018-06-22 05:15:04 +00:00
|
|
|
move = self.getNextTabStop(self.cursorPos.col) - self.cursorPos.col;
|
|
|
|
self.cursorPos.col += move;
|
|
|
|
self.client.term.rawWrite(ansi.right(move));
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
//
|
|
|
|
// Next tabstop to the left
|
|
|
|
//
|
|
|
|
case 'left':
|
2018-06-22 05:15:04 +00:00
|
|
|
move = self.cursorPos.col - self.getPrevTabStop(self.cursorPos.col);
|
|
|
|
self.cursorPos.col -= move;
|
|
|
|
self.client.term.rawWrite(ansi.left(move));
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'up':
|
|
|
|
case 'down':
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Jump to the tabstop nearest the cursor
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
var newCol = self.tabStops.reduce(function r(prev, curr) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return Math.abs(curr - self.cursorPos.col) <
|
|
|
|
Math.abs(prev - self.cursorPos.col)
|
|
|
|
? curr
|
|
|
|
: prev;
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (newCol > self.cursorPos.col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
move = newCol - self.cursorPos.col;
|
|
|
|
self.cursorPos.col += move;
|
|
|
|
self.client.term.rawWrite(ansi.right(move));
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (newCol < self.cursorPos.col) {
|
2018-06-22 05:15:04 +00:00
|
|
|
move = self.cursorPos.col - newCol;
|
|
|
|
self.cursorPos.col -= move;
|
|
|
|
self.client.term.rawWrite(ansi.left(move));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return false; // did not fall on a tab
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.cursorStartOfDocument = function () {
|
|
|
|
self.topVisibleIndex = 0;
|
|
|
|
self.cursorPos = { row: 0, col: 0 };
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
self.moveClientCursorToCursorPos();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.cursorEndOfDocument = function () {
|
|
|
|
self.topVisibleIndex = Math.max(self.textLines.length - self.dimens.height, 0);
|
|
|
|
self.cursorPos.row = self.textLines.length - self.topVisibleIndex - 1;
|
|
|
|
self.cursorPos.col = self.getTextEndOfLineColumn();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
self.moveClientCursorToCursorPos();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.cursorBeginOfNextLine = function () {
|
2018-06-23 03:26:46 +00:00
|
|
|
// e.g. when scrolling right past eol
|
2018-06-22 05:15:04 +00:00
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (linesBelow > 0) {
|
|
|
|
var lastVisibleRow = Math.min(self.dimens.height, self.textLines.length) - 1;
|
|
|
|
if (self.cursorPos.row < lastVisibleRow) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorPos.row++;
|
|
|
|
} else {
|
|
|
|
self.scrollDocumentUp();
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
self.keyPressHome(); // same as pressing 'home'
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.cursorEndOfPreviousLine = function () {
|
2018-06-23 03:26:46 +00:00
|
|
|
// e.g. when scrolling left past start of line
|
2018-06-22 05:15:04 +00:00
|
|
|
var moveToEnd;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.cursorPos.row > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.cursorPos.row--;
|
|
|
|
moveToEnd = true;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (self.topVisibleIndex > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.scrollDocumentDown();
|
|
|
|
moveToEnd = true;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (moveToEnd) {
|
2018-06-23 03:26:46 +00:00
|
|
|
self.keyPressEnd(); // same as pressing 'end'
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
this.cusorEndOfNextLine = function() {
|
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
|
|
|
|
|
|
|
if(linesBelow > 0) {
|
|
|
|
var lastVisibleRow = Math.min(self.dimens.height, self.textLines.length) - 1;
|
|
|
|
if(self.cursorPos.row < lastVisibleRow) {
|
|
|
|
self.cursorPos.row++;
|
|
|
|
} else {
|
|
|
|
self.scrollDocumentUp();
|
|
|
|
}
|
|
|
|
self.keyPressEnd(); // same as pressing 'end'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
2015-06-14 03:49:12 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.scrollDocumentUp = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Note: We scroll *up* when the cursor goes *down* beyond
|
|
|
|
// the visible area!
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (linesBelow > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.topVisibleIndex++;
|
|
|
|
self.redraw();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.scrollDocumentDown = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Note: We scroll *down* when the cursor goes *up* beyond
|
|
|
|
// the visible area!
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.topVisibleIndex > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.topVisibleIndex--;
|
|
|
|
self.redraw();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.emitEditPosition = function () {
|
|
|
|
self.emit('edit position', self.getEditPosition());
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.toggleTextEditMode = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.overtypeMode = !self.overtypeMode;
|
|
|
|
self.emit('text edit mode', self.getTextEditMode());
|
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.insertRawText(''); // init to blank/empty
|
2015-06-01 03:18:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
require('util').inherits(MultiLineEditTextView, View);
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.setWidth = function (width) {
|
2018-06-22 05:15:04 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.setWidth.call(this, width);
|
2015-06-15 03:19:25 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.calculateTabStops();
|
2015-06-15 03:19:25 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.redraw = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.redraw.call(this);
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.redrawVisibleArea();
|
2015-06-01 03:18:23 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.setFocus = function (focused) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.rawWrite(this.getSGRFor('text'));
|
|
|
|
this.moveClientCursorToCursorPos();
|
2015-06-08 22:51:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.setFocus.call(this, focused);
|
2015-06-08 22:51:27 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.setText = function (
|
|
|
|
text,
|
|
|
|
options = { scrollMode: 'default' }
|
|
|
|
) {
|
|
|
|
this.textLines = [];
|
2018-06-22 05:15:04 +00:00
|
|
|
this.addText(text, options);
|
|
|
|
/*this.insertRawText(text);
|
2015-09-16 04:44:31 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
if(this.isEditMode()) {
|
|
|
|
this.cursorEndOfDocument();
|
|
|
|
} else if(this.isPreviewMode()) {
|
|
|
|
this.cursorStartOfDocument();
|
|
|
|
}*/
|
2015-09-16 04:44:31 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.setAnsi = function (
|
|
|
|
ansi,
|
|
|
|
options = { prepped: false },
|
|
|
|
cb
|
|
|
|
) {
|
|
|
|
this.textLines = [];
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.setAnsiWithOptions(ansi, options, cb);
|
2017-08-06 17:32:32 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.addText = function (
|
|
|
|
text,
|
|
|
|
options = { scrollMode: 'default' }
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.insertRawText(text);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (options.scrollMode) {
|
|
|
|
case 'default':
|
|
|
|
if (this.isEditMode() || this.autoScroll) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.cursorEndOfDocument();
|
|
|
|
} else {
|
|
|
|
this.cursorStartOfDocument();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'top':
|
|
|
|
case 'start':
|
2018-06-22 05:15:04 +00:00
|
|
|
this.cursorStartOfDocument();
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'end':
|
|
|
|
case 'bottom':
|
2018-06-22 05:15:04 +00:00
|
|
|
this.cursorEndOfDocument();
|
|
|
|
break;
|
|
|
|
}
|
2015-06-02 05:00:54 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.getData = function (options = { forceLineTerms: false }) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.getOutputText(0, this.textLines.length, '\r\n', options);
|
2015-06-27 21:32:29 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.setPropertyValue = function (propName, value) {
|
|
|
|
switch (propName) {
|
|
|
|
case 'mode':
|
2018-06-22 05:15:04 +00:00
|
|
|
this.mode = value;
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('preview' === value && !this.specialKeyMap.next) {
|
|
|
|
this.specialKeyMap.next = ['tab'];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'autoScroll':
|
2018-06-22 05:15:04 +00:00
|
|
|
this.autoScroll = value;
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'tabSwitchesView':
|
2018-06-22 05:15:04 +00:00
|
|
|
this.tabSwitchesView = value;
|
|
|
|
this.specialKeyMap.next = this.specialKeyMap.next || [];
|
|
|
|
this.specialKeyMap.next.push('tab');
|
|
|
|
break;
|
2023-02-04 18:51:47 +00:00
|
|
|
|
|
|
|
case 'maxLength':
|
|
|
|
if (_.isNumber(value)) {
|
|
|
|
this.maxLength = value;
|
|
|
|
}
|
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MultiLineEditTextView.super_.prototype.setPropertyValue.call(this, propName, value);
|
2015-06-30 05:14:17 +00:00
|
|
|
};
|
|
|
|
|
2017-01-23 04:30:49 +00:00
|
|
|
const HANDLED_SPECIAL_KEYS = [
|
2022-06-05 20:04:25 +00:00
|
|
|
'up',
|
|
|
|
'down',
|
|
|
|
'left',
|
|
|
|
'right',
|
|
|
|
'home',
|
|
|
|
'end',
|
|
|
|
'page up',
|
|
|
|
'page down',
|
2018-06-22 05:15:04 +00:00
|
|
|
'line feed',
|
|
|
|
'insert',
|
|
|
|
'tab',
|
2022-06-05 20:04:25 +00:00
|
|
|
'backspace',
|
|
|
|
'delete',
|
2018-06-22 05:15:04 +00:00
|
|
|
'delete line',
|
2015-06-05 22:20:26 +00:00
|
|
|
];
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const PREVIEW_MODE_KEYS = ['up', 'down', 'page up', 'page down'];
|
2015-09-03 05:11:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.onKeyPress = function (ch, key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const self = this;
|
|
|
|
let handled;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
HANDLED_SPECIAL_KEYS.forEach(function aKey(specialKey) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.isKeyMapped(specialKey, key.name)) {
|
|
|
|
if (
|
|
|
|
self.isPreviewMode() &&
|
|
|
|
-1 === PREVIEW_MODE_KEYS.indexOf(specialKey)
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('tab' !== key.name || !self.tabSwitchesView) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self[_.camelCase('keyPress ' + specialKey)]();
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.isEditMode() && ch && strUtil.isPrintable(ch)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.keyPressCharacter(ch);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!handled) {
|
2018-06-22 05:15:04 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.onKeyPress.call(this, ch, key);
|
|
|
|
}
|
2015-06-05 22:20:26 +00:00
|
|
|
};
|
2015-07-06 05:16:22 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.scrollUp = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.scrollDocumentUp();
|
2016-06-28 05:01:31 +00:00
|
|
|
};
|
2016-06-27 07:29:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.scrollDown = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.scrollDocumentDown();
|
2016-06-28 05:01:31 +00:00
|
|
|
};
|
2016-06-27 07:29:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.deleteLine = function (line) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.textLines.splice(line, 1);
|
2016-06-28 05:01:31 +00:00
|
|
|
};
|
2016-06-27 07:29:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.getLineCount = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.textLines.length;
|
2016-06-28 05:01:31 +00:00
|
|
|
};
|
2016-06-27 07:29:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.getTextEditMode = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.overtypeMode ? 'overtype' : 'insert';
|
2015-07-06 05:16:22 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MultiLineEditTextView.prototype.getEditPosition = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
var currentIndex = this.getTextLinesIndex() + 1;
|
|
|
|
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
row: this.getTextLinesIndex(this.cursorPos.row),
|
|
|
|
col: this.cursorPos.col,
|
|
|
|
percent: Math.floor((currentIndex / this.textLines.length) * 100),
|
|
|
|
below: this.getRemainingLinesBelowRow(),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
2015-07-06 05:16:22 +00:00
|
|
|
};
|