AI-native design editor. Open-source Figma alternative.
fix(editor): close preview and history lifecycle gaps
Danila Poyarkovc59879105cd0
diff --git a/packages/core/src/editor/create.ts b/packages/core/src/editor/create.tsindex e1b5d8ba3..0abb14a80 100644--- a/packages/core/src/editor/create.ts+++ b/packages/core/src/editor/create.ts@@ -256,6 +256,7 @@ export function createEditor(options?: EditorOptions) {function replaceGraph(newGraph: SceneGraph) {nodes.cancelNodePreviews()+ undo.discardBatches()_graph = newGraphsubscribeToGraph()const previousPageId = state.currentPageIddiff --git a/packages/scene-graph/src/undo.ts b/packages/scene-graph/src/undo.tsindex 7b930d2e2..6d838c6cf 100644--- a/packages/scene-graph/src/undo.ts+++ b/packages/scene-graph/src/undo.ts@@ -103,10 +103,15 @@ export class UndoManager {for (const entry of batch.entries.toReversed()) entry.inverse()}+ /** Abandon provisional history without replaying it or changing committed undo/redo entries. */+ discardBatches(): void {+ this.batches = []+ }+clear(): void {this.undoStack = []this.redoStack = []- this.batches = []+ this.discardBatches()this.onChange?.()}diff --git a/packages/vue/src/controls/node-preview/use.ts b/packages/vue/src/controls/node-preview/use.tsindex 98c41337d..12bbd35f9 100644--- a/packages/vue/src/controls/node-preview/use.ts+++ b/packages/vue/src/controls/node-preview/use.ts@@ -15,19 +15,27 @@ export function useNodePreview(editor: Editor) {targets = [...ids]preview = editor.beginNodePreview(label)}- for (const id of targets) preview.update(id, changes)+ try {+ for (const id of targets) preview.update(id, changes)+ } catch (error) {+ cancel()+ throw error+ }}- function commit() {- preview?.commit()+ function takePreview() {+ const current = previewpreview = undefinedtargets = []+ return current+ }++ function commit() {+ takePreview()?.commit()}function cancel() {- preview?.cancel()- preview = undefined- targets = []+ takePreview()?.cancel()}tryOnScopeDispose(cancel)diff --git a/packages/vue/src/editor/selection-state/nodes.ts b/packages/vue/src/editor/selection-state/nodes.tsindex c625b7b35..45a380bf6 100644--- a/packages/vue/src/editor/selection-state/nodes.ts+++ b/packages/vue/src/editor/selection-state/nodes.ts@@ -1,5 +1,5 @@import { tryOnScopeDispose, watchImmediate } from '@vueuse/core'-import { computed, shallowReactive } from 'vue'+import { computed, shallowReactive, shallowRef } from 'vue'import type { ComputedRef, Ref } from 'vue'import type { Editor } from '@open-pencil/core/editor'@@ -19,8 +19,16 @@ export function createSelectedNodeState(editor: Editor,active?: Readonly<Ref<boolean>>): SelectedNodeState {+ const revision = shallowRef(0)+ const refresh = () => {+ revision.value+++ }+ const refreshSelected = (id: string) => {+ if (editor.state.selectedIds.has(id)) refresh()+ }const nodes = computed<SceneNode[]>((previous) => {if (active?.value === false && previous) return previous+ void revision.valuevoid editor.state.sceneVersionvoid editor.state.currentPageIdreturn editor.getSelectedNodes().map((node) => shallowReactive(node))@@ -38,7 +46,16 @@ export function createSelectedNodeState(() => active?.value ?? true,(enabled, _previous, onCleanup) => {if (!enabled) return+ refresh()onCleanup(editor.onEditorEvent('node:previewUpdated', updatePreview))+ onCleanup(editor.onEditorEvent('node:updated', refreshSelected))+ onCleanup(editor.onEditorEvent('selection:changed', refresh))+ onCleanup(editor.onEditorEvent('graph:replaced', refresh))+ onCleanup(editor.onEditorEvent('page:changed', refresh))+ onCleanup(editor.onEditorEvent('node:created', refresh))+ onCleanup(editor.onEditorEvent('node:deleted', refresh))+ onCleanup(editor.onEditorEvent('node:reparented', refresh))+ onCleanup(editor.onEditorEvent('node:reordered', refresh))},{ flush: 'sync' })diff --git a/tests/e2e/canvas/scene-picture-freshness.spec.ts-snapshots/fresh-scene-picture-openpencil-darwin.png b/tests/e2e/canvas/scene-picture-freshness.spec.ts-snapshots/fresh-scene-picture-openpencil-darwin.pngindex c84cd3858..e2f923a75 100644Binary files a/tests/e2e/canvas/scene-picture-freshness.spec.ts-snapshots/fresh-scene-picture-openpencil-darwin.png and b/tests/e2e/canvas/scene-picture-freshness.spec.ts-snapshots/fresh-scene-picture-openpencil-darwin.png differdiff --git a/tests/engine/scene-graph/undo/batch.test.ts b/tests/engine/scene-graph/undo/batch.test.tsnew file mode 100644index 000000000..bbaa28cd1--- /dev/null+++ b/tests/engine/scene-graph/undo/batch.test.ts@@ -0,0 +1,38 @@+import { expect, test } from 'bun:test'++import { UndoManager } from '@open-pencil/scene-graph'++test('discarding nested batches preserves committed undo/redo without replay or notifications', () => {+ let changes = 0+ let replayed = 0+ const undo = new UndoManager({+ onChange: () => {+ changes+++ }+ })+ const noop = () => undefined+ undo.push({ label: 'First', forward: noop, inverse: noop })+ undo.push({ label: 'Second', forward: noop, inverse: noop })+ undo.undo()+ const pending = {+ label: 'Pending',+ forward: () => {+ replayed+++ },+ inverse: () => {+ replayed+++ }+ }+ undo.beginBatch('Outer')+ undo.push(pending)+ undo.beginBatch('Inner')+ undo.push(pending)+ undo.discardBatches()+ expect(undo.isBatching).toBe(false)+ expect(replayed).toBe(0)+ expect(changes).toBe(3)+ expect(undo.undoLabel).toBe('First')+ expect(undo.redoLabel).toBe('Second')+ undo.commitBatch()+ expect(undo.redo()).toBe('Second')+})diff --git a/tests/engine/vue/controls/node-preview.test.ts b/tests/engine/vue/controls/node-preview.test.tsnew file mode 100644index 000000000..537d394eb--- /dev/null+++ b/tests/engine/vue/controls/node-preview.test.ts@@ -0,0 +1,38 @@+import { expect, test } from 'bun:test'++import { createEditor } from '@open-pencil/core/editor'++import { useNodePreview } from '#vue/controls/node-preview/use'++for (const operation of ['update', 'commit'] as const) {+ test(`a failed preview ${operation} releases targets for the next interaction`, () => {+ const editor = createEditor()+ const control = useNodePreview(editor)+ try {+ const node = editor.graph.createNode('COMPONENT', editor.state.currentPageId, { x: 10 })+ const next = editor.graph.createNode('RECTANGLE', editor.state.currentPageId, { x: 20 })+ if (operation === 'commit') control.update([node.id], { x: 50 }, 'Move')+ editor.graph.updateNode(node.id, {+ librarySource: {+ identity: { libraryId: 'preview-test', assetKey: 'component', revisionId: 'r1' },+ sourceNodeId: 'source',+ readOnly: true+ }+ })+ expect(() => {+ if (operation === 'commit') control.commit()+ else control.update([node.id], { x: 50 }, 'Move')+ }).toThrow('Edit the source library')+ expect(editor.isInteractiveEditing()).toBe(false)+ control.update([next.id], { x: 90 }, 'Move')+ expect(editor.graph.getNode(next.id)?.x).toBe(90)+ control.commit()+ expect(next.x).toBe(90)+ editor.undoAction()+ expect(next.x).toBe(20)+ } finally {+ control.cancel()+ editor.dispose()+ }+ })+}diff --git a/tests/engine/vue/editor/selected-nodes.test.ts b/tests/engine/vue/editor/selected-nodes.test.tsindex 1a3ebeab5..08d392603 100644--- a/tests/engine/vue/editor/selected-nodes.test.ts+++ b/tests/engine/vue/editor/selected-nodes.test.ts@@ -31,6 +31,52 @@ function setup() {}describe('selected-node preview projections', () => {+ test('tracks committed changes and selection with a plain editor state', () => {+ const editor = createEditor()+ const active = ref(true)+ const selection = createSelectedNodeState(editor, active)+ try {+ expect(selection.node.value).toBeNull()+ const first = editor.graph.createNode('RECTANGLE', editor.state.currentPageId, { x: 10 })+ editor.select([first.id])+ expect(selection.node.value?.x).toBe(10)+ editor.updateNodeWithUndo(first.id, { x: 40 })+ expect(selection.node.value?.x).toBe(40)+ editor.undoAction()+ expect(selection.node.value?.x).toBe(10)+ active.value = false+ editor.updateNode(first.id, { x: 70 })+ expect(selection.node.value?.x).toBe(10)+ active.value = true+ expect(selection.node.value?.x).toBe(70)+ editor.graph.deleteNode(first.id)+ expect(selection.node.value).toBeNull()+ } finally {+ selection.dispose()+ editor.dispose()+ }+ })++ test('projection writes stay isolated and preview cancellation restores the graph without history', () => {+ const fixture = setup()+ const { editor, first, selection } = fixture+ try {+ expect(selection.node.value?.x).toBe(10)+ const preview = editor.beginNodePreview('Move')+ preview.update(first.id, { x: 80 })+ const projected = selection.node.value+ if (!projected) throw new Error('Missing selected node')+ expect(projected.x).toBe(80)+ projected.x = 99+ expect(first.x).toBe(80)+ preview.cancel()+ expect(selection.node.value?.x).toBe(10)+ expect(first.x).toBe(10)+ expect(editor.undo.canUndo).toBe(false)+ } finally {+ fixture.dispose()+ }+ })test('freezes inactive projections, refreshes on activation and disposes subscriptions', () => {const fixture = setup()const { editor, first, second } = fixturediff --git a/tests/engine/vue/input/draw.test.ts b/tests/engine/vue/input/draw.test.tsindex 7e3bb3708..387f90b85 100644--- a/tests/engine/vue/input/draw.test.ts+++ b/tests/engine/vue/input/draw.test.ts@@ -1,6 +1,7 @@import { describe, expect, test } from 'bun:test'import { createEditor, type Editor, type Tool } from '@open-pencil/core/editor'+import { SceneGraph } from '@open-pencil/scene-graph'import { handleDrawMove, startShapeDraw, startTextDraw } from '#vue/shared/input/draw'import type { DragState } from '#vue/shared/input/types'@@ -19,6 +20,27 @@ function start(editor: Editor, tool: Tool = 'FRAME') {}describe('draw creation previews', () => {+ test('graph replacement discards pending draw batches without replaying or deleting history', () => {+ const editor = createEditor()+ try {+ editor.createShape('RECTANGLE', 0, 0, 20, 20)+ const previousLabel = editor.undo.undoLabel+ const drag = start(editor)+ const graph = new SceneGraph()+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, { x: 10 })+ editor.replaceGraph(graph)+ expect(editor.undo.isBatching).toBe(false)+ expect(editor.undo.undoLabel).toBe(previousLabel)+ drag.commit()+ editor.updateNodeWithUndo(node.id, { x: 50 }, 'New graph move')+ expect(editor.undo.undoLabel).toBe('New graph move')+ editor.undoAction()+ expect(node.x).toBe(10)+ expect(editor.undo.undoLabel).toBe(previousLabel)+ } finally {+ editor.dispose()+ }+ })test('is interactive immediately, previews geometry, and creates one undo step', () => {const editor = createEditor()try {diff --git a/tests/engine/vue/renderer.test.ts b/tests/engine/vue/renderer.test.tsnew file mode 100644index 000000000..e5388fbea--- /dev/null+++ b/tests/engine/vue/renderer.test.ts@@ -0,0 +1,16 @@+import { expect, test } from 'bun:test'++import { createStaticVNode } from 'vue'++import { createTestRenderer, hostNode } from '#tests/helpers/vue/renderer'++test('the host renderer mounts and removes compiled static content', () => {+ const renderer = createTestRenderer()+ const root = hostNode()+ renderer.render(createStaticVNode('<span>Static content</span>', 1), root)+ expect(root.children).toHaveLength(1)+ expect(root.children[0].text).toBe('<span>Static content</span>')+ expect(root.children[0].parent).toBe(root)+ renderer.render(null, root)+ expect(root.children).toEqual([])+})diff --git a/tests/helpers/vue/renderer.ts b/tests/helpers/vue/renderer.tsindex 23614cf11..61a026f7e 100644--- a/tests/helpers/vue/renderer.ts+++ b/tests/helpers/vue/renderer.ts@@ -18,16 +18,18 @@ function remove(child: HostNode): void {child.parent = null}+function insert(child: HostNode, parent: HostNode, anchor?: HostNode | null): void {+ remove(child)+ child.parent = parent+ const index = anchor ? parent.children.indexOf(anchor) : -1+ if (index < 0) parent.children.push(child)+ else parent.children.splice(index, 0, child)+}+export function createTestRenderer() {return createRenderer<HostNode, HostNode>({patchProp: () => undefined,- insert(child, parent, anchor) {- remove(child)- child.parent = parent- const index = anchor ? parent.children.indexOf(anchor) : -1- if (index < 0) parent.children.push(child)- else parent.children.splice(index, 0, child)- },+ insert,remove,createElement: () => hostNode(),createText: hostNode,@@ -47,7 +49,7 @@ export function createTestRenderer() {setScopeId: () => undefined,insertStaticContent(content, parent, anchor) {const node = hostNode(content)- this.insert(node, parent, anchor)+ insert(node, parent, anchor)return [node, node]}})