import { JSONObject } from '../algorithm/json'; import { IDisposable } from '../core/disposable'; import { ISignal } from '../core/signaling'; /** * A registry which holds user-defined commands. */ export declare class CommandRegistry { /** * Construct a new command registry. */ constructor(); /** * A signal emmitted when a command is changed. */ commandChanged: ISignal; /** * A signal emitted when a command is executed. */ commandExecuted: ISignal; /** * List the ids of the registered commands. * * @returns A new array of the registered command ids. */ listCommands(): string[]; /** * Test whether a specific command is registered. * * @param id - The id of the command of interest. * * @returns `true` if the command is registered, `false` otherwise. */ hasCommand(id: string): boolean; /** * Add a command to the registry. * * @param id - The unique id of the command. * * @param options - The options for the command. * * @returns A disposable which will unregister the command. * * @throws An error if the given `id` is already registered. */ addCommand(id: string, options: CommandRegistry.ICommandOptions): IDisposable; /** * Notify listeners that the state of a command has changed. * * @param id - The id of the command which has changed. * * #### Notes * This method should be called by the command author whenever the * application state changes such that the results of the command * metadata functions may have changed. * * This will cause the `commandChanged` signal to be emitted. * * If the command is not registered, this is a no-op. */ notifyCommandChanged(id: string): void; /** * Get the display label for a specific command. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns The display label for the command. * * #### Notes * Returns an empty string if the command is not registered. */ label(id: string, args: JSONObject): string; /** * Get the mnemonic index for a specific command. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns The mnemonic index for the command. * * #### Notes * Returns `-1` if the command is not registered. */ mnemonic(id: string, args: JSONObject): number; /** * Get the icon class for a specific command. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns The icon class for the command. * * #### Notes * Returns an empty string if the command is not registered. */ icon(id: string, args: JSONObject): string; /** * Get the short form caption for a specific command. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns The caption for the command. * * #### Notes * Returns an empty string if the command is not registered. */ caption(id: string, args: JSONObject): string; /** * Get the usage help text for a specific command. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns The usage text for the command. * * #### Notes * Returns an empty string if the command is not registered. */ usage(id: string, args: JSONObject): string; /** * Get the extra class name for a specific command. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns The class name for the command. * * #### Notes * Returns an empty string if the command is not registered. */ className(id: string, args: JSONObject): string; /** * Test whether a specific command is enabled. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns `true` if the command is enabled, `false` otherwise. * * #### Notes * Returns `false` if the command is not registered. */ isEnabled(id: string, args: JSONObject): boolean; /** * Test whether a specific command is toggled. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns `true` if the command is toggled, `false` otherwise. * * #### Notes * Returns `false` if the command is not registered. */ isToggled(id: string, args: JSONObject): boolean; /** * Test whether a specific command is visible. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns `true` if the command is visible, `false` otherwise. * * #### Notes * Returns `false` if the command is not registered. */ isVisible(id: string, args: JSONObject): boolean; /** * Execute a specific command. * * @param id - The id of the command of interest. * * @param args - The arguments for the command. * * @returns A promise which resolves to the result of the command. * * #### Notes * The promise will reject if the command is not registered. */ execute(id: string, args: JSONObject): Promise; private _commands; } /** * The namespace for the `CommandRegistry` class statics. */ export declare namespace CommandRegistry { /** * An arguments object for the `commandChanged` signal. */ interface ICommandChangedArgs { /** * The id of the associated command. */ id: string; /** * Whether the command was added, removed, or changed. */ type: 'added' | 'removed' | 'changed'; } /** * An arguments object for the `commandExecuted` signal. */ interface ICommandExecutedArgs { /** * The id of the associated command. */ id: string; /** * The arguments object passed to the command. */ args: JSONObject; } /** * A type alias for a command execute function. * * @param args - The arguments for the command. * * @returns The command result, a promise to the result, or void. */ type ExecFunc = (args: JSONObject) => any; /** * A type alias for a command string function. * * @param args - The arguments for the command. * * @returns The relevant string result. */ type StringFunc = (args: JSONObject) => string; /** * A type alias for a command number function. * * @param args - The arguments for the command. * * @returns The relevant number result. */ type NumberFunc = (args: JSONObject) => number; /** * A type alias for a command boolean function. * * @param args - The arguments for the command. * * @returns The relevant boolean result. */ type BoolFunc = (args: JSONObject) => boolean; /** * An options object for creating a command. * * #### Notes * A command is an abstract representation of code to be executed along * with metadata for describing how the command should be displayed in * a visual representation. * * A command is a collection of functions, *not* methods. The command * registry will always invoke the command functions with a `thisArg` * which is `undefined`. * * The metadata functions for the command should be well behaved in * the presence of `null` args by returning a default value. */ interface ICommandOptions { /** * The function to invoke when the command is executed. * * #### Notes * This function may be invoked manually by the user, even when * the `isEnabled` function returns `false`. */ execute: ExecFunc; /** * The label text for the command. * * #### Notes * This can be a string literal, or a function which returns the * label based on the provided command arguments. * * The default value is an empty string. */ label?: string | StringFunc; /** * The index of the mnemonic character for the command. * * #### Notes * This can be an index literal, or a function which returns the * mnemonic index based on the provided command arguments. * * The default value is `-1`. */ mnemonic?: number | NumberFunc; /** * The icon class for the command. * * #### Notes * This class name will be added to the icon node for the visual * representation of the command. * * Multiple class names can be separated with white space. * * This can be a string literal, or a function which returns the * icon based on the provided command arguments. * * The default value is an empty string. */ icon?: string | StringFunc; /** * The caption for the command. * * #### Notes * This should be a simple one line description of the command. It * is used by some visual representations to show quick info about * the command. * * This can be a string literal, or a function which returns the * caption based on the provided command arguments. * * The default value is an empty string. */ caption?: string | StringFunc; /** * The usage text for the command. * * #### Notes * This should be a full description of the command, which includes * information about the structure of the arguments and the type of * the return value. It is used by some visual representations when * displaying complete help info about the command. * * This can be a string literal, or a function which returns the * usage text based on the provided command arguments. * * The default value is an empty string. */ usage?: string | StringFunc; /** * The general class name for the command. * * #### Notes * This class name will be added to the primary node for the visual * representation of the command. * * Multiple class names can be separated with white space. * * This can be a string literal, or a function which returns the * class name based on the provided command arguments. * * The default value is an empty string. */ className?: string | StringFunc; /** * A function which indicates whether the command is enabled. * * #### Notes * Visual representations may use this value to display a disabled * command as grayed-out or in some other non-interactive fashion. * * The default value is `true`. */ isEnabled?: BoolFunc; /** * A function which indicates whether the command is toggled. * * #### Notes * Visual representations may use this value to display a toggled * command in a different form, such as a check mark icon for a * menu item or a depressed state for a toggle button. * * The default value is `false`. */ isToggled?: BoolFunc; /** * A function which indicates whether the command is visible. * * #### Notes * Visual representations may use this value to hide or otherwise * not display a non-visible command. * * The default value is `true`. */ isVisible?: BoolFunc; } }