Undo redo in Angular Diagram control
10 Dec 202415 minutes to read
Diagram tracks the history of actions that are performed after initializing the diagram and provides support to reverse and restore those changes.
Undo and redo
Diagram provides built-in support to track the changes that are made through interaction and through public APIs. The changes can be reverted or restored either through shortcut keys or through commands.
NOTE
If you want to use Undo-Redo in diagram, you need to inject UndoRedo in the diagram.
Undo/redo through shortcut keys
Undo/redo commands can be executed through shortcut keys. Shortcut key for undo is Ctrl+z
and shortcut key for redo is Ctrl+y
.
Undo/redo through public APIs
The client-side methods undo
and redo
help you to revert/restore the changes. The following code example illustrates how to undo/redo the changes through script.
@Component({
selector: "app-container",
// Diagram template
template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px" (created)='created($event)'>
</ejs-diagram>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild("diagram")
public diagram: DiagramComponent;
public created(args: Object): void {
// Reverts the last action performed
this.diagram.undo();
// Restores the last undone action
this.diagram.redo();
}
}
Undo/Redo for diagram can be enabled/disabled with the constraints
property of diagram.
When a change in the diagram is reverted or restored (undo/redo), the historyChange
event gets triggered.
Group multiple changes
History list allows to revert or restore multiple changes through a single undo/redo command. For example, revert/restore the fill color change of multiple elements at a time.
The diagram method startGroupAction
allows you to log multiple actions at a time in the history manager stack. It is easier to undo or revert the changes made in the diagram in a single undo/redo process instead of reverting every actions one by one.The diagram method endGroupAction
allows you to end the group actions that are stored in the stack history. The following code illustrates how to undo/redo multiple fillColor change of a node at a time.
import { Component, ViewEncapsulation, ViewChild } from "@angular/core";
import { DiagramModule, UndoRedoService, DiagramComponent, Diagram, ShapeStyleModel } from "@syncfusion/ej2-angular-diagrams";
@Component({
imports: [
DiagramModule
],
providers: [UndoRedoService],
standalone: true,
selector: "app-container",
template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px" (created)='created($event)'>
<e-nodes>
<e-node id='node1' [offsetX]=150 [offsetY]=150 [width]=100 [height]=100>
</e-node>
</e-nodes>
</ejs-diagram>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild("diagram")
public diagram?: DiagramComponent;
public created(args: Object): void {
//Start to group the changes
(this.diagram as Diagram).startGroupAction();
//Makes the changes
let color: string[] = ['black', 'red', 'green', 'yellow'];
for (var i = 0; i < color.length; i++) {
// Updates the fillColor for all the child elements.
((this.diagram as Diagram).nodes[0].style as ShapeStyleModel).fill = color[i];
(this.diagram as Diagram).dataBind();
}
//Ends grouping the changes
(this.diagram as Diagram).endGroupAction();
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Stack limit
The stackLimit
property of history manager is used to limits the number of actions to be stored on the history manager.
import { Component, ViewEncapsulation, ViewChild } from "@angular/core";
import { DiagramModule, UndoRedoService,DiagramComponent, Diagram } from "@syncfusion/ej2-angular-diagrams";
@Component({
imports: [
DiagramModule
],
providers: [UndoRedoService],
standalone: true,
selector: "app-container",
template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px" (created)='created($event)'>
<e-nodes>
<e-node id='node1' [offsetX]=150 [offsetY]=150 [width]=100 [height]=100>
</e-node>
</e-nodes>
</ejs-diagram>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild("diagram")
public diagram?: DiagramComponent;
public created(args: Object): void {
(this.diagram as Diagram).historyManager.stackLimit =3;
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Restrict Undo/Redo
Undo, Redo process can be avoided for particular element by using canLog
property in the history manager. The following example illustrates how to prevent history entry using canLog
function.
import { Component, ViewEncapsulation, ViewChild } from "@angular/core";
import { DiagramModule, UndoRedoService, DiagramComponent, Diagram, HistoryEntry } from "@syncfusion/ej2-angular-diagrams";
@Component({
imports: [
DiagramModule
],
providers: [UndoRedoService],
standalone: true,
selector: "app-container",
template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px" (created)='created($event)'>
<e-nodes>
<e-node id='node1' [offsetX]=150 [offsetY]=150 [width]=100 [height]=100>
</e-node>
</e-nodes>
</ejs-diagram>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild("diagram")
public diagram?: DiagramComponent;
public created(args: Object): void {
// canLog decide whether the entry add or not in history List
(this.diagram as Diagram | any).historyManager.canLog = function(entry: HistoryEntry) {
entry.cancel = true;
return entry;
}
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
undo/redo stack
The undoStack
property is used to get the collection of undo actions which should be performed in the diagram. The redoStack
property is used to get the collection of redo actions which should be performed in the diagram. The undoStack/redoStack is the read-only property.
@Component({
selector: "app-container",
// render initialized Diagram
template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px">
</ejs-diagram>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild("diagram")
public diagram: DiagramComponent;
//get the collection of undoStack objects
public undoStack: HistoryEntry[] = this.diagram.historyList.undoStack;
//get the collection of redoStack objects
public redoStack: HistoryEntry[] = this.diagram.historyList.redoStack;
}
Current entry
While performing interactions with a node or connector, the current history entry is added to the currentEntry
property of the historyManager
.
The following code shows how to get the current entry from the diagram history:
import { Component, ViewEncapsulation, ViewChild } from "@angular/core";
import { DiagramModule, UndoRedoService, DiagramComponent, Diagram, IHistoryChangeArgs } from "@syncfusion/ej2-angular-diagrams";
@Component({
imports: [
DiagramModule
],
providers: [UndoRedoService],
standalone: true,
selector: "app-container",
template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px" (historyChange)="historyChange($event)">
<e-nodes>
<e-node id='node1' [offsetX]=150 [offsetY]=150 [width]=100 [height]=100>
</e-node>
</e-nodes>
</ejs-diagram>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild("diagram")
public diagram?: DiagramComponent;
public historyChange(args: IHistoryChangeArgs): void {
console.log((this.diagram as Diagram | any).historyManager.currentEntry)
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Clear history
The clearHistory
method of diagram is used to remove all the recorded actions from the undo and redo history.
//Clears all the histories
this.diagram.clearHistory();
Get history stack
The getHistoryStack
method of the diagram retrieves the undoStack
or redoStack
from the historyManager. This method takes a single parameter, isUndoStack. Pass true to get the undoStack or false to get the redoStack.
// Fetch undoStack from history manager
this.diagram.getHistoryStack(true)
// Fetch redoStack from history manager
this.diagram.getHistoryStack(false)
History change event
The historyChange
event triggers, whenever the interaction of the node and connector is take place. When interacting, the entries get added to the history manager to trigger this event. The following example shows how to get this event in diagram.
import { Component, ViewEncapsulation, ViewChild } from "@angular/core";
import { DiagramModule, UndoRedoService, DiagramComponent, NodeModel, IHistoryChangeArgs } from "@syncfusion/ej2-angular-diagrams";
@Component({
imports: [
DiagramModule
],
providers: [UndoRedoService],
standalone: true,
selector: "app-container",
template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px" [getNodeDefaults]='getNodeDefaults' (historyChange)="historyChange($event)">
<e-nodes>
<e-node id='node1' [offsetX]=250 [offsetY]=150>
<e-node-annotations>
<e-node-annotation content="Node 1">
</e-node-annotation>
</e-node-annotations>
</e-node>
<e-node id='node2' [offsetX]=250 [offsetY]=350>
<e-node-annotations>
<e-node-annotation content="Node 2">
</e-node-annotation>
</e-node-annotations>
</e-node>
</e-nodes>
<e-connectors>
<e-connector id='connector' sourceID='node1' targetID='node2'>
</e-connector>
</e-connectors>
</ejs-diagram>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild("diagram")
public diagram?: DiagramComponent;
public getNodeDefaults(node: NodeModel): NodeModel {
node.height = 100;
node.width = 100;
return node;
};
public historyChange(args: IHistoryChangeArgs): void {
console.log(args)
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
While interacting with diagram, this event can be used to do the customization.