Paged Docs / Embedding the editor
mountEditor and createEditor
Mount the paged editor into a host element, give it host actions, a language and a theme, and drive it through a handle — load, save, fields, role, features.
Reviewed against engine v0.74.2
Mount an editor into your page and drive it from your code.
In short mountEditor(element, options) mounts an editor, configured by a
profile and the host's
actions, into any element. createEditor(element, config) builds on it and
resolves, once the shell, its bundles and the first document are up, to an
EditorHandle: load and save documents, export a PDF, read and fill
template fields, change the session's role and
the theme, switch
features and listen for changes. Every
write goes through the same engine doors the editor's own UI uses, so a filler's
handle can change only what the template permits.
A package with the next release. The embedding API is built on main as the
npm package @paged-media/editor (createEditor, mountEditor and the types;
./iframe for connectEditor; ./presets; ./style.css;
./feature-ids.json and ./theme-tokens.json), with React 19.3 or later as a
peer and the engine, @paged-media/paged, as a dependency. It is not on npm
yet; until it is, a host builds the editor from its source
(apps/editor/apps/canvas/src/embed in
paged-media/paged). The editor renders
with WebGPU and needs a cross-origin-isolated page (COOP same-origin, COEP
credentialless or require-corp), as the playground
does. A host that cannot isolate its page uses the iframe.
Once published:
npm i @paged-media/editor react react-domimport { createEditor } from '@paged-media/editor';
import '@paged-media/editor/style.css';createEditor
import { createEditor } from './embed'; // apps/editor/apps/canvas/src/embed
const editor = await createEditor(document.getElementById('editor')!, {
profile: 'template-fill', // a preset id or an EditorProfile
locale: 'de', // the editor's language; English by default
document: { bytes: templateBytes, name: 'card.paged' },
values: { headline: 'Grand opening', showBadge: true },
actions: {
save: async ({ bytes, name }) => upload(name, bytes),
submit: async () => finish(await editor.getFieldValues()),
},
});
editor.on((e) => {
if (e.type === 'fieldChange') renderSummary(e.fields);
});EditorConfig
| Field | Type | Meaning |
|---|---|---|
profile | string | EditorProfile | A preset id (full, template-fill, viewer, solo:draw, …) or a profile. Default full. |
role | 'author' | 'editor' | 'filler' | 'viewer' | The session's role; overrides the profile's. |
document | { bytes, name? } | The document to open: .paged, .idml, or a template in either. |
seed | string | A seed document to start from, by name (one-text-frame-selected, a-table, flyer, …): a blank US Letter page and the seed's paged.* prelude. Ignored when document is given; an unknown name refuses with an EditorError of code unknownSeed. |
sample | string | A sample publication to open, by its id in the sample catalog (a real magazine, catalogue or report, rebuilt from its publisher's PDF and shown for illustration). The catalog is read before anything mounts: an id it does not list, or a catalog that cannot be read, refuses with an EditorError of code unknownSample. Ignored when document is given. The host page's Content-Security-Policy must admit the catalog's host. |
samplesUrl | string | The sample catalog's URL; https://modules.paged.media/samples/catalog.json by default. |
actions | HostActions | What the host decides: save, submit, close, back (below). |
values | Record<string, FieldValue | string | boolean> | Field values to set once the document is open. |
locale | string | The editor's language (de); English by default. See Languages. |
messages | Record<string, string> | The locale's catalogue, when the host supplies one. |
storage | 'memory' | 'local' | 'none' | EditorStorage | Where the editor keeps its settings (theme, mode, panel layout, recent commands, export options). Default 'memory': for this editor's lifetime, so it never touches the host's origin storage unasked. 'local' uses localStorage under the editor's own keys; an EditorStorage (getItem, setItem, removeItem) is the host's own store. |
assetBase | string | Where the editor's files are served from; a locale the host does not supply is fetched from <assetBase>/locales/<locale>.json. |
EditorHandle
| Member | What it does |
|---|---|
load({ bytes, name? }) | Open a document, replacing the open one. |
newDocument(widthPt, heightPt) | Start a blank one-page document of that size, in points. |
save() | The open document as .paged bytes (Uint8Array). |
exportPdf(options?) | The open document as PDF bytes. |
fields() | The template's fields in form order, each with its definition, target, value and overset state. |
getFieldValues() | The current values, by field name. |
setFieldValues(values) | Fill fields as one undo step, checked against the session's role. Resolves to { ok: true } or { ok: false, problems }; if any value does not fit, none is set. |
setRole(role) | Change the session's role, in the engine and the UI. |
setTheme({ mode, tokens }) | Switch between the dark and light theme and override design tokens; a token set to null returns to the editor's own. See Theming. |
features | The feature store: enable, disable, set, isEnabled, … |
on(listener) | Listen for { type: 'ready' }, { type: 'change' } (any document change, undo, redo or load) and { type: 'fieldChange', fields }. Returns the unsubscribe. |
hostSlot(id) | The element a profile's { kind: "host", id } region item leaves for the host to render its own component into; null until that region is rendered. |
client | The engine client, for what the handle does not cover. |
dispose() | Unmount the editor and release its worker. |
A value in values and setFieldValues may be written short: a string is a
text value, a boolean a visibility value. The full forms are
{ kind: 'text', text }, { kind: 'image', uri } and
{ kind: 'visible', visible }. See Fields for
which kinds can be set today.
fields() lists module fields too (a sheet's cell, a doc's block), but
setFieldValues sets the engine's own kinds only: a module field's value goes
through its module, which the editor's template form and
paged template fill do. Through the handle, a
module field is refused with "a module field is set through its module".
mountEditor
createEditor is a convenience over mountEditor, which a host can also call
directly:
const mounted = mountEditor(element, {
profile: 'viewer', // a preset id or a profile; else read from `search`
search: location.search, // ?profile= / ?solo=, when no profile is given
actions: {}, // host actions
library: false, // start the local document library (browser storage)
onClient: (client) => {}, // the engine client exists
onShell: (shell) => {}, // the shell is mounted
});
mounted.dispose();The editor's own page is one such host: it mounts one editor into #root,
configured by the URL (?profile=, ?solo=, ?locale=).
Several editors can share a page, each with its own document, settings, keys and
popovers: each mounts under its own root element ([data-paged-editor]), its
styles are scoped to that root, and mounting leaves the page's <html> and
<body> alone. Two limits remain: the string catalogue is one per page, so two
editors on a page speak one language, and a modal dialog holds the page's focus
and scroll while it is open.
Host actions
HostActions are optional callbacks the host supplies. Each one but save
becomes a command, so a profile can place it in a region (the header or the
actions bar) and a feature can switch it off:
| Action | Command | Called when |
|---|---|---|
save({ bytes, name }) | — (replaces the editor's own save) | The user saves: the host receives the .paged bytes instead of a file write or a download. |
submit() | paged.host.submit | The user presses the submit button. |
close() | paged.host.close | The user leaves the editor. |
back() | paged.host.back | The user goes back in the host. |
A command for an action exists only when the host supplies that action. Placing one in a region:
createEditor(el, {
profile: {
extends: 'template-fill',
adjust: [{ region: 'actions-bar', op: 'set',
items: ['paged.host.back', { kind: 'spacer' }, 'paged.host.submit'] }],
},
actions: { back: () => history.back(), submit: () => sendOrder() },
});Further actions from the concept — export, upload, asset picking and resolving, validation — are planned.
Roles
The handle's setRole and the profile's role set the engine's session role:
| Role | May |
|---|---|
author | everything, including writing the template policy |
editor | everything except writing the template policy (the default) |
filler | only what the document's template policy permits |
viewer | nothing |
The engine enforces the role for every caller, the handle included; see Permissions and roles.
Related
- On paged.media: the embeddable editor.
Features
Switch single commands, panels, tools, menu paths, keybindings and canvas furniture off and on while the editor runs — by surface id, glob or predicate.
The editor in an iframe
Embed the paged editor from another origin with embed.html and connectEditor — the createEditor handle over messages, host actions answered by the host page.