Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-women-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'layerchart': patch
---

fix(Text): Performance improvement by only determining word width if `width` prop defined (for word wrapping)
49 changes: 28 additions & 21 deletions packages/layerchart/src/lib/components/Text.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,34 @@
// Split each line into words
const words = line.split(/(?:(?!\u00A0+)\s+)/);

// Handle word wrapping within each line
return words.reduce((result: { words: string[]; width?: number }[], item) => {
const currentLine = result[result.length - 1];
const itemWidth = getStringWidth(item, style) || 0;

if (
currentLine &&
(width == null || scaleToFit || (currentLine.width || 0) + itemWidth + spaceWidth < width)
) {
// Word can be added to an existing line
currentLine.words.push(item);
currentLine.width = currentLine.width || 0;
currentLine.width += itemWidth + spaceWidth;
} else {
// Add first word to line or word is too long to scaleToFit on existing line
const newLine = { words: [item], width: itemWidth };
result.push(newLine);
}

return result;
}, []);
if (width == null) {
// No width specified, only use explicit line breaks (if used)
return [{ words }];
} else {
// Handle word wrapping within each line
return words.reduce((result: { words: string[]; width?: number }[], item) => {
const currentLine = result[result.length - 1];
const itemWidth = getStringWidth(item, style) || 0;

if (
currentLine &&
(width == null ||
scaleToFit ||
(currentLine.width || 0) + itemWidth + spaceWidth < width)
) {
// Word can be added to an existing line
currentLine.words.push(item);
currentLine.width = currentLine.width || 0;
currentLine.width += itemWidth + spaceWidth;
} else {
// Add first word to line or word is too long to scaleToFit on existing line
const newLine = { words: [item], width: itemWidth };
result.push(newLine);
}

return result;
}, []);
}
});
});

Expand Down