Skip to content

Commit

Permalink
Merge pull request #731 from nextcloud-libraries/feat/fileMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv committed Aug 17, 2023
2 parents 97ff779 + 61928f4 commit 848391c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 57 deletions.
75 changes: 25 additions & 50 deletions __tests__/newFileMenu.spec.ts
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, test, vi } from 'vitest'

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

describe('NewFileMenu init', () => {
test('Initializing NewFileMenu', () => {
Expand Down Expand Up @@ -244,7 +245,7 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create empty file',
templateName: 'New file',
iconClass: 'icon-file',
if: fileInfo => fileInfo.permissions.includes('CK'),
if: folder => (folder.permissions & Permission.CREATE) !== 0,
handler: () => {},
}
newFileMenu.registerEntry(entry1)
Expand All @@ -254,26 +255,18 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
if: fileInfo => fileInfo.permissions.includes('CK'),
if: folder => (folder.permissions & Permission.CREATE) !== 0,
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,
const context = new Folder({
id: 56,
owner: 'admin',
size: 2610077102,
timestamp: 1661410,
type: 'dir',
}
source: 'https://example.com/remote.php/dav/files/admin/Folder',
permissions: Permission.ALL,
})

const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(2)
Expand All @@ -288,7 +281,7 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create empty file',
templateName: 'New file',
iconClass: 'icon-file',
if: fileInfo => fileInfo.permissions.includes('CK'),
if: folder => (folder.permissions & Permission.CREATE) !== 0,
handler: () => {},
}
newFileMenu.registerEntry(entry1)
Expand All @@ -298,27 +291,18 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
if: fileInfo => fileInfo.permissions.includes('CK'),
if: folder => (folder.permissions & Permission.CREATE) !== 0,
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,
const context = new Folder({
id: 56,
owner: 'admin',
size: 2610077102,
timestamp: 1661410,
type: 'dir',
}
source: 'https://example.com/remote.php/dav/files/admin/Folder',
permissions: Permission.READ,
})

const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(0)
Expand All @@ -331,7 +315,6 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create template',
templateName: 'New file',
iconClass: 'icon-file',
// No conditions
handler: () => {},
}
newFileMenu.registerEntry(entry1)
Expand All @@ -341,27 +324,19 @@ describe('NewFileMenu getEntries filter', () => {
displayName: 'Create new markdown file',
templateName: 'New text.md',
iconClass: 'icon-filetype-text',
if: fileInfo => fileInfo.permissions.includes('CK'),
if: folder => (folder.permissions & Permission.CREATE) !== 0,
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,
const context = new Folder({
id: 56,
owner: 'admin',
size: 2610077102,
timestamp: 1661410,
type: 'dir',
}
source: 'https://example.com/remote.php/dav/files/admin/Foo/Bar',
permissions: Permission.NONE,
root: '/files/admin',
})

const entries = newFileMenu.getEntries(context)
expect(entries).toHaveLength(1)
Expand Down
5 changes: 3 additions & 2 deletions lib/index.ts
Expand Up @@ -22,6 +22,7 @@
*/

import { type Entry, getNewFileMenu } from './newFileMenu'
import { Folder } from './files/folder'

export { formatFileSize } from './humanfilesize'
export { FileAction, getFileActions, registerFileAction } from './fileAction'
Expand Down Expand Up @@ -63,9 +64,9 @@ export const removeNewFileMenuEntry = function(entry: Entry | string) {
/**
* Get the list of registered entries from the upload menu
*
* @param {FileInfo} context the creation context. Usually the current folder FileInfo
* @param {Folder} context the creation context. Usually the current folder FileInfo
*/
export const getNewFileMenuEntries = function(context?: object) {
export const getNewFileMenuEntries = function(context?: Folder) {
const newFileMenu = getNewFileMenu()
return newFileMenu.getEntries(context)
}
14 changes: 9 additions & 5 deletions lib/newFileMenu.ts
Expand Up @@ -20,17 +20,21 @@
*
*/

import { Folder } from '.'
import logger from './utils/logger'

export interface Entry {
/** Unique ID */
id: string
/** Translatable string displayed in the menu */
displayName: string
// Default new file name
/** Default new file name */
templateName?: string
// Condition wether this entry is shown or not
if?: (context: object) => boolean
/**
* Condition wether this entry is shown or not
* @param {Folder} context the creation context. Usually the current folder
*/
if?: (context: Folder) => boolean
/**
* Either iconSvgInline or iconClass must be defined
* Svg as inline string. <svg><path fill="..." /></svg>
Expand Down Expand Up @@ -67,9 +71,9 @@ export class NewFileMenu {
/**
* Get the list of registered entries
*
* @param {FileInfo} context the creation context. Usually the current folder FileInfo
* @param {Folder} context the creation context. Usually the current folder FileInfo
*/
public getEntries(context?: object): Array<Entry> {
public getEntries(context?: Folder): Array<Entry> {
if (context) {
return this._entries
.filter(entry => typeof entry.if === 'function' ? entry.if(context) : true)
Expand Down

0 comments on commit 848391c

Please sign in to comment.