Skip to content

Build your personal knowledge base with Trilium Notes

fix(sort): re-sort when a #sorted label is inherited from a template

Scott Campbell77bc407707bb
diff --git a/packages/trilium-core/src/services/handlers.spec.ts b/packages/trilium-core/src/services/handlers.spec.ts
index 0892b573f1..b8c53fc53f 100644
--- a/packages/trilium-core/src/services/handlers.spec.ts
+++ b/packages/trilium-core/src/services/handlers.spec.ts
@@ -141,6 +141,41 @@ describe("handlers", () => {
eventService.emit(eventService.ENTITY_CHANGED, { entityName: "attributes", entity: topAttr });
expect(sortNotesIfNeeded).toHaveBeenCalledWith("par");
});
+
+ it("re-sorts the parents of notes that inherit a change to a #sorted label", () => {
+ buildNote({ id: "ai-tpl" });
+ buildNote({ id: "ai-folder", children: [{ id: "ai-clone" }] });
+ buildNote({
+ id: "ai-par",
+ "#sorted": "#archived last",
+ children: [{ id: "ai-chld", "~template": "ai-tpl" }, { id: "ai-other" }]
+ });
+ buildNote({ id: "ai-plain", "#sorted": "", children: [{ id: "ai-plain-chld" }] });
+ new BBranch({
+ noteId: "ai-clone", parentNoteId: "ai-par", branchId: "ai-par_ai-clone"
+ });
+ new BBranch({
+ noteId: "ai-clone", parentNoteId: "ai-plain", branchId: "ai-plain_ai-clone"
+ });
+
+ const templateArchived = addAttribute("ai-tpl", "label", "archived", "");
+ eventService.emit(eventService.ENTITY_CHANGED, {
+ entityName: "attributes", entity: templateArchived
+ });
+ expect(sortNotesIfNeeded).toHaveBeenCalledWith("ai-par");
+ sortNotesIfNeeded.mockClear();
+
+ const folderArchived = new BAttribute({
+ noteId: "ai-folder", attributeId: randomString(12), type: "label",
+ name: "archived", value: "", position: 0, isInheritable: true
+ });
+ eventService.emit(eventService.ENTITY_CHANGED, {
+ entityName: "attributes", entity: folderArchived
+ });
+ expect(sortNotesIfNeeded).toHaveBeenCalledWith("ai-par");
+ // `ai-plain` also holds the clone, but its #sorted does not name #archived.
+ expect(sortNotesIfNeeded).not.toHaveBeenCalledWith("ai-plain");
+ });
});
describe("ENTITY_CREATED (template relation)", () => {
diff --git a/packages/trilium-core/src/services/handlers.ts b/packages/trilium-core/src/services/handlers.ts
index 9a78829b05..589ae6ba56 100644
--- a/packages/trilium-core/src/services/handlers.ts
+++ b/packages/trilium-core/src/services/handlers.ts
@@ -185,24 +185,38 @@ function handleMaybeSortingLabel(entity: BAttribute) {
const note = becca.notes[entity.noteId];
// this will not work on deleted notes, but in that case we don't really need to re-sort
- 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 (!note || !becca.findAttributes("label", "sorted").some(
+ (sorted) => sortsByLabel(sorted.value, entity.name)
+ )) {
+ return;
+ }
- if (
- entity.name === "top" ||
- entity.name === "bottom" ||
- parseSortCriteria(sorted).some(({ key, isLabel }) => isLabel && key === entity.name)
- ) {
- treeService.sortNotesIfNeeded(parentNote.noteId);
- }
+ // An inheritable or template label also applies to the notes that inherit it.
+ const affectedNotes = entity.isInheritable
+ ? note.getSubtreeNotesIncludingTemplated()
+ : note.getInheritingNotes();
+ const parentNotes = new Set<BNote>();
+
+ for (const affectedNote of affectedNotes) {
+ for (const parentNote of affectedNote.getParentNotes()) {
+ parentNotes.add(parentNote);
}
}
+
+ for (const parentNote of parentNotes) {
+ const sorted = parentNote.getLabelValue("sorted");
+ // checking specifically for null since that means the label doesn't exist
+ // empty valued "sorted" is still valid
+ if (sorted !== null && sortsByLabel(sorted, entity.name)) {
+ treeService.sortNotesIfNeeded(parentNote.noteId);
+ }
+ }
+}
+
+function sortsByLabel(sorted: string, labelName: string) {
+ return labelName === "top"
+ || labelName === "bottom"
+ || parseSortCriteria(sorted).some(({ key, isLabel }) => isLabel && key === labelName);
}
eventService.subscribe(eventService.ENTITY_CHANGED, ({ entityName, entity }) => {