Rich-text elements
Brightspot’s rich-text editor accommodates custom rich-text elements, such as embedded videos, image galleries, or pull quotes. For example, the following illustration is an example of a rich-text element rendered as a pull quote. The element contains typeface, alignment, and a left border.

Editing lifecycle of rich-text elements
Subclasses of RichTextElement can implement the callbacks required for editing rich-text elements.
-
Inside the rich-text editor, clicking Edit fires two callbacks:
RichTextElement#fromBodyto populate the editing widget’s Link Text field and the Main tab of the Link field.RichTextElement#fromAttributesto populate the editing widget’s Attributes field in the Advanced tab.
-
Inside the editing widget, clicking Save & Close fires two callbacks to create a traditional HTML link of the form
<a href="value" otherattribute="othervalue">link text</a>:RichTextElement#toBodyto retrieve the link text from the editing widget’s Link Text field.RichTextElement#toAttributesto retrieve the link’s attributes from the editing widget’s Attributes field in the Advanced tab.
The following snippet describes the interaction between the rich-text element and the editing widget.
1import com.psddev.cms.db.RichTextElement;2import com.psddev.dari.util.CompactMap;3import com.psddev.dari.util.StringUtils;45public class AnotherLinkRichTextElement extends RichTextElement {67@Required8private Link link;910private String linkText;1112public String getLinkText() {13return linkText;14}1516public void setLinkText(String linkText) {17this.linkText = linkText;18}1920@Override21public void fromBody(String body) {22setLinkText(StringUtils.unescapeHtml(body));23}2425@Override26public String toBody() {27return getLinkText();28}2930@Override31public void fromAttributes(Map<String, String> attributes) {32List<Attribute> createdAttributes = attributes.keySet()33.stream()34.map(key -> {35Attribute attribute = new Attribute();36attribute.setName(key);37attribute.setValue(attributes.get(key));3839return attribute;4041})42.collect(Collectors.toList());4344link.setAttributes(createdAttributes);45}4647@Override48public Map<String, String> toAttributes() {49Map<String, String> htmlAttributes = new CompactMap<>();50link.getAttributes()51.forEach(attribute -> htmlAttributes.put(attribute.getName(), attribute.getValue()));52return htmlAttributes;53}54}
- 8. Declares a
Linkfield that contains the attributes in an HTML<a>element. - 10. Declares a field, getter, and setter for the link text (the text inside an HTML
<a>element. - 21. Populates the field Link Text in the editing widget, unescaping any escaped HTML entities that may have been received from the rich-text element.
- 26. Retrieves the value in the field Link Text, and uses it as the link text when constructing an HTML
<a>element. - 31. Reads the attribute map passed from the rich-text element and populates the Attributes list in the editing widget's Advanced tab.
- 48. Reads the Attributes list in the editing widget's Advanced tab, and uses it to construct the attributes in an HTML
<a>element.
Deploying a rich-text element
The following steps outline how to design rich-text elements and attach them to the rich-text editor’s toolbar. Although the steps are specific to pull quotes, you can apply them to any custom rich-text element.
Step 1: Extend a rich-text element
All custom rich-text elements extend from RichTextElement.
1import com.psddev.cms.db.RichTextElement;2import com.psddev.dari.db.Recordable;3import com.psddev.dari.util.StringUtils;45@Recordable.DisplayName("Pull Quote") 16@RichTextElement.Tag( 27value = "quote",8preview = true,9block = false,10root = true,11menu = "Enhancement")12public class PullQuoteRichTextElement extends RichTextElement {1314@Required15private String quote;1617public String getQuote() {18return quote;19}2021public void setQuote(String quote) {22this.quote = quote;23}2425@Override 326public Map<String, String> toAttributes() {27return null;28}2930@Override31public void fromAttributes(Map<String, String> attributes) {32}3334@Override35public void fromBody(String body) {36setQuote(StringUtils.unescapeHtml(body));37}3839@Override40public String toBody() {41return getQuote();42}4344}
-
5. Specifies the rich-text element displays as a pull quote in the rich-text toolbar.
-
6. Specifies the presentation aspects of the rich-text element:
-
The theme's data files use the
valueelement for creating the view. In this snippet, the data files must provision the rich-text element using the identifierquote. -
In the toolbar, the rich-text element appears under the menu item Enhancement.
For information about the @RichTextElement.Tag annotation see @RichTextElement.Tag.
- 25. Overrides the methods
toAttributes,fromAttributes,fromBody, andtoBody. These methods provide the interface between the rich-text element and the associated editing widget. For details, see Editing lifecycle of rich-text elements.
Step 2: Attach rich-text element to toolbar
1import com.psddev.cms.rte.RichTextToolbar;2import com.psddev.cms.rte.RichTextToolbarItem;3import com.psddev.cms.rte.RichTextToolbarStyle;45public class CustomRichTextToolbar implements RichTextToolbar {67@Override8public List<RichTextToolbarItem> getItems() { 19return Arrays.asList(10RichTextToolbarStyle.BOLD,11RichTextToolbarStyle.ITALIC,12RichTextToolbarStyle.UNDERLINE13RichTextToolbarStyle.ELEMENTS);14}1516}
- 8. Attaches all custom rich-text elements to the toolbar.
Step 3: Create theme files
Under your project’s styleguide/ directory, create files for each field appearing in the rich-text element. For example, if a field body is typed as a rich-text field, then the theme’s files must provide the template, data file, and styling for the field body. As a best practice, ensure each file’s path and base name correspond to the rich-text element’s class. The following example provides an illustration for theme files corresponding to the class PullQuoteRichTextElement developed in Step 1.
1./23└── <root>45├── src |67└── main |89└── java |1011└── brightspot |1213└── content |1415└── article |1617└── PullQuoteRichTextElement.java1819└── styleguide2021└── content2223└── article2425├── PullQuoteRichTextElement.hbs2627├── PullQuoteRichTextElement.json2829└── PullQuoteRichTextElement.less
For information about creating theming files, see Theme guide.
See also:
Storage of rich-text elements
Brightspot stores rich-text elements in XML format, similar to the following:
1{2"caption": "Here is a link <a href=\"http://www.brightspot.com/\" target=\"_blank\" cursor=\"wait\" type=\"text/html\" >Brightspot</a>."3}
The element and attribute names you can use in a custom rich-text element are arbitrary, so you can design rich-text elements to fit your publishing needs. You then implement runtime parsers in view models that extract the elements and attributes from the database and populate the view.