Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add action title #767

Merged
merged 1 commit into from Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions __tests__/fileAction.spec.ts
Expand Up @@ -109,6 +109,17 @@ describe('Invalid FileAction creation', () => {
} as any as FileAction)
}).toThrowError('Invalid displayName function')
})
test('Invalid title', () => {
expect(() => {
new FileAction({
id: 'test',
displayName: () => 'Test',
title: 'Test',
iconSvgInline: () => '<svg></svg>',
exec: async () => true,
} as any as FileAction)
}).toThrowError('Invalid title function')
})
test('Invalid iconSvgInline', () => {
expect(() => {
new FileAction({
Expand Down Expand Up @@ -203,6 +214,7 @@ describe('FileActions creation', () => {
const action = new FileAction({
id: 'test',
displayName: () => 'Test',
title: () => 'Test title',
iconSvgInline: () => '<svg></svg>',
exec: async () => true,
execBatch: async () => [true],
Expand All @@ -219,6 +231,7 @@ describe('FileActions creation', () => {

expect(action.id).toBe('test')
expect(action.displayName([], {} as any)).toBe('Test')
expect(action.title?.([], {} as any)).toBe('Test title')
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])
Expand Down
10 changes: 10 additions & 0 deletions lib/fileAction.ts
Expand Up @@ -34,6 +34,8 @@ interface FileActionData {
id: string
/** Translatable string displayed in the menu */
displayName: (files: Node[], view: View) => string
/** Translatable title for of the action */
title?: (files: Node[], view: View) => string
/** Svg as inline string. <svg><path fill="..." /></svg> */
iconSvgInline: (files: Node[], view: View) => string
/** Condition wether this action is shown or not */
Expand Down Expand Up @@ -93,6 +95,10 @@ export class FileAction {
return this._action.displayName
}

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

get iconSvgInline() {
return this._action.iconSvgInline
}
Expand Down Expand Up @@ -134,6 +140,10 @@ export class FileAction {
throw new Error('Invalid displayName function')
}

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

if (!action.iconSvgInline || typeof action.iconSvgInline !== 'function') {
throw new Error('Invalid iconSvgInline function')
}
Expand Down