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 'obsidian-dev-utils/obsidian/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.
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()boolean | undefinedGets whether to add the command to a submenu.
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.