|
| 1 | +import { Command } from './types'; |
| 2 | +/** |
| 3 | + * This class allows executing or registering new Joplin commands. Commands |
| 4 | + * can be executed or associated with |
| 5 | + * {@link JoplinViewsToolbarButtons | toolbar buttons} or |
| 6 | + * {@link JoplinViewsMenuItems | menu items}. |
| 7 | + * |
| 8 | + * [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/register_command) |
| 9 | + * |
| 10 | + * ## Executing Joplin's internal commands |
| 11 | + * |
| 12 | + * It is also possible to execute internal Joplin's commands which, as of |
| 13 | + * now, are not well documented. You can find the list directly on GitHub |
| 14 | + * though at the following locations: |
| 15 | + * |
| 16 | + * * [Main screen commands](https://github.com/laurent22/joplin/tree/dev/packages/app-desktop/gui/MainScreen/commands) |
| 17 | + * * [Global commands](https://github.com/laurent22/joplin/tree/dev/packages/app-desktop/commands) |
| 18 | + * * [Editor commands](https://github.com/laurent22/joplin/tree/dev/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.ts) |
| 19 | + * |
| 20 | + * To view what arguments are supported, you can open any of these files |
| 21 | + * and look at the `execute()` command. |
| 22 | + * |
| 23 | + * ## Executing editor commands |
| 24 | + * |
| 25 | + * There might be a situation where you want to invoke editor commands |
| 26 | + * without using a {@link JoplinContentScripts | contentScript}. For this |
| 27 | + * reason Joplin provides the built in `editor.execCommand` command. |
| 28 | + * |
| 29 | + * `editor.execCommand` should work with any core command in both the |
| 30 | + * [CodeMirror](https://codemirror.net/doc/manual.html#execCommand) and |
| 31 | + * [TinyMCE](https://www.tiny.cloud/docs/api/tinymce/tinymce.editorcommands/#execcommand) editors, |
| 32 | + * as well as most functions calls directly on a CodeMirror editor object (extensions). |
| 33 | + * |
| 34 | + * * [CodeMirror commands](https://codemirror.net/doc/manual.html#commands) |
| 35 | + * * [TinyMCE core editor commands](https://www.tiny.cloud/docs/advanced/editor-command-identifiers/#coreeditorcommands) |
| 36 | + * |
| 37 | + * `editor.execCommand` supports adding arguments for the commands. |
| 38 | + * |
| 39 | + * ```typescript |
| 40 | + * await joplin.commands.execute('editor.execCommand', { |
| 41 | + * name: 'madeUpCommand', // CodeMirror and TinyMCE |
| 42 | + * args: [], // CodeMirror and TinyMCE |
| 43 | + * ui: false, // TinyMCE only |
| 44 | + * value: '', // TinyMCE only |
| 45 | + * }); |
| 46 | + * ``` |
| 47 | + * |
| 48 | + * [View the example using the CodeMirror editor](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/codemirror_content_script/src/index.ts) |
| 49 | + * |
| 50 | + */ |
| 51 | +export default class JoplinCommands { |
| 52 | + /** |
| 53 | + * <span class="platform-desktop">desktop</span> Executes the given |
| 54 | + * command. |
| 55 | + * |
| 56 | + * The command can take any number of arguments, and the supported |
| 57 | + * arguments will vary based on the command. For custom commands, this |
| 58 | + * is the `args` passed to the `execute()` function. For built-in |
| 59 | + * commands, you can find the supported arguments by checking the links |
| 60 | + * above. |
| 61 | + * |
| 62 | + * ```typescript |
| 63 | + * // Create a new note in the current notebook: |
| 64 | + * await joplin.commands.execute('newNote'); |
| 65 | + * |
| 66 | + * // Create a new sub-notebook under the provided notebook |
| 67 | + * // Note: internally, notebooks are called "folders". |
| 68 | + * await joplin.commands.execute('newFolder', "SOME_FOLDER_ID"); |
| 69 | + * ``` |
| 70 | + */ |
| 71 | + execute(commandName: string, ...args: any[]): Promise<any | void>; |
| 72 | + /** |
| 73 | + * <span class="platform-desktop">desktop</span> Registers a new command. |
| 74 | + * |
| 75 | + * ```typescript |
| 76 | + * // Register a new commmand called "testCommand1" |
| 77 | + * |
| 78 | + * await joplin.commands.register({ |
| 79 | + * name: 'testCommand1', |
| 80 | + * label: 'My Test Command 1', |
| 81 | + * iconName: 'fas fa-music', |
| 82 | + * execute: () => { |
| 83 | + * alert('Testing plugin command 1'); |
| 84 | + * }, |
| 85 | + * }); |
| 86 | + * ``` |
| 87 | + */ |
| 88 | + register(command: Command): Promise<void>; |
| 89 | +} |
0 commit comments