enigma-bbs/core/multi_line_edit_text_view2.js

498 lines
13 KiB
JavaScript
Raw Normal View History

/* jslint node: true */
'use strict';
var View = require('./view.js').View;
var miscUtil = require('./misc_util.js');
var strUtil = require('./string_util.js');
var ansi = require('./ansi_term.js');
//var TextBuffer = require('./text_buffer.js').TextBuffer;
var assert = require('assert');
var _ = require('lodash');
// :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
/* Mystic
[^B] Reformat Paragraph [^O] Show this help file
[^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-03 04:18:00 +00:00
var SPECIAL_KEY_MAP_DEFAULT = {
lineFeed : [ 'return' ],
2015-06-03 04:18:00 +00:00
exit : [ 'esc' ],
backspace : [ 'backspace' ],
del : [ 'del' ],
tabs : [ 'tab' ],
up : [ 'up arrow' ],
down : [ 'down arrow' ],
end : [ 'end' ],
home : [ 'home' ],
left : [ 'left arrow' ],
right : [ 'right arrow' ],
clearLine : [ 'ctrl + y' ],
pageUp : [ 'page up' ],
pageDown : [ 'page down' ],
};
2015-06-03 04:18:00 +00:00
exports.MultiLineEditTextView2 = MultiLineEditTextView2;
function MultiLineEditTextView2(options) {
if(!_.isBoolean(options.acceptsFocus)) {
options.acceptsFocus = true;
}
if(!_.isBoolean(this.acceptsInput)) {
options.acceptsInput = true;
}
2015-06-03 04:18:00 +00:00
if(!_.isObject(options.specialKeyMap)) {
options.specialKeyMap = SPECIAL_KEY_MAP_DEFAULT;
}
View.call(this, options);
var self = this;
//
// 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
//
this.tabWidth = _.isNumber(options.tabWidth) ? options.tabWidth : 8;
2015-06-02 05:00:54 +00:00
this.textLines = [];
this.topVisibleIndex = 0;
2015-06-03 04:18:00 +00:00
//
// cursorPos represents zero-based row, col positions
// within the editor itself
//
2015-06-02 22:36:55 +00:00
this.cursorPos = { col : 0, row : 0 };
// :TODO: Most of the calls to this could be avoided via incrementRow(), decrementRow() that keeps track or such
2015-06-03 04:18:00 +00:00
this.getTextLinesIndex = function(row) {
if(!_.isNumber(row)) {
row = self.cursorPos.row;
}
var index = self.topVisibleIndex + row;
2015-06-03 04:18:00 +00:00
return index;
};
this.redrawVisibleArea = function() {
2015-06-02 05:00:54 +00:00
assert(self.topVisibleIndex < self.textLines.length);
2015-06-02 05:00:54 +00:00
self.client.term.write(self.getSGR());
2015-06-02 22:36:55 +00:00
self.client.term.write(ansi.hideCursor());
2015-06-02 05:00:54 +00:00
var bottomIndex = Math.min(self.topVisibleIndex + self.dimens.height, self.textLines.length);
var row = self.position.row;
for(var i = self.topVisibleIndex; i < bottomIndex; i++) {
self.client.term.write(ansi.goto(row, this.position.col));
2015-06-02 22:36:55 +00:00
self.client.term.write(self.getRenderText(i));
2015-06-02 05:00:54 +00:00
++row;
}
2015-06-02 22:36:55 +00:00
self.client.term.write(ansi.showCursor());
};
2015-06-02 05:00:54 +00:00
2015-06-02 22:36:55 +00:00
this.getVisibleText = function(index) {
if(!_.isNumber(index)) {
index = self.getTextLinesIndex();
}
2015-06-02 22:36:55 +00:00
return self.textLines[index].text.replace(/\t/g, ' ');
};
this.getText = function(index) {
if(!_.isNumber(index)) {
index = self.getTextLinesIndex();
}
return self.textLines[index].text;
};
this.getTextEndOfLineColumn = function(index) {
return Math.max(0, self.getText(index).length - 1);
};
2015-06-02 22:36:55 +00:00
this.getRenderText = function(index) {
var text = self.getVisibleText(index);
var remain = self.dimens.width - text.length;
if(remain > 0) {
text += new Array(remain).join(' ');
}
return text;
};
this.replaceCharacterInText = function(c, index, col) {
self.textLines[index].text = strUtil.replaceAt(
self.textLines[index].text, col, c);
};
this.expandTab = function(col, expandChar) {
expandChar = expandChar || ' ';
var count = self.tabWidth - (col % self.tabWidth);
return new Array(count).join(expandChar);
};
this.wordWrapSingleLine = function(s, width) {
//
// Notes
// * Sublime Text 3 for example considers spaces after a word
// part of said word. For example, "word " would be wraped
// in it's entirity.
//
// * Tabs in Sublime Text 3 are also treated as a word, so, e.g.
// "\t" may resolve to " " and must fit within the space.
//
2015-06-02 05:00:54 +00:00
// note: we cannot simply use \s below as it includes \t
var re = new RegExp(
2015-06-02 22:36:55 +00:00
'\t|[ \f\n\r\v\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006' +
'\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]+', 'g');
var m;
var wordStart;
var wrapped = [ '' ];
var i = 0;
var word;
function addWord() {
if(wrapped[i].length + word.length > self.dimens.width) {
wrapped[++i] = word;
} else {
wrapped[i] += word;
}
}
do {
wordStart = re.lastIndex + (_.isObject(m) ? m[0].length - 1 : 0);
m = re.exec(s);
if(null !== m) {
2015-06-02 05:00:54 +00:00
word = s.substring(wordStart, re.lastIndex - 1);
switch(m[0].charAt(0)) {
case ' ' :
word += m[0];
break;
case '\t' :
//
// Expand tab given position
//
word += self.expandTab(wrapped[i].length, '\t');
break;
}
addWord();
}
} while(0 !== re.lastIndex);
//
// Remainder
//
word = s.substring(wordStart);
addWord();
return wrapped;
};
// :TODO: Change this to (text, row, col) & make proper adjustments
2015-06-03 04:18:00 +00:00
this.insertText = function(text, index, col) {
//
// 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.
//
//
// Try to handle any possible newline that can be fed to us.
// See http://stackoverflow.com/questions/5034781/js-regex-to-split-by-line
//
2015-06-03 04:18:00 +00:00
// :TODO: support index/col insertion point
2015-06-02 05:00:54 +00:00
2015-06-03 04:18:00 +00:00
if(_.isNumber(index)) {
2015-06-02 05:00:54 +00:00
if(_.isNumber(col)) {
//
2015-06-03 04:18:00 +00:00
// Modify text to have information from index
2015-06-02 05:00:54 +00:00
// before and and after column
//
// :TODO: Need to clean this string (e.g. collapse tabs)
text = self.textLines
2015-06-03 04:18:00 +00:00
// :TODO: Remove original line @ index
2015-06-02 05:00:54 +00:00
}
} else {
2015-06-03 04:18:00 +00:00
index = self.textLines.length;
2015-06-02 05:00:54 +00:00
}
var tempLines = text
.replace(/\b/g, '')
.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g);
var wrapped;
2015-06-02 05:00:54 +00:00
for(var i = 0; i < tempLines.length; ++i) {
wrapped = self.wordWrapSingleLine(tempLines[i], self.dimens.width);
2015-06-02 05:00:54 +00:00
for(var j = 0; j < wrapped.length - 1; ++j) {
2015-06-03 04:18:00 +00:00
self.textLines.splice(index++, 0, { text : wrapped[j] } );
2015-06-02 05:00:54 +00:00
}
2015-06-03 04:18:00 +00:00
self.textLines.splice(index++, 0, { text : wrapped[wrapped.length - 1], eol : true });
}
};
2015-06-02 05:00:54 +00:00
2015-06-04 23:06:37 +00:00
this.getAbsolutePosition = function(row, col) {
return { row : self.position.row + self.cursorPos.row, col : self.position.col + self.cursorPos.col };
};
this.moveClientCusorToCursorPos = function() {
var absPos = self.getAbsolutePosition(self.cursorPos.row, self.cursorPos.col);
self.client.term.write(ansi.goto(absPos.row, absPos.col));
};
this.keyPressCharacter = function(c, row, col) {
var index = self.getTextLinesIndex(row);
if(!_.isNumber(col)) {
col = self.cursorPos.col;
}
// :TODO: Even in overtypeMode, word wrapping must apply for e.g.
// if a user types past bounds
if(self.overtypeMode) {
// :TODO: special handing for insert over eol mark?
self.replaceCharacterInText(c, index, col);
self.cursorPos.col++;
self.client.term.write(c);
} else {
}
};
2015-06-04 23:06:37 +00:00
this.keyPressUp = function() {
2015-06-03 04:18:00 +00:00
if(self.cursorPos.row > 0) {
self.cursorPos.row--;
self.client.term.write(ansi.up());
// :TODO: self.makeTabAdjustment('up')
self.adjustCursorIfPastEndOfLine(false);
} else {
self.scrollDocumentDown();
self.adjustCursorIfPastEndOfLine(true);
2015-06-03 04:18:00 +00:00
}
2015-06-02 05:00:54 +00:00
};
2015-06-02 22:36:55 +00:00
2015-06-04 23:06:37 +00:00
this.keyPressDown = function() {
var lastVisibleRow = Math.min(self.dimens.height, self.textLines.length) - 1;
if(self.cursorPos.row < lastVisibleRow) {
self.cursorPos.row++;
self.client.term.write(ansi.down());
// :TODO: make tab adjustment if needed
self.adjustCursorIfPastEndOfLine(false);
} else {
self.scrollDocumentUp();
self.adjustCursorIfPastEndOfLine(true);
}
2015-06-02 22:36:55 +00:00
};
2015-06-04 23:06:37 +00:00
this.keyPressLeft = function() {
2015-06-03 04:18:00 +00:00
if(self.cursorPos.col > 0) {
self.cursorPos.col--;
self.client.term.write(ansi.left());
// :TODO: handle landing on a tab
} else {
// :TODO: goto previous line if possible and scroll if needed
}
2015-06-02 22:36:55 +00:00
};
2015-06-04 23:06:37 +00:00
this.keyPressRight = function() {
var eolColumn = self.getTextEndOfLineColumn();
if(self.cursorPos.col < eolColumn) {
2015-06-02 22:36:55 +00:00
self.cursorPos.col++;
self.client.term.write(ansi.right());
2015-06-03 04:18:00 +00:00
// :TODO: handle landing on a tab
2015-06-02 22:36:55 +00:00
} else {
// :TODO: goto next line; scroll if needed, etc.
2015-06-03 04:18:00 +00:00
2015-06-02 22:36:55 +00:00
}
};
2015-06-04 23:06:37 +00:00
this.keyPressHome = function() {
2015-06-03 04:18:00 +00:00
var firstNonWhitespace = self.getVisibleText().search(/\S/);
if(-1 !== firstNonWhitespace) {
self.cursorPos.col = firstNonWhitespace;
} else {
self.cursorPos.col = 0;
}
console.log(self.getVisibleText())
self.moveClientCusorToCursorPos();
};
2015-06-04 23:06:37 +00:00
this.keyPressEnd = function() {
self.cursorPos.col = self.getTextEndOfLineColumn();
2015-06-03 04:18:00 +00:00
self.moveClientCusorToCursorPos();
};
2015-06-04 23:06:37 +00:00
this.keyPressPageUp = function() {
};
2015-06-04 23:06:37 +00:00
this.keyPressPageDown = function() {
};
this.keyPressLineFeed = function() {
};
this.adjustCursorIfPastEndOfLine = function(alwaysUpdateCursor) {
var eolColumn = self.getTextEndOfLineColumn();
if(self.cursorPos.col > eolColumn) {
self.cursorPos.col = eolColumn;
alwaysUpdateCursor = true;
}
if(alwaysUpdateCursor) {
self.moveClientCusorToCursorPos();
}
};
this.cursorStartOfDocument = function() {
2015-06-02 22:36:55 +00:00
self.topVisibleIndex = 0;
self.cursorPos = { row : 0, col : 0 };
self.redraw();
self.moveClientCusorToCursorPos();
};
this.cursorEndOfDocument = function() {
2015-06-02 22:36:55 +00:00
self.topVisibleIndex = Math.max(self.textLines.length - self.dimens.height, 0);
2015-06-03 04:18:00 +00:00
self.cursorPos.row = (self.textLines.length - self.topVisibleIndex) - 1;
self.cursorPos.col = self.getTextEndOfLineColumn();
2015-06-02 22:36:55 +00:00
self.redraw();
self.moveClientCusorToCursorPos();
};
2015-06-03 04:18:00 +00:00
this.scrollDocumentUp = function() {
//
// Note: We scroll *up* when the cursor goes *down* beyond
// the visible area!
//
var linesBelow = self.textLines.length - (self.topVisibleIndex + self.cursorPos.row) - 1;
if(linesBelow > 0) {
self.topVisibleIndex++;
self.redraw();
}
};
this.scrollDocumentDown = function() {
//
// Note: We scroll *down* when the cursor goes *up* beyond
// the visible area!
//
if(self.topVisibleIndex > 0) {
self.topVisibleIndex--;
self.redraw();
}
};
}
require('util').inherits(MultiLineEditTextView2, View);
MultiLineEditTextView2.prototype.redraw = function() {
MultiLineEditTextView2.super_.prototype.redraw.call(this);
this.redrawVisibleArea();
};
MultiLineEditTextView2.prototype.setText = function(text) {
this.textLines = [];
2015-06-02 22:36:55 +00:00
//text = 'Supper fluffy bunny test thing\nHello, everyone!\n\nStuff and thing and what nots\r\na\tb\tc\td\te';
2015-06-02 05:00:54 +00:00
//text = "You. Now \ttomorrow \tthere'll \tbe \ttwo \tsessions, \tof\t course, morning and afternoon.";
this.insertText(text);//, 0, 0);
this.cursorEndOfDocument();
this.overtypeMode = true; // :TODO: remove... testing
2015-06-02 05:00:54 +00:00
};
var HANDLED_SPECIAL_KEYS = [
'up', 'down', 'left', 'right',
'home', 'end',
'pageUp', 'pageDown',
'lineFeed',
];
MultiLineEditTextView2.prototype.onKeyPress = function(ch, key) {
var self = this;
var handled;
if(key) {
HANDLED_SPECIAL_KEYS.forEach(function aKey(specialKey) {
if(self.isSpecialKeyMapped(specialKey, key.name)) {
self[_.camelCase('keyPress ' + specialKey)]();
handled = true;
}
});
}
if(ch && strUtil.isPrintable(ch)) {
this.keyPressCharacter(ch);
}
if(!handled) {
2015-06-05 22:37:17 +00:00
MultiLineEditTextView2.super_.prototype.onKeyPress.call(this, ch, key);
}
};
/*
MultiLineEditTextView2.prototype.onKeyPress = function(key, isSpecial) {
if(isSpecial) {
return;
}
this.keyPressCharacter(key);
MultiLineEditTextView2.super_.prototype.onKeyPress.call(this, key, isSpecial);
};
2015-06-02 05:00:54 +00:00
MultiLineEditTextView2.prototype.onSpecialKeyPress = function(keyName) {
var self = this;
2015-06-03 04:18:00 +00:00
console.log(keyName);
2015-06-04 23:06:37 +00:00
var handled = false;
HANDLED_SPECIAL_KEYS.forEach(function key(arrowKey) {
2015-06-02 05:00:54 +00:00
if(self.isSpecialKeyMapped(arrowKey, keyName)) {
2015-06-04 23:06:37 +00:00
self[_.camelCase('keyPress ' + arrowKey)]();
handled = true;
2015-06-02 05:00:54 +00:00
}
});
2015-06-04 23:06:37 +00:00
if(!handled) {
MultiLineEditTextView2.super_.prototype.onSpecialKeyPress.call(this, keyName);
2015-06-03 04:18:00 +00:00
}
2015-06-02 22:36:55 +00:00
};
*/