Plugins: Provides selected notes when triggering a command from the note list context menu

pull/4127/head
Laurent Cozic 2020-11-21 12:35:19 +00:00
parent d953f6bcab
commit 4ac2409318
3 changed files with 24 additions and 1 deletions

View File

@ -208,6 +208,13 @@ export enum MenuItemLocation {
* @deprecated Do not use - same as NoteListContextMenu
*/
Context = 'context',
/**
* The context menu that appears when right-clicking on the note
* list, or when multiple notes are selected. Any command triggered from
* this location will receive a `noteIds` array with the list of notes that
* were right-clicked or selected.
*/
NoteListContextMenu = 'noteListContextMenu',
EditorContextMenu = 'editorContextMenu',
}

View File

@ -21,6 +21,20 @@ joplin.plugins.register({
},
});
await joplin.commands.register({
name: 'contextMenuCommandExample',
label: 'My Context Menu command',
execute: async (noteIds:string[]) => {
const notes = [];
for (const noteId of noteIds) {
notes.push(await joplin.data.get(['notes', noteId]));
}
const noteTitles = notes.map((note:any) => note.title);
alert('The following notes will be processed:\n\n' + noteTitles.join(', '));
},
});
// Commands that return a result and take argument can only be used
// programmatically, so it's not necessary to set a label and icon.
await joplin.commands.register({
@ -40,6 +54,8 @@ joplin.plugins.register({
await joplin.views.menuItems.create('myMenuItem1', 'testCommand1', MenuItemLocation.Tools, { accelerator: 'CmdOrCtrl+Alt+Shift+B' });
await joplin.views.menuItems.create('myMenuItem2', 'testCommand2', MenuItemLocation.Tools);
await joplin.views.menuItems.create('contextMenuItem1', 'contextMenuCommandExample', MenuItemLocation.NoteListContextMenu);
console.info('Running command with arguments...');
const result = await joplin.commands.execute('commandWithResult', 'abcd', 123);
console.info('Result was: ' + result);

View File

@ -186,7 +186,7 @@ export default class NoteListUtils {
if (location !== MenuItemLocation.Context && location !== MenuItemLocation.NoteListContextMenu) continue;
menu.append(
new MenuItem(menuUtils.commandToStatefulMenuItem(info.view.commandName))
new MenuItem(menuUtils.commandToStatefulMenuItem(info.view.commandName, noteIds))
);
}