AI-native design editor. Open-source Figma alternative.
Fix layout blowup from 100×100 text fallback, skip compute for read-only AI tools
Anton A S036f486571a1
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.tsindex a91487228..d8f0e996a 100644--- a/packages/core/src/layout.ts+++ b/packages/core/src/layout.ts@@ -333,6 +333,22 @@ function configureChildAsAutoLayout(}}+// Rough estimate for text size when CanvasKit/font is not available.+// Uses ~0.6 × fontSize per character as average glyph width.+// DO NOT REMOVE: without this, text nodes keep their 100×100 default size+// and blow up every HUG container. The real MeasureFunc (CanvasKit) overrides+// this when available — this is only the fallback.+const GLYPH_WIDTH_FACTOR = 0.6+function estimateTextSize(node: SceneNode): { width: number; height: number } {+ const fontSize = node.fontSize || 14+ const text = node.characters || node.text || ''+ const lineHeight = fontSize * 1.2+ const charWidth = fontSize * GLYPH_WIDTH_FACTOR+ const width = Math.ceil(text.length * charWidth)+ const height = Math.ceil(lineHeight)+ return { width, height }+}+function configureChildAsLeaf(yogaChild: YogaNode, child: SceneNode, parent: SceneNode): void {const isRow = parent.layoutMode === 'HORIZONTAL'const selfOverride = child.layoutAlignSelf !== 'AUTO'@@ -347,6 +363,8 @@ function configureChildAsLeaf(yogaChild: YogaNode, child: SceneNode, parent: Sceif (needsMeasureFunc) {configureTextLeaf(yogaChild, child, parent)} else if (isText && !globalTextMeasurer && child.textAutoResize !== 'NONE') {+ // No CanvasKit — use rough estimate so HUG containers don't inherit+ // the 100×100 default SceneNode size. See estimateTextSize above.const est = estimateTextSize(child)if (child.textAutoResize === 'WIDTH_AND_HEIGHT') {yogaChild.setWidth(est.width)@@ -365,17 +383,7 @@ function configureChildAsLeaf(yogaChild: YogaNode, child: SceneNode, parent: SceapplyMinMaxConstraints(yogaChild, child)}-// Fallback text size estimate when CanvasKit is unavailable (headless/tests).-// Without this, text nodes keep their 100×100 default and blow up HUG containers.-function estimateTextSize(node: SceneNode): { width: number; height: number } {- const fontSize = node.fontSize || 14- const lineHeight = fontSize * 1.2- const charWidth = fontSize * GLYPH_WIDTH_FACTOR- return {- width: Math.ceil(node.text.length * charWidth),- height: Math.ceil(lineHeight)- }-}+function configureTextLeaf(yogaChild: YogaNode,@@ -399,7 +407,7 @@ function configureTextLeaf(if (cached) return cachedconst measured = globalTextMeasurer?.(child, maxW)- const result = measured ?? { width: child.width, height: child.height }+ const result = measured ?? estimateTextSize(child)cache.set(cacheKey, result)return result})@@ -416,7 +424,7 @@ function configureTextLeaf(if (cached) return cachedconst measured = globalTextMeasurer?.(child, constraintW)- const result = { width: constraintW, height: measured?.height ?? child.height }+ const result = { width: constraintW, height: measured?.height ?? estimateTextSize(child).height }cache.set(cacheKey, result)return result})diff --git a/packages/core/src/render/renderer.ts b/packages/core/src/render/renderer.tsindex c49b7b3df..ae6c567ea 100644--- a/packages/core/src/render/renderer.ts+++ b/packages/core/src/render/renderer.ts@@ -397,7 +397,10 @@ function applyTextOverrides(const fillsParent = w === 'fill' || (props.grow as number) > 0const isInsideAutoLayout = parentLayout !== 'NONE'- // Coupled with estimateTextSize() in layout.ts — test headless layout after changes.+ // DO NOT CHANGE these defaults without testing headless layout (no CanvasKit).+ // WIDTH_AND_HEIGHT relies on MeasureFunc — without it, text keeps the 100×100+ // default SceneNode size and blows up every HUG container. layout.ts has a+ // fallback estimator, but changing this logic can silently break all JSX rendering.if (props.textAutoResize) {o.textAutoResize = TEXT_AUTO_RESIZE_MAP[props.textAutoResize as string] ?? 'NONE'} else if (hasExplicitWidth || (isInsideAutoLayout && fillsParent)) {diff --git a/src/ai/tools.ts b/src/ai/tools.tsindex 90d974f77..879443a87 100644--- a/src/ai/tools.ts+++ b/src/ai/tools.ts@@ -21,17 +21,19 @@ export function createAITools(store: EditorStore) {}},onAfterExecute: (def) => {- computeAllLayouts(store.graph, store.state.currentPageId)- store.requestRender()- if (def.mutates && beforeSnapshot) {- const before = beforeSnapshot- const after = store.snapshotPage()- store.pushUndoEntry({- label: `AI: ${def.name}`,- forward: () => store.restorePageFromSnapshot(after),- inverse: () => store.restorePageFromSnapshot(before)- })- beforeSnapshot = null+ if (def.mutates) {+ computeAllLayouts(store.graph, store.state.currentPageId)+ store.requestRender()+ if (beforeSnapshot) {+ const before = beforeSnapshot+ const after = store.snapshotPage()+ store.pushUndoEntry({+ label: `AI: ${def.name}`,+ forward: () => store.restorePageFromSnapshot(after),+ inverse: () => store.restorePageFromSnapshot(before)+ })+ beforeSnapshot = null+ }}},onFlashNodes: (nodeIds) => {diff --git a/tests/engine/layout.test.ts b/tests/engine/layout.test.tsindex 3bb323123..fdc335fcc 100644--- a/tests/engine/layout.test.ts+++ b/tests/engine/layout.test.ts@@ -1033,7 +1033,7 @@ describe('Auto Layout', () => {primaryAxisAlign: 'CENTER',})- const text = graph.createNode('TEXT', frame.id, {+ graph.createNode('TEXT', frame.id, {width: 200,height: 20,text: 'Test',@@ -1044,7 +1044,8 @@ describe('Auto Layout', () => {setTextMeasurer(null)computeAllLayouts(graph)- const updatedText = graph.getNode(text.id)!+ const children = graph.getChildren(frame.id)+ const updatedText = children[0]// Rough estimate: ~0.6 × fontSize × charCount, not the 100×100 defaultexpect(updatedText.width).toBeLessThan(100)expect(updatedText.width).toBeGreaterThan(0)