snippit
TextMorph.prototype.parse = function () {
var paragraphs = this.text.split('\n'),
context = this.measureCtx,
oldline = '',
newline,
w,
slot = 0;
context.font = this.font();
this.maxLineWidth = 0;
this.lines = [];
this.lineSlots = [0];
this.words = [];
paragraphs.forEach(p => {
this.words = this.words.concat(p.split(' '));
this.words.push('\n');
});
this.words.forEach(word => {
if (word === '\n') {
this.lines.push(oldline);
this.lineSlots.push(slot);
this.maxLineWidth = Math.max(
this.maxLineWidth,
context.measureText(
(this.parent instanceof TextSlotMorph) ? oldline.slice(0, -1) : oldline
).width
);
oldline = '';
} else {
if (this.maxWidth > 0) {
newline = oldline + word + ' ';
w = context.measureText(newline).width;
if (w > this.maxWidth) {
this.lines.push(oldline);
this.lineSlots.push(slot);
this.maxLineWidth = Math.max(
this.maxLineWidth,
context.measureText(oldline).width
);
oldline = word + ' ';
} else {
oldline = newline;
}
} else {
oldline = oldline + word + ' ';
}
slot += word.length + 1;
}
});
};
one-liner
TextMorph.prototype.parse=function(){var paragraphs=this.text.split('\n'),context=this.measureCtx,oldline='',newline,w,slot=0;context.font=this.font();this.maxLineWidth=0;this.lines=[];this.lineSlots=[0];this.words=[];paragraphs.forEach(p=>{this.words=this.words.concat(p.split(' '));this.words.push('\n');});this.words.forEach(word=>{if(word==='\n'){this.lines.push(oldline);this.lineSlots.push(slot);this.maxLineWidth=Math.max(this.maxLineWidth,context.measureText((this.parent instanceof TextSlotMorph)?oldline.slice(0,-1):oldline).width);oldline='';}else{if(this.maxWidth>0){newline=oldline+word+' ';w=context.measureText(newline).width;if(w>this.maxWidth){this.lines.push(oldline);this.lineSlots.push(slot);this.maxLineWidth=Math.max(this.maxLineWidth,context.measureText(oldline).width);oldline=word+' ';}else{oldline=newline;}}else{oldline=oldline+word+' ';}slot+=word.length+1;}});};