2020-10-09 19:35:46 +02:00
|
|
|
import eventManager from 'lib/eventManager';
|
|
|
|
import markdownUtils, { MarkdownTableHeader, MarkdownTableRow } from 'lib/markdownUtils';
|
|
|
|
import BaseService from 'lib/services/BaseService';
|
|
|
|
import shim from 'lib/shim';
|
|
|
|
|
|
|
|
type LabelFunction = () => string;
|
2020-07-03 23:32:39 +02:00
|
|
|
|
|
|
|
export interface CommandRuntime {
|
2020-10-09 19:35:46 +02:00
|
|
|
execute(props:any):Promise<any>
|
2020-07-03 23:32:39 +02:00
|
|
|
isEnabled?(props:any):boolean
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// "state" type is "AppState" but in order not to introduce a
|
|
|
|
// dependency to the desktop app (so that the service can
|
|
|
|
// potentially be used by the mobile app too), we keep it as "any".
|
|
|
|
// Individual commands can define it as state:AppState when relevant.
|
|
|
|
//
|
|
|
|
// In general this method should reduce the provided state to only
|
|
|
|
// what's absolutely necessary. For example, if the property of a
|
|
|
|
// note is needed, return only that particular property and not the
|
|
|
|
// whole note object. This will ensure that components that depends
|
|
|
|
// on this command are not uncessarily re-rendered. A note object for
|
|
|
|
// example might change frequently but its markdown_language property
|
|
|
|
// will almost never change.
|
2020-07-03 23:32:39 +02:00
|
|
|
mapStateToProps?(state:any):any
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
// Used for the (optional) toolbar button title
|
|
|
|
title?(props:any):string,
|
2020-10-09 19:35:46 +02:00
|
|
|
// props?:any
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CommandDeclaration {
|
|
|
|
name: string
|
2020-09-13 18:21:11 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
// Used for the menu item label, and toolbar button tooltip
|
2020-10-09 19:35:46 +02:00
|
|
|
label?: LabelFunction | string,
|
2020-09-13 18:21:11 +02:00
|
|
|
|
|
|
|
// This is a bit of a hack because some labels don't make much sense in isolation. For example,
|
|
|
|
// the commmand to focus the note list is called just "Note list". This makes sense within the menu
|
|
|
|
// but not so much within the keymap config screen, where the parent item is not displayed. Because
|
|
|
|
// of this we have this "parentLabel()" property to clarify the meaning of the certain items.
|
|
|
|
// For example, the focusElementNoteList will have these two properties:
|
|
|
|
// label() => _('Note list'),
|
|
|
|
// parentLabel() => _('Focus'),
|
|
|
|
// Which will be displayed as "Focus: Note list" in the keymap config screen.
|
2020-10-09 19:35:46 +02:00
|
|
|
parentLabel?:LabelFunction | string,
|
|
|
|
|
|
|
|
// All free Font Awesome icons are available: https://fontawesome.com/icons?d=gallery&m=free
|
2020-07-03 23:32:39 +02:00
|
|
|
iconName?: string,
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// Will be used by TinyMCE (which doesn't support Font Awesome icons).
|
|
|
|
// Defaults to the "preferences" icon (a cog) if not specified.
|
|
|
|
// https://www.tiny.cloud/docs/advanced/editor-icon-identifiers/
|
|
|
|
tinymceIconName?: string,
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
// Same as `role` key in Electron MenuItem:
|
|
|
|
// https://www.electronjs.org/docs/api/menu-item#new-menuitemoptions
|
|
|
|
// Note that due to a bug in Electron, menu items with a role cannot
|
|
|
|
// be disabled.
|
|
|
|
role?: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Command {
|
|
|
|
declaration: CommandDeclaration,
|
|
|
|
runtime?: CommandRuntime,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Commands {
|
|
|
|
[key:string]: Command;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ReduxStore {
|
|
|
|
dispatch(action:any):void;
|
|
|
|
getState():any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Utils {
|
|
|
|
store: ReduxStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const utils:Utils = {
|
|
|
|
store: {
|
|
|
|
dispatch: () => {},
|
|
|
|
getState: () => {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
interface CommandByNameOptions {
|
|
|
|
mustExist?:boolean,
|
|
|
|
runtimeMustBeRegistered?:boolean,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommandState {
|
|
|
|
title: string,
|
|
|
|
enabled: boolean,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommandStates {
|
|
|
|
[key:string]: CommandState
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class CommandService extends BaseService {
|
|
|
|
|
|
|
|
private static instance_:CommandService;
|
|
|
|
|
|
|
|
static instance():CommandService {
|
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new CommandService();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private commands_:Commands = {};
|
|
|
|
private commandPreviousStates_:CommandStates = {};
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
initialize(store:any) {
|
2020-07-03 23:32:39 +02:00
|
|
|
utils.store = store;
|
|
|
|
}
|
|
|
|
|
|
|
|
public on(eventName:string, callback:Function) {
|
|
|
|
eventManager.on(eventName, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public off(eventName:string, callback:Function) {
|
|
|
|
eventManager.off(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
public commandByName(name:string, options:CommandByNameOptions = null):Command {
|
2020-07-03 23:32:39 +02:00
|
|
|
options = {
|
|
|
|
mustExist: true,
|
|
|
|
runtimeMustBeRegistered: false,
|
2020-09-06 14:00:25 +02:00
|
|
|
...options,
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const command = this.commands_[name];
|
|
|
|
|
|
|
|
if (!command) {
|
|
|
|
if (options.mustExist) throw new Error(`Command not found: ${name}. Make sure the declaration has been registered.`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.runtimeMustBeRegistered && !command.runtime) throw new Error(`Runtime is not registered for command ${name}`);
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerDeclaration(declaration:CommandDeclaration) {
|
|
|
|
declaration = { ...declaration };
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!declaration.label) declaration.label = '';
|
2020-07-03 23:32:39 +02:00
|
|
|
if (!declaration.iconName) declaration.iconName = '';
|
|
|
|
|
|
|
|
this.commands_[declaration.name] = {
|
|
|
|
declaration: declaration,
|
|
|
|
};
|
2020-08-01 16:11:14 +02:00
|
|
|
|
|
|
|
delete this.commandPreviousStates_[declaration.name];
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
registerRuntime(commandName:string, runtime:CommandRuntime) {
|
|
|
|
if (typeof commandName !== 'string') throw new Error(`Command name must be a string. Got: ${JSON.stringify(commandName)}`);
|
|
|
|
|
|
|
|
const command = this.commandByName(commandName);
|
|
|
|
|
|
|
|
runtime = Object.assign({}, runtime);
|
|
|
|
if (!runtime.isEnabled) runtime.isEnabled = () => true;
|
|
|
|
if (!runtime.title) runtime.title = () => null;
|
|
|
|
command.runtime = runtime;
|
2020-08-01 16:11:14 +02:00
|
|
|
|
|
|
|
delete this.commandPreviousStates_[commandName];
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentRegisterCommands(component:any, commands:any[]) {
|
|
|
|
for (const command of commands) {
|
|
|
|
CommandService.instance().registerRuntime(command.declaration.name, command.runtime(component));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentUnregisterCommands(commands:any[]) {
|
|
|
|
for (const command of commands) {
|
|
|
|
CommandService.instance().unregisterRuntime(command.declaration.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unregisterRuntime(commandName:string) {
|
|
|
|
const command = this.commandByName(commandName, { mustExist: false });
|
|
|
|
if (!command || !command.runtime) return;
|
|
|
|
delete command.runtime;
|
2020-08-01 16:11:14 +02:00
|
|
|
|
|
|
|
delete this.commandPreviousStates_[commandName];
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async execute(commandName:string, props:any = null):Promise<any> {
|
2020-07-03 23:32:39 +02:00
|
|
|
const command = this.commandByName(commandName);
|
2020-10-09 19:35:46 +02:00
|
|
|
this.logger().info('CommandService::execute:', commandName, props);
|
|
|
|
return command.runtime.execute(props ? props : {});
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
scheduleExecute(commandName:string, args:any) {
|
|
|
|
shim.setTimeout(() => {
|
2020-07-03 23:32:39 +02:00
|
|
|
this.execute(commandName, args);
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
isEnabled(commandName:string, props:any):boolean {
|
2020-07-03 23:32:39 +02:00
|
|
|
const command = this.commandByName(commandName);
|
|
|
|
if (!command || !command.runtime) return false;
|
2020-10-09 19:35:46 +02:00
|
|
|
// if (!command.runtime.props) return false;
|
|
|
|
return command.runtime.isEnabled(props);
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
commandMapStateToProps(commandName:string, state:any):any {
|
2020-07-03 23:32:39 +02:00
|
|
|
const command = this.commandByName(commandName);
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!command.runtime) return null;
|
|
|
|
if (!command.runtime.mapStateToProps) return {};
|
|
|
|
return command.runtime.mapStateToProps(state);
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
title(commandName:string, props:any):string {
|
|
|
|
const command = this.commandByName(commandName);
|
|
|
|
if (!command || !command.runtime) return null;
|
|
|
|
return command.runtime.title(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
iconName(commandName:string, variant:string = null):string {
|
2020-09-15 15:01:07 +02:00
|
|
|
const command = this.commandByName(commandName);
|
|
|
|
if (!command) throw new Error(`No such command: ${commandName}`);
|
2020-10-09 19:35:46 +02:00
|
|
|
if (variant === 'tinymce') return command.declaration.tinymceIconName ? command.declaration.tinymceIconName : 'preferences';
|
2020-09-15 15:01:07 +02:00
|
|
|
return command.declaration.iconName;
|
|
|
|
}
|
|
|
|
|
2020-09-13 18:21:11 +02:00
|
|
|
label(commandName:string, fullLabel:boolean = false):string {
|
2020-09-06 14:00:25 +02:00
|
|
|
const command = this.commandByName(commandName);
|
|
|
|
if (!command) throw new Error(`Command: ${commandName} is not declared`);
|
2020-09-13 18:21:11 +02:00
|
|
|
const output = [];
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
const parentLabel = (d:CommandDeclaration):string => {
|
|
|
|
if (!d.parentLabel) return '';
|
|
|
|
if (typeof d.parentLabel === 'function') return d.parentLabel();
|
|
|
|
return d.parentLabel as string;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (fullLabel && parentLabel(command.declaration)) output.push(parentLabel(command.declaration));
|
|
|
|
output.push(typeof command.declaration.label === 'function' ? command.declaration.label() : command.declaration.label);
|
2020-09-13 18:21:11 +02:00
|
|
|
return output.join(': ');
|
2020-09-06 14:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exists(commandName:string):boolean {
|
|
|
|
const command = this.commandByName(commandName, { mustExist: false });
|
|
|
|
return !!command;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
public commandsToMarkdownTable(state:any):string {
|
|
|
|
const headers:MarkdownTableHeader[] = [
|
|
|
|
{
|
|
|
|
name: 'commandName',
|
|
|
|
label: 'Name',
|
2020-07-03 23:32:39 +02:00
|
|
|
},
|
2020-10-09 19:35:46 +02:00
|
|
|
{
|
|
|
|
name: 'description',
|
|
|
|
label: 'Description',
|
2020-07-03 23:32:39 +02:00
|
|
|
},
|
2020-10-09 19:35:46 +02:00
|
|
|
{
|
|
|
|
name: 'props',
|
|
|
|
label: 'Props',
|
|
|
|
},
|
|
|
|
];
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const rows:MarkdownTableRow[] = [];
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
for (const commandName in this.commands_) {
|
|
|
|
const props = this.commandMapStateToProps(commandName, state);
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const row:MarkdownTableRow = {
|
|
|
|
commandName: commandName,
|
|
|
|
description: this.label(commandName),
|
|
|
|
props: JSON.stringify(props),
|
|
|
|
};
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
rows.push(row);
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
return markdownUtils.createMarkdownTable(headers, rows);
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|