AI-native design editor. Open-source Figma alternative.
Auto-layout drag reordering with insertion indicator
Danila Poyarkovf6cee76d2214
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.tsindex 0dd5709d8..6d4cf2dff 100644--- a/src/composables/use-canvas-input.ts+++ b/src/composables/use-canvas-input.ts@@ -21,8 +21,12 @@ interface DragMove {startY: numberoriginals: Map<string, { x: number; y: number }>duplicated?: boolean+ autoLayoutParentId?: string+ brokeFromAutoLayout?: boolean}+const AUTO_LAYOUT_BREAK_THRESHOLD = 8+interface DragPan {type: 'pan'startScreenX: number@@ -310,7 +314,24 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:return}- drag.value = { type: 'move', startX: cx, startY: cy, originals }+ // Detect if we're inside an auto-layout frame+ let autoLayoutParentId: string | undefined+ if (store.state.selectedIds.size === 1) {+ const selectedId = [...store.state.selectedIds][0]+ const selectedNode = store.graph.getNode(selectedId)+ if (selectedNode?.parentId) {+ const parent = store.graph.getNode(selectedNode.parentId)+ if (+ parent &&+ parent.layoutMode !== 'NONE' &&+ selectedNode.layoutPositioning !== 'ABSOLUTE'+ ) {+ autoLayoutParentId = parent.id+ }+ }+ }++ drag.value = { type: 'move', startX: cx, startY: cy, originals, autoLayoutParentId }} else {store.clearSelection()drag.value = { type: 'marquee', startX: cx, startY: cy }@@ -428,6 +449,39 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:let dx = cx - d.startXlet dy = cy - d.startY+ // Auto-layout: dead zone before breaking out+ if (d.autoLayoutParentId && !d.brokeFromAutoLayout) {+ const dist = Math.sqrt(dx * dx + dy * dy)+ if (dist < AUTO_LAYOUT_BREAK_THRESHOLD) {+ // Still in dead zone — compute insertion indicator within parent+ computeAutoLayoutIndicator(d, cx, cy)+ return+ }+ d.brokeFromAutoLayout = true+ store.setLayoutInsertIndicator(null)+ }++ // Check if we're hovering over an auto-layout frame+ const dropTarget = store.graph.hitTestFrame(cx, cy, store.state.selectedIds)+ const dropParent = dropTarget ? store.graph.getNode(dropTarget.id) : null++ if (dropParent && dropParent.layoutMode !== 'NONE') {+ computeAutoLayoutIndicatorForFrame(dropParent, cx, cy)+ store.setDropTarget(dropParent.id)++ // Still move the nodes freely so the user sees them floating+ for (const [id, orig] of d.originals) {+ store.graph.updateNode(id, {+ x: Math.round(orig.x + dx),+ y: Math.round(orig.y + dy)+ })+ }+ store.requestRender()+ return+ }++ store.setLayoutInsertIndicator(null)+// Compute snap using absolute positionsconst selectedNodes: SceneNode[] = []for (const [id, orig] of d.originals) {@@ -458,10 +512,7 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store: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}@@ -574,14 +625,25 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:const d = drag.valueif (d.type === 'move') {- store.commitMove(d.originals)+ const indicator = store.state.layoutInsertIndicator+ store.setLayoutInsertIndicator(null)store.setSnapGuides([])- // Reparent into frame if dropped on one- const dropId = store.state.dropTargetId- if (dropId) {- store.reparentNodes([...store.state.selectedIds], dropId)+ if (indicator) {+ // Drop into auto-layout at the indicated position+ for (const id of store.state.selectedIds) {+ store.reorderInAutoLayout(id, indicator.parentId, indicator.index)+ }store.setDropTarget(null)+ } else {+ store.commitMove(d.originals)++ // Reparent into frame if dropped on one+ const dropId = store.state.dropTargetId+ if (dropId) {+ store.reparentNodes([...store.state.selectedIds], dropId)+ store.setDropTarget(null)+ }}}@@ -633,6 +695,97 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:}}+ function computeAutoLayoutIndicator(d: DragMove, cx: number, cy: number) {+ if (!d.autoLayoutParentId) return+ const parent = store.graph.getNode(d.autoLayoutParentId)+ if (!parent || parent.layoutMode === 'NONE') return+ computeAutoLayoutIndicatorForFrame(parent, cx, cy)+ }++ function computeAutoLayoutIndicatorForFrame(parent: SceneNode, cx: number, cy: number) {+ const children = store.graph.getChildren(parent.id).filter(+ (c) => c.layoutPositioning !== 'ABSOLUTE' && !store.state.selectedIds.has(c.id)+ )++ const parentAbs = store.graph.getAbsolutePosition(parent.id)+ const isRow = parent.layoutMode === 'HORIZONTAL'++ let insertIndex = children.length++ for (let i = 0; i < children.length; i++) {+ const child = children[i]+ const childAbs = store.graph.getAbsolutePosition(child.id)+ const mid = isRow+ ? childAbs.x + child.width / 2+ : childAbs.y + child.height / 2+ const cursor = isRow ? cx : cy++ if (cursor < mid) {+ insertIndex = i+ break+ }+ }++ // Compute indicator position+ let indicatorPos: number+ const crossStart = isRow ? parentAbs.y + parent.paddingTop : parentAbs.x + parent.paddingLeft+ const crossLength = isRow+ ? parent.height - parent.paddingTop - parent.paddingBottom+ : parent.width - parent.paddingLeft - parent.paddingRight++ if (children.length === 0) {+ indicatorPos = isRow+ ? parentAbs.x + parent.paddingLeft+ : parentAbs.y + parent.paddingTop+ } else if (insertIndex === 0) {+ const first = children[0]+ const firstAbs = store.graph.getAbsolutePosition(first.id)+ indicatorPos = isRow+ ? firstAbs.x - parent.itemSpacing / 2+ : firstAbs.y - parent.itemSpacing / 2+ } else if (insertIndex >= children.length) {+ const last = children[children.length - 1]+ const lastAbs = store.graph.getAbsolutePosition(last.id)+ indicatorPos = isRow+ ? lastAbs.x + last.width + parent.itemSpacing / 2+ : lastAbs.y + last.height + parent.itemSpacing / 2+ } else {+ const prev = children[insertIndex - 1]+ const next = children[insertIndex]+ const prevAbs = store.graph.getAbsolutePosition(prev.id)+ const nextAbs = store.graph.getAbsolutePosition(next.id)+ indicatorPos = isRow+ ? (prevAbs.x + prev.width + nextAbs.x) / 2+ : (prevAbs.y + prev.height + nextAbs.y) / 2+ }++ // Account for the dragged node being in the children list+ // (we filtered it out, so insertIndex is relative to the filtered list)+ // Convert to actual childIds index+ const allChildren = store.graph.getChildren(parent.id)+ let realIndex = 0+ let filteredCount = 0+ for (let i = 0; i < allChildren.length; i++) {+ if (store.state.selectedIds.has(allChildren[i].id)) continue+ if (allChildren[i].layoutPositioning === 'ABSOLUTE') {+ realIndex+++ continue+ }+ if (filteredCount === insertIndex) break+ filteredCount+++ realIndex+++ }++ store.setLayoutInsertIndicator({+ parentId: parent.id,+ index: realIndex,+ x: isRow ? indicatorPos : crossStart,+ y: isRow ? crossStart : indicatorPos,+ length: crossLength,+ direction: isRow ? 'VERTICAL' : 'HORIZONTAL'+ })+ }+useEventListener(canvasRef, 'dblclick', onDblClick)useEventListener(canvasRef, 'mousedown', onMouseDown)useEventListener(canvasRef, 'mousemove', onMouseMove)diff --git a/src/composables/use-canvas.ts b/src/composables/use-canvas.tsindex ca4ea88c0..e2c8d8549 100644--- a/src/composables/use-canvas.ts+++ b/src/composables/use-canvas.ts@@ -56,7 +56,8 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Editomarquee: store.state.marquee,snapGuides: store.state.snapGuides,rotationPreview: store.state.rotationPreview,- dropTargetId: store.state.dropTargetId+ dropTargetId: store.state.dropTargetId,+ layoutInsertIndicator: store.state.layoutInsertIndicator})}diff --git a/src/engine/renderer.ts b/src/engine/renderer.tsindex 111d60cf7..90e5575a6 100644--- a/src/engine/renderer.ts+++ b/src/engine/renderer.ts@@ -15,6 +15,12 @@ export interface RenderOverlays {snapGuides?: SnapGuide[]rotationPreview?: { nodeId: string; angle: number } | nulldropTargetId?: string | null+ layoutInsertIndicator?: {+ x: number+ y: number+ length: number+ direction: 'HORIZONTAL' | 'VERTICAL'+ } | null}export class SkiaRenderer {@@ -104,6 +110,7 @@ export class SkiaRenderer {this.drawSelection(canvas, graph, selectedIds, overlays)this.drawSnapGuides(canvas, overlays.snapGuides)this.drawMarquee(canvas, overlays.marquee)+ this.drawLayoutInsertIndicator(canvas, overlays.layoutInsertIndicator)canvas.restore()this.surface.flush()@@ -349,6 +356,35 @@ export class SkiaRenderer {fill.delete()}+ // --- Layout insert indicator ---++ private drawLayoutInsertIndicator(+ canvas: Canvas,+ indicator?: RenderOverlays['layoutInsertIndicator']+ ): void {+ if (!indicator) return++ const paint = new this.ck.Paint()+ paint.setStyle(this.ck.PaintStyle.Stroke)+ paint.setStrokeWidth(2)+ paint.setColor(this.ck.Color4f(0.23, 0.51, 0.96, 1.0))+ paint.setAntiAlias(true)++ if (indicator.direction === 'HORIZONTAL') {+ const y = indicator.y * this.zoom + this.panY+ const x1 = indicator.x * this.zoom + this.panX+ const x2 = (indicator.x + indicator.length) * this.zoom + this.panX+ canvas.drawLine(x1, y, x2, y, paint)+ } else {+ const x = indicator.x * this.zoom + this.panX+ const y1 = indicator.y * this.zoom + this.panY+ const y2 = (indicator.y + indicator.length) * this.zoom + this.panY+ canvas.drawLine(x, y1, x, y2, paint)+ }++ paint.delete()+ }+// --- Scene rendering ---private renderNode(diff --git a/src/engine/scene-graph.ts b/src/engine/scene-graph.tsindex ab312698d..3fc361887 100644--- a/src/engine/scene-graph.ts+++ b/src/engine/scene-graph.ts@@ -277,6 +277,30 @@ export class SceneGraph {node.y = absPos.y - newParentAbs.y}+ reorderChild(nodeId: string, parentId: string, insertIndex: number): void {+ const node = this.nodes.get(nodeId)+ if (!node) return++ const oldParent = node.parentId ? this.nodes.get(node.parentId) : undefined+ const newParent = this.nodes.get(parentId)+ if (!newParent) return++ // Remove from old parent+ if (oldParent) {+ oldParent.childIds = oldParent.childIds.filter((cid) => cid !== nodeId)+ }++ // If same parent, adjust index since we removed the item+ let idx = insertIndex+ if (oldParent === newParent && idx > (oldParent.childIds.indexOf(nodeId) === -1 ? idx : oldParent.childIds.length)) {+ // Already removed above, no adjustment needed+ }++ node.parentId = parentId+ idx = Math.min(idx, newParent.childIds.length)+ newParent.childIds.splice(idx, 0, nodeId)+ }+deleteNode(id: string): void {const node = this.nodes.get(id)if (!node || id === this.rootId) returndiff --git a/src/stores/editor.ts b/src/stores/editor.tsindex 04f716fc3..74da1dc52 100644--- a/src/stores/editor.ts+++ b/src/stores/editor.ts@@ -83,6 +83,14 @@ export function createEditorStore() {snapGuides: [] as SnapGuide[],rotationPreview: null as { nodeId: string; angle: number } | null,dropTargetId: null as string | null,+ layoutInsertIndicator: null as {+ parentId: string+ index: number+ x: number+ y: number+ length: number+ direction: 'HORIZONTAL' | 'VERTICAL'+ } | null,editingTextId: null as string | null,panX: 0,panY: 0,@@ -153,6 +161,33 @@ export function createEditorStore() {requestRender()}+ function setLayoutInsertIndicator(+ indicator: typeof state.layoutInsertIndicator+ ) {+ state.layoutInsertIndicator = indicator+ requestRender()+ }++ function reorderInAutoLayout(nodeId: string, parentId: string, insertIndex: number) {+ const parent = graph.getNode(parentId)+ if (!parent || parent.layoutMode === 'NONE') return++ const node = graph.getNode(nodeId)+ if (!node) return++ // Convert position if coming from different parent+ if (node.parentId !== parentId) {+ const absPos = graph.getAbsolutePosition(nodeId)+ const parentAbs = graph.getAbsolutePosition(parentId)+ graph.updateNode(nodeId, { x: absPos.x - parentAbs.x, y: absPos.y - parentAbs.y })+ }++ graph.reorderChild(nodeId, parentId, insertIndex)+ computeLayout(graph, parentId)+ runLayoutForNode(parentId)+ requestRender()+ }+function reparentNodes(nodeIds: string[], newParentId: string) {for (const id of nodeIds) {graph.reparentNode(id, newParentId)@@ -505,6 +540,8 @@ export function createEditorStore() {setSnapGuides,setRotationPreview,setDropTarget,+ setLayoutInsertIndicator,+ reorderInAutoLayout,reparentNodes,startTextEditing,commitTextEdit,