Preview Frame¶
A frame that shows an image made by an async render, such as a generated poster, built on the thumb core. While it renders it shows the Spinner and a status line, and a failed render shows a Field Message. A pressed group picks a dark, light, or checkered background, and a click opens the image larger in a dialog.
Decision¶
| When | Use |
|---|---|
| Fields that shape a generated image, so the reader sees the result as they type | Preview Frame, beside the fields |
| An image the reader chooses | Image Picker |
Usage¶
import { createPreviewFrame } from './configurationpage?name=myplugin_stolon.js';
var frame = createPreviewFrame({
label: 'Poster preview',
render: function () { return shared.apiRequest('Preview', 'POST', readForm()).then(function (r) { return r.Url; }); }
});
view.querySelector('#previewHost').appendChild(frame.element);
view.addEventListener('input', function () { frame.update(); });
Classes: stolon-preview-frame, with the stolon-busy modifier, and the parts stolon-preview-frame-backgrounds, stolon-preview-frame-stage, stolon-preview-frame-image, stolon-preview-frame-status, and stolon-preview-frame-zoom. The handle is { element, getValue, setValue, update, refresh, destroy }.
States and Modifiers¶
renderreturns an address, a Blob, or a canvas, or a promise of one. A Blob shows through an object URL the frame frees.update()renders once its inputs stop changing fordelay(300 milliseconds), andrefresh()renders at once. A render that finishes after a newer one starts is dropped.stolon-busyis on the root while it renders, with the status line "Rendering…" (renderingText). An empty result shows "No preview." (emptyText), and a failure shows "Could not render the preview." (errorText).backgroundisdark(default),light, orchecker, chosen from the pills, whose labelsdarkText,lightText, andcheckerTextreplace.getValueandsetValueread and set it.backgrounds: falsehides the choice. The dark and light backgrounds are the theme's surface and text colors.- The stage is a button named "Zoom" (
zoomText) and the label, which opens a large dialog titled by the label.labelis the image's alt text, "Preview" by default.zoom: falsemakes it a plain frame.
Gotchas¶
- Call
destroy()when the page hides. It drops a pending update.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createPreviewFrame¶
function createPreviewFrame(options?: PreviewFrameOptions): PreviewFrameHandle
A frame that shows an image made by an async render, such as a generated poster, built on the thumb core. While it renders it shows the spinner and a status line, and a failed render shows a message under it. update renders again once its inputs stop changing for the delay. A pressed group picks a dark, light, or checkered background, and a click on the image opens it larger in a dialog.
| Option | Type | What it does |
|---|---|---|
render |
(): PreviewResult | PromiseLike<PreviewResult> |
function() returning an address, a Blob, or a canvas, or a promise of one [optional] |
label |
string |
the image's alt text and the zoom dialog's title (default 'Preview') |
delay |
number |
milliseconds update waits for its inputs to settle (default 300) |
background |
PreviewBackground |
'dark', 'light', or 'checker' (default 'dark') |
backgrounds |
boolean |
false hides the background choice (default true) |
zoom |
boolean |
false turns off click to zoom (default true) |
renderingText |
string |
the status line while rendering (default 'Rendering…') |
errorText |
string |
the message when render fails (default 'Could not render the preview.') |
emptyText |
string |
the line while there is no image (default 'No preview.') |
darkText |
string |
the dark background's label (default 'Dark') |
lightText |
string |
the light background's label (default 'Light') |
checkerText |
string |
the checkered background's label (default 'Checkered') |
zoomText |
string |
the image button's title and aria-label (default 'Zoom') |
| Handle member | Type |
|---|---|
getValue |
(): PreviewBackground |
setValue |
(background: PreviewBackground): void |
update |
(): void |
refresh |
(): Promise<void> |
destroy |
(): void |
| Type | What it is |
|---|---|
PreviewBackground |
The background a preview frame shows its image on. |
PreviewResult |
What a preview frame's render may return: an address, a Blob, or a canvas, or a promise of one. |
| Class | What it looks like |
|---|---|
stolon-preview-frame |
A frame that shows a rendered image over a dark, light, or checkered background, with the background choice above it and a status line under it |
stolon-preview-frame-stage |
The stage the image sits on, a button that opens the zoom, as wide as its column and a fixed height, the image contained inside it |
stolon-preview-frame-stage.stolon-dark |
The three backgrounds: dark and light from the theme's text and surface, and a checkerboard of the wash for transparency |
stolon-preview-frame-zoom |
The image larger in the zoom dialog, on the same background, never wider than the dialog |
Example¶
Example Code¶
<!-- title: Preview Frame -->
<div style="display: grid; gap: 16px;">
<div class="inputContainer">
<label class="inputLabel" for="previewTitle">Title</label>
<input is="emby-input" type="text" id="previewTitle" value="Example Movie" />
</div>
<div id="mount"></div>
<div id="failing"></div>
</div>
<script type="module">
const base = await window.__baseReady;
const input = document.getElementById('previewTitle');
// A poster drawn on a canvas after a short wait, as a server render would take.
const frame = base.createPreviewFrame({
label: 'Poster preview',
background: 'checker',
render: () => new Promise((resolve) => setTimeout(() => {
const c = document.createElement('canvas');
c.width = 240; c.height = 360;
const g = c.getContext('2d');
g.fillStyle = '#3498db'; g.fillRect(16, 16, 208, 328);
g.fillStyle = '#101010'; g.font = '600 22px sans-serif'; g.textAlign = 'center';
g.fillText(input.value || 'Untitled', 120, 190);
resolve(c);
}, 600))
});
input.addEventListener('input', () => frame.update());
document.getElementById('mount').appendChild(frame.element);
document.getElementById('failing').appendChild(base.createPreviewFrame({ label: 'Broken render', backgrounds: false, render: () => Promise.reject(new Error('timeout')) }).element);
</script>