RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,2 @@
|
||||
RuneStone Plugin SDK
|
||||
Copyright 2026 Knotty Wyvern
|
||||
@@ -0,0 +1,142 @@
|
||||
# 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).
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "@runestone/plugin-sdk",
|
||||
"version": "0.1.2",
|
||||
"private": true,
|
||||
"description": "Types and helpers for RuneStone plugins",
|
||||
"license": "Apache-2.0",
|
||||
"type": "module",
|
||||
"sideEffects": false,
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
},
|
||||
"./icons": {
|
||||
"types": "./dist/icons.d.ts",
|
||||
"default": "./dist/icons.js"
|
||||
},
|
||||
"./tooltip": {
|
||||
"types": "./dist/tooltip.d.ts",
|
||||
"default": "./dist/tooltip.js"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"NOTICE"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import type { CompletionApi } from "./completion.ts";
|
||||
import type { Contributions } from "./contributions.ts";
|
||||
import type { ActionIcon } from "./icons.ts";
|
||||
import type { ActionMenuItem, ActionMenuOptions } from "./menu.ts";
|
||||
|
||||
/** Plugin API version 1. A compiled plugin exports `activate(api)`. */
|
||||
export type Dispose = () => void | Promise<void>;
|
||||
export interface PluginCommand { id: string; title: string; icon?: ActionIcon | PluginView["icon"]; category?: "open" | "action"; run: () => void | Promise<void> }
|
||||
export interface PluginView { id: string; title: string; settings?: boolean; launcher?: boolean; launch?: () => void | Promise<void>; placement?: "dialog" | "editor" | "sidebar"; navigation?: boolean; toolbar?: boolean; pinned?: boolean; icon?: "list" | "kanban" | "puzzle" | "globe" | "git"; iconImage?: string | null; tabActions?: (context: { pinned: boolean }) => ActionMenuItem[]; tabDragData?: (context: { pinned: boolean }) => string | undefined; find?: () => void; saveTabState?: () => unknown; restoreTabState?: (state: unknown) => void | Promise<void>; mount: (container: HTMLElement, context?: { surface: "settings" | "view" }) => void | Dispose | Promise<void | Dispose> }
|
||||
export interface PluginViewOpenOptions { split: "right" | "down"; relativeTo: string }
|
||||
export interface PluginDocumentContext { id: string; path: string }
|
||||
export interface PluginDocumentType {
|
||||
id: string;
|
||||
title: string;
|
||||
icon?: "kanban" | "file";
|
||||
extensions: string[];
|
||||
content?: "text" | "binary";
|
||||
identify(path: string, source: string): { id: string } | null;
|
||||
create?: { label: string; defaultName: string; extension: string; contents(): string };
|
||||
mount(container: HTMLElement, document: PluginDocumentContext): void | Dispose | Promise<void | Dispose>;
|
||||
}
|
||||
export interface FolderGrant { id: string; path: string; name: string }
|
||||
export interface FolderEntry { name: string; size: number }
|
||||
/** Folders outside the workspace that the user granted to this plugin. */
|
||||
export interface FilesApi {
|
||||
readonly version: 1;
|
||||
chooseFolder(options?: { title?: string }): Promise<FolderGrant | null>;
|
||||
pictureFolder(name: string): Promise<FolderGrant>;
|
||||
grants(): Promise<FolderGrant[]>;
|
||||
revoke(grant: string): Promise<boolean>;
|
||||
list(grant: string): Promise<FolderEntry[]>;
|
||||
readBytes(grant: string, name: string): Promise<Uint8Array>;
|
||||
readSidecar(grant: string, name: string): Promise<string | null>;
|
||||
writeSidecar(grant: string, name: string, contents: string, expected: string | null): Promise<void>;
|
||||
}
|
||||
export type ServiceMethods = Record<string, (...args: never[]) => unknown>;
|
||||
/** Data that one plugin offers to other plugins in the same window. */
|
||||
export interface ServicesApi {
|
||||
readonly version: 1;
|
||||
provide(name: string, methods: ServiceMethods): { notify(): void; dispose(): void };
|
||||
get(plugin: string, name: string): Record<string, (...args: unknown[]) => Promise<unknown>> | null;
|
||||
subscribe(listener: (plugin: string, name: string) => void): Dispose;
|
||||
}
|
||||
/** Files in the active workspace. All paths are relative to the workspace root. */
|
||||
export interface WorkspaceApi {
|
||||
list(path?: string): Promise<{ name: string; rel_path: string; kind: string }[]>;
|
||||
listMarkdown?(): Promise<string[]>;
|
||||
read(path: string): Promise<string>;
|
||||
readMany?(paths: string[]): Promise<{ path: string; source?: string; error?: string }[]>;
|
||||
readBytes?(path: string): Promise<Uint8Array>;
|
||||
readOptional?(path: string): Promise<string | null>;
|
||||
create?(path: string, contents: string): Promise<void>;
|
||||
write(path: string, contents: string, expected: string): Promise<void>;
|
||||
subscribe?(listener: () => void): Dispose;
|
||||
}
|
||||
/** The saved JSON settings of the plugin. */
|
||||
export interface SettingsApi { read(): Promise<unknown>; write(value: unknown): Promise<void>; subscribe?(listener: () => void): Dispose }
|
||||
/** The files in the plugin package. */
|
||||
export interface AssetsApi { read(path: string): Promise<Uint8Array> }
|
||||
/** Interface parts that the host draws. */
|
||||
export interface UiApi { showActionMenu(options: ActionMenuOptions): { close(): void } }
|
||||
export interface PluginApi {
|
||||
files?: FilesApi;
|
||||
services?: ServicesApi;
|
||||
ui?: UiApi;
|
||||
completion?: CompletionApi;
|
||||
readonly id: string;
|
||||
readonly signal: AbortSignal;
|
||||
contribute<K extends keyof Contributions>(key: K, value: Contributions[K]): void;
|
||||
registerCommand(command: PluginCommand): void;
|
||||
registerView(view: PluginView): void;
|
||||
registerTabDrop?(receive: (data: string) => void | Promise<void>): void;
|
||||
updateViewTitle?(id: string, title: string): void;
|
||||
updateViewIcon?(id: string, image: string | null): void;
|
||||
registerDocumentType(type: PluginDocumentType): void;
|
||||
openDocument(path: string): Promise<void>;
|
||||
openFile?(path: string): Promise<void>;
|
||||
renameDocument?(document: PluginDocumentContext, name: string): Promise<string>;
|
||||
openView(id: string, options?: PluginViewOpenOptions): void;
|
||||
closeView?(id: string): void;
|
||||
replaceView?(currentId: string, nextId: string): void;
|
||||
replaceDocumentWithView?(document: PluginDocumentContext, nextId: string): void;
|
||||
onDispose(cleanup: Dispose): void;
|
||||
workspace: WorkspaceApi;
|
||||
settings: SettingsApi;
|
||||
assets: AssetsApi;
|
||||
}
|
||||
/** The entry module of a plugin. */
|
||||
export interface PluginModule { activate(api: PluginApi): void | Dispose | Promise<void | Dispose> }
|
||||
@@ -0,0 +1,59 @@
|
||||
/** Autocomplete types. See the completion API in the RuneStone documentation (`docs/completion.md`). */
|
||||
export type CompletionDispose = () => void;
|
||||
export interface CompletionSnapshot {
|
||||
documentId: string;
|
||||
revision: string | number;
|
||||
text: string;
|
||||
caret: number;
|
||||
selectionEnd: number;
|
||||
focused: boolean;
|
||||
composing?: boolean;
|
||||
sensitive?: boolean;
|
||||
readOnly?: boolean;
|
||||
kind: "prose" | "code";
|
||||
language?: string;
|
||||
}
|
||||
export interface CompletionRequest {
|
||||
surfaceId: string;
|
||||
documentId: string;
|
||||
revision: string | number;
|
||||
prefix: string;
|
||||
suffix: string;
|
||||
kind: "prose" | "code";
|
||||
language: string;
|
||||
manual: boolean;
|
||||
signal: AbortSignal;
|
||||
}
|
||||
export interface CompletionProvider {
|
||||
id: string;
|
||||
delayMs?: number;
|
||||
timeoutMs?: number;
|
||||
complete(request: CompletionRequest, publish: (text: string) => void): Promise<string | null>;
|
||||
}
|
||||
export interface CompletionSurface {
|
||||
id: string;
|
||||
snapshot(): CompletionSnapshot;
|
||||
preview(text: string | null): void;
|
||||
insert(text: string, expected: CompletionSnapshot): boolean;
|
||||
}
|
||||
export interface CompletionHandle {
|
||||
changed(): void;
|
||||
request(): void;
|
||||
accept(word?: boolean): boolean;
|
||||
dismiss(): boolean;
|
||||
dispose(): void;
|
||||
}
|
||||
export interface CompletionTransportRequest {
|
||||
endpoint: string;
|
||||
route: "/v1/models" | "/v1/chat/completions" | "/api/tags" | "/api/generate";
|
||||
body?: string;
|
||||
timeoutMs?: number;
|
||||
}
|
||||
export interface CompletionApi {
|
||||
version: 1;
|
||||
registerProvider(provider: CompletionProvider): CompletionDispose;
|
||||
registerSurface(surface: CompletionSurface): CompletionHandle;
|
||||
registerInput(element: HTMLInputElement | HTMLTextAreaElement, options: { id: string; kind?: "prose" | "code"; sensitive?: boolean }): CompletionHandle;
|
||||
invalidate(): void;
|
||||
transport?(request: CompletionTransportRequest, signal: AbortSignal, onLine: (line: string) => void): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { Dispose } from "./api.ts";
|
||||
|
||||
/** Host features that `api.contribute(key, value)` registers. Only one enabled plugin can own each key. */
|
||||
export interface Contributions {
|
||||
browser: {
|
||||
/** Opens a browser tab, with `url` when it is given. */
|
||||
newTab?(url?: string): Promise<void>;
|
||||
mountSettings(container: HTMLElement): Dispose | Promise<Dispose>;
|
||||
mountBookmarks(container: HTMLElement): Dispose | Promise<Dispose>;
|
||||
/** Opens a web link from a document. False means that the system browser opens it. */
|
||||
open(url: string): Promise<boolean>;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import type { FilesApi, PluginApi, ServicesApi, SettingsApi, UiApi, WorkspaceApi } from "./api.ts";
|
||||
import type { CompletionApi } from "./completion.ts";
|
||||
|
||||
/**
|
||||
* The optional members of `PluginApi`, by path, and the type that `requireFeature` returns for each path.
|
||||
* Older RuneStone hosts can omit these members. Required members, such as `workspace.read`, are not listed.
|
||||
*/
|
||||
export interface PluginFeatures {
|
||||
files: FilesApi;
|
||||
services: ServicesApi;
|
||||
completion: CompletionApi;
|
||||
"completion.transport": NonNullable<CompletionApi["transport"]>;
|
||||
"ui.showActionMenu": UiApi["showActionMenu"];
|
||||
registerTabDrop: NonNullable<PluginApi["registerTabDrop"]>;
|
||||
updateViewTitle: NonNullable<PluginApi["updateViewTitle"]>;
|
||||
updateViewIcon: NonNullable<PluginApi["updateViewIcon"]>;
|
||||
openFile: NonNullable<PluginApi["openFile"]>;
|
||||
renameDocument: NonNullable<PluginApi["renameDocument"]>;
|
||||
closeView: NonNullable<PluginApi["closeView"]>;
|
||||
replaceView: NonNullable<PluginApi["replaceView"]>;
|
||||
replaceDocumentWithView: NonNullable<PluginApi["replaceDocumentWithView"]>;
|
||||
"workspace.listMarkdown": NonNullable<WorkspaceApi["listMarkdown"]>;
|
||||
"workspace.readMany": NonNullable<WorkspaceApi["readMany"]>;
|
||||
"workspace.readBytes": NonNullable<WorkspaceApi["readBytes"]>;
|
||||
"workspace.readOptional": NonNullable<WorkspaceApi["readOptional"]>;
|
||||
"workspace.create": NonNullable<WorkspaceApi["create"]>;
|
||||
"workspace.subscribe": NonNullable<WorkspaceApi["subscribe"]>;
|
||||
"settings.subscribe": NonNullable<SettingsApi["subscribe"]>;
|
||||
}
|
||||
export type PluginFeature = keyof PluginFeatures;
|
||||
|
||||
const descriptions: { [K in PluginFeature]: string } = {
|
||||
files: "the folder grant API",
|
||||
services: "the plugin service API",
|
||||
completion: "the completion API",
|
||||
"completion.transport": "the completion model transport",
|
||||
"ui.showActionMenu": "the shared action menu",
|
||||
registerTabDrop: "tab drops from other windows",
|
||||
updateViewTitle: "view title changes",
|
||||
updateViewIcon: "view tab icons",
|
||||
openFile: "opening files in the RuneStone editor",
|
||||
renameDocument: "document renames",
|
||||
closeView: "closing views",
|
||||
replaceView: "view replacement",
|
||||
replaceDocumentWithView: "replacing a document tab with a view",
|
||||
"workspace.listMarkdown": "the Markdown file list",
|
||||
"workspace.readMany": "reading many workspace files in one request",
|
||||
"workspace.readBytes": "reading workspace files as bytes",
|
||||
"workspace.readOptional": "reading optional workspace files",
|
||||
"workspace.create": "creating workspace files",
|
||||
"workspace.subscribe": "workspace change notifications",
|
||||
"settings.subscribe": "settings change notifications",
|
||||
};
|
||||
|
||||
/** All the feature paths that `hasFeature` and `requireFeature` accept. */
|
||||
export const pluginFeatures = Object.freeze(Object.keys(descriptions) as PluginFeature[]);
|
||||
|
||||
/** A readable name of a feature, with its API path, for messages to users. */
|
||||
export function describeFeature(feature: PluginFeature): string {
|
||||
if (!Object.hasOwn(descriptions, feature)) throw new TypeError(`Unknown plugin feature: ${String(feature)}`);
|
||||
return `${descriptions[feature]} (api.${feature})`;
|
||||
}
|
||||
|
||||
/** The error that `requireFeature` and `requireFeatures` throw when the host does not have a feature. */
|
||||
export class MissingFeatureError extends Error {
|
||||
readonly features: readonly PluginFeature[];
|
||||
constructor(features: readonly PluginFeature[]) {
|
||||
const names = features.map(describeFeature);
|
||||
super(`This plugin needs ${names.length > 1 ? names.slice(0, -1).join(", ") + " and " + names.at(-1) : names[0]}. Update RuneStone to a version that has ${names.length > 1 ? "these features" : "this feature"}.`);
|
||||
this.name = "MissingFeatureError";
|
||||
this.features = Object.freeze([...features]);
|
||||
}
|
||||
}
|
||||
|
||||
function lookup(api: PluginApi, feature: PluginFeature): { parent: unknown; value: unknown } {
|
||||
describeFeature(feature);
|
||||
let parent: unknown;
|
||||
let value: unknown = api;
|
||||
for (const key of feature.split(".")) {
|
||||
parent = value;
|
||||
value = parent !== null && typeof parent === "object" ? (parent as Record<string, unknown>)[key] : undefined;
|
||||
}
|
||||
return { parent, value };
|
||||
}
|
||||
|
||||
/** Returns `true` when the host gives this optional member of the plugin API. */
|
||||
export function hasFeature(api: PluginApi, feature: PluginFeature): boolean {
|
||||
const { value } = lookup(api, feature);
|
||||
return typeof value === "function" || (value !== null && typeof value === "object");
|
||||
}
|
||||
|
||||
/** Returns the features in the list that the host does not give, in list order. */
|
||||
export function missingFeatures(api: PluginApi, features: readonly PluginFeature[]): PluginFeature[] {
|
||||
return features.filter(feature => !hasFeature(api, feature));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the member at the feature path, or throws a `MissingFeatureError` that tells the user to update RuneStone.
|
||||
* A returned function stays bound to its parent object.
|
||||
*/
|
||||
export function requireFeature<F extends PluginFeature>(api: PluginApi, feature: F): PluginFeatures[F] {
|
||||
const { parent, value } = lookup(api, feature);
|
||||
if (typeof value === "function") return value.bind(parent) as PluginFeatures[F];
|
||||
if (value !== null && typeof value === "object") return value as PluginFeatures[F];
|
||||
throw new MissingFeatureError([feature]);
|
||||
}
|
||||
|
||||
/** Throws one `MissingFeatureError` that names every missing feature in the list. */
|
||||
export function requireFeatures(api: PluginApi, features: readonly PluginFeature[]): void {
|
||||
const missing = missingFeatures(api, features);
|
||||
if (missing.length) throw new MissingFeatureError(missing);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// The action icons of RuneStone. Menus, commands, and plugin controls use the same artwork.
|
||||
// Stroke icons share the same 24px grid and weight as the host icon buttons.
|
||||
// The host styles the `action-icon` class: size, stroke, and color. Outside RuneStone, add your own style.
|
||||
export const actionIcons = {
|
||||
play: '<path d="m7 4 14 8-14 8Z"/>',
|
||||
pause: '<path d="M7 4v16M17 4v16"/>',
|
||||
'skip-forward': '<path d="m4 4 12 8-12 8ZM20 4v16"/>',
|
||||
eye: '<path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12Z"/><circle cx="12" cy="12" r="3"/>',
|
||||
'eye-off': '<path d="m3 3 18 18M10.6 5.1 12 5c6.5 0 10 7 10 7a18 18 0 0 1-3.1 3.8M6.2 6.2A21 21 0 0 0 2 12s3.5 7 10 7a13 13 0 0 0 5.8-1.4M9.9 9.9a3 3 0 0 0 4.2 4.2"/>',
|
||||
image: '<rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8" cy="8" r="1.5"/><path d="m21 15-5-5L5 21"/>',
|
||||
settings: '<path d="M4 7h16M4 17h16"/><circle cx="9" cy="7" r="3"/><circle cx="15" cy="17" r="3"/>',
|
||||
archive: '<path d="M4 8v12h16V8M3 3h18v5H3zM9 12h6"/>',
|
||||
save: '<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><path d="M17 21v-8H7v8M7 3v5h8"/>',
|
||||
refresh: '<path d="M20 7v5h-5M4 17v-5h5M6 7a7 7 0 0 1 12-1l2 3M4 15l2 3a7 7 0 0 0 12-1"/>',
|
||||
'arrow-left': '<path d="M20 12H4m6-6-6 6 6 6"/>',
|
||||
'arrow-right': '<path d="M4 12h16m-6-6 6 6-6 6"/>',
|
||||
terminal: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="m6 8 4 4-4 4m6 0h6"/>',
|
||||
undo: '<path d="M3 10h11a7 7 0 0 1 0 14" transform="translate(0 -4)"/><path d="m7 2-4 4 4 4"/>',
|
||||
redo: '<path d="M21 6H10a7 7 0 0 0 0 14"/><path d="m17 2 4 4-4 4"/>',
|
||||
git: '<circle cx="7" cy="5" r="2"/><circle cx="7" cy="19" r="2"/><circle cx="17" cy="6" r="2"/><path d="M7 7v10m0-5h4a6 6 0 0 0 6-6"/>',
|
||||
globe: '<circle cx="12" cy="12" r="9"/><ellipse cx="12" cy="12" rx="4" ry="9"/><path d="M3 12h18"/>',
|
||||
puzzle: '<path d="M9 3H4v6a3 3 0 1 1 0 6v5h6a3 3 0 1 1 6 0h4v-6a3 3 0 1 0 0-6V3h-5a3 3 0 1 1-6 0Z"/>',
|
||||
'split-right': '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M12 4v16m3-11 3 3-3 3"/>',
|
||||
'split-down': '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M3 12h18m-12 3 3 3 3-3"/>',
|
||||
kanban: '<rect x="3" y="4" width="5" height="16" rx="1"/><rect x="10" y="4" width="5" height="11" rx="1"/><rect x="17" y="4" width="4" height="7" rx="1"/>',
|
||||
bookmark: '<path d="M6 4h12v16l-6-3-6 3z"/>',
|
||||
'file-plus': '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8zM14 2v6h6M12 18v-6M9 15h6"/>',
|
||||
'folder-plus': '<path d="M4 20h16a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.7-.9L9.2 3.9A2 2 0 0 0 7.5 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2zM12 10v6M9 13h6"/>',
|
||||
|
||||
pin: '<path d="m16 3 5 5-4 1-3 3 1 4-2 2-7-7 2-2 4 1 3-3ZM8 16l-5 5"/>',
|
||||
book: '<path d="M12 6v15M3 3h5a4 4 0 0 1 4 3 4 4 0 0 1 4-3h5v15h-5a4 4 0 0 0-4 3 4 4 0 0 0-4-3H3Z"/>',
|
||||
folder: '<path d="M3 7V4h6l3 3h9v13H3Z"/>',
|
||||
rename: '<path d="M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z"/>',
|
||||
'zoom-in': '<circle cx="10" cy="10" r="7"/><path d="m15 15 6 6M7 10h6M10 7v6"/>',
|
||||
'zoom-out': '<circle cx="10" cy="10" r="7"/><path d="m15 15 6 6M7 10h6"/>',
|
||||
close: '<path d="m6 6 12 12M6 18 18 6"/>',
|
||||
swap: '<path d="M3 7h18m-4-4 4 4-4 4M21 17H3m4-4-4 4 4 4"/>',
|
||||
bullet: '<path d="M9 5h12M9 12h12M9 19h12"/><circle cx="3" cy="5" r="1"/><circle cx="3" cy="12" r="1"/><circle cx="3" cy="19" r="1"/>',
|
||||
number: '<path d="M10 5h11M10 12h11M10 19h11M3 3h2v6M3 9h4M3 14c4-3 5 1 2 3l-2 3h4"/>',
|
||||
task: '<path d="M20 12v8H4V4h10M9 10l4 4 8-11"/>',
|
||||
heading: '<path d="M5 4v16M19 4v16M5 12h14"/>',
|
||||
quote: '<path d="M3 4h7v9c0 4-2 6-6 7v-3c2-1 3-2 3-4H3ZM14 4h7v9c0 4-2 6-6 7v-3c2-1 3-2 3-4h-4Z"/>',
|
||||
table: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M3 15h18M9 3v18M15 3v18"/>',
|
||||
rule: '<path d="M4 12h16"/>',
|
||||
footnote: '<path d="M4 4h9v16H4ZM7 8h3M7 12h3M16 4l2-1v6M16 9h4"/>',
|
||||
cut: '<circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="m8 8 12 12M8 16 20 4"/>',
|
||||
copy: '<rect x="8" y="8" width="12" height="13" rx="2"/><path d="M16 8V3H3v13h5"/>',
|
||||
duplicate: '<rect x="8" y="8" width="12" height="13" rx="2"/><path d="M16 8V3H3v13h5M14 11.5v6M11 14.5h6"/>',
|
||||
template: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M9 9v12"/>',
|
||||
tab: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M3 9h7V4"/>',
|
||||
paste: '<rect x="5" y="5" width="14" height="16" rx="2"/><rect x="9" y="2" width="6" height="5" rx="1"/><path d="m9 14 2 2 4-4"/>',
|
||||
'paste-text': '<rect x="5" y="5" width="14" height="16" rx="2"/><path d="M9 5V2h6v3M8 11h8M12 11v7"/>',
|
||||
format: '<path d="m14 3 7 7-10 10H4v-7ZM11 6l7 7M3 21h18"/>',
|
||||
bold: '<path d="M6 4h7a4 4 0 0 1 0 8H6zm0 8h8a4 4 0 0 1 0 8H6z"/>',
|
||||
italic: '<path d="M10 4h10M4 20h10M15 4 9 20"/>',
|
||||
strike: '<path d="M17 5c-2-3-10-3-10 2 0 2 2 3 5 4M7 19c3 3 10 2 10-2 0-2-2-3-5-4M3 12h18"/>',
|
||||
highlight: '<path d="m15 3 6 6-9 9-6-6ZM6 12l-3 6 3 3 6-3M3 21h12"/>',
|
||||
code: '<path d="m7 6-5 6 5 6m10-12 5 6-5 6M14 3l-4 18"/>',
|
||||
math: '<path d="M19 4H5l7 8-7 8h14"/>',
|
||||
comment: '<circle cx="6" cy="6" r="3"/><circle cx="18" cy="18" r="3"/><path d="M4 20 20 4"/>',
|
||||
clear: '<path d="m14 3 7 7-11 11H6l-5-5ZM7 10l7 7M13 21h9"/>',
|
||||
left: '<path d="M3 5h18M3 10h12M3 15h18M3 20h12"/>',
|
||||
center: '<path d="M3 5h18M6 10h12M3 15h18M6 20h12"/>',
|
||||
right: '<path d="M3 5h18M9 10h12M3 15h18M9 20h12"/>',
|
||||
row: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M3 15h18M9 3v18M15 3v18"/><path d="M4 10h16v4H4Z" fill="currentColor" opacity=".25"/>',
|
||||
column: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M3 15h18M9 3v18M15 3v18"/><path d="M10 4h4v16h-4Z" fill="currentColor" opacity=".25"/>',
|
||||
'row-before': '<rect x="3" y="10" width="18" height="11" rx="2"/><path d="M3 15h18M12 2v6M9 5h6"/>',
|
||||
'row-after': '<rect x="3" y="3" width="18" height="11" rx="2"/><path d="M3 8h18M12 16v6M9 19h6"/>',
|
||||
'column-before': '<rect x="10" y="3" width="11" height="18" rx="2"/><path d="M15 3v18M2 12h6M5 9v6"/>',
|
||||
'column-after': '<rect x="3" y="3" width="11" height="18" rx="2"/><path d="M8 3v18M16 12h6M19 9v6"/>',
|
||||
up: '<path d="M12 21V3m-7 7 7-7 7 7"/>',
|
||||
down: '<path d="M12 3v18m-7-7 7 7 7-7"/>',
|
||||
'move-left': '<path d="M21 12H3m7-7-7 7 7 7"/>',
|
||||
'move-right': '<path d="M3 12h18m-7-7 7 7-7 7"/>',
|
||||
trash: '<path d="M3 6h18M9 6V3h6v3M5 6l1 15h12l1-15M10 10v7M14 10v7"/>',
|
||||
'sort-asc': '<path d="M5 3v18m-3-3 3 3 3-3M12 4h4M12 9h6M12 14h8M12 19h10"/>',
|
||||
'sort-desc': '<path d="M5 3v18m-3-3 3 3 3-3M12 4h10M12 9h8M12 14h6M12 19h4"/>',
|
||||
link: '<path d="m10 13 4-4M8 16l-2 2a4 4 0 0 1-6-6l5-5a4 4 0 0 1 6 0m2 1 2-2a4 4 0 0 1 6 6l-5 5a4 4 0 0 1-6 0" transform="translate(1 0)"/>',
|
||||
external: '<path d="M14 3h7v7M10 14 21 3M10 5H4v16h16v-7"/>',
|
||||
search: '<circle cx="10" cy="10" r="7"/><path d="m15 15 6 6"/>',
|
||||
filter: '<path d="M3 4h18l-7 8v7l-4 2v-9Z"/>',
|
||||
extract: '<path d="M11 4H4v16h16v-7M14 3h7v7M11 13 21 3"/><path d="M7 8h2M7 12h2M7 16h7"/>',
|
||||
select: '<path d="M8 3H3v5M16 3h5v5M21 16v5h-5M8 21H3v-5M3 11v2M21 11v2M11 3h2M11 21h2"/>',
|
||||
paragraph: '<path d="M13 21V3h-3a5 5 0 0 0 0 10h3M18 3v18M10 3h12"/>',
|
||||
insert: '<path d="M3 5h18M3 11h8M3 17h8M17 11v10M12 16h10"/>',
|
||||
chevron: '<path d="m9 5 7 7-7 7"/>',
|
||||
plus: '<path d="M12 5v14M5 12h14"/>',
|
||||
grip: '<circle cx="8" cy="5" r="1"/><circle cx="16" cy="5" r="1"/><circle cx="8" cy="12" r="1"/><circle cx="16" cy="12" r="1"/><circle cx="8" cy="19" r="1"/><circle cx="16" cy="19" r="1"/>',
|
||||
} as const;
|
||||
export type ActionIcon = keyof typeof actionIcons;
|
||||
|
||||
/** Makes a new SVG element for an action icon. The element has `aria-hidden`, so give its control an accessible name. */
|
||||
export function actionIcon(name: ActionIcon): SVGSVGElement {
|
||||
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
||||
svg.setAttribute("viewBox", "0 0 24 24"); svg.setAttribute("aria-hidden", "true");
|
||||
svg.classList.add("action-icon"); svg.innerHTML = actionIcons[name];
|
||||
return svg;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// The public types and helpers of the RuneStone plugin API.
|
||||
// The icon and tooltip helpers use the DOM. Import them from "@runestone/plugin-sdk/icons" and "@runestone/plugin-sdk/tooltip".
|
||||
export type * from "./api.ts";
|
||||
export type * from "./completion.ts";
|
||||
export type * from "./contributions.ts";
|
||||
export type * from "./menu.ts";
|
||||
export type { ActionIcon } from "./icons.ts";
|
||||
export { API_VERSION, type PluginManifest } from "./manifest.ts";
|
||||
export { describeFeature, hasFeature, missingFeatures, MissingFeatureError, pluginFeatures, requireFeature, requireFeatures, type PluginFeature, type PluginFeatures } from "./features.ts";
|
||||
@@ -0,0 +1,8 @@
|
||||
/** The plugin API version that this SDK describes. It is the `apiVersion` value in `plugin.json`. */
|
||||
export const API_VERSION = 1;
|
||||
|
||||
/** The contents of `plugin.json` at the root of a plugin ZIP. The host rejects other fields. */
|
||||
export interface PluginManifest {
|
||||
schemaVersion: 1; id: string; name: string; author: string; description: string;
|
||||
version: string; apiVersion: typeof API_VERSION; entry: string; access: "full-app";
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { ActionIcon } from "./icons.ts";
|
||||
|
||||
/** One row of the shared RuneStone action menu, or a separator. */
|
||||
export type ActionMenuItem = { label: string; icon?: ActionIcon; shortcut?: string; run?: () => void; children?: ActionMenuItem[]; disabled?: boolean; checked?: boolean; radio?: boolean; title?: string } | { separator: true };
|
||||
|
||||
/** The options of `api.ui.showActionMenu`. `x` and `y` are viewport coordinates. */
|
||||
export type ActionMenuOptions = { x: number; y: number; items: ActionMenuItem[]; label?: string; parent?: HTMLElement };
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* A single themed tooltip for DOM controls, with hover and keyboard dismissal.
|
||||
* The host styles the `rs-tooltip` class. Returns a cleanup function that removes the tooltip and its listeners.
|
||||
*/
|
||||
export function attachTooltip(target: HTMLElement, label: () => string) {
|
||||
const tip = document.createElement('span');
|
||||
tip.className = 'rs-tooltip';
|
||||
tip.setAttribute('popover', 'manual');
|
||||
tip.setAttribute('role', 'tooltip');
|
||||
tip.id = `rs-tooltip-${crypto.randomUUID()}`;
|
||||
document.body.append(tip);
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const previousDescription = target.getAttribute('aria-describedby');
|
||||
function hide() {
|
||||
clearTimeout(timer);
|
||||
if (tip.matches(':popover-open')) tip.hidePopover();
|
||||
if (previousDescription) target.setAttribute('aria-describedby', previousDescription);
|
||||
else target.removeAttribute('aria-describedby');
|
||||
}
|
||||
function show() {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
if (!target.isConnected || !label()) return;
|
||||
tip.textContent = label();
|
||||
tip.showPopover();
|
||||
const bounds = target.getBoundingClientRect(), size = tip.getBoundingClientRect();
|
||||
tip.style.left = `${Math.max(8, Math.min(bounds.left, innerWidth - size.width - 8))}px`;
|
||||
tip.style.top = `${Math.max(8, bounds.bottom + size.height + 8 > innerHeight ? bounds.top - size.height - 8 : bounds.bottom + 8)}px`;
|
||||
target.setAttribute('aria-describedby', [previousDescription, tip.id].filter(Boolean).join(' '));
|
||||
}, 300);
|
||||
}
|
||||
const escape = (event: KeyboardEvent) => { if (event.key === 'Escape') hide(); };
|
||||
target.addEventListener('mouseenter', show);
|
||||
target.addEventListener('mouseleave', hide);
|
||||
target.addEventListener('focusin', show);
|
||||
target.addEventListener('focusout', hide);
|
||||
target.addEventListener('pointerdown', hide);
|
||||
window.addEventListener('resize', hide);
|
||||
window.addEventListener('scroll', hide, true);
|
||||
window.addEventListener('keydown', escape);
|
||||
return () => {
|
||||
hide(); tip.remove();
|
||||
target.removeEventListener('mouseenter', show);
|
||||
target.removeEventListener('mouseleave', hide);
|
||||
target.removeEventListener('focusin', show);
|
||||
target.removeEventListener('focusout', hide);
|
||||
target.removeEventListener('pointerdown', hide);
|
||||
window.removeEventListener('resize', hide);
|
||||
window.removeEventListener('scroll', hide, true);
|
||||
window.removeEventListener('keydown', escape);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
// Builds dist/ with `pnpm sdk:build`. `pnpm check` runs this configuration with --noEmit.
|
||||
// The source must stay erasable, because the host unit tests load it with node --experimental-strip-types.
|
||||
"compilerOptions": {
|
||||
"target": "es2022",
|
||||
"module": "nodenext",
|
||||
"lib": ["es2022", "dom", "dom.iterable"],
|
||||
"types": [],
|
||||
"strict": true,
|
||||
"declaration": true,
|
||||
"rootDir": "src",
|
||||
"outDir": "dist",
|
||||
"rewriteRelativeImportExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"isolatedModules": true,
|
||||
"erasableSyntaxOnly": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in New Issue
Block a user