Skip to content

AI-native design editor. Open-source Figma alternative.

Add nested frames with reparenting and relative coordinates

Danila Poyarkov7d483f340b52
diff --git a/src/components/LayersPanel.vue b/src/components/LayersPanel.vue
index deb1a4cef..2adc0944f 100644
--- a/src/components/LayersPanel.vue
+++ b/src/components/LayersPanel.vue
@@ -9,6 +9,8 @@ function nodeIcon(type: string) {
return '○'
case 'FRAME':
return '⊞'
+ case 'GROUP':
+ return '⊟'
case 'LINE':
return '╱'
case 'TEXT':
@@ -17,6 +19,11 @@ function nodeIcon(type: string) {
return '□'
}
}
+
+function hasChildren(nodeId: string) {
+ const node = store.graph.getNode(nodeId)
+ return node ? node.childIds.length > 0 : false
+}
</script>
<template>
@@ -24,12 +31,18 @@ function nodeIcon(type: string) {
<header class="panel-section-label">Layers</header>
<div class="layers-list">
<button
- v-for="node in store.layerNodes.value"
+ v-for="{ node, depth } in store.layerTree.value"
:key="node.id"
class="layer-row"
- :class="{ selected: store.state.selectedIds.has(node.id) }"
+ :class="{
+ selected: store.state.selectedIds.has(node.id),
+ container: hasChildren(node.id)
+ }"
+ :style="{ paddingLeft: `${8 + depth * 16}px` }"
@click="store.select([node.id])"
+ @click.shift.exact="store.select([node.id], true)"
>
+ <span v-if="hasChildren(node.id)" class="expand-arrow">▾</span>
<span class="layer-icon">{{ nodeIcon(node.type) }}</span>
<span class="layer-name">{{ node.name }}</span>
</button>
@@ -65,7 +78,7 @@ function nodeIcon(type: string) {
.layer-row {
display: flex;
align-items: center;
- gap: 6px;
+ gap: 4px;
width: 100%;
padding: 4px 8px;
border: none;
@@ -87,11 +100,20 @@ function nodeIcon(type: string) {
color: white;
}
+.expand-arrow {
+ width: 12px;
+ font-size: 10px;
+ text-align: center;
+ flex-shrink: 0;
+ opacity: 0.5;
+}
+
.layer-icon {
- width: 16px;
+ width: 14px;
text-align: center;
flex-shrink: 0;
opacity: 0.7;
+ font-size: 11px;
}
.layer-name {
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index b9299340c..353879b94 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -79,17 +79,33 @@ const HANDLE_CURSORS: Record<HandlePosition, string> = {
w: 'ew-resize'
}
-function getScreenRect(node: SceneNode, zoom: number, panX: number, panY: number) {
+function getScreenRect(
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
+ zoom: number,
+ panX: number,
+ panY: number
+) {
return {
- x1: node.x * zoom + panX,
- y1: node.y * zoom + panY,
- x2: (node.x + node.width) * zoom + panX,
- y2: (node.y + node.height) * zoom + panY
+ x1: absX * zoom + panX,
+ y1: absY * zoom + panY,
+ x2: (absX + w) * zoom + panX,
+ y2: (absY + h) * zoom + panY
}
}
-function getHandlePositions(node: SceneNode, zoom: number, panX: number, panY: number) {
- const { x1, y1, x2, y2 } = getScreenRect(node, zoom, panX, panY)
+function getHandlePositions(
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
+ zoom: number,
+ panX: number,
+ panY: number
+) {
+ const { x1, y1, x2, y2 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
const mx = (x1 + x2) / 2
const my = (y1 + y2) / 2
@@ -108,12 +124,15 @@ function getHandlePositions(node: SceneNode, zoom: number, panX: number, panY: n
function hitTestHandle(
sx: number,
sy: number,
- node: SceneNode,
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
zoom: number,
panX: number,
panY: number
): HandlePosition | null {
- const handles = getHandlePositions(node, zoom, panX, panY)
+ const handles = getHandlePositions(absX, absY, w, h, zoom, panX, panY)
for (const [pos, pt] of Object.entries(handles)) {
if (Math.abs(sx - pt.x) < HANDLE_HIT_RADIUS && Math.abs(sy - pt.y) < HANDLE_HIT_RADIUS) {
return pos as HandlePosition
@@ -125,12 +144,15 @@ function hitTestHandle(
function hitTestRotationHandle(
sx: number,
sy: number,
- node: SceneNode,
+ absX: number,
+ absY: number,
+ w: number,
+ h: number,
zoom: number,
panX: number,
panY: number
): boolean {
- const { x1, x2, y1 } = getScreenRect(node, zoom, panX, panY)
+ const { x1, x2, y1 } = getScreenRect(absX, absY, w, h, zoom, panX, panY)
const mx = (x1 + x2) / 2
const rotY = y1 - 24
return Math.abs(sx - mx) < ROTATION_HIT_RADIUS && Math.abs(sy - rotY) < ROTATION_HIT_RADIUS
@@ -181,22 +203,34 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
if (store.state.selectedIds.size === 1) {
const id = [...store.state.selectedIds][0]
const node = store.graph.getNode(id)
- if (
- node &&
- hitTestRotationHandle(sx, sy, node, store.state.zoom, store.state.panX, store.state.panY)
- ) {
- const screenCx = (node.x + node.width / 2) * store.state.zoom + store.state.panX
- const screenCy = (node.y + node.height / 2) * store.state.zoom + store.state.panY
- const startAngle = Math.atan2(sy - screenCy, sx - screenCx) * (180 / Math.PI)
- drag.value = {
- type: 'rotate',
- nodeId: id,
- centerX: screenCx,
- centerY: screenCy,
- startAngle,
- origRotation: node.rotation
+ if (node) {
+ const abs = store.graph.getAbsolutePosition(id)
+ if (
+ hitTestRotationHandle(
+ sx,
+ sy,
+ abs.x,
+ abs.y,
+ node.width,
+ node.height,
+ store.state.zoom,
+ store.state.panX,
+ store.state.panY
+ )
+ ) {
+ const screenCx = (abs.x + node.width / 2) * store.state.zoom + store.state.panX
+ const screenCy = (abs.y + node.height / 2) * store.state.zoom + store.state.panY
+ const startAngle = Math.atan2(sy - screenCy, sx - screenCx) * (180 / Math.PI)
+ drag.value = {
+ type: 'rotate',
+ nodeId: id,
+ centerX: screenCx,
+ centerY: screenCy,
+ startAngle,
+ origRotation: node.rotation
+ }
+ return
}
- return
}
}
@@ -204,10 +238,14 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
for (const id of store.state.selectedIds) {
const node = store.graph.getNode(id)
if (!node) continue
+ const abs = store.graph.getAbsolutePosition(id)
const handle = hitTestHandle(
sx,
sy,
- node,
+ abs.x,
+ abs.y,
+ node.width,
+ node.height,
store.state.zoom,
store.state.panX,
store.state.panY
@@ -310,12 +348,24 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
if (store.state.selectedIds.size === 1) {
const id = [...store.state.selectedIds][0]
const node = store.graph.getNode(id)
- if (
- node &&
- hitTestRotationHandle(sx, sy, node, store.state.zoom, store.state.panX, store.state.panY)
- ) {
- cursor =
- "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2'%3E%3Cpath d='M21 2v6h-6'/%3E%3Cpath d='M21 13a9 9 0 1 1-3-7.7L21 8'/%3E%3C/svg%3E\") 12 12, pointer"
+ if (node) {
+ const abs = store.graph.getAbsolutePosition(id)
+ if (
+ hitTestRotationHandle(
+ sx,
+ sy,
+ abs.x,
+ abs.y,
+ node.width,
+ node.height,
+ store.state.zoom,
+ store.state.panX,
+ store.state.panY
+ )
+ ) {
+ cursor =
+ "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2'%3E%3Cpath d='M21 2v6h-6'/%3E%3Cpath d='M21 13a9 9 0 1 1-3-7.7L21 8'/%3E%3C/svg%3E\") 12 12, pointer"
+ }
}
}
@@ -323,10 +373,14 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
for (const id of store.state.selectedIds) {
const node = store.graph.getNode(id)
if (!node) continue
+ const abs = store.graph.getAbsolutePosition(id)
const handle = hitTestHandle(
sx,
sy,
- node,
+ abs.x,
+ abs.y,
+ node.width,
+ node.height,
store.state.zoom,
store.state.panX,
store.state.panY
@@ -374,15 +428,19 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
let dx = cx - d.startX
let dy = cy - d.startY
- // Compute snap
+ // Compute snap using absolute positions
const selectedNodes: SceneNode[] = []
for (const [id, orig] of d.originals) {
const n = store.graph.getNode(id)
if (n) {
+ const abs = store.graph.getAbsolutePosition(id)
+ const parentAbs = n.parentId
+ ? store.graph.getAbsolutePosition(n.parentId)
+ : { x: 0, y: 0 }
selectedNodes.push({
...n,
- x: orig.x + dx,
- y: orig.y + dy
+ x: abs.x - parentAbs.x - n.x + orig.x + dx,
+ y: abs.y - parentAbs.y - n.y + orig.y + dy
})
}
}
@@ -399,6 +457,11 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
for (const [id, orig] of d.originals) {
store.updateNode(id, { x: Math.round(orig.x + dx), y: Math.round(orig.y + dy) })
}
+
+ // Detect frame drop target
+ const dropTarget = store.graph.hitTestFrame(cx, cy, store.state.selectedIds)
+ store.setDropTarget(dropTarget?.id ?? null)
+
return
}
@@ -513,6 +576,13 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
if (d.type === 'move') {
store.commitMove(d.originals)
store.setSnapGuides([])
+
+ // Reparent into frame if dropped on one
+ const dropId = store.state.dropTargetId
+ if (dropId) {
+ store.reparentNodes([...store.state.selectedIds], dropId)
+ store.setDropTarget(null)
+ }
}
if (d.type === 'rotate') {
diff --git a/src/composables/use-canvas.ts b/src/composables/use-canvas.ts
index 3c52fccfd..30e2060d9 100644
--- a/src/composables/use-canvas.ts
+++ b/src/composables/use-canvas.ts
@@ -54,7 +54,8 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Edito
renderer.render(store.graph, store.state.selectedIds, {
marquee: store.state.marquee,
snapGuides: store.state.snapGuides,
- rotationPreview: store.state.rotationPreview
+ rotationPreview: store.state.rotationPreview,
+ dropTargetId: store.state.dropTargetId
})
}
diff --git a/src/engine/renderer.ts b/src/engine/renderer.ts
index 66cde84c1..678373b81 100644
--- a/src/engine/renderer.ts
+++ b/src/engine/renderer.ts
@@ -6,6 +6,7 @@ export interface RenderOverlays {
marquee?: { x: number; y: number; width: number; height: number } | null
snapGuides?: SnapGuide[]
rotationPreview?: { nodeId: string; angle: number } | null
+ dropTargetId?: string | null
}
export class SkiaRenderer {
@@ -62,7 +63,7 @@ export class SkiaRenderer {
const root = graph.getNode(graph.rootId)
if (root) {
for (const childId of root.childIds) {
- this.renderNode(canvas, graph, childId, overlays.rotationPreview)
+ this.renderNode(canvas, graph, childId, overlays)
}
}
@@ -72,7 +73,7 @@ export class SkiaRenderer {
canvas.save()
canvas.scale(this.dpr, this.dpr)
- this.drawSelection(canvas, graph, selectedIds, overlays.rotationPreview)
+ this.drawSelection(canvas, graph, selectedIds, overlays)
this.drawSnapGuides(canvas, overlays.snapGuides)
this.drawMarquee(canvas, overlays.marquee)
@@ -86,7 +87,7 @@ export class SkiaRenderer {
canvas: Canvas,
graph: SceneGraph,
selectedIds: Set<string>,
- rotationPreview?: { nodeId: string; angle: number } | null
+ overlays: RenderOverlays
): void {
if (selectedIds.size === 0) return
@@ -97,29 +98,35 @@ export class SkiaRenderer {
const node = graph.getNode(id)
if (!node) return
- const rotation = rotationPreview?.nodeId === id ? rotationPreview.angle : node.rotation
- this.drawNodeSelection(canvas, node, rotation)
+ const rotation =
+ overlays.rotationPreview?.nodeId === id ? overlays.rotationPreview.angle : node.rotation
+ this.drawNodeSelection(canvas, node, rotation, graph)
return
}
- // Multi-select: individual outlines + unified bounding box
for (const id of selectedIds) {
const node = graph.getNode(id)
if (!node) continue
- const rotation = rotationPreview?.nodeId === id ? rotationPreview.angle : node.rotation
- this.drawNodeOutline(canvas, node, rotation)
+ const rotation =
+ overlays.rotationPreview?.nodeId === id ? overlays.rotationPreview.angle : node.rotation
+ this.drawNodeOutline(canvas, node, rotation, graph)
}
- // Unified bounding box
const nodes = [...selectedIds]
.map((id) => graph.getNode(id))
.filter((n): n is SceneNode => n !== undefined)
- this.drawGroupBounds(canvas, nodes)
+ this.drawGroupBounds(canvas, nodes, graph)
}
- private drawNodeSelection(canvas: Canvas, node: SceneNode, rotation: number): void {
- const cx = (node.x + node.width / 2) * this.zoom + this.panX
- const cy = (node.y + node.height / 2) * this.zoom + this.panY
+ private drawNodeSelection(
+ canvas: Canvas,
+ node: SceneNode,
+ rotation: number,
+ graph: SceneGraph
+ ): void {
+ const abs = graph.getAbsolutePosition(node.id)
+ const cx = (abs.x + node.width / 2) * this.zoom + this.panX
+ const cy = (abs.y + node.height / 2) * this.zoom + this.panY
const hw = (node.width / 2) * this.zoom
const hh = (node.height / 2) * this.zoom
@@ -170,9 +177,15 @@ export class SkiaRenderer {
canvas.restore()
}
- private drawNodeOutline(canvas: Canvas, node: SceneNode, rotation: number): void {
- const cx = (node.x + node.width / 2) * this.zoom + this.panX
- const cy = (node.y + node.height / 2) * this.zoom + this.panY
+ private drawNodeOutline(
+ canvas: Canvas,
+ node: SceneNode,
+ rotation: number,
+ graph: SceneGraph
+ ): void {
+ const abs = graph.getAbsolutePosition(node.id)
+ const cx = (abs.x + node.width / 2) * this.zoom + this.panX
+ const cy = (abs.y + node.height / 2) * this.zoom + this.panY
const hw = (node.width / 2) * this.zoom
const hh = (node.height / 2) * this.zoom
@@ -185,16 +198,16 @@ export class SkiaRenderer {
canvas.restore()
}
- private drawGroupBounds(canvas: Canvas, nodes: SceneNode[]): void {
+ private drawGroupBounds(canvas: Canvas, nodes: SceneNode[], graph: SceneGraph): void {
let minX = Infinity
let minY = Infinity
let maxX = -Infinity
let maxY = -Infinity
for (const n of nodes) {
- // For rotated nodes, use AABB of the rotated corners
+ const abs = graph.getAbsolutePosition(n.id)
if (n.rotation !== 0) {
- const corners = this.getRotatedCorners(n)
+ const corners = this.getRotatedCorners(n, abs)
for (const c of corners) {
minX = Math.min(minX, c.x)
minY = Math.min(minY, c.y)
@@ -202,10 +215,10 @@ export class SkiaRenderer {
maxY = Math.max(maxY, c.y)
}
} else {
- const x1 = n.x * this.zoom + this.panX
- const y1 = n.y * this.zoom + this.panY
- const x2 = (n.x + n.width) * this.zoom + this.panX
- const y2 = (n.y + n.height) * this.zoom + this.panY
+ const x1 = abs.x * this.zoom + this.panX
+ const y1 = abs.y * this.zoom + this.panY
+ const x2 = (abs.x + n.width) * this.zoom + this.panX
+ const y2 = (abs.y + n.height) * this.zoom + this.panY
minX = Math.min(minX, x1)
minY = Math.min(minY, y1)
maxX = Math.max(maxX, x2)
@@ -237,9 +250,9 @@ export class SkiaRenderer {
dashPaint.delete()
}
- private getRotatedCorners(n: SceneNode) {
- const cx = (n.x + n.width / 2) * this.zoom + this.panX
- const cy = (n.y + n.height / 2) * this.zoom + this.panY
+ private getRotatedCorners(n: SceneNode, abs: { x: number; y: number }) {
+ const cx = (abs.x + n.width / 2) * this.zoom + this.panX
+ const cy = (abs.y + n.height / 2) * this.zoom + this.panY
const hw = (n.width / 2) * this.zoom
const hh = (n.height / 2) * this.zoom
const rad = (n.rotation * Math.PI) / 180
@@ -314,12 +327,13 @@ export class SkiaRenderer {
canvas: Canvas,
graph: SceneGraph,
nodeId: string,
- rotationPreview?: { nodeId: string; angle: number } | null
+ overlays: RenderOverlays
): void {
const node = graph.getNode(nodeId)
if (!node || !node.visible) return
canvas.save()
+ canvas.translate(node.x, node.y)
if (node.opacity < 1) {
const layerPaint = new this.ck.Paint()
@@ -328,26 +342,41 @@ export class SkiaRenderer {
layerPaint.delete()
}
- const rotation = rotationPreview?.nodeId === nodeId ? rotationPreview.angle : node.rotation
+ const rotation =
+ overlays.rotationPreview?.nodeId === nodeId ? overlays.rotationPreview.angle : node.rotation
if (rotation !== 0) {
- canvas.rotate(rotation, node.x + node.width / 2, node.y + node.height / 2)
+ canvas.rotate(rotation, node.width / 2, node.height / 2)
}
- // Clip children for frames
- if (node.type === 'FRAME') {
- const clipRect = this.ck.LTRBRect(node.x, node.y, node.x + node.width, node.y + node.height)
- this.renderShape(canvas, node)
+ this.renderShape(canvas, node)
+
+ // Drop target highlight
+ if (overlays.dropTargetId === nodeId) {
+ const highlight = new this.ck.Paint()
+ highlight.setStyle(this.ck.PaintStyle.Stroke)
+ highlight.setStrokeWidth(2 / this.zoom)
+ highlight.setColor(this.ck.Color4f(0.23, 0.51, 0.96, 0.8))
+ highlight.setAntiAlias(true)
+ canvas.drawRect(this.ck.LTRBRect(0, 0, node.width, node.height), highlight)
+ highlight.delete()
+ }
+
+ // Clip + render children for containers
+ if (node.type === 'FRAME' && node.childIds.length > 0) {
canvas.save()
- canvas.clipRect(clipRect, this.ck.ClipOp.Intersect, true)
+ canvas.clipRect(
+ this.ck.LTRBRect(0, 0, node.width, node.height),
+ this.ck.ClipOp.Intersect,
+ true
+ )
for (const childId of node.childIds) {
- this.renderNode(canvas, graph, childId, rotationPreview)
+ this.renderNode(canvas, graph, childId, overlays)
}
canvas.restore()
} else {
- this.renderShape(canvas, node)
for (const childId of node.childIds) {
- this.renderNode(canvas, graph, childId, rotationPreview)
+ this.renderNode(canvas, graph, childId, overlays)
}
}
@@ -358,7 +387,7 @@ export class SkiaRenderer {
}
private renderShape(canvas: Canvas, node: SceneNode): void {
- const rect = this.ck.LTRBRect(node.x, node.y, node.x + node.width, node.y + node.height)
+ const rect = this.ck.LTRBRect(0, 0, node.width, node.height)
const hasRadius =
node.cornerRadius > 0 ||
@@ -382,16 +411,16 @@ export class SkiaRenderer {
this.renderText(canvas, node)
break
case 'LINE':
- canvas.drawLine(node.x, node.y, node.x + node.width, node.y + node.height, this.fillPaint)
+ canvas.drawLine(0, 0, node.width, node.height, this.fillPaint)
break
default:
if (hasRadius) {
if (node.independentCorners) {
const rrect = new Float32Array([
- node.x,
- node.y,
- node.x + node.width,
- node.y + node.height,
+ 0,
+ 0,
+ node.width,
+ node.height,
node.topLeftRadius,
node.topLeftRadius,
node.topRightRadius,
@@ -441,7 +470,7 @@ export class SkiaRenderer {
const text = (node as SceneNode & { text?: string }).text ?? ''
if (!text) return
- canvas.drawText(text, node.x, node.y + 14, this.fillPaint, this.textFont)
+ canvas.drawText(text, 0, 14, this.fillPaint, this.textFont)
}
private applyFill(fill: Fill): void {
diff --git a/src/engine/scene-graph.ts b/src/engine/scene-graph.ts
index c8f5a0b41..dd0dcd333 100644
--- a/src/engine/scene-graph.ts
+++ b/src/engine/scene-graph.ts
@@ -75,7 +75,6 @@ export interface SceneNode {
visible: boolean
locked: boolean
- // Text-specific
text: string
fontSize: number
fontFamily: string
@@ -127,6 +126,8 @@ function createDefaultNode(type: NodeType, overrides: Partial<SceneNode> = {}):
}
}
+const CONTAINER_TYPES = new Set<NodeType>(['FRAME', 'GROUP', 'SECTION'])
+
export class SceneGraph {
nodes = new Map<string, SceneNode>()
rootId: string
@@ -153,6 +154,43 @@ export class SceneGraph {
.filter((n): n is SceneNode => n !== undefined)
}
+ isContainer(id: string): boolean {
+ const node = this.nodes.get(id)
+ return node ? CONTAINER_TYPES.has(node.type) : false
+ }
+
+ isDescendant(childId: string, ancestorId: string): boolean {
+ let current = this.nodes.get(childId)
+ while (current) {
+ if (current.id === ancestorId) return true
+ current = current.parentId ? this.nodes.get(current.parentId) : undefined
+ }
+ return false
+ }
+
+ getAbsolutePosition(id: string): { x: number; y: number } {
+ let ax = 0
+ let ay = 0
+ let current = this.nodes.get(id)
+ while (current && current.id !== this.rootId) {
+ ax += current.x
+ ay += current.y
+ current = current.parentId ? this.nodes.get(current.parentId) : undefined
+ }
+ return { x: ax, y: ay }
+ }
+
+ getAbsoluteBounds(id: string): { x: number; y: number; width: number; height: number } {
+ const pos = this.getAbsolutePosition(id)
+ const node = this.nodes.get(id)
+ return {
+ x: pos.x,
+ y: pos.y,
+ width: node?.width ?? 0,
+ height: node?.height ?? 0
+ }
+ }
+
createNode(type: NodeType, parentId: string, overrides: Partial<SceneNode> = {}): SceneNode {
const node = createDefaultNode(type, overrides)
node.parentId = parentId
@@ -172,11 +210,39 @@ export class SceneGraph {
Object.assign(node, changes)
}
+ reparentNode(nodeId: string, newParentId: string): void {
+ const node = this.nodes.get(nodeId)
+ if (!node || nodeId === this.rootId) return
+ if (this.isDescendant(newParentId, nodeId)) return
+
+ const oldParent = node.parentId ? this.nodes.get(node.parentId) : undefined
+ const newParent = this.nodes.get(newParentId)
+ if (!newParent) return
+ if (node.parentId === newParentId) return
+
+ // Convert absolute position
+ const absPos = this.getAbsolutePosition(nodeId)
+ const newParentAbs =
+ newParentId === this.rootId ? { x: 0, y: 0 } : this.getAbsolutePosition(newParentId)
+
+ // Remove from old parent
+ if (oldParent) {
+ oldParent.childIds = oldParent.childIds.filter((cid) => cid !== nodeId)
+ }
+
+ // Add to new parent
+ node.parentId = newParentId
+ newParent.childIds.push(nodeId)
+
+ // Adjust position so node stays in same visual place
+ node.x = absPos.x - newParentAbs.x
+ node.y = absPos.y - newParentAbs.y
+ }
+
deleteNode(id: string): void {
const node = this.nodes.get(id)
if (!node || id === this.rootId) return
- // Remove from parent
if (node.parentId) {
const parent = this.nodes.get(node.parentId)
if (parent) {
@@ -184,7 +250,6 @@ export class SceneGraph {
}
}
- // Delete children recursively
for (const childId of Array.from(node.childIds)) {
this.deleteNode(childId)
}
@@ -192,16 +257,95 @@ export class SceneGraph {
this.nodes.delete(id)
}
- hitTest(px: number, py: number): SceneNode | null {
+ hitTest(px: number, py: number, scopeId?: string): SceneNode | null {
+ const scope = scopeId ?? this.rootId
+ return this.hitTestChildren(px, py, scope, 0, 0)
+ }
+
+ private hitTestChildren(
+ px: number,
+ py: number,
+ parentId: string,
+ offsetX: number,
+ offsetY: number
+ ): SceneNode | null {
+ const parent = this.nodes.get(parentId)
+ if (!parent) return null
+
// Reverse order = topmost first
- const allNodes = [...this.nodes.values()].filter((n) => n.id !== this.rootId && n.visible)
+ for (let i = parent.childIds.length - 1; i >= 0; i--) {
+ const childId = parent.childIds[i]
+ const child = this.nodes.get(childId)
+ if (!child || !child.visible) continue
+
+ const ax = offsetX + child.x
+ const ay = offsetY + child.y
- for (let i = allNodes.length - 1; i >= 0; i--) {
- const n = allNodes[i]
- if (px >= n.x && px <= n.x + n.width && py >= n.y && py <= n.y + n.height) {
- return n
+ // Check children first (deeper hit)
+ if (CONTAINER_TYPES.has(child.type)) {
+ const deepHit = this.hitTestChildren(px, py, childId, ax, ay)
+ if (deepHit) return deepHit
+ }
+
+ if (px >= ax && px <= ax + child.width && py >= ay && py <= ay + child.height) {
+ return child
}
}
return null
}
+
+ hitTestFrame(px: number, py: number, excludeIds: Set<string>): SceneNode | null {
+ return this.hitTestFrameChildren(px, py, this.rootId, 0, 0, excludeIds)
+ }
+
+ private hitTestFrameChildren(
+ px: number,
+ py: number,
+ parentId: string,
+ offsetX: number,
+ offsetY: number,
+ excludeIds: Set<string>
+ ): SceneNode | null {
+ const parent = this.nodes.get(parentId)
+ if (!parent) return null
+
+ // Deepest matching frame wins
+ let best: SceneNode | null = null
+
+ for (const childId of parent.childIds) {
+ if (excludeIds.has(childId)) continue
+ const child = this.nodes.get(childId)
+ if (!child || !child.visible) continue
+
+ const ax = offsetX + child.x
+ const ay = offsetY + child.y
+
+ if (!CONTAINER_TYPES.has(child.type)) continue
+ if (px < ax || px > ax + child.width || py < ay || py > ay + child.height) continue
+
+ best = child
+
+ const deeper = this.hitTestFrameChildren(px, py, childId, ax, ay, excludeIds)
+ if (deeper) best = deeper
+ }
+
+ return best
+ }
+
+ flattenTree(parentId?: string, depth = 0): Array<{ node: SceneNode; depth: number }> {
+ const id = parentId ?? this.rootId
+ const parent = this.nodes.get(id)
+ if (!parent) return []
+
+ const result: Array<{ node: SceneNode; depth: number }> = []
+ for (const childId of parent.childIds) {
+ const child = this.nodes.get(childId)
+ if (!child) continue
+ result.push({ node: child, depth })
+ if (child.childIds.length > 0) {
+ result.push(...this.flattenTree(childId, depth + 1))
+ }
+ }
+ return result
+ }
}
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index 7cef5252f..36c2f9a8e 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -70,6 +70,7 @@ export function createEditorStore() {
marquee: null as { x: number; y: number; width: number; height: number } | null,
snapGuides: [] as SnapGuide[],
rotationPreview: null as { nodeId: string; angle: number } | null,
+ dropTargetId: null as string | null,
panX: 0,
panY: 0,
zoom: 1,
@@ -89,9 +90,9 @@ export function createEditorStore() {
selectedNodes.value.length === 1 ? selectedNodes.value[0] : undefined
)
- const layerNodes = computed(() => {
+ const layerTree = computed(() => {
void state.renderVersion
- return graph.getChildren(graph.rootId)
+ return graph.flattenTree()
})
function requestRender() {
@@ -134,14 +135,33 @@ export function createEditorStore() {
requestRender()
}
+ function setDropTarget(id: string | null) {
+ state.dropTargetId = id
+ requestRender()
+ }
+
+ function reparentNodes(nodeIds: string[], newParentId: string) {
+ for (const id of nodeIds) {
+ graph.reparentNode(id, newParentId)
+ }
+ requestRender()
+ }
+
function updateNode(id: string, changes: Partial<SceneNode>) {
graph.updateNode(id, changes)
requestRender()
}
- function createShape(type: NodeType, x: number, y: number, w: number, h: number): string {
+ function createShape(
+ type: NodeType,
+ x: number,
+ y: number,
+ w: number,
+ h: number,
+ parentId?: string
+ ): string {
const fill = DEFAULT_FILLS[type] ?? DEFAULT_FILLS.RECTANGLE
- const node = graph.createNode(type, graph.rootId, {
+ const node = graph.createNode(type, parentId ?? graph.rootId, {
x,
y,
width: w,
@@ -281,7 +301,7 @@ export function createEditorStore() {
state,
selectedNodes,
selectedNode,
- layerNodes,
+ layerTree,
requestRender,
setTool,
select,
@@ -290,6 +310,8 @@ export function createEditorStore() {
setMarquee,
setSnapGuides,
setRotationPreview,
+ setDropTarget,
+ reparentNodes,
updateNode,
createShape,
duplicateSelected,