I’m building a Lightning component to edit html and need a robust WYSIWYG rich text editor (RTE) with “add link” feature and embed features. I really need the “add link” feature. When I search for RTE and Lightning I found and considered the following:
Trailblazer gives UI guidelines if you want to layout your own markup for a RTE but why reinvent the wheel when there are good plugins out there.
http://lightningdesignsystem.com/components/rich-text-editor/
The old ui:inputRichText is not supported when LockerService is activated:
So this leaves <lightning:inputRichText />
. But this RTE lacks is deficient in both add link and image features.
<lightning:inputRichText value="{!v.body}" aura:id="iBody">
</lightning:inputRichText>
I’ve seen code that suggests you can add a button to let uses insert an image.
<lightning:inputRichText value="{!v.body}">
<lightning:insertImageButton/>
</lightning:inputRichText>
But this image uploader has a 1MB limit and has a terrible way of dealing with files that are too big. The user is told a serious error happened and they are asked to send a report into SF.
Is there some inner component that can be added to provide a Add Hyperlink feature?
Or, can we get the RTE that is used in Salesforce Lightning? Its the CKE editor and it’d be good enough. Is there a way to use this in our Lightning apps? I’ll guess that it might be imported as a static resource etc. Has anyone tried this and had success?
Best Answer
I implemented a RTE component by using dijit/Editor inside a Visualforce page. I then wrapped that Visualforce page inside a Lightning Component by using an iFrame, so that I can easily use it anywhere I wanted.
Below is the code from my Visualforce page without the Lightning Component <-> VF Page communication part. This should be enough to get you started quickly:
I needed this component to provide the ability to edit
EmailMessages
, so that's why it's usingEmailMessage
standard controller, but you can easily change that to support something else. Also, the reason I used dijit/Editor was because it messed the least with the HTML that was generated by the email templates.It has the functionality to support adding links, but for uploading attachments I think you'll need to implement a separate component that you'll use in conjunction with RTE (which shouldn't be that hard).
P.S. As I said, I used dijit/Editor for my own reasons, however, you can probably use a very similar approach to implement any other RTE library that is out there. For example, Salesforce's new
lightning-input-rich-text
LWC is using Quill.