Editor Dialog¶
The record editor a config page builds. It has Remove (unless the record is new), any extra actions, Cancel, and Done, asks "Discard your changes?" when dismissed with edits, and tracks its own changes.
Decision¶
| When | Use |
|---|---|
| Adding or editing one record, such as a server | Editor Dialog |
| A quick value | Prompt Dialog |
Usage¶
import { openEditorDialog } from './configurationpage?name=myplugin_stolon.js';
var editor = openEditorDialog({
title: 'Edit Server',
body: editorNode,
page: page,
isNew: false,
serialize: function () { return JSON.stringify(readEditor()); },
ready: loadingPromise,
onDone: function (close) { return saveServer().then(function () { return true; }, function () { return false; }); },
onRemove: function (close) { confirmRemove().then(function (yes) { if (yes) { removeServer(); close('removed'); } }); }
});
It returns the openDialog handle plus isDirty() and markClean().
States and Modifiers¶
title,body,className,size,onClose,autofocus, andreturnFocusare as on Dialog. It islargeunlesssizeis set.pageis thecreateConfigPagehandle. An open editor then counts towardguardUnsavedand closes when the page hides.isNewmarks a record not saved yet. It has no Remove button.serialize()returns a stable string of the editor state.readyis a promise to wait on before taking the clean snapshot, for fields still loading.doneText,removeText, andcancelTextreplace "Done", "Remove", and "Cancel".onDone(close)returnsfalse, or a promise offalse, to stay open. Done is disabled while a returned promise is pending, and a rejection enables it again. A true or undefined result closes with'done'.actionsadds buttons to the pinned footer, such as Duplicate. Each is{ text, icon, variant, onClick(handle, button) }, whereiconis a Material icon name before the text andvariantis'secondary'(default),'primary', or'destructive'. The footer reads Remove and any destructive action first, then the other actions, then Cancel, then Done, in the same button look and size, on one line that wraps on a phone. An action leaves the editor open, andonClickcloses it through the handle when it should.onRemove(close)closes the dialog itself. Remove shows only when this is set andisNewis false.- Dismissing with changes opens a destructive confirm titled "Unsaved changes" that asks "Discard your changes?".
Gotchas¶
- The clean snapshot is taken when
readysettles. CallmarkClean()after a save that keeps the dialog open.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
openEditorDialog¶
function openEditorDialog(opts?: EditorDialogOptions): EditorDialogHandle
Opens an editor dialog for one record with Remove, Cancel, and Done buttons, and asks before it discards edits. Given the createConfigPage handle, it counts toward the page unsaved guard and closes when the page hides.
| Option | Type | What it does |
|---|---|---|
page |
Pick<PageHandle, 'trackEditor' | 'untrackEditor'> | null |
the createConfigPage handle [optional] |
isNew |
boolean |
true for a record not saved yet, which has no Remove button (default false) |
serialize |
(): string |
function() returning a stable string of the editor's state, for its change tracking |
ready |
unknown |
a promise to wait on before taking the clean snapshot, such as fields still loading [optional] |
doneText |
string |
|
removeText |
string |
|
cancelText |
string |
|
actions |
EditorDialogAction[] |
[{ text, icon, variant, onClick(handle, button) }] extra footer buttons, such as Duplicate, drawn after Remove and before Cancel. variant is 'secondary', 'primary', or 'destructive' (default 'secondary'), and a destructive one sits beside Remove [optional] |
| Handle member | Type |
|---|---|
isDirty |
(): boolean |
markClean |
(): void |
| Type | What it is |
|---|---|
EditorDialogAction |
One extra footer button of an editor, such as Duplicate. icon is a Material icon name, and variant picks its look. |
Example¶
Example Code¶
<!-- title: Editor Dialog -->
<!-- min-height: 460 -->
<button type="button" id="btnEdit" class="raised stolon-button stolon-secondary"><span>Edit Server</span></button>
<span id="editorStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
shared.bindClick('btnEdit', () => {
const body = document.createElement('div');
body.innerHTML = '<div class="inputContainer"><label for="txtEditorName">Name</label>' +
'<input is="emby-input" type="text" id="txtEditorName" value="Living room"></div>';
const input = body.querySelector('input');
base.openEditorDialog({
title: 'Edit Server',
body: body,
isNew: false,
serialize: () => input.value,
onDone: () => true,
onRemove: (close) => close('removed'),
actions: [{ text: 'Duplicate', icon: 'content_copy', onClick: (editor) => editor.close('duplicated') }],
onClose: (value) => shared.setStatus('editorStatus', 'Closed with ' + (value || 'a dismissal') + '.', false, { timeout: 0 })
});
});
</script>