Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
add natural sort for notes, introduce new label #sortNatural
Nriverd3ec9f022c6a
diff --git a/src/public/app/widgets/dialogs/sort_child_notes.js b/src/public/app/widgets/dialogs/sort_child_notes.jsindex 65440efe4e..ccb12544a1 100644--- a/src/public/app/widgets/dialogs/sort_child_notes.js+++ b/src/public/app/widgets/dialogs/sort_child_notes.js@@ -65,6 +65,17 @@ const TPL = `<div class="sort-child-notes-dialog modal mx-auto" tabindex="-1" rosort folders at the top</label></div>++ <br />++ <h5>Natural Sort</h5>++ <div class="form-check">+ <label class="form-check-label">+ <input class="form-check-input" type="checkbox" name="sort-natural" value="1">+ sort by alphanumeric order+ </label>+ </div></div><div class="modal-footer"><button type="submit" class="btn btn-primary">Sort <kbd>enter</kbd></button>@@ -83,8 +94,9 @@ export default class SortChildNotesDialog extends BasicWidget {const sortBy = this.$form.find("input[name='sort-by']:checked").val();const sortDirection = this.$form.find("input[name='sort-direction']:checked").val();const foldersFirst = this.$form.find("input[name='sort-folders-first']").is(":checked");+ const sortNatural = this.$form.find("input[name='sort-natural']").is(":checked");- await server.put(`notes/${this.parentNoteId}/sort-children`, {sortBy, sortDirection, foldersFirst});+ await server.put(`notes/${this.parentNoteId}/sort-children`, {sortBy, sortDirection, foldersFirst, sortNatural});utils.closeActiveDialog();});diff --git a/src/routes/api/notes.js b/src/routes/api/notes.jsindex 2083a98581..e019ddb8b4 100644--- a/src/routes/api/notes.js+++ b/src/routes/api/notes.js@@ -94,13 +94,13 @@ function undeleteNote(req) {function sortChildNotes(req) {const noteId = req.params.noteId;- const {sortBy, sortDirection, foldersFirst} = req.body;+ const {sortBy, sortDirection, foldersFirst, sortNatural} = req.body;- log.info(`Sorting '${noteId}' children with ${sortBy} ${sortDirection}, foldersFirst=${foldersFirst}`);+ log.info(`Sorting '${noteId}' children with ${sortBy} ${sortDirection}, foldersFirst=${foldersFirst}, sortNatural=${sortNatural}`);const reverse = sortDirection === 'desc';- treeService.sortNotes(noteId, sortBy, reverse, foldersFirst);+ treeService.sortNotes(noteId, sortBy, reverse, foldersFirst, sortNatural);}function protectNote(req) {diff --git a/src/services/builtin_attributes.js b/src/services/builtin_attributes.jsindex 1597c65999..b05ef66e54 100644--- a/src/services/builtin_attributes.js+++ b/src/services/builtin_attributes.js@@ -42,6 +42,7 @@ module.exports = [{ type: 'label', name: 'sorted' },{ type: 'label', name: 'sortDirection' },{ type: 'label', name: 'sortFoldersFirst' },+ { type: 'label', name: 'sortNatural' },{ type: 'label', name: 'top' },{ type: 'label', name: 'fullContentWidth' },{ type: 'label', name: 'shareHiddenFromTree' },diff --git a/src/services/handlers.js b/src/services/handlers.jsindex 4bb21f9104..e0902d8bc7 100644--- a/src/services/handlers.js+++ b/src/services/handlers.js@@ -46,7 +46,7 @@ eventService.subscribe([ eventService.ENTITY_CHANGED, eventService.ENTITY_DELETEif (entityName === 'attributes') {runAttachedRelations(entity.getNote(), 'runOnAttributeChange', entity);- if (entity.type === 'label' && ['sorted', 'sortDirection', 'sortFoldersFirst'].includes(entity.name)) {+ if (entity.type === 'label' && ['sorted', 'sortDirection', 'sortFoldersFirst', 'sortNatural'].includes(entity.name)) {handleSortedAttribute(entity);} else if (entity.type === 'label') {handleMaybeSortingLabel(entity);@@ -101,7 +101,7 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>noteService.duplicateSubtreeWithoutRoot(templateNote.noteId, note.noteId);}}- else if (entity.type === 'label' && ['sorted', 'sortDirection', 'sortFoldersFirst'].includes(entity.name)) {+ else if (entity.type === 'label' && ['sorted', 'sortDirection', 'sortFoldersFirst', 'sortNatural'].includes(entity.name)) {handleSortedAttribute(entity);}else if (entity.type === 'label') {diff --git a/src/services/tree.js b/src/services/tree.jsindex a0feb07a02..bf537c9851 100644--- a/src/services/tree.js+++ b/src/services/tree.js@@ -123,7 +123,7 @@ function loadSubtreeNoteIds(parentNoteId, subtreeNoteIds) {}}-function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, foldersFirst = false) {+function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, foldersFirst = false, sortNatural = false) {if (!customSortBy) {customSortBy = 'title';}@@ -153,7 +153,14 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder}function compare(a, b) {- return b === null || b === undefined || a < b ? -1 : 1;+ if (!sortNatural){+ // alphabetical sort+ return b === null || b === undefined || a < b ? -1 : 1;+ } else {+ // alphanumeric sort, or natural sort+ return a.localeCompare(b, undefined, {numeric: true, sensitivity: 'base'});+ }+}const topAEl = fetchValue(a, 'top');@@ -224,8 +231,9 @@ function sortNotesIfNeeded(parentNoteId) {const sortReversed = parentNote.getLabelValue('sortDirection')?.toLowerCase() === "desc";const sortFoldersFirstLabel = parentNote.getLabel('sortFoldersFirst');const sortFoldersFirst = sortFoldersFirstLabel && sortFoldersFirstLabel.value.toLowerCase() !== "false";-- sortNotes(parentNoteId, sortedLabel.value, sortReversed, sortFoldersFirst);+ const sortNaturalLabel = parentNote.getLabel('sortNatural');+ const sortNatural = sortNaturalLabel && sortNaturalLabel.value.toLowerCase() !== "false";+ sortNotes(parentNoteId, sortedLabel.value, sortReversed, sortFoldersFirst, sortNatural);}/**