mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Desktop: Added support for Menu API for plugins
This commit is contained in:
parent
2caaf8e8c1
commit
c648f19693
@ -264,11 +264,13 @@ ReactNativeClient/lib/services/plugins/api/JoplinSettings.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViews.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsDialogs.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsMenuItems.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsMenus.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsPanels.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsToolbarButtons.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinWorkspace.js
|
||||
ReactNativeClient/lib/services/plugins/api/types.js
|
||||
ReactNativeClient/lib/services/plugins/BasePluginRunner.js
|
||||
ReactNativeClient/lib/services/plugins/MenuController.js
|
||||
ReactNativeClient/lib/services/plugins/MenuItemController.js
|
||||
ReactNativeClient/lib/services/plugins/Plugin.js
|
||||
ReactNativeClient/lib/services/plugins/PluginService.js
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -258,11 +258,13 @@ ReactNativeClient/lib/services/plugins/api/JoplinSettings.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViews.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsDialogs.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsMenuItems.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsMenus.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsPanels.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinViewsToolbarButtons.js
|
||||
ReactNativeClient/lib/services/plugins/api/JoplinWorkspace.js
|
||||
ReactNativeClient/lib/services/plugins/api/types.js
|
||||
ReactNativeClient/lib/services/plugins/BasePluginRunner.js
|
||||
ReactNativeClient/lib/services/plugins/MenuController.js
|
||||
ReactNativeClient/lib/services/plugins/MenuItemController.js
|
||||
ReactNativeClient/lib/services/plugins/Plugin.js
|
||||
ReactNativeClient/lib/services/plugins/PluginService.js
|
||||
|
2
CliClient/tests/support/plugins/menu/.gitignore
vendored
Normal file
2
CliClient/tests/support/plugins/menu/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
dist/*
|
||||
node_modules/
|
14
CliClient/tests/support/plugins/menu/README.md
Normal file
14
CliClient/tests/support/plugins/menu/README.md
Normal file
@ -0,0 +1,14 @@
|
||||
# Joplin Plugin
|
||||
|
||||
This is a template to create a new Joplin plugin.
|
||||
|
||||
The main two files you will want to look at are:
|
||||
|
||||
- `/src/index.ts`, which contains the entry point for the plugin source code.
|
||||
- `/src/manifest.json`, which is the plugin manifest. It contains information such as the plugin a name, version, etc.
|
||||
|
||||
The plugin is built using webpack, which create the compiled code in `/dist`. The project is setup to use TypeScript, although you can change the configuration to use plain JavaScript.
|
||||
|
||||
## Building the plugin
|
||||
|
||||
To build the plugin, simply run `npm run dist`.
|
15
CliClient/tests/support/plugins/menu/api/Global.d.ts
vendored
Normal file
15
CliClient/tests/support/plugins/menu/api/Global.d.ts
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
import Plugin from '../Plugin';
|
||||
import Joplin from './Joplin';
|
||||
import Logger from 'lib/Logger';
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export default class Global {
|
||||
private joplin_;
|
||||
private requireWhiteList_;
|
||||
constructor(logger: Logger, implementation: any, plugin: Plugin, store: any);
|
||||
get joplin(): Joplin;
|
||||
private requireWhiteList;
|
||||
require(filePath: string): any;
|
||||
get process(): any;
|
||||
}
|
38
CliClient/tests/support/plugins/menu/api/Joplin.d.ts
vendored
Normal file
38
CliClient/tests/support/plugins/menu/api/Joplin.d.ts
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
import Plugin from '../Plugin';
|
||||
import JoplinData from './JoplinData';
|
||||
import JoplinPlugins from './JoplinPlugins';
|
||||
import JoplinWorkspace from './JoplinWorkspace';
|
||||
import JoplinFilters from './JoplinFilters';
|
||||
import JoplinCommands from './JoplinCommands';
|
||||
import JoplinViews from './JoplinViews';
|
||||
import JoplinInterop from './JoplinInterop';
|
||||
import JoplinSettings from './JoplinSettings';
|
||||
import Logger from 'lib/Logger';
|
||||
/**
|
||||
* This is the main entry point to the Joplin API. You can access various services using the provided accessors.
|
||||
*/
|
||||
export default class Joplin {
|
||||
private data_;
|
||||
private plugins_;
|
||||
private workspace_;
|
||||
private filters_;
|
||||
private commands_;
|
||||
private views_;
|
||||
private interop_;
|
||||
private settings_;
|
||||
constructor(logger: Logger, implementation: any, plugin: Plugin, store: any);
|
||||
get data(): JoplinData;
|
||||
get plugins(): JoplinPlugins;
|
||||
get workspace(): JoplinWorkspace;
|
||||
/**
|
||||
* @ignore
|
||||
*
|
||||
* Not sure if it's the best way to hook into the app
|
||||
* so for now disable filters.
|
||||
*/
|
||||
get filters(): JoplinFilters;
|
||||
get commands(): JoplinCommands;
|
||||
get views(): JoplinViews;
|
||||
get interop(): JoplinInterop;
|
||||
get settings(): JoplinSettings;
|
||||
}
|
51
CliClient/tests/support/plugins/menu/api/JoplinCommands.d.ts
vendored
Normal file
51
CliClient/tests/support/plugins/menu/api/JoplinCommands.d.ts
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
import { Command } from './types';
|
||||
/**
|
||||
* This class allows executing or registering new Joplin commands. Commands can be executed or associated with
|
||||
* {@link JoplinViewsToolbarButtons | toolbar buttons} or {@link JoplinViewsMenuItems | menu items}.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/register_command)
|
||||
*
|
||||
* ## Executing Joplin's internal commands
|
||||
*
|
||||
* It is also possible to execute internal Joplin's commands which, as of now, are not well documented.
|
||||
* You can find the list directly on GitHub though at the following locations:
|
||||
*
|
||||
* https://github.com/laurent22/joplin/tree/dev/ElectronClient/gui/MainScreen/commands
|
||||
* https://github.com/laurent22/joplin/tree/dev/ElectronClient/commands
|
||||
* https://github.com/laurent22/joplin/tree/dev/ElectronClient/gui/NoteEditor/commands/editorCommandDeclarations.ts
|
||||
*
|
||||
* To view what arguments are supported, you can open any of these files and look at the `execute()` command.
|
||||
*/
|
||||
export default class JoplinCommands {
|
||||
/**
|
||||
* <span class="platform-desktop">desktop</span> Executes the given command.
|
||||
* The `props` are the arguments passed to the command, and they vary based on the command
|
||||
*
|
||||
* ```typescript
|
||||
* // Create a new note in the current notebook:
|
||||
* await joplin.commands.execute('newNote');
|
||||
*
|
||||
* // Create a new sub-notebook under the provided notebook
|
||||
* // Note: internally, notebooks are called "folders".
|
||||
* await joplin.commands.execute('newFolder', { parent_id: "SOME_FOLDER_ID" });
|
||||
* ```
|
||||
*/
|
||||
execute(commandName: string, props?: any): Promise<any>;
|
||||
/**
|
||||
* <span class="platform-desktop">desktop</span> Registers a new command.
|
||||
*
|
||||
* ```typescript
|
||||
* // Register a new commmand called "testCommand1"
|
||||
*
|
||||
* await joplin.commands.register({
|
||||
* name: 'testCommand1',
|
||||
* label: 'My Test Command 1',
|
||||
* iconName: 'fas fa-music',
|
||||
* execute: () => {
|
||||
* alert('Testing plugin command 1');
|
||||
* },
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
register(command: Command): Promise<void>;
|
||||
}
|
47
CliClient/tests/support/plugins/menu/api/JoplinData.d.ts
vendored
Normal file
47
CliClient/tests/support/plugins/menu/api/JoplinData.d.ts
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
import { Path } from './types';
|
||||
/**
|
||||
* This module provides access to the Joplin data API: https://joplinapp.org/api/references/rest_api/
|
||||
* This is the main way to retrieve data, such as notes, notebooks, tags, etc.
|
||||
* or to update them or delete them.
|
||||
*
|
||||
* This is also what you would use to search notes, via the `search` endpoint.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/simple)
|
||||
*
|
||||
* In general you would use the methods in this class as if you were using a REST API. There are four methods that map to GET, POST, PUT and DELETE calls.
|
||||
* And each method takes these parameters:
|
||||
*
|
||||
* * `path`: This is an array that represents the path to the resource in the form `["resouceName", "resourceId", "resourceLink"]` (eg. ["tags", ":id", "notes"]). The "resources" segment is the name of the resources you want to access (eg. "notes", "folders", etc.). If not followed by anything, it will refer to all the resources in that collection. The optional "resourceId" points to a particular resources within the collection. Finally, an optional "link" can be present, which links the resource to a collection of resources. This can be used in the API for example to retrieve all the notes associated with a tag.
|
||||
* * `query`: (Optional) The query parameters. In a URL, this is the part after the question mark "?". In this case, it should be an object with key/value pairs.
|
||||
* * `data`: (Optional) Applies to PUT and POST calls only. The request body contains the data you want to create or modify, for example the content of a note or folder.
|
||||
* * `files`: (Optional) Used to create new resources and associate them with files.
|
||||
*
|
||||
* Please refer to the [Joplin API documentation](https://joplinapp.org/api/references/rest_api/) for complete details about each call. As the plugin runs within the Joplin application **you do not need an authorisation token** to use this API.
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
* ```typescript
|
||||
* // Get a note ID, title and body
|
||||
* const noteId = 'some_note_id';
|
||||
* const note = await joplin.data.get(['notes', noteId], { fields: ['id', 'title', 'body'] });
|
||||
*
|
||||
* // Get all folders
|
||||
* const folders = await joplin.data.get(['folders']);
|
||||
*
|
||||
* // Set the note body
|
||||
* await joplin.data.put(['notes', noteId], null, { body: "New note body" });
|
||||
*
|
||||
* // Create a new note under one of the folders
|
||||
* await joplin.data.post(['notes'], null, { body: "my new note", title: "some title", parent_id: folders[0].id });
|
||||
* ```
|
||||
*/
|
||||
export default class JoplinData {
|
||||
private api_;
|
||||
private pathSegmentRegex_;
|
||||
private serializeApiBody;
|
||||
private pathToString;
|
||||
get(path: Path, query?: any): Promise<any>;
|
||||
post(path: Path, query?: any, body?: any, files?: any[]): Promise<any>;
|
||||
put(path: Path, query?: any, body?: any, files?: any[]): Promise<any>;
|
||||
delete(path: Path, query?: any): Promise<any>;
|
||||
}
|
10
CliClient/tests/support/plugins/menu/api/JoplinFilters.d.ts
vendored
Normal file
10
CliClient/tests/support/plugins/menu/api/JoplinFilters.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
/**
|
||||
* @ignore
|
||||
*
|
||||
* Not sure if it's the best way to hook into the app
|
||||
* so for now disable filters.
|
||||
*/
|
||||
export default class JoplinFilters {
|
||||
on(name: string, callback: Function): Promise<void>;
|
||||
off(name: string, callback: Function): Promise<void>;
|
||||
}
|
17
CliClient/tests/support/plugins/menu/api/JoplinInterop.d.ts
vendored
Normal file
17
CliClient/tests/support/plugins/menu/api/JoplinInterop.d.ts
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
import { ExportModule, ImportModule } from './types';
|
||||
/**
|
||||
* Provides a way to create modules to import external data into Joplin or to export notes into any arbitrary format.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/json_export)
|
||||
*
|
||||
* To implement an import or export module, you would simply define an object with various event handlers that are called
|
||||
* by the application during the import/export process.
|
||||
*
|
||||
* See the documentation of the [[ExportModule]] and [[ImportModule]] for more information.
|
||||
*
|
||||
* You may also want to refer to the Joplin API documentation to see the list of properties for each item (note, notebook, etc.) - https://joplinapp.org/api/references/rest_api/
|
||||
*/
|
||||
export default class JoplinInterop {
|
||||
registerExportModule(module: ExportModule): Promise<void>;
|
||||
registerImportModule(module: ImportModule): Promise<void>;
|
||||
}
|
24
CliClient/tests/support/plugins/menu/api/JoplinPlugins.d.ts
vendored
Normal file
24
CliClient/tests/support/plugins/menu/api/JoplinPlugins.d.ts
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
import Plugin from '../Plugin';
|
||||
import Logger from 'lib/Logger';
|
||||
import { Script } from './types';
|
||||
/**
|
||||
* This class provides access to plugin-related features.
|
||||
*/
|
||||
export default class JoplinPlugins {
|
||||
private logger;
|
||||
private plugin;
|
||||
constructor(logger: Logger, plugin: Plugin);
|
||||
/**
|
||||
* Registers a new plugin. This is the entry point when creating a plugin. You should pass a simple object with an `onStart` method to it.
|
||||
* That `onStart` method will be executed as soon as the plugin is loaded.
|
||||
*
|
||||
* ```typescript
|
||||
* joplin.plugins.register({
|
||||
* onStart: async function() {
|
||||
* // Run your plugin code here
|
||||
* }
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
register(script: Script): Promise<void>;
|
||||
}
|
43
CliClient/tests/support/plugins/menu/api/JoplinSettings.d.ts
vendored
Normal file
43
CliClient/tests/support/plugins/menu/api/JoplinSettings.d.ts
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
import Plugin from '../Plugin';
|
||||
import { SettingItem, SettingSection } from './types';
|
||||
/**
|
||||
* This API allows registering new settings and setting sections, as well as getting and setting settings. Once a setting has been registered it will appear in the config screen and be editable by the user.
|
||||
*
|
||||
* Settings are essentially key/value pairs.
|
||||
*
|
||||
* Note: Currently this API does **not** provide access to Joplin's built-in settings. This is by design as plugins that modify user settings could give unexpected results
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/settings)
|
||||
*/
|
||||
export default class JoplinSettings {
|
||||
private plugin_;
|
||||
constructor(plugin: Plugin);
|
||||
private namespacedKey;
|
||||
/**
|
||||
* Registers a new setting. Note that registering a setting item is dynamic and will be gone next time Joplin starts.
|
||||
* What it means is that you need to register the setting every time the plugin starts (for example in the onStart event).
|
||||
* The setting value however will be preserved from one launch to the next so there is no risk that it will be lost even if for some
|
||||
* reason the plugin fails to start at some point.
|
||||
*/
|
||||
registerSetting(key: string, settingItem: SettingItem): Promise<void>;
|
||||
/**
|
||||
* Registers a new setting section. Like for registerSetting, it is dynamic and needs to be done every time the plugin starts.
|
||||
*/
|
||||
registerSection(name: string, section: SettingSection): Promise<void>;
|
||||
/**
|
||||
* Gets a setting value (only applies to setting you registered from your plugin)
|
||||
*/
|
||||
value(key: string): Promise<any>;
|
||||
/**
|
||||
* Sets a setting value (only applies to setting you registered from your plugin)
|
||||
*/
|
||||
setValue(key: string, value: any): Promise<void>;
|
||||
/**
|
||||
* Gets a global setting value, including app-specific settings and those set by other plugins.
|
||||
*
|
||||
* The list of available settings is not documented yet, but can be found by looking at the source code:
|
||||
*
|
||||
* https://github.com/laurent22/joplin/blob/3539a452a359162c461d2849829d2d42973eab50/ReactNativeClient/lib/models/Setting.ts#L142
|
||||
*/
|
||||
globalValue(key: string): Promise<any>;
|
||||
}
|
28
CliClient/tests/support/plugins/menu/api/JoplinViews.d.ts
vendored
Normal file
28
CliClient/tests/support/plugins/menu/api/JoplinViews.d.ts
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
import Plugin from '../Plugin';
|
||||
import JoplinViewsDialogs from './JoplinViewsDialogs';
|
||||
import JoplinViewsMenuItems from './JoplinViewsMenuItems';
|
||||
import JoplinViewsMenus from './JoplinViewsMenus';
|
||||
import JoplinViewsToolbarButtons from './JoplinViewsToolbarButtons';
|
||||
import JoplinViewsPanels from './JoplinViewsPanels';
|
||||
/**
|
||||
* This namespace provides access to view-related services.
|
||||
*
|
||||
* All view services provide a `create()` method which you would use to create the view object, whether it's a dialog, a toolbar button or a menu item.
|
||||
* In some cases, the `create()` method will return a [[ViewHandle]], which you would use to act on the view, for example to set certain properties or call some methods.
|
||||
*/
|
||||
export default class JoplinViews {
|
||||
private store;
|
||||
private plugin;
|
||||
private dialogs_;
|
||||
private panels_;
|
||||
private menuItems_;
|
||||
private menus_;
|
||||
private toolbarButtons_;
|
||||
private implementation_;
|
||||
constructor(implementation: any, plugin: Plugin, store: any);
|
||||
get dialogs(): JoplinViewsDialogs;
|
||||
get panels(): JoplinViewsPanels;
|
||||
get menuItems(): JoplinViewsMenuItems;
|
||||
get menus(): JoplinViewsMenus;
|
||||
get toolbarButtons(): JoplinViewsToolbarButtons;
|
||||
}
|
36
CliClient/tests/support/plugins/menu/api/JoplinViewsDialogs.d.ts
vendored
Normal file
36
CliClient/tests/support/plugins/menu/api/JoplinViewsDialogs.d.ts
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
import Plugin from '../Plugin';
|
||||
import { ButtonSpec, ViewHandle, ButtonId } from './types';
|
||||
/**
|
||||
* Allows creating and managing dialogs. A dialog is modal window that contains a webview and a row of buttons. You can update the update the webview using the `setHtml` method.
|
||||
* Dialogs are hidden by default and you need to call `open()` to open them. Once the user clicks on a button, the `open` call will return and provide the button ID that was
|
||||
* clicked on. There is currently no "close" method since the dialog should be thought as a modal one and thus can only be closed by clicking on one of the buttons.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/dialog)
|
||||
*/
|
||||
export default class JoplinViewsDialogs {
|
||||
private store;
|
||||
private plugin;
|
||||
private implementation_;
|
||||
constructor(implementation: any, plugin: Plugin, store: any);
|
||||
private controller;
|
||||
/**
|
||||
* Creates a new dialog
|
||||
*/
|
||||
create(): Promise<ViewHandle>;
|
||||
/**
|
||||
* Displays a message box with OK/Cancel buttons. Returns the button index that was clicked - "0" for OK and "1" for "Cancel"
|
||||
*/
|
||||
showMessageBox(message: string): Promise<number>;
|
||||
/**
|
||||
* Sets the dialog HTML content
|
||||
*/
|
||||
setHtml(handle: ViewHandle, html: string): Promise<string>;
|
||||
/**
|
||||
* Sets the dialog buttons.
|
||||
*/
|
||||
setButtons(handle: ViewHandle, buttons: ButtonSpec[]): Promise<ButtonSpec[]>;
|
||||
/**
|
||||
* Opens the dialog
|
||||
*/
|
||||
open(handle: ViewHandle): Promise<ButtonId>;
|
||||
}
|
16
CliClient/tests/support/plugins/menu/api/JoplinViewsMenuItems.d.ts
vendored
Normal file
16
CliClient/tests/support/plugins/menu/api/JoplinViewsMenuItems.d.ts
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
import { CreateMenuItemOptions, MenuItemLocation } from './types';
|
||||
import Plugin from '../Plugin';
|
||||
/**
|
||||
* Allows creating and managing menu items.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/register_command)
|
||||
*/
|
||||
export default class JoplinViewsMenuItems {
|
||||
private store;
|
||||
private plugin;
|
||||
constructor(plugin: Plugin, store: any);
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
create(commandName: string, location?: MenuItemLocation, options?: CreateMenuItemOptions): Promise<void>;
|
||||
}
|
18
CliClient/tests/support/plugins/menu/api/JoplinViewsMenus.d.ts
vendored
Normal file
18
CliClient/tests/support/plugins/menu/api/JoplinViewsMenus.d.ts
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
import { MenuItem, MenuItemLocation } from './types';
|
||||
import Plugin from '../Plugin';
|
||||
/**
|
||||
* Allows creating menus.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/menu)
|
||||
*/
|
||||
export default class JoplinViewsMenus {
|
||||
private store;
|
||||
private plugin;
|
||||
constructor(plugin: Plugin, store: any);
|
||||
private registerCommandAccelerators;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
create(label: string, menuItems: MenuItem[], location?: MenuItemLocation): Promise<void>;
|
||||
}
|
30
CliClient/tests/support/plugins/menu/api/JoplinViewsPanels.d.ts
vendored
Normal file
30
CliClient/tests/support/plugins/menu/api/JoplinViewsPanels.d.ts
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
import Plugin from '../Plugin';
|
||||
import { ViewHandle } from './types';
|
||||
/**
|
||||
* Allows creating and managing view panels. View panels currently are displayed at the right of the sidebar and allows displaying any HTML content (within a webview) and update it in real-time. For example
|
||||
* it could be used to display a table of content for the active note, or display various metadata or graph.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/toc)
|
||||
*/
|
||||
export default class JoplinViewsPanels {
|
||||
private store;
|
||||
private plugin;
|
||||
constructor(plugin: Plugin, store: any);
|
||||
private controller;
|
||||
/**
|
||||
* Creates a new panel
|
||||
*/
|
||||
create(): Promise<ViewHandle>;
|
||||
/**
|
||||
* Sets the panel webview HTML
|
||||
*/
|
||||
setHtml(handle: ViewHandle, html: string): Promise<string>;
|
||||
/**
|
||||
* Adds and loads a new JS or CSS files into the panel.
|
||||
*/
|
||||
addScript(handle: ViewHandle, scriptPath: string): Promise<void>;
|
||||
/**
|
||||
* Called when a message is sent from the webview (using postMessage).
|
||||
*/
|
||||
onMessage(handle: ViewHandle, callback: Function): Promise<void>;
|
||||
}
|
16
CliClient/tests/support/plugins/menu/api/JoplinViewsToolbarButtons.d.ts
vendored
Normal file
16
CliClient/tests/support/plugins/menu/api/JoplinViewsToolbarButtons.d.ts
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
import { ToolbarButtonLocation } from './types';
|
||||
import Plugin from '../Plugin';
|
||||
/**
|
||||
* Allows creating and managing toolbar buttons.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/register_command)
|
||||
*/
|
||||
export default class JoplinViewsToolbarButtons {
|
||||
private store;
|
||||
private plugin;
|
||||
constructor(plugin: Plugin, store: any);
|
||||
/**
|
||||
* Creates a new toolbar button and associate it with the given command.
|
||||
*/
|
||||
create(commandName: string, location: ToolbarButtonLocation): Promise<void>;
|
||||
}
|
34
CliClient/tests/support/plugins/menu/api/JoplinWorkspace.d.ts
vendored
Normal file
34
CliClient/tests/support/plugins/menu/api/JoplinWorkspace.d.ts
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* The workspace service provides access to all the parts of Joplin that are being worked on - i.e. the currently selected notes or notebooks as well
|
||||
* as various related events, such as when a new note is selected, or when the note content changes.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins)
|
||||
*/
|
||||
export default class JoplinWorkspace {
|
||||
private store;
|
||||
constructor(_implementation: any, store: any);
|
||||
/**
|
||||
* Called when a new note or notes are selected.
|
||||
*/
|
||||
onNoteSelectionChange(callback: Function): Promise<void>;
|
||||
/**
|
||||
* Called when the content of a note changes.
|
||||
*/
|
||||
onNoteContentChange(callback: Function): Promise<void>;
|
||||
/**
|
||||
* Called when an alarm associated with a to-do is triggered.
|
||||
*/
|
||||
onNoteAlarmTrigger(callback: Function): Promise<void>;
|
||||
/**
|
||||
* Called when the synchronisation process has finished.
|
||||
*/
|
||||
onSyncComplete(callback: Function): Promise<void>;
|
||||
/**
|
||||
* Gets the currently selected note
|
||||
*/
|
||||
selectedNote(): Promise<any>;
|
||||
/**
|
||||
* Gets the IDs of the selected notes (can be zero, one, or many). Use the data API to retrieve information about these notes.
|
||||
*/
|
||||
selectedNoteIds(): Promise<string[]>;
|
||||
}
|
5
CliClient/tests/support/plugins/menu/api/index.ts
Normal file
5
CliClient/tests/support/plugins/menu/api/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import type Joplin from './Joplin';
|
||||
|
||||
declare const joplin:Joplin;
|
||||
|
||||
export default joplin;
|
263
CliClient/tests/support/plugins/menu/api/types.ts
Normal file
263
CliClient/tests/support/plugins/menu/api/types.ts
Normal file
@ -0,0 +1,263 @@
|
||||
// =================================================================
|
||||
// Command API types
|
||||
// =================================================================
|
||||
|
||||
export interface Command {
|
||||
name: string
|
||||
label: string
|
||||
iconName?: string,
|
||||
execute(props:any):Promise<any>
|
||||
isEnabled?(props:any):boolean
|
||||
mapStateToProps?(state:any):any
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Interop API types
|
||||
// =================================================================
|
||||
|
||||
export enum FileSystemItem {
|
||||
File = 'file',
|
||||
Directory = 'directory',
|
||||
}
|
||||
|
||||
export enum ImportModuleOutputFormat {
|
||||
Markdown = 'md',
|
||||
Html = 'html',
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to implement a module to export data from Joplin. [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/json_export) for an example.
|
||||
*
|
||||
* In general, all the event handlers you'll need to implement take a `context` object as a first argument. This object will contain the export or import path as well as various optional properties, such as which notes or notebooks need to be exported.
|
||||
*
|
||||
* To get a better sense of what it will contain it can be useful to print it using `console.info(context)`.
|
||||
*/
|
||||
export interface ExportModule {
|
||||
/**
|
||||
* The format to be exported, eg "enex", "jex", "json", etc.
|
||||
*/
|
||||
format: string,
|
||||
|
||||
/**
|
||||
* The description that will appear in the UI, for example in the menu item.
|
||||
*/
|
||||
description: string,
|
||||
|
||||
/**
|
||||
* Whether the module will export a single file or multiple files in a directory. It affects the open dialog that will be presented to the user when using your exporter.
|
||||
*/
|
||||
target: FileSystemItem,
|
||||
|
||||
/**
|
||||
* Only applies to single file exporters or importers
|
||||
* It tells whether the format can package multiple notes into one file.
|
||||
* For example JEX or ENEX can, but HTML cannot.
|
||||
*/
|
||||
isNoteArchive: boolean,
|
||||
|
||||
/**
|
||||
* The extensions of the files exported by your module. For example, it is `["htm", "html"]` for the HTML module, and just `["jex"]` for the JEX module.
|
||||
*/
|
||||
fileExtensions?: string[],
|
||||
|
||||
/**
|
||||
* Called when the export process starts.
|
||||
*/
|
||||
onInit(context:ExportContext): Promise<void>;
|
||||
|
||||
/**
|
||||
* Called when an item needs to be processed. An "item" can be any Joplin object, such as a note, a folder, a notebook, etc.
|
||||
*/
|
||||
onProcessItem(context:ExportContext, itemType:number, item:any):Promise<void>;
|
||||
|
||||
/**
|
||||
* Called when a resource file needs to be exported.
|
||||
*/
|
||||
onProcessResource(context:ExportContext, resource:any, filePath:string):Promise<void>;
|
||||
|
||||
/**
|
||||
* Called when the export process is done.
|
||||
*/
|
||||
onClose(context:ExportContext):Promise<void>;
|
||||
}
|
||||
|
||||
export interface ImportModule {
|
||||
/**
|
||||
* The format to be exported, eg "enex", "jex", "json", etc.
|
||||
*/
|
||||
format: string,
|
||||
|
||||
/**
|
||||
* The description that will appear in the UI, for example in the menu item.
|
||||
*/
|
||||
description: string,
|
||||
|
||||
/**
|
||||
* Only applies to single file exporters or importers
|
||||
* It tells whether the format can package multiple notes into one file.
|
||||
* For example JEX or ENEX can, but HTML cannot.
|
||||
*/
|
||||
isNoteArchive: boolean,
|
||||
|
||||
/**
|
||||
* The type of sources that are supported by the module. Tells whether the module can import files or directories or both.
|
||||
*/
|
||||
sources: FileSystemItem[],
|
||||
|
||||
/**
|
||||
* Tells the file extensions of the exported files.
|
||||
*/
|
||||
fileExtensions?: string[],
|
||||
|
||||
/**
|
||||
* Tells the type of notes that will be generated, either HTML or Markdown (default).
|
||||
*/
|
||||
outputFormat?: ImportModuleOutputFormat,
|
||||
|
||||
/**
|
||||
* Called when the import process starts. There is only one event handler within which you should import the complete data.
|
||||
*/
|
||||
onExec(context:ImportContext): Promise<void>;
|
||||
}
|
||||
|
||||
export interface ExportOptions {
|
||||
format?: string,
|
||||
path?:string,
|
||||
sourceFolderIds?: string[],
|
||||
sourceNoteIds?: string[],
|
||||
modulePath?:string,
|
||||
target?:FileSystemItem,
|
||||
}
|
||||
|
||||
export interface ExportContext {
|
||||
destPath: string,
|
||||
options: ExportOptions,
|
||||
|
||||
/**
|
||||
* You can attach your own custom data using this propery - it will then be passed to each event handler, allowing you to keep state from one event to the next.
|
||||
*/
|
||||
userData?: any,
|
||||
}
|
||||
|
||||
export interface ImportContext {
|
||||
sourcePath: string,
|
||||
options: any,
|
||||
warnings: string[],
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Misc types
|
||||
// =================================================================
|
||||
|
||||
export interface Script {
|
||||
onStart?(event:any):Promise<void>,
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Menu types
|
||||
// =================================================================
|
||||
|
||||
export interface CreateMenuItemOptions {
|
||||
accelerator: string,
|
||||
}
|
||||
|
||||
export enum MenuItemLocation {
|
||||
File = 'file',
|
||||
Edit = 'edit',
|
||||
View = 'view',
|
||||
Note = 'note',
|
||||
Tools = 'tools',
|
||||
Help = 'help',
|
||||
Context = 'context',
|
||||
}
|
||||
|
||||
export interface MenuItem {
|
||||
commandName?: string,
|
||||
accelerator?: string,
|
||||
submenu?: MenuItem[],
|
||||
label?: string,
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// View API types
|
||||
// =================================================================
|
||||
|
||||
export interface ButtonSpec {
|
||||
id: ButtonId,
|
||||
title?: string,
|
||||
onClick?():void,
|
||||
}
|
||||
|
||||
export type ButtonId = string;
|
||||
|
||||
export enum ToolbarButtonLocation {
|
||||
/**
|
||||
* This toolbar in the top right corner of the application. It applies to the note as a whole, including its metadata.
|
||||
*/
|
||||
NoteToolbar = 'noteToolbar',
|
||||
|
||||
/**
|
||||
* This toolbar is right above the text editor. It applies to the note body only.
|
||||
*/
|
||||
EditorToolbar = 'editorToolbar',
|
||||
}
|
||||
|
||||
export type ViewHandle = string;
|
||||
|
||||
export interface EditorCommand {
|
||||
name: string;
|
||||
value?: any;
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Settings types
|
||||
// =================================================================
|
||||
|
||||
export enum SettingItemType {
|
||||
Int = 1,
|
||||
String = 2,
|
||||
Bool = 3,
|
||||
Array = 4,
|
||||
Object = 5,
|
||||
Button = 6,
|
||||
}
|
||||
|
||||
// Redefine a simplified interface to mask internal details
|
||||
// and to remove function calls as they would have to be async.
|
||||
export interface SettingItem {
|
||||
value: any,
|
||||
type: SettingItemType,
|
||||
public: boolean,
|
||||
label:string,
|
||||
|
||||
description?:string,
|
||||
isEnum?: boolean,
|
||||
section?: string,
|
||||
options?:any,
|
||||
appTypes?:string[],
|
||||
secure?: boolean,
|
||||
advanced?: boolean,
|
||||
minimum?: number,
|
||||
maximum?: number,
|
||||
step?: number,
|
||||
}
|
||||
|
||||
export interface SettingSection {
|
||||
label: string,
|
||||
iconName?: string,
|
||||
description?: string,
|
||||
name?: string,
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Data API types
|
||||
// =================================================================
|
||||
|
||||
/**
|
||||
* An array of at least one element and at most three elements.
|
||||
*
|
||||
* [0]: Resource name (eg. "notes", "folders", "tags", etc.)
|
||||
* [1]: (Optional) Resource ID.
|
||||
* [2]: (Optional) Resource link.
|
||||
*/
|
||||
export type Path = string[];
|
4524
CliClient/tests/support/plugins/menu/package-lock.json
generated
Normal file
4524
CliClient/tests/support/plugins/menu/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
CliClient/tests/support/plugins/menu/package.json
Normal file
20
CliClient/tests/support/plugins/menu/package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "joplin_plugin",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"dist": "webpack",
|
||||
"postinstall": "npm run dist"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^14.0.14",
|
||||
"copy-webpack-plugin": "^6.1.0",
|
||||
"ts-loader": "^7.0.5",
|
||||
"typescript": "^3.9.3",
|
||||
"webpack": "^4.43.0",
|
||||
"webpack-cli": "^3.3.11"
|
||||
}
|
||||
}
|
25
CliClient/tests/support/plugins/menu/src/index.ts
Normal file
25
CliClient/tests/support/plugins/menu/src/index.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import joplin from 'api';
|
||||
|
||||
joplin.plugins.register({
|
||||
onStart: async function() {
|
||||
await joplin.views.menus.create('My Menu', [
|
||||
{
|
||||
commandName: "newNote",
|
||||
},
|
||||
{
|
||||
commandName: "newFolder",
|
||||
},
|
||||
{
|
||||
label: 'My sub-menu',
|
||||
submenu: [
|
||||
{
|
||||
commandName: 'print',
|
||||
},
|
||||
{
|
||||
commandName: 'setTags',
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
},
|
||||
});
|
8
CliClient/tests/support/plugins/menu/src/manifest.json
Normal file
8
CliClient/tests/support/plugins/menu/src/manifest.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"manifest_version": 1,
|
||||
"name": "Menu Test",
|
||||
"description": "",
|
||||
"version": "1.0.0",
|
||||
"author": "",
|
||||
"homepage_url": ""
|
||||
}
|
10
CliClient/tests/support/plugins/menu/tsconfig.json
Normal file
10
CliClient/tests/support/plugins/menu/tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist/",
|
||||
"module": "commonjs",
|
||||
"target": "es2015",
|
||||
"jsx": "react",
|
||||
"allowJs": true,
|
||||
"baseUrl": "."
|
||||
}
|
||||
}
|
44
CliClient/tests/support/plugins/menu/webpack.config.js
Normal file
44
CliClient/tests/support/plugins/menu/webpack.config.js
Normal file
@ -0,0 +1,44 @@
|
||||
const path = require('path');
|
||||
const CopyPlugin = require('copy-webpack-plugin');
|
||||
|
||||
module.exports = {
|
||||
mode: 'production',
|
||||
entry: './src/index.ts',
|
||||
target: 'node',
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
use: 'ts-loader',
|
||||
exclude: /node_modules/,
|
||||
},
|
||||
],
|
||||
},
|
||||
resolve: {
|
||||
alias: {
|
||||
api: path.resolve(__dirname, 'api')
|
||||
},
|
||||
extensions: [ '.tsx', '.ts', '.js' ],
|
||||
},
|
||||
output: {
|
||||
filename: 'index.js',
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
},
|
||||
plugins: [
|
||||
new CopyPlugin({
|
||||
patterns: [
|
||||
{
|
||||
from: "**/*",
|
||||
context: path.resolve(__dirname, 'src'),
|
||||
to: path.resolve(__dirname, 'dist'),
|
||||
globOptions: {
|
||||
ignore: [
|
||||
'**/*.ts',
|
||||
'**/*.tsx',
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
};
|
@ -5,14 +5,14 @@ import { stateUtils } from 'lib/reducer';
|
||||
import CommandService from 'lib/services/CommandService';
|
||||
import MenuUtils from 'lib/services/commands/MenuUtils';
|
||||
import KeymapService from 'lib/services/KeymapService';
|
||||
import { utils as pluginUtils, ViewInfo } from 'lib/services/plugins/reducer';
|
||||
import { PluginStates, utils as pluginUtils } from 'lib/services/plugins/reducer';
|
||||
import shim from 'lib/shim';
|
||||
import Setting from 'lib/models/Setting';
|
||||
import versionInfo from 'lib/versionInfo';
|
||||
import { Module } from 'lib/services/interop/types';
|
||||
import InteropServiceHelper from '../InteropServiceHelper';
|
||||
import { _ } from 'lib/locale';
|
||||
import { MenuItemLocation } from 'lib/services/plugins/api/types';
|
||||
import { MenuItem, MenuItemLocation } from 'lib/services/plugins/api/types';
|
||||
|
||||
const { connect } = require('react-redux');
|
||||
const { reg } = require('lib/registry.js');
|
||||
@ -23,6 +23,51 @@ const Menu = bridge().Menu;
|
||||
const PluginManager = require('lib/services/PluginManager');
|
||||
const TemplateUtils = require('lib/TemplateUtils');
|
||||
|
||||
const menuUtils = new MenuUtils(CommandService.instance());
|
||||
|
||||
function pluginMenuItemsCommandNames(menuItems:MenuItem[]):string[] {
|
||||
let output:string[] = [];
|
||||
for (const menuItem of menuItems) {
|
||||
if (menuItem.submenu) {
|
||||
output = output.concat(pluginMenuItemsCommandNames(menuItem.submenu));
|
||||
} else {
|
||||
if (menuItem.commandName) output.push(menuItem.commandName);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
function pluginCommandNames(plugins:PluginStates):string[] {
|
||||
let output:string[] = [];
|
||||
|
||||
for (const view of pluginUtils.viewsByType(plugins, 'menu')) {
|
||||
output = output.concat(pluginMenuItemsCommandNames(view.menuItems));
|
||||
}
|
||||
|
||||
for (const view of pluginUtils.viewsByType(plugins, 'menuItem')) {
|
||||
if (view.commandName) output.push(view.commandName);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function createPluginMenuTree(label:string, menuItems:MenuItem[], onMenuItemClick:Function) {
|
||||
const output:any = {
|
||||
label: label,
|
||||
submenu: [],
|
||||
};
|
||||
|
||||
for (const menuItem of menuItems) {
|
||||
if (menuItem.submenu) {
|
||||
output.submenu.push(createPluginMenuTree(menuItem.label, menuItem.submenu, onMenuItemClick));
|
||||
} else {
|
||||
output.submenu.push(menuUtils.commandToMenuItem(menuItem.commandName, onMenuItemClick));
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
dispatch: Function,
|
||||
menuItemProps: any,
|
||||
@ -36,7 +81,8 @@ interface Props {
|
||||
showNoteCounts: boolean,
|
||||
uncompletedTodosOnTop: boolean,
|
||||
showCompletedTodos: boolean,
|
||||
pluginMenuItemInfos: ViewInfo[],
|
||||
pluginMenuItems: any[],
|
||||
pluginMenus: any[],
|
||||
}
|
||||
|
||||
const commandNames:string[] = [
|
||||
@ -85,8 +131,6 @@ function menuItemSetEnabled(id:string, enabled:boolean) {
|
||||
menuItem.enabled = enabled;
|
||||
}
|
||||
|
||||
const menuUtils = new MenuUtils(CommandService.instance());
|
||||
|
||||
function useMenu(props:Props) {
|
||||
const [menu, setMenu] = useState(null);
|
||||
const [keymapLastChangeTime, setKeymapLastChangeTime] = useState(Date.now());
|
||||
@ -143,7 +187,7 @@ function useMenu(props:Props) {
|
||||
useEffect(() => {
|
||||
const keymapService = KeymapService.instance();
|
||||
|
||||
const pluginCommandNames = props.pluginMenuItemInfos.map((viewInfo:ViewInfo) => viewInfo.view.commandName);
|
||||
const pluginCommandNames = props.pluginMenuItems.map((view:any) => view.commandName);
|
||||
const menuItemDic = menuUtils.commandsToMenuItems(commandNames.concat(pluginCommandNames), (commandName:string) => onMenuItemClickRef.current(commandName));
|
||||
|
||||
const quitMenuItem = {
|
||||
@ -645,26 +689,36 @@ function useMenu(props:Props) {
|
||||
rootMenus[key].submenu = cleanUpSeparators(rootMenus[key].submenu);
|
||||
}
|
||||
|
||||
const pluginMenuItems = PluginManager.instance().menuItems();
|
||||
for (const item of pluginMenuItems) {
|
||||
const itemParent = rootMenus[item.parent] ? rootMenus[item.parent] : 'tools';
|
||||
itemParent.submenu.push(item);
|
||||
{
|
||||
// This is for GotoAnything only - should be refactored since this plugin manager is not used otherwise
|
||||
const pluginMenuItems = PluginManager.instance().menuItems();
|
||||
for (const item of pluginMenuItems) {
|
||||
const itemParent = rootMenus[item.parent] ? rootMenus[item.parent] : 'tools';
|
||||
itemParent.submenu.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
|
||||
const pluginViewInfos = props.pluginMenuItemInfos;
|
||||
|
||||
for (const info of pluginViewInfos) {
|
||||
const location:MenuItemLocation = info.view.location;
|
||||
for (const view of props.pluginMenuItems) {
|
||||
const location:MenuItemLocation = view.location;
|
||||
if (location === MenuItemLocation.Context) continue;
|
||||
|
||||
const itemParent = rootMenus[location];
|
||||
|
||||
if (!itemParent) {
|
||||
reg.logger().error('Menu item location does not exist: ', location, info);
|
||||
reg.logger().error('Menu item location does not exist: ', location, view);
|
||||
} else {
|
||||
itemParent.submenu.push(menuItemDic[info.view.commandName]);
|
||||
itemParent.submenu.push(menuItemDic[view.commandName]);
|
||||
}
|
||||
}
|
||||
|
||||
for (const view of props.pluginMenus) {
|
||||
if (view.location === MenuItemLocation.Context) continue;
|
||||
const itemParent = rootMenus[view.location];
|
||||
|
||||
if (!itemParent) {
|
||||
reg.logger().error('Menu location does not exist: ', location, view);
|
||||
} else {
|
||||
itemParent.submenu.push(createPluginMenuTree(view.label, view.menuItems, (commandName:string) => onMenuItemClickRef.current(commandName)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -729,7 +783,7 @@ function useMenu(props:Props) {
|
||||
} else {
|
||||
setMenu(Menu.buildFromTemplate(template));
|
||||
}
|
||||
}, [props.routeName, props.pluginMenuItemInfos, keymapLastChangeTime, modulesLastChangeTime]);
|
||||
}, [props.routeName, props.pluginMenuItems, props.pluginMenus, keymapLastChangeTime, modulesLastChangeTime]);
|
||||
|
||||
useEffect(() => {
|
||||
for (const commandName in props.menuItemProps) {
|
||||
@ -805,7 +859,7 @@ function MenuBar(props:Props):JSX.Element {
|
||||
|
||||
const mapStateToProps = (state:AppState) => {
|
||||
return {
|
||||
menuItemProps: menuUtils.commandsToMenuItemProps(state, commandNames),
|
||||
menuItemProps: menuUtils.commandsToMenuItemProps(state, commandNames.concat(pluginCommandNames(state.pluginService.plugins))),
|
||||
routeName: state.route.routeName,
|
||||
selectedFolderId: state.selectedFolderId,
|
||||
layoutButtonSequence: state.settings.layoutButtonSequence,
|
||||
@ -816,7 +870,8 @@ const mapStateToProps = (state:AppState) => {
|
||||
showNoteCounts: state.settings.showNoteCounts,
|
||||
uncompletedTodosOnTop: state.settings.uncompletedTodosOnTop,
|
||||
showCompletedTodos: state.settings.showCompletedTodos,
|
||||
pluginMenuItemInfos: stateUtils.selectArrayShallow({ array: pluginUtils.viewInfosByType(state.pluginService.plugins, 'menuItem') }, 'menuBar.pluginMenuItemInfos'),
|
||||
pluginMenuItems: stateUtils.selectArrayShallow({ array: pluginUtils.viewsByType(state.pluginService.plugins, 'menuItem') }, 'menuBar.pluginMenuItems'),
|
||||
pluginMenus: stateUtils.selectArrayShallow({ array: pluginUtils.viewsByType(state.pluginService.plugins, 'menu') }, 'menuBar.pluginMenus'),
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -172,12 +172,14 @@ export default class KeymapService extends BaseService {
|
||||
.replace(/Alt/g, this.platform === 'darwin' ? 'Option' : 'Alt');
|
||||
}
|
||||
|
||||
registerCommandAccelerator(commandName:string, accelerator:string) {
|
||||
public registerCommandAccelerator(commandName:string, accelerator:string) {
|
||||
// If the command is already registered, we don't register it again and
|
||||
// we don't update the accelerator. This is because it might have been
|
||||
// modified by the user and we don't want the plugin to overwrite this.
|
||||
if (this.keymap[commandName]) return;
|
||||
|
||||
if (!commandName) throw new Error('Cannot register an accelerator without a command name');
|
||||
|
||||
const validatedAccelerator = this.convertToPlatform(accelerator);
|
||||
this.validateAccelerator(validatedAccelerator);
|
||||
|
||||
|
@ -69,7 +69,7 @@ export default class MenuUtils {
|
||||
return KeymapService.instance();
|
||||
}
|
||||
|
||||
private commandToMenuItem(commandName:string, onClick:Function):MenuItem {
|
||||
public commandToMenuItem(commandName:string, onClick:Function):MenuItem {
|
||||
const command = this.service.commandByName(commandName);
|
||||
|
||||
const item:MenuItem = {
|
||||
|
26
ReactNativeClient/lib/services/plugins/MenuController.ts
Normal file
26
ReactNativeClient/lib/services/plugins/MenuController.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { MenuItem, MenuItemLocation } from './api/types';
|
||||
import ViewController from './ViewController';
|
||||
|
||||
export default class MenuController extends ViewController {
|
||||
|
||||
constructor(id:string, pluginId:string, store:any, label:string, menuItems: MenuItem[], location:MenuItemLocation) {
|
||||
super(id, pluginId, store);
|
||||
|
||||
this.store.dispatch({
|
||||
type: 'PLUGIN_VIEW_ADD',
|
||||
pluginId: pluginId,
|
||||
view: {
|
||||
id: this.handle,
|
||||
type: this.type,
|
||||
label: label,
|
||||
menuItems: menuItems,
|
||||
location: location,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public get type():string {
|
||||
return 'menu';
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import Plugin from '../Plugin';
|
||||
import JoplinViewsDialogs from './JoplinViewsDialogs';
|
||||
import JoplinViewsMenuItems from './JoplinViewsMenuItems';
|
||||
import JoplinViewsMenus from './JoplinViewsMenus';
|
||||
import JoplinViewsToolbarButtons from './JoplinViewsToolbarButtons';
|
||||
import JoplinViewsPanels from './JoplinViewsPanels';
|
||||
|
||||
@ -18,6 +19,7 @@ export default class JoplinViews {
|
||||
private dialogs_:JoplinViewsDialogs = null;
|
||||
private panels_:JoplinViewsPanels = null;
|
||||
private menuItems_:JoplinViewsMenuItems = null;
|
||||
private menus_:JoplinViewsMenus = null;
|
||||
private toolbarButtons_:JoplinViewsToolbarButtons = null;
|
||||
private implementation_:any = null;
|
||||
|
||||
@ -42,6 +44,11 @@ export default class JoplinViews {
|
||||
return this.menuItems_;
|
||||
}
|
||||
|
||||
public get menus():JoplinViewsMenus {
|
||||
if (!this.menus_) this.menus_ = new JoplinViewsMenus(this.plugin, this.store);
|
||||
return this.menus_;
|
||||
}
|
||||
|
||||
public get toolbarButtons():JoplinViewsToolbarButtons {
|
||||
if (!this.toolbarButtons_) this.toolbarButtons_ = new JoplinViewsToolbarButtons(this.plugin, this.store);
|
||||
return this.toolbarButtons_;
|
||||
|
@ -0,0 +1,45 @@
|
||||
import KeymapService from 'lib/services/KeymapService';
|
||||
import { MenuItem, MenuItemLocation } from './types';
|
||||
import MenuController from '../MenuController';
|
||||
import Plugin from '../Plugin';
|
||||
import createViewHandle from '../utils/createViewHandle';
|
||||
|
||||
/**
|
||||
* Allows creating menus.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/CliClient/tests/support/plugins/menu)
|
||||
*/
|
||||
export default class JoplinViewsMenus {
|
||||
|
||||
private store: any;
|
||||
private plugin: Plugin;
|
||||
|
||||
constructor(plugin: Plugin, store: any) {
|
||||
this.store = store;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
private registerCommandAccelerators(menuItems:MenuItem[]) {
|
||||
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.
|
||||
*/
|
||||
public async create(label:string, menuItems:MenuItem[], location:MenuItemLocation = MenuItemLocation.Tools) {
|
||||
const handle = createViewHandle(this.plugin);
|
||||
const controller = new MenuController(handle, this.plugin.id, this.store, label, menuItems, location);
|
||||
this.plugin.addViewController(controller);
|
||||
this.registerCommandAccelerators(menuItems);
|
||||
}
|
||||
|
||||
}
|
@ -154,17 +154,9 @@ export interface Script {
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// View API types
|
||||
// Menu types
|
||||
// =================================================================
|
||||
|
||||
export type ButtonId = string;
|
||||
|
||||
export interface ButtonSpec {
|
||||
id: ButtonId,
|
||||
title?: string,
|
||||
onClick?():void,
|
||||
}
|
||||
|
||||
export interface CreateMenuItemOptions {
|
||||
accelerator: string,
|
||||
}
|
||||
@ -179,6 +171,41 @@ export enum MenuItemLocation {
|
||||
Context = 'context',
|
||||
}
|
||||
|
||||
export interface MenuItem {
|
||||
/**
|
||||
* Command that should be associated with the menu item. All menu item should
|
||||
* have a command associated with them unless they are a sub-menu.
|
||||
*/
|
||||
commandName?: string,
|
||||
|
||||
/**
|
||||
* Accelerator associated with the menu item
|
||||
*/
|
||||
accelerator?: string,
|
||||
|
||||
/**
|
||||
* Menu items that should appear below this menu item. Allows creating a menu tree.
|
||||
*/
|
||||
submenu?: MenuItem[],
|
||||
|
||||
/**
|
||||
* Menu item label. If not specified, the command label will be used instead.
|
||||
*/
|
||||
label?: string,
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// View API types
|
||||
// =================================================================
|
||||
|
||||
export interface ButtonSpec {
|
||||
id: ButtonId,
|
||||
title?: string,
|
||||
onClick?():void,
|
||||
}
|
||||
|
||||
export type ButtonId = string;
|
||||
|
||||
export enum ToolbarButtonLocation {
|
||||
/**
|
||||
* This toolbar in the top right corner of the application. It applies to the note as a whole, including its metadata.
|
||||
|
@ -34,6 +34,9 @@ export const defaultState:State = {
|
||||
};
|
||||
|
||||
export const utils = {
|
||||
|
||||
// It is best to use viewsByType instead as this method creates new objects
|
||||
// which might trigger unecessary renders even when plugin and views haven't changed.
|
||||
viewInfosByType: function(plugins:PluginStates, type:string):ViewInfo[] {
|
||||
const output:ViewInfo[] = [];
|
||||
|
||||
@ -53,6 +56,22 @@ export const utils = {
|
||||
return output;
|
||||
},
|
||||
|
||||
viewsByType: function(plugins:PluginStates, type:string):any[] {
|
||||
const output:any[] = [];
|
||||
|
||||
for (const pluginId in plugins) {
|
||||
const plugin = plugins[pluginId];
|
||||
for (const viewId in plugin.views) {
|
||||
const view = plugin.views[viewId];
|
||||
if (view.type !== type) continue;
|
||||
|
||||
output.push(view);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
commandNamesFromViews: function(plugins:PluginStates, toolbarType:string):string[] {
|
||||
const infos = utils.viewInfosByType(plugins, 'toolbarButton');
|
||||
|
||||
|
@ -23,7 +23,7 @@ export default function manifestFromObject(o:any):PluginManifest {
|
||||
name: getString('name', true),
|
||||
version: getString('version', true),
|
||||
description: getString('description', false),
|
||||
homepage_url: getString('homepage_url'),
|
||||
homepage_url: getString('homepage_url', false),
|
||||
permissions: permissions,
|
||||
};
|
||||
|
||||
|
@ -697,7 +697,11 @@
|
||||
"ReactNativeClient/lib/markdownUtils.js": true,
|
||||
"ReactNativeClient/lib/services/plugins/api/types.js": true,
|
||||
"ReactNativeClient/lib/services/rest/Api.js": true,
|
||||
"CliClient/tests/services_rest_Api.js": true
|
||||
"CliClient/tests/services_rest_Api.js": true,
|
||||
"CliClient/tests/services/plugins/api/JoplinSetting.js": true,
|
||||
"ReactNativeClient/lib/components/BackButtonDialogBox.js": true,
|
||||
"ReactNativeClient/lib/services/plugins/api/JoplinViewsMenus.js": true,
|
||||
"ReactNativeClient/lib/services/plugins/MenuController.js": true
|
||||
},
|
||||
"spellright.language": [
|
||||
"en"
|
||||
|
Loading…
Reference in New Issue
Block a user