2020-11-05 18:58:23 +02:00
import KeymapService from '../../KeymapService' ;
2020-10-09 19:35:46 +02:00
import { CreateMenuItemOptions , MenuItemLocation } from './types' ;
import MenuItemController from '../MenuItemController' ;
import Plugin from '../Plugin' ;
import createViewHandle from '../utils/createViewHandle' ;
/ * *
* Allows creating and managing menu items .
*
2020-11-05 18:58:23 +02:00
* [ View the demo plugin ] ( https : //github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins/register_command)
2020-10-09 19:35:46 +02:00
* /
export default class JoplinViewsMenuItems {
private store : any ;
private plugin : Plugin ;
2020-12-08 21:45:08 +02:00
public constructor ( plugin : Plugin , store : any ) {
2020-10-09 19:35:46 +02:00
this . store = store ;
this . plugin = plugin ;
}
/ * *
* Creates a new menu item and associate it with the given command . You can specify under which menu the item should appear using the ` location ` parameter .
* /
2020-12-08 21:45:08 +02:00
public async create ( id : string , commandName : string , location : MenuItemLocation = MenuItemLocation . Tools , options : CreateMenuItemOptions = null ) {
2020-11-13 19:09:28 +02:00
if ( typeof location !== 'string' ) {
2021-08-05 13:02:03 +02:00
this . plugin . deprecationNotice ( '1.5' , 'Creating a view without an ID is deprecated. To fix it, change your call to `joplin.views.menuItem.create("my-unique-id", ...)`' , true ) ;
2020-11-13 19:09:28 +02:00
options = location as any ;
location = commandName as any || MenuItemLocation . Tools ;
commandName = id as any ;
id = ` ${ this . plugin . viewCount } ` ;
}
const handle = createViewHandle ( this . plugin , id ) ;
2020-10-09 19:35:46 +02:00
const controller = new MenuItemController ( handle , this . plugin . id , this . store , commandName , location ) ;
this . plugin . addViewController ( controller ) ;
2020-12-08 21:45:08 +02:00
// Register the command with the keymap service - not that if no
// accelerator is provided, we still register the command, so that
// it appears in the keymap editor, which will allow the user to
// set a custom shortcut.
//
// https://discourse.joplinapp.org/t/plugin-note-tabs/12752/39
2020-10-09 19:35:46 +02:00
if ( options && options . accelerator ) {
KeymapService . instance ( ) . registerCommandAccelerator ( commandName , options . accelerator ) ;
2020-12-08 21:45:08 +02:00
} else {
KeymapService . instance ( ) . registerCommandAccelerator ( commandName , null ) ;
2020-10-09 19:35:46 +02:00
}
}
}