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
- groups the plugin’s context-menu commands under one entry that opens them as a submenu,
- shows or hides each context-menu command,
- hides every command at once with
Hide all, or puts every switch back on its default withReset.
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.
Turn them on
Section titled “Turn them on”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:
shouldRegisterCommandSwitchesis notfalse,- a
commandHandlerComponentis passed, - 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.
The options
Section titled “The options”| 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. |
Which commands are listed
Section titled “Which commands are listed”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.
Which submenu answer wins
Section titled “Which submenu answer wins”A handler decides whether to wrap its item in the submenu in this order:
- the registering menu surface: the
Notebook Navigatorbridge forces it off, because it wraps every plugin in an entry of its own, - the user’s command switches,
- the handler’s
shouldAddCommandToSubmenuconstructor option, getDefaultShouldAddCommandToSubmenu().
The submenu entry is the handler’s menu section, which is the plugin name unless the handler sets editorMenuSection, fileMenuSection or filesMenuSection.
Where they are stored
Section titled “Where they are stored”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.