From 984bd726e920add359cfd11b08ee169f992d22ad Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 25 Sep 2023 23:25:00 +0200 Subject: [PATCH] add support for "bottom" sort, closes #4277 --- src/services/builtin_attributes.js | 1 + src/services/handlers.js | 9 ++++++++- src/services/tree.js | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/services/builtin_attributes.js b/src/services/builtin_attributes.js index c4388622a..5083f3dc7 100644 --- a/src/services/builtin_attributes.js +++ b/src/services/builtin_attributes.js @@ -45,6 +45,7 @@ module.exports = [ { type: 'label', name: 'sortNatural' }, { type: 'label', name: 'sortLocale' }, { type: 'label', name: 'top' }, + { type: 'label', name: 'bottom' }, { type: 'label', name: 'fullContentWidth' }, { type: 'label', name: 'shareHiddenFromTree' }, { type: 'label', name: 'shareAlias' }, diff --git a/src/services/handlers.js b/src/services/handlers.js index e44e3ff5f..06cb35abe 100644 --- a/src/services/handlers.js +++ b/src/services/handlers.js @@ -171,8 +171,15 @@ function handleMaybeSortingLabel(entity) { if (note) { for (const parentNote of note.getParentNotes()) { const sorted = parentNote.getLabelValue("sorted"); + if (sorted === null) { + // checking specifically for null since that means the label doesn't exist + // empty valued "sorted" is still valid + continue; + } - if (sorted?.includes(entity.name)) { // hacky check if the sorting is affected by this label + if (sorted.includes(entity.name) // hacky check if this label is used in the sort + || entity.name === "top" + || entity.name === "bottom") { treeService.sortNotesIfNeeded(parentNote.noteId); } } diff --git a/src/services/tree.js b/src/services/tree.js index adc075f28..a614669a9 100644 --- a/src/services/tree.js +++ b/src/services/tree.js @@ -126,6 +126,14 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder return compare(topAEl, topBEl) * (reverse ? -1 : 1); } + const bottomAEl = fetchValue(a, 'bottom'); + const bottomBEl = fetchValue(b, 'bottom'); + + if (bottomAEl !== bottomBEl) { + // since "bottom" should not be reversible, we'll reverse it once more to nullify this effect + return compare(bottomBEl, bottomAEl) * (reverse ? -1 : 1); + } + const customAEl = fetchValue(a, customSortBy); const customBEl = fetchValue(b, customSortBy);