AI-native design editor. Open-source Figma alternative.
fix(canvas): hide nested component labels
Danila Poyarkov383e03defa60
diff --git a/packages/core/src/canvas/labels/cache.ts b/packages/core/src/canvas/labels/cache.tsindex ac2fde61c..752a4a90c 100644--- a/packages/core/src/canvas/labels/cache.ts+++ b/packages/core/src/canvas/labels/cache.ts@@ -22,6 +22,7 @@ interface Viewport {}const LABEL_TYPES = new Set(['COMPONENT', 'COMPONENT_SET'])+const COMPONENT_LABEL_PARENT_TYPES = new Set(['CANVAS', 'SECTION'])function isInViewport(absX: number, absY: number, w: number, h: number, vp: Viewport): boolean {return absX + w >= vp.x && absY + h >= vp.y && absX <= vp.x + vp.w && absY <= vp.y + vp.h@@ -137,7 +138,9 @@ export class LabelCache {this.sections.push({ nodeId: childId, absX: ax, absY: ay, nested: insideSection })this.walkChildren(graph, childId, ax, ay, true)} else if (LABEL_TYPES.has(child.type)) {- this.components.push({ nodeId: childId, absX: ax, absY: ay, parentType })+ if (COMPONENT_LABEL_PARENT_TYPES.has(parentType)) {+ this.components.push({ nodeId: childId, absX: ax, absY: ay, parentType })+ }if (child.childIds.length > 0) {this.walkChildren(graph, childId, ax, ay, insideSection)}diff --git a/tests/engine/render/canvas/label-cache.test.ts b/tests/engine/render/canvas/label-cache.test.tsindex 588716e60..bc6b037e7 100644--- a/tests/engine/render/canvas/label-cache.test.ts+++ b/tests/engine/render/canvas/label-cache.test.ts@@ -136,32 +136,28 @@ describe('LabelCache', () => {expect(nested?.nested).toBe(true)})- it('collects components and component sets', () => {+ it('collects top-level components and component sets', () => {const { g, pageId } = buildGraph()const cache = new LabelCache()cache.update(g, pageId, 1)const components = cache.getComponents(g, { x: -1000, y: -1000, w: 3000, h: 3000 })- expect(components.length).toBe(3)+ expect(components.length).toBe(2)const names = components.map((c) => c.node.name).sort()- expect(names).toEqual(['Button', 'Button Set', 'Standalone'])+ expect(names).toEqual(['Button Set', 'Standalone'])})- it('identifies components inside component sets', () => {+ it('skips components nested inside other components', () => {const { g, pageId } = buildGraph()const cache = new LabelCache()cache.update(g, pageId, 1)const components = cache.getComponents(g, { x: -1000, y: -1000, w: 3000, h: 3000 })- const buttonSet = components.find((c) => c.node.name === 'Button Set')- const button = components.find((c) => c.node.name === 'Button')- const standalone = components.find((c) => c.node.name === 'Standalone')-- expect(buttonSet?.inside).toBe(false)- expect(button?.inside).toBe(true)- expect(standalone?.inside).toBe(false)+ expect(components.find((c) => c.node.name === 'Button Set')).toBeDefined()+ expect(components.find((c) => c.node.name === 'Button')).toBeUndefined()+ expect(components.find((c) => c.node.name === 'Standalone')).toBeDefined()})it('skips invisible nodes', () => {@@ -216,8 +212,8 @@ describe('LabelCache', () => {expect(nested?.absY).toBe(10)const components = cache.getComponents(g, { x: -1000, y: -1000, w: 3000, h: 3000 })- const button = components.find((c) => c.node.name === 'Button')- expect(button?.absX).toBe(310)- expect(button?.absY).toBe(10)+ const buttonSet = components.find((c) => c.node.name === 'Button Set')+ expect(buttonSet?.absX).toBe(300)+ expect(buttonSet?.absY).toBe(0)})})