AI-native design editor. Open-source Figma alternative.
Fix text edit not pushing to undo history
Danila Poyarkovf77c42d79803
diff --git a/packages/core/src/editor/index.ts b/packages/core/src/editor/index.tsindex 3feed594b..5cd8693c1 100644--- a/packages/core/src/editor/index.ts+++ b/packages/core/src/editor/index.ts@@ -1,4 +1,5 @@export { createDefaultEditorState, createEditor } from './create'export type { Editor } from './create'+export { createTextActions } from './text'export { EDITOR_TOOLS, TOOL_SHORTCUTS } from './types'export type { EditorContext, EditorOptions, EditorState, EditorToolDef, Tool } from './types'diff --git a/packages/core/src/editor/text.ts b/packages/core/src/editor/text.tsindex 0d9f776c6..bf22605e9 100644--- a/packages/core/src/editor/text.ts+++ b/packages/core/src/editor/text.ts@@ -1,11 +1,14 @@import type { EditorContext } from './types'export function createTextActions(ctx: EditorContext) {+ let textBeforeEdit: string | null = null+function startTextEditing(nodeId: string) {const te = ctx.getTextEditor()if (ctx.state.editingTextId) commitTextEdit()const node = ctx.graph.getNode(nodeId)if (!node) return+ textBeforeEdit = node.textctx.state.editingTextId = nodeIdif (te) {te.setRenderer(ctx.getRenderer())@@ -18,19 +21,21 @@ export function createTextActions(ctx: EditorContext) {const te = ctx.getTextEditor()if (!te?.isActive) {ctx.state.editingTextId = null+ textBeforeEdit = nullreturn}const result = te.stop()if (!result) {ctx.state.editingTextId = null+ textBeforeEdit = nullctx.requestRender()return}- const node = ctx.graph.getNode(result.nodeId)- const prevText = node?.text ?? ''+ const prevText = textBeforeEdit ?? ''const newText = result.textctx.graph.updateNode(result.nodeId, { text: newText })ctx.state.editingTextId = null+ textBeforeEdit = nullif (prevText !== newText) {ctx.undo.push({label: 'Edit text',diff --git a/tests/engine/text-edit-undo.test.ts b/tests/engine/text-edit-undo.test.tsnew file mode 100644index 000000000..4d19e8652--- /dev/null+++ b/tests/engine/text-edit-undo.test.ts@@ -0,0 +1,120 @@+import { describe, test, expect } from 'bun:test'++import { SceneGraph, TextEditor, UndoManager } from '@open-pencil/core'+import { createTextActions } from '@open-pencil/core/editor'++import type { EditorContext, EditorState } from '@open-pencil/core/editor'++function setup() {+ const graph = new SceneGraph()+ const pageId = graph.getPages()[0].id+ const undo = new UndoManager()+ const textEditor = new TextEditor({} as any)++ const state = {+ editingTextId: null,+ currentPageId: pageId,+ renderVersion: 0,+ sceneVersion: 0,+ } as EditorState++ const ctx: EditorContext = {+ graph,+ undo,+ state,+ requestRender: () => { state.renderVersion++; state.sceneVersion++ },+ requestRepaint: () => { state.renderVersion++ },+ getTextEditor: () => textEditor,+ getRenderer: () => null,+ runLayoutForNode: () => {},+ getCk: () => null,+ loadFont: async () => {},+ getViewportSize: () => ({ width: 800, height: 600 }),+ subscribeToGraph: () => {},+ }++ const textNode = graph.createNode('TEXT', pageId, {+ name: 'Label',+ text: 'Hello',+ width: 100,+ height: 20,+ })++ const actions = createTextActions(ctx)+ return { graph, undo, textEditor, state, textNode, actions }+}++describe('text edit undo', () => {+ test('commitTextEdit pushes undo entry when text changed', () => {+ const { graph, undo, textEditor, textNode, actions } = setup()++ actions.startTextEditing(textNode.id)+ expect(textEditor.isActive).toBe(true)++ textEditor.insert(' World', textNode)+ graph.updateNode(textNode.id, { text: textEditor.state!.text })++ actions.commitTextEdit()++ expect(undo.canUndo).toBe(true)+ expect(undo.undoLabel).toBe('Edit text')+ expect(graph.getNode(textNode.id)!.text).toBe('Hello World')++ undo.undo()+ expect(graph.getNode(textNode.id)!.text).toBe('Hello')++ undo.redo()+ expect(graph.getNode(textNode.id)!.text).toBe('Hello World')+ })++ test('commitTextEdit does not push undo when text unchanged', () => {+ const { undo, actions, textNode } = setup()++ actions.startTextEditing(textNode.id)+ actions.commitTextEdit()++ expect(undo.canUndo).toBe(false)+ })++ test('undo restores original text even when graph was synced mid-edit', () => {+ const { graph, undo, textEditor, textNode, actions } = setup()++ actions.startTextEditing(textNode.id)++ textEditor.insert(' Beautiful', textNode)+ graph.updateNode(textNode.id, { text: textEditor.state!.text })++ textEditor.insert(' World', textNode)+ graph.updateNode(textNode.id, { text: textEditor.state!.text })++ actions.commitTextEdit()++ expect(graph.getNode(textNode.id)!.text).toBe('Hello Beautiful World')+ expect(undo.canUndo).toBe(true)++ undo.undo()+ expect(graph.getNode(textNode.id)!.text).toBe('Hello')+ })++ test('sequential edits create separate undo entries', () => {+ const { graph, undo, textEditor, textNode, actions } = setup()++ actions.startTextEditing(textNode.id)+ textEditor.insert('!', textNode)+ graph.updateNode(textNode.id, { text: textEditor.state!.text })+ actions.commitTextEdit()++ actions.startTextEditing(textNode.id)+ textEditor.insert('!', textNode)+ graph.updateNode(textNode.id, { text: textEditor.state!.text })+ actions.commitTextEdit()++ expect(graph.getNode(textNode.id)!.text).toBe('Hello!!')++ undo.undo()+ expect(graph.getNode(textNode.id)!.text).toBe('Hello!')++ undo.undo()+ expect(graph.getNode(textNode.id)!.text).toBe('Hello')+ })+})