Rich Text Editors

Rich Text Editors

The editor is TinyMCE

Version 6.3.1 of TinyMCE (MIT licence), loaded only by a page that has an editor.

<div class="field">
<label for="demo-editor-description">Description</label>
<textarea class="editor" id="demo-editor-description" name="description">
<h2>Magic Sheets</h2>
<p>Woven from <strong>long-staple cotton</strong> for a cool, crisp feel. See the <a href="#">care guide</a> before the first wash.</p>
<ul>
<li>300 thread count</li>
<li>Fits mattresses up to 40cm deep</li>
</ul>
</textarea>
</div>

A page loads TinyMCE itself, before the Tsugi script

TinyMCE is not in the bundle. A page with an editor loads dist/js/tinymce/tinymce.min.js before the Tsugi script.

<script src="js/tinymce/tinymce.min.js" defer></script>
<script src="tsugi.min.js" defer></script>

The markup is a textarea with the class editor

The textarea's text is the starting HTML. Its placeholder shows while the editor is empty.

<textarea class="editor" name="description" placeholder="Describe the product"></textarea>

The textarea's placeholder shows in an empty editor

<div class="field">
<label for="demo-editor-placeholder">Excerpt</label>
<textarea class="editor" id="demo-editor-placeholder" name="excerpt" placeholder="A sentence or two for the product listing"></textarea>
</div>

A form sent by the browser carries the HTML in the textarea

The editor writes its HTML to the textarea when its form is submitted. A script that handles the submit itself calls editor.save() first.

A script that reads the form itself calls save() first

Nothing sent yet.
<form class="flex flex-col gap-3" id="demo-editor-form" onsubmit="event.preventDefault(); document.getElementById('demo-editor-form-field').editor.save(); document.getElementById('demo-editor-form-result').textContent = new FormData(this).get('note')">
<div class="field">
<label for="demo-editor-form-field">Note</label>
<textarea class="editor" id="demo-editor-form-field" name="note"><p>Ships in <strong>two parcels</strong>.</p></textarea>
</div>
<button type="submit" class="btn self-start">Save</button>
<pre class="bg-muted overflow-x-auto rounded-lg p-4 font-mono text-xs" id="demo-editor-form-result">Nothing sent yet.</pre>
</form>

A script reads and sets the HTML through the textarea's editor property

Nothing read yet.
<div class="flex flex-col gap-3">
<textarea class="editor" id="demo-editor-read" aria-label="Care instructions"><p>Wash at <em>30 degrees</em>.</p></textarea>
<div class="flex flex-wrap gap-2">
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-editor-read-result').textContent = document.getElementById('demo-editor-read').editor.getContent()">Read the HTML</button>
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-editor-read').editor.setContent('<p>Do not tumble dry.</p>')">Set the HTML</button>
</div>
<pre class="bg-muted overflow-x-auto rounded-lg p-4 font-mono text-xs" id="demo-editor-read-result">Nothing read yet.</pre>
</div>

Other TinyMCE options go through mount(), on a textarea without the class

<div class="grid gap-6 md:grid-cols-2">
<div class="field">
<label for="demo-editor-compact">Short note, with a small toolbar</label>
<textarea id="demo-editor-compact" name="short_note"><p>Gift wrapping on request.</p></textarea>
</div>
<div class="field">
<label for="demo-editor-readonly">Terms, read only</label>
<textarea id="demo-editor-readonly" name="terms"><p>Returns are accepted within <strong>30 days</strong>.</p></textarea>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
window.tsugi.mount("editor", document.getElementById("demo-editor-compact"), {
toolbar: "bold italic underline | link",
min_height: 120,
max_height: 240,
});
window.tsugi.mount("editor", document.getElementById("demo-editor-readonly"), { readonly: true });
});
</script>

Settings the editor starts with

Setting Value
Toolbar Block style, bold, italic, underline, bullet list, numbered list, link, image, media, four alignments, outdent, indent, clear formatting, source code, fullscreen.
Plugins image link fullscreen media autoresize lists code
Height From 200px to 800px, growing with the text.
Menu bar, status bar, branding Off.
Text being edited Loads the page's Tsugi stylesheet and takes the typography class, in the page's theme.

Only the seven plugins above ship in dist/js/tinymce/. A toolbar button from another plugin needs that plugin's file added there.

API Reference

Attributes

Attribute On Values Default Description
placeholder textarea.editor text none Shown in an empty editor.
name textarea.editor text none Sends the HTML with the form.

Methods and properties

Member Type Description
editor property, read only The TinyMCE editor, once it has started; null before that. It has TinyMCE's own methods.
editor.getContent() method The HTML as it stands.
editor.setContent(html) method Replaces the HTML.
editor.save() method Writes the HTML to the textarea. The editor does this itself when its form is submitted, after the page's own submit listeners.

Uploading an image from inside the editor is not set up. An app passes TinyMCE's images_upload_handler option through mount().

Once the script has started it, the element carries data-editor-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.