Code Editors

Code Editors

The code editor is Monaco

Version 0.57.0 of Monaco (MIT licence), loaded only by a page that has a code editor.

<div class="field">
<label for="demo-code-editor-template">Template</label>
<div class="code-editor" data-language="html">
<textarea id="demo-code-editor-template" name="template">&lt;section class="hero"&gt;
&lt;h1&gt;{{ settings.hero_text }}&lt;/h1&gt;
&lt;a class="btn" href="/catalogue/"&gt;Shop All&lt;/a&gt;
&lt;/section&gt;</textarea>
</div>
</div>

A page loads Monaco's loader itself, before the Tsugi script

Monaco is not in the bundle. The loader fetches the rest of Monaco from dist/js/monaco/vs/ when the first editor starts.

<script src="js/monaco/vs/loader.js" defer></script>
<script src="tsugi.min.js" defer></script>

An app serves the whole monaco folder, with its layout kept

The loader finds the other files beside itself, so no path is set. The page loads one tag, and Monaco fetches the editor, its stylesheet, a worker and the language being edited.

dist/js/monaco/
  LICENSE.txt
  ThirdPartyNotices.txt
  vs/
    loader.js                 the one file a page loads
    editor/editor.main.js     the editor's entry, which names the files below
    editor/editor.main.css
    *.js                      the editor and the five languages, each named with a hash
    assets/*.worker-*.js      the workers that check the code
    basic-languages/

The loader's tag goes after every other library's

The loader defines the globals define and require. A library loaded after it can register with the loader and set no global of its own.

<script src="js/chart.umd.min.js" defer></script>
<script src="js/monaco/vs/loader.js" defer></script>
<script src="tsugi.min.js" defer></script>

Monaco's own API is window.monaco, once the first editor has loaded

The element's editor property is the Monaco editor, and is null until then. Everything in Monaco's documentation works on it.

const editor = document.querySelector(".code-editor").editor;
editor.getValue();
editor.getAction("editor.action.formatDocument").run();
window.monaco.editor.setModelLanguage(editor.getModel(), "css");

The markup is a box around a textarea

The textarea's text is the starting code, and a form sends the textarea. Until Monaco has loaded, the textarea is the editor.

<div class="code-editor" data-language="html">
<textarea name="template"></textarea>
</div>

data-language is css, json, scss, javascript or html, and a height utility sizes the box

<div class="grid gap-6 md:grid-cols-2">
<div class="field">
<label for="demo-code-editor-css">Styles</label>
<div class="code-editor h-40" data-language="css">
<textarea id="demo-code-editor-css" name="styles">.hero {
color: #ffffff;
min-height: 60vh;
}</textarea>
</div>
</div>
<div class="field">
<label for="demo-code-editor-json">Settings</label>
<div class="code-editor h-40" data-language="json">
<textarea id="demo-code-editor-json" name="settings">{
"hero_text": "Store Value Proposition",
"hero_height": "medium"
}</textarea>
</div>
</div>
<div class="field">
<label for="demo-code-editor-scss">Sass</label>
<div class="code-editor h-40" data-language="scss">
<textarea id="demo-code-editor-scss" name="sass">$primary: #0a7f6e;
.btn-primary {
background: darken($primary, 5%);
}</textarea>
</div>
</div>
<div class="field">
<label for="demo-code-editor-javascript">Script</label>
<div class="code-editor h-40" data-language="javascript">
<textarea id="demo-code-editor-javascript" name="script">const count = document.querySelector("#cart-count");
count.textContent = String(3);</textarea>
</div>
</div>
</div>

readonly on the textarea makes the editor read only

<div class="field">
<label for="demo-code-editor-readonly">Base layout</label>
<div class="code-editor h-32" data-language="html">
<textarea id="demo-code-editor-readonly" name="layout" readonly>&lt;body&gt;
{% block content %}{% endblock %}
&lt;/body&gt;</textarea>
</div>
</div>

Each edit is written to the textarea, and the form hears input

Nothing typed yet.
<form class="flex flex-col gap-3" id="demo-code-editor-form" oninput="document.getElementById('demo-code-editor-result').textContent = new FormData(this).get('announcement')">
<div class="field">
<label for="demo-code-editor-announcement">Announcement</label>
<div class="code-editor h-32" data-language="html">
<textarea id="demo-code-editor-announcement" name="announcement">&lt;p&gt;Free shipping this week.&lt;/p&gt;</textarea>
</div>
</div>
<pre class="bg-muted overflow-x-auto rounded-lg p-4 font-mono text-xs" id="demo-code-editor-result">Nothing typed yet.</pre>
</form>

A script reads and sets the code through the element's editor property

Nothing read yet.
<div class="flex flex-col gap-3">
<div class="code-editor h-32" id="demo-code-editor-read" data-language="css">
<textarea name="banner_styles" aria-label="Banner styles">.banner { color: #191a1f; }</textarea>
</div>
<div class="flex flex-wrap gap-2">
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-code-editor-read-result').textContent = document.getElementById('demo-code-editor-read').editor.getValue()">Read the code</button>
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-code-editor-read').editor.setValue('.banner { color: #0a7f6e; }')">Set the code</button>
</div>
<pre class="bg-muted overflow-x-auto rounded-lg p-4 font-mono text-xs" id="demo-code-editor-read-result">Nothing read yet.</pre>
</div>

Several textareas in one box are several files, and tabs whose panels are the textareas switch between them

<div class="tabs w-full" id="demo-code-editor-files">
<nav role="tablist" aria-orientation="horizontal" aria-label="Open files">
<button type="button" role="tab" id="demo-code-editor-files-tab-1" aria-controls="demo-code-editor-files-1" aria-selected="true">index.html<i class="icon icon-x" data-close aria-hidden="true"></i></button>
<button type="button" role="tab" id="demo-code-editor-files-tab-2" aria-controls="demo-code-editor-files-2">theme.css<i class="icon icon-x" data-close aria-hidden="true"></i></button>
<button type="button" role="tab" id="demo-code-editor-files-tab-3" aria-controls="demo-code-editor-files-3">settings.json<i class="icon icon-x" data-close aria-hidden="true"></i></button>
</nav>
<div class="code-editor h-48">
<textarea id="demo-code-editor-files-1" name="index_html" aria-label="index.html" data-language="html">&lt;main&gt;
&lt;h1&gt;{{ store.name }}&lt;/h1&gt;
&lt;/main&gt;</textarea>
<textarea id="demo-code-editor-files-2" name="theme_css" aria-label="theme.css" data-language="css" hidden>main {
max-width: 72rem;
}</textarea>
<textarea id="demo-code-editor-files-3" name="settings_json" aria-label="settings.json" data-language="json" hidden>{
"logo_align": "left"
}</textarea>
</div>
</div>

The textarea without hidden is the file being edited

The editor follows when a textarea is hidden, shown, added or removed. A form sends every textarea in the box, so one save can carry every open file.

Other Monaco options go through mount(), on an element without the class

<div class="field">
<label for="demo-code-editor-wrap">Footer note, with long lines wrapped and no line numbers</label>
<div class="h-32 w-full overflow-hidden rounded-md border" id="demo-code-editor-options" data-language="html">
<textarea id="demo-code-editor-wrap" name="footer_note">&lt;p&gt;Orders placed before noon ship the same day, and every order can be returned within thirty days of delivery.&lt;/p&gt;</textarea>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
window.tsugi.mount("code-editor", document.getElementById("demo-code-editor-options"), { wordWrap: "on", lineNumbers: "off" });
});
</script>

Settings the editor starts with

Setting Value
automaticLayout On. The editor follows the size of its box.
fontSize 13.
minimap Off.
scrollBeyondLastLine Off.
Theme Monaco's light or dark theme, with the page's background, text and line colours.

Only the five languages above ship in dist/js/monaco/. Another language needs its files added there.

API Reference

Attributes

Attribute On Values Default Description
data-language .code-editor, or a textarea "html", "css", "scss", "javascript", "json" plain text The language the code is coloured and checked as. On a textarea it is that file's language.
readonly the textarea present or absent absent Makes the editor read only.
aria-label the textarea text the textarea's label Names the editor for a screen reader.
hidden the textarea present or absent absent In a box of several textareas, the textarea without it is the file being edited. The page, or tabs, sets it.
data-loaded .code-editor present or absent set by the script Present once Monaco has loaded and stands in for the textareas.

Methods and properties

Member Type Description
editor property, read only The Monaco editor, for getValue(), setValue() and the rest. It is null until Monaco has loaded.

Events

Event On Detail Description
input the textarea, and it bubbles none Sent after each edit, once the new code is in the textarea. It is the browser's own event.

Keyboard

Key Description
Ctrl F Opens Monaco's find box.
F1 Opens Monaco's command list, which holds every other key.
Ctrl M Switches whether Tab indents or leaves the editor.

A box may hold several textareas, one per file. The editor follows when a textarea is hidden, shown, added or removed, and keeps each file's cursor, scroll and undo history.

window.tsugi.mount('code-editor', element, options) starts an editor by a call, on an element without the class. Its options are Monaco's, laid over the component's own.

Monaco has one theme for every editor on a page. The component sets it from the design system's colours, and again when the theme changes.

Monaco's loader defines the globals define and require. A library that looks for them must load before it.

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