Custom tools

Extending

Custom tools

Register your own blocks. A custom tool appears in the Content panel like a built-in, renders on the canvas, is configured through inspector fields, and exports to email-safe HTML.

Register a tool (Unlayer's call)#

Call registerTool on the editor, the same call Unlayer uses. It works in react-email-craft (ref.current.editor), vue-email-craft (this.$refs.editor.editor), angular-email-craft (component.editor) and vanilla-email-craft (mailkaro.registerTool, also before mailkaro.init). A tool written for Unlayer needs no change:

tsxproductTool.ts
const onReady = (editor) => {
  editor.registerTool({
    name: "product_card",
    label: "Product",
    icon: "https://cdn.example.com/icons/product.png", // a URL or an emoji; Font Awesome names get the default icon
    supportedDisplayModes: ["web", "email"],
    options: {
      content: {
        title: "Content",
        position: 1,
        options: {
          title: { label: "Title", defaultValue: "Trail Runners", widget: "text" },
          price: { label: "Price", defaultValue: "$120", widget: "text" },
          color: { label: "Accent", defaultValue: "#0f766e", widget: "color_picker" },
          photo: { label: "Photo", defaultValue: { url: "https://cdn.example.com/shoe.jpg" }, widget: "image" },
          link: { label: "Link", defaultValue: { name: "web", values: { href: "https://shop.example.com", target: "_blank" } }, widget: "link" },
        },
      },
    },
    values: {},
    renderer: {
      // What the canvas shows (HTML).
      Viewer: editor.createViewer({
        render: (v) => `<div style="border:2px solid ${v.color};padding:12px"><img src="${v.photo.url}" width="120"> ${v.title} ${v.price}</div>`,
      }),
      // What the export and the sent email get (keep it table-based with inline styles).
      exporters: {
        email: (v) => `<table role="presentation" width="100%"><tr><td style="color:${v.color}">
          <a href="${v.link.values.href}"><img src="${v.photo.url}" width="100%" alt=""></a>
          <strong>${v.title}</strong> ${v.price}</td></tr></table>`,
        web: (v) => `<div>${v.title}</div>`,
      },
      head: { css: () => "", js: () => "" },
    },
  });
};

How it runs#

The editor runs in a frame on another origin, and functions cannot cross into it. So your tool's code stays on your page: the editor gets the plain part (name, label, icon, fields, default values) and, each time a block's values change, asks your page for its HTML. It keeps that HTML in the block (values._html), so exportHtml, saveDesign and a server render never wait for your page, and a design that uses the tool still renders where the tool is not registered.

  • The canvas shows renderer.Viewer.render(values) (or the export when there is no Viewer). HTML from your page is cleaned like an HTML block.
  • The exporter for the current display mode is used (exporters.email, exporters.web, ...), falling back to email.
  • Exporters may return a promise. head.css(values) is put in a <style> in front of the block's HTML.
  • If an exporter throws, the block keeps its last good HTML.
  • Registering the same name again replaces the tool. registerTool returns a promise that settles once the editor has the tool, and rejects for a config without a name.
  • The tool's code (your customJS in Unlayer) does not run inside the frame: register it from your page.

Widgets#

Unlayer's property widgets map to the editor's fields. The exporter gets values in Unlayer's shape:

PropertyControlsDefault
textSingle-line inputstring
rich_text, html, textareaMulti-line inputstring
color_pickerColor pickerstring
toggleSwitchboolean
counterNumber input (data.min / data.max)number
dropdownDropdown (data.options)string
alignmentLeft / Center / Rightstring
imageUpload field with a link box{ url, ... }
linkAddress input{ name, values: { href, target } }
font_familyText input{ label, value }
Others with an object value (padding, border, ...)Not shown; the default value is keptas given

MailKaro's own shape#

registerTool also takes MailKaro's tool object: id, label, icon, values, inspector fields and exportHtml(values). An optional render(values) that returns an HTML string is used for the canvas.

PropTypeDefaultDescription
idstring-Unique id, stored on the block as values._tool.
labelstring-Name in the Content panel, on the block and in the inspector.
iconstring-An image URL, or an emoji.
descriptionstring-Optional help text.
valuesRecord<string, unknown>-Default values for a new block.
inspectorToolField[]-Fields (below). Omit for a view-only block.
exportHtml(values) => string | Promise<string>-Email HTML for export and the sent email.
render(values) => string-Optional canvas HTML.

Each field binds a control to a key in the block's values:

PropertyControlsDefault
type: text / urlSingle-line input,
type: textareaMulti-line input,
type: numberNumeric input (min / max),
type: colorColor picker,
type: fileUpload button with a progress bar and cancel question, a drop area and a link box. The value is the URL; the name, type and size are saved as <key>_name, <key>_mime and <key>_size,
type: imageThe same upload field, for images,
type: selectDropdown (options: [{label, value}]),
type: toggleBoolean switch,
Server-side rendering. The renderer and the sent email use each custom block's values._html. If you render on your own server and want fresh HTML, pass the same exporters to renderDesign via its tools option.
Keep the exporter email-safe: table-based layout, inline styles, absolute image URLs. The Viewer can be richer since it only shows in the editor.