Rich Text Editor
- Usage
- Styling
- Value Format
- Read-Only
- Automatic Height Adjustment
- Theme Variants
- Toolbar Actions
- Related Components
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
RichTextEditor rte = new RichTextEditor();
rte.setMaxHeight("400px");
String valueAsHtml = DataService.getTemplates().getRichTextHtml();
rte.setValue(valueAsHtml);
add(rte);
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; and
-
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 a Base64-encoded data URL |
|
Note
| Only Base64-encoded data URLs are supported for images. Web URLs are not supported. |
The following snippet contains an HTML document that is supported by the component. It 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
RichTextEditor rte = new RichTextEditor();
rte.getStyle().set("max-height", "400px");
rte.setValueChangeMode(ValueChangeMode.TIMEOUT);
// HTML value
TextArea htmlTextArea = new TextArea("HTML Value");
htmlTextArea.setHelperText(
"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.");
htmlTextArea.setWidthFull();
rte.addValueChangeListener(e -> htmlTextArea.setValue(e.getValue()));
htmlTextArea.addValueChangeListener(e -> {
if (!rte.getValue().equals(e.getValue())) {
rte.setValue(e.getValue());
}
});
// Delta value
TextArea deltaTextArea = new TextArea("Delta Value");
deltaTextArea.setHelperText(
"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.");
deltaTextArea.setWidthFull();
rte.asDelta().addValueChangeListener(
e -> deltaTextArea.setValue(e.getValue()));
deltaTextArea.addValueChangeListener(e -> {
if (!rte.asDelta().getValue().equals(e.getValue())) {
rte.asDelta().setValue(e.getValue());
}
});
add(rte, htmlTextArea, deltaTextArea);
Read-Only
Setting the component to read-only hides the toolbar and makes the content non-editable.
new tab
RichTextEditor rte = new RichTextEditor();
rte.setMaxHeight("400px");
String valueAsHtml = DataService.getTemplates().getRichTextHtml();
rte.setValue(valueAsHtml);
rte.setReadOnly(true);
add(rte);
Automatic Height Adjustment
Unless set to a fixed height, Rich Text Area adjusts its height automatically based on its content.
Minimum & Maximum Height
The automatic resizing can be restricted to a minimum and maximum height.
new tab
RichTextEditor rte = new RichTextEditor();
rte.setMaxHeight("400px");
rte.setMinHeight("200px");
String valueAsHtml = DataService.getTemplates().getRichTextHtml();
rte.setValue(valueAsHtml);
add(rte);
Theme Variants
Compact
Apply the compact
theme to make the toolbar more compact.
new tab
RichTextEditor rte = new RichTextEditor();
rte.getStyle().set("max-height", "400px");
String valueAsHtml = DataService.getTemplates().getRichTextHtml();
rte.setValue(valueAsHtml);
rte.addThemeVariants(RichTextEditorVariant.LUMO_COMPACT);
add(rte);
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
RichTextEditor rte = new RichTextEditor();
rte.getStyle().set("max-height", "400px");
String valueAsHtml = DataService.getTemplates().getRichTextHtml();
rte.setValue(valueAsHtml);
rte.addThemeVariants(RichTextEditorVariant.LUMO_NO_BORDER);
add(rte);
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. |
Color
Button | Title | Description |
---|---|---|
Text color | Changes text color. | |
Text background | Changes text background. |
It’s possible to provide custom palette for color pickers used by the Rich Text Editor. The palette must be a list of strings in the hexadecimal color format.
RichTextEditor editor = new RichTextEditor();
editor.setColorOptions(List.of("#000000", "#0066cc", "#ffffff"));
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 & 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. |