Skip to content

Commit

Permalink
Merge pull request #814 from nextcloud-libraries/feat/nested-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv committed Oct 25, 2023
2 parents 52467b5 + c0a5e91 commit 6aa33d3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions __tests__/fileAction.spec.ts
Expand Up @@ -173,6 +173,17 @@ describe('Invalid FileAction creation', () => {
} as any as FileAction)
}).toThrowError('Invalid order')
})
test('Invalid parent', () => {
expect(() => {
new FileAction({
id: 'test',
displayName: () => 'Test',
iconSvgInline: () => '<svg></svg>',
exec: async () => true,
parent: true,
} as any as FileAction)
}).toThrowError('Invalid parent')
})
test('Invalid default', () => {
expect(() => {
new FileAction({
Expand Down
16 changes: 16 additions & 0 deletions lib/fileAction.ts
Expand Up @@ -40,6 +40,7 @@ interface FileActionData {
iconSvgInline: (files: Node[], view: View) => string
/** Condition wether this action is shown or not */
enabled?: (files: Node[], view: View) => boolean

/**
* Function executed on single file action
* @return true if the action was executed successfully,
Expand All @@ -54,9 +55,16 @@ interface FileActionData {
* @throws Error if the action failed
*/
execBatch?: (files: Node[], view: View, dir: string) => Promise<(boolean|null)[]>

/** This action order in the list */
order?: number,

/**
* This action's parent id in the list.
* If none found, will be displayed as a top-level action.
*/
parent?: string,

/**
* Make this action the default.
* If multiple actions are default, the first one
Expand Down Expand Up @@ -119,6 +127,10 @@ export class FileAction {
return this._action.order
}

get parent() {
return this._action.parent
}

get default() {
return this._action.default
}
Expand Down Expand Up @@ -165,6 +177,10 @@ export class FileAction {
throw new Error('Invalid order')
}

if ('parent' in action && typeof action.parent !== 'string') {
throw new Error('Invalid parent')
}

if (action.default && !Object.values(DefaultType).includes(action.default)) {
throw new Error('Invalid default')
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -31,7 +31,7 @@
"build": "vite --mode production build",
"build:doc": "typedoc --out dist/doc lib && touch dist/doc/.nojekyll",
"dev": "vite --mode development build",
"dev:watch": "vite --mode development build --watch",
"watch": "vite --mode development build --watch",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "vitest run",
Expand Down

0 comments on commit 6aa33d3

Please sign in to comment.