> ## Documentation Index
> Fetch the complete documentation index at: https://docs.widgetic.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Composition Editor

> Let your customers change the texts, pictures, colours and fonts of a widget they own, beside a live preview, in your page

The **Composition Editor** lets a customer change the texts, pictures, colours and fonts of a widget they own, beside a live preview, and save it (see it in [the gallery](https://docs.widgetic.com/docs/partners/ui-components#the-pieces)). It is the editor of Widgetic's own site, drawn in your page.

Like every piece, it is mounted with the [UI SDK](https://docs.widgetic.com/docs/partners/ui-components) and uses an [editor session](https://docs.widgetic.com/docs/partners/editor-sessions.md) for one of your [managed users](https://docs.widgetic.com/docs/partners/managed-users.md), made with the surfaces `["editor", "files"]`. Installing the SDK, tokens, the theme, the language and the Content Security Policy are the same for every piece, and are on [the UI Components page](https://docs.widgetic.com/docs/partners/ui-components). This page covers what is specific to the Composition Editor.

## Mount it

Create the composition on your server with your key and file it under the customer (see [Filing Content Under a Managed User](https://docs.widgetic.com/docs/partners/managed-users.md#filing-content-under-a-managed-user)). A session opens only compositions filed under its customer. Then mount the editor on it:

```javascript theme={"theme":"css-variables"}
import { mountModule } from '@widgetic/ui-sdk';

const editor = await mountModule('composition-editor', {
  container: '#editor', // give it a height: it fills its container
  compositionId: 'cmp_123', // the composition of the customer who is on the page; one editor edits one composition
  getToken: async () => (await fetch('/widgetic-session', { method: 'POST' })).json(), // { token, expiresAt }
  on: {
    ready: ({ name, widgetId }) => showTitle(name),
    dirty: ({ dirty }) => markUnsaved(dirty),
    saved: ({ savedAt }) => markSaved(savedAt),
    error: ({ message, code, fatal, sessionEnded }) => report(code, message),
  },
});

// later: saves what is unsaved first, then takes the editor away
await editor.unmount();
```

**One Composition Editor per page.** A second one is refused with a clear message. Unmount the first before you mount another.

## Options

| Option | |
| - | - |
| `compositionId` | **Required.** The composition to edit. |
| `mediaLibrary` | Your own media library for the file pickers. See [Show your own media library](https://docs.widgetic.com/docs/partners/ui-components/file-browser.md#show-your-own-media-library). |
| `pickFile` | Your own picker for the file fields. See [Your own file picker for the editors](https://docs.widgetic.com/docs/partners/ui-components/file-browser.md#your-own-file-picker-for-the-editors). |
| `googleFontsApiKey` | A Google Fonts Web Fonts API key for the font picker's list. Without one the picker lists a short set of popular families. |
| `autosaveDelayMs` | How long after the last change the editor saves by itself. Default 2500. `0` turns autosave off. |
| `locale` | A BCP 47 tag such as `de`: its words (65 languages come with the editor), plural forms, numbers and dates, and the names of the months and weekdays in its calendars. |
| `messages` | The editor's words in your language: one object for the editor, its file picker and its rich text editor. A key you leave out keeps the built-in words of the `locale` (English when it has none). See [The Composition Editor](https://docs.widgetic.com/docs/partners/ui-components#the-composition-editor) and [Show the pieces in the language of your customers](https://docs.widgetic.com/docs/partners/ui-components#show-the-pieces-in-the-language-of-your-customers). |
| `theme`, `apiUrl` | As for every piece. See [Make the pieces look like your product](https://docs.widgetic.com/docs/partners/ui-components#make-the-pieces-look-like-your-product). |

## Events

| Event | Payload | |
| - | - | - |
| `ready` | `{ name, widgetId }` | The composition has loaded and the editor is on screen. |
| `dirty` | `{ dirty }` | There is, or is no longer, work that is not saved. |
| `saved` | `{ composition, savedAt }` | A save went through. |
| `conflict` | `{ currentVersion, lastEditedBy, lastEditedAt, correlationId? }` | A save was refused because somebody else saved the composition after the editor loaded it (your own server, with your key, for example). Nothing is lost: the editor keeps the customer's edits and asks which version stays. Not an `error`. `lastEditedBy` and `lastEditedAt` are `null` in an editor session. |
| `conflictResolved` | `{ resolution }` | The customer (or your page, with `editor.module.keepMyChanges()` or `editor.module.loadLatestVersion()`) chose: `'keepMine'` (the edits are saved on top of the newer version) or `'loadLatest'` (the edits are gone). |
| `error` | `{ message, code?, fatal, during?, status?, retryable?, sessionEnded?, correlationId? }` | See [When something goes wrong](#when-something-goes-wrong). |

## Saving

* **The editor saves by itself** shortly after the last change. `editor.module.save()` saves what is unsaved now, `editor.module.flush()` saves everything unsaved and waits until it is (both resolve `true` when nothing is left unsaved), and `editor.module.isDirty()` tells whether anything is unsaved. A save is safe to retry: it carries an `Idempotency-Key`.
* **`unmount()` saves first**, for up to 10 seconds, so closing the editor never loses the last edits. If some could not be saved you hear an `error` with the code `UNSAVED_ON_UNMOUNT`. `editor.module.unmount({ discard: true })` throws unsaved edits away instead.

## When something goes wrong

* **Problems** are shown to the customer by the editor itself, with "Try again" where that helps. `error` is for your page, and adds `during`, `status`, `retryable`, `sessionEnded` and `correlationId` to the usual fields. `fatal: true` means the editor cannot go on: the composition cannot be opened, or the session is over (`sessionEnded: true`). Make a new session or take the editor away. Quote `correlationId` to Widgetic's support.
* **Another customer's composition** is answered with `404`, and the editor says it cannot be opened.

## Next Steps

> [card] [Editor Sessions](https://docs.widgetic.com/docs/partners/editor-sessions.md)
>
> Make the token on your server

> [card] [UI Components](https://docs.widgetic.com/docs/partners/ui-components)
>
> The SDK, tokens, the look and the language of every piece

> [card] [Managed Users](https://docs.widgetic.com/docs/partners/managed-users.md)
>
> File your customers' compositions under them

> [card] [Compositions](https://docs.widgetic.com/docs/api/compositions.md)
>
> Make a composition with your key


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.