Skip to content

EditorCommandHandler

Command handler for editor commands.

Subclasses override canExecuteEditor and executeEditor to provide behavior. Optionally integrates with the editor context menu via shouldAddToEditorMenu, and with the markdown viewport (margin) menu via shouldAddToViewportMenu.

Import:

import { EditorCommandHandler } from '@mnaoumov/obsidian-lib/command-handlers/editor-command-handler';

Signature:

export class EditorCommandHandler extends CommandHandler

Extends: CommandHandler

Constructor

new EditorCommandHandler(params: EditorCommandHandlerConstructorParams)

Creates a new editor command handler.

Properties

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

Methods

MethodReturnsDescription
buildCommand()CommandBuilds a plain Obsidian Command object with an editorCheckCallback.
canExecuteEditor(_editor, _context)booleanChecks whether the command can execute for the given editor and context.
executeEditor(editor, context)Promisable<void>Executes the command for the given editor and context.
getCommandSwitchesShouldAddCommandToSubmenu()boolean | nullThe submenu answer of the user's command switches.
(Inherited from CommandHandler)
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 shouldAddToEditorMenu or shouldAddToViewportMenu, which both answer false here.
isShownInMenus()booleanWhether the user's command switches let this command into the context menus. Always true while no switches are attached.
(Inherited from CommandHandler)
onRegistered(context)Promise<void>Registers the editor-menu and markdown-viewport-menu event handlers.

Both are registered unconditionally; whether an item actually appears is decided per menu render by shouldAddToEditorMenu / shouldAddToViewportMenu, which both default to false.
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.
shouldAddToCommandPalette()booleanChecks whether the command should appear in the command palette.
shouldAddToEditorMenu(_editor, _context)booleanChecks whether the command should appear in the editor context menu.
shouldAddToViewportMenu(_view, _mode, _source)booleanChecks whether the command should appear in the markdown viewport (margin) context menu.

That is the menu carrying Readable line length / Line numbers / Inline title, raised by right-clicking the empty space beside the text or the line-number gutter — a menu shouldAddToEditorMenu can never reach, because a margin click lands outside the editor's contentDOM. It is a natural home for whole-note commands once the editor menu gets crowded.

mode and source are passed through rather than filtered here: whether a command belongs in reading mode is the consuming plugin's call. Note that Obsidian 1.13.7 always passes 'gutter' as source, so only mode currently discriminates - see MarkdownViewportMenuEventHandler.