Skip to content

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

Wire undo/redo into all editor operations

Danila Poyarkov179acd0edb21
diff --git a/src/components/PropertiesPanel.vue b/src/components/PropertiesPanel.vue
index 13af7c8ad..95033b0bd 100644
--- a/src/components/PropertiesPanel.vue
+++ b/src/components/PropertiesPanel.vue
@@ -6,7 +6,7 @@ import ColorPicker from './ColorPicker.vue'
import ScrubInput from './ScrubInput.vue'
import { useEditorStore } from '../stores/editor'
-import type { Color, Fill, Stroke, LayoutSizing, LayoutAlign, LayoutCounterAlign } from '../engine/scene-graph'
+import type { Color, Fill, Stroke, LayoutSizing, LayoutAlign, LayoutCounterAlign, SceneNode } from '../engine/scene-graph'
const store = useEditorStore()
@@ -96,6 +96,16 @@ function setUniformPadding(v: number) {
})
}
+function commitUniformPadding(_value: number, previous: number) {
+ if (!node.value) return
+ store.commitNodeUpdate(node.value.id, {
+ paddingTop: previous,
+ paddingRight: previous,
+ paddingBottom: previous,
+ paddingLeft: previous
+ } as unknown as Partial<SceneNode>, 'Change padding')
+}
+
const ALIGN_GRID: Array<{ primary: LayoutAlign; counter: LayoutCounterAlign }> = [
{ primary: 'MIN', counter: 'MIN' },
{ primary: 'CENTER', counter: 'MIN' },
@@ -110,10 +120,10 @@ const ALIGN_GRID: Array<{ primary: LayoutAlign; counter: LayoutCounterAlign }> =
function setAlignment(primary: LayoutAlign, counter: LayoutCounterAlign) {
if (!node.value) return
- store.updateNode(node.value.id, {
+ store.updateNodeWithUndo(node.value.id, {
primaryAxisAlign: primary,
counterAxisAlign: counter
- })
+ }, 'Change alignment')
}
function updateProp(key: string, value: number | string) {
@@ -126,11 +136,21 @@ function updateProp(key: string, value: number | string) {
}
}
+function commitProp(key: string, _value: number | string, previous: number | string) {
+ if (multiCount.value > 1) {
+ for (const n of store.selectedNodes.value) {
+ store.commitNodeUpdate(n.id, { [key]: previous } as Partial<SceneNode>, `Change ${key}`)
+ }
+ } else if (node.value) {
+ store.commitNodeUpdate(node.value.id, { [key]: previous } as Partial<SceneNode>, `Change ${key}`)
+ }
+}
+
function updateFillColor(index: number, color: Color) {
if (!node.value) return
const fills = [...node.value.fills]
fills[index] = { ...fills[index], color }
- store.updateNode(node.value.id, { fills })
+ store.updateNodeWithUndo(node.value.id, { fills }, "Change fill")
}
function addFill() {
@@ -141,20 +161,20 @@ function addFill() {
opacity: 1,
visible: true
}
- store.updateNode(node.value.id, { fills: [...node.value.fills, fill] })
+ store.updateNodeWithUndo(node.value.id, { fills: [...node.value.fills, fill] }, "Add fill")
}
function removeFill(index: number) {
if (!node.value) return
const fills = node.value.fills.filter((_, i) => i !== index)
- store.updateNode(node.value.id, { fills })
+ store.updateNodeWithUndo(node.value.id, { fills }, "Change fill")
}
function toggleFillVisibility(index: number) {
if (!node.value) return
const fills = [...node.value.fills]
fills[index] = { ...fills[index], visible: !fills[index].visible }
- store.updateNode(node.value.id, { fills })
+ store.updateNodeWithUndo(node.value.id, { fills }, "Change fill")
}
function addStroke() {
@@ -166,27 +186,27 @@ function addStroke() {
visible: true,
align: 'CENTER'
}
- store.updateNode(node.value.id, { strokes: [...node.value.strokes, stroke] })
+ store.updateNodeWithUndo(node.value.id, { strokes: [...node.value.strokes, stroke] }, "Add stroke")
}
function updateStrokeColor(index: number, color: Color) {
if (!node.value) return
const strokes = [...node.value.strokes]
strokes[index] = { ...strokes[index], color }
- store.updateNode(node.value.id, { strokes })
+ store.updateNodeWithUndo(node.value.id, { strokes }, "Change stroke")
}
function updateStrokeWeight(index: number, weight: number) {
if (!node.value) return
const strokes = [...node.value.strokes]
strokes[index] = { ...strokes[index], weight }
- store.updateNode(node.value.id, { strokes })
+ store.updateNodeWithUndo(node.value.id, { strokes }, "Change stroke")
}
function removeStroke(index: number) {
if (!node.value) return
const strokes = node.value.strokes.filter((_, i) => i !== index)
- store.updateNode(node.value.id, { strokes })
+ store.updateNodeWithUndo(node.value.id, { strokes }, "Change stroke")
}
function colorHex(c: Color) {
@@ -243,15 +263,15 @@ function colorHex(c: Color) {
<div class="border-b border-border px-3 py-2">
<label class="mb-1.5 block text-[11px] text-muted">Position</label>
<div class="flex gap-1.5">
- <ScrubInput icon="X" :model-value="Math.round(node.x)" @update:model-value="updateProp('x', $event)" />
- <ScrubInput icon="Y" :model-value="Math.round(node.y)" @update:model-value="updateProp('y', $event)" />
+ <ScrubInput icon="X" :model-value="Math.round(node.x)" @update:model-value="updateProp('x', $event)" @commit="(v: number, p: number) => commitProp('x', v, p)" />
+ <ScrubInput icon="Y" :model-value="Math.round(node.y)" @update:model-value="updateProp('y', $event)" @commit="(v: number, p: number) => commitProp('y', v, p)" />
</div>
</div>
<!-- Rotation -->
<div class="border-b border-border px-3 py-2">
<div class="flex gap-1.5">
- <ScrubInput icon="R" suffix="°" :model-value="Math.round(node.rotation)" :min="-360" :max="360" @update:model-value="updateProp('rotation', $event)" />
+ <ScrubInput icon="R" suffix="°" :model-value="Math.round(node.rotation)" :min="-360" :max="360" @update:model-value="updateProp('rotation', $event)" @commit="(v: number, p: number) => commitProp('rotation', v, p)" />
</div>
</div>
@@ -261,7 +281,7 @@ function colorHex(c: Color) {
<div class="flex gap-1.5">
<!-- Width -->
<div ref="widthDimRef" class="relative flex min-w-0 flex-1 items-center gap-1">
- <ScrubInput icon="W" :model-value="Math.round(node.width)" :min="0" @update:model-value="updateProp('width', $event)" />
+ <ScrubInput icon="W" :model-value="Math.round(node.width)" :min="0" @update:model-value="updateProp('width', $event)" @commit="(v: number, p: number) => commitProp('width', v, p)" />
<button
v-if="node.layoutMode !== 'NONE' || isInAutoLayout"
class="cursor-pointer whitespace-nowrap rounded border-none bg-transparent px-1 py-px text-[10px] text-muted hover:bg-hover hover:text-surface"
@@ -289,7 +309,7 @@ function colorHex(c: Color) {
</div>
<!-- Height -->
<div ref="heightDimRef" class="relative flex min-w-0 flex-1 items-center gap-1">
- <ScrubInput icon="H" :model-value="Math.round(node.height)" :min="0" @update:model-value="updateProp('height', $event)" />
+ <ScrubInput icon="H" :model-value="Math.round(node.height)" :min="0" @update:model-value="updateProp('height', $event)" @commit="(v: number, p: number) => commitProp('height', v, p)" />
<button
v-if="node.layoutMode !== 'NONE' || isInAutoLayout"
class="cursor-pointer whitespace-nowrap rounded border-none bg-transparent px-1 py-px text-[10px] text-muted hover:bg-hover hover:text-surface"
@@ -318,7 +338,7 @@ function colorHex(c: Color) {
</div>
<!-- Corner radius -->
<div class="mt-1.5 flex gap-1.5">
- <ScrubInput icon="↻" :model-value="node.cornerRadius" :min="0" @update:model-value="updateProp('cornerRadius', $event)" />
+ <ScrubInput icon="↻" :model-value="node.cornerRadius" :min="0" @update:model-value="updateProp('cornerRadius', $event)" @commit="(v: number, p: number) => commitProp('cornerRadius', v, p)" />
</div>
</div>
@@ -392,7 +412,7 @@ function colorHex(c: Color) {
/>
</button>
</div>
- <ScrubInput :model-value="node.itemSpacing" :min="0" @update:model-value="updateProp('itemSpacing', $event)">
+ <ScrubInput :model-value="node.itemSpacing" :min="0" @update:model-value="updateProp('itemSpacing', $event)" @commit="(v: number, p: number) => commitProp('itemSpacing', v, p)">
<template #icon>
<svg width="14" height="14" viewBox="0 0 14 14"><rect x="0" y="1" width="4" height="12" rx="0.5" fill="currentColor" opacity="0.4" /><rect x="5" y="5" width="4" height="4" rx="0.5" fill="currentColor" /><rect x="10" y="1" width="4" height="12" rx="0.5" fill="currentColor" opacity="0.4" /></svg>
</template>
@@ -415,7 +435,7 @@ function colorHex(c: Color) {
</div>
</template>
<template v-else>
- <ScrubInput :model-value="node.paddingTop" :min="0" @update:model-value="setUniformPadding($event)">
+ <ScrubInput :model-value="node.paddingTop" :min="0" @update:model-value="setUniformPadding($event)" @commit="commitUniformPadding">
<template #icon>
<svg width="14" height="14" viewBox="0 0 14 14"><rect x="0" y="0" width="14" height="14" rx="2" fill="none" stroke="currentColor" stroke-width="1" /><rect x="3" y="3" width="8" height="8" rx="1" fill="currentColor" opacity="0.3" /></svg>
</template>
@@ -439,7 +459,7 @@ function colorHex(c: Color) {
<div class="border-b border-border px-3 py-2">
<label class="mb-1.5 block text-[11px] text-muted">Appearance</label>
<div class="flex gap-1.5">
- <ScrubInput icon="⊘" suffix="%" :model-value="Math.round(node.opacity * 100)" :min="0" :max="100" @update:model-value="updateProp('opacity', $event / 100)" />
+ <ScrubInput icon="⊘" suffix="%" :model-value="Math.round(node.opacity * 100)" :min="0" :max="100" @update:model-value="updateProp('opacity', $event / 100)" @commit="(v: number, p: number) => commitProp('opacity', v / 100, p / 100)" />
</div>
</div>
diff --git a/src/components/ScrubInput.vue b/src/components/ScrubInput.vue
index edba36640..628f9f22d 100644
--- a/src/components/ScrubInput.vue
+++ b/src/components/ScrubInput.vue
@@ -20,6 +20,7 @@ const props = withDefaults(defineProps<{
const emit = defineEmits<{
'update:modelValue': [value: number]
+ 'commit': [value: number, previous: number]
}>()
const editing = ref(false)
@@ -36,6 +37,7 @@ function startScrub(e: PointerEvent) {
scrubbing.value = true
let startX = e.clientX
let accumulated = props.modelValue
+ const valueBeforeScrub = props.modelValue
document.body.style.cursor = 'ew-resize'
stopMove = useEventListener(document, 'pointermove', (ev: PointerEvent) => {
@@ -53,6 +55,9 @@ function startScrub(e: PointerEvent) {
document.body.style.cursor = ''
stopMove?.()
stopUp?.()
+ if (props.modelValue !== valueBeforeScrub) {
+ emit('commit', props.modelValue, valueBeforeScrub)
+ }
})
}
@@ -65,9 +70,13 @@ function startEdit() {
function commitEdit(e: Event) {
const val = +(e.target as HTMLInputElement).value
+ const previous = props.modelValue
if (!Number.isNaN(val)) {
const clamped = Math.min(props.max, Math.max(props.min, val))
emit('update:modelValue', clamped)
+ if (clamped !== previous) {
+ emit('commit', clamped, previous)
+ }
}
editing.value = false
}
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index 0f246140b..49971b93b 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -699,10 +699,15 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
}
}
+ if (d.type === 'resize') {
+ store.commitResize(d.nodeId, d.origRect)
+ }
+
if (d.type === 'rotate') {
const preview = store.state.rotationPreview
if (preview) {
store.updateNode(d.nodeId, { rotation: preview.angle })
+ store.commitRotation(d.nodeId, d.origRotation)
}
store.setRotationPreview(null)
}
diff --git a/src/engine/undo.ts b/src/engine/undo.ts
index 3f1c1db98..cc8141a3f 100644
--- a/src/engine/undo.ts
+++ b/src/engine/undo.ts
@@ -22,6 +22,15 @@ export class UndoManager {
}
}
+ push(entry: UndoEntry): void {
+ if (this.batchEntries) {
+ this.batchEntries.push(entry)
+ } else {
+ this.undoStack.push(entry)
+ this.redoStack = []
+ }
+ }
+
undo(): string | null {
const entry = this.undoStack.pop()
if (!entry) return null
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index aa3fb1059..f40a1342b 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -201,8 +201,21 @@ export function createEditorStore() {
}
function commitTextEdit(nodeId: string, text: string) {
+ const node = graph.getNode(nodeId)
+ const prevText = node?.text ?? ''
graph.updateNode(nodeId, { text })
state.editingTextId = null
+ undo.push({
+ label: 'Edit text',
+ forward: () => {
+ graph.updateNode(nodeId, { text })
+ requestRender()
+ },
+ inverse: () => {
+ graph.updateNode(nodeId, { text: prevText })
+ requestRender()
+ }
+ })
requestRender()
}
@@ -245,10 +258,50 @@ export function createEditorStore() {
requestRender()
}
+ function updateNodeWithUndo(id: string, changes: Partial<SceneNode>, label = 'Update') {
+ const node = graph.getNode(id)
+ if (!node) return
+ const previous: Partial<SceneNode> = {}
+ for (const key of Object.keys(changes) as (keyof SceneNode)[]) {
+ ;(previous as Record<string, unknown>)[key] = node[key]
+ }
+ graph.updateNode(id, changes)
+ runLayoutForNode(id)
+ undo.push({
+ label,
+ forward: () => {
+ graph.updateNode(id, changes)
+ runLayoutForNode(id)
+ requestRender()
+ },
+ inverse: () => {
+ graph.updateNode(id, previous)
+ runLayoutForNode(id)
+ requestRender()
+ }
+ })
+ requestRender()
+ }
+
function setLayoutMode(id: string, mode: LayoutMode) {
const node = graph.getNode(id)
if (!node) return
+ const previous: Partial<SceneNode> = {
+ layoutMode: node.layoutMode,
+ itemSpacing: node.itemSpacing,
+ paddingTop: node.paddingTop,
+ paddingRight: node.paddingRight,
+ paddingBottom: node.paddingBottom,
+ paddingLeft: node.paddingLeft,
+ primaryAxisSizing: node.primaryAxisSizing,
+ counterAxisSizing: node.counterAxisSizing,
+ primaryAxisAlign: node.primaryAxisAlign,
+ counterAxisAlign: node.counterAxisAlign,
+ width: node.width,
+ height: node.height
+ }
+
const updates: Partial<SceneNode> = { layoutMode: mode }
if (mode !== 'NONE' && node.layoutMode === 'NONE') {
updates.itemSpacing = 0
@@ -265,6 +318,27 @@ export function createEditorStore() {
graph.updateNode(id, updates)
if (mode !== 'NONE') computeLayout(graph, id)
runLayoutForNode(id)
+
+ const finalState: Partial<SceneNode> = {}
+ const updated = graph.getNode(id)!
+ for (const key of Object.keys(previous) as (keyof SceneNode)[]) {
+ ;(finalState as Record<string, unknown>)[key] = updated[key]
+ }
+
+ undo.push({
+ label: mode === 'NONE' ? 'Remove auto layout' : 'Add auto layout',
+ forward: () => {
+ graph.updateNode(id, finalState)
+ if (mode !== 'NONE') computeLayout(graph, id)
+ runLayoutForNode(id)
+ requestRender()
+ },
+ inverse: () => {
+ graph.updateNode(id, previous)
+ runLayoutForNode(id)
+ requestRender()
+ }
+ })
requestRender()
}
@@ -272,12 +346,13 @@ export function createEditorStore() {
const nodes = selectedNodes.value
if (nodes.length === 0) return
- // Find common parent
const parentId = nodes[0].parentId ?? graph.rootId
const sameParent = nodes.every((n) => (n.parentId ?? graph.rootId) === parentId)
if (!sameParent) return
- // Compute bounding box in absolute coords
+ const prevSelection = new Set(state.selectedIds)
+ const origPositions = nodes.map((n) => ({ id: n.id, x: n.x, y: n.y, parentId }))
+
let minX = Infinity
let minY = Infinity
let maxX = -Infinity
@@ -290,7 +365,6 @@ export function createEditorStore() {
maxY = Math.max(maxY, abs.y + n.height)
}
- // Position relative to parent
const parentAbs =
parentId === graph.rootId ? { x: 0, y: 0 } : graph.getAbsolutePosition(parentId)
@@ -307,15 +381,38 @@ export function createEditorStore() {
counterAxisAlign: 'MIN',
fills: [{ type: 'SOLID', color: { r: 1, g: 1, b: 1, a: 1 }, opacity: 1, visible: true }]
})
+ const frameId = frame.id
- // Reparent nodes into the new frame
for (const n of nodes) {
- graph.reparentNode(n.id, frame.id)
+ graph.reparentNode(n.id, frameId)
}
- computeLayout(graph, frame.id)
- runLayoutForNode(frame.id)
- state.selectedIds = new Set([frame.id])
+ computeLayout(graph, frameId)
+ runLayoutForNode(frameId)
+ state.selectedIds = new Set([frameId])
+
+ undo.push({
+ label: 'Wrap in auto layout',
+ forward: () => {
+ // Re-create frame and reparent
+ const f = graph.createNode('FRAME', parentId, { ...frame })
+ for (const n of origPositions) graph.reparentNode(n.id, f.id)
+ computeLayout(graph, f.id)
+ runLayoutForNode(f.id)
+ state.selectedIds = new Set([f.id])
+ requestRender()
+ },
+ inverse: () => {
+ // Move children back to original parent and delete frame
+ for (const orig of origPositions) {
+ graph.reparentNode(orig.id, orig.parentId)
+ graph.updateNode(orig.id, { x: orig.x, y: orig.y })
+ }
+ graph.deleteNode(frameId)
+ state.selectedIds = prevSelection
+ requestRender()
+ }
+ })
requestRender()
}
@@ -328,15 +425,30 @@ export function createEditorStore() {
parentId?: string
): string {
const fill = DEFAULT_FILLS[type] ?? DEFAULT_FILLS.RECTANGLE
- const node = graph.createNode(type, parentId ?? graph.rootId, {
+ const pid = parentId ?? graph.rootId
+ const node = graph.createNode(type, pid, {
x,
y,
width: w,
height: h,
fills: [{ ...fill }]
})
+ const id = node.id
+ const snapshot = { ...node }
+ undo.push({
+ label: `Create ${type.toLowerCase()}`,
+ forward: () => {
+ graph.createNode(snapshot.type, pid, snapshot)
+ requestRender()
+ },
+ inverse: () => {
+ graph.deleteNode(id)
+ state.selectedIds.delete(id)
+ requestRender()
+ }
+ })
requestRender()
- return node.id
+ return id
}
function selectAll() {
@@ -345,20 +457,41 @@ export function createEditorStore() {
}
function duplicateSelected() {
+ const prevSelection = new Set(state.selectedIds)
const newIds: string[] = []
+ const snapshots: Array<{ id: string; parentId: string; snapshot: SceneNode }> = []
+
for (const id of state.selectedIds) {
const src = graph.getNode(id)
if (!src) continue
- const node = graph.createNode(src.type, graph.rootId, {
+ const parentId = src.parentId ?? graph.rootId
+ const node = graph.createNode(src.type, parentId, {
...src,
name: src.name + ' copy',
x: src.x + 20,
y: src.y + 20
})
newIds.push(node.id)
+ snapshots.push({ id: node.id, parentId, snapshot: { ...node } })
}
+
if (newIds.length > 0) {
state.selectedIds = new Set(newIds)
+ undo.push({
+ label: 'Duplicate',
+ forward: () => {
+ for (const { snapshot, parentId } of snapshots) {
+ graph.createNode(snapshot.type, parentId, snapshot)
+ }
+ state.selectedIds = new Set(newIds)
+ requestRender()
+ },
+ inverse: () => {
+ for (const { id } of snapshots) graph.deleteNode(id)
+ state.selectedIds = prevSelection
+ requestRender()
+ }
+ })
requestRender()
}
}
@@ -399,7 +532,9 @@ export function createEditorStore() {
parentId?: string
) {
const target = parentId ?? graph.rootId
+ const prevSelection = new Set(state.selectedIds)
const newIds: string[] = []
+ const created: Array<{ id: string; parentId: string; snapshot: SceneNode }> = []
function createTree(src: SceneNode & { children?: SceneNode[] }, pid: string, isTop: boolean) {
const node = graph.createNode(src.type, pid, {
@@ -407,6 +542,7 @@ export function createEditorStore() {
x: src.x + (isTop ? 20 : 0),
y: src.y + (isTop ? 20 : 0)
})
+ created.push({ id: node.id, parentId: pid, snapshot: { ...node } })
if (isTop) newIds.push(node.id)
if (src.children) {
for (const child of src.children) {
@@ -420,26 +556,58 @@ export function createEditorStore() {
}
if (newIds.length > 0) {
state.selectedIds = new Set(newIds)
+ undo.push({
+ label: 'Paste',
+ forward: () => {
+ for (const { snapshot, parentId: pid } of created) {
+ graph.createNode(snapshot.type, pid, snapshot)
+ }
+ state.selectedIds = new Set(newIds)
+ requestRender()
+ },
+ inverse: () => {
+ for (const { id } of [...created].reverse()) graph.deleteNode(id)
+ state.selectedIds = prevSelection
+ requestRender()
+ }
+ })
requestRender()
}
}
function deleteSelected() {
- undo.beginBatch('Delete')
+ const entries: Array<{ id: string; parentId: string; snapshot: SceneNode; index: number }> = []
for (const id of state.selectedIds) {
const node = graph.getNode(id)
if (!node) continue
- const snapshot = { ...node }
const parentId = node.parentId ?? graph.rootId
- undo.apply({
- label: 'Delete',
- forward: () => graph.deleteNode(id),
- inverse: () => {
+ const parent = graph.getNode(parentId)
+ const index = parent?.childIds.indexOf(id) ?? -1
+ entries.push({ id, parentId, snapshot: { ...node }, index })
+ }
+ if (entries.length === 0) return
+
+ const prevSelection = new Set(state.selectedIds)
+ for (const { id } of entries) graph.deleteNode(id)
+
+ undo.push({
+ label: 'Delete',
+ forward: () => {
+ for (const { id } of entries) graph.deleteNode(id)
+ clearSelection()
+ requestRender()
+ },
+ inverse: () => {
+ for (const { snapshot, parentId, index } of [...entries].reverse()) {
graph.createNode(snapshot.type, parentId, snapshot)
+ if (index >= 0) {
+ graph.reorderChild(snapshot.id, parentId, index)
+ }
}
- })
- }
- undo.commitBatch()
+ state.selectedIds = prevSelection
+ requestRender()
+ }
+ })
clearSelection()
requestRender()
}
@@ -450,13 +618,82 @@ export function createEditorStore() {
const n = graph.getNode(id)
if (n) finals.set(id, { x: n.x, y: n.y })
}
- undo.apply({
+ undo.push({
label: 'Move',
forward: () => {
- for (const [id, pos] of finals) graph.updateNode(id, pos)
+ for (const [id, pos] of finals) {
+ graph.updateNode(id, pos)
+ runLayoutForNode(id)
+ }
+ requestRender()
+ },
+ inverse: () => {
+ for (const [id, pos] of originals) {
+ graph.updateNode(id, pos)
+ runLayoutForNode(id)
+ }
+ requestRender()
+ }
+ })
+ }
+
+ function commitResize(
+ nodeId: string,
+ origRect: { x: number; y: number; width: number; height: number }
+ ) {
+ const node = graph.getNode(nodeId)
+ if (!node) return
+ const finalRect = { x: node.x, y: node.y, width: node.width, height: node.height }
+ undo.push({
+ label: 'Resize',
+ forward: () => {
+ graph.updateNode(nodeId, finalRect)
+ runLayoutForNode(nodeId)
+ requestRender()
+ },
+ inverse: () => {
+ graph.updateNode(nodeId, origRect)
+ runLayoutForNode(nodeId)
+ requestRender()
+ }
+ })
+ }
+
+ function commitRotation(nodeId: string, origRotation: number) {
+ const node = graph.getNode(nodeId)
+ if (!node) return
+ const finalRotation = node.rotation
+ undo.push({
+ label: 'Rotate',
+ forward: () => {
+ graph.updateNode(nodeId, { rotation: finalRotation })
+ requestRender()
+ },
+ inverse: () => {
+ graph.updateNode(nodeId, { rotation: origRotation })
+ requestRender()
+ }
+ })
+ }
+
+ function commitNodeUpdate(nodeId: string, previous: Partial<SceneNode>, label = 'Update') {
+ const node = graph.getNode(nodeId)
+ if (!node) return
+ const current: Partial<SceneNode> = {}
+ for (const key of Object.keys(previous) as (keyof SceneNode)[]) {
+ ;(current as Record<string, unknown>)[key] = node[key]
+ }
+ undo.push({
+ label,
+ forward: () => {
+ graph.updateNode(nodeId, current)
+ runLayoutForNode(nodeId)
+ requestRender()
},
inverse: () => {
- for (const [id, pos] of originals) graph.updateNode(id, pos)
+ graph.updateNode(nodeId, previous)
+ runLayoutForNode(nodeId)
+ requestRender()
}
})
}
@@ -556,6 +793,10 @@ export function createEditorStore() {
pasteFromHTML,
deleteSelected,
commitMove,
+ commitResize,
+ commitRotation,
+ commitNodeUpdate,
+ updateNodeWithUndo,
undoAction,
redoAction,
screenToCanvas,