AI-native design editor. Open-source Figma alternative.
Add components, component sets, and instances
Danila Poyarkov3c5795f90b92
diff --git a/src/components/LayersPanel.vue b/src/components/LayersPanel.vueindex 14754adca..9931e565b 100644--- a/src/components/LayersPanel.vue+++ b/src/components/LayersPanel.vue@@ -4,8 +4,11 @@ import { useEventListener } from '@vueuse/core'import { TreeRoot, TreeItem } from 'reka-ui'import IconCircle from '~icons/lucide/circle'+import IconComponent from '~icons/lucide/diamond'+import IconComponentSet from '~icons/lucide/diamonds'import IconFrame from '~icons/lucide/frame'import IconGroup from '~icons/lucide/group'+import IconInstance from '~icons/lucide/diamond'import IconMinus from '~icons/lucide/minus'import IconPenTool from '~icons/lucide/pen-tool'import IconSection from '~icons/lucide/layout-grid'@@ -30,12 +33,17 @@ const nodeIcons: Record<string, typeof IconSquare> = {ELLIPSE: IconCircle,FRAME: IconFrame,GROUP: IconGroup,+ COMPONENT: IconComponent,+ COMPONENT_SET: IconComponentSet,+ INSTANCE: IconInstance,LINE: IconMinus,TEXT: IconType,VECTOR: IconPenTool,RECTANGLE: IconSquare}+const COMPONENT_TYPES = new Set(['COMPONENT', 'COMPONENT_SET', 'INSTANCE'])+function buildTree(parentId: string): LayerNode[] {const parent = store.graph.getNode(parentId)if (!parent) return []@@ -249,7 +257,13 @@ function updateDropTarget(ev: PointerEvent) {<icon-lucide-chevron-right class="size-3" /></span><span v-else class="w-4 shrink-0" />- <component :is="nodeIcons[item.value.type] ?? IconSquare" class="size-3 shrink-0 opacity-70" />+ <component+ :is="nodeIcons[item.value.type] ?? IconSquare"+ class="size-3 shrink-0"+ :class="COMPONENT_TYPES.has(item.value.type)+ ? 'text-[#9747ff] opacity-100'+ : 'opacity-70'"+ /><span class="min-w-0 flex-1 truncate">{{ item.value.name }}</span><icon-lucide-eye-offv-if="!item.value.visible"diff --git a/src/components/PropertiesPanel.vue b/src/components/PropertiesPanel.vueindex b47dc7711..05a7fb246 100644--- a/src/components/PropertiesPanel.vue+++ b/src/components/PropertiesPanel.vue@@ -15,6 +15,10 @@ const store = useEditorStore()const node = computed(() => store.selectedNode.value)const multiCount = computed(() => store.selectedNodes.value.length)+const isComponentType = computed(() => {+ const t = node.value?.type+ return t === 'COMPONENT' || t === 'COMPONENT_SET' || t === 'INSTANCE'+})</script><template>@@ -41,10 +45,38 @@ const multiCount = computed(() => store.selectedNodes.value.length)<div v-else-if="node" class="flex-1 overflow-y-auto pb-4"><!-- Node header --><div class="flex items-center gap-1.5 border-b border-border px-3 py-2">- <span class="text-[11px] text-muted">{{ node.type }}</span>+ <span+ class="text-[11px]"+ :class="isComponentType ? 'text-[#9747ff]' : 'text-muted'"+ >{{ node.type }}</span><span class="text-xs font-semibold">{{ node.name }}</span></div>+ <!-- Component actions -->+ <div v-if="node.type === 'INSTANCE'" class="flex flex-col gap-1 border-b border-border px-3 py-2">+ <button+ class="rounded bg-[#9747ff]/10 px-2 py-1 text-left text-[11px] text-[#9747ff] hover:bg-[#9747ff]/20"+ @click="store.goToMainComponent()"+ >+ Go to Main Component+ </button>+ <button+ class="rounded px-2 py-1 text-left text-[11px] text-muted hover:bg-hover"+ @click="store.detachInstance()"+ >+ Detach Instance+ </button>+ </div>++ <div v-if="node.type === 'COMPONENT'" class="flex flex-col gap-1 border-b border-border px-3 py-2">+ <button+ class="rounded bg-[#9747ff]/10 px-2 py-1 text-left text-[11px] text-[#9747ff] hover:bg-[#9747ff]/20"+ @click="store.createInstanceFromComponent(node.id)"+ >+ Create Instance+ </button>+ </div>+<PositionSection /><LayoutSection /><AppearanceSection />diff --git a/src/composables/use-keyboard.ts b/src/composables/use-keyboard.tsindex 73735d875..3927a7f9d 100644--- a/src/composables/use-keyboard.ts+++ b/src/composables/use-keyboard.ts@@ -39,6 +39,27 @@ export function useKeyboard(store: EditorStore) {return}+ if ((e.metaKey || e.ctrlKey) && e.altKey) {+ if (e.key === 'k' || e.key === 'K') {+ e.preventDefault()+ store.createComponentFromSelection()+ return+ }+ if (e.key === 'b' || e.key === 'B') {+ e.preventDefault()+ store.detachInstance()+ return+ }+ }++ if ((e.metaKey || e.ctrlKey) && e.shiftKey) {+ if (e.key === 'K' || e.key === 'k') {+ e.preventDefault()+ store.createComponentSetFromComponents()+ return+ }+ }+if (e.metaKey || e.ctrlKey) {if (e.key === 'z' && !e.shiftKey) {e.preventDefault()diff --git a/src/composables/use-menu.ts b/src/composables/use-menu.tsindex 0016597a7..b77ca148c 100644--- a/src/composables/use-menu.ts+++ b/src/composables/use-menu.ts@@ -52,6 +52,9 @@ const MENU_ACTIONS: Record<string, (store: EditorStore) => void> = {delete: (store) => store.deleteSelected(),group: (store) => store.groupSelected(),ungroup: (store) => store.ungroupSelected(),+ 'create-component': (store) => store.createComponentFromSelection(),+ 'create-component-set': (store) => store.createComponentSetFromComponents(),+ 'detach-instance': (store) => store.detachInstance(),'zoom-fit': (store) => store.zoomToFit(),}diff --git a/src/constants.ts b/src/constants.tsindex 154280051..68354f19f 100644--- a/src/constants.ts+++ b/src/constants.ts@@ -4,6 +4,7 @@ import type { Fill, Stroke } from './engine/scene-graph'export const IS_TAURI = typeof window !== 'undefined' && '__TAURI_INTERNALS__' in windowexport const SELECTION_COLOR = { r: 0.23, g: 0.51, b: 0.96, a: 1 } satisfies Color+export const COMPONENT_COLOR = { r: 0.592, g: 0.278, b: 1, a: 1 } satisfies Colorexport const SNAP_COLOR = { r: 1.0, g: 0.0, b: 0.56, a: 1 } satisfies Colorexport const CANVAS_BG_COLOR = { r: 0.96, g: 0.96, b: 0.96, a: 1 } satisfies Color@@ -99,5 +100,9 @@ export const SECTION_DEFAULT_STROKE: Stroke = {align: 'INSIDE'}+export const COMPONENT_SET_DASH = 6+export const COMPONENT_SET_DASH_GAP = 4+export const COMPONENT_SET_BORDER_WIDTH = 1.5+export const RULER_TARGET_PIXEL_SPACING = 100export const RULER_MAJOR_TOLERANCE = 0.01diff --git a/src/engine/clipboard.ts b/src/engine/clipboard.tsindex 4227cb54b..3d639d12b 100644--- a/src/engine/clipboard.ts+++ b/src/engine/clipboard.ts@@ -365,10 +365,13 @@ function mapCounterAlign(align?: string): LayoutCounterAlign {function mapNodeType(type?: string): SceneNode['type'] {switch (type) {case 'FRAME':+ return 'FRAME'case 'COMPONENT':+ return 'COMPONENT'case 'COMPONENT_SET':+ return 'COMPONENT_SET'case 'INSTANCE':- return 'FRAME'+ return 'INSTANCE'case 'RECTANGLE':case 'ROUNDED_RECTANGLE':return 'RECTANGLE'diff --git a/src/engine/fig-export.ts b/src/engine/fig-export.tsindex 1a993ba22..3ac643a4f 100644--- a/src/engine/fig-export.ts+++ b/src/engine/fig-export.ts@@ -66,9 +66,9 @@ function mapToFigmaType(type: SceneNode['type']): string {case 'VECTOR': return 'VECTOR'case 'GROUP': return 'FRAME'case 'SECTION': return 'SECTION'- case 'COMPONENT': return 'FRAME'- case 'COMPONENT_SET': return 'FRAME'- case 'INSTANCE': return 'FRAME'+ case 'COMPONENT': return 'COMPONENT'+ case 'COMPONENT_SET': return 'COMPONENT_SET'+ case 'INSTANCE': return 'INSTANCE'case 'CONNECTOR': return 'CONNECTOR'case 'SHAPE_WITH_TEXT': return 'SHAPE_WITH_TEXT'default: return 'RECTANGLE'diff --git a/src/engine/renderer.ts b/src/engine/renderer.tsindex 2479d6b25..4bb3798df 100644--- a/src/engine/renderer.ts+++ b/src/engine/renderer.ts@@ -1,5 +1,5 @@import {- SELECTION_COLOR, SNAP_COLOR, CANVAS_BG_COLOR, ROTATION_HANDLE_OFFSET,+ SELECTION_COLOR, COMPONENT_COLOR, SNAP_COLOR, CANVAS_BG_COLOR, ROTATION_HANDLE_OFFSET,RULER_SIZE, RULER_BG_COLOR, RULER_TICK_COLOR, RULER_TEXT_COLOR,RULER_BADGE_HEIGHT, RULER_BADGE_PADDING, RULER_BADGE_RADIUS, RULER_BADGE_EXCLUSION,RULER_TEXT_BASELINE, RULER_MAJOR_TICK, RULER_MINOR_TICK, RULER_HIGHLIGHT_ALPHA,@@ -13,6 +13,7 @@ import {LAYOUT_INDICATOR_STROKE,SECTION_CORNER_RADIUS, SECTION_TITLE_HEIGHT, SECTION_TITLE_PADDING_X,SECTION_TITLE_RADIUS, SECTION_TITLE_FONT_SIZE, SECTION_TITLE_GAP,+ COMPONENT_SET_DASH, COMPONENT_SET_DASH_GAP, COMPONENT_SET_BORDER_WIDTH,RULER_TARGET_PIXEL_SPACING, RULER_MAJOR_TOLERANCE} from '../constants'import type { SceneNode, SceneGraph, Fill, GradientStop, GradientTransform, ArcData } from './scene-graph'@@ -84,6 +85,14 @@ export class SkiaRenderer {return this.ck.Color4f(SELECTION_COLOR.r, SELECTION_COLOR.g, SELECTION_COLOR.b, alpha)}+ private compColor(alpha = 1) {+ return this.ck.Color4f(COMPONENT_COLOR.r, COMPONENT_COLOR.g, COMPONENT_COLOR.b, alpha)+ }++ private isComponentType(type: string): boolean {+ return type === 'COMPONENT' || type === 'COMPONENT_SET' || type === 'INSTANCE'+ }+constructor(ck: CanvasKit, surface: Surface) {this.ck = ckthis.surface = surface@@ -256,8 +265,6 @@ export class SkiaRenderer {): void {if (selectedIds.size === 0) return- this.selectionPaint.setStrokeWidth(1)-this.drawParentFrameOutlines(canvas, graph, selectedIds)if (selectedIds.size === 1) {@@ -266,21 +273,34 @@ export class SkiaRenderer {const node = graph.getNode(id)if (!node) return+ const useComponentColor = this.isComponentType(node.type)+ this.selectionPaint.setColor(useComponentColor ? this.compColor() : this.selColor())+ this.selectionPaint.setStrokeWidth(1)+const rotation =overlays.rotationPreview?.nodeId === id ? overlays.rotationPreview.angle : node.rotationthis.drawNodeSelection(canvas, node, rotation, graph)this.drawSelectionLabels(canvas, graph, selectedIds)++ this.selectionPaint.setColor(this.selColor())return}for (const id of selectedIds) {const node = graph.getNode(id)if (!node) continue++ const useComponentColor = this.isComponentType(node.type)+ this.selectionPaint.setColor(useComponentColor ? this.compColor() : this.selColor())+ this.selectionPaint.setStrokeWidth(1)+const rotation =overlays.rotationPreview?.nodeId === id ? overlays.rotationPreview.angle : node.rotationthis.drawNodeOutline(canvas, node, rotation, graph)}+ this.selectionPaint.setColor(this.selColor())+const nodes = [...selectedIds].map((id) => graph.getNode(id)).filter((n): n is SceneNode => n !== undefined)@@ -381,14 +401,15 @@ export class SkiaRenderer {const sy2 = maxY * this.zoom + this.panYconst smx = (sx1 + sx2) / 2- // Frame name label — only for top-level frames (direct children of root)if (nodes.length === 1) {const node = nodes[0]const parentNode = node.parentId ? graph.getNode(node.parentId) : null- if (node.type === 'FRAME' && (!parentNode || parentNode.type === 'CANVAS' || parentNode.type === 'SECTION')) {+ const isTopLevel = !parentNode || parentNode.type === 'CANVAS' || parentNode.type === 'SECTION'+ const showLabel = (node.type === 'FRAME' && isTopLevel) || this.isComponentType(node.type)+ if (showLabel) {const labelPaint = new this.ck.Paint()labelPaint.setStyle(this.ck.PaintStyle.Fill)- labelPaint.setColor(this.selColor())+ labelPaint.setColor(this.isComponentType(node.type) ? this.compColor() : this.selColor())labelPaint.setAntiAlias(true)canvas.drawText(node.name, sx1, sy1 - LABEL_OFFSET_Y, labelPaint, this.labelFont)labelPaint.delete()@@ -408,9 +429,12 @@ export class SkiaRenderer {const pillX = smx - pillW / 2const pillY = sy2 + SIZE_PILL_PADDING_Y+ const allComponents = nodes.length > 0 && nodes.every((n) => this.isComponentType(n.type))+ const pillColor = allComponents ? this.compColor() : this.selColor()+const pillPaint = new this.ck.Paint()pillPaint.setStyle(this.ck.PaintStyle.Fill)- pillPaint.setColor(this.selColor())+ pillPaint.setColor(pillColor)pillPaint.setAntiAlias(true)const rrect = this.ck.RRectXY(this.ck.LTRBRect(pillX, pillY, pillX + pillW, pillY + pillH), SIZE_PILL_RADIUS, SIZE_PILL_RADIUS)@@ -664,6 +688,8 @@ export class SkiaRenderer {if (node.type === 'SECTION') {this.renderSection(canvas, node, graph)+ } else if (node.type === 'COMPONENT_SET') {+ this.renderComponentSet(canvas, node, graph)} else if (overlays.editingTextId !== nodeId) {this.renderShape(canvas, node, graph)}@@ -684,14 +710,15 @@ export class SkiaRenderer {const hoverPaint = new this.ck.Paint()hoverPaint.setStyle(this.ck.PaintStyle.Stroke)hoverPaint.setStrokeWidth(1 / this.zoom)- hoverPaint.setColor(this.selColor())+ hoverPaint.setColor(this.isComponentType(node.type) ? this.compColor() : this.selColor())hoverPaint.setAntiAlias(true)this.strokeNodeShape(canvas, node, hoverPaint)hoverPaint.delete()}// Clip + render children for containers- if (node.type === 'FRAME' && node.clipsContent && node.childIds.length > 0) {+ const isClippableContainer = node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'+ if (isClippableContainer && node.clipsContent && node.childIds.length > 0) {canvas.save()canvas.clipRect(this.ck.LTRBRect(0, 0, node.width, node.height),@@ -883,6 +910,33 @@ export class SkiaRenderer {}}+ private renderComponentSet(canvas: Canvas, node: SceneNode, graph: SceneGraph): void {+ const rect = this.ck.LTRBRect(0, 0, node.width, node.height)+ const rrect = this.ck.RRectXY(rect, 5, 5)++ for (const fill of node.fills) {+ if (!fill.visible) continue+ this.applyFill(fill, node, graph)+ this.fillPaint.setAlphaf(fill.opacity)+ canvas.drawRRect(rrect, this.fillPaint)+ this.fillPaint.setShader(null)+ }++ const dashPaint = new this.ck.Paint()+ dashPaint.setStyle(this.ck.PaintStyle.Stroke)+ dashPaint.setStrokeWidth(COMPONENT_SET_BORDER_WIDTH / this.zoom)+ dashPaint.setColor(this.compColor())+ dashPaint.setAntiAlias(true)+ dashPaint.setPathEffect(+ this.ck.PathEffect.MakeDash(+ [COMPONENT_SET_DASH / this.zoom, COMPONENT_SET_DASH_GAP / this.zoom],+ 0+ )+ )+ canvas.drawRRect(rrect, dashPaint)+ dashPaint.delete()+ }+private renderShape(canvas: Canvas, node: SceneNode, graph: SceneGraph): void {const rect = this.ck.LTRBRect(0, 0, node.width, node.height)diff --git a/src/engine/scene-graph.ts b/src/engine/scene-graph.tsindex 1fe1cc3b8..253f7be93 100644--- a/src/engine/scene-graph.ts+++ b/src/engine/scene-graph.ts@@ -202,6 +202,9 @@ export interface SceneNode {borderBottomWeight: numberborderLeftWeight: numberindependentStrokeWeights: boolean++ componentId: string | null+ overrides: Record<string, unknown>}let nextLocalID = 1@@ -276,6 +279,8 @@ function createDefaultNode(type: NodeType, overrides: Partial<SceneNode> = {}):borderBottomWeight: 0,borderLeftWeight: 0,independentStrokeWeights: false,+ componentId: null,+ overrides: {},...overrides}}@@ -524,6 +529,86 @@ export class SceneGraph {return best}+ cloneTree(sourceId: string, parentId: string, overrides: Partial<SceneNode> = {}): SceneNode | null {+ const src = this.nodes.get(sourceId)+ if (!src) return null++ const { id: _srcId, parentId: _srcParent, childIds: _srcChildren, ...rest } = src+ const clone = this.createNode(src.type, parentId, { ...rest, ...overrides })++ for (const childId of src.childIds) {+ this.cloneTree(childId, clone.id)+ }++ return clone+ }++ createInstance(componentId: string, parentId: string, overrides: Partial<SceneNode> = {}): SceneNode | null {+ const component = this.nodes.get(componentId)+ if (!component || component.type !== 'COMPONENT') return null++ const instance = this.createNode('INSTANCE', parentId, {+ name: component.name,+ width: component.width,+ height: component.height,+ fills: component.fills.map((f) => ({ ...f })),+ strokes: component.strokes.map((s) => ({ ...s })),+ effects: component.effects.map((e) => ({ ...e })),+ opacity: component.opacity,+ cornerRadius: component.cornerRadius,+ topLeftRadius: component.topLeftRadius,+ topRightRadius: component.topRightRadius,+ bottomRightRadius: component.bottomRightRadius,+ bottomLeftRadius: component.bottomLeftRadius,+ independentCorners: component.independentCorners,+ layoutMode: component.layoutMode,+ layoutWrap: component.layoutWrap,+ primaryAxisAlign: component.primaryAxisAlign,+ counterAxisAlign: component.counterAxisAlign,+ primaryAxisSizing: component.primaryAxisSizing,+ counterAxisSizing: component.counterAxisSizing,+ itemSpacing: component.itemSpacing,+ counterAxisSpacing: component.counterAxisSpacing,+ paddingTop: component.paddingTop,+ paddingRight: component.paddingRight,+ paddingBottom: component.paddingBottom,+ paddingLeft: component.paddingLeft,+ clipsContent: component.clipsContent,+ componentId,+ ...overrides+ })++ for (const childId of component.childIds) {+ this.cloneTree(childId, instance.id)+ }++ return instance+ }++ detachInstance(instanceId: string): void {+ const node = this.nodes.get(instanceId)+ if (!node || node.type !== 'INSTANCE') return+ node.type = 'FRAME'+ node.componentId = null+ node.overrides = {}+ }++ getMainComponent(instanceId: string): SceneNode | undefined {+ const node = this.nodes.get(instanceId)+ if (!node?.componentId) return undefined+ return this.nodes.get(node.componentId)+ }++ getInstances(componentId: string): SceneNode[] {+ const instances: SceneNode[] = []+ for (const node of this.nodes.values()) {+ if (node.type === 'INSTANCE' && node.componentId === componentId) {+ instances.push(node)+ }+ }+ return instances+ }+flattenTree(parentId?: string, depth = 0): Array<{ node: SceneNode; depth: number }> {const id = parentId ?? this.rootIdconst parent = this.nodes.get(id)diff --git a/src/stores/editor.ts b/src/stores/editor.tsindex b1ee55fe3..72fd0e93b 100644--- a/src/stores/editor.ts+++ b/src/stores/editor.ts@@ -789,6 +789,259 @@ export function createEditorStore() {requestRender()}+ function createComponentFromSelection() {+ const nodes = selectedNodes.value+ if (nodes.length === 0) return++ const prevSelection = new Set(state.selectedIds)++ if (nodes.length === 1) {+ const node = nodes[0]+ const prevType = node.type++ if (node.type === 'COMPONENT') return++ if (node.type === 'FRAME' || node.type === 'GROUP') {+ graph.updateNode(node.id, { type: 'COMPONENT' })+ state.selectedIds = new Set([node.id])+ undo.push({+ label: 'Create component',+ forward: () => {+ graph.updateNode(node.id, { type: 'COMPONENT' })+ state.selectedIds = new Set([node.id])+ requestRender()+ },+ inverse: () => {+ graph.updateNode(node.id, { type: prevType })+ state.selectedIds = prevSelection+ requestRender()+ }+ })+ requestRender()+ return+ }+ }++ const parentId = nodes[0].parentId ?? state.currentPageId+ const sameParent = nodes.every((n) => (n.parentId ?? state.currentPageId) === parentId)+ if (!sameParent) return++ const parent = graph.getNode(parentId)+ if (!parent) return++ const nodeIds = nodes.map((n) => n.id)+ const origPositions = nodes.map((n) => ({ id: n.id, x: n.x, y: n.y }))++ let minX = Infinity+ let minY = Infinity+ let maxX = -Infinity+ let maxY = -Infinity+ for (const n of nodes) {+ const abs = graph.getAbsolutePosition(n.id)+ minX = Math.min(minX, abs.x)+ minY = Math.min(minY, abs.y)+ maxX = Math.max(maxX, abs.x + n.width)+ maxY = Math.max(maxY, abs.y + n.height)+ }++ const parentAbs = isTopLevel(parentId) ? { x: 0, y: 0 } : graph.getAbsolutePosition(parentId)+ const firstIndex = Math.min(...nodeIds.map((id) => parent.childIds.indexOf(id)))++ const component = graph.createNode('COMPONENT', parentId, {+ name: 'Component',+ x: minX - parentAbs.x,+ y: minY - parentAbs.y,+ width: maxX - minX,+ height: maxY - minY,+ fills: [{ type: 'SOLID', color: { r: 1, g: 1, b: 1, a: 1 }, opacity: 1, visible: true }]+ })+ const componentId = component.id++ parent.childIds = parent.childIds.filter((id) => id !== componentId)+ parent.childIds.splice(firstIndex, 0, componentId)++ for (const n of nodes) {+ graph.reparentNode(n.id, componentId)+ }++ state.selectedIds = new Set([componentId])++ undo.push({+ label: 'Create component',+ forward: () => {+ const c = graph.createNode('COMPONENT', parentId, { ...component })+ parent.childIds = parent.childIds.filter((id) => id !== c.id)+ parent.childIds.splice(firstIndex, 0, c.id)+ for (const n of origPositions) graph.reparentNode(n.id, c.id)+ state.selectedIds = new Set([c.id])+ requestRender()+ },+ inverse: () => {+ for (const orig of origPositions) {+ graph.reparentNode(orig.id, parentId)+ graph.updateNode(orig.id, { x: orig.x, y: orig.y })+ }+ graph.deleteNode(componentId)+ state.selectedIds = prevSelection+ requestRender()+ }+ })+ requestRender()+ }++ function createComponentSetFromComponents() {+ const nodes = selectedNodes.value+ if (nodes.length < 2) return+ if (!nodes.every((n) => n.type === 'COMPONENT')) return++ const parentId = nodes[0].parentId ?? state.currentPageId+ const sameParent = nodes.every((n) => (n.parentId ?? state.currentPageId) === parentId)+ if (!sameParent) return++ const parent = graph.getNode(parentId)+ if (!parent) return++ const prevSelection = new Set(state.selectedIds)+ const nodeIds = nodes.map((n) => n.id)+ const origPositions = nodes.map((n) => ({ id: n.id, x: n.x, y: n.y }))++ let minX = Infinity+ let minY = Infinity+ let maxX = -Infinity+ let maxY = -Infinity+ for (const n of nodes) {+ const abs = graph.getAbsolutePosition(n.id)+ minX = Math.min(minX, abs.x)+ minY = Math.min(minY, abs.y)+ maxX = Math.max(maxX, abs.x + n.width)+ maxY = Math.max(maxY, abs.y + n.height)+ }++ const padding = 40+ const parentAbs = isTopLevel(parentId) ? { x: 0, y: 0 } : graph.getAbsolutePosition(parentId)+ const firstIndex = Math.min(...nodeIds.map((id) => parent.childIds.indexOf(id)))++ const componentSet = graph.createNode('COMPONENT_SET', parentId, {+ name: nodes[0].name.split('/')[0]?.trim() || 'Component Set',+ x: minX - parentAbs.x - padding,+ y: minY - parentAbs.y - padding,+ width: maxX - minX + padding * 2,+ height: maxY - minY + padding * 2,+ fills: [{ type: 'SOLID', color: { r: 0.96, g: 0.96, b: 0.96, a: 1 }, opacity: 1, visible: true }]+ })+ const setId = componentSet.id++ parent.childIds = parent.childIds.filter((id) => id !== setId)+ parent.childIds.splice(firstIndex, 0, setId)++ for (const n of nodes) {+ graph.reparentNode(n.id, setId)+ }++ state.selectedIds = new Set([setId])++ undo.push({+ label: 'Create component set',+ forward: () => {+ const cs = graph.createNode('COMPONENT_SET', parentId, { ...componentSet })+ parent.childIds = parent.childIds.filter((id) => id !== cs.id)+ parent.childIds.splice(firstIndex, 0, cs.id)+ for (const n of origPositions) graph.reparentNode(n.id, cs.id)+ state.selectedIds = new Set([cs.id])+ requestRender()+ },+ inverse: () => {+ for (const orig of origPositions) {+ graph.reparentNode(orig.id, parentId)+ graph.updateNode(orig.id, { x: orig.x, y: orig.y })+ }+ graph.deleteNode(setId)+ state.selectedIds = prevSelection+ requestRender()+ }+ })+ requestRender()+ }++ function createInstanceFromComponent(componentId: string, x?: number, y?: number) {+ const component = graph.getNode(componentId)+ if (!component || component.type !== 'COMPONENT') return null++ const parentId = component.parentId ?? state.currentPageId+ const instance = graph.createInstance(componentId, parentId, {+ x: x ?? component.x + component.width + 40,+ y: y ?? component.y+ })+ if (!instance) return null++ const instanceId = instance.id+ state.selectedIds = new Set([instanceId])++ undo.push({+ label: 'Create instance',+ forward: () => {+ graph.createInstance(componentId, parentId, { ...instance })+ state.selectedIds = new Set([instanceId])+ requestRender()+ },+ inverse: () => {+ graph.deleteNode(instanceId)+ state.selectedIds = new Set([componentId])+ requestRender()+ }+ })+ requestRender()+ return instanceId+ }++ function detachInstance() {+ const node = selectedNode.value+ if (!node || node.type !== 'INSTANCE') return++ const prevComponentId = node.componentId++ graph.detachInstance(node.id)+ state.selectedIds = new Set([node.id])++ undo.push({+ label: 'Detach instance',+ forward: () => {+ graph.detachInstance(node.id)+ requestRender()+ },+ inverse: () => {+ graph.updateNode(node.id, { type: 'INSTANCE', componentId: prevComponentId, overrides: {} })+ requestRender()+ }+ })+ requestRender()+ }++ function goToMainComponent() {+ const node = selectedNode.value+ if (!node?.componentId) return+ const main = graph.getMainComponent(node.id)+ if (!main) return++ // Find which page the main component is on+ let current: SceneNode | undefined = main+ while (current && current.type !== 'CANVAS') {+ current = current.parentId ? graph.getNode(current.parentId) : undefined+ }+ if (current && current.id !== state.currentPageId) {+ switchPage(current.id)+ }++ state.selectedIds = new Set([main.id])++ const abs = graph.getAbsolutePosition(main.id)+ const viewW = 800+ const viewH = 600+ state.panX = viewW / 2 - (abs.x + main.width / 2) * state.zoom+ state.panY = viewH / 2 - (abs.y + main.height / 2) * state.zoom+ requestRender()+ }+function ungroupSelected() {const node = selectedNode.valueif (!node || node.type !== 'GROUP') return@@ -1290,6 +1543,11 @@ export function createEditorStore() {wrapInAutoLayout,groupSelected,ungroupSelected,+ createComponentFromSelection,+ createComponentSetFromComponents,+ createInstanceFromComponent,+ detachInstance,+ goToMainComponent,createShape,adoptNodesIntoSection,duplicateSelected,