Implement undo/redo functionality and manage multiple history stacks to track editing operations.
CE.SDK keeps a history of undo steps, so you can undo and redo changes programmatically. An edit through the engine API becomes an undo step when you call engine.editor.addUndoStep(). You can also create multiple independent history stacks to isolate different editing contexts, such as separate histories for different processing pipelines.
This guide covers how to perform undo and redo operations programmatically, subscribe to history changes, manually manage undo steps, and work with multiple history stacks.
Setup#
We start by initializing the CE.SDK engine and creating a design scene. The engine automatically creates a history stack when initialized.
const engine = await CreativeEngine.init({ baseURL: process.env.IMGLY_LOCAL_ASSETS_URL // license: process.env.CESDK_LICENSE});
try { // Create a design scene with specific page dimensions engine.scene.create('VerticalStack', { page: { size: { width: 800, height: 600 } } }); const page = engine.block.findByType('page')[0];Undo Step Creation#
An edit through the engine API becomes an undo step when you call engine.editor.addUndoStep(). We add a shape to the scene and then add an undo step for it.
// Create a triangle shape and add an undo step to record it in historyconst block = engine.block.create('graphic');engine.block.setPositionX(block, 140);engine.block.setPositionY(block, 95);engine.block.setWidth(block, 265);engine.block.setHeight(block, 265);const triangleShape = engine.block.createShape('polygon');engine.block.setInt(triangleShape, 'shape/polygon/sides', 3);engine.block.setShape(block, triangleShape);const triangleFill = engine.block.createFill('color');engine.block.setColor(triangleFill, 'fill/color/value', { r: 0.2, g: 0.5, b: 0.9, a: 1});engine.block.setFill(block, triangleFill);engine.block.appendChild(page, block);// Commit the block creation to history so it can be undoneengine.editor.addUndoStep();After creating the shape, canUndo() returns true since the operation has been recorded as an undoable step.
Performing Undo and Redo Operations#
We use engine.editor.undo() and engine.editor.redo() to programmatically revert or restore changes. Before calling these methods, check availability with engine.editor.canUndo() and engine.editor.canRedo() to prevent errors.
// Undo the block creationif (engine.editor.canUndo()) { engine.editor.undo(); console.log( 'After undo - canUndo:', engine.editor.canUndo(), 'canRedo:', engine.editor.canRedo() );}The undo operation reverts the most recent change. After undoing, canRedo() returns true since there’s now a step available to restore.
// Redo to restore the blockif (engine.editor.canRedo()) { engine.editor.redo(); console.log( 'After redo - canUndo:', engine.editor.canUndo(), 'canRedo:', engine.editor.canRedo() );}The redo operation restores the most recently undone change. After redoing, canRedo() returns false (unless there are more undo steps to restore).
Subscribing to History Changes#
We use engine.editor.onHistoryUpdatedWithKind() to receive notifications when the history state changes. The callback receives a HistoryUpdate argument that distinguishes the kind of change:
'Updated'— the active history’s snapshots changed because of an edit, anaddUndoStepcall, or anundo/redo. The scene reflects the new state.'Activated'— a different history buffer was made active viasetActiveHistory(). The undo/redo stack visible to the user changed, but no new snapshot was created and no undo or redo was applied.
This separation lets dirty-state or auto-save logic ignore pure activation events when switching between isolated processing pipelines.
// Subscribe to history updates.const unsubscribe = engine.editor.onHistoryUpdatedWithKind((kind) => { if (kind === 'Activated') { console.log('Active history switched, scene unchanged.'); return; } const canUndo = engine.editor.canUndo(); const canRedo = engine.editor.canRedo(); console.log('History updated:', { canUndo, canRedo });});The subscription returns an unsubscribe function. Call it when you no longer need notifications.
Managing Undo Steps Manually#
Most engine API calls create no undo step by themselves. Call engine.editor.addUndoStep() after a change, or after several related changes that should be undone as a single unit. undo() and redo() discard scene edits made since the last undo step.
// Manually add an undo step after custom operationsengine.block.setPositionX(block, 190);engine.editor.addUndoStep();console.log('Manual undo step added. canUndo:', engine.editor.canUndo());
// Remove the most recent undo step if neededif (engine.editor.canUndo()) { engine.editor.removeUndoStep(); console.log('Most recent undo step removed');}// Reset block position to its original locationengine.block.setPositionX(block, 140);We use engine.editor.removeUndoStep() to remove the most recent undo step. Always check canUndo() before calling this method to ensure an undo step is available. Its edits stay in the scene without an undo step, so the next undo() discards them.
Working with Multiple History Stacks#
CE.SDK supports multiple independent history stacks for isolated editing contexts. This is useful when you need separate undo/redo histories for different parts of your application, such as different processing pipelines or batch operations.
Creating and Switching History Stacks#
We create a new history stack using engine.editor.createHistory(). Use engine.editor.setActiveHistory() to switch between stacks. Only the active history responds to undo/redo operations. Setting a history that is not new as active, also the active one, discards edits without an undo step. Edits with an undo step stay in the history they were made in.
// Create a second history stack for isolated operationsconst secondaryHistory = engine.editor.createHistory();const primaryHistory = engine.editor.getActiveHistory();console.log( 'Created secondary history. Primary:', primaryHistory, 'Secondary:', secondaryHistory);
// Switch to the secondary historyengine.editor.setActiveHistory(secondaryHistory);console.log( 'Switched to secondary history. Active:', engine.editor.getActiveHistory());
// Operations in secondary history are isolated from the primary historyconst secondBlock = engine.block.create('graphic');engine.block.setPositionX(secondBlock, 440);engine.block.setPositionY(secondBlock, 95);engine.block.setWidth(secondBlock, 220);engine.block.setHeight(secondBlock, 220);const circleShape = engine.block.createShape('ellipse');engine.block.setShape(secondBlock, circleShape);const circleFill = engine.block.createFill('color');engine.block.setColor(circleFill, 'fill/color/value', { r: 0.9, g: 0.3, b: 0.3, a: 1});engine.block.setFill(secondBlock, circleFill);engine.block.appendChild(page, secondBlock);// Commit changes to the secondary historyengine.editor.addUndoStep();console.log( 'Block added in secondary history. canUndo:', engine.editor.canUndo());
// Switch back to primary historyengine.editor.setActiveHistory(primaryHistory);console.log( 'Switched back to primary history. canUndo:', engine.editor.canUndo());Operations performed while a history is active only affect that history. When you switch back to the primary history, its undo/redo state remains unchanged by operations performed in the secondary history. A new history starts from the current scene, including edits that have no undo step yet and the current selection. When you switch back to a history, the scene returns to that history’s last undo step.
Cleaning Up History Stacks#
We destroy unused history stacks with engine.editor.destroyHistory() to free resources. Always clean up secondary histories when they’re no longer needed.
// Clean up the secondary history when no longer neededengine.editor.destroyHistory(secondaryHistory);console.log('Secondary history destroyed');Troubleshooting#
Common issues when working with undo/redo functionality:
- Undo step not recorded: Call
engine.editor.addUndoStep()after changes made through the engine API. - Redo not available: Adding an undo step after an undo clears the redo stack. This is standard behavior to prevent branching history states. An edit made without
addUndoStep()does not clear it: the nextundo()orredo()discards that edit. - Wrong history active: Always verify the correct history is set with
getActiveHistory()before performing undo/redo operations when using multiple stacks.