Extending the image editor
The Image editor tab and the paint window on media fields (Edit on a loaded image,
Draw on an empty field) share one paint engine and read the same registries, which a
plugin frontend extends through window.__potionui.imageEditor. Every register* call returns
an unregister function. Registrations are global and survive editor sessions, so register once
when your plugin's frontend loads. Where each one takes effect:
- Tools (
registerTool): listed in the Image editor's Paint tool after the built-in brush, eraser, fill, picker, rectangle, lasso and move tools, and in the field paint window's tool rail. - Adjustments (
registerAdjustment) and image sources (registerImageSource): offered in both surfaces. - Actions (
registerAction): shown under Actions in the Image editor's Paint options and in the field paint window's Canvas panel. addImageandopenImageact on whichever paint surface is open.
A tool or action that throws is logged and skipped; it never stops the editor.
const editor = window.__potionui.imageEditor;
const offTool = editor.registerTool({
id: 'line',
label: 'Line',
icon: 'M5 19L19 5',
key: 'N',
group: 'shapes',
options: ['size', 'color'],
hint: 'Drag to draw a straight line.',
cursor: 'crosshair',
pointerDown(host, point) { start = point; },
pointerUp(host, point) {
const layer = host.activeLayer;
const before = host.snapshotLayer(layer);
const context = layer.canvas.getContext('2d');
context.strokeStyle = host.settings.color;
context.lineWidth = host.settings.size;
context.beginPath();
context.moveTo(start.x - layer.x, start.y - layer.y);
context.lineTo(point.x - layer.x, point.y - layer.y);
context.stroke();
host.commitPixels('Line', layer, { x: 0, y: 0, width: layer.canvas.width, height: layer.canvas.height }, before);
}
});- Tools (
registerTool): aPaintToolwithid,label,icon(an SVG pathdfor a 24x24 viewBox), optionalkeyshortcut,group(tools in the same group sit together),options(any ofsize,opacity,color,tolerance) and pointer handlerspointerDown,pointerMove,pointerUp,pointerCancelreceiving thehostand a document-space point.overlay(host, context)draws guides above the canvas. Edit pixels onhost.activeLayer, then callhost.commitPixels(label, layer, dirtyRect, before)with a copy fromhost.snapshotLayer(layer)taken first; that single call is what makes the edit undoable. Pointer handlers run only for the primary button. - Adjustments (
registerAdjustment;registerFilterstays as a deprecated alias for one release): aPaintFilterwithid,label, numericparams({id, label, min, max, value}) ortoggle: true,active(values)andapply(image, values)returning the new{width, height, data}RGBA buffer (it may be a promise). Filters appear in the Adjust panel, preview live, and honour the selection automatically: the editor blends the result back through the selection mask, so a filter never needs to know about it. - Image sources (
registerImageSource):{id, label, pick()};pick()resolves{url | blob | canvas, name}ornull. The source is listed in the Open and Add image menus. - Actions (
registerAction):{id, label, run(editor)}shown as a button;editoroffersflatten(),addImage(canvas, name)andopenImage(canvas, name). addImage(canvas, name)/openImage(canvas, name)/isOpen()act on the editor that is currently open.addImagereturnsfalsewhen none is.openImagereplaces the open editor's image, and with none open it uploads the canvas as a PNG and opens an Image editor tab on it (History and Library Edit actions open that tab too), so it always returnstrue.
The editor saves a flattened PNG as a new Library upload through the normal upload API, so a plugin never writes files itself.