AI-native design editor. Open-source Figma alternative.
fix(canvas): ellipsize frame labels
Danila Poyarkovf365ccf23bd2
diff --git a/packages/core/src/canvas/labels/draw.ts b/packages/core/src/canvas/labels/draw.tsindex 0aaaf6138..7893013d9 100644--- a/packages/core/src/canvas/labels/draw.ts+++ b/packages/core/src/canvas/labels/draw.ts@@ -13,6 +13,8 @@ import {} from '#core/constants'import type { SceneNode, SceneGraph } from '#core/scene-graph'+import { ellipsizeLabelText } from './text'+export function drawSectionTitles(r: SkiaRenderer, canvas: Canvas, graph: SceneGraph): void {if (!r.sectionTitleFont) return@@ -98,34 +100,6 @@ 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@@ -149,7 +123,7 @@ export function drawComponentLabels(r: SkiaRenderer, canvas: Canvas, graph: Scen}const maxTextWidth = node.width * r.zoom - iconS - COMPONENT_LABEL_ICON_GAP- const displayText = ellipsizeComponentLabel(font, node.name, maxTextWidth)+ const displayText = ellipsizeLabelText(font, node.name, maxTextWidth)if (!displayText) continueconst iconX = labelXdiff --git a/packages/core/src/canvas/labels/selection.ts b/packages/core/src/canvas/labels/selection.tsindex 6490c1688..76b27f6ef 100644--- a/packages/core/src/canvas/labels/selection.ts+++ b/packages/core/src/canvas/labels/selection.ts@@ -14,6 +14,8 @@ import type { SceneNode, SceneGraph } from '#core/scene-graph'import type { SkiaRenderer, RenderOverlays } from '#core/canvas/renderer'+import { ellipsizeLabelText } from './text'+function getOverlayRotation(node: SceneNode, overlays?: RenderOverlays): number {return overlays?.rotationPreview?.nodeId === node.id? overlays.rotationPreview.angle@@ -76,10 +78,13 @@ function drawSingleFrameTitle(r.auxFill.setColor(r.selColor())+ const displayText = ellipsizeLabelText(labelFont, node.name, node.width * r.zoom)+ if (!displayText) return+canvas.save()canvas.translate(origin[0] * r.zoom + r.panX, origin[1] * r.zoom + r.panY)if (overlayRotation !== 0) canvas.rotate(overlayRotation, 0, 0)- canvas.drawText(node.name, 0, -LABEL_OFFSET_Y, r.auxFill, labelFont)+ canvas.drawText(displayText, 0, -LABEL_OFFSET_Y, r.auxFill, labelFont)canvas.restore()}diff --git a/packages/core/src/canvas/labels/text.ts b/packages/core/src/canvas/labels/text.tsnew file mode 100644index 000000000..1eddb671e--- /dev/null+++ b/packages/core/src/canvas/labels/text.ts@@ -0,0 +1,29 @@+import type { Font } from 'canvaskit-wasm'++export function measureLabelText(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 ellipsizeLabelText(font: Font, text: string, maxWidth: number): string {+ if (maxWidth <= 0) return ''+ if (measureLabelText(font, text) <= maxWidth) return text++ const ellipsis = '…'+ const ellipsisWidth = measureLabelText(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+}diff --git a/tests/engine/render/canvas/component-label-text.test.ts b/tests/engine/render/canvas/component-label-text.test.tsindex edc227789..106fcce46 100644--- a/tests/engine/render/canvas/component-label-text.test.ts+++ b/tests/engine/render/canvas/component-label-text.test.ts@@ -1,6 +1,6 @@import { describe, expect, test } from 'bun:test'-import { ellipsizeComponentLabel } from '#core/canvas/labels/draw'+import { ellipsizeLabelText } from '#core/canvas/labels/text'const fixedWidthFont = {getGlyphIDs(text: string) {@@ -9,18 +9,18 @@ const fixedWidthFont = {getGlyphWidths(glyphs: number[]) {return glyphs.map(() => 10)}-} as Parameters<typeof ellipsizeComponentLabel>[0]+} as Parameters<typeof ellipsizeLabelText>[0]-describe('component label text', () => {+describe('label text', () => {test('keeps labels that fit inside the component width', () => {- expect(ellipsizeComponentLabel(fixedWidthFont, 'Button', 60)).toBe('Button')+ expect(ellipsizeLabelText(fixedWidthFont, 'Button', 60)).toBe('Button')})test('ellipsizes labels that exceed the component width', () => {- expect(ellipsizeComponentLabel(fixedWidthFont, 'Very long component', 55)).toBe('Very…')+ expect(ellipsizeLabelText(fixedWidthFont, 'Very long component', 55)).toBe('Very…')})test('returns only ellipsis when only the ellipsis fits', () => {- expect(ellipsizeComponentLabel(fixedWidthFont, 'Component', 10)).toBe('…')+ expect(ellipsizeLabelText(fixedWidthFont, 'Component', 10)).toBe('…')})})