Skip to content

Commit

Permalink
feat(new-menu): Allow to set the category for entries
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed May 16, 2024
1 parent 4b8160f commit 005df94
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
35 changes: 34 additions & 1 deletion __tests__/newFileMenu.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test, vi, afterEach } from 'vitest'

import { NewFileMenu, getNewFileMenu, type Entry } from '../lib/newFileMenu'
import { NewFileMenu, NewMenuEntryCategory, getNewFileMenu, type Entry } from '../lib/newFileMenu'
import logger from '../lib/utils/logger'
import { Folder, Permission, addNewFileMenuEntry, getNewFileMenuEntries } from '../lib'

Expand Down Expand Up @@ -196,6 +196,39 @@ describe('NewFileMenu addEntry', () => {
} as unknown as Entry)
}).toThrowError('Invalid handler property')
})

test('Adding a Entry without category', () => {
const newFileMenu = new NewFileMenu()
const entry = {
id: 'empty-file',
displayName: 'Create empty file',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
handler: () => {},
}
newFileMenu.registerEntry(entry)

const entries = newFileMenu.getEntries()
expect(entries).toHaveLength(1)
expect(entries[0].category).toBe(NewMenuEntryCategory.CreateNew)
})

test('Adding a Entry with category', () => {
const newFileMenu = new NewFileMenu()
const entry = {
id: 'empty-file',
category: NewMenuEntryCategory.Other,
displayName: 'Create empty file',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
handler: () => {},
}
newFileMenu.registerEntry(entry)

const entries = newFileMenu.getEntries()
expect(entries).toHaveLength(1)
expect(entries[0].category).toBe(NewMenuEntryCategory.Other)
})
})

describe('NewFileMenu removeEntry', () => {
Expand Down
24 changes: 24 additions & 0 deletions lib/newFileMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,33 @@
import type { Folder, Node } from './index'
import logger from './utils/logger'

export enum NewMenuEntryCategory {
/**
* For actions where the user is intended to upload from their device
*/
UploadFromDevice = 0,
/**
* For actions that create new nodes on the server without uploading
*/
CreateNew = 1,
/**
* For everything not matching the other categories
*/
Other = 2,
}

export interface Entry {
/** Unique ID */
id: string

/**
* Category to put this entry in
* (supported since Nextcloud 30)
* @since 3.3.0
* @default NewMenuEntryCategory.CreateNew
*/
category?: NewMenuEntryCategory

/** Translatable string displayed in the menu */
displayName: string

Expand Down Expand Up @@ -65,6 +88,7 @@ export class NewFileMenu {

public registerEntry(entry: Entry) {
this.validateEntry(entry)
entry.category = entry.category ?? NewMenuEntryCategory.CreateNew
this._entries.push(entry)
}

Expand Down

0 comments on commit 005df94

Please sign in to comment.