Skip to content

Element editors ​

An element can supply its own editor for parameters that generated controls handle poorly, such as marking days in a calendar or panning a map:

tsx
const calendar = { month, firstWeekday, marked };
function CalendarEditor({ values, set, readOnly }: ElementEditorProps<typeof calendar, 'month' | 'marked'>) {
  // Render any UI; call set({ ... }) to apply a change. The book preview updates while the editor stays open.
}

elements: { grid: {
  label: 'Calendar grid',
  parameters: calendar,
  editor: { label: 'Edit calendar', parameters: ['month', 'marked'], component: CalendarEditor },
} },
  • Opening it: clicking the element opens a non-modal panel, and so does the element's entry in the sidebar's list. With presentation: 'inline' it opens on the page instead.
  • Layout parameters with it: a task can need a layout (spread) parameter too, such as a title shown with a calendar. List those in spread: editor: { parameters: ['month', 'marked'], spread: ['title'], component }. The editor reads all layout parameters as spread, changes the listed ones with setSpread(changes) or <Control parameter="title" scope="spread" />, and applies those changes immediately. They stay layout parameters: saved data, MCP's set_parameter and other layouts that declare them are unchanged; only where they are edited moves. A listed parameter gets no sidebar control and may not appear in the layout's controls; each one belongs to one editor. Document settings stay read-only to element editors; a document editor edits them. Included in the contract-1 baseline.
  • One editor for the element: parameters the editor lists are edited only by it. The element's other parameters appear below it as generated controls (arranged by the element's controls). Both apply valid changes as they happen; the editor's values follow the generated controls too. Clicking × or outside the panel first applies anything still being typed in the editor's frame, including in <Control> fields. If the editor fails while applying, or does not answer within a few seconds, the panel stays open with an error; after a timeout, × closes it directly. Changes already applied remain saved.
  • Typed props: ElementEditorProps<P, K, D, S, SK> types the props from the element's parameters P, the IDs the editor lists K, optionally the template's document parameters D, the layout parameters S, and the layout IDs the editor lists SK. spread, setSpread and Control with scope="spread" use those layout types and owned IDs. values and document then have each parameter's type (an enum's options, a number, a string), set accepts only the editor's own parameters with values of their type, and Control only the editor's own parameters, so set({ zoon: 2 }), set({ size: 'huge' }) for an enum without that option, or <Control parameter="opacity" /> for a parameter the generated controls edit fail type checking. An editor typed for other parameters than its declaration lists fails too. Declare the parameters as a constant to write typeof calendar; an editor written inline in editor.component is not typed from the declaration, so annotate its props the same way. ElementEditorProps without type arguments keeps the untyped props.
  • Sandbox: the component runs in a frame on the render origin with the template's CSS, like the book previews. It receives values (all of the element's parameters, with the draft applied), set(changes), spread and setSpread(changes) (above), document, format, readOnly, commit(), cancel(), Control and importFile. It cannot change project data itself.
  • ImageFlow's controls inside: <Control parameter="track" /> draws the app's own control for one of the editor's parameters, bound to the draft: the same field, slider, choice or file chooser (with import) as the generated controls, with their validation and accessibility, in the editor's appearance. Compose it with your own UI instead of rebuilding file handling or form controls. label overrides the parameter's label. For a choice that is not one parameter, <Choice> draws the app's segmented control.
  • Files: importFile(accept) asks the app to import a file into the project and resolves to its file ID, or undefined when the user cancels; <Control> for a file parameter uses it for Choose file…. Assign the ID with set to save the assignment immediately; Undo can revert the assignment while keeping the imported file in the project. file(id) reads the new file at once.
  • What ImageFlow owns: the non-modal panel, × to close, Escape and ⌘↩, read-only mode, validation against the parameter schema, and saving. Each valid set() or setSpread() change is undoable. Closing leaves applied changes in place. Escape discards only uncommitted typing.
  • Applying and closing: set() applies changes immediately, so set({ ... }); commit() applies those changes and closes; values catches up on the next render. commit() finishes the active field and closes. cancel() closes without applying the active field; it does not revert earlier changes. These callbacks remain available to existing custom components.
  • Changes set() and setSpread() cannot make: a key that is not one of the editor's parameters, or a value the parameter does not allow (an enum value outside its options, a number out of range), is not applied. The other changes in the same call are. Each such problem is reported once, in the frame's console and in the dialog, with the parameters the editor edits or the values the parameter takes; it does not prevent valid edits or closing. <Control> for a parameter the editor does not own fails the editor with a message listing the ones it does.
  • Context: context is the spread the editor was opened from, read-only, with the same values render reads: spread and layout (its ID and layout), params, document, boxes (with each photo's metadata and each Markdown box's flow position and header fields), elements, page, book (page count, anchors, spreads and texts), format and formatId. The editor's own draft is applied to it: an element editor's element values (including the generated controls' draft) and the layout parameters it owns, or a document editor's own document parameters. The top-level format and an element editor's document are the context's, so they follow it too. Write helpers that need the page's place in the book against Pick<RenderContext, 'page' | 'book' | 'document'>, so render and the editor share them instead of saving a second, possibly inconsistent value. Included in the contract-1 baseline.
  • The context follows the book; the editor's values do not. While the editor is open, every newer render of the book refreshes context: moving the spread, another photo, another format, or a changed document setting the editor does not edit. The draft is kept and nothing is reported. A change to the values the editor edits is a conflict, handled as below. Deleting the spread closes the editor.
  • Changes from elsewhere: if what the editor works on changes while it is open (a saved value after Reload or Undo, or a template edit that changes a default or a declaration of the parameters it handles), the panel says so and refuses to save; the user reopens the editor on the current values. During a reload, controls pause visibly and resume when the preview is ready. A rejected or failed save also pauses the editor with a message; reopening starts from confirmed saved values.
  • Failures: if the component throws while rendering, in an event handler or in async work, the panel shows the error and keeps Close available. Earlier applied changes remain saved. The editor counts as loaded once the component has mounted.
  • Network: a template may list HTTPS origins in network, such as network: ['https://tiles.openfreemap.org'] (or https://*.example.com for subdomains; no paths). Importing the template into the library lists these sites, with an option to allow them in every book made from that version; Templates → Manage Templates… shows them and can allow them or stop allowing them later. That library trust belongs to the template's hash: a book whose template still has exactly that hash needs no question. Built-in templates are trusted for the sites they declare, and so are the built-in templates of every earlier release, so a book made from an earlier version of a built-in template needs no question after an update changes it. A book you make with New Book from such a trusted template (or from one without sites) is yours: its template reaches every site it declares without asking, including sites you or your agent add later, and a newly declared site is only named in a notice. A book from elsewhere asks once, the first time it renders or one of its element editors opens: Trust This Book makes it yours from then on; until then the book stays offline, its rendered images that need the sites fail, so the Book check reports them and export stops (each failure names, in details.networkPending, the sites that wait for the user's permission), and the question comes back when an editor opens. Templates → Stop Trusting This Book's Template… withdraws that trust. Once allowed, the book's element editors, previews, thumbnails, Book check and export may fetch, load images and fonts from, and open blob workers for the declared origins; undeclared origins stay blocked. Revoking the trust in Manage Templates renders open books again under the new policy, loads an open element editor again, and stops a running export, which then saves nothing. Printing such a book needs the network. Included in the contract-1 baseline.
  • Scope today: the element's parameters and the layout parameters it lists, shown in a non-modal panel (presentation: 'dialog', the default) or on the page (presentation: 'inline'); a sidebar placement is not available yet. Editors can read files of file parameters with file(), and reach remote resources only through network. The photobook starter's Map layout has a reference editor (below). The file-control smoke test uses a separate route editor fixture (scripts/fixtures/route.tsx) to check choosing a track and framing its viewport together.

The Map layout (sample/starter/template/map.tsx) is the full-capability example (ADR 0006). The map uses OpenFreeMap tiles, declared in network, in book styles drawn for print (in map-styles.ts: quiet palettes without icons, place names in the book's fonts) and OpenFreeMap's own Liberty, Bright and Positron. The default, Book, follows the book's colour theme: its SVG names each colour by its palette role as a CSS variable (maplibre-svg's color option), and the map element sets the variables from the theme's palette, so switching between the light and the dark theme recolours the map without drawing it again; Lines follows the theme too, in the page's own colour variables (ink, muted, rule, accent and paper): contour lines of the terrain, shores, rivers, roads and borders as lines, water as a faint wash, for a map that sits on the page. Its contours come from Mapterhorn's open terrain tiles (also declared in network, credited in the attribution), which the maplibre-contour package turns into contour lines inside MapLibre, so they print as vector paths too; it is the default of the Inset map layout, which insets the map inside a single page's margins and can face a story or any other page design. Paper, Ink, Atlas and Night are fixed palettes. A line layer on areas (a shore, say) leaves out the edges where tiles cut the area, and area fills get a hairline of their own colour so their tile edges never show as seams. Its editor runs MapLibre GL from the saved dependency bundle, shows the same palette with real colours, and is shown on the page: dragging and zooming frame the map in its printed place, with the tracks, the attribution and the map title where the page draws them. It owns the view (longitude, latitude and a print zoom that is independent of the screen size), saved as ordinary parameters as you move it; its list of GPX tracks (each with a colour and a label), its country names, the style and the track width are generated controls in its toolbar, where each list is a button such as Tracks (n) that opens it. The base map names each country at one point, often outside a regional map; a country name (a name with a longitude and latitude) sets it where the map should show it, and the base map's labels give way to it. A button beside the zoom buttons fits the view to all tracks. The layout draws the map with a <RenderedImage output="vector"> through maplibre-svg, a small package in the template's packages/ folder (dependencies, linked with file:): MapLibre loads the style and tiles and places the labels off screen, and drawMapSvg writes the drawn features out as SVG paths, and labels the page sets in the book's fonts (a class per label role, styled in styles.css, with fonts.settable keeping only what those fonts can set). Icons, fill patterns and raster relief are left out. The package draws only the base map; what the book adds goes on top in the layout, drawn with projector(view) from the same package so it lines up: the starter's GPX tracks, and in your own template markers, names or insets. avoid keeps the base map's labels clear of such markers, and layers leaves style layers out. A map that cannot be drawn, for example because the map server is down, fails the Book check with rendered-image-failed.

For setup, a minimal vector map, helper options and supported layers, see SVG maps.