Events
Respond to save, export, close, and error events.
The editor tells your code about four moments: a design is saved, a design is exported, the editor closes, and something goes wrong. You receive them as callback props. The custom element also sends them as DOM events.
This page shows every callback and every event:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Image editor</title>
</head>
<body>
<media-editor-image id="editor" style="position: fixed; inset: 0"></media-editor-image>
<script type="module">
import './node_modules/@layermetry/media-editor/dist/embed.modern.js';
const editor = document.getElementById('editor');
editor.props = {
licenseKey: 'YOUR_LICENSE_KEY',
onExport(file) {
console.log(`onExport: ${file.name}`);
},
onClose() {
console.log('onClose');
},
async onSaveTemplate({ template }) {
console.log(`onSaveTemplate: ${template.id}`);
},
onLicenseState(report) {
console.log(`onLicenseState: ${report.state.status}`);
},
};
// The same moments, as DOM events. They bubble, so a listener on document also works.
editor.addEventListener('templatesave', (event) => {
console.log(`templatesave: saved=${event.detail.saved}`);
});
editor.addEventListener('close', () => {
console.log('close event');
});
</script>
</body>
</html>| Callback | DOM event | When |
|---|---|---|
onSaveTemplate | templatesave | The user saves a template or a draft. |
onExport | export | The user presses Save & Exit or Share in the export panel. |
onClose | close | The user closes the editor, and after Save & Exit. |
onBrandChange | brandchange | The editor opens with a brand kit, and the user switches the kit. See Brand kits. |
onLicenseState | none | The state of the license changes. |
onUploadAsset | none | The editor needs to store a picture. See Asset storage. |
The DOM events bubble, and they cross shadow DOM boundaries, so a listener on document also receives them.
Save
onSaveTemplate({ ...metadata, template }) saves the design. The editor waits for the promise that it returns, so a DOM event cannot replace it. See Saving and loading.
After each save, the element also sends templatesave, with detail: { template, saved }. saved is false when the save failed or took more than 30 seconds. template is null when the editor could not prepare the design.
editor.addEventListener('templatesave', (event) => {
console.log(`templatesave: saved=${event.detail.saved}`);
});Export
onExport(file, extras) receives the finished design as a File. See Export for the formats.
onExport={(file) => console.log(`${file.name}: ${file.size} bytes, ${file.type}`)}extras is present when the user presses Share, or when the editor was opened with a defaultTemplate. extras.email is { to, name }: the address and the name that the user typed. extras.templateID is the id of the defaultTemplate. Read each field with a check, such as extras?.email?.to.
callback is an older name for onExport. The editor calls both, so use only one of them.
The custom element sends one export event for each export. event.detail is the File. The extras object is not in the event: read it from the second argument of onExport.
The Download button in the export panel saves the file on the user's computer and calls no callback.
Close
onClose() is required. The editor calls it when the user presses the close button in the top bar, and after Save & Exit. Remove or hide the editor when it runs.
onClose={() => setIsOpen(false)}The custom element also sends a close event. Do not read its detail: after Save & Exit it is empty, and after the close button it is the editor's own click event.
The close button does not ask the user to save first. Save the design in onClose if your product needs it, for example with a draft. See Drafts.
Errors
The editor has no single error callback. Each kind of problem reaches you in its own way:
| Problem | How you learn about it |
|---|---|
| The license is refused or cannot be confirmed | onLicenseState, with state.status 'blocked' and a reason. See License validation. |
| A save fails | Your own onSaveTemplate promise rejects, and templatesave has saved: false. |
| A picture cannot be stored | Your own onUploadAsset promise rejects. The save stops. |
| An AI request fails | The editor shows the error in the AI box. Your proxy or requestFn sees the failure first. See Custom AI endpoint. |
The ai setting is wrong | The console shows "[MediaEditor] ai is not usable, so AI stays off:" and the reason. |
onLicenseState={(report) => {
if (report.state.status === 'blocked') {
console.error(`License blocked (${report.state.reason}): ${report.reference}`);
}
}}report.reference is a short line for your support team. It never contains the license key. See Error codes for every reason.