AI-native design editor. Open-source Figma alternative.
refactor(vue): extract useUndoBatch composable and add batch tests
Danila Poyarkova2a5376b74b0
diff --git a/packages/vue/src/PropertyList/PropertyListRoot.vue b/packages/vue/src/PropertyList/PropertyListRoot.vueindex a4ec045ec..c43c9c032 100644--- a/packages/vue/src/PropertyList/PropertyListRoot.vue+++ b/packages/vue/src/PropertyList/PropertyListRoot.vue@@ -1,8 +1,9 @@<script setup lang="ts">-import { computed, onBeforeUnmount } from 'vue'+import { computed } from 'vue'import { useEditor } from '@open-pencil/vue/context/editorContext'import { useNodeProps } from '@open-pencil/vue/controls/useNodeProps'+import { useUndoBatch } from '@open-pencil/vue/controls/useUndoBatch'import { useSceneComputed } from '@open-pencil/vue/internal/useSceneComputed'import { providePropertyList } from './context'@@ -26,6 +27,7 @@ const emit = defineEmits<{const editor = useEditor()const { isArrayMixed } = useNodeProps()+const batch = useUndoBatch(editor.undo)const selectedNodes = useSceneComputed(() => {void editor.state.sceneVersion@@ -51,35 +53,8 @@ function targetNodes(): SceneNode[] {return activeNode.value ? [activeNode.value] : []}-const BATCH_IDLE_MS = 300-let batchKey: string | null = null-let batchTimer: ReturnType<typeof setTimeout> | null = null--function flushBatch() {- if (batchTimer !== null) {- clearTimeout(batchTimer)- batchTimer = null- }- if (batchKey !== null) {- editor.undo.commitBatch()- batchKey = null- }-}--function ensureBatch(key: string, label: string) {- if (batchKey !== key) {- flushBatch()- editor.undo.beginBatch(label)- batchKey = key- }- if (batchTimer !== null) clearTimeout(batchTimer)- batchTimer = setTimeout(flushBatch, BATCH_IDLE_MS)-}--onBeforeUnmount(flushBatch)-function add(defaults: ArrayItemType) {- flushBatch()+ batch.flush()emit('add', defaults)for (const n of targetNodes()) {const arr = isMulti.value ? [defaults] : [...n[propKey], defaults]@@ -92,7 +67,7 @@ function add(defaults: ArrayItemType) {}function remove(index: number) {- flushBatch()+ batch.flush()emit('remove', index)for (const n of targetNodes()) {editor.updateNodeWithUndo(@@ -110,7 +85,7 @@ function update(index: number, item: ArrayItemType) {const nodes = targetNodes()if (nodes.length === 0) returnconst key = `update:${propKey}:${index}:${nodes.map((n) => n.id).join(',')}`- ensureBatch(key, `Change ${propKey}`)+ batch.ensure(key, `Change ${propKey}`)for (const n of nodes) {const arr = [...n[propKey]] as ArrayItemType[]arr[index] = item@@ -123,7 +98,7 @@ function patch(index: number, changes: Record<string, unknown>) {const nodes = targetNodes()if (nodes.length === 0) returnconst key = `patch:${propKey}:${index}:${nodes.map((n) => n.id).join(',')}`- ensureBatch(key, `Change ${propKey}`)+ batch.ensure(key, `Change ${propKey}`)for (const n of nodes) {const arr = [...n[propKey]] as ArrayItemType[]arr[index] = { ...arr[index], ...changes } as ArrayItemType@@ -132,7 +107,7 @@ function patch(index: number, changes: Record<string, unknown>) {}function toggleVisibility(index: number) {- flushBatch()+ batch.flush()emit('toggleVisibility', index)const nodes = targetNodes()if (nodes.length === 0) returndiff --git a/packages/vue/src/controls/useUndoBatch.ts b/packages/vue/src/controls/useUndoBatch.tsnew file mode 100644index 000000000..c9aef9a66--- /dev/null+++ b/packages/vue/src/controls/useUndoBatch.ts@@ -0,0 +1,35 @@+import { onBeforeUnmount } from 'vue'++import type { UndoManager } from '@open-pencil/core'++const BATCH_IDLE_MS = 300++export function useUndoBatch(undo: UndoManager) {+ let batchKey: string | null = null+ let batchTimer: ReturnType<typeof setTimeout> | null = null++ function flush() {+ if (batchTimer !== null) {+ clearTimeout(batchTimer)+ batchTimer = null+ }+ if (batchKey !== null) {+ undo.commitBatch()+ batchKey = null+ }+ }++ function ensure(key: string, label: string) {+ if (batchKey !== key) {+ flush()+ undo.beginBatch(label)+ batchKey = key+ }+ if (batchTimer !== null) clearTimeout(batchTimer)+ batchTimer = setTimeout(flush, BATCH_IDLE_MS)+ }++ onBeforeUnmount(flush)++ return { ensure, flush }+}diff --git a/tests/engine/undo-batch-idle.test.ts b/tests/engine/undo-batch-idle.test.tsnew file mode 100644index 000000000..0a5a23dc7--- /dev/null+++ b/tests/engine/undo-batch-idle.test.ts@@ -0,0 +1,107 @@+import { describe, test, expect } from 'bun:test'++import { UndoManager } from '@open-pencil/core'++describe('UndoManager idle-timer batching', () => {+ function noop() {}+ function entry(label: string, forward = noop, inverse = noop) {+ return { label, forward, inverse }+ }++ test('rapid pushes inside a batch produce a single undo entry', () => {+ const undo = new UndoManager()+ let value = 0++ undo.beginBatch('drag color')+ for (let i = 1; i <= 10; i++) {+ const prev = value+ const next = i+ undo.push({ label: `step ${i}`, forward: () => { value = next }, inverse: () => { value = prev } })+ value = next+ }+ undo.commitBatch()++ expect(value).toBe(10)+ expect(undo.undoLabel).toBe('drag color')++ undo.undo()+ expect(value).toBe(0)+ expect(undo.canUndo).toBe(false)+ })++ test('changing batch key flushes the previous batch', () => {+ const undo = new UndoManager()+ let a = 0+ let b = 0++ undo.beginBatch('batch A')+ undo.push(entry('a1', () => { a = 1 }, () => { a = 0 }))+ a = 1+ undo.push(entry('a2', () => { a = 2 }, () => { a = 1 }))+ a = 2+ undo.commitBatch()++ undo.beginBatch('batch B')+ undo.push(entry('b1', () => { b = 10 }, () => { b = 0 }))+ b = 10+ undo.commitBatch()++ expect(undo.undoLabel).toBe('batch B')+ undo.undo()+ expect(b).toBe(0)++ expect(undo.undoLabel).toBe('batch A')+ undo.undo()+ expect(a).toBe(0)+ })++ test('discrete action between batches is separate undo entry', () => {+ const undo = new UndoManager()+ let v = 0++ undo.beginBatch('drag')+ undo.push(entry('d1', () => { v = 1 }, () => { v = 0 }))+ v = 1+ undo.push(entry('d2', () => { v = 2 }, () => { v = 1 }))+ v = 2+ undo.commitBatch()++ undo.push(entry('add fill', () => { v = 100 }, () => { v = 2 }))+ v = 100++ undo.undo()+ expect(v).toBe(2)++ undo.undo()+ expect(v).toBe(0)+ })++ test('batch redo replays all steps in order', () => {+ const undo = new UndoManager()+ const log: number[] = []++ undo.beginBatch('batch')+ undo.apply(entry('s1', () => log.push(1), () => log.pop()))+ undo.apply(entry('s2', () => log.push(2), () => log.pop()))+ undo.apply(entry('s3', () => log.push(3), () => log.pop()))+ undo.commitBatch()++ expect(log).toEqual([1, 2, 3])++ undo.undo()+ expect(log).toEqual([])++ undo.redo()+ expect(log).toEqual([1, 2, 3])+ })++ test('commitBatch with no entries is a no-op', () => {+ const undo = new UndoManager()+ undo.push(entry('existing', noop, noop))++ undo.beginBatch('empty')+ undo.commitBatch()++ expect(undo.undoLabel).toBe('existing')+ })+})