RuneStone 1.0.0

This commit is contained in:
Knotty Wyvern
2026-11-03 00:23:07 -07:00
commit 5557443ed1
818 changed files with 239368 additions and 0 deletions
+89
View File
@@ -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> }
+59
View File
@@ -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>;
}
+13
View File
@@ -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>;
};
}
+112
View File
@@ -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);
}
+98
View File
@@ -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;
}
+9
View File
@@ -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";
+8
View File
@@ -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";
}
+7
View File
@@ -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 };
+52
View File
@@ -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);
};
}