Skip to content

Commit

Permalink
Add context to getEntries
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Sep 8, 2022
1 parent a728c13 commit 8494d88
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 23 deletions.
148 changes: 136 additions & 12 deletions __tests__/newFileMenu.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NewFileMenu, getNewFileMenu, FileType, type Entry } from '../lib/newFileMenu'
import { NewFileMenu, getNewFileMenu, type Entry } from '../lib/newFileMenu'
import logger from '../lib/utils/logger';

declare global {
Expand Down Expand Up @@ -34,7 +34,6 @@ describe('NewFileMenu addEntry', () => {
displayName: 'Create empty file',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'file' as FileType,
handler: () => {}
}
newFileMenu.registerEntry(entry)
Expand All @@ -51,7 +50,6 @@ describe('NewFileMenu addEntry', () => {
displayName: 'Create empty file',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'file' as FileType,
handler: () => {}
}
newFileMenu.registerEntry(entry1)
Expand All @@ -64,7 +62,6 @@ describe('NewFileMenu addEntry', () => {
displayName: 'Create new image',
templateName: 'New drawing.png',
iconClass: 'icon-filetype-image',
fileType: 'file' as FileType,
handler: () => {}
}
newFileMenu.registerEntry(entry2)
Expand All @@ -77,7 +74,6 @@ describe('NewFileMenu addEntry', () => {
displayName: 'New folder',
templateName: 'New folder',
iconClass: 'icon-folder',
fileType: 'folder' as FileType,
handler: () => {}
}
newFileMenu.registerEntry(entry3)
Expand All @@ -93,7 +89,6 @@ describe('NewFileMenu addEntry', () => {
displayName: 'Create empty file',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'file' as FileType,
handler: () => {}
}
newFileMenu.registerEntry(entry)
Expand Down Expand Up @@ -122,7 +117,6 @@ describe('NewFileMenu addEntry', () => {
displayName: 123456,
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'file' as FileType,
handler: () => {}
} as unknown as Entry)
}).toThrowError('Invalid entry')
Expand All @@ -133,18 +127,17 @@ describe('NewFileMenu addEntry', () => {
displayName: '123456',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'image' as FileType,
if: false,
handler: () => {}
} as unknown as Entry)
}).toThrowError('Invalid entry fileType')
}).toThrowError('Invalid entry, if must be a valid function')

expect(() => {
newFileMenu.registerEntry({
id: 'empty-file',
displayName: '123456',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'folder' as FileType,
handler: 'handler'
} as unknown as Entry)
}).toThrowError('Invalid entry handler')
Expand All @@ -159,7 +152,6 @@ describe('NewFileMenu removeEntry', () => {
displayName: 'Create empty file',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'file' as FileType,
handler: () => {}
}
newFileMenu.registerEntry(entry)
Expand All @@ -179,7 +171,6 @@ describe('NewFileMenu removeEntry', () => {
displayName: 'Create empty file',
templateName: 'New file.txt',
iconClass: 'icon-filetype-text',
fileType: 'file' as FileType,
handler: () => {}
}
newFileMenu.registerEntry(entry)
Expand All @@ -200,4 +191,137 @@ describe('NewFileMenu removeEntry', () => {
expect(newFileMenu.getEntries()).toHaveLength(0)
expect(logger.warn).toHaveBeenCalled()
})
})

describe('NewFileMenu getEntries filter', () => {
test('Filter no entries', () => {
const newFileMenu = new NewFileMenu()
const entry1 = {
id: 'empty-file',
displayName: 'Create empty file',
templateName: 'New file',
iconClass: 'icon-file',
if: fileInfo => fileInfo.permissions.includes('CK'),
handler: () => {}
}
newFileMenu.registerEntry(entry1)

const entry2 = {
id: 'empty-text-md',
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
if: fileInfo => fileInfo.permissions.includes('CK'),
handler: () => {}
}
newFileMenu.registerEntry(entry2)

const context = {
basename: 'Folder',
etag: '63071eedd82fe',
fileid: '56',
filename: '/Folder',
hasPreview: false,
lastmod: 1661410576,
mime: 'httpd/unix-directory',
month: '197001',
permissions: 'CKGWDR',
showShared: false,
size: 2610077102,
timestamp: 1661410,
type: 'dir',
}

const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(2)
expect(entries[0]).toBe(entry1)
expect(entries[1]).toBe(entry2)
})

test('Filter all entries', () => {
const newFileMenu = new NewFileMenu()
const entry1 = {
id: 'empty-file',
displayName: 'Create empty file',
templateName: 'New file',
iconClass: 'icon-file',
if: fileInfo => fileInfo.permissions.includes('CK'),
handler: () => {}
}
newFileMenu.registerEntry(entry1)

const entry2 = {
id: 'empty-text-md',
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
if: fileInfo => fileInfo.permissions.includes('CK'),
handler: () => {}
}
newFileMenu.registerEntry(entry2)

const context = {
basename: 'Shared folder',
etag: '63071eedd82fe',
fileid: '56',
filename: '/Shared folder',
hasPreview: false,
lastmod: 1661410576,
mime: 'httpd/unix-directory',
month: '197001',
// Only read and share
permissions: 'GR',
showShared: false,
size: 2610077102,
timestamp: 1661410,
type: 'dir',
}

const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(0)
})

test('Filter some entries', () => {
const newFileMenu = new NewFileMenu()
const entry1 = {
id: 'empty-file',
displayName: 'Create template',
templateName: 'New file',
iconClass: 'icon-file',
// No conditions
handler: () => {}
}
newFileMenu.registerEntry(entry1)

const entry2 = {
id: 'empty-text-md',
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
if: fileInfo => fileInfo.permissions.includes('CK'),
handler: () => {}
}
newFileMenu.registerEntry(entry2)

const context = {
basename: 'Shared folder',
etag: '63071eedd82fe',
fileid: '56',
filename: '/Shared folder',
hasPreview: false,
lastmod: 1661410576,
mime: 'httpd/unix-directory',
month: '197001',
// Only read and share
permissions: 'GR',
showShared: false,
size: 2610077102,
timestamp: 1661410,
type: 'dir',
}

const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(1)
expect(entries[0]).toBe(entry1)
})
})
26 changes: 15 additions & 11 deletions lib/newFileMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@

import logger from "./utils/logger"

export enum FileType {
FILE = 'file',
FOLDER = 'folder',
}

export interface Entry {
/** Unique ID */
id: string
/** Translatable string displayed in the menu */
displayName: string
// Default new file name
templateName: string
// Condition wether this entry is shown or not
if?: (context: Object) => Boolean
/**
* Either iconSvgInline or iconClass must be defined
* Svg as inline string. <svg><path fill="..." /></svg>
*/
iconSvgInline?: string
/** Existing icon css class */
iconClass?: string
fileType: FileType
/** Function to be run after creation */
handler?: Function
}
Expand All @@ -67,7 +63,16 @@ export class NewFileMenu {
this._entries.splice(entryIndex, 1)
}

public getEntries(): Array<Entry> {
/**
* Get the list of registered entries
*
* @param {FileInfo} context the creation context. Usually the current folder FileInfo
*/
public getEntries(context?: Object): Array<Entry> {
if (context) {
return this._entries
.filter(entry => typeof entry.if === 'function' ? entry.if(context) : true)
}
return this._entries
}

Expand All @@ -76,7 +81,7 @@ export class NewFileMenu {
}

private validateEntry(entry: Entry) {
if (!entry.id || !entry.displayName || !entry.templateName || !(entry.iconSvgInline || entry.iconClass) || !entry.fileType || !entry.handler) {
if (!entry.id || !entry.displayName || !entry.templateName || !(entry.iconSvgInline || entry.iconClass) ||!entry.handler) {
throw new Error('Invalid entry')
}

Expand All @@ -88,9 +93,8 @@ export class NewFileMenu {
throw new Error('Invalid entry')
}

if (typeof entry.fileType !== 'string'
|| !(entry.fileType === FileType.FILE || entry.fileType === FileType.FOLDER)) {
throw new Error('Invalid entry fileType')
if (entry.if !== undefined && typeof entry.if !== 'function') {
throw new Error('Invalid entry, if must be a valid function')
}

if (typeof entry.handler !== 'function') {
Expand Down

0 comments on commit 8494d88

Please sign in to comment.