Skip to content

Commit

Permalink
feat: support new file menu entry order and remove unused templateName
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 29, 2023
1 parent ebe87cc commit 0768b2c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
7 changes: 6 additions & 1 deletion lib/index.ts
Expand Up @@ -70,5 +70,10 @@ export const removeNewFileMenuEntry = function(entry: Entry | string) {
*/
export const getNewFileMenuEntries = function(context?: Folder) {
const newFileMenu = getNewFileMenu()
return newFileMenu.getEntries(context)
return newFileMenu.getEntries(context).sort((a: Entry, b: Entry) => {
if (a.order !== undefined && b.order !== undefined) {
return a.order - b.order
}
return a.displayName.localeCompare(b.displayName)
})
}
28 changes: 13 additions & 15 deletions lib/newFileMenu.ts
Expand Up @@ -26,29 +26,35 @@ import logger from './utils/logger'
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
* @param {Folder} context the creation context. Usually the current folder
* @param 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>
*/
iconSvgInline?: string

/**
* Existing icon css class
* @deprecated use iconSvgInline instead
*/
iconClass?: string

/** Order of the entry in the menu */
order?: number

/**
* Function to be run after creation
* @param {Folder} context the creation context. Usually the current folder
* @param {Node[]} content list of file/folders present in the context folder
* @param context the creation context. Usually the current folder
* @param content list of file/folders present in the context folder
*/
handler: (context: Folder, content: Node[]) => void
}
Expand Down Expand Up @@ -93,7 +99,7 @@ export class NewFileMenu {
}

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

Expand All @@ -111,18 +117,10 @@ export class NewFileMenu {
throw new Error('Invalid if property')
}

if (entry.templateName && typeof entry.templateName !== 'string') {
throw new Error('Invalid templateName property')
}

if (entry.handler && typeof entry.handler !== 'function') {
if (typeof entry.handler !== 'function') {
throw new Error('Invalid handler property')
}

if (!entry.templateName && !entry.handler) {
throw new Error('At least a templateName or a handler must be provided')
}

if (this.getEntryIndex(entry.id) !== -1) {
throw new Error('Duplicate entry')
}
Expand Down

0 comments on commit 0768b2c

Please sign in to comment.