To anyone who came here looking to add the new line after each word rather than abruptly right at the character:
function addWordNewLine(str, breakpoint=80){
var words = str.split(" ");
var numChars = 0;
var mult = 1;
for(var i = 0; i < words.length; i++){
numChars += words[i].length;
// if( we hit a multiple of our breakpoint number )
if(numChars > breakpoint * mult){
words[i] += '\n';
mult++;
}
}
// As we re-arrange our text, make sure that a newline isn't followed
// with a space so that there's no odd single-space indenting
var p = words.join(" ").replaceAll("\n ", "\n");
return p;
}
var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
var formatted = addWordNewLine(str); // default breaks after word after every 80 chars
var wideFormatted = addWordNewLine(str, 200); // breaks after word after every 200 chars