* Improvement to word wrapping in multi line editor. Now handles spaces, etc.
This commit is contained in:
parent
019dbaf034
commit
9ead6ab0fa
|
@ -244,60 +244,57 @@ function MultiLineEditTextView2(options) {
|
||||||
//
|
//
|
||||||
var re = new RegExp(
|
var re = new RegExp(
|
||||||
'\t|[ \f\n\r\v\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006' +
|
'\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');
|
'\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]', 'g');
|
||||||
var m;
|
var m;
|
||||||
var wordStart;
|
var wordStart = 0;
|
||||||
var wrapped = [ '' ];
|
var results = { wrapped : [ '' ] };
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var word;
|
var word;
|
||||||
var firstWordWrapWordRange;
|
|
||||||
|
|
||||||
function addWord() {
|
function addWord() {
|
||||||
word.match(new RegExp('.{0,' + self.dimens.width + '}', 'g')).forEach(function wrd(w) {
|
word.match(new RegExp('.{0,' + self.dimens.width + '}', 'g')).forEach(function wrd(w) {
|
||||||
if(wrapped[i].length + w.length >= self.dimens.width) {
|
if(results.wrapped[i].length + w.length >= self.dimens.width) {
|
||||||
if(0 === i) {
|
if(0 === i) {
|
||||||
firstWordWrapWordRange = { start : wordStart, end : wordStart + w.length };
|
results.firstWrapRange = { start : wordStart, end : wordStart + w.length };
|
||||||
}
|
}
|
||||||
wrapped[++i] = w;
|
results.wrapped[++i] = w;
|
||||||
} else {
|
} else {
|
||||||
wrapped[i] += w;
|
results.wrapped[i] += w;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
while((m = re.exec(s)) !== null) {
|
||||||
wordStart = re.lastIndex + (_.isObject(m) ? m[0].length - 1 : 0);
|
word = s.substring(wordStart, re.lastIndex - 1);
|
||||||
m = re.exec(s);
|
|
||||||
|
|
||||||
if(null !== m) {
|
switch(m[0].charAt(0)) {
|
||||||
word = s.substring(wordStart, re.lastIndex - 1);
|
case ' ' :
|
||||||
|
word += m[0];
|
||||||
|
break;
|
||||||
|
|
||||||
switch(m[0].charAt(0)) {
|
case '\t' :
|
||||||
case ' ' :
|
//
|
||||||
word += m[0];
|
// Expand tab given position
|
||||||
break;
|
//
|
||||||
|
// Nice info here: http://c-for-dummies.com/blog/?p=424
|
||||||
case '\t' :
|
//
|
||||||
//
|
word += self.expandTab(results.wrapped[i].length + word.length, '\t');
|
||||||
// Expand tab given position
|
break;
|
||||||
//
|
|
||||||
// Nice info here: http://c-for-dummies.com/blog/?p=424
|
|
||||||
//
|
|
||||||
word += self.expandTab(wrapped[i].length + word.length, '\t');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
addWord();
|
|
||||||
}
|
}
|
||||||
} while(0 !== re.lastIndex);
|
|
||||||
|
addWord();
|
||||||
|
|
||||||
|
wordStart = re.lastIndex + m[0].length - 1;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Remainder
|
// Remainder
|
||||||
//
|
//
|
||||||
|
console.log(wordStart + ' / ' + s.length)
|
||||||
word = s.substring(wordStart);
|
word = s.substring(wordStart);
|
||||||
addWord();
|
addWord();
|
||||||
|
|
||||||
return { wrapped : wrapped, firstWordWrapWordRange : firstWordWrapWordRange };
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
// :TODO: Change this to (text, row, col) & make proper adjustments
|
// :TODO: Change this to (text, row, col) & make proper adjustments
|
||||||
|
@ -403,9 +400,9 @@ function MultiLineEditTextView2(options) {
|
||||||
// on the next line.
|
// on the next line.
|
||||||
//
|
//
|
||||||
var lastCol = self.cursorPos.col - 1;
|
var lastCol = self.cursorPos.col - 1;
|
||||||
console.log('lastCol=' + lastCol + ' / firstWordWrapWordRange=' + JSON.stringify(wrapped.firstWordWrapWordRange))
|
console.log('lastCol=' + lastCol + ' / firstWrapRange=' + JSON.stringify(wrapped.firstWrapRange))
|
||||||
if(lastCol >= wrapped.firstWordWrapWordRange.start && lastCol <= wrapped.firstWordWrapWordRange.end) {
|
if(lastCol >= wrapped.firstWrapRange.start && lastCol <= wrapped.firstWrapRange.end) {
|
||||||
cursorOffset = self.cursorPos.col - wrapped.firstWordWrapWordRange.start;
|
cursorOffset = self.cursorPos.col - wrapped.firstWrapRange.start;
|
||||||
console.log('cursorOffset=' + cursorOffset)
|
console.log('cursorOffset=' + cursorOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -680,7 +677,8 @@ MultiLineEditTextView2.prototype.setFocus = function(focused) {
|
||||||
MultiLineEditTextView2.prototype.setText = function(text) {
|
MultiLineEditTextView2.prototype.setText = function(text) {
|
||||||
this.textLines = [ ];
|
this.textLines = [ ];
|
||||||
//text = "Tab:\r\n\tA\tB\tC\tD\tE\tF\tG\r\n reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally long word!!!";
|
//text = "Tab:\r\n\tA\tB\tC\tD\tE\tF\tG\r\n reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally long word!!!";
|
||||||
text = require('fs').readFileSync('/home/bashby/Downloads/test_text.txt', { encoding : 'utf-8'});
|
text = require('fs').readFileSync('/home/nuskooler/Downloads/test_text.txt', { encoding : 'utf-8'});
|
||||||
|
//text = 'An excerpt from A Clockwork Orange:'
|
||||||
|
|
||||||
this.insertText(text);//, 0, 0);
|
this.insertText(text);//, 0, 0);
|
||||||
this.cursorEndOfDocument();
|
this.cursorEndOfDocument();
|
||||||
|
|
Loading…
Reference in New Issue