From 5d341778c5d558949a5c88905645dca2a9526483 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Fri, 7 Apr 2017 07:08:19 -0700 Subject: [PATCH] fix notification word wrapping --- scripts/system/notifications.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/scripts/system/notifications.js b/scripts/system/notifications.js index 3989e8e372..25a5edf3a3 100644 --- a/scripts/system/notifications.js +++ b/scripts/system/notifications.js @@ -427,21 +427,24 @@ function deleteNotification(index) { arrays.splice(index, 1); } -// wraps whole word to newline -function stringDivider(str, slotWidth, spaceReplacer) { - var left, right; - if (str.length > slotWidth && slotWidth > 0) { - left = str.substring(0, slotWidth); - right = str.substring(slotWidth); - return left + spaceReplacer + stringDivider(right, slotWidth, spaceReplacer); +// Trims extra whitespace and breaks into lines of length no more than MAX_LENGTH, breaking at spaces. Trims extra whitespace. +var MAX_LENGTH = 42; +function wordWrap(string) { + var finishedLines = [], currentLine = ''; + string.split(/\s/).forEach(function (word) { + var tail = currentLine ? ' ' + word : word; + if ((currentLine.length + tail.length) <= MAX_LENGTH) { + currentLine += tail; + } else { + finishedLines.push(currentLine); + currentLine = word; + } + }); + if (currentLine) { + finishedLines.push(currentLine); } - return str; -} - -// formats string to add newline every 43 chars -function wordWrap(str) { - return stringDivider(str, 43.0, "\n"); + return finishedLines.join('\n'); } function update() {