Skip to content

Commit

Permalink
feat(actions): standardize contexts
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <[email protected]>
  • Loading branch information
skjnldsv committed Nov 20, 2024
1 parent 333c1fc commit d994eb4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 50 deletions.
28 changes: 16 additions & 12 deletions __tests__/fileAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
* SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Node } from '../lib/files/node'
import type { View } from '../lib/navigation/view'
import type { Node, Folder, View } from '../lib/index.ts'

import { beforeEach, describe, expect, test, vi } from 'vitest'
import { getFileActions, registerFileAction, FileAction, DefaultType, FileActionData } from '../lib/fileAction'
import logger from '../lib/utils/logger'

const context = {} as Folder
const view = {} as View

describe('FileActions init', () => {

beforeEach(() => {
Expand All @@ -33,8 +35,8 @@ describe('FileActions init', () => {
})

expect(action.id).toBe('test')
expect(action.displayName([], {} as unknown as View)).toBe('Test')
expect(action.iconSvgInline([], {} as unknown as View)).toBe('<svg></svg>')
expect(action.displayName({ view, context, nodes: [] })).toBe('Test')
expect(action.iconSvgInline({ view, context, nodes: [] })).toBe('<svg></svg>')

registerFileAction(action)

Expand Down Expand Up @@ -243,18 +245,20 @@ describe('FileActions creation', () => {
},
})

const node = {} as Node

expect(action.id).toBe('test')
expect(action.displayName([], {} as unknown as View)).toBe('Test')
expect(action.title?.([], {} as unknown as View)).toBe('Test title')
expect(action.iconSvgInline([], {} as unknown as View)).toBe('<svg></svg>')
await expect(action.exec({} as unknown as Node, {} as unknown as View, '/')).resolves.toBe(true)
await expect(action.execBatch?.([], {} as unknown as View, '/')).resolves.toStrictEqual([true])
expect(action.enabled?.([], {} as unknown as View)).toBe(true)
expect(action.displayName({ view, context, nodes: [] })).toBe('Test')
expect(action.title?.({ view, context, nodes: [] })).toBe('Test title')
expect(action.iconSvgInline({ view, context, nodes: [] })).toBe('<svg></svg>')
await expect(action.exec({ view, context, nodes: [node] })).resolves.toBe(true)
await expect(action.execBatch?.({ view, context, nodes: [] })).resolves.toStrictEqual([true])
expect(action.enabled?.({ view, context, nodes: [] })).toBe(true)
expect(action.order).toBe(100)
expect(action.parent).toBe('123')
expect(action.destructive).toBe(true)
expect(action.default).toBe(DefaultType.DEFAULT)
expect(action.inline?.({} as unknown as Node, {} as unknown as View)).toBe(true)
expect((await action.renderInline?.({} as unknown as Node, {} as unknown as View))?.outerHTML).toBe('<span>test</span>')
expect(action.inline?.({ view, context, nodes: [] })).toBe(true)
expect((await action.renderInline?.({ view, context, nodes: [] }))?.outerHTML).toBe('<span>test</span>')
})
})
17 changes: 10 additions & 7 deletions __tests__/fileListAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

import { beforeEach, describe, expect, test, vi } from 'vitest'

import type { View } from '../lib/navigation/view.ts'
import type { View } from '../lib/index.ts'

import { getFileListActions, registerFileListAction, FileListAction } from '../lib/fileListAction.ts'
import { Folder } from '../lib/files/folder.ts'
import logger from '../lib/utils/logger.ts'

const context = {} as Folder
const view = {} as View

const mockAction = (id: string) => new FileListAction({
id,
displayName: () => 'Test',
Expand All @@ -37,8 +40,8 @@ describe('FileListActions init', () => {
const testAction = mockAction('test')

expect(testAction.id).toBe('test')
expect(testAction.displayName({} as unknown as View)).toBe('Test')
expect(testAction.iconSvgInline({} as unknown as View)).toBe('<svg></svg>')
expect(testAction.displayName({ view, context })).toBe('Test')
expect(testAction.iconSvgInline({ view, context })).toBe('<svg></svg>')

registerFileListAction(testAction)
expect(actions).toHaveLength(1)
Expand Down Expand Up @@ -153,10 +156,10 @@ describe('FileListAction creation', () => {
})

expect(testAction.id).toBe('test')
expect(testAction.displayName({} as unknown as View)).toBe('Test')
expect(testAction.iconSvgInline({} as unknown as View)).toBe('<svg></svg>')
expect(testAction.displayName({ view, context })).toBe('Test')
expect(testAction.iconSvgInline({ view, context })).toBe('<svg></svg>')
expect(testAction.order).toBe(0)
expect(testAction.enabled?.({} as unknown as View, [], { folder: {} as Folder })).toBe(true)
await expect(testAction.exec({} as unknown as View, [], { folder: {} as Folder })).resolves.toBe(undefined)
expect(testAction.enabled?.({ view, context })).toBe(true)
await expect(testAction.exec({ view, context, nodes: [] })).resolves.toBe(undefined)
})
})
19 changes: 9 additions & 10 deletions lib/fileAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Node } from './files/node'
import { View } from './navigation/view'
import type { ActionContext, ActionContextSingle } from './types'
import logger from './utils/logger'

export enum DefaultType {
Expand All @@ -16,28 +15,28 @@ export interface FileActionData {
/** Unique ID */
id: string
/** Translatable string displayed in the menu */
displayName: (files: Node[], view: View) => string
displayName: (context: ActionContext) => string
/** Translatable title for of the action */
title?: (files: Node[], view: View) => string
title?: (context: ActionContext) => string
/** Svg as inline string. <svg><path fill="..." /></svg> */
iconSvgInline: (files: Node[], view: View) => string
iconSvgInline: (context: ActionContext) => string
/** Condition wether this action is shown or not */
enabled?: (files: Node[], view: View) => boolean
enabled?: (context: ActionContext) => boolean

/**
* Function executed on single file action
* @return true if the action was executed successfully,
* false otherwise and null if the action is silent/undefined.
* @throws Error if the action failed
*/
exec: (file: Node, view: View, dir: string) => Promise<boolean|null>,
exec: (context: ActionContextSingle) => Promise<boolean|null>,
/**
* Function executed on multiple files action
* @return true if the action was executed successfully,
* false otherwise and null if the action is silent/undefined.
* @throws Error if the action failed
*/
execBatch?: (files: Node[], view: View, dir: string) => Promise<(boolean|null)[]>
execBatch?: (context: ActionContext) => Promise<(boolean|null)[]>

/** This action order in the list */
order?: number,
Expand Down Expand Up @@ -67,12 +66,12 @@ export interface FileActionData {
/**
* If true, the renderInline function will be called
*/
inline?: (file: Node, view: View) => boolean,
inline?: (context: ActionContext) => boolean,
/**
* If defined, the returned html element will be
* appended before the actions menu.
*/
renderInline?: (file: Node, view: View) => Promise<HTMLElement | null>,
renderInline?: (context: ActionContext) => Promise<HTMLElement | null>,
}

export class FileAction {
Expand Down
29 changes: 8 additions & 21 deletions lib/fileListAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,32 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Node } from './files/node.ts'
import { Folder } from './files/folder.ts'
import { View } from './navigation/view.ts'
import logger from './utils/logger.ts'
import type { ActionContext, ViewActionContext } from './types'

interface ActionContext {
folder: Folder,
}
import logger from './utils/logger.ts'

interface FileListActionData {
/** Unique ID */
id: string

/** Translated name of the action */
displayName: (view: View) => string
displayName: (context: ViewActionContext) => string

/** Raw svg string */
iconSvgInline: (view: View) => string
iconSvgInline: (context: ViewActionContext) => string

/** Sort order */
order: number

/**
* Returns true if this action shoud be shown
*
* @param nodes The nodes in the current directory
* @param context The context
* @param context.folder The current folder
* Returns true if this action should be shown
*/
enabled?: (view: View, nodes: Node[], context: ActionContext) => boolean
enabled?: (context: ViewActionContext) => boolean

/**
* Function to execute
*
* @param nodes The nodes in the current directory
* @param context The context
* @param context.folder The current folder
* Function to execute the action
*/
exec: (view: View, nodes: Node[], context: ActionContext) => Promise<void>
exec: (context: ActionContext) => Promise<void>
}

export class FileListAction {
Expand Down
25 changes: 25 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Folder } from './files/folder'
import { Node } from './files/node'
import { View } from './navigation'

type ActionContextSingle = {
nodes: [Node],
view: View,
context: Folder,
}

type ActionContext = {
nodes: Node[],
view: View,
context: Folder,
}

type ViewActionContext = {
view: View,
context: Folder,
}

0 comments on commit d994eb4

Please sign in to comment.