fix hyphen in textarea

This commit is contained in:
Stefan Niederhauser 2020-02-28 18:42:18 +01:00
parent 444c22ce93
commit 36cae315aa
2 changed files with 7 additions and 3 deletions

View File

@ -36,8 +36,8 @@ describe('textarea-layout', () => {
['Donaudampfsc', 'x2']); ['Donaudampfsc', 'x2']);
}); });
it('should wrap lines at -', () => { it('should wrap lines at -', () => {
layoutEqual(' Long text- message with sev lines.', layoutEqual(' Long text- message with lines.',
[' Long text-', 'message with','sev lines.']); [' Long text', '- message','with lines.']);
}); });
it('should wrap lines at - 2', () => { it('should wrap lines at - 2', () => {
layoutEqual('Long text- message with sev lines.', layoutEqual('Long text- message with sev lines.',

View File

@ -13,7 +13,7 @@ export function layout(chars: string[], width: number, measure: (s: string, len:
const lineWidth = measure(lineString, lineLen); const lineWidth = measure(lineString, lineLen);
pos.push([lineWidth - measure(chars[i], 1), line]); pos.push([lineWidth - measure(chars[i], 1), line]);
if (lineWidth > width) { if (lineWidth > width) {
if (chars[i] === ' ' || chars[i] === '-') { if (chars[i] === ' ') {
let p = i; let p = i;
while (p > 0 && pos[p][1] === line && chars[p] === ' ') p--; while (p > 0 && pos[p][1] === line && chars[p] === ' ') p--;
for (let j = i; j > p; j--) { for (let j = i; j > p; j--) {
@ -25,6 +25,10 @@ export function layout(chars: string[], width: number, measure: (s: string, len:
pos.push([-1, line]); pos.push([-1, line]);
i++; i++;
} }
} else if (chars[i] === '-') {
line++;
setLine(i, i + 1);
pos[i] = [0, line];
} else { } else {
let p = i; let p = i;
while (p > 0 && pos[p][1] === line && chars[p] !== ' ' && chars[p] !== '-') p--; while (p > 0 && pos[p][1] === line && chars[p] !== ' ' && chars[p] !== '-') p--;