Skip to content

Commit 9361d39

Browse files
authored
Merge pull request #1124 from nextcloud-libraries/feat/standardize-context
2 parents fd3a895 + d9c2e53 commit 9361d39

5 files changed

Lines changed: 64 additions & 39 deletions

File tree

__tests__/actions/fileAction.spec.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import type { Node } from '../../lib/node/index.ts'
6+
import type { Folder, Node } from '../../lib/node/index.ts'
77
import type { View } from '../../lib/navigation/view.ts'
88

99
import { beforeEach, describe, expect, test, vi } from 'vitest'
1010
import { getFileActions, registerFileAction, FileAction, DefaultType, FileActionData } from '../../lib/actions/index.ts'
1111
import logger from '../../lib/utils/logger.ts'
1212

13+
const folder = {} as Folder
14+
const view = {} as View
1315
describe('FileActions init', () => {
1416

1517
beforeEach(() => {
@@ -34,8 +36,8 @@ describe('FileActions init', () => {
3436
})
3537

3638
expect(action.id).toBe('test')
37-
expect(action.displayName([], {} as unknown as View)).toBe('Test')
38-
expect(action.iconSvgInline([], {} as unknown as View)).toBe('<svg></svg>')
39+
expect(action.displayName({ view, folder, nodes: [], content: [] })).toBe('Test')
40+
expect(action.iconSvgInline({ view, folder, nodes: [], content: [] })).toBe('<svg></svg>')
3941

4042
registerFileAction(action)
4143

@@ -244,18 +246,20 @@ describe('FileActions creation', () => {
244246
},
245247
})
246248

249+
const node = {} as Node
250+
247251
expect(action.id).toBe('test')
248-
expect(action.displayName([], {} as unknown as View)).toBe('Test')
249-
expect(action.title?.([], {} as unknown as View)).toBe('Test title')
250-
expect(action.iconSvgInline([], {} as unknown as View)).toBe('<svg></svg>')
251-
await expect(action.exec({} as unknown as Node, {} as unknown as View, '/')).resolves.toBe(true)
252-
await expect(action.execBatch?.([], {} as unknown as View, '/')).resolves.toStrictEqual([true])
253-
expect(action.enabled?.([], {} as unknown as View)).toBe(true)
252+
expect(action.displayName({ view, folder, nodes: [], content: [] })).toBe('Test')
253+
expect(action.title?.({ view, folder, nodes: [], content: [] })).toBe('Test title')
254+
expect(action.iconSvgInline({ view, folder, nodes: [], content: [] })).toBe('<svg></svg>')
255+
await expect(action.exec({ view, folder, nodes: [node], content: [] })).resolves.toBe(true)
256+
await expect(action.execBatch?.({ view, folder, nodes: [], content: [] })).resolves.toStrictEqual([true])
257+
expect(action.enabled?.({ view, folder, nodes: [], content: [] })).toBe(true)
254258
expect(action.order).toBe(100)
255259
expect(action.parent).toBe('123')
256260
expect(action.destructive).toBe(true)
257261
expect(action.default).toBe(DefaultType.DEFAULT)
258-
expect(action.inline?.({} as unknown as Node, {} as unknown as View)).toBe(true)
259-
expect((await action.renderInline?.({} as unknown as Node, {} as unknown as View))?.outerHTML).toBe('<span>test</span>')
262+
expect(action.inline?.({ view, folder, nodes: [], content: [] })).toBe(true)
263+
expect((await action.renderInline?.({ view, folder, nodes: [], content: [] }))?.outerHTML).toBe('<span>test</span>')
260264
})
261265
})

__tests__/actions/fileListAction.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { getFileListActions, registerFileListAction, FileListAction } from '../.
1010
import { Folder } from '../../lib/node/index.ts'
1111
import logger from '../../lib/utils/logger.ts'
1212

13+
const folder = {} as Folder
14+
const view = {} as View
15+
1316
const mockAction = (id: string) => new FileListAction({
1417
id,
1518
displayName: () => 'Test',
@@ -37,8 +40,8 @@ describe('FileListActions init', () => {
3740
const testAction = mockAction('test')
3841

3942
expect(testAction.id).toBe('test')
40-
expect(testAction.displayName({} as unknown as View)).toBe('Test')
41-
expect(testAction.iconSvgInline!({} as unknown as View)).toBe('<svg></svg>')
43+
expect(testAction.displayName({ view, folder })).toBe('Test')
44+
expect(testAction.iconSvgInline!({ view, folder })).toBe('<svg></svg>')
4245

4346
registerFileListAction(testAction)
4447
expect(actions).toHaveLength(1)
@@ -155,10 +158,10 @@ describe('FileListAction creation', () => {
155158
})
156159

157160
expect(testAction.id).toBe('test')
158-
expect(testAction.displayName({} as unknown as View)).toBe('Test')
159-
expect(testAction.iconSvgInline!({} as unknown as View)).toBe('<svg></svg>')
161+
expect(testAction.displayName({ view, folder })).toBe('Test')
162+
expect(testAction.iconSvgInline!({ view, folder })).toBe('<svg></svg>')
160163
expect(testAction.order).toBe(0)
161-
expect(testAction.enabled?.({} as unknown as View, [], {} as Folder)).toBe(true)
162-
await expect(testAction.exec({} as unknown as View, [], {} as Folder)).resolves.toBe(undefined)
164+
expect(testAction.enabled?.({ view, folder })).toBe(true)
165+
await expect(testAction.exec({ view, folder, nodes: [], content: [] })).resolves.toBe(undefined)
163166
})
164167
})

lib/actions/fileAction.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5-
6-
import type { Node } from '../node/node.ts'
7-
import type { View } from '../navigation/view.ts'
5+
import type { ActionContext, ActionContextSingle } from '../types'
86

97
import logger from '../utils/logger.ts'
108

@@ -48,28 +46,28 @@ export interface FileActionData {
4846
/** Unique ID */
4947
id: string
5048
/** Translatable string displayed in the menu */
51-
displayName: (files: Node[], view: View) => string
49+
displayName: (context: ActionContext) => string
5250
/** Translatable title for of the action */
53-
title?: (files: Node[], view: View) => string
51+
title?: (context: ActionContext) => string
5452
/** Svg as inline string. <svg><path fill="..." /></svg> */
55-
iconSvgInline: (files: Node[], view: View) => string
53+
iconSvgInline: (context: ActionContext) => string
5654
/** Condition wether this action is shown or not */
57-
enabled?: (files: Node[], view: View) => boolean
55+
enabled?: (context: ActionContext) => boolean
5856

5957
/**
6058
* Function executed on single file action
6159
* @return true if the action was executed successfully,
6260
* false otherwise and null if the action is silent/undefined.
6361
* @throws Error if the action failed
6462
*/
65-
exec: (file: Node, view: View, dir: string) => Promise<boolean|null>,
63+
exec: (context: ActionContextSingle) => Promise<boolean|null>,
6664
/**
6765
* Function executed on multiple files action
6866
* @return true if the action was executed successfully,
6967
* false otherwise and null if the action is silent/undefined.
7068
* @throws Error if the action failed
7169
*/
72-
execBatch?: (files: Node[], view: View, dir: string) => Promise<(boolean|null)[]>
70+
execBatch?: (context: ActionContext) => Promise<(boolean|null)[]>
7371

7472
/** This action order in the list */
7573
order?: number
@@ -104,12 +102,12 @@ export interface FileActionData {
104102
/**
105103
* If true, the renderInline function will be called
106104
*/
107-
inline?: (file: Node, view: View) => boolean,
105+
inline?: (context: ActionContext) => boolean,
108106
/**
109107
* If defined, the returned html element will be
110108
* appended before the actions menu.
111109
*/
112-
renderInline?: (file: Node, view: View) => Promise<HTMLElement | null>,
110+
renderInline?: (context: ActionContext) => Promise<HTMLElement | null>,
113111
}
114112

115113
export class FileAction {

lib/actions/fileListAction.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5-
6-
import type { Folder } from '../node/folder.ts'
7-
import type { Node } from '../node/node.ts'
8-
import type { View } from '../navigation/view.ts'
5+
import type { ActionContext, ViewActionContext } from '../types'
96

107
import logger from '../utils/logger.ts'
118

@@ -14,29 +11,26 @@ interface FileListActionData {
1411
id: string
1512

1613
/** Translated name of the action */
17-
displayName: (view: View) => string
14+
displayName: (context: ViewActionContext) => string
1815

1916
/** Raw svg string */
20-
iconSvgInline?: (view: View) => string
17+
iconSvgInline?: (context: ViewActionContext) => string
2118

2219
/** Sort order */
2320
order: number
2421

2522
/**
2623
* Condition whether this action is shown or not
27-
* @param view The current view
28-
* @param nodes The nodes in the current directory
29-
* @param folder The current folder
3024
*/
31-
enabled?: (view: View, nodes: Node[], folder: Folder) => boolean
25+
enabled?: (context: ViewActionContext) => boolean
3226

3327
/**
3428
* Function executed on single file action
3529
* @return true if the action was executed successfully,
3630
* false otherwise and null if the action is silent/undefined.
3731
* @throws Error if the action failed
3832
*/
39-
exec: (view: View, nodes: Node[], folder: Folder) => Promise<boolean|null>,
33+
exec: (context: ActionContext) => Promise<boolean|null>,
4034
}
4135

4236
export class FileListAction {

lib/types.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { Folder, Node } from './node/index.ts'
7+
import { View } from './navigation/index.ts'
8+
9+
type ActionContextSingle = {
10+
nodes: [Node],
11+
view: View,
12+
folder: Folder,
13+
content: Node[],
14+
}
15+
16+
type ActionContext = {
17+
nodes: Node[],
18+
view: View,
19+
folder: Folder,
20+
content: Node[],
21+
}
22+
23+
type ViewActionContext = {
24+
view: View,
25+
folder: Folder,
26+
}

0 commit comments

Comments
 (0)