Creating a Custom AEM RTE Plugin
Developing a custom Rich Text Editor widget is a frequently encountered task that may be required for a variety of purposes. For example, on one of our projects we needed to create a custom widget in the Rich Text Editor to support managing footnotes. Initially, footnoted content was coming from another source in a special format and we had a front-end only code to parse and show this structure properly.
Then the authors decided to use both content sources: external from the API and the AEM-managed.
What Stands Behind AEM RTE Plugin?
As an RTE field is widely used by the authors, and it provides such flexibility and freedom to create and manage texts, we decided to have a new widget in the RTE field which can produce the same HTML markup. This allowed our existing Front-end library to function the same as before, no matter whether the content source was from the API or AEM-managed components.
In addition to that, an RTE field has options which can be mixed with the new functionality. For example, to make a footnote bold or italic or to insert a link inside.
AEM RTE Plugins: Initial HTML Markup
Initially the API provided the following html markup — the markup the existing FE js library understands:
AEM RTE Plugin That Seals the Deal
The idea was to add a new icon to the RTE field to manage footnotes:
- If we put an RTE cursor to any blank space – a popup appears with no data:
2. If we select any text and click this icon — the same popup appears with this text as a Footnote title:
3. If we click on a previously-added footnote — the popup opens with the filled data (the same image as in case no. 2)
4. In the RTE field itself, a footnote should be clearly visible (it has a custom style)
Custom Client Lib: AEM RTE Magic
This is a custom AEM clientlib to support footnotes
/apps/test-com/clientlibs/clientlib-author/rte/js/footnotebuilder.js:
We’ll start by defining constants and adding this new plugin to the UI settings
The functions to add a new plugin to the toolbar and the dialog template:
Footnote builder plugin itself:
- UI initialization — adding an icon to the RTE toolbar
- On-click widget handling — a custom dialog to enter footnote data
- Calling a command to process HTML transformation
RTE command TouchUIFootnoteBuilderCmd which is being called above: HTML transformation is being performed here:
And finally, an iframe containing the dialog, its styling, configuring its header and buttons
The custom css /apps/test-com/clientlibs/clientlib-author/rte/css/rte.css:
And the popup dialog /apps/test-com/components/dialogs/rte/footnote/cq:dialog.xml:
How to Use AEM RTE Custom Plugin
The usage of the new widget is very simple. You just add it to an RTE definition:
The Exadel Team Creates an AEM RTE Custom Plugin
As a result, we now have a new widget inside the RTE field which gives us the ability to create AEM-managed footnotes without changing front-end library code. The widget, as a part of RTE, is easy to use because it extends the basic RTE and is easy for authors to get used to and start working with it. Inside the RTE text field, the footnotes are clearly visible and will not confuse editors. It’s easy to create a new footnote as well as to edit an existing one.
This is pretty much it. The client lib is ready to be used in any component with an RTE field. Stay tuned for more articles from the Exadel Marketing Technology Team!
Was this article useful for you?
Get in the know with our publications, including the latest expert blogs
End-to-End Digital Transformation
Reach out to our experts to discuss how we can elevate your business