Skip to content

PluginContext

The object that the app passes to the onload function of a plugin. A plugin reaches the app only through this object. The parts of it that do something sensitive require a permission, which the plugin requests in its manifest, and which the person approves when installing the plugin.

The page “The ctx object” of the guide lists every property and explains the most common tasks.

export default definePlugin({
onload(ctx) {
ctx.log('info', 'The plugin has started');
ctx.ui.notice({ text: 'Hello from the plugin' }); // requires the `ui` permission
},
});
readonly audit: PluginAudit;

Reads the signed record of the changes made to an outline. Requires audit. See PluginAudit.


readonly clock: PluginClock;

Provides the current time. See PluginClock.


readonly commands: PluginCommands;

Registers actions that have a name. See PluginCommands.


readonly crypto: PluginCrypto;

Locks and unlocks text, and signs it. Requires crypto, and crypto:friends for devices and friends. See PluginCrypto.


readonly editing: PluginEditing;

Examines the text that the person types, before each change becomes final. Requires read. See PluginEditing.


readonly facets: PluginFacets;

Defines the extra information that the plugin stores on rows, outlines and the library. See PluginFacets.


readonly hooks: PluginHooks;

Registers functions that the app calls when a change is about to happen or has just happened. Requires read. See PluginHooks.


readonly i18n: PluginI18n;

Provides the texts of the plugin in several languages. See PluginI18n.


readonly jobs: PluginJobs;

Runs slow work in the background. Requires jobs. See PluginJobs.


readonly keys: PluginKeys;

The values that ctx.observe can watch. See PluginKeys.


readonly net: PluginNet;

Fetches from a website. Requires net:<domain>. See PluginNet.


readonly notifications: PluginNotifications;

Sends events and listens to events, between plugins and from the app. See PluginNotifications.


readonly outlines: PluginOutlines;

Ready-made changes to outlines. Requires write:outlines. See PluginOutlines.


readonly platform: PluginPlatform;

Where the plugin runs, and the version of the app. See PluginPlatform.


readonly query: PluginQuery;

Searches the library, and extends the search with new words, filters and lists. Requires read. See PluginQuery.


readonly rows: PluginRows;

Ready-made changes to rows. Requires write:rows. See PluginRows.


readonly state: StateView;

The library as it is now: rows, outlines, areas and saved searches. The plugin cannot change it through this object, and it does not update by itself, so the plugin asks again to see later changes. Requires read. See StateView.


readonly topics: Readonly<{
alertDidRing: Topic<{
date: string;
time?: string;
title: string;
}>;
auditDidReport: Topic<{
at?: number;
author?: string;
count?: number;
device: string;
head?: number;
kind: "arrived" | "proved" | "bad" | "fork" | "ahead" | "noted";
mine?: number;
notes?: readonly object[];
outline?: string;
reason?: string;
scope: string;
seq?: number;
theirs?: number;
titles?: readonly string[];
who?: {
avatar: string;
me: boolean;
name: string;
};
}>;
auditDidWrite: Topic<{
scopes: readonly string[];
}>;
dayDidChange: Topic<unknown>;
deviceDidChange: Topic<PeopleNotice>;
didCheckpoint: Topic<unknown>;
didCloseUndoGroup: Topic<unknown>;
didDefer: Topic<{
by: string;
phase: string;
reason: string;
}>;
didMerge: Topic<unknown>;
didModify: Topic<unknown>;
didOpenUndoGroup: Topic<unknown>;
didRedo: Topic<unknown>;
didReject: Topic<unknown>;
didUndo: Topic<unknown>;
didUpdate: Topic<unknown>;
didWrite: Topic<unknown>;
flowDidChange: Topic<undefined>;
friendDidChange: Topic<PeopleNotice>;
mediaDidArrive: Topic<{
id: string;
}>;
registryDidChange: Topic<{
topic: string;
}>;
shareDidChange: Topic<PeopleNotice>;
syncDidChange: Topic<{
from: string;
to: string;
}>;
willCloseUndoGroup: Topic<unknown>;
willRedo: Topic<unknown>;
willUndo: Topic<unknown>;
}>;

The events that the app itself sends, for example a device was added, a friend changed, or an alert rang (ctx.topics.alertDidRing). ctx.notifications.subscribe takes one of them as an argument.


readonly ui: PluginUi;

Talks to the person and adds elements to the screen. Requires ui. See PluginUi.


readonly views: Readonly<{
classifier: Readonly<{
status: ViewRef<undefined>;
}>;
datesNl: Readonly<{
words: ViewRef<{
heading?: boolean;
language?: string;
text: string;
todo?: boolean;
}>;
}>;
history: Readonly<{
events: ViewRef<Free>;
}>;
journal: Readonly<{
days: ViewRef<Free>;
}>;
links: Readonly<{
offers: ViewRef<{
query: string;
row: string;
}>;
}>;
numbering: Readonly<{
section: ViewRef<{
row: string | readonly string[] | undefined;
}>;
}>;
outlines: Readonly<{
clip: ViewRef<{
ids: readonly string[];
}>;
export: ViewRef<{
outline: string;
}>;
header: ViewRef<{
outline: string;
}>;
inspector: ViewRef<{
outline: string;
}>;
list: ViewRef<Free>;
sidebar: ViewRef<undefined>;
}>;
schedule: Readonly<{
alerts: ViewRef<undefined>;
counts: ViewRef<undefined>;
deadlines: ViewRef<Free>;
editor: ViewRef<Free>;
today: ViewRef<Free>;
upcoming: ViewRef<Free>;
}>;
search: Readonly<{
results: ViewRef<{
query: string;
}>;
saved: ViewRef<{
id: string;
}>;
}>;
tags: Readonly<{
offers: ViewRef<{
query: string;
row: string;
}>;
quotes: ViewRef<undefined>;
rows: ViewRef<Free>;
tree: ViewRef<undefined>;
}>;
tasks: Readonly<{
anytime: ViewRef<Free>;
logbook: ViewRef<Free>;
}>;
ui: Readonly<{
contributions: ViewRef<undefined>;
outline: ViewRef<{
outline: string;
plugin: string;
type: string;
}>;
}>;
views: Readonly<{
cards: ViewRef<{
outline: string;
}>;
table: ViewRef<{
outline: string;
}>;
}>;
}>;

The lists that the app already provides, such as ctx.views.outlines.header. ctx.query.view takes one of them as an argument.

dispatch(intent, options?): Promise<Outcome>;

Makes a change. An intent is a description of a change. The app checks it in the same way as a change that the person makes, and it marks the change as made by a plugin.

The result is an Outcome, and its status tells what happened:

  • committed: the change was applied.
  • vetoed: a plugin refused the change.
  • unavailable: the change cannot be made in the current context.
  • invalid: the request was not acceptable.
  • conflict: the library changed while the app waited for a deferred decision.
  • cancelled: the change was abandoned.
  • failed: an error stopped the change.

Requires write:rows, or write:outlines for the commands that create, modify, move, delete or rename outlines, areas and saved searches.

Parameter Type Description
intent Intent the description of the change, for example the value that ctx.rows.modify.intent({ id, title }) returns
options? { quiet?: boolean; } quiet is reserved for the features that ship with the app
options.quiet? boolean -

Promise<Outcome>

the result of the change

const outcome = await ctx.dispatch(ctx.rows.modify.intent({ id: row.id, title: 'Buy milk' }));
if (outcome.status !== 'committed') ctx.log('warn', `Not changed: ${outcome.status}`);

log(level, message): void;

Writes a line to the log of the plugin. Settings › Plugins shows the log together with the diagnostics.

Parameter Type Description
level string info, warn or error
message string the line to write

void


normalize(fn): object;

Keeps the data of the plugin consistent. After every change, the app calls the function, which can add repairs to the change, for example to remove a value that no longer makes sense. The app applies the repairs in the same step as the change. The function has to be idempotent: when it runs a second time on data that it has already repaired, it changes nothing.

Parameter Type Description
fn (state, change, draft) => void | DraftBuilder receives the state after the change, the draft of the change and a DraftBuilder, and returns the builder or nothing

object

an object with dispose(), which removes the function

Name Type
dispose() () => void

observe<T>(
key,
options,
handler
): object;

Calls a function when a value changes. The value is identified by a key: either one of ctx.keys, or the value of a facet on one row (handle.on(id)). Requires read.

Type Parameter Description
T the type of the value that is watched
Parameter Type Description
key Key<T> the value to watch
options ObserveOptions says which of old, new, initial and prior the handler receives
handler (change) => void receives the old value and the new value

object

an object with dispose(), which stops the watching

Name Type
dispose() () => void
ctx.observe(ctx.keys.app.language, { new: true }, change => {
ctx.log('info', `The language is now ${change.new}`);
});