AI-native design editor. Open-source Figma alternative.
fix(canvas): ellipsize component labels
Danila Poyarkov7b428b58aab9
diff --git a/packages/core/src/canvas/labels/draw.ts b/packages/core/src/canvas/labels/draw.tsindex 1aef53f13..0aaaf6138 100644--- a/packages/core/src/canvas/labels/draw.ts+++ b/packages/core/src/canvas/labels/draw.ts@@ -98,6 +98,34 @@ function drawSectionTitle(canvas.restore()}+function measureText(font: Font, text: string): number {+ const glyphIds = font.getGlyphIDs(text)+ const widths = font.getGlyphWidths(glyphIds)+ let result = 0+ for (const width of widths) result += width+ return result+}++export function ellipsizeComponentLabel(font: Font, text: string, maxWidth: number): string {+ if (maxWidth <= 0) return ''+ if (measureText(font, text) <= maxWidth) return text++ const ellipsis = '…'+ const ellipsisWidth = measureText(font, ellipsis)+ if (maxWidth <= ellipsisWidth) return ellipsis++ let width = 0+ let end = 0+ const glyphIds = font.getGlyphIDs(text)+ const widths = font.getGlyphWidths(glyphIds)+ for (let index = 0; index < widths.length; index++) {+ if (width + widths[index] + ellipsisWidth > maxWidth) break+ width += widths[index]+ end = index + 1+ }+ return text.slice(0, end) + ellipsis+}+export function drawComponentLabels(r: SkiaRenderer, canvas: Canvas, graph: SceneGraph): void {if (!r.componentLabelFont) return@@ -120,6 +148,10 @@ export function drawComponentLabels(r: SkiaRenderer, canvas: Canvas, graph: ScenlabelY = screenY - COMPONENT_LABEL_GAP}+ const maxTextWidth = node.width * r.zoom - iconS - COMPONENT_LABEL_ICON_GAP+ const displayText = ellipsizeComponentLabel(font, node.name, maxTextWidth)+ if (!displayText) continue+const iconX = labelXconst iconY = labelY - COMPONENT_LABEL_FONT_SIZE * 0.75const iconCx = iconX + iconS / 2@@ -159,6 +191,6 @@ export function drawComponentLabels(r: SkiaRenderer, canvas: Canvas, graph: Scenpath.delete()}- canvas.drawText(node.name, labelX + iconS + COMPONENT_LABEL_ICON_GAP, labelY, r.auxFill, font)+ canvas.drawText(displayText, labelX + iconS + COMPONENT_LABEL_ICON_GAP, labelY, r.auxFill, font)}}diff --git a/tests/engine/render/canvas/component-label-text.test.ts b/tests/engine/render/canvas/component-label-text.test.tsnew file mode 100644index 000000000..edc227789--- /dev/null+++ b/tests/engine/render/canvas/component-label-text.test.ts@@ -0,0 +1,26 @@+import { describe, expect, test } from 'bun:test'++import { ellipsizeComponentLabel } from '#core/canvas/labels/draw'++const fixedWidthFont = {+ getGlyphIDs(text: string) {+ return [...text].map((_, index) => index)+ },+ getGlyphWidths(glyphs: number[]) {+ return glyphs.map(() => 10)+ }+} as Parameters<typeof ellipsizeComponentLabel>[0]++describe('component label text', () => {+ test('keeps labels that fit inside the component width', () => {+ expect(ellipsizeComponentLabel(fixedWidthFont, 'Button', 60)).toBe('Button')+ })++ test('ellipsizes labels that exceed the component width', () => {+ expect(ellipsizeComponentLabel(fixedWidthFont, 'Very long component', 55)).toBe('Very…')+ })++ test('returns only ellipsis when only the ellipsis fits', () => {+ expect(ellipsizeComponentLabel(fixedWidthFont, 'Component', 10)).toBe('…')+ })+})