/** * @file * * Mock of Obsidian's `WorkspaceLeaf`, the layout node that hosts a single view. */ import type { HoverPopover as HoverPopoverOriginal, IconName as IconNameOriginal, OpenViewState as OpenViewStateOriginal, View as ViewOriginal, ViewState as ViewStateOriginal, WorkspaceLeaf as WorkspaceLeafOriginal, WorkspaceMobileDrawer as WorkspaceMobileDrawerOriginal, WorkspaceTabs as WorkspaceTabsOriginal } from 'obsidian'; import type { SerializedWorkspaceItem } from '../internal/types.mjs'; import type { App } from './App.mjs'; import type { TFile } from './TFile.mjs'; import { EmptyView } from '../internal/views/empty-view.mjs'; import { WorkspaceItem } from './WorkspaceItem.mjs'; /** * Mock of Obsidian's `WorkspaceLeaf`. * * The leaf keeps its opened file, ephemeral state, group and pinned flag in memory so a test can read them back. It * sits in the workspace's layout tree, and {@link WorkspaceLeaf.detach} removes it from there. * * As in Obsidian its {@link WorkspaceLeaf.view} is REAL and never `null`: the leaf is born holding * {@link WorkspaceLeaf._empty}, the "New tab" page, and {@link WorkspaceLeaf.setViewState} builds the view its type * names through `App.viewRegistry`. So the view state is not stored — {@link WorkspaceLeaf.getViewState} derives it * from the view, exactly as the app does. */ export declare class WorkspaceLeaf extends WorkspaceItem { /** * The "New tab" page the leaf is born holding, and goes back to whenever {@link WorkspaceLeaf.open} is passed * `null`. Obsidian keeps it under this name too. */ _empty: EmptyView; /** * When the leaf was last made active, as a timestamp; `0` until then. `Workspace.setActiveLeaf` sets it, and * `Workspace.getMostRecentLeaf` picks the leaf with the highest value. */ activeTime: number; /** * Mock-only: the app the leaf was created for, used to reach the mock workspace and the view registry. */ readonly app__: App; /** * The hover popover currently shown for the leaf, or `null` when there is none. */ hoverPopover: HoverPopoverOriginal | null; /** * Whether the leaf is deferred because it is in the background, holding a placeholder instead of its real view. * * Always `false` in the mock. Obsidian defers a view only when the leaf has no history state, is not already * deferred, carries an icon and a title in its view state, and its element is off screen; the mock has neither a * history nor a rendered element, so that branch is unreachable and a registered type always goes through its * creator. */ readonly isDeferred = false; /** * The direct parent of the leaf: a `WorkspaceTabs` on desktop, possibly a `WorkspaceMobileDrawer` on mobile. A leaf * with no parent holds an empty strict proxy instead, which throws on any member access. */ parent: WorkspaceMobileDrawerOriginal | WorkspaceTabsOriginal; /** * The tab header's close button, the last child of the header's inner element. As in Obsidian it carries the * `lucide-x` icon and the `Close` tooltip; the mock records them as its `data-icon` and `aria-label` attributes, and * does not attach Obsidian's click listener, so clicking it does not detach the leaf. */ tabHeaderCloseEl: HTMLDivElement; /** * The leaf's tab header, a `workspace-tab-header tappable` div, built in the constructor exactly as Obsidian builds * it. It holds one `workspace-tab-header-inner` div, whose children are, in order, * {@link WorkspaceLeaf.tabHeaderInnerIconEl}, {@link WorkspaceLeaf.tabHeaderInnerTitleEl}, * {@link WorkspaceLeaf.tabHeaderStatusContainerEl} and {@link WorkspaceLeaf.tabHeaderCloseEl}. * * The mock builds the element but not the rest of the header: it is not placed in a tab group's header container, * Obsidian's drag, context-menu and middle-click listeners are not attached, and `updateHeader` — which fills the * icon and title and creates the pinned and linked status icons — is not modelled, so both inner elements stay * empty. */ tabHeaderEl: HTMLElement; /** * The tab header's icon element, a `workspace-tab-header-inner-icon` div inside {@link WorkspaceLeaf.tabHeaderEl}. * Empty in the mock, which does not model `updateHeader`. */ tabHeaderInnerIconEl: HTMLElement; /** * The tab header's title element, a `workspace-tab-header-inner-title` div inside {@link WorkspaceLeaf.tabHeaderEl}. * Empty in the mock, which does not model `updateHeader`. */ tabHeaderInnerTitleEl: HTMLElement; /** * The container for the tab header's status icons, a `workspace-tab-header-status-container` div inside * {@link WorkspaceLeaf.tabHeaderEl}, between the title and the close button. Obsidian puts the pinned and linked * icons here, and plugins append their own indicators to it, which a test can read back. */ tabHeaderStatusContainerEl: HTMLDivElement; /** * The item kind, `leaf`, as Obsidian names it. */ type: string; /** * The view shown in the leaf. Never `null`: a leaf showing nothing holds {@link WorkspaceLeaf._empty}. */ view: ViewOriginal; /** * Mock-only: the file the leaf's view has loaded. * * @returns The file view's file, or `null` when the view is not a file view or has none. */ get file__(): null | TFile; private ephemeralState; private group; private pinned; private working; /** * Creates a leaf showing the empty view. Obsidian does not construct leaves publicly; use * {@link WorkspaceLeaf.create2__}. * * @param app - The app the leaf belongs to. * @param id - The leaf id; a unique numeric id is generated when omitted. */ protected constructor(app: App, id?: string); /** * Mock-only factory: creates a leaf, spyable via `vi.spyOn(WorkspaceLeaf, 'create2__')`. The numbered subclass * variant of `create__`. * * @param app - The app the leaf belongs to. * @param id - The leaf id; a unique numeric id is generated when omitted. * @returns The new leaf. */ static create2__(app: App, id?: string): WorkspaceLeaf; /** * Mock-only: views a value typed as Obsidian's `WorkspaceLeaf` as this mock. The numbered subclass variant of * `fromOriginalType__`. * * @param value - The value typed as the original `WorkspaceLeaf`. * @returns The same object, typed as the mock. */ static fromOriginalType3__(value: WorkspaceLeafOriginal): WorkspaceLeaf; /** * Mock-only: views this mock as Obsidian's `WorkspaceLeaf` type. The numbered subclass variant of * `asOriginalType__`. * * @returns The same object, typed as the original `WorkspaceLeaf`. */ asOriginalType3__(): WorkspaceLeafOriginal; /** * Whether the leaf may be navigated away from, which is what `Workspace.getUnpinnedLeaf` — and so `getLeaf(false)` — * uses to decide whether the leaf can be reused. As in Obsidian, that is its view's `navigation` flag and the leaf * not being pinned; the empty view navigates, so a leaf showing nothing is reusable. * * @returns Whether the leaf can navigate. */ canNavigate(): boolean; /** * Mock-only construction hook, called at the end of the constructor; a no-op meant for * `vi.spyOn(WorkspaceLeaf.prototype, 'constructor3__')`. * * @param _app - The app the leaf was created with. * @param _id - The leaf id the leaf was created with. */ constructor3__(_app: App, _id?: string): void; /** * Closes the leaf and removes it from the workspace's layout tree, through the mock workspace's `removeLeaf__`. */ detach(): void; /** * Gets the text shown for the leaf, such as in its tab header. * * @returns The view's display text. */ getDisplayText(): string; /** * Gets the leaf's ephemeral state: transient view state, such as the scroll position, that is not saved in the * layout. * * @returns A shallow copy of the stored ephemeral state. */ getEphemeralState(): Record; /** * Mock-only: reads the linked-view group set by {@link WorkspaceLeaf.setGroup} or * {@link WorkspaceLeaf.setGroupMember}. * * @returns The group name, or `null` when the leaf is in no group. */ getGroup__(): null | string; /** * Gets the icon shown for the leaf. * * @returns The view's icon. */ getIcon(): IconNameOriginal; /** * Gets the leaf's serializable view state, derived from the view as Obsidian derives it: its type and state, plus * the pinned flag when the leaf is pinned. * * Obsidian also writes the view's icon and display text into the result, for the placeholder it shows while a * deferred view loads. Neither `obsidian.d.ts` nor `obsidian-typings` declares those two members of `ViewState`, * and the mock never defers, so it leaves them out. * * @returns The view state. */ getViewState(): ViewStateOriginal; /** * Mock-only: reads the pinned flag set by {@link WorkspaceLeaf.setPinned} or {@link WorkspaceLeaf.togglePinned}. * * @returns Whether the leaf is pinned. */ isPinned__(): boolean; /** * Loads the leaf's real view if it is deferred, resolving once it has fully loaded. The mock's leaves are never * deferred, so it resolves immediately. */ loadIfDeferred(): Promise; /** * Notifies the leaf that it was resized. The mock forwards the call to the view. */ onResize(): void; /** * Shows a view in the leaf, replacing whatever it was showing. As in Obsidian the outgoing view is closed first — * its element detached and its component unloaded — and a failure to close is logged rather than thrown; `null` * puts {@link WorkspaceLeaf._empty} back. * * The user-facing notice Obsidian shows when a view fails to close is not modeled. * * @param view - The view to show, or `null` for the empty view. * @returns The view the leaf ended up showing. */ open(view: null | ViewOriginal): Promise; /** * Opens a file in the leaf, as Obsidian does: the view type registered for the file's extension, or the current * view's own type when it already accepts that extension, and then {@link WorkspaceLeaf.setViewState} with the * file's path in the state. * * A file whose extension no view type is registered for changes nothing, which is Obsidian's own branch for one — * the notice and the open-with-default-app fallback it takes there are not modeled. Only the Markdown view is * registered by default, so that is what a `.md` file opens in. * * @param file - The file to open. * @param openState - The state, ephemeral state, active flag and link group to open the file with. */ openFile(file: TFile, openState?: OpenViewStateOriginal): Promise; /** * Serializes the leaf, as Obsidian does: the item's id, type and dimension, then `pinned` when the leaf is pinned, * its view state, and its linked-view group when it has one. * * @returns The serialized leaf. */ serialize(): SerializedWorkspaceItem; /** * Sets the leaf's ephemeral state. The mock stores a shallow copy. * * @param state - The new ephemeral state. */ setEphemeralState(state: Record): void; /** * Puts the leaf into a linked-view group, whose members follow each other's navigation. As in Obsidian, a change of * group first pins the leaf when it or any leaf already in the new group is pinned, then triggers `group-change` * with the new group; setting the group the leaf is already in does nothing. * * @param group - The group name, or `null` to leave any group. */ setGroup(group: null | string): void; /** * Puts the leaf into the same linked-view group as another leaf. When that leaf is in no group yet, it is first put * into a new one, as Obsidian does. Passing the leaf itself does nothing, and passing `null` leaves any group. * * @param other - The leaf whose group to join, or `null`. */ setGroupMember(other: null | WorkspaceLeaf): void; /** * Pins or unpins the leaf. As in Obsidian, it triggers `pinned-change` with the new flag, requests a layout save, * and pins or unpins every other leaf in the same group to match. * * @param pinned - Whether the leaf should be pinned. */ setPinned(pinned: boolean): void; /** * Shows a view of the given type in the leaf, building it through `App.viewRegistry`, exactly as Obsidian does. * * - **The view is only rebuilt when the type CHANGES.** A state naming the type the leaf already shows keeps the * view and only calls `setState` on it. * - A registered type goes through its creator; a creator that throws is logged and falls back to the unknown-type * view, which keeps the type it could not build. * - An unregistered type gives the unknown-type view, except for `'empty'`, which gives * {@link WorkspaceLeaf._empty} back — Obsidian's own answer for it. * - A view that answers `close` on the result — a file view left with no file — sends the leaf back to the empty * view; one that answers `layout` asks the workspace to update the layout. * - `active` activates the leaf, `group` joins that leaf's link group, and the ephemeral state is applied last. * - A call reached from inside another one is dropped, through Obsidian's own `working` guard. * * Obsidian also records navigation history around all of this; the mock has no history, so it does not. * * @param viewState - The new view state. * @param eState - The ephemeral state to apply along with it. */ setViewState(viewState: ViewStateOriginal, eState?: Record): Promise; /** * Flips the leaf's pinned flag through {@link WorkspaceLeaf.setPinned}. */ togglePinned(): void; private buildView; }