Developer's Field Notes
Rich Text Editors are Impossible to build. I'm trying anyway.
Since I've started building CMS websites for clients more years ago than a care to count, the biggest hurtle has been giving content creators an rich-text editing experience that meets both our needs.
The rich-text experience has gotten better over the past ten years, especially when it comes to copy-pasting content from word processors. But I'm still finding online editors to be the biggest pain point in delivering a great experience for users.
What makes a RTE seemingly impossible to build? Originally, HTML was meant to be a simple markup language, much like how Markdown is promoted today. And in the beginning, it was. Back in the mid-nineties, a complete tutorial was about 10 pages long and could be mastered in a couple of hours. The current HTML Standard is over 1500 pages, and this isn't including CSS or other Web APIs.
An RTE involves hosting an HTML editor inside another HTML document. That requires handling all the complexity of that 1500 page specification along with anything a user might paste into the editor.
These field notes are a record of me trying to build an experimental rich-text editor to scratch my particular itches. New items will be added below as work continues.
Here are a few of my ideas....
Side-by-side source code and rendered views I'm quite tired of jumping back and forth from the source view and rendered HTML text. So often am I switching views while creating particular layouts or verifying custom editor features, I feel that a side-by-side view would be a great feature. Some RTEs don't have a source view at all.
Importing front-end styles into HTML view Content creators deserve a true WYSIWYG experience. To provide this, most RTEs require the developer to provide a second css stylesheet just for the editor that mimics a subset of the front-end site's styles. The issues arise when changes to one stylesheet are not copied over to the other. Most every large project now breaks up it's styles into smaller, easy to manage, documents. So it should not be an issue to place all shared styles into one file, but I'm not seeing it done.
Support for responsive images and <Picture> elements It's 2026, yet editors still only offer users images at a single size.
Let's begin by seeing what the web platform provides us to build a modern RTE. Below are the browser features I'm looking to use.
The contenteditable attribute: adding this attribute to a div instantly turns it intro a simple RTE that can be extended by JS.
Web components with shadow root: the web components API makes it simple to create portable HTML elements that are easily added to any web page. These components also encapsulate their contents from the rest of the page which should allow us to apply custom styles without interference from the outer document.
Web Assembly?: If the editor starts to get bogged down by size, moving from plain JavaScript to Web Assembly might provide a big boost in both loading time and performance.
I'm starting by building a small prototype in Svelte. I call prototype creation as "Making friends with the problem." Just as you might make a friend by learning their likes and dislikes. You learn about a problem, in my case building an editor, by creating a series of simple prototypes and learning what factors get in the way.
Starting a most basic of editors was simple, I just create a <div> tag and set it's
`contenteditable` attribute to `true`. Right away, I can insert into the box and start typing. Bold and
italic text are set using the common keyboard shortcuts. I also was able to create simple side-by-side
views of the rich text and source view by using Svelte's build-in bindings. Text or code inserted into
one side immediately created updates on the other.
Going beyond that is the real work. I started by adding a bar across the top with buttons labeled "h1", "h2", "h3" and wired them into an event handler. When a button is clicked the following should happen:
After a couple of weeks off, I'm back to my exploration of rich-text editors. My first design is just a `contenteditable` field containing rich-text on the left with a `textarea` field showing raw HTML on the right. A button bar sits above it allowing the user to switch between a plain paragraph tag and the top three headers.
Everything went fine until I noticed that selections are undone whenever I clicked a formatting button. The solution is below.
Sample Text
The issues were caused by my ignorance of how the Selection and Range APIs worked. When a formatting button is clicked, I grabbed the current location of the curser and any text that may have been selected. That way, I could find the block of text that needed to change (a paragraph, heading, etc.). The changed occurred, but I would lose my selected text and would have to select the same text again if I wanted to make a different change.
So why was I losing my selection? Turns out the answer was simple. To the browsers eyes, I was deleting the element containing the selected text and inserting a new element in it's place. The selection had nowhere to point to so it was reset to the containing element, in this case the content editable field. Fixing it required me to record the selection's location before replacing the markup and re-selecting it after the update.
Try it with the demo above. Insert a curser or select some text in the left-hand field and click one of the buttons to change it's format. They curser location should remain. It's also fun to see the markup change in the right-hand field as you make edits.
With that fixed, I'm now exploring another little oddity in my RTE. When editing text, the expected behavior when pressing the return key is creating a new paragraph tag below the currently selected element. When I press return while inside a paragraph that indeed happens, but when I hit return at the end of a header tag, it creates a new DIV.
Looks like I'll have to write a keydown handler...
Building EditorOne gave me more insight into what it actually takes to build a working editor. As I reviewed what I wrote, I noticed that the work breaks down into lifecycle of many discrete steps. I also noticed that EditorOne's code was quite disorganized.
Here's my idea for the lifecycle...
$state() object will be fine.Here's the dedicated object for storing selection data.
/**
* Copies current range data into a custom type that's
* returned for storage. Allows app to recreate a
* selection after the document is changed.
* @param range a Range interface
*/
function getRangeData(range:Range): RangeData {
// does range span elements?
let container = range.commonAncestorContainer;
if (range.startContainer === range.endContainer) {
container = range.startContainer
}
// crete fresh RangeData obj
let temp: RangeData = {
startOffset: range.startOffset,
endOffset: range.endOffset,
container,
elementName: undefined,
};
if (container) {
temp.elementName = getParentEl(range)?.nodeName;
}
return temp;
}Here's my second version of the editor. Understanding the lifecycle as described above helped keep the new codebase organized. For example, the toolbar's click handler just identifies what button was clicked and passes the corresponding command to a separate command processor. Selection data is constantly observed and stored in a global variable available to all other functions so they don't need to consult the Selection API themselves.
Try for yourself.
These separation of concerns made adding new features easier. For example, having selection state stored in a global made it easy to highlight toolbar buttons base on the selected text. Insert the cursor into a section of bold text and the bold button highlights, letting the user know what style or markup has been applied.
<button class="{ rangeData.elementName === command ? 'active' : '' }" disabled="{disabled}" data-command="{ command }" >{ command }</button>What was most interesting was how I had to handle making text bold and italic and then back again into plain text. That really showed me the difference between HTML elements and DOM nodes, the subjects of my next post.
John O'Donnell - Developer
I am a full-stack web developer and founder of Critical Mash. I work with marketing teams to create great CMS experiences.