Skip to content

Commit ec7e024

Browse files
authored
Merge pull request #1288 from nextcloud-libraries/feat/allow-hotkey
feat(actions): allow to define hotkey for file action
2 parents 8444439 + ded8c3f commit ec7e024

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

lib/actions/fileAction.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,37 @@ export enum DefaultType {
1313
HIDDEN = 'hidden',
1414
}
1515

16+
export interface IHotkeyConfig {
17+
/**
18+
* Short, translated, description what this action is doing.
19+
* This will be used as the description next to the hotkey in the shortcuts overview.
20+
*/
21+
description: string
22+
23+
/**
24+
* The key to be pressed.
25+
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
26+
*/
27+
key: string
28+
29+
/**
30+
* If set then the callback is only called when the shift key is (not) pressed.
31+
* When left `undefined` a pressed shift key is ignored (callback is run with and without shift pressed).
32+
*/
33+
shift?: boolean
34+
35+
/**
36+
* Only execute the action if the control key is pressed.
37+
* On Mac devices the command key is used instead.
38+
*/
39+
ctrl?: true
40+
41+
/**
42+
* Only execute the action if the alt key is pressed
43+
*/
44+
alt?: true
45+
}
46+
1647
export interface FileActionData {
1748
/** Unique ID */
1849
id: string
@@ -41,7 +72,12 @@ export interface FileActionData {
4172
execBatch?: (files: Node[], view: View, dir: string) => Promise<(boolean|null)[]>
4273

4374
/** This action order in the list */
44-
order?: number,
75+
order?: number
76+
77+
/**
78+
* Allows to define a hotkey which will trigger this action for the selected node.
79+
*/
80+
hotkey?: IHotkeyConfig
4581

4682
/**
4783
* Set to true if this action is a destructive action, like "delete".
@@ -113,6 +149,10 @@ export class FileAction {
113149
return this._action.execBatch
114150
}
115151

152+
get hotkey() {
153+
return this._action.hotkey
154+
}
155+
116156
get order() {
117157
return this._action.order
118158
}
@@ -190,6 +230,20 @@ export class FileAction {
190230
if ('renderInline' in action && typeof action.renderInline !== 'function') {
191231
throw new Error('Invalid renderInline function')
192232
}
233+
234+
if ('hotkey' in action && action.hotkey !== undefined) {
235+
if (typeof action.hotkey !== 'object') {
236+
throw new Error('Invalid hotkey configuration')
237+
}
238+
239+
if (typeof action.hotkey.key !== 'string' || !action.hotkey.key) {
240+
throw new Error('Missing or invalid hotkey key')
241+
}
242+
243+
if (typeof action.hotkey.description !== 'string' || !action.hotkey.description) {
244+
throw new Error('Missing or invalid hotkey description')
245+
}
246+
}
193247
}
194248

195249
}

lib/actions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
export type { FileActionData } from './fileAction.ts'
6+
export type { FileActionData, IHotkeyConfig } from './fileAction.ts'
77

88
export { FileAction, getFileActions, registerFileAction, DefaultType } from './fileAction.ts'
99
export { getFileListActions, registerFileListAction, FileListAction } from './fileListAction.ts'

0 commit comments

Comments
 (0)