2023-06-30 10:55:56 +02:00
/* eslint-disable multiline-comment-style */
2020-11-05 18:58:23 +02:00
import KeymapService from '../../KeymapService' ;
2020-10-13 13:57:03 +02:00
import { MenuItem , MenuItemLocation } from './types' ;
import MenuController from '../MenuController' ;
import Plugin from '../Plugin' ;
import createViewHandle from '../utils/createViewHandle' ;
/ * *
* Allows creating menus .
*
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/menu)
2024-03-26 13:36:15 +02:00
*
* < span class = "platform-desktop" > desktop < / span >
2020-10-13 13:57:03 +02:00
* /
export default class JoplinViewsMenus {
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-10-13 13:57:03 +02:00
private store : any ;
private plugin : Plugin ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-03-06 16:22:01 +02:00
public constructor ( plugin : Plugin , store : any ) {
2020-10-13 13:57:03 +02:00
this . store = store ;
this . plugin = plugin ;
}
2020-11-12 21:13:28 +02:00
private registerCommandAccelerators ( menuItems : MenuItem [ ] ) {
2020-10-13 13:57:03 +02:00
for ( const menuItem of menuItems ) {
if ( menuItem . accelerator ) {
KeymapService . instance ( ) . registerCommandAccelerator ( menuItem . commandName , menuItem . accelerator ) ;
}
if ( menuItem . submenu ) {
this . registerCommandAccelerators ( menuItem . submenu ) ;
}
}
}
/ * *
* Creates a new menu from the provided menu items and place it at the given location . As of now , it is only possible to place the
* menu as a sub - menu of the application build - in menus .
* /
2020-11-13 19:09:28 +02:00
public async create ( id : string , label : string , menuItems : MenuItem [ ] , location : MenuItemLocation = MenuItemLocation . Tools ) {
if ( ! Array . isArray ( menuItems ) ) {
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.menus.create("my-unique-id", ...)`' , true ) ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-11-13 19:09:28 +02:00
location = menuItems as any || MenuItemLocation . Tools ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-11-13 19:09:28 +02:00
menuItems = label as any ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-11-13 19:09:28 +02:00
label = id as any ;
id = ` ${ this . plugin . viewCount } ` ;
}
const handle = createViewHandle ( this . plugin , id ) ;
2020-10-13 13:57:03 +02:00
const controller = new MenuController ( handle , this . plugin . id , this . store , label , menuItems , location ) ;
this . plugin . addViewController ( controller ) ;
this . registerCommandAccelerators ( menuItems ) ;
}
}