Undo redo in EJ2 JavaScript Block Editor control
15 Dec 20255 minutes to read
The undo/redo feature in Block Editor enables users to revert or reapply changes made to the content, offering a safety net for edits and enhancing the overall editing experience.
Keyboard shortcuts
| Action | Windows | Mac | Description |
|---|---|---|---|
| Undo | Ctrl + Z | ⌘ + Z | Reverts the last action. |
| Redo | Ctrl + Y | ⌘ + Y | Reapplies the last undone action. |
Configuring Undo/Redo stack
The Block Editor stores a history of actions, allowing users to perform undo and redo operations. By default, it saves up to 30 actions. You can customize this limit using the undoRedoStack property to control the maximum number of steps that can be undone or redone.
The example below sets the undo/redo history limit to 20 actions.
let blocksData = [
{
blockType: 'Heading',
properties: { level: 1},
content: [
{
contentType: ej.blockeditor.ContentType.Text,
content: 'Undo/Redo Demo'
}
]
},
{
blockType: 'Paragraph',
content: [
{
contentType: ej.blockeditor.ContentType.Text,
content: 'Try adding new blocks or modifying content below:'
}
]
},
{
blockType: 'Paragraph',
content: [
{
contentType: ej.blockeditor.ContentType.Text,
content: '1. Undo stack set to maximum 40 actions\n2. Press Ctrl+Z to undo\n3. Press Ctrl+Y to redo\n4. Actions include text edits, block moves, additions, deletions'
}
]
}
];
var blockeditor = new ej.blockeditor.BlockEditor({
blocks: blocksData,
undoRedoStack: 20
});
blockeditor.appendTo('#blockeditor_undo');<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 - BlockEditor</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="description" content="Essential JS 2">
<meta name="author" content="Syncfusion">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-base/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-inputs/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/fluent2.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/fluent2.css" rel="stylesheet"/>
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-dropdowns/styles/fluent2.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-blockeditor/styles/fluent2.css" rel="stylesheet" />
<!--style reference from app-->
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="blockeditor_undo"></div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<style>
#container {
visibility: hidden;
margin: 50px;
}
</style>
<script src="index.js" type="text/javascript"></script>
</body>
</html>