PotionUI Docs v0.3.0

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.
  • addImage and openImage act on whichever paint surface is open.

A tool or action that throws is logged and skipped; it never stops the editor.

js
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): a PaintTool with id, label, icon (an SVG path d for a 24x24 viewBox), optional key shortcut, group (tools in the same group sit together), options (any of size, opacity, color, tolerance) and pointer handlers pointerDown, pointerMove, pointerUp, pointerCancel receiving the host and a document-space point. overlay(host, context) draws guides above the canvas. Edit pixels on host.activeLayer, then call host.commitPixels(label, layer, dirtyRect, before) with a copy from host.snapshotLayer(layer) taken first; that single call is what makes the edit undoable. Pointer handlers run only for the primary button.
  • Adjustments (registerAdjustment; registerFilter stays as a deprecated alias for one release): a PaintFilter with id, label, numeric params ({id, label, min, max, value}) or toggle: true, active(values) and apply(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} or null. The source is listed in the Open and Add image menus.
  • Actions (registerAction): {id, label, run(editor)} shown as a button; editor offers flatten(), addImage(canvas, name) and openImage(canvas, name).
  • addImage(canvas, name) / openImage(canvas, name) / isOpen() act on the editor that is currently open. addImage returns false when none is. openImage replaces 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 returns true.

The editor saves a flattened PNG as a new Library upload through the normal upload API, so a plugin never writes files itself.