Rich Text Editor
- Usage
- Styling
Note
|
Commercial feature
A commercial Vaadin subscription is required to use Rich Text Editor in your project. |
Rich Text Editor allows the user to author text that has rich formatting. It allows you to format and style your text using boldface, italics, headings, lists, images, links, etc.
new tab
<vaadin-rich-text-editor
style="max-height: 400px"
value="${this.richText}"
></vaadin-rich-text-editor>
Value Format
Rich Text Editor supports the HTML format and the Quill Delta format for reading and setting its value.
HTML format details
Rich Text Editor supports values in the HTML format, with the following restrictions:
-
Only a subset of HTML tags are supported, which are listed in the table below.
-
Block elements, such as paragraphs, lists, or block quotes, can’t be nested.
-
Unsupported tags, such as
<b>
, are replaced with an equivalent supported tag, such as<strong>
, or with a paragraph (<p>
).
Feature | Tags |
---|---|
Paragraphs and line breaks |
|
Headings |
|
Bold, italic, underlined and strike-through text |
|
Links |
|
Text alignment via the |
|
Ordered, unordered lists, and list items |
|
Block quotes |
|
Pre-formatted text |
|
Images, using either a web URL or a Base64-encoded data URL |
|
The following snippet contains an HTML document that is supported by the component, and demonstrates the usage of several tags. Try pasting the snippet into the HTML Value
text area in the example below and see how the editor updates. Then try modifying the value, either by using the editor’s features, or by changing the HTML value directly.
<h2>High quality rich text editor for the web</h2>
<p>Rich text editor handles the following formatting:</p>
<ul>
<li><strong>Bold</strong></li>
<li><em>Italic</em></li>
<li><u>Underline</u></li>
<li><s>Strike-through</s></li>
</ul><h3>Blockquotes</h3>
<blockquote>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.
</blockquote><h3>Code blocks</h3>
<pre spellcheck='false'><body>
<vaadin-rich-text-editor></vaadin-rich-text-editor>
</body>
</pre>
Delta format details
The JSON-based Delta format consists of an array of operations to apply to a document. Rich Text Editor specifically only uses insert operations, each operation sequentially adding content to the document. Operations can have attributes, such as whether to render a piece of content with a specific text style, or as a link. For the full specification of the format, see the Quill Delta GitHub repository.
The following snippet contains a Delta document that demonstrates some of the format’s features.
Try pasting the snippet into the Delta Value
text area in the example below and see how the editor updates.
Then try modifying the value, either by using the editor’s features, or by changing the Delta value directly.
[
{"insert": "High quality rich text editor for the web\n", "attributes": {"header": 2}},
{"insert": "Rich text editor handles the following formatting:\n"},
{"insert": "Bold\n","attributes": { "bold": true, "list": "bullet" }},
{"insert": "Italic\n", "attributes": { "italic": true, "list": "bullet" }},
{"insert": "Underline\n", "attributes": { "underline": true, "list": "bullet" }},
{"insert": "Strike-through\n", "attributes": { "strike": true, "list": "bullet" }},
{"insert": "Blockquotes\n", "attributes": { "header": 3 }},
{"insert": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n", "attributes": { "blockquote": true }},
{"insert": "Code blocks\n", "attributes": { "header": 3 }},
{"insert": "<vaadin-rich-text-editor></vaadin-rich-text-editor>\n", "attributes": { "code-block": true }}
]
For the Flow component, the default is the HTML format, which is also used automatically when binding the component with Binder
.
To read, write, or bind the component’s value using the Delta format, use the RichTextEditor.asDelta()
wrapper.
For the web component, to read or write the value in the Delta format, use the value
property.
To read or write the value in the HTML format, use the htmlValue
property and the dangerouslySetHtmlValue
method.
Note
|
HTML sanitization
To prevent injecting malicious content, be sure to sanitize HTML strings before passing them to the web component using dangerouslySetHtmlValue . An example of this would be using a library such as dompurify.
|
new tab
<vaadin-rich-text-editor
style="height: 400px;"
.value="${this.deltaValue}"
@value-changed="${(event: RichTextEditorValueChangedEvent) => {
this.deltaValue = event.detail.value;
}}"
@html-value-changed="${(event: RichTextEditorHtmlValueChangedEvent) => {
this.htmlValue = event.detail.value;
}}"
></vaadin-rich-text-editor>
<vaadin-text-area
label="HTML Value"
helper-text="Shows the HTML representation of the edited document. You can also modify or paste HTML here to see the changes reflected in the editor above. Note that you have to leave (blur) this field in order for the editor to update."
style="width: 100%;"
.value="${this.htmlValue}"
@change="${(e: TextAreaChangeEvent) => this.setHtmlValue(e.target.value)}"
></vaadin-text-area>
<vaadin-text-area
label="Delta Value"
helper-text="Shows the Delta representation of the edited document. You can also modify or paste the Delta JSON here to see the changes reflected in the editor above. Note that you have to leave (blur) this field in order for the editor to update."
style="width: 100%;"
.value="${this.deltaValue}"
@change="${(e: TextAreaChangeEvent) => {
this.deltaValue = e.target.value;
}}"
></vaadin-text-area>
...
setHtmlValue(htmlValue: string) {
this.htmlValue = htmlValue;
this.richTextEditor.dangerouslySetHtmlValue(htmlValue);
}
Read-Only
Setting the component to read-only hides the toolbar and makes the content non-editable.
new tab
<vaadin-rich-text-editor
style="height: 400px;"
readonly
.value="${this.richText}"
></vaadin-rich-text-editor>
Automatic Height Adjustment
Unless set to a fixed height, Rich Text Area adjusts its height automatically based on its content.
Minimum and Maximum Height
The automatic resizing can be restricted to a minimum and maximum height.
new tab
<vaadin-rich-text-editor
style="min-height:200px; max-height: 400px;"
.value="${this.richText}"
></vaadin-rich-text-editor>
Theme Variants
Compact
Apply the compact
theme to make the toolbar more compact.
new tab
<vaadin-rich-text-editor
style="height: 400px;"
theme="compact"
.value="${this.richText}"
></vaadin-rich-text-editor>
No Border
Apply the no-border
theme variant to remove Rich Text Editor’s border. An example of this is when the component is wrapped in a container with its own borders.
new tab
<vaadin-rich-text-editor
style="height: 400px;"
theme="no-border"
.value="${this.richText}"
></vaadin-rich-text-editor>
Toolbar Actions
History
Button | Title | Description |
---|---|---|
Undo | Reverses the previous action. | |
Redo | Restores actions undone by |
Emphasis
Button | Title | Description |
---|---|---|
Bold | Boldens text. | |
Italic | Italicizes text. | |
Underline | Underlines text. | |
Strikethrough | Strikes through text. |
Headings
Three different headings are available in Rich Text Editor: H1, H2, and H3. Use them to signify structure, as well as importance.
Button | Title | Description |
---|---|---|
H1 | Heading level 1. | |
H2 | Heading level 2. | |
H3 | Heading level 3. |
Subscript and Superscript
Button | Title | Description |
---|---|---|
Subscript | Subscript text is positioned below the normal baseline and with a smaller font size. | |
Superscript | Superscript text is positioned above the normal baseline and a with smaller font size. |
List
Button | Title | Description |
---|---|---|
Ordered list |
| |
Unordered list |
|
Alignment
Button | Title | Description |
---|---|---|
Left align | Left-aligns text (default). | |
Center align | Center-aligns text. | |
Right align | Right-aligns text. |
Related Components
Component | Usage recommendations |
---|---|
Basic single-line text input. | |
Basic multi-line text input. |
DC8286C2-D152-4234-831F-F90A00B97305