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
feat(collections): honor #maxNestingDepth in calendar and geo map
Elian Doran0b9343707337
diff --git a/apps/client/src/entities/fnote.spec.ts b/apps/client/src/entities/fnote.spec.tsindex 0883c160db..e741885719 100644--- a/apps/client/src/entities/fnote.spec.ts+++ b/apps/client/src/entities/fnote.spec.ts@@ -219,6 +219,26 @@ describe("FNote branches & children", () => {expect(subtreeNotes.map((n) => n.noteId).sort()).toEqual(["branchA", "leafA"]);});+ it("getSubtreeNoteIds stops at maxDepth, a negative one meaning no limit", async () => {+ const root = buildNote({+ title: "root",+ children: [{+ id: "depth0",+ title: "depth0",+ children: [{+ id: "depth1",+ title: "depth1",+ children: [{ id: "depth2", title: "depth2" }]+ }]+ }]+ });++ expect(await root.getSubtreeNoteIds(false, 0)).toEqual(["depth0"]);+ expect(await root.getSubtreeNoteIds(false, 1)).toEqual(["depth0", "depth1"]);+ expect(await root.getSubtreeNoteIds(false, -1)).toEqual(["depth0", "depth1", "depth2"]);+ expect(await root.getSubtreeNoteIds()).toEqual(["depth0", "depth1", "depth2"]);+ });+it("getChildNoteIdsWithArchiveFiltering returns children directly for hidden/search/includeArchived", async () => {const hidden = buildNote({ id: "_hiddenParent", title: "hidden", children: [{ id: "hc", title: "hc" }] });expect(await hidden.getChildNoteIdsWithArchiveFiltering()).toEqual(["hc"]);diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.tsindex 09edb74777..5a6279cdc9 100644--- a/apps/client/src/entities/fnote.ts+++ b/apps/client/src/entities/fnote.ts@@ -272,13 +272,19 @@ export default class FNote {return this.children;}- async getSubtreeNoteIds(includeArchived = false) {+ /**+ * @param maxDepth how many levels below the children to descend: `0` returns only the children,+ * and a negative value descends without limit.+ */+ async getSubtreeNoteIds(includeArchived = false, maxDepth = -1) {const noteIds: (string | string[])[] = [];for (const child of await this.getChildNotes()) {if (child.isArchived && !includeArchived) continue;noteIds.push(child.noteId);- noteIds.push(await child.getSubtreeNoteIds(includeArchived));+ if (maxDepth !== 0) {+ noteIds.push(await child.getSubtreeNoteIds(includeArchived, maxDepth - 1));+ }}return noteIds.flat();}diff --git a/apps/client/src/widgets/collections/NoteList.spec.tsx b/apps/client/src/widgets/collections/NoteList.spec.tsxindex 44729fbfa7..41810e4435 100644--- a/apps/client/src/widgets/collections/NoteList.spec.tsx+++ b/apps/client/src/widgets/collections/NoteList.spec.tsx@@ -20,6 +20,7 @@ import type { EntityChange } from "../../server_types";import LoadResults from "../../services/load_results";import { buildNote, buildNotes } from "../../test/easy-froca";import { ParentComponent } from "../react/react_utils";+import type { ViewTypeOptions } from "./interface";import { CustomNoteList, useNoteIds } from "./NoteList";let currentNoteIds: string[] = [];@@ -38,8 +39,8 @@ async function flushMicrotasks() {}/** Renders `useNoteIds` and exposes its current value through the module-level `currentNoteIds`. */-function Harness({ note }: { note: FNote }) {- currentNoteIds = useNoteIds(note, "dashboard", "ntx-1");+function Harness({ note, viewType = "dashboard" }: { note: FNote; viewType?: ViewTypeOptions }) {+ currentNoteIds = useNoteIds(note, viewType, "ntx-1");return null;}@@ -194,6 +195,51 @@ describe("useNoteIds refresh race", () => {});});+describe("useNoteIds nesting depth", () => {+ async function renderNoteIds(viewType: ViewTypeOptions, labels: Record<string, string> = {}) {+ const note = buildNote({+ title: "Collection",+ type: "book",+ ...labels,+ children: [{+ id: "nested-0",+ title: "Depth 0",+ children: [{+ id: "nested-1",+ title: "Depth 1",+ children: [{ id: "nested-2", title: "Depth 2" }]+ }]+ }]+ });+ const mountPoint = document.createElement("div");+ currentNoteIds = [];+ await act(async () => {+ render(+ <ParentComponent.Provider value={new Component()}>+ <Harness note={note} viewType={viewType} />+ </ParentComponent.Provider>,+ mountPoint+ );+ });+ await act(async () => {+ await flushMicrotasks();+ });+ await act(async () => {+ render(null, mountPoint);+ });+ return currentNoteIds;+ }++ it("limits the calendar and geo map subtree to #maxNestingDepth", async () => {+ for (const viewType of [ "calendar", "geoMap" ] as const) {+ expect(await renderNoteIds(viewType)).toEqual([ "nested-0", "nested-1", "nested-2" ]);+ expect(await renderNoteIds(viewType, { "#maxNestingDepth": "0" })).toEqual([ "nested-0" ]);+ expect(await renderNoteIds(viewType, { "#maxNestingDepth": "1" }))+ .toEqual([ "nested-0", "nested-1" ]);+ }+ });+});+/*** Regression test for #7901: the legacy list/grid views defer rendering until the widget scrolls* into view via an IntersectionObserver. The observer's first callback fires right afterdiff --git a/apps/client/src/widgets/collections/NoteList.tsx b/apps/client/src/widgets/collections/NoteList.tsxindex eca49e22d7..75d0b8e504 100644--- a/apps/client/src/widgets/collections/NoteList.tsx+++ b/apps/client/src/widgets/collections/NoteList.tsx@@ -9,7 +9,7 @@ import FNote from "../../entities/fnote";import type { PrintReport } from "../../print";import froca from "../../services/froca";import { subscribeToMessages, unsubscribeToMessage as unsubscribeFromMessage } from "../../services/ws";-import { useGetContextDataFrom, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useTriliumEvent } from "../react/hooks";+import { useGetContextDataFrom, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteLabelInt, useNoteProperty, useTriliumEvent } from "../react/hooks";import { allViewTypes, ViewModeMedia, ViewModeProps, ViewTypeOptions } from "./interface";import ViewModeStorage, { type ViewModeStorageType } from "./view_mode_storage";@@ -190,6 +190,10 @@ export function useNoteIds(note: FNote | null | undefined, viewType: ViewTypeOptconst [ noteIds, setNoteIds ] = useState<string[]>([]);const [ includeArchived ] = useNoteLabelBoolean(note, "includeArchived");const directChildrenOnly = (viewType === "list" || viewType === "grid" || viewType === "table" || viewType === "dashboard" || note?.type === "search");+ // The board walks the subtree itself in `getBoardData()`, so its note IDs stay unlimited to+ // match the cards it draws.+ const [ labelMaxDepth ] = useNoteLabelInt(note, "maxNestingDepth");+ const maxDepth = (viewType === "calendar" || viewType === "geoMap") ? labelMaxDepth : undefined;// getNoteIds can do a server round-trip (archive filtering), so concurrent refreshes may resolve// out of order. Track the latest issued refresh so a stale one can't clobber a newer result.const refreshSeqRef = useRef(0);@@ -211,14 +215,14 @@ export function useNoteIds(note: FNote | null | undefined, viewType: ViewTypeOptif (directChildrenOnly) {return await note.getChildNoteIdsWithArchiveFiltering(includeArchived);}- return await note.getSubtreeNoteIds(includeArchived);+ return await note.getSubtreeNoteIds(includeArchived, maxDepth);}// Refresh on note switch.useEffect(() => {refreshNoteIds();- }, [ note, includeArchived, directChildrenOnly ]);+ }, [ note, includeArchived, directChildrenOnly, maxDepth ]);// Refresh on alterations to the note subtree.useTriliumEvent("entitiesReloaded", ({ loadResults }) => {diff --git a/apps/client/src/widgets/note_bars/collection-properties-config.tsx b/apps/client/src/widgets/note_bars/collection-properties-config.tsxindex fe1bfc9711..d170083beb 100644--- a/apps/client/src/widgets/note_bars/collection-properties-config.tsx+++ b/apps/client/src/widgets/note_bars/collection-properties-config.tsx@@ -89,7 +89,9 @@ export const bookPropertiesConfig: Record<ViewTypeOptions, BookConfig> = {options: slotDurationOptions([15, 30, 60])}]- }+ },+ maxNestingDepthProperty((note) =>+ note.hasLabel("calendarRoot") || note.hasLabel("workspaceCalendarRoot"))]},geoMap: {@@ -145,19 +147,13 @@ export const bookPropertiesConfig: Record<ViewTypeOptions, BookConfig> = {icon: "bx bx-collection",type: "checkbox",bindToLabel: "map:cluster"- }+ },+ maxNestingDepthProperty()]},table: {properties: [- {- label: t("book_properties_config.max-nesting-depth"),- icon: "bx bx-subdirectory-right",- type: "number",- bindToLabel: "maxNestingDepth",- width: 65,- disabled: (note) => note.type === "search"- }+ maxNestingDepthProperty()]},board: {@@ -198,6 +194,21 @@ function buildMapLayer([ id, layer ]: [ string, MapLayer ]): ComboBoxItem {};}+/**+ * The `#maxNestingDepth` input, disabled for a search collection, which shows only its results.+ * @param isDisabled disables the input for further notes.+ */+function maxNestingDepthProperty(isDisabled?: (note: FNote) => boolean): BookProperty {+ return {+ label: t("book_properties_config.max-nesting-depth"),+ icon: "bx bx-subdirectory-right",+ type: "number",+ bindToLabel: "maxNestingDepth",+ width: 65,+ disabled: (note) => note.type === "search" || !!isDisabled?.(note)+ };+}+/** Builds FullCalendar duration options (`HH:MM:00`) from a list of whole minutes, with localized labels. */function slotDurationOptions(minutes: number[]): ComboBoxItem[] {return minutes.map((total) => ({