diff --git a/lib/actions/fileAction.ts b/lib/actions/fileAction.ts index 5f76f66dc..a47ab5d0e 100644 --- a/lib/actions/fileAction.ts +++ b/lib/actions/fileAction.ts @@ -13,6 +13,37 @@ export enum DefaultType { HIDDEN = 'hidden', } +export interface IHotkeyConfig { + /** + * Short, translated, description what this action is doing. + * This will be used as the description next to the hotkey in the shortcuts overview. + */ + description: string + + /** + * The key to be pressed. + * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key + */ + key: string + + /** + * If set then the callback is only called when the shift key is (not) pressed. + * When left `undefined` a pressed shift key is ignored (callback is run with and without shift pressed). + */ + shift?: boolean + + /** + * Only execute the action if the control key is pressed. + * On Mac devices the command key is used instead. + */ + ctrl?: true + + /** + * Only execute the action if the alt key is pressed + */ + alt?: true +} + export interface FileActionData { /** Unique ID */ id: string @@ -41,7 +72,12 @@ export interface FileActionData { execBatch?: (files: Node[], view: View, dir: string) => Promise<(boolean|null)[]> /** This action order in the list */ - order?: number, + order?: number + + /** + * Allows to define a hotkey which will trigger this action for the selected node. + */ + hotkey?: IHotkeyConfig /** * Set to true if this action is a destructive action, like "delete". @@ -113,6 +149,10 @@ export class FileAction { return this._action.execBatch } + get hotkey() { + return this._action.hotkey + } + get order() { return this._action.order } @@ -190,6 +230,20 @@ export class FileAction { if ('renderInline' in action && typeof action.renderInline !== 'function') { throw new Error('Invalid renderInline function') } + + if ('hotkey' in action && action.hotkey !== undefined) { + if (typeof action.hotkey !== 'object') { + throw new Error('Invalid hotkey configuration') + } + + if (typeof action.hotkey.key !== 'string' || !action.hotkey.key) { + throw new Error('Missing or invalid hotkey key') + } + + if (typeof action.hotkey.description !== 'string' || !action.hotkey.description) { + throw new Error('Missing or invalid hotkey description') + } + } } } diff --git a/lib/actions/index.ts b/lib/actions/index.ts index dd994eaa2..afd067404 100644 --- a/lib/actions/index.ts +++ b/lib/actions/index.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -export type { FileActionData } from './fileAction.ts' +export type { FileActionData, IHotkeyConfig } from './fileAction.ts' export { FileAction, getFileActions, registerFileAction, DefaultType } from './fileAction.ts' export { getFileListActions, registerFileListAction, FileListAction } from './fileListAction.ts'