143 lines
6.6 KiB
Markdown
143 lines
6.6 KiB
Markdown
# RuneStone Plugin SDK
|
|
|
|
`@runestone/plugin-sdk` gives TypeScript types and small helpers for [RuneStone](https://github.com/kwyvern-gh/RuneStone) plugins.
|
|
RuneStone is a desktop workspace for Markdown notes, code, and project work. Plugins add views, commands, and document editors to it.
|
|
|
|
The SDK contains:
|
|
|
|
- The types of the plugin API: `PluginApi` and its members, the `plugin.json` manifest, commands, views, document types, menus, and contributions
|
|
- `API_VERSION`, the plugin API version that the SDK describes
|
|
- Feature checks for API members that older RuneStone versions do not have
|
|
- The RuneStone action icons and the tooltip helper, as DOM functions
|
|
|
|
The SDK does not run plugins and does not call RuneStone. RuneStone gives the `api` object to your plugin at activation.
|
|
|
|
## Install
|
|
|
|
Install the SDK from the URL of its package file on the RuneStone releases page.
|
|
|
|
```sh
|
|
pnpm add -D "https://github.com/kwyvern-gh/RuneStone/releases/download/plugin-sdk-v0.1.2/runestone-plugin-sdk-0.1.2.tgz"
|
|
```
|
|
|
|
The result in `package.json`:
|
|
|
|
```json
|
|
{
|
|
"devDependencies": {
|
|
"@runestone/plugin-sdk": "https://github.com/kwyvern-gh/RuneStone/releases/download/plugin-sdk-v0.1.2/runestone-plugin-sdk-0.1.2.tgz"
|
|
}
|
|
}
|
|
```
|
|
|
|
Always give the full URL. The SDK is not on the npm registry, and another account owns the `@runestone` scope there.
|
|
The download needs no access token and no `.npmrc` file. npm accepts the same URL.
|
|
Your lock file records the integrity value of the package, so the installation stops if the file changes.
|
|
The package contains compiled JavaScript and declaration files, so the installation runs no build step.
|
|
|
|
Use TypeScript 5.0 or later, with `moduleResolution` set to `bundler`, `node16`, or `nodenext`. Include the `DOM` library.
|
|
|
|
## Write a plugin
|
|
|
|
A plugin is one JavaScript module that exports `activate(api)`. This example adds a view and a command that opens it.
|
|
|
|
```ts
|
|
import type { PluginApi } from "@runestone/plugin-sdk";
|
|
|
|
export function activate(api: PluginApi) {
|
|
api.registerView({
|
|
id: "hello",
|
|
title: "Hello",
|
|
mount(container) {
|
|
const text = document.createElement("p");
|
|
text.textContent = "Hello from a RuneStone plugin.";
|
|
container.append(text);
|
|
return () => text.remove();
|
|
},
|
|
});
|
|
api.registerCommand({ id: "show-hello", title: "Show hello", icon: "puzzle", run: () => api.openView("hello") });
|
|
}
|
|
```
|
|
|
|
Compile the module with a bundler, so the entry file contains the SDK helpers that it uses. RuneStone loads the entry from a Blob URL, so imports from `node_modules` do not resolve at run time.
|
|
Then make the plugin ZIP. See [Build and package a plugin](https://github.com/kwyvern-gh/RuneStone/blob/master/docs/plugin-development.md#build-and-package-a-plugin).
|
|
|
|
A type-only import, such as `import type { PluginApi }`, adds no code to the bundle.
|
|
|
|
## Check for features
|
|
|
|
Members marked optional in `PluginApi` can be absent on older RuneStone versions. Examples are `api.files`, `api.services`, and `api.workspace.readBytes`.
|
|
Make sure that a member exists before you use it.
|
|
|
|
```ts
|
|
import { hasFeature, requireFeature, requireFeatures, type PluginApi } from "@runestone/plugin-sdk";
|
|
|
|
export function activate(api: PluginApi) {
|
|
// Stop the activation with one message that names each missing feature.
|
|
requireFeatures(api, ["files", "workspace.readOptional"]);
|
|
const files = requireFeature(api, "files");
|
|
|
|
// Hide a control when the feature is absent.
|
|
if (hasFeature(api, "ui.showActionMenu")) showMenuButton();
|
|
}
|
|
```
|
|
|
|
| Function | Result |
|
|
| --- | --- |
|
|
| `hasFeature(api, feature)` | `true` when RuneStone gives the member |
|
|
| `missingFeatures(api, features)` | The features of the list that RuneStone does not give |
|
|
| `requireFeature(api, feature)` | The member, with its type. A function stays bound to its object. |
|
|
| `requireFeatures(api, features)` | Nothing. Throws one error for all missing features. |
|
|
|
|
When a feature is missing, `requireFeature` and `requireFeatures` throw a `MissingFeatureError`. Its `features` property lists the feature names.
|
|
Its message names each feature and its API path, and tells the user to update RuneStone. For example:
|
|
`This plugin needs the folder grant API (api.files). Update RuneStone to a version that has this feature.`
|
|
|
|
`pluginFeatures` lists all the feature names. The checks examine only whether the member exists.
|
|
|
|
## Icons and tooltips
|
|
|
|
RuneStone has no API for icons or tooltips. The SDK gives the host code as DOM functions, so your controls look and behave like the RuneStone controls.
|
|
|
|
```ts
|
|
import { actionIcon } from "@runestone/plugin-sdk/icons";
|
|
import { attachTooltip } from "@runestone/plugin-sdk/tooltip";
|
|
|
|
const button = document.createElement("button");
|
|
button.setAttribute("aria-label", "Refresh");
|
|
button.append(actionIcon("refresh"));
|
|
const removeTooltip = attachTooltip(button, () => "Refresh");
|
|
```
|
|
|
|
| Import | Contents |
|
|
| --- | --- |
|
|
| `@runestone/plugin-sdk/icons` | `actionIcons`, the SVG markup of each icon. `actionIcon(name)` makes an SVG element with the `action-icon` class. |
|
|
| `@runestone/plugin-sdk/tooltip` | `attachTooltip(element, label)` shows a tooltip on hover and keyboard focus. It returns a cleanup function. |
|
|
|
|
RuneStone styles the `action-icon` and `rs-tooltip` classes. Call the cleanup function of each tooltip when your view closes.
|
|
|
|
For menus, call `api.ui.showActionMenu`. RuneStone draws the menu, so the SDK contains only its types: `ActionMenuItem` and `ActionMenuOptions`.
|
|
|
|
## Versions
|
|
|
|
The SDK has its own version number in the form `zz.yy.xx`, for example `0.1.0`. Each release has the tag `plugin-sdk-v<version>` on the RuneStone releases page.
|
|
A hotfix adds a lowercase letter to the version, for example `0.1.0a`.
|
|
|
|
`API_VERSION` is the `apiVersion` value of `plugin.json`. The SDK version and the RuneStone version are different numbers.
|
|
A new SDK release can add types for new RuneStone features. Use the feature checks for features that older RuneStone versions do not have.
|
|
|
|
## Documentation and help
|
|
|
|
- [Plugin development reference](https://github.com/kwyvern-gh/RuneStone/blob/master/docs/plugin-development.md): the package format, the API, and the build procedure
|
|
- [Completion API](https://github.com/kwyvern-gh/RuneStone/blob/master/docs/completion.md)
|
|
- [Install and manage plugins](https://github.com/kwyvern-gh/RuneStone/blob/master/docs/plugins.md)
|
|
- [Release policy](https://github.com/kwyvern-gh/RuneStone/blob/master/docs/releases.md#sdk-releases)
|
|
|
|
The SDK source is in the `packages/plugin-sdk` folder of the [RuneStone repository](https://github.com/kwyvern-gh/RuneStone). Report problems there.
|
|
|
|
Knotty Wyvern maintains RuneStone and the SDK.
|
|
|
|
## License
|
|
|
|
Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
|