Skip to content

@astrolabe/editor

The editor-agnostic authoring kit: createAuthoring, the insert menu, inspector, issues and preview elements, and the textarea adapter.

<astl-insert-menu>: what authors can add, in named groups: "Components" (each type with its default template), "Exercises" (the types' named starters, when any) and "Content" ("Picture", which asks for the picture in a dialog). Each button's name is its label and description, as shown; a press inserts at the cursor.

  • AuthoringElement
new AstlInsertMenu(): AstlInsertMenu;

AstlInsertMenu

AuthoringElement.constructor
Property Type
adapter EditorAdapter | null
authoring Authoring | null
insert(
tag,
template?,
picture?
): string | null;

Inserts a new element of tag at the cursor: its default template, or the named starter template; with picture, the element takes it (a type whose zones or pins sit on a picture: its picture field's address, its alt text and size). An element whose companion the document lacks brings it at the end (the word bank of a first gap); the cursor stays on the new element. Returns its source HTML, or null without authoring or adapter.

Parameter Type
tag string
template? string
picture? PictureFields

string | null

insertPicture(): Promise<string | null>;

Asks for a picture in the dialog and inserts it at the cursor. Resolves with its HTML, or null when cancelled.

Promise<string | null>


<astl-inspector>: a form for the component at the cursor (answers, alternatives, correct options, whether each statement is true, the category of each card, scoring, points, variant, look), or, with the cursor outside every component, the document's own settings (its look). Every edit goes through authoring.update and the adapter (replaceElement, or setSource for the document), so the editor stays the source of truth.

  • AuthoringElement
new AstlInspector(): AstlInspector;

AstlInspector

AuthoringElement.constructor
Property Type
adapter EditorAdapter | null
authoring Authoring | null
get model():
| InspectorModel
| null;

The inspector model on screen (a component's, or the document's), or null.

| InspectorModel | null


<astl-issues>: live validation of the whole document in the UI locale, errors first. Selecting an issue moves the editor's cursor to its element (adapter.select).

  • AuthoringElement
new AstlIssues(): AstlIssues;

AstlIssues

AuthoringElement.constructor
Property Type
adapter EditorAdapter | null
authoring Authoring | null
get issues(): readonly Issue[] | null;

The issues on screen (null before the first validation).

readonly Issue[] | null

validate(): void;

Validates now instead of waiting for the debounce.

void


<astl-picture-dialog>: asks for a picture in a modal dialog: a file, uploaded with the host's uploadAsset, or an address (the only way without one, as it says); the alt text, required unless the picture is decorative (alt=""); and, for a new picture, an optional caption. The address must pass the authoring's URL policy, an uploaded one too. ask resolves with the fields, or null when the author cancels.

  • AuthoringElement
new AstlPictureDialog(): AstlPictureDialog;

AstlPictureDialog

AuthoringElement.constructor
Property Type
adapter EditorAdapter | null
authoring Authoring | null
ask(request?): Promise<
| PictureFields
| null>;

Opens the dialog. Resolves with the picture's fields, or null when the author cancels.

Parameter Type
request? PictureRequest

Promise< | PictureFields | null>


<astl-picture-field>: the inspector's picture-regions and picture-points fields. The picture (by address, or uploaded with the host's uploadAsset) with its zones or pins drawn on it in palette colours and numbered; drag on it to draw a zone, click to place a pin; and every value in number fields, so nothing needs a pointer. Changes go to the inspector as astl-field-change events.

  • LitElement
new AstlPictureField(): AstlPictureField;

AstlPictureField

LitElement.constructor
Property Type Description
authoring Authoring | null -
element string | null The source id of the element the field belongs to (the inspector sets it).
field Field | null -

<astl-preview>: a button that compiles the current source with a throw-away salt and plays it in <astl-player> with local grading and the authoring's style templates, the way a student sees it. Invalid sources show their errors instead. Tokens set on the preview reach the player; the player's events stay inside.

  • AuthoringElement
new AstlPreview(): AstlPreview;

AstlPreview

AuthoringElement.constructor
Property Type
adapter EditorAdapter | null
authoring Authoring | null
get player(): AstlPlayer | null;

The player, once the first preview started.

AstlPlayer | null

get state(): PreviewState;

idle, loading, ready, invalid (errors shown) or failed.

PreviewState

refresh(): Promise<void>;

Compiles the current source and plays it. Resolves when the player is ready or the errors are shown.

Promise<void>

Property Modifier Type Description
descriptors readonly readonly EditorDescriptor[] -
elements readonly readonly ElementEntry[] -
engine readonly Engine -
locale readonly string -
styleTemplates readonly readonly StyleTemplate[] -
uploadAsset readonly UploadAsset | null The host's upload, or null: pictures are added by address only.
urlPolicy readonly UrlPolicy -
assignIds(source): string;

source with fresh ids on pages and components that lack one (or have an empty one).

Parameter Type
source string

string

describe(issue): string;

An issue as a sentence in the authoring locale.

Parameter Type
issue Issue

string

descriptor(tag):
| EditorDescriptor
| null;

The descriptor of a component tag, or null.

Parameter Type
tag string

| EditorDescriptor | null

inspect(elementHtml, source?):
| InspectorModel
| null;

The inspector model of one component element (source HTML), or null for elements it does not know. With the whole document (source), fields offer what other elements hold (the words of the bank a drop takes its words from); without it they show the attribute's own control.

Parameter Type
elementHtml string
source? string

| InspectorModel | null

inspectDocument(source):
| InspectorModel
| null;

The document's own settings (its style template) as an inspector model for <astl-doc>, or null when the source has no <astl-doc> root or there is nothing to set. Apply its changes with update(source, change): the first element of the source is the document, so only its start tag changes.

Parameter Type
source string

| InspectorModel | null

instantiate(
tag,
source,
templateId?
): string;

The descriptor's template, or its named starter templateId, ready to insert into source: {{id}} becomes <idPrefix>-<n> with the smallest n whose id source does not use, {{t:key}} an escaped message of the manifest namespace, and other components written without an id get fresh ones too. An element of a type with a companion and an empty attribute names the first such element of the template, else of source, else the one withCompanion adds. Throws for a tag without a descriptor or an unknown starter.

Parameter Type
tag string
source string
templateId? string

string

outline(source): Outline;

Pages, their components and issue counts.

Parameter Type
source string

Outline

preview(source): Promise<PreviewResult>;

Compiles with a random per-session salt and grades locally. The source is not stored anywhere.

Parameter Type
source string

Promise<PreviewResult>

t(
namespace,
key,
params?
): string;

A message in the authoring locale: kit (astl.editor), descriptor, engine and extra catalogs.

Parameter Type
namespace string
key string
params? Readonly<Record<string, string | number>>

string

templates(tag): readonly EditorTemplate[];

The named starters of a component tag (EditorDescriptor.templates); none without a descriptor.

Parameter Type
tag string

readonly EditorTemplate[]

update(elementHtml, change): string;

The element HTML with one inspector change applied; everything else stays byte-identical.

Parameter Type
elementHtml string
change InspectorChange

string

validate(source): ValidationReport;
Parameter Type
source string

ValidationReport

withCompanion(tag, source): string | null;

source (with an inserted tag element) and the template of the descriptor's companion at the end of the document (in the last page), when it has no such element yet; else null.

Parameter Type
tag string
source string

string | null


Property Modifier Type Description
catalogs? readonly readonly MessageCatalog[] More or overriding catalogs (any namespace), e.g. another UI locale.
descriptors readonly readonly EditorDescriptor[] One per component type authors can insert and inspect. Each tag must be registered in the engine.
elements? readonly readonly ElementEntry[] Player elements for <astl-preview>, eager or lazy.
engine readonly Engine -
locale readonly string The author's UI locale: labels, issues, template texts.
styleTemplates? readonly readonly StyleTemplate[] Style templates authors can pick (register the same ones with the engine and the player): the inspector offers them per component and for the whole document, labelled from their catalogs; <astl-preview> renders them.
uploadAsset? readonly UploadAsset Stores a picture file and returns its address (and size): the host's upload, used by the picture dialog, by pictures pasted or dropped into the editor and by picture fields; also by url fields with accept (a video, its captions). The address must pass urlPolicy. Without it, pictures and files are added by address only. signal aborts when the author cancels or picks another file. The file's type is the browser's guess: check type and size on the server, and serve SVG with nosniff.
urlPolicy? readonly UrlPolicy The engine's URL policy (createEngine({ urlPolicy })): the preview's player cleans with it and editors check picture addresses with it before they show them. Default isSafeUrl.

  • FieldTexts
Property Modifier Type Description
checked readonly boolean -
control readonly "checkbox" -
help readonly string | null Translated help text, or null.
kind readonly "attribute" -
label readonly string Translated label.
name readonly string -

  • FieldTexts
Property Modifier Type Description
attribute readonly string -
children readonly readonly ChildChoiceModel[] -
control readonly "select" | "radios" Radios for the enum values of the attribute, a select for the ids of other children (options).
help readonly string | null Translated help text, or null.
kind readonly "child-choice" -
label readonly string Translated label.
tag readonly string -

Property Modifier Type Description
id readonly string | null -
index readonly number Position among the children with this tag, from 0 (what child-choice changes use).
label readonly string The child's text, or a placeholder when it is empty.
options readonly readonly InspectorOption[] The choices of this child. Enum source: the attribute's values, the same for every child (an absent or unknown value matches none). Child source: each option child's id with its text, plus the "not set" option when the value is absent and the written value itself when it names no option (so the control shows what the source says).
value readonly string The written value; '' when absent.

  • FieldTexts
Property Modifier Type Description
attribute readonly string -
children readonly readonly ChildFlagModel[] -
help readonly string | null Translated help text, or null.
kind readonly "child-flag" -
label readonly string Translated label.
tag readonly string -

Property Modifier Type Description
checked readonly boolean -
id readonly string | null -
index readonly number Position among the children with this tag, from 0 (what child-flag changes use).
label readonly string The child's text, or a placeholder when it is empty.

  • FieldTexts
Property Modifier Type Description
attribute readonly string -
children readonly readonly ChildTextModel[] -
help readonly string | null Translated help text, or null.
kind readonly "child-text" -
label readonly string Translated label.
tag readonly string -

Property Modifier Type Description
id readonly string | null -
index readonly number Position among the children with this tag, from 0 (what child-attributes changes use).
label readonly string The child's text, or a placeholder when it is empty.
value readonly string The written value; '' when absent.

A change of the field, and the source id of the element it is for (an upload may end after the cursor moved).

Property Modifier Type
change readonly InspectorChange
element readonly string | null

Property Modifier Type Description
fields readonly readonly InspectorFieldModel[] -
icon readonly string | null Icon name from the descriptor.
id readonly string | null -
label readonly string Translated menu label of the type (the tag when there is no descriptor).
tag readonly string -

Property Modifier Type
label readonly string
value readonly string

  • FieldTexts
Property Modifier Type Description
control readonly "number" -
help readonly string | null Translated help text, or null.
kind readonly "attribute" -
label readonly string Translated label.
max readonly number | null -
min readonly number | null -
name readonly string -
required readonly boolean -
step readonly "any" | "1" 1 for integers.
value readonly string The written value, else the manifest default; '' when neither.

Property Modifier Type Description
errors readonly number Every issue of the document.
pages readonly readonly OutlinePage[] -
warnings readonly number -

Property Modifier Type Description
errors readonly number Issues located on this component or its children.
id readonly string | null -
label readonly string Translated type label (the tag when there is no descriptor).
tag readonly string -
warnings readonly number -

Property Modifier Type Description
components readonly readonly OutlineComponent[] -
errors readonly number Issues located on the page, its content and its components.
id readonly string | null null for the implicit page of a document without pages.
label readonly string | null -
warnings readonly number -

A picture as the picture dialog asks for it: alt is '' for a decorative picture.

Property Modifier Type Description
alt readonly string -
caption? readonly string A caption makes the picture a figure.
height? readonly number -
src readonly string -
width? readonly number -

One pin of a picture-points field, in percent of the picture.

Property Modifier Type
id readonly string
index readonly number
label readonly string
x readonly number
y readonly number

  • PictureFieldBase
Property Modifier Type Description
elementTag readonly string The tag of the element with the picture: the URL policy is asked for its srcAttribute, as the engine does.
help readonly string | null Translated help text, or null.
kind readonly "picture-points" -
label readonly string Translated label.
points readonly readonly PicturePointModel[] -
src readonly string -
srcAttribute readonly string The element's attribute with the picture's address, and its value ('' when absent).
tag readonly string -

One zone of a picture-regions field, in percent of the picture (absent or invalid numbers read as 0).

Property Modifier Type Description
flagged readonly boolean Whether the zone has the field's flag (a right place): drawn solid, the others dashed.
height readonly number -
id readonly string -
index readonly number Position among the children with the field's tag, from 0 (what child-* changes use).
label readonly string -
points readonly readonly (readonly [number, number])[] -
r readonly number -
shape readonly PictureShape -
width readonly number -
x readonly number -
y readonly number -

  • PictureFieldBase
Property Modifier Type Description
elementTag readonly string The tag of the element with the picture: the URL policy is asked for its srcAttribute, as the engine does.
flag readonly string | null The boolean attribute of a child-flag field on the same child tag (a hotspot's right places), whose zones are drawn solid and the others dashed; null without one (every zone solid).
help readonly string | null Translated help text, or null.
kind readonly "picture-regions" -
label readonly string Translated label.
regions readonly readonly PictureRegionModel[] -
shapes readonly readonly PictureShape[] The shapes a zone may take: the values of the child's shape enum, or rectangles only when it has no shape (then no shape is written).
src readonly string -
srcAttribute readonly string The element's attribute with the picture's address, and its value ('' when absent).
tag readonly string -

What ask opens the dialog with.

Property Modifier Type Description
altRequired? readonly boolean The picture carries the task (a component's): its alt text is required, with no decorative choice.
caption? readonly boolean Whether to ask for a caption (a new picture). Default: true for a new picture, false for an edit.
current? readonly PictureFields A picture to edit: its address, alt text and size. Absent: a new picture.
decorative? readonly boolean Whether current is decorative; default: when its alt text is empty.
file? readonly File A file to upload first (pasted or dropped), through the authoring's uploadAsset.
target? readonly object The element and attribute the address is for, as the URL policy is asked; default img and src.
target.attribute readonly string -
target.tag readonly string -

A page or a component found by scanElements. Offsets index into the scanned string.

Property Modifier Type Description
end readonly number -
id readonly string | null The id attribute (entities decoded), or null when absent.
start readonly number -
tag readonly string -

  • FieldTexts
Property Modifier Type Description
control readonly "select" -
help readonly string | null Translated help text, or null.
kind readonly "attribute" -
label readonly string Translated label.
name readonly string -
options readonly readonly InspectorOption[] -
value readonly string The written value, else the default; '' (the "not set" option) when neither.

What a WYSIWYG integration must provide so the authoring kit can drive it. Everything is in source HTML (the <astl-doc> format), so an adapter is free to use any internal model.

destroy(): void;

Removes the textarea listeners and every subscription.

void

getElement(id): string | null;

Source HTML of one component element, or null when the id is not in the document.

Parameter Type
id string

string | null

EditorAdapter.getElement

getSelection():
| EditorSelection
| null;

The component at the cursor, or null.

| EditorSelection | null

EditorAdapter.getSelection

getSource(): string;

The whole document as source HTML.

string

EditorAdapter.getSource

insert(html): void;

Inserts a source snippet (an instantiated template) at the cursor.

Parameter Type
html string

void

EditorAdapter.insert

onChange(listener): () => void;

Subscribe to document changes. Returns an unsubscribe function.

Parameter Type
listener () => void

() => void

EditorAdapter.onChange

onSelectionChange(listener): () => void;

Subscribe to selection changes. Returns an unsubscribe function.

Parameter Type
listener (selection) => void

() => void

EditorAdapter.onSelectionChange

replaceElement(id, html): void;

Replaces one component element in place (inspector edits); the rest of the document is untouched.

Parameter Type
id string
html string

void

EditorAdapter.replaceElement

select(id): void;

Moves the cursor to an element by source id (a component or a page) and focuses the editor.

Parameter Type
id string

void

EditorAdapter.select

setSource(source): void;

Replaces the whole document.

Parameter Type
source string

void

EditorAdapter.setSource

optional wrap(tag): boolean;

Runs the wrap command of the type tag (EditorDescriptor.wrap) on the selection and focuses the editor; returns whether the document changed. Absent: the editor offers no such command.

Parameter Type
tag string

boolean

EditorAdapter.wrap


  • FieldTexts
Property Modifier Type Description
accept readonly string | null A url attribute's files to upload (the field's accept), or null: the address is typed.
control readonly "text" -
help readonly string | null Translated help text, or null.
kind readonly "attribute" -
label readonly string Translated label.
name readonly string -
required readonly boolean -
value readonly string The written value; '' when absent.

  • FieldTexts
Property Modifier Type Description
help readonly string | null Translated help text, or null.
kind readonly "text-list" -
label readonly string Translated label.
max readonly number | null null = unbounded.
multiline readonly boolean Each value in a text area that keeps its line breaks.
tag readonly string -
values readonly readonly string[] -

A stored picture, as uploadAsset returns it: its address, and its size in pixels when known.

Property Modifier Type
height? readonly number
src readonly string
width? readonly number
type InspectorChange =
| {
kind: "attribute";
name: string;
value: string | boolean | null;
}
| {
kind: "text-list";
tag: string;
values: readonly string[];
}
| {
attribute: string;
index: number;
kind: "child-flag";
tag: string;
value: boolean;
}
| {
attribute: string;
index: number;
kind: "child-choice";
tag: string;
value: string | null;
}
| {
attributes: Readonly<Record<string, string | null>>;
index: number;
kind: "child-attributes";
tag: string;
}
| {
attributes: Readonly<Record<string, string>>;
kind: "child-add";
tag: string;
}
| {
index: number;
kind: "child-remove";
tag: string;
};

What an inspector control changes. Indexes count children with the same tag, from 0.

{
kind: "attribute";
name: string;
value: string | boolean | null;
}
Name Type Description
kind "attribute" -
name string -
value string | boolean | null A string sets the value, true writes a bare boolean attribute, false or null removes it.

{
kind: "text-list";
tag: string;
values: readonly string[];
}

{
attribute: string;
index: number;
kind: "child-flag";
tag: string;
value: boolean;
}

{
attribute: string;
index: number;
kind: "child-choice";
tag: string;
value: string | null;
}
Name Type Description
attribute string -
index number -
kind "child-choice" -
tag string -
value string | null The value to write; null removes the attribute.

{
attributes: Readonly<Record<string, string | null>>;
index: number;
kind: "child-attributes";
tag: string;
}
Name Type Description
attributes Readonly<Record<string, string | null>> -
index number -
kind "child-attributes" Several attributes of one child (a zone's shape and coordinates): a string sets one, null removes it.
tag string -

{
attributes: Readonly<Record<string, string>>;
kind: "child-add";
tag: string;
}

A new empty child with these attributes, after the last child of its tag (or at the end).


{
index: number;
kind: "child-remove";
tag: string;
}

Removes one child, its content included.


type InspectorFieldModel =
| TextFieldModel
| NumberFieldModel
| CheckboxFieldModel
| SelectFieldModel
| TextListFieldModel
| ChildFlagFieldModel
| ChildTextFieldModel
| ChildChoiceFieldModel
| ChildTableFieldModel
| PictureRegionsFieldModel
| PicturePointsFieldModel;

type PictureShape = "rect" | "circle" | "polygon";

type PreviewResult =
| {
checker: LocalChecker;
issues: readonly Issue[];
ok: true;
view: string;
}
| {
issues: readonly Issue[];
ok: false;
};

A playable preview, or the issues that block compiling.

{
checker: LocalChecker;
issues: readonly Issue[];
ok: true;
view: string;
}
Name Type Description
checker LocalChecker Local grading for this preview (practice policy).
issues readonly Issue[] Warnings.
ok true -
view string -

{
issues: readonly Issue[];
ok: false;
}

type UploadAsset = (file, options) => Promise<UploadedAsset>;
Parameter Type
file File
options { signal: AbortSignal; }
options.signal AbortSignal

Promise<UploadedAsset>

const EDITOR_NAMESPACE: "astl.editor" = 'astl.editor';

i18n namespace of the authoring kit's own UI (insert menu, inspector, issues, preview).


const editorMessages: readonly MessageCatalog[];

const FIELD_CHANGE: "astl-field-change" = 'astl-field-change';

Fired with a FieldChange for the inspector to apply.

function createAuthoring(options): Authoring;
Parameter Type
options AuthoringOptions

Authoring


function createTextareaAdapter(textarea): TextareaAdapter;
Parameter Type
textarea HTMLTextAreaElement

TextareaAdapter


function defineAuthoringElements(registry?): void;

Defines <astl-insert-menu>, <astl-inspector>, <astl-issues> and <astl-preview>, and the two they use for pictures, <astl-picture-dialog> and <astl-picture-field> (nothing is defined on import). Safe to call again; a tag already defined by another class throws, as two copies of the kit would otherwise mix silently.

Parameter Type Default value
registry? CustomElementRegistry customElements

void


function pictureHtml(picture): string;

The source HTML of a picture: an <img> that sits in text, or with a caption a <figure> that stands on its own. Every value is read once and escaped, and only the attributes of the content format are written.

Parameter Type
picture PictureFields

string


function pictureSize(value): number | undefined;

A picture's width or height as the content format takes it: a positive whole number of pixels (a safe integer), from a number or its written form; anything else is no size.

Parameter Type
value unknown

number | undefined


function scanElements(source): ScannedElement[];

Pages (astl-page) and components in document order, with offsets into source, for text-based adapters. An component is any custom element outside another component (so a misspelt tag is found too); custom elements inside a component are its children. Tolerant of messy input: comments, quoted > in attributes, any case, unclosed or self-closed elements.

Parameter Type
source string

ScannedElement[]