/** * @file * * Mock of Obsidian's `Menu`, the context menu built from items and separators. */ import type { MenuItem as MenuItemOriginal, Menu as MenuOriginal, MenuPositionDef as MenuPositionDefOriginal } from 'obsidian'; import { Component } from './Component.cjs'; import { MenuItem } from './MenuItem.cjs'; import { MenuSeparator } from './MenuSeparator.cjs'; /** * A section's submenu, as recorded by `Menu.setSectionSubmenu`. */ export interface SectionSubmenu__ { readonly icon: string; readonly title: string; } /** * Mock of Obsidian's `Menu`. * * Nothing is shown: added items and separators are collected in {@link Menu.items} so a test can inspect them and * invoke their click handlers, and the show and configuration methods only return the menu for chaining. */ export declare class Menu extends Component { /** * The menu's root element; detached in the mock. */ dom: HTMLElement; /** * The menu's items and separators, in the order they were added. */ items: (MenuItem | MenuSeparator)[]; /** * Mock-only: the submenu configuration recorded for each section by {@link Menu.setSectionSubmenu}, keyed by * section name. */ sectionSubmenus__: Map; /** * The added items, separators excluded. * * `items` is Obsidian's own member and so holds both kinds. Nearly every reader wants only the * items — it is reading `title__` or `submenu`, which a separator does not have — so this saves * narrowing at each read. Use `items` when the separators themselves matter (asserting on their * positions, or on a count that has to match what Obsidian reports). * * @returns The menu items, in the order they were added. */ get menuItems__(): MenuItem[]; private onHideCallback; /** * Creates an empty menu. Use {@link Menu.create2__} from tests. */ protected constructor(); /** * Mock-only factory: creates a menu, spyable via `vi.spyOn(Menu, 'create2__')`. The subclass variant of * {@link Component.create__}. * * @returns The new menu. */ static create2__(): Menu; /** * Creates a menu for a mouse or pointer event, choosing the native or DOM menu to suit how it was triggered. * * The mock ignores the event and returns a new menu. * * @param _event - The event that opens the menu. * @returns The new menu. */ static forEvent(_event: MouseEvent | PointerEvent): Menu; /** * Mock-only: views a value typed as Obsidian's `Menu` as this mock. * * @param value - The value typed as the original `Menu`. * @returns The same object, typed as the mock. */ static fromOriginalType2__(value: MenuOriginal): Menu; /** * Adds a menu item. Obsidian only allows this before the menu is shown; the mock allows it at any time. * * @param callback - Called right away with the new item, to configure it. * @returns This menu, for chaining. */ addItem(callback: (item: MenuItemOriginal) => unknown): this; /** * Adds a separator. Obsidian only allows this before the menu is shown; the mock allows it at any time. * * @returns This menu, for chaining. */ addSeparator(): this; /** * Mock-only: views this mock as Obsidian's `Menu` type. * * @returns The same object, typed as the original `Menu`. */ asOriginalType2__(): MenuOriginal; /** * Closes the menu, through {@link Menu.hide}. */ close(): void; /** * Mock-only construction hook, called at the end of the constructor; a no-op meant for * `vi.spyOn(Menu.prototype, 'constructor2__')`. */ constructor2__(): void; /** * Hides the menu. As in Obsidian, the callback registered with {@link Menu.onHide} is cleared and then called, so it * runs once however many times the menu is hidden. * * @returns This menu, for chaining. */ hide(): this; /** * Registers a callback to run the next time the menu is hidden, by {@link Menu.hide} or {@link Menu.close}. Only the * latest callback is kept. * * @param callback - The callback to run. */ onHide(callback: () => unknown): void; /** * Handles the history-back action (the back gesture or the mobile back button) while the menu is the topmost * history handler. As in Obsidian, it hides the menu. */ onHistoryBack(): void; /** * Makes the menu reserve no space for item icons. A no-op in the mock. * * @returns This menu, for chaining. */ setNoIcon(): this; /** * Sets the element the menu is attached to. A no-op in the mock. * * @param _el - The parent element. * @returns This menu, for chaining. */ setParentElement(_el: HTMLElement): this; /** * Records a section's submenu configuration. * * Obsidian keys these by section name and renders the section as a submenu; the mock only has to * remember what was asked for, which is what callers such as `@mnaoumov/obsidian-lib`'s * `AbstractFileCommandHandler` assert on. Read the recorded configs back through * `sectionSubmenus__`. * * @param section - The section name. * @param config - The submenu's icon and title. * @returns This menu, for chaining. */ setSectionSubmenu(section: string, config: SectionSubmenu__): this; /** * Forces the menu to use the native OS menu or the DOM menu (desktop only). A no-op in the mock. * * @param _useNativeMenu - Whether to use the native menu. * @returns This menu, for chaining. */ setUseNativeMenu(_useNativeMenu: boolean): this; /** * Shows the menu at a mouse event's position. A no-op in the mock. * * @param _event - The mouse event to position the menu at. * @returns This menu, for chaining. */ showAtMouseEvent(_event: MouseEvent): this; /** * Shows the menu at a position. A no-op in the mock. * * @param _position - The coordinates, and optional alignment, to show the menu at. * @param _doc - The document to show the menu in, for popout windows. * @returns This menu, for chaining. */ showAtPosition(_position: MenuPositionDefOriginal, _doc?: Document): this; }