Customize tool bar in Angular Document editor component
24 Jun 20264 minutes to read
How to customize existing toolbar in DocumentEditorContainer
Angular DOCX Editor (Document Editor) Container allows you to customize(add, show, hide, enable, and disable) existing items in a toolbar.
-
Add - New items can defined by
CustomToolbarItemModeland with existing items intoolbarItemsproperty. Newly added item click action can be defined intoolbarClick. - Show, Hide - Existing items can be shown or hidden using the
toolbarItemsproperty. Pre-defined toolbar items are available withToolbarItem. - Enable, Disable - Toolbar items can be enabled or disable using
enableItems
import { Component, OnInit, ViewChild } from '@angular/core';
import {
ToolbarService,
DocumentEditorContainerComponent,
} from '@syncfusion/ej2-angular-documenteditor';
import { ClickEventArgs } from '@syncfusion/ej2-navigations';
import {
CustomToolbarItemModel,
DocumentEditorContainerModule,
} from '@syncfusion/ej2-angular-documenteditor';
@Component({
selector: 'app-container',
standalone: true,
imports: [DocumentEditorContainerModule],
providers: [ToolbarService],
template: `
<ejs-documenteditorcontainer #documenteditor_default
serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/"
height="600px"
style="display:block"
[toolbarItems]="items"
(toolbarClick)="onToolbarClick($event)"
(created)="onCreate()"
[enableToolbar]="true">
</ejs-documenteditorcontainer>
`,
})
export class AppComponent implements OnInit {
@ViewChild('documenteditor_default')
public container?: DocumentEditorContainerComponent;
// Define custom toolbar item
public toolItem: CustomToolbarItemModel = {
prefixIcon: 'e-de-ctnr-lock',
tooltipText: 'Disable Image',
text: this.onWrapText('Disable Image'),
id: 'Custom',
};
// Define toolbar items array
public items = [
this.toolItem,
'Undo',
'Redo',
'Separator',
'Image',
'Table',
'Hyperlink',
'Bookmark',
'TableOfContents',
'Separator',
'Header',
'Footer',
'PageSetup',
'PageNumber',
'Break',
'InsertFootnote',
'InsertEndnote',
'Separator',
'Find',
'Separator',
'Comments',
'TrackChanges',
'Separator',
'LocalClipboard',
'RestrictEditing',
'Separator',
'FormFields',
'UpdateFields',
'ContentControl',
];
ngOnInit(): void {}
// Called when the DocumentEditorContainer is created
onCreate() {
// Logic to handle the creation of the editor
}
// Event handler for toolbar clicks
onToolbarClick(args: ClickEventArgs): void {
switch (args.item.id) {
case 'Custom':
// Disable the image toolbar item
if (this.container) {
this.container.toolbar.enableItems(4, false);
}
break;
}
}
// Wrap text with custom HTML
private onWrapText(text: string): string {
let content: string = '';
const index: number = text.lastIndexOf(' ');
if (index !== -1) {
content =
text.slice(0, index) +
"<div class='e-de-text-wrap'>" +
text.slice(index + 1) +
'</div>';
} else {
content = text;
}
return content;
}
}Note: Default value of
toolbarItemsis['New', 'Open', 'Separator', 'Undo', 'Redo', 'Separator', 'Image', 'Table', 'Hyperlink', 'Bookmark', 'TableOfContents', 'Separator', 'Header', 'Footer', 'PageSetup', 'PageNumber', 'Break', 'InsertFootnote', 'InsertEndnote', 'Separator', 'Find', 'Separator', 'Comments', 'TrackChanges', 'Separator', 'LocalClipboard', 'RestrictEditing', 'Separator', 'FormFields', 'UpdateFields','ContentControl'].
Online Demo
Explore how to customize the toolbar in the Angular Document Editor for working with Word documents in this live demo here.