Skip to content

Command Switches

A plugin that adds many commands to the editor, file and folder context menus can make those menus taller than the screen. Command switches give every plugin built on this library the same answer, with no settings code of its own: a Commands and menus section at the end of the settings tab, where the user

  1. groups the plugin’s context-menu commands under one entry that opens them as a submenu,
  2. shows or hides each context-menu command,
  3. hides every command at once with Hide all, or puts every switch back on its default with Reset.

A hidden command still runs from the command palette and its hotkeys: the switches govern the context menus only (the editor menu, the margin menu, the file and files menus, and the Notebook Navigator bridge). Obsidian already lets a user hide palette commands and remove hotkeys.

Pass the plugin’s commandHandlerComponent to the PluginSettingsTabComponent. Nothing else is needed: the option shouldRegisterCommandSwitches is true by default.

import { PluginSettingsTabComponent } from '@mnaoumov/obsidian-lib/components/plugin-settings-tab-component';
const settingsTabComponent = new PluginSettingsTabComponent({
commandHandlerComponent,
plugin,
pluginSettingsTab
});

Inside a PluginBase subclass, commandHandlerComponent is this.commandHandlerComponent, and the component is added with this.addChild(...) in onloadImpl, as before.

The section is added only when all three hold:

  1. shouldRegisterCommandSwitches is not false,
  2. a commandHandlerComponent is passed,
  3. the tab extends PluginSettingsTabBase, which stores the switches.

Otherwise the tab and the menus are exactly what the plugin built. shouldRegisterCommandSwitches: false is the opt-out.

Option Default Meaning
commandHandlerComponent none The component whose registered commands the section lists.
shouldRegisterCommandSwitches true Whether to add the section and apply the switches to the menus.
defaultShouldAddCommandsToSubmenu true Whether the commands are grouped under a submenu while the user has not chosen.

A command is listed when it was registered through CommandHandlerComponent.registerCommandHandlers and its handler can add a context-menu item: an EditorCommandHandler that overrides shouldAddToEditorMenu or shouldAddToViewportMenu, or a file or folder handler that overrides one of its menu hooks. A handler can answer for itself by overriding isMenuCommand(). Palette-only commands get no row.

A row is named after the command, as the palette shows it after the plugin name. Obsidian builds the tab’s search index when the tab is added, so a command registered after that is shown in the section but not found by the settings search.

A handler decides whether to wrap its item in the submenu in this order:

  1. the registering menu surface: the Notebook Navigator bridge forces it off, because it wraps every plugin in an entry of its own,
  2. the user’s command switches,
  3. the handler’s shouldAddCommandToSubmenu constructor option,
  4. getDefaultShouldAddCommandToSubmenu().

The submenu entry is the handler’s menu section, which is the plugin name unless the handler sets editorMenuSection, fileMenuSection or filesMenuSection.

The switches live in the plugin’s own data.json, under the reserved key __obsidianLib, so they sync like every other setting and need no key in the plugin’s settings class:

{
"__obsidianLib": {
"commandSwitches": {
"hiddenCommandIds": ["extract-current-selection"],
"shouldAddCommandsToSubmenu": false
}
}
}

An absent field means the default, so Reset deletes the entry instead of writing the defaults. PluginSettingsComponentBase keeps the record through every load and save, includes it in Export, reads it back on Import, and clears it on Reset to defaults. getLibraryRecordValue and setLibraryRecordValue are its API.