[plugin-dev] question about obtaining list of commands

Hello again Takuya!

It’s been a while but yes I have released a working plugin on inkdrop under command-palette. (I had to do some hacky workarounds to get some stuff to work though)

Before and After releasing I noticed I could use inkdrops.keymaps.getKeybindings to get all available keybinds and upon logging it, i thought it gave all available commands (my bad honestly) but it turns out it doesn’t give you all the commands but only commands that have keybinds on them. For example: application:about is missing because there’s no keybinds associated with that.

Looking further at the documentation, there’s the CommandRegistry class but there’s no method to get all available commands, so I currently resorted into a object array containing a list of commands based off here and a couple of stuff I found along the way.

Looking at the problem, I was wondering if there would be another way to obtain all available commands so the plugin won’t have to rely on developers to put their commands on plugins.js and it would be also much easier because that would mean the plugin can only show the commands that can really be invoked with their respective keybinds (instead of just sticking to defaults, current limitation) and I won’t be relying on such a hacky method of listing all the commands with theoretically the same layout as getKeybindings. (like if i used getKeybindings, all the available information such as selector, category, and keystrokes and command are available.)

1 Like

Hi Jariel,

Whoaa, amazing! I was planning to make that feature in the future but you did it :star_struck:

Yeah, there are commands of plugins as well, so it’s impossible to maintain the list manually.
I agree that it’s useful if it can invoke commands that aren’t bound with a shortcut key.

As you may know, the commands are bound directly with DOM elements’ event listeners. For example, you can define a command with inkdrop.commands.add like so:

// inkdrop.commands.add(target, commandName, eventHandler)
inkdrop.commands.add(document.body, 'myplugin:command', () => console.log('do something neat'))

CommandRegistry registers the command by invoking target.addEventListener(commandName, eventHandler).
You can get a list of the commands bound with document.body like so:


So, you can find available commands by scanning all the parent nodes from the current active element:

for (let el = document.activeElement; el; el = el.parentElement) {
	const commands = getEventListeners(el)
	console.log(el, commands)

Note that you have to filter the browser’s built-in event listeners.
Keybindings are managed separately. You can check if a command has a keybinding like so:

inkdrop.keymaps.findKeyBindings({ command: 'core:strong', element: document.body })

Hope that helps!

1 Like

Thank you Takuya!

That helps a lot and helped me steer in the right direction! I’ll continue working on it and implementing it and hope to publish an update as soon as possible :slight_smile:

1 Like