Skip to content

AbstractFileCommandHandler

Command handler for abstract file commands.

Handles both single-file and multi-file context menus. Subclasses override canExecuteAbstractFile and executeAbstractFile.

Import:

import { AbstractFileCommandHandler } from '@mnaoumov/obsidian-lib/command-handlers/abstract-file-command-handler';

Signature:

export class AbstractFileCommandHandler extends GlobalCommandHandler

Extends: GlobalCommandHandler

Constructor

new AbstractFileCommandHandler(params: AbstractFileCommandHandlerConstructorParams)

Creates a new abstract file command handler.

Properties

PropertyTypeDescription
iconIconNameThe icon for the command.
(Inherited from GlobalCommandHandler)
idstringThe ID of the command.
(Inherited from GlobalCommandHandler)
namestringThe display name of the command.
(Inherited from GlobalCommandHandler)

Methods

MethodReturnsDescription
buildCommand()CommandBuilds a plain Obsidian Command object with a checkCallback.
(Inherited from GlobalCommandHandler)
canExecute()booleanChecks whether the command can execute from the command palette. Uses the active file as the target.
canExecuteAbstractFile(_abstractFile)booleanChecks whether the command can execute for a single abstract file.
canExecuteAbstractFiles(abstractFiles)booleanChecks whether the command can execute for multiple abstract files. Default implementation checks each file individually.
execute()Promisable<void>Executes the command from the command palette using the active file.
executeAbstractFile(abstractFile)Promisable<void>Executes the command for a single abstract file.
executeAbstractFiles(abstractFiles)Promisable<void>Executes the command for multiple abstract files. Default implementation executes sequentially.
getActiveFile()null | TAbstractFileGets the active file, if any, to use as the target when executing from the command palette.
getCommandSwitchesShouldAddCommandToSubmenu()boolean | nullThe submenu answer of the user's command switches.
(Inherited from GlobalCommandHandler)
getDefaultShouldAddCommandToSubmenu()booleanGets whether to add the command to a submenu when neither the registration context nor the constructor set a value.
isMenuCommand()booleanWhether the command can appear in a context menu: whether the subclass overrides shouldAddToAbstractFileMenu or shouldAddToAbstractFilesMenu. FileCommandHandler and FolderCommandHandler ask the same of their own file and folder hooks.
isShownInMenus()booleanWhether the user's command switches let this command into the context menus. Always true while no switches are attached.
(Inherited from GlobalCommandHandler)
onRegistered(context)Promise<void>Registers file-menu and files-menu event handlers.
shouldAddCommandToSubmenu()booleanGets whether to add the command to a submenu.

The registration context wins over the constructor value: a menu surface that already wraps every contributed item in a plugin-titled parent entry forces this off, because a section submenu declared here would make Menu.sort() nest a second plugin-titled entry inside the first.

Next come the user's command switches, when the settings tab component attached them, then the constructor value. When none of those sets a value, getDefaultShouldAddCommandToSubmenu decides. Override that hook, not this method, to supply a fallback such as a plugin setting, so a registration that forces the flag off still wins.
shouldAddToAbstractFileMenu(params)booleanChecks whether the command should appear in the single-file context menu.
shouldAddToAbstractFilesMenu(params)booleanChecks whether the command should appear in the multi-file context menu. Default implementation checks each file individually.
shouldAddToCommandPalette()booleanChecks whether the command should appear in the command palette.

Links to this page: