Undo Redo in React Image Editor
03 Sep 20268 minutes to read
The undo and redo features provide a way to reverse and repeat editing actions performed on an image. These features are essential for maintaining control and flexibility during the editing process.
In an React Image Editor, the undo and redo history typically has a limited capacity of 16 steps.
Undo an action
The undo action in an React Image Editor allows users to revert the most recent editing action or a series of actions to their previous state. When the undo command is triggered, the React Image Editor undoes the last applied modification, effectively restoring the image to its state before the action was performed. The undo action is useful for correcting mistakes, removing unwanted changes, or exploring different editing options without permanently altering the image.
Redo an action
The redo action in an React Image Editor allows users to reapply previously undone actions or modifications to the image. When the redo command is triggered, the React Image Editor reapplies the last action that was undone, bringing the image back to the state it was in after the action was initially applied. The redo is useful when users want to repeat an action that was previously undone or restore changes that were temporarily reversed.
Here is an example of undoing and redoing actions using the undo and redo methods.
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";
export default class App extends React.Component {
imgObj;
imageEditorCreated() {
if (Browser.isDevice) {
this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
}
else {
this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
}
}
drawText() {
let dimension = this.imgObj.getImageDimension();
this.imgObj.drawText(dimension.x, dimension.y);
}
undoClick() {
this.imgObj.undo();
}
redoClick() {
this.imgObj.redo();
}
render() {
return (<div className='e-img-editor-sample'>
<ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)} toolbar = {[]}>
</ImageEditorComponent>
<div>
<ButtonComponent cssClass='e-primary' content='Draw Text' onClick={this.drawText.bind(this)}/>
<ButtonComponent cssClass='e-primary' content='Undo' onClick={this.undoClick.bind(this)}/>
<ButtonComponent cssClass='e-primary' content='Redo' onClick={this.redoClick.bind(this)}/>
</div>
</div>);
}
}
ReactDOM.render(<App />, document.getElementById('image-editor'));import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
let imgObj: ImageEditorComponent;
function imageEditorCreated(): void {
if (Browser.isDevice) {
imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
} else {
imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
}
}
function drawText(): void {
let dimension: any = imgObj.getImageDimension();
imgObj.drawText(dimension.x, dimension.y);
}
function undoClick(): void {
imgObj.undo();
}
function redoClick(): void {
imgObj.redo();
}
return (
<div className='e-img-editor-sample'>
<ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated} toolbar = {[]}>
</ImageEditorComponent>
<div>
<ButtonComponent cssClass='e-primary' content='Draw Text' onClick = {drawText}/>
<ButtonComponent cssClass='e-primary' content='Undo' onClick = {undoClick}/>
<ButtonComponent cssClass='e-primary' content='Redo' onClick = {redoClick}/>
</div>
</div>
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));<!DOCTYPE html>
<html lang="en">
<head>
<title>Syncfusion React Image Editor</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for React Components" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='image-editor'>
<div id='loader'>Loading....</div>
</div>
</body>
</html>