Skip to main content
Version: Next

Custom Plugins

Plugin authoring guide for command registration, toolbar wiring, and lifecycle-safe behavior.

Plugin anatomy

A plugin usually contains:

  • Unique name
  • commands map
  • toolbar item declarations
  • Optional keymap
  • Optional initialization logic

Minimal plugin template

export function MyPlugin() {
return {
name: "myPlugin",
toolbar: [
{ label: "My Action", command: "myCommand", type: "button", icon: "✨" },
],
commands: {
myCommand: (_args, context) => {
const el = context?.contentElement;
if (!el) return false;
document.execCommand("insertText", false, "My value");
return true;
},
},
keymap: {
"Mod-Shift-M": "myCommand",
},
};
}

Plugin types

  • Mark/formatting plugins
  • Block/structure plugins
  • Command-only utility plugins
  • Dialog-driven plugins

Integration checklist

  1. Register commands before toolbar binding
  2. Preserve and restore selection around dialogs
  3. Scope all overlays to the active editor instance
  4. Emit input/update events after DOM mutation
  5. Verify undo/redo compatibility with structural changes

Packaging checklist

  • Export from package root
  • Provide TS types
  • Include build artifacts (ESM/CJS as required)
  • Document commands and keyboard shortcuts

Common pitfalls

  • Global query selectors causing cross-instance bugs
  • Selection loss on toolbar click
  • Missing cleanup for event listeners/dialog nodes
  • Theme-incompatible plugin UI surfaces

API Surface

SurfaceTypeNotes
nameRequired plugin fieldUnique plugin identity used in diagnostics/registration
commandsRequired plugin fieldCommand handlers invoked by toolbar/shortcuts/programmatic execution
toolbarOptional plugin fieldToolbar item declarations bound to plugin commands
keymapOptional plugin fieldKeyboard shortcut bindings to command keys
Lifecycle hooks (init/cleanup contract)Optional plugin fieldSetup listeners/UI and dispose safely on teardown
Selection utilities (plugin runtime context)Runtime surfacePreserve/restore selection for dialog-driven actions

Config Matrix

FieldRequiredPurpose
nameYesPlugin identity and diagnostics
commandsYesRuntime action handlers
toolbarNoToolbar integration
keymapNoKeyboard command mapping
initNoStartup/setup behavior

Validation Checklist

  • Commands run only in intended editor context
  • Selection-sensitive actions preserve caret/range
  • Plugin DOM mutations are undo/redo safe
  • Event listeners are cleaned up on teardown paths