Concepts
Four ideas appear on every page of this guide. You do not need them to write your first plugin, but you will need them as soon as a plugin reacts to a change.
Every change is described first
Section titled “Every change is described first”When the person edits a title, the app does not modify the row directly. It first builds a description of the change, for example “set the title of row 12 to Buy milk”. This description is called an intent.
A change that comes from another device, from undo or from an import is described in the same way. A plugin that examines intents therefore sees every change, whatever its origin, before the app applies it.
A plugin answers with an object
Section titled “A plugin answers with an object”A hook is a function that the app calls when a change is about to happen. The hook does not decide anything itself. It returns an object that states its answer, and the app reads the object and makes the decision.
ctx.hooks.shouldDelete({ on: 'row' }, delta => { return isProtected(delta) ? veto('protected', 'This row cannot be deleted') // refuse the change : allow(); // accept the change});The two answers used most often are allow() and veto(code, message). The code is a short word that the app records, and the message is the text that the person reads.
Some answers must be immediate
Section titled “Some answers must be immediate”The app needs the answer of a hook before it applies the change, so a hook cannot wait. When a plugin needs time, for example to ask the person a question, the hook returns defer(promise, reason), and the app waits for the promise in its place. Meanwhile the screen keeps working.
ctx.hooks.shouldDelete({ on: 'outline' }, () => defer( ctx.ui.confirm({ title: 'Delete this outline?' }) .then(answer => (answer.confirmed ? allow() : veto('declined', 'The outline was kept'))), 'Asking the person', ),);A plugin can only use what it has declared
Section titled “A plugin can only use what it has declared”The manifest of a plugin lists the permissions that the plugin needs, and the person accepts them when installing the plugin. A call that requires a permission the plugin does not hold fails with an error named PermissionDenied.
For example, a plugin whose manifest lacks the permission ui fails when it calls ctx.ui.notice(…). The page Manifest and permissions lists every permission and what it allows.
