HTML - Rich Text Editor (Edition)

1 - About

A rich text editor is a component that permits to edit text to make it rich. ie it allows the text to be styled via editing button.

3 - Basic Example

This code is explained in the html editor section. If you want to see the code, hover over the form and click on the Try this code button.

textToConsole = (event) => {
  event.preventDefault();
  console.log(event.target.article.value);
};
editAction = (event, cmd, element) => {
        event.preventDefault(); // Avoids loosing focus from the editable area
        document.execCommand(cmd, false, element); // Send the command to the browser
};  


<button onMouseDown="editAction(event,'italic');">Italic</button>
  <button onMouseDown="editAction(event,'bold');">Bold</button>
  <button onMouseDown="editAction(event,'formatBlock','h1');">H1</button>
  <button onMouseDown="editAction(event,'formatBlock','p');">P</button>
<form onSubmit="textToConsole(event);">
 <fieldset>
  <legend>New article</legend>
  <textarea id=textarea name=article></textarea>
  <div id=div style="white-space: pre-wrap;outline-width: 0px;" hidden><p>Update Me, Select a Text and use the Edit Button to style the text and Send to the console to see the result</p></div>
  <script>
   let textarea = document.getElementById("textarea");
   let div = document.getElementById("div");
   textarea.hidden = true;
   div.hidden = false;
   div.contentEditable = "true";
   textarea.value = div.innerHTML;
   div.oninput = (e) => {
     textarea.value = div.innerHTML;
   };
  </script>
 </fieldset>
 <p><button>Send to Console</button>
</form>

Ref: Example adapted from the Editing section of the spec

4 - How it works

4.1 - HTML Editor

The most simple implementation are made via Javascript that manipulates directly the syntax tree of the HTML document called the DOM.

It means that this is HTML that is edited.

The content is made editable with the contentEditable attribute and the styling buttons are implemented with:

  • the execCommand This function will be replaced by Content Editable and Input Events but they are still not fully implemented

The basic example is using the execCommand.

4.2 - Other Language Editor

If you want to create a editor for another language, there is two possible architectures that increase in complexity:

  • an unidirectional editor, the easiest, where you translate your language to HTML
  • or a bidirectional editor, the hardest, where you need also to translate the HTML back to your language.

4.2.1 - Translation

To translate you language to HTML, you can do it:

  • with a function directly (one pass)
  • or via a parser that builds a syntax tree

Example of parser:

4.2.2 - Virtual DOM

The HTML dom may be and is generally virtual. Editor may use:

Some implements also their own such as:

The advantage of a virtual DOM is that the page does not need to be entirely refreshed. The virtual dom is making the diff and its update transparent.

Keep in mind that the DOM implementation chosen will limit the expressiveness of your language.

It's easy to add a inline feature such as bold but it's:

  • less easy to define hierarchical component such as :
    • a table,
    • or a image gallery
  • even less if you want to introduce templating feature such as:
    • the list of the last 10 pages published

4.2.3 - Debounce

To smoothing the experience and to not render on every stroke, you should apply a debounce (overview, lodash, react hook). It reduces overhead by not rendering for every keystroke. The rendering is triggered via a keystroke or a timer.

4.2.4 - Sanitization / Security

To avoid Xss attack, your input should be sanitized to delete every scriptable/callable text also on the server please as the client code is never reliable.

5 - Editors

This sections regroups:

  • the rich text editor
  • but also the code editor because if you are creating a markup language, you may also just want to go this route.

5.1 - Rich Text Editor

5.1.1 - React-contenteditable

react-contenteditable - Example with:

  • two directions update between HTML Text to React HTML Dom
  • with a edit button powered by execCommand to modify the HTML

5.1.2 - Draft

Draft.js - Framework for building rich text editors in React such as:

5.1.3 - QuilJs

https://quilljs.com/

(Used by purpose)

5.1.4 - Prosemirror

https://prosemirror.net/

5.1.5 - Squire

https://github.com/neilj/Squire - Largely used. The HTML remains the source-of-truth (the DOM)

5.1.6 - Syntax tree to HTML

5.1.7 - Medium Editor

Medium Editor - Simple project, buggy in the extension that shows a medium like editor

5.1.8 - Pell

5.2 - Code Editor

If you are creating a markup language, you may also just want to implement the modification in a code editor such as

6 - See also

6.1 - Language Server Protocol

  • Language Server Protocol - Interface protocol used between an editor or IDE and a language server that provides language features like auto complete, go to definition, find all references etc.

7 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap