Skip to content

Commit

Permalink
fix: update fileAction from server
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Aug 18, 2023
1 parent befd766 commit 4854b0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
22 changes: 11 additions & 11 deletions __tests__/fileAction.spec.ts
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable no-new */
import { beforeEach, describe, expect, test, vi } from 'vitest'

import { getFileActions, registerFileAction, FileAction } from '../lib/fileAction'
import { getFileActions, registerFileAction, FileAction, DefaultType } from '../lib/fileAction'
import logger from '../lib/utils/logger'

describe('FileActions init', () => {
Expand Down Expand Up @@ -208,24 +208,24 @@ describe('FileActions creation', () => {
execBatch: async () => [true],
enabled: () => true,
order: 100,
default: true,
default: DefaultType.DEFAULT,
inline: () => true,
renderInline() {
renderInline: async() => {

Check failure on line 213 in __tests__/fileAction.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Missing space before function parentheses
const span = document.createElement('span')
span.textContent = 'test'
return span
},
})

expect(action.id).toBe('test')
expect(action.displayName([], {})).toBe('Test')
expect(action.iconSvgInline([], {})).toBe('<svg></svg>')
await expect(action.exec({} as any, {}, '/')).resolves.toBe(true)
await expect(action.execBatch?.([], {}, '/')).resolves.toStrictEqual([true])
expect(action.enabled?.({} as any, {})).toBe(true)
expect(action.displayName([], {} as any)).toBe('Test')
expect(action.iconSvgInline([], {} as any)).toBe('<svg></svg>')
await expect(action.exec({} as any, {} as any, '/')).resolves.toBe(true)
await expect(action.execBatch?.([], {} as any, '/')).resolves.toStrictEqual([true])
expect(action.enabled?.({} as any, {} as any)).toBe(true)
expect(action.order).toBe(100)
expect(action.default).toBe(true)
expect(action.inline?.({} as any, {})).toBe(true)
expect(action.renderInline?.({} as any, {}).outerHTML).toBe('<span>test</span>')
expect(action.default).toBe(DefaultType.DEFAULT)
expect(action.inline?.({} as any, {} as any)).toBe(true)
expect((await action.renderInline?.({} as any, {} as any))?.outerHTML).toBe('<span>test</span>')
})
})
20 changes: 16 additions & 4 deletions lib/fileAction.ts
@@ -1,5 +1,5 @@
/**
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
Expand All @@ -24,6 +24,18 @@ import { Node } from './files/node'
import { View } from './navigation/view'
import logger from './utils/logger'

declare global {
interface Window {
OC: any;

Check warning on line 29 in lib/fileAction.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
_nc_fileactions: FileAction[] | undefined;
}
}

export enum DefaultType {
DEFAULT = 'default',
HIDDEN = 'hidden',
}

interface FileActionData {
/** Unique ID */
id: string
Expand All @@ -50,7 +62,7 @@ interface FileActionData {
/** This action order in the list */
order?: number,
/** Make this action the default */
default?: boolean,
default?: DefaultType,
/**
* If true, the renderInline function will be called
*/
Expand All @@ -59,7 +71,7 @@ interface FileActionData {
* If defined, the returned html element will be
* appended before the actions menu.
*/
renderInline?: (file: Node, view: View) => HTMLElement,
renderInline?: (file: Node, view: View) => Promise<HTMLElement | null>,
}

export class FileAction {
Expand Down Expand Up @@ -141,7 +153,7 @@ export class FileAction {
throw new Error('Invalid order')
}

if ('default' in action && typeof action.default !== 'boolean') {
if (action.default && !Object.values(DefaultType).includes(action.default)) {
throw new Error('Invalid default')
}

Expand Down

0 comments on commit 4854b0c

Please sign in to comment.