Skip to content

Commit 08f0afb

Browse files
authored
fix(Text): Performance improvement by only determining word width if width prop defined (for word wrapping) (#554)
1 parent 2f2c057 commit 08f0afb

2 files changed

Lines changed: 33 additions & 21 deletions

File tree

.changeset/warm-women-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
fix(Text): Performance improvement by only determining word width if `width` prop defined (for word wrapping)

packages/layerchart/src/lib/components/Text.svelte

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -281,27 +281,34 @@
281281
// Split each line into words
282282
const words = line.split(/(?:(?!\u00A0+)\s+)/);
283283
284-
// Handle word wrapping within each line
285-
return words.reduce((result: { words: string[]; width?: number }[], item) => {
286-
const currentLine = result[result.length - 1];
287-
const itemWidth = getStringWidth(item, style) || 0;
288-
289-
if (
290-
currentLine &&
291-
(width == null || scaleToFit || (currentLine.width || 0) + itemWidth + spaceWidth < width)
292-
) {
293-
// Word can be added to an existing line
294-
currentLine.words.push(item);
295-
currentLine.width = currentLine.width || 0;
296-
currentLine.width += itemWidth + spaceWidth;
297-
} else {
298-
// Add first word to line or word is too long to scaleToFit on existing line
299-
const newLine = { words: [item], width: itemWidth };
300-
result.push(newLine);
301-
}
302-
303-
return result;
304-
}, []);
284+
if (width == null) {
285+
// No width specified, only use explicit line breaks (if used)
286+
return [{ words }];
287+
} else {
288+
// Handle word wrapping within each line
289+
return words.reduce((result: { words: string[]; width?: number }[], item) => {
290+
const currentLine = result[result.length - 1];
291+
const itemWidth = getStringWidth(item, style) || 0;
292+
293+
if (
294+
currentLine &&
295+
(width == null ||
296+
scaleToFit ||
297+
(currentLine.width || 0) + itemWidth + spaceWidth < width)
298+
) {
299+
// Word can be added to an existing line
300+
currentLine.words.push(item);
301+
currentLine.width = currentLine.width || 0;
302+
currentLine.width += itemWidth + spaceWidth;
303+
} else {
304+
// Add first word to line or word is too long to scaleToFit on existing line
305+
const newLine = { words: [item], width: itemWidth };
306+
result.push(newLine);
307+
}
308+
309+
return result;
310+
}, []);
311+
}
305312
});
306313
});
307314

0 commit comments

Comments
 (0)