Angular HtmlEditor Methods
This section describes methods that control the HtmlEditor widget.
beginUpdate()
Prevents the widget from refreshing until the endUpdate() method is called.
The beginUpdate() and endUpdate() methods prevent the widget from excessive updates when you are changing multiple widget settings at once. After the beginUpdate() method is called, the widget does not update its UI until the endUpdate() method is called.
See Also
defaultOptions(rule)
Specifies the device-dependent default configuration options for this component.
The component's default device options.
defaultOptions is a static method that the widget class supports. The following code demonstrates how to specify default options for all instances of the HtmlEditor widget in an application executed on the desktop.
jQuery
DevExpress.ui.dxHtmlEditor.defaultOptions({ device: { deviceType: "desktop" }, options: { // Here go the HtmlEditor options } });
Angular
import HtmlEditor from "devextreme/ui/html_editor"; // ... export class AppComponent { constructor () { HtmlEditor.defaultOptions({ device: { deviceType: "desktop" }, options: { // Here go the HtmlEditor options } }); } }
Vue
<template> <div> <dx-html-editor id="htmlEditor1" /> <dx-html-editor id="htmlEditor2" /> </div> </template> <script> import DxHtmlEditor from "devextreme-vue/html-editor"; import HtmlEditor from "devextreme/ui/html_editor"; HtmlEditor.defaultOptions({ device: { deviceType: "desktop" }, options: { // Here go the HtmlEditor options } }); export default { components: { DxHtmlEditor } } </script>
React
import React from "react"; import dxHtmlEditor from "devextreme/ui/html_editor"; import HtmlEditor from "devextreme-react/html-editor"; class App extends React.Component { render () { dxHtmlEditor.defaultOptions({ device: { deviceType: "desktop" }, options: { // Here go the HtmlEditor options } }) return ( <div> <HtmlEditor id="htmlEditor1" /> <HtmlEditor id="htmlEditor2" /> </div> ) } } export default App;
delete(index, length)
Deletes content from the given range.
A zero-based index at which to begin deleting.
The length of the content to be deleted.
Embedded items have a length of 1.
dispose()
Disposes of all the resources allocated to the HtmlEditor instance.
The following code disposes of an HtmlEditor instance that corresponds to the element ID (or reference variable in Angular) and removes the element from the DOM:
jQuery
$("#myHtmlEditor").dxHtmlEditor("dispose"); $("#myHtmlEditor").remove();
Angular
<dx-html-editor #htmlEditorVar id="myHtmlEditor"></dx-html-editor>
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild("htmlEditorVar") htmlEditor: DxHtmlEditorComponent; removeHtmlEditor (e) { this.htmlEditor.instance.dispose(); document.getElementById("myHtmlEditor").remove(); } }
element()
Gets the root widget element.
An HTML element or a jQuery element when you use jQuery.
endUpdate()
Refreshes the widget after a call of the beginUpdate() method.
Main article: beginUpdate()
See Also
format(formatName, formatValue)
Applies a format to the selected content. Cannot be used with embedded formats.
If no content is selected, the format applies to the character typed next.
jQuery
var htmlEditorInstance = $("#htmlEditorContainer").dxHtmlEditor("instance"); // Makes text bold htmlEditorInstance.format("bold", true); // Inserts a link htmlEditorInstance.format("link", { href: "https://www.google.com/", text: "Google", title: "Go to Google" });
Angular
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; makeTextBold() { this.htmlEditor.instance.format("bold", true); } insertGoogleLink(){ this.htmlEditor.instance.format("link", { href: "https://www.google.com/", text: "Google", title: "Go to Google" }); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
See Also
formatLine(index, length, formatName, formatValue)
Applies a single block format to all lines in the given range.
A zero-based index at which to begin formatting.
The length of the content to be formatted.
Embedded items have a length of 1.
A format name.
A format value.
jQuery
// Aligns the first line to the right $("#htmlEditorContainer").dxHtmlEditor("instance").formatLine(0, 1, "align", "right");
Angular
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; alignFirstLineToRight() { this.htmlEditor.instance.formatLine(0, 1, "align", "right"); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
See Also
formatLine(index, length, formats)
Applies several block formats to all lines in the given range.
A zero-based index at which to begin formatting.
The length of the content to be formatted.
Embedded items have a length of 1.
Formats to be applied.
This object should have the following structure:
{ "formatName1": "formatValue1", ... }
jQuery
// Aligns the first line to the right and turns it into an ordered list's item. $("#htmlEditorContainer").dxHtmlEditor("instance").formatLine(0, 2, { "align": "right", "list": "ordered" });
Angular
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; applyLineFormats() { // Aligns the first line to the right and turns it into an ordered list's item. this.htmlEditor.instance.formatLine(0, 1, { "align": "right", "list": "ordered" }); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
See Also
formatText(index, length, formatName, formatValue)
Applies a single text format to all characters in the given range.
A zero-based index at which to begin formatting.
The length of the content to be formatted.
Embedded items have a length of 1.
A format name.
A format value.
jQuery
// Makes the first five characters bold $("#htmlEditorContainer").dxHtmlEditor("instance").formatText(0, 5, "bold", true);
Angular
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; makeTextBold() { // Makes the first five characters bold this.htmlEditor.instance.formatText(0, 5, "bold", true); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
See Also
formatText(index, length, formats)
Applies several text formats to all characters in the given range.
A zero-based index at which to begin formatting.
The length of the content to be formatted.
Embedded items have a length of 1.
Formats to be applied.
This object should have the following structure:
{ "formatName1": "formatValue1", ... }
jQuery
// Makes the first five characters bold and underlined $("#htmlEditorContainer").dxHtmlEditor("instance").formatText(0, 5, { "bold": "true", "underline": "true" });
Angular
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; applyLineFormats() { // Makes the first five characters bold and underlined this.htmlEditor.instance.formatText(0, 5, { "bold": "true", "underline": "true" }); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
See Also
getFormat(index, length)
Gets formats applied to the content in the specified range.
A zero-based index indicating the range's start.
The range's length.
Embedded items have a length of 1.
The applied formats.
This object has the following structure:
{ "formatName1": "formatValue1", ... }
getInstance(element)
Gets the instance of a widget found using its DOM node.
The widget's instance.
getInstance is a static method that the widget class supports. The following code demonstrates how to get the HtmlEditor instance found in an element with the myHtmlEditor
ID:
// Modular approach import HtmlEditor from "devextreme/ui/html_editor"; ... let element = document.getElementById("myHtmlEditor"); let instance = HtmlEditor.getInstance(element) as HtmlEditor; // Non-modular approach let element = document.getElementById("myHtmlEditor"); let instance = DevExpress.ui.dxHtmlEditor.getInstance(element);
See Also
getLength()
Gets the entire content's length.
The content's length.
Embedded items have a length of 1.
getModule(modulePath)
Gets a format, module, or Parchment.
A path to a format ("formats/[formatName]"), module ("modules/[moduleName]"), or Parchment ("parchment").
A format, module, or Parchment content.
You can perform the following tasks after getting a format, module, or Parchment:
- Modify the format
You can change the markup tag associated with the format and allowed format values, as shown in the code example after this list. - Extend the format or module
You can extend HtmlEditor's formats and modules and also Quill's formats and modules. See the Extend Built-In Formats and Modules topic for the code example. - Create a custom format based on the Parchment
Refer to the Parchment documentation for more information.
In the following code, the allowed font sizes are limited in the size format and the bold format is associated with the <b>
tag instead of the default <strong>
tag. After the modifications are made, the formats are registered.
jQuery
$(function() { // ... var htmlEditor = $("#htmlEditor").dxHtmlEditor("instance"); var Size = htmlEditor.getModule("formats/size"); Size.whitelist = ["11px", "12px", "14px"]; var Bold = htmlEditor.getModule("formats/bold"); Bold.tagName = "b"; htmlEditor.registerModules({ "formats/size": Size, "formats/bold": Bold }); });
Angular
import { ..., ViewChild, AfterViewInit } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent implements AfterViewInit { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; ngAfterViewInit () { let htmlEditor = this.htmlEditor.instance; let Size = htmlEditor.getModule("formats/size"); Size.whitelist = ["11px", "12px", "14px"]; var Bold = htmlEditor.getModule("formats/bold"); Bold.tagName = "b"; htmlEditor.registerModules({ "formats/size": Size, "formats/bold": Bold }); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
getSelection()
Gets the selected content's position and length.
The selected content's range. Has the following structure:
- index
A zero-based index at which the selection starts. - length
The selected content's length.
Embedded items have a length of 1.
insertEmbed(index, type, config)
Inserts an embedded content at the specified position.
jQuery
// Adds an image at the beginning of the content $("#htmlEditorContainer").dxHtmlEditor("instance").insertEmbed(0, "extendedImage", { src: "https://js.devexpress.com/Content/images/doc/18_2/PhoneJS/person1.png", alt: "Photo", width: "100px" });
Angular
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; insertImageAtTheBeginning() { this.htmlEditor.instance.insertEmbed(0, "extendedImage", { src: "https://js.devexpress.com/Content/images/doc/18_2/PhoneJS/person1.png", alt: "Photo", width: "100px" }); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
insertText(index, text, formats)
Inserts formatted text at the specified position. Used with all formats except embedded.
jQuery
// Inserts bold, green text at the beginning of the content $("#htmlEditorContainer").dxHtmlEditor("instance").insertText(0, "I will be the first", { bold: true, color: "green" });
Angular
import { ..., ViewChild } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; insertTextAtTheBeginning() { // Inserts bold, green text at the beginning of the content this.htmlEditor.instance.insertText(0, "I will be the first", { bold: true, color: "green" }); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
instance()
Gets the widget's instance. Use it to access other methods of the widget.
This widget's instance.
off(eventName)
Detaches all event handlers from a single event.
The event's name.
The object for which this method is called.
off(eventName, eventHandler)
Detaches a particular event handler from a single event.
The object for which this method is called.
on(eventName, eventHandler)
Subscribes to an event.
The object for which this method is called.
on(events)
Subscribes to events.
Events with their handlers: { "eventName1": handler1, "eventName2": handler2, ...}
The object for which this method is called.
option()
Gets all widget options.
The widget's options.
See Also
- Get and Set Options
- Call Methods: jQuery | Angular | AngularJS | Knockout | Vue | React | ASP.NET MVC
option(optionName)
Gets the value of a single option.
The option's name or full path.
This option's value.
See Also
- Get and Set Options
- Call Methods: jQuery | Angular | AngularJS | Knockout | Vue | React | ASP.NET MVC
option(optionName, optionValue)
Updates the value of a single option.
See Also
- Get and Set Options
- Call Methods: jQuery | Angular | AngularJS | Knockout | Vue | React | ASP.NET MVC
option(options)
Updates the values of several options.
Options with their new values.
See Also
- Get and Set Options
- Call Methods: jQuery | Angular | AngularJS | Knockout | Vue | React | ASP.NET MVC
redo()
Reapplies the most recent undone change. Repeated calls reapply preceding undone changes.
See Also
registerKeyHandler(key, handler)
Registers a handler to be executed when a user presses a specific key.
A key.
A handler. Accepts the keydown event as the argument. It is a dxEvent or a jQuery.Event when you use jQuery.
The key argument accepts one of the following values:
- "backspace"
- "tab"
- "enter"
- "escape"
- "pageUp"
- "pageDown"
- "end"
- "home"
- "leftArrow"
- "upArrow"
- "rightArrow"
- "downArrow"
- "del"
- "space"
- "F"
- "A"
- "asterisk"
- "minus"
A custom handler for a key cancels the default handler for this key.
See Also
registerModules(modules)
Registers formats and modules.
Formats and modules to be registered.
This object should have the following structure:
{ "path1": "object1", ... }
where path1
is formats/[formatName] for formats or modules/[moduleName] for modules.
jQuery
$(function() { // ... var htmlEditor = $("#htmlEditor").dxHtmlEditor("instance"); htmlEditor.registerModules({ "modules/myModule": moduleObject }); });
Angular
import { ..., ViewChild, AfterViewInit } from "@angular/core"; import { DxHtmlEditorModule, DxHtmlEditorComponent } from "devextreme-angular"; // ... export class AppComponent implements AfterViewInit { @ViewChild(DxHtmlEditorComponent) htmlEditor: DxHtmlEditorComponent; ngAfterViewInit () { this.htmlEditor.instance.registerModules({ "modules/myModule": moduleObject }); } } @NgModule({ imports: [ // ... DxHtmlEditorModule ], // ... })
See Also
removeFormat(index, length)
Removes all formatting and embedded content from the specified range.
A zero-based index at which to begin removing.
The length of the content to be removed.
Embedded items have a length of 1.
setSelection(index, length)
Selects and highlights content in the specified range.
A zero-based index at which to begin selecting.
The length of the content to be selected.
Embedded items have a length of 1.
If you have technical questions, please create a support ticket in the DevExpress Support Center.