Skip to content

PluginFacets

Defines facets. A facet is a piece of extra information that a plugin attaches to a row, to an outline or to the library as a whole. define creates a facet, and core holds the facets that come with the app.

Count the work sessions of a row

const sessions = ctx.facets.define<number>({
key: 'com.example.pomodoro/sessions',
on: 'row',
merge: 'counter',
schema: { type: 'integer', minimum: 0 },
});
const count = sessions.of(row); // the value on a row, or undefined
await ctx.dispatch(sessions.set(row.id, 3)); // writes the value
readonly core: Readonly<{
journal: Readonly<{
day: FacetRef<string>;
}>;
links: Readonly<{
targets: FacetRef<Readonly<Record<string, string>>>;
}>;
media: Readonly<{
attachments: FacetRef<Attachment[]>;
}>;
schedule: Readonly<{
alert: FacetRef<{
minutes_before: number;
}>;
due: FacetRef<{
date: string;
time?: string;
}>;
repeat: FacetRef<{
from: "schedule" | "completion";
occurrence?: string;
rule: string;
}>;
when: FacetRef<{
date: string;
time?: string;
}>;
}>;
sharing: Readonly<{
assignee: FacetRef<string>;
}>;
tags: Readonly<{
tags: FacetRef<string[]>;
}>;
tasks: Readonly<{
priority: FacetRef<"high" | "low">;
status: FacetRef<{
closed_at?: number;
state: "cancelled" | "done" | "open";
}>;
}>;
views: Readonly<{
cells: FacetRef<Readonly<Record<string, string>>>;
layout: FacetRef<{
columns?: object[];
presentation: string;
title_column?: {
name?: string;
width?: number;
};
}>;
}>;
}>;

The facets that come with the app. A plugin reads one through its object, for example ctx.facets.core.tasks.status.of(row). The key property of the object is the name of the facet.

define<T>(ref, owner?): FacetHandle<T>;

Creates a facet from a reference that was declared with facet({ key, target, schema }). The app checks every value that is written against the schema, which describes the values that are allowed. When two devices change the value at the same time, the app resolves the conflict with the merge kind of the facet.

Type Parameter Description
T the type of the values of the facet
Parameter Type Description
ref FacetRef<T> the declaration of the facet. The type of the value comes from its schema
owner? FacetOwner an optional { validate } object with an additional check for a value that already fits the schema. The check returns valid(), coerced(value) or invalid(issue)

FacetHandle<T>

a handle. of(row) reads the value. set(id, value) and clear(id) build the change that writes or removes the value. on(id) watches the value. dispose() removes the facet

define<T>(definition): FacetHandle<T>;

Creates a facet from a plain object, which has the following fields: the key of the facet, written <plugin id>/<name>, the kind of thing it is attached to (on), its merge kind, its schema, and whether the other devices receive it (sync).

Type Parameter Default type Description
T unknown the type of the values of the facet
Parameter Type Description
definition FacetDeclaration the object that describes the facet

FacetHandle<T>

a handle, as described for the other form of define