Authoring with any WYSIWYG editor
The Astrolabe-specific parts of an editor are an authoring kit: a toolbar with an insert menu, an inspector for
answers and settings, a live issues list and a preview that grades. It works with the editor you already use (TipTap,
ProseMirror, CKEditor, Lexical, Quill, a plain textarea, your own) through one small interface, EditorAdapter.
<astl-editor>is the kit around a surface, ready to use: TipTap by default, or your editor in itsslot="surface"with your adapter inadapter, and then TipTap is never loaded.@astl/editorhas the kit's pieces, for a layout of your own:createAuthoringand the elements. It contains no WYSIWYG engine; only@astl/editor-tiptapimports TipTap.
The adapter contract
Section titled “The adapter contract”Everything crosses the boundary as source HTML in the content format, so your editor may keep any internal model.
interface EditorAdapter { getSource(): string; // the whole <astl-doc> setSource(source: string): void; insert(html: string): void; // an instantiated template, at the cursor getElement(id: string): string | null; // one component element as source HTML replaceElement(id: string, html: string): void; // inspector edits, in place getSelection(): { id: string; tag: string } | null; // the component at the cursor select(id: string): void; // jump to a component or page (from the issues list) onChange(listener: () => void): () => void; onSelectionChange(listener: (s: { id: string; tag: string } | null) => void): () => void; // Optional: commands an editor may offer. wrap?(tag: string): boolean; // a type's command on the selection ("Mark word") splitPage?(): string | null; // a page break at the cursor: the new page's id addPage?(after?: string): string; // an empty page after the page `after` (else the last): its id mergePage?(id: string): boolean; // joins a page to the page before it removePage?(id: string): boolean; // deletes a page and everything on it, not the only one setPageCheck?(check: boolean, page?: string): boolean; // a page's own Check on or off, or every page's format?(format: EditorFormat, href?: string): boolean; // bold, italic, underline, link, text styles, lists formatState?(): EditorFormatState; // { active, enabled, href, undo, redo } at the cursor undo?(): boolean; redo?(): boolean; moveBlock?(block: EditorBlockPlace, direction: 'up' | 'down'): boolean; // { page, index }, within its page or column removeBlock?(block: EditorBlockPlace): boolean; getCursor?(): EditorBlockPlace | null; // the block with the cursor: the page being edited onCursorChange?(listener: () => void): () => void; selectedText?(): string; // plain text selected in one block, for {{selection}} focus?(): void; // back to the editor, the cursor where it was}The comments of EditorAdapter in @astl/core give the rules of each method, such as where a page breaks, which
page keeps its title when two join, and what a block is. Each capability turns on its controls and nothing breaks
without it: an adapter without splitPage gets no "Page break", one without format no format buttons in the
toolbar, one without undo no undo and redo buttons. Format, history and block changes are one undo step each.
Quick start with the built-in textarea adapter
Section titled “Quick start with the built-in textarea adapter”<astl-editor> with a textarea in its surface slot, as in examples/editor-textarea:
<astl-editor> <label slot="surface" for="source">Source (HTML)</label> <textarea slot="surface" id="source" rows="20"></textarea></astl-editor>import { createTextareaAdapter, defineEditor } from '@astl/astrolabe/editor';
defineEditor(); // every built-in; nothing is defined on importconst editor = document.querySelector('astl-editor')!;editor.adapter = createTextareaAdapter(editor.querySelector('textarea')!);Your own editor plugs in the same way: its element in slot="surface", your adapter in adapter. await editor.ready (or the astl-editor-ready event) tells when editor.adapter and editor.value are usable.
The pieces on their own
Section titled “The pieces on their own”For a layout of your own, place the kit elements yourself and give each the same authoring and adapter. With
the @astl/* packages, every list is explicit:
<astl-toolbar></astl-toolbar><textarea id="source" rows="20" cols="80"></textarea><astl-insert-menu></astl-insert-menu><astl-inspector></astl-inspector><astl-issues></astl-issues><astl-preview></astl-preview>import { createEngine } from '@astl/engine';import { createAuthoring, createTextareaAdapter, defineAuthoringElements } from '@astl/editor';import { builtInDefinitions } from '@astl/components/definitions';import { builtInEditors } from '@astl/components/editors';import { builtInElements } from '@astl/components/elements';
const engine = createEngine({ components: builtInDefinitions, secret: 'editor-preview-only-key' });const authoring = createAuthoring({ engine, descriptors: builtInEditors, elements: builtInElements, // for the preview locale: 'en', // uploadAsset: (file) => upload(file), // optional: { src, width?, height? } of a stored picture // pickPicture: ({ signal }) => openLibrary(signal), // optional: a picture from your library, or null});
defineAuthoringElements(); // nothing is defined on importconst adapter = createTextareaAdapter(document.querySelector<HTMLTextAreaElement>('#source')!);for (const el of document.querySelectorAll('astl-toolbar, astl-insert-menu, astl-inspector, astl-issues, astl-preview')) { Object.assign(el, { authoring, adapter });}document.querySelector('astl-toolbar')!.featured = ['astl-gap', 'astl-choice']; // buttons before "Elements"The pieces
Section titled “The pieces”| Piece | What it does |
|---|---|
createAuthoring(options) |
Pure helpers (no DOM): instantiate(tag, source, templateId?, selection?) (the template, or a named starter, with a fresh id: <idPrefix>-<n>, the smallest n the source does not use, and {{selection}} the selected text), templates(tag) (named starters), inspect(html, source?) (inspector model; with the document, a drop lists the words of its bank), update(html, change), validate(source), describe(issue, source?) (with the document, a component named by its block's number on its page, or in text by its number among its type, never by its id; with { page: false } without its page), assignIds(source), outline(source) (pages and their blocks), preview(source), t(namespace, key) |
<astl-toolbar> |
A word processor's toolbar: text style, marks, link, lists (each when the adapter can), the featured inserts as buttons, "Elements" and "Templates" (the insert menu in a searchable popover, components and content or the starters), the types' commands, the views you give it (views, view: two or more make a radio group that fires astl-toolbar-view) and undo and redo. One tab stop with the arrow keys; a press never moves the cursor |
<astl-inspector> actions |
With an adapter that has getCursor and moveBlock or removeBlock, the inspector's head offers "Move up", "Move down" and "Delete" for the block component with the cursor |
<astl-insert-menu> |
Named groups: "Components" (one button per type: label, icon name, description from its descriptor), "Exercises" (named starters) and "Content" ("Picture", through <astl-picture-dialog>: upload with uploadAsset, your library with pickPicture or an address, alt text, caption), the ones in groups; a press inserts at the cursor. The descriptions are menuMessages of createAuthoring, loaded when the menu first opens |
<astl-inspector> |
Form for the component at the cursor: answers, alternatives, which options are correct, scoring, points, zones and pins on a picture. Fields come from the EditorDescriptor (or the manifest), controls from the attribute types |
<astl-issues> |
Live validation in the UI language (300 ms after the last change), errors first; selecting an issue calls adapter.select(id) |
<astl-preview> |
Compiles with the engine you gave createAuthoring plus a random per-session salt (preview ids never match published views) and plays the result in <astl-player> with local grading; an invalid source shows its errors instead |
createTextareaAdapter(textarea) |
Reference adapter: edits the source as text, keeps your formatting, finds elements by id |
scanElements(source) |
Pages and components with their offsets in the text, for text-based adapters; tolerant of comments, quoting, case and unclosed tags |
Use as many pieces as you like; each element only needs its authoring and adapter properties. Pictures pasted
or dropped into your editor are your editor's to handle: hand the file to <astl-picture-dialog>'s ask({ file }),
which uploads it with uploadAsset and asks for the alt text, then insert pictureHtml(fields) with
adapter.insert. The elements use native controls, the --astl-* tokens (override them on the elements) and
::part(root), ::part(title).
Inspector models and changes
Section titled “Inspector models and changes”inspect(html, source?) returns { tag, id, label, icon, fields }. Each field is plain data: an attribute control
(text, number with min, max and step, checkbox, select with translated options from
editor.value.<attribute>.<value>, or with the document a select of what another element holds, e.g. the words of a
drop's bank), a text-list (accepted alternatives), child-flags (which options are correct, each labelled by its
text), child-texts (a free-text attribute per child, e.g. the word each clue hides), child-choices (one value of
an enum attribute per child, e.g. whether each statement is true or false; with options, the id of another child,
e.g. the group of each card) or a child-table (a row of attribute controls per child, with add and remove, e.g. a
crossword's words).
update(html, change) applies one change and leaves everything else byte-identical:
type InspectorChange = | { kind: 'attribute'; name: string; value: string | boolean | null } // true = bare attribute, false/null = remove | { kind: 'text-list'; tag: string; values: readonly string[] } | { kind: 'child-flag'; tag: string; attribute: string; index: number; value: boolean } | { kind: 'child-choice'; tag: string; attribute: string; index: number; value: string | null } // null = remove | { kind: 'child-attributes'; tag: string; index: number; attributes: Record<string, string | null> }; // child-textThe textarea adapter
Section titled “The textarea adapter”insert(html) replaces the selection, except where that would break the source: inside a tag it inserts after the
tag, inside or on a component after that component (components do not nest), outside <astl-doc> at the end of
the last page. The inserted element is selected, so the inspector shows it. replaceElement keeps the caret inside
the element it replaces. splitPage(), addPage(after?) and mergePage(id) write only page tags (an empty
paragraph too, where a page would be left without a line, and in the middle of a paragraph its tags closed before the
break and opened again after it), so the rest of the source stays as written. Change events fire on input and after
the adapter's own edits; selection events on select, keyup, click and focus, only when the component at the
caret changes.
Writing an adapter for your editor
Section titled “Writing an adapter for your editor”- Map component elements to your editor's model. Each manifest tells you how:
display: 'inline'(a gap) is an inline atom;display: 'block'(a closed question) is a block whosecontent: 'inline'children (prompt, options) are editable rich text; privatecontent: 'text'children (accepted alternatives) and private attributes are data the inspector edits. - Serialise your document to the content format in
getSource()and parse it back insetSource(). - Implement
getElement/replaceElementfor one component by id (the inspector uses them). A cursor inside the element stays inside it throughreplaceElement: the inspector writes while the author types in the card (a hint, a table row) and follows the element at the cursor. - Fire
onChangeafter every document change andonSelectionChangewhen the cursor enters or leaves a component. - Optionally, the capabilities:
formatandformatStatefor the toolbar's formats,undoandredo,moveBlock,removeBlockandgetCursorover the page's blocks (asoutline(source)lists them),onCursorChangeso the toolbar and a host follow the cursor, andstylefor the style controls and layouts. An editor that edits the source as text (a code editor) setseditsSource: true:<astl-editor>then offers no "HTML" view of its own.
The TipTap integration is the package @astl/editor-tiptap: <astl-editor> uses it, and its /tiptap entry
has the extensions and the adapter for a TipTap editor of your own (how it maps the manifests).
The textarea version is examples/editor-textarea, live on the authoring kit example
page. Every option, method and element: Editor and the
authoring kit reference.
Secrets
Section titled “Secrets”The authoring kit runs the engine in the author's browser (live validation, preview). <astl-editor> gives its
engine a random secret per page. With the pieces on their own, give that engine a throw-away secret such as
'editor-preview-only-key', never your production secret. Publishing compiles on your server with the real secret;
preview views and keys never need to match published ones.
Publishing
Section titled “Publishing”The kit never publishes. When the author saves, take the source (editor.value, or adapter.getSource()), validate
it (editor.authoring.validate, engine.validate, or compile on your server) and store it. A source with zero errors
compiles; warnings are advice.