stilo

Embedding the editor

A step-by-step tutorial, from an empty folder to an app that loads, edits and saves Texl documents, shows comments, and has a cell type of its own. For every detail, see the API reference; for how the parts work inside, see the architecture.

  1. Install
  2. Configure Vite
  3. Show a document
  4. Load and save
  5. Imports and the document URL
  6. Comments and cell names
  7. Write your own cell type
  8. Style the editor
  9. Work with documents without the editor
  10. Use it from a framework

What you will build

The editor is one web component, <texl-editor>. You give it the text of a Texl document; it shows the document as a stack of cells on lined paper, lets the user edit them, renders the Texl live, and gives the text back. Your app decides where documents come from and where they go.

A document is plain text. Each cell starts with a marker line:

<!--{ bek4 : markdown }-->
# Beam check
<% L = 6, q = 12.5 %>
<!--{ mom3 : markdown }-->
Midspan moment: <% q * L ^ 2 / 8 %> kNm
<!--[ Anna: check the load combination. ]-->

<% ... %> tags are formulas, written in the Swan language; a name bound in one cell (L, q) is visible in every cell below it. <!--[ ... ]--> is a comment on the cell. The Texl primer explains the language.

1. Install

The editor is part of Stilo’s web app, published on npm as @onlabsorg/stilo-web; the editor is its entry point @onlabsorg/stilo-web/editor. Install it, together with Vite as dev server and bundler:

mkdir my-app && cd my-app
npm init -y
npm pkg set type=module
npm install @onlabsorg/stilo-web
npm install --save-dev vite

You need Node.js 22 or later. Any bundler that resolves bare module imports, serves .wasm files and gives a stylesheet as text for an import ending in .css?inline works (the Markdown cell needs KaTeX’s styles in its shadow root); this guide uses Vite.

The package is the web app’s source, as plain ES modules. Its interface is its entry points, @onlabsorg/stilo-web/editor here; other files inside it may change without notice. Before 1.0, any version may change the interface too (see the changelog).

2. Configure Vite

One setting. Texl runs as WebAssembly: its JavaScript glue loads the .wasm file by a URL relative to itself. Vite’s dependency pre-bundling would move the glue away from the file, so exclude Texl from it. Create vite.config.js:

import { defineConfig } from "vite";

export default defineConfig({
    optimizeDeps: {
        exclude: ["@onlabsorg/texl"],
    },
});

Without this, rendering fails with an error about loading the wasm module. Pages must be served over HTTP (not opened as file://).

3. Show a document

Create index.html:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>My Texl app</title>
        <style>
            body {
                display: flex;
                flex-direction: column;
                height: 100vh;
                max-width: 52rem;
                margin: 0 auto;
                font-family: system-ui, sans-serif;
            }
            texl-editor {
                flex: 1;
                min-height: 0;
            }
        </style>
    </head>
    <body>
        <texl-editor id="editor"></texl-editor>
        <script type="module" src="./main.js"></script>
    </body>
</html>

and main.js:

import "@onlabsorg/stilo-web/editor";

const editor = document.getElementById("editor");

editor.value = `<!--{ markdown }-->
# Beam check
<% L = 6, q = 12.5 %>
<!--{ markdown }-->
Midspan moment: **<% q * L ^ 2 / 8 %> kNm**
`;

editor.addEventListener("change", () => console.log(editor.value));
editor.focus();

Run npx vite and open the address it prints. You see two cells, rendered as Markdown, and empty numbered lines below them. Try it:

Things to notice:

4. Load and save

The editor has no storage of its own: your app loads the text, sets value, and saves the text when change fires. Here is a version that loads a document from a URL and saves it back with a delay, so that a burst of changes makes one request:

import "@onlabsorg/stilo-web/editor";

const editor = document.getElementById("editor");
const url = "/api/documents/beam.texl";

async function load() {
    const response = await fetch(url);
    editor.value = response.ok ? await response.text() : "";
    await editor.rendered;              // the cells show their output now
    editor.focus();
}

let timer;
editor.addEventListener("change", () => {
    clearTimeout(timer);
    timer = setTimeout(() => {
        fetch(url, { method: "PUT", body: editor.value });
    }, 1000);
});

load();

Without a server, localStorage works the same way:

editor.value = localStorage.getItem("doc") ?? "";
editor.addEventListener("change", () => localStorage.setItem("doc", editor.value));

Points to keep in mind:

5. Imports and the document URL

A document can import other documents by URL with texl.import, e.g. a library of steel sections:

<!--{ markdown }-->
<% texl.import("references/steel.texl") ~> (lib -> bind "steel" lib.data) %>
Section <% steel.IPE.IPE300.name %>

The editor renders the document as the document at its URL, set with the url attribute; relative imports resolve against the document’s folder. A relative url resolves against the page. The default, untitled.texl, puts an unsaved document in the page’s folder.

<texl-editor id="editor" url="https://example.com/docs/report.texl"></texl-editor>

or from JavaScript, at any time (the editor renders again):

editor.url = new URL("/api/documents/report.texl", location.href).href;
editor.value = text;

Nothing is cached between renderings: every rendering fetches the imported documents again, so a changed library shows on the next one (the browser’s HTTP cache still applies). Imports are loaded by the browser, so the usual CORS rules apply. Links in the rendered output, e.g. [steel](steel.texl) in Markdown, are resolved by the browser against the page, not url: a page whose address differs from its document’s must handle that itself.

6. Comments and cell names

Comments live in the document, as <!--[ ... ]--> inside a cell, so they move with the cell. The editor counts them in the right margin; showing them is up to your app. When the user clicks a comment count, or presses Shift+F2 on a line, the editor fires comments-click with the line number and the comments as strings.

A comments panel beside the editor:

<aside id="comments" hidden>
    <h2></h2>
    <ul></ul>
</aside>
const panel = document.getElementById("comments");
let shownLine = 0;

editor.addEventListener("comments-click", (e) => {
    const { line, comments } = e.detail;
    shownLine = line;
    panel.querySelector("h2").textContent = `Comments on line ${line}`;
    panel.querySelector("ul").replaceChildren(
        ...comments.map((text) => Object.assign(document.createElement("li"), { textContent: text })),
    );
    panel.hidden = false;
});

// Hide the panel when the user moves to another line
editor.addEventListener("selection-change", (e) => {
    if (e.detail.line !== shownLine) panel.hidden = true;
});

To the editor a comment is just a string. Give it a structure of your own if you need one, e.g. <!--[ Anna 2026-10-21: check the load ]-->, and parse it in your handler. Users add comments while editing a cell with Shift+F2.

Cell names must be unique in a document. The editor guarantees that for the names it gives, but names edited by hand (in the source file) can clash. The editor leaves them as they are and tells you:

editor.addEventListener("name-conflict", (e) => {
    for (const { name, lines } of e.detail.conflicts)
        console.warn(`Cell name "${name}" is used on lines ${lines.join(", ")}`);
});

7. Write your own cell type

A cell type turns a widget’s input into Texl source, and reads it back. The source is all that is stored. Here is a quantity cell: the user fills in a name, a value and a unit; the cell writes one line of Texl that binds the name, so the cells below can use it, and shows L = 6 m.

The Texl it produces:

<!--{ ruv4 : quantity }-->
<% L = 6 %>L = <% L %> m

Create quantity-cell.js:

import { html, css } from "lit";
import { TexlCell } from "@onlabsorg/stilo-web/editor";

// `<% L = 6 %>L = <% L %> m`: the name, the value and the unit
const QUANTITY = /^<% (\w+) = (.*?) %>\1 = <% \1 %> ?(.*?)\r?\n?$/;

export class QuantityCell extends TexlCell {
    // The type in the marker, its name in the type menu, and its icon
    static type = "quantity";
    static label = "Quantity";
    static icon = html`<svg viewBox="0 0 24 24">
        <path d="M3 8h18v8H3z" />
        <path d="M7 8v3M11 8v4M15 8v3M19 8v4" />
    </svg>`;

    static styles = [
        TexlCell.styles,
        css`
            .fields {
                display: flex;
                gap: 0.75rem;
            }
            input {
                font: inherit;
            }
        `,
    ];

    /** Reads the fields back from the source; a new cell gets defaults. */
    fromTexl(texl) {
        const [, name = "x", value = "0", unit = ""] = QUANTITY.exec(texl) ?? [];
        this.input = { name, value, unit };
    }

    /** Writes the fields as Texl: bind the name, then show it. */
    toTexl() {
        const { name, value, unit } = this.input;
        return `<% ${name} = ${value} %>${name} = <% ${name} %> ${unit}`.trimEnd() + "\n";
    }

    renderEditor() {
        const field = (key, label, size) => html`<label>
            ${label}
            <input
                size=${size}
                .value=${this.input[key]}
                @input=${(e) => (this.input = { ...this.input, [key]: e.target.value })}
            />
        </label>`;
        return html`<div class="fields">
            ${field("name", "Name", 6)} ${field("value", "Value", 10)} ${field("unit", "Unit", 6)}
        </div>`;
    }
}

Register it in main.js, before setting value:

import { registerCell } from "@onlabsorg/stilo-web/editor";
import { QuantityCell } from "./quantity-cell.js";

registerCell(QuantityCell);         // defines <texl-cell-quantity>

editor.value = `<!--{ quantity }-->
<% L = 6 %>L = <% L %> m
<!--{ quantity }-->
<% q = 12.5 %>q = <% q %> kN/m
<!--{ markdown }-->
Midspan moment: **<% q * L ^ 2 / 8 %> kNm**
`;

“Quantity” now appears in the type menu. Edit line 1, change the value to 8 and press Ctrl+Enter: line 3 is rendered again with the new span.

How it works:

More rules for cells:

8. Style the editor

The editor and cells use shadow DOM, so page CSS does not reach inside. Style them with custom properties, set on the editor or an ancestor, and with the parts left-margin and right-margin:

texl-editor {
    --texl-paper: #fffef8;          /* sheet background */
    --texl-line: #ece8dc;           /* rules between lines */
    --texl-margin: 4rem;            /* width of each margin */
    --texl-margin-line: #e3a6a6;    /* the vertical margin lines */
    --texl-accent: #0f766e;         /* edit bar and menu highlights */
    box-shadow: 0 4px 16px rgb(0 0 0 / 0.08);
}

texl-editor::part(right-margin) {
    background: #faf8f0;
}

The API reference lists all of them.

9. Work with documents without the editor

The functions the editor uses on the file format are exported too. Use them to list comments, render a read-only preview or check a document, without showing an editor:

import { parseDocument, serializeDocument, cellComments, splitRendering, createEngine } from "@onlabsorg/stilo-web/editor";

const cells = parseDocument(text);

// Every comment, with its line
const comments = cells.flatMap((cell, i) =>
    cellComments(cell.texl).map((comment) => ({ line: i + 1, comment })),
);

// The rendered output of every cell, as the editor renders it
const engine = await createEngine();
const doc = await engine.render("docs/report.texl", serializeDocument(cells));   // the document's URL
const outputs = splitRendering(doc, cells);

serializeDocument(cells) turns cells back into text, so you can also change a document programmatically, e.g. append a cell:

import { parseDocument, serializeDocument, nameCells } from "@onlabsorg/stilo-web/editor";

const cells = parseDocument(text);
cells.push({ type: "markdown", texl: "Checked by Anna.\n" });
text = serializeDocument(nameCells(cells));

10. Use it from a framework

<texl-editor> is a standard custom element, so it works in any framework. Two rules: set value as a property, and listen to change as a DOM event. Set value only when a different document is opened, not after each change.

React 19:

import { useEffect, useRef } from "react";
import "@onlabsorg/stilo-web/editor";

export function TexlEditor({ documentText, onChange }) {
    const ref = useRef(null);

    // Open the document when it changes (not on every edit)
    useEffect(() => {
        ref.current.value = documentText;
    }, [documentText]);

    useEffect(() => {
        const editor = ref.current;
        const handler = () => onChange(editor.value);
        editor.addEventListener("change", handler);
        return () => editor.removeEventListener("change", handler);
    }, [onChange]);

    return <texl-editor ref={ref} style= />;
}

Lit:

html`<texl-editor
    .value=${this.documentText}
    url=${this.documentUrl}
    @change=${(e) => this.save(e.target.value)}
></texl-editor>`;

With Lit, keep documentText unchanged while the user edits (store the saved text elsewhere): the .value binding sets the property again whenever the bound value changes.

Next steps