/** * @file * * Mock of Obsidian's `Plugin`, the base class every community plugin extends. */ import type { BasesViewRegistration as BasesViewRegistrationOriginal, CliFlags as CliFlagsOriginal, CliHandler as CliHandlerOriginal, Command as CommandOriginal, EditorSuggest as EditorSuggestOriginal, HoverLinkSource as HoverLinkSourceOriginal, MarkdownPostProcessorContext as MarkdownPostProcessorContextOriginal, MarkdownPostProcessor as MarkdownPostProcessorOriginal, ObsidianProtocolHandler as ObsidianProtocolHandlerOriginal, PluginManifest as PluginManifestOriginal, Plugin as PluginOriginal, PluginSettingTab as PluginSettingTabOriginal, ViewCreator as ViewCreatorOriginal } from 'obsidian'; import type { App } from './App.mjs'; import { Component } from './Component.mjs'; /** * Mock of Obsidian's `Plugin` base class. * * Nothing is registered with the app: every `add*` and `register*` call is recorded in a mock-only collection on the * plugin (such as {@link Plugin.commands__} or {@link Plugin.views__}) so a test can inspect it, and * {@link Plugin.loadData} / {@link Plugin.saveData} read and write {@link Plugin.data__} in memory instead of * `data.json`. */ export declare abstract class Plugin extends Component { /** * The app instance the plugin runs in. */ app: App; /** * Mock-only: the Bases view registrations passed to {@link Plugin.registerBasesView}, keyed by view id. */ basesViewRegistrations__: Map; /** * Mock-only: the handlers passed to {@link Plugin.registerCliHandler}, keyed by command id. */ cliHandlers__: Map; /** * Mock-only: the commands passed to {@link Plugin.addCommand}, keyed by their unprefixed id. */ commands__: Map; /** * Mock-only: the in-memory stand-in for the plugin's `data.json`, returned by {@link Plugin.loadData} and replaced * by {@link Plugin.saveData} (an empty object initially). */ data__: unknown; /** * Mock-only: the CodeMirror extensions passed to {@link Plugin.registerEditorExtension}, in registration order. */ editorExtensions__: unknown[]; /** * Mock-only: the editor suggests passed to {@link Plugin.registerEditorSuggest}, in registration order. */ editorSuggests__: EditorSuggestOriginal[]; /** * Mock-only: the file extensions passed to {@link Plugin.registerExtensions}, each mapped to its view type. */ extensions__: Map; /** * Mock-only: the hover link sources passed to {@link Plugin.registerHoverLinkSource}, keyed by id. */ hoverLinkSources__: Map; /** * The plugin's manifest, as read from its `manifest.json`. */ manifest: PluginManifestOriginal; /** * Mock-only: the code block handlers passed to {@link Plugin.registerMarkdownCodeBlockProcessor}, keyed by * language. */ markdownCodeBlockProcessors__: Map unknown>; /** * Mock-only: the post processors registered directly or created for code block processors, in registration order. */ markdownPostProcessors__: MarkdownPostProcessorOriginal[]; /** * Mock-only: the `obsidian://` handlers passed to {@link Plugin.registerObsidianProtocolHandler}, keyed by action. */ obsidianProtocolHandlers__: Map; /** * Mock-only: the elements returned by {@link Plugin.addRibbonIcon}, in creation order. */ ribbonActions__: HTMLElement[]; /** * The plugin's settings. Obsidian leaves it for the plugin to assign, typically from loaded data in `onload`. */ settings?: unknown; /** * Mock-only: the tabs passed to {@link Plugin.addSettingTab}, in registration order. */ settingTabs__: PluginSettingTabOriginal[]; /** * Mock-only: the elements returned by {@link Plugin.addStatusBarItem}, in creation order. */ statusBarItems__: HTMLElement[]; /** * Mock-only: the view creators passed to {@link Plugin.registerView}, keyed by view type. */ views__: Map; /** * Creates the plugin. Obsidian constructs it when the plugin is loaded, before calling `onload`. * * @param app - The app instance. * @param manifest - The plugin's manifest. */ constructor(app: App, manifest: PluginManifestOriginal); /** * Mock-only: views a value typed as Obsidian's `Plugin` as this mock. * * @param value - The value typed as the original `Plugin`. * @returns The same object, typed as the mock. */ static fromOriginalType2__(value: PluginOriginal): Plugin; /** * Registers a command globally, making it available in the command palette. Obsidian prefixes the id and name with * the plugin's; the mock stores the command unchanged in {@link Plugin.commands__}, replacing one with the same id. * * @param command - The command to register. * @returns The same command object. */ addCommand(command: CommandOriginal): CommandOriginal; /** * Adds an icon to the left ribbon. The mock creates a detached element, records it in * {@link Plugin.ribbonActions__}, and never calls the callback. * * @param _icon - The icon name. * @param _title - The tooltip text. * @param _callback - The click callback. * @returns The ribbon icon element. */ addRibbonIcon(_icon: string, _title: string, _callback: ($event: MouseEvent) => unknown): HTMLElement; /** * Registers a settings tab that lets users change the plugin's settings. The mock records it in * {@link Plugin.settingTabs__}. * * @param settingTab - The settings tab. */ addSettingTab(settingTab: PluginSettingTabOriginal): void; /** * Adds an item to the status bar at the bottom of the app (not available on mobile). The mock creates a detached * element and records it in {@link Plugin.statusBarItems__}. * * @returns The status bar item element, for the plugin to fill. */ addStatusBarItem(): HTMLElement; /** * Mock-only: views this mock as Obsidian's `Plugin` type. * * @returns The same object, typed as the original `Plugin`. */ asOriginalType2__(): PluginOriginal; /** * Mock-only construction hook, called at the end of the constructor; a no-op meant for * `vi.spyOn(Plugin.prototype, 'constructor2__')`. * * @param _app - The app the plugin was created with. * @param _manifest - The manifest the plugin was created with. */ constructor2__(_app: App, _manifest: PluginManifestOriginal): void; /** * Loads the plugin's settings data, which Obsidian reads from `data.json` in the plugin folder. * * @returns A promise resolving to {@link Plugin.data__}. */ loadData(): Promise; /** * Called when `data.json` is modified on disk outside Obsidian, for example by a sync service; plugins override it * to reload their settings. A no-op by default, and the mock never calls it. */ onExternalSettingsChange(): void; /** * Called once the user has explicitly enabled the plugin, so it is safe to engage with them (for example, to open * a custom view). A no-op by default, and the mock never calls it. */ onUserEnable(): void; /** * Registers a Bases view handler that renders data from property queries. The mock records it in * {@link Plugin.basesViewRegistrations__}. * * @param viewId - The view id. * @param registration - The view registration. * @returns Always `true` in the mock; Obsidian returns `false` when Bases is not enabled in the vault. */ registerBasesView(viewId: string, registration: BasesViewRegistrationOriginal): boolean; /** * Registers a handler for a command invoked from the Obsidian CLI. Obsidian throws when the command id is already * registered; the mock records the handler in {@link Plugin.cliHandlers__}, replacing any previous one. * * @param command - The globally unique command id. * @param _description - The help text shown for the command. * @param _flags - The command-line flags the command accepts, or `null`. * @param handler - The callback that handles an invocation. */ registerCliHandler(command: string, _description: string, _flags: CliFlagsOriginal | null, handler: CliHandlerOriginal): void; /** * Registers a CodeMirror 6 extension, or an array of them, for the editor. The mock records it in * {@link Plugin.editorExtensions__}. * * @param extension - The extension or array of extensions. */ registerEditorExtension(extension: unknown): void; /** * Registers an editor suggest that offers live suggestions while the user types. The mock records it in * {@link Plugin.editorSuggests__}. * * @param editorSuggest - The editor suggest. */ registerEditorSuggest(editorSuggest: EditorSuggestOriginal): void; /** * Associates file extensions with a view type, in `App.viewRegistry` as Obsidian does — so `WorkspaceLeaf.openFile` * opens such a file in that view — and unregisters them when the plugin unloads. The mock also maps each extension * to `viewType` in {@link Plugin.extensions__}. * * @param extensions - The file extensions, without the leading dot. * @param viewType - The view type that opens those files. */ registerExtensions(extensions: string[], viewType: string): void; /** * Registers a view with the Page preview core plugin as a source of `hover-link` events. The mock records it in * {@link Plugin.hoverLinkSources__}. * * @param id - The source id. * @param info - The hover link source description. */ registerHoverLinkSource(id: string, info: HoverLinkSourceOriginal): void; /** * Registers a handler for fenced code blocks of a language; Obsidian replaces the block's `
` with a
     * `
` the handler fills. The mock records the handler in {@link Plugin.markdownCodeBlockProcessors__} and * never calls it. * * @param language - The code block language. * @param handler - The callback that renders a block's source into its element. * @param _sortOrder - The processor's position relative to other post processors. * @returns A new no-op post processor, also recorded in {@link Plugin.markdownPostProcessors__}. */ registerMarkdownCodeBlockProcessor(language: string, handler: (source: string, el: HTMLElement, context: MarkdownPostProcessorContextOriginal) => unknown, _sortOrder?: number): MarkdownPostProcessorOriginal; /** * Registers a post processor that changes how documents look in reading mode. The mock records it in * {@link Plugin.markdownPostProcessors__} and never calls it. * * @param postProcessor - The post processor. * @param _sortOrder - The processor's position relative to other post processors. * @returns The same post processor. */ registerMarkdownPostProcessor(postProcessor: MarkdownPostProcessorOriginal, _sortOrder?: number): MarkdownPostProcessorOriginal; /** * Registers a handler for `obsidian://` URLs with the given action. The mock records it in * {@link Plugin.obsidianProtocolHandlers__}. * * @param action - The action, such as `open` for `obsidian://open`. * @param handler - The callback, given the key-value pairs decoded from the URL's query. */ registerObsidianProtocolHandler(action: string, handler: ObsidianProtocolHandlerOriginal): void; /** * Registers a view type and the function that creates its views, in `App.viewRegistry` as Obsidian does — so a * leaf whose view state names `type` really builds one — and unregisters it when the plugin unloads. The mock also * records it in {@link Plugin.views__}. * * Obsidian additionally detaches that type's leaves on unload, but only when the user switched the plugin off; the * mock has no such distinction and so never does. * * @param type - The view type. * @param viewCreator - The function that creates a view for a leaf. */ registerView(type: string, viewCreator: ViewCreatorOriginal): void; /** * Removes a command from the global command list; only needed for commands registered dynamically. The mock * deletes it from {@link Plugin.commands__}. * * @param commandId - The id the command was added with. */ removeCommand(commandId: string): void; /** * Saves the plugin's settings data, which Obsidian writes to `data.json` in the plugin folder. The mock replaces * {@link Plugin.data__}. * * @param data - The data to save. */ saveData(data: unknown): Promise; }