import type { BackgroundApi } from "./backgrounds.ts"; 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; export interface PluginCommand { id: string; title: string; icon?: ActionIcon | PluginView["icon"]; category?: "open" | "action"; run: () => void | Promise } export interface PluginView { id: string; title: string; settings?: boolean; launcher?: boolean; launch?: () => void | Promise; 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; mount: (container: HTMLElement, context?: { surface: "settings" | "view" }) => void | Dispose | Promise } 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; } 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; pictureFolder(name: string): Promise; grants(): Promise; revoke(grant: string): Promise; list(grant: string): Promise; readBytes(grant: string, name: string): Promise; readSidecar(grant: string, name: string): Promise; writeSidecar(grant: string, name: string, contents: string, expected: string | null): Promise; } export type ServiceMethods = Record 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 Promise> | 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; read(path: string): Promise; readMany?(paths: string[]): Promise<{ path: string; source?: string; error?: string }[]>; readBytes?(path: string): Promise; readOptional?(path: string): Promise; create?(path: string, contents: string): Promise; write(path: string, contents: string, expected: string): Promise; subscribe?(listener: () => void): Dispose; } /** The saved JSON settings of the plugin. */ export interface SettingsApi { read(): Promise; write(value: unknown): Promise; subscribe?(listener: () => void): Dispose } /** The files in the plugin package. */ export interface AssetsApi { read(path: string): Promise } /** Interface parts that the host draws. */ export interface UiApi { showActionMenu(options: ActionMenuOptions): { close(): void } } export interface PluginApi { files?: FilesApi; services?: ServicesApi; backgrounds?: BackgroundApi; ui?: UiApi; completion?: CompletionApi; readonly id: string; readonly signal: AbortSignal; contribute(key: K, value: Contributions[K]): void; registerCommand(command: PluginCommand): void; registerView(view: PluginView): void; registerTabDrop?(receive: (data: string) => void | Promise): void; updateViewTitle?(id: string, title: string): void; updateViewIcon?(id: string, image: string | null): void; registerDocumentType(type: PluginDocumentType): void; openDocument(path: string): Promise; openFile?(path: string): Promise; renameDocument?(document: PluginDocumentContext, name: string): Promise; 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 }