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

fix(newfilemenu): better sorting and proper fallback to displayName #806

Merged
merged 1 commit into from
Oct 12, 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
17 changes: 14 additions & 3 deletions __tests__/newFileMenu.spec.ts
Expand Up @@ -409,7 +409,7 @@ describe('NewFileMenu sort test', () => {

const entry2 = {
id: 'image',
displayName: 'Create new image',
displayName: 'Create new image 2',
templateName: 'New drawing.png',
iconClass: 'icon-filetype-image',
order: 1,
Expand All @@ -425,14 +425,25 @@ describe('NewFileMenu sort test', () => {
handler: () => {},
}

const entry4 = {
id: 'image2',
displayName: 'Create new image 1',
templateName: 'New drawing 2.png',
iconClass: 'icon-filetype-image',
order: 1,
handler: () => {},
}

addNewFileMenuEntry(entry1)
addNewFileMenuEntry(entry2)
addNewFileMenuEntry(entry3)
addNewFileMenuEntry(entry4)

const entries = getNewFileMenuEntries()
expect(entries).toHaveLength(3)
expect(entries).toHaveLength(4)
expect(entries[0]).toBe(entry1)
expect(entries[1]).toBe(entry3)
expect(entries[2]).toBe(entry2)
expect(entries[2]).toBe(entry4)
expect(entries[3]).toBe(entry2)
})
})
8 changes: 6 additions & 2 deletions lib/index.ts
Expand Up @@ -71,9 +71,13 @@ export const removeNewFileMenuEntry = function(entry: Entry | string) {
export const getNewFileMenuEntries = function(context?: Folder) {
const newFileMenu = getNewFileMenu()
return newFileMenu.getEntries(context).sort((a: Entry, b: Entry) => {
if (a.order !== undefined && b.order !== undefined) {
// If defined and different, sort by order
if (a.order !== undefined
&& b.order !== undefined
&& a.order !== b.order) {
return a.order - b.order
}
return a.displayName.localeCompare(b.displayName)
// else sort by display name
return a.displayName.localeCompare(b.displayName, undefined, { numeric: true, sensitivity: 'base' })
})
}