AI-native design editor. Open-source Figma alternative.
fix(clipboard): preserve Figma text edit metrics
Danila Poyarkov72766acda512
diff --git a/packages/core/src/text/derived-text/clipboard.ts b/packages/core/src/text/derived-text/clipboard.tsindex 649867ce3..1fc110eeb 100644--- a/packages/core/src/text/derived-text/clipboard.ts+++ b/packages/core/src/text/derived-text/clipboard.ts@@ -12,7 +12,7 @@ import { buildDerivedTextData } from './data'function computeWordWrapBreaks(text: string,- glyphMetrics: GlyphOutlineMetrics[],+ glyphMetrics: Array<Pick<GlyphOutlineMetrics, 'advance'>>,fallbackAdvance: number,maxWidth: number,fontSize: number,@@ -37,7 +37,7 @@ function computeWordWrapBreaks(function computeFallbackBreaks(text: string,- glyphMetrics: GlyphOutlineMetrics[],+ glyphMetrics: Array<Pick<GlyphOutlineMetrics, 'advance'>>,fallbackAdvance: number,maxWidth: number): number[] {@@ -53,7 +53,7 @@ function computeFallbackBreaks(lastBreak = i + 1lastBreakX = x + advance}- if (x > 0 && x + advance > maxWidth) {+ if (x > 0 && x + advance > maxWidth + 0.5) {const lineStart = breaks.length > 0 ? breaks[breaks.length - 1] : 0if (lastBreak > lineStart) {breaks.push(lastBreak)@@ -84,6 +84,47 @@ export interface ShapedClipboardText {logicalIndexToCharacterOffsetMap: number[]}+type DerivedGlyphMetric = { advance: number; commands: GlyphOutlineMetrics['commands'] }++function buildTextGlyphs(+ text: string,+ glyphMetrics: GlyphOutlineMetrics[],+ fallbackAdvance: number,+ fontSize: number+): DerivedGlyphMetric[] {+ if (glyphMetrics.length > 0) {+ return glyphMetrics.map((glyph) => ({+ advance: glyph.advance || fallbackAdvance,+ commands: glyph.commands+ }))+ }+ return Array.from({ length: text.length }, () => ({+ advance: fallbackAdvance || fontSize * 0.6,+ commands: []+ }))+}++function computeLineBreaks(+ node: SceneNode,+ glyphMetrics: GlyphOutlineMetrics[],+ textGlyphs: DerivedGlyphMetric[],+ fallbackAdvance: number,+ shaped?: ShapedClipboardText | null+): number[] {+ if (shaped) return []+ if (glyphMetrics.length > 0) {+ return computeWordWrapBreaks(+ node.text,+ textGlyphs,+ fallbackAdvance,+ node.width,+ node.fontSize,+ node.fontFamily+ )+ }+ return computeFallbackBreaks(node.text, textGlyphs, fallbackAdvance, node.width)+}+export async function buildDerivedTextDataV4(node: SceneNode,digestMap: Map<string, Uint8Array>,@@ -97,10 +138,9 @@ export async function buildDerivedTextDataV4(const glyphMetrics = getGlyphOutlineMetricsSync(node.fontFamily, style, node.text, node.fontSize) ?? []const fallbackAdvance = node.text.length > 0 ? node.width / Math.max(node.text.length, 1) : 0+ const textGlyphs = buildTextGlyphs(node.text, glyphMetrics, fallbackAdvance, node.fontSize)const lineAscent = Math.max(lineHeightFallback - node.fontSize * 0.2, 0)- const lineBreaks = shaped- ? []- : computeWordWrapBreaks(node.text, glyphMetrics, fallbackAdvance, node.width, node.fontSize, node.fontFamily)+ const lineBreaks = computeLineBreaks(node, glyphMetrics, textGlyphs, fallbackAdvance, shaped)const lineBreakSet = new Set(lineBreaks)const shapedByChar = new Map<number, (typeof shaped extends null | undefined ? never : NonNullable<typeof shaped>)['glyphs'][number]>()@@ -114,7 +154,7 @@ export async function buildDerivedTextDataV4(let fallbackY = lineHeightFallbacklet lineStart = 0- const glyphs = glyphMetrics.map((glyph, index) => {+ const glyphs = textGlyphs.map((glyph, index) => {const shapedGlyph = shapedByChar.get(index)const fallbackGlyphAdvance = glyph.advance || fallbackAdvanceif (!shapedGlyph && lineBreakSet.has(index)) {@@ -133,9 +173,10 @@ export async function buildDerivedTextDataV4(const glyphX = fallbackXfallbackOffsets[index] = glyphXfallbackX += fallbackGlyphAdvance- const commandsBlob = blobs- ? blobs.push(encodePathCommandsBlob(glyph.commands, node.fontSize)) - 1- : undefined+ const commandsBlob =+ blobs && glyph.commands.length > 0+ ? blobs.push(encodePathCommandsBlob(glyph.commands, node.fontSize)) - 1+ : undefinedreturn {commandsBlob,position: {diff --git a/tests/engine/clipboard/figma/html.test.ts b/tests/engine/clipboard/figma/html.test.tsindex 01694fa3b..f86c456fe 100644--- a/tests/engine/clipboard/figma/html.test.ts+++ b/tests/engine/clipboard/figma/html.test.ts@@ -61,6 +61,35 @@ describe('buildFigmaClipboardHTML', () => {expect(textNode?.derivedTextData?.baselines?.length).toBeGreaterThan(0)})+ it('encodes fallback derived text metrics when outline fonts are unavailable', async () => {+ const graph = new SceneGraph()+ const page = graph.getPages()[0]+ graph.createNode('TEXT', page.id, {+ name: 'Title',+ x: 0,+ y: 0,+ width: 552,+ height: 70,+ text: 'Analytics Overview',+ fontFamily: 'Missing Preview Font',+ fontWeight: 700,+ fontSize: 56,+ lineHeight: 67,+ textAutoResize: 'HEIGHT'+ })++ const html = await buildFigmaClipboardHTML(graph.getChildren(page.id), graph)+ const parsed = await parseFigmaClipboard(html)+ const textNode = parsed?.nodes.find((node) => node.type === 'TEXT')+ const baseline = textNode?.derivedTextData?.baselines?.[0]++ expect(textNode?.textUserLayoutVersion).toBe(4)+ expect(textNode?.derivedTextData?.glyphs?.length).toBe('Analytics Overview'.length)+ expect(baseline?.width).toBe(552)+ expect(baseline?.lineHeight).toBe(67)+ expect(textNode?.derivedTextData?.layoutSize).toEqual({ x: 552, y: 70 })+ })+it('encodes auto-layout frames', async () => {const graph = new SceneGraph()const page = graph.getPages()[0]