only split if string is over length.

This commit is contained in:
Sidharth Vinod
2023-06-08 00:07:33 +05:30
parent 0cb6df1ef8
commit ad09d63f65

View File

@@ -119,23 +119,27 @@ function createFormattedText(width, g, structuredText, addBackground = false) {
let tempStr = ''; let tempStr = '';
let linesUnderWidth = []; let linesUnderWidth = [];
let prevIndex = 0; let prevIndex = 0;
for (let i = 0; i <= fullStr.length; i++) { if (computeWidthOfText(labelGroup, lineHeight, fullStr) <= width) {
tempStr = fullStr.slice(prevIndex, i); linesUnderWidth.push(fullStr);
log.info(tempStr, prevIndex, i); } else {
if (computeWidthOfText(labelGroup, lineHeight, tempStr) > width) { for (let i = 0; i <= fullStr.length; i++) {
const subStr = fullStr.slice(prevIndex, i); tempStr = fullStr.slice(prevIndex, i);
// Break at space if any log.info(tempStr, prevIndex, i);
const lastSpaceIndex = subStr.lastIndexOf(' '); if (computeWidthOfText(labelGroup, lineHeight, tempStr) > width) {
if (lastSpaceIndex > -1) { const subStr = fullStr.slice(prevIndex, i);
i = prevIndex + lastSpaceIndex + 1; // Break at space if any
const lastSpaceIndex = subStr.lastIndexOf(' ');
if (lastSpaceIndex > -1) {
i = prevIndex + lastSpaceIndex + 1;
}
linesUnderWidth.push(fullStr.slice(prevIndex, i).trim());
prevIndex = i;
tempStr = null;
} }
linesUnderWidth.push(fullStr.slice(prevIndex, i).trim());
prevIndex = i;
tempStr = null;
} }
} if (tempStr != null) {
if (tempStr != null) { linesUnderWidth.push(tempStr);
linesUnderWidth.push(tempStr); }
} }
/** Add each prepared line as a tspan to the parent node */ /** Add each prepared line as a tspan to the parent node */
const preparedLines = linesUnderWidth.map((w) => ({ content: w, type: line.type })); const preparedLines = linesUnderWidth.map((w) => ({ content: w, type: line.type }));