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
fix(collections/dashboard): preserve layout of hidden archived widgets
Elian Doranac14fd27fac4
diff --git a/apps/client/src/widgets/collections/dashboard/index.tsx b/apps/client/src/widgets/collections/dashboard/index.tsxindex c7fa01e41c..95f52b75c3 100644--- a/apps/client/src/widgets/collections/dashboard/index.tsx+++ b/apps/client/src/widgets/collections/dashboard/index.tsx@@ -18,7 +18,7 @@ import NoteLink from "../../react/NoteLink";import { ViewModeProps } from "../interface";import { getNotePath, NoteContent } from "../legacy/ListOrGridView";import openWidgetContextMenu from "./context_menu";-import { computeDropCell, DEFAULT_WIDGET_SIZE, GRID_COLUMNS, sameLayout, WidgetLayouts } from "./layout";+import { computeDropCell, DEFAULT_WIDGET_SIZE, GRID_COLUMNS, reconcilePersistedLayout, sameLayout, WidgetLayouts } from "./layout";export interface DashboardViewConfig {widgets?: WidgetLayouts;@@ -116,12 +116,13 @@ function useDashboardLayoutPersistence({ viewConfig, saveConfig, gridRef, contaireturn;}- const widgets: WidgetLayouts = {};+ const present: WidgetLayouts = {};for (const node of grid.engine.nodes) {if (typeof node.id === "string") {- widgets[node.id] = { x: node.x ?? 0, y: node.y ?? 0, w: node.w ?? 1, h: node.h ?? 1 };+ present[node.id] = { x: node.x ?? 0, y: node.y ?? 0, w: node.w ?? 1, h: node.h ?? 1 };}}+ const widgets = reconcilePersistedLayout(savedWidgetsRef.current, present);if (sameLayout(widgets, savedWidgetsRef.current)) {return;}diff --git a/apps/client/src/widgets/collections/dashboard/layout.spec.ts b/apps/client/src/widgets/collections/dashboard/layout.spec.tsindex 0b0cd16510..5cf7cd2ccb 100644--- a/apps/client/src/widgets/collections/dashboard/layout.spec.ts+++ b/apps/client/src/widgets/collections/dashboard/layout.spec.ts@@ -1,7 +1,7 @@import type { GridStack } from "gridstack";import { describe, expect, it } from "vitest";-import { computeDropCell, DEFAULT_WIDGET_SIZE, GRID_COLUMNS, sameLayout, WidgetLayouts } from "./layout";+import { computeDropCell, DEFAULT_WIDGET_SIZE, GRID_COLUMNS, reconcilePersistedLayout, sameLayout, WidgetLayouts } from "./layout";/** Minimal GridStack stand-in exposing only what computeDropCell reads. */function fakeGrid({ columns = GRID_COLUMNS, cellHeight = 80 }: { columns?: number; cellHeight?: number } = {}) {@@ -47,6 +47,29 @@ describe("computeDropCell", () => {});});+describe("reconcilePersistedLayout", () => {+ const archived = { x: 5, y: 3, w: 4, h: 3 };+ const normal = { x: 0, y: 0, w: 4, h: 3 };++ it("retains a widget's saved position while it is filtered out of the grid (archived hidden → shown)", () => {+ // Archived notes shown: both widgets live in the grid and are persisted together.+ const shown: WidgetLayouts = { archived, normal };++ // Archived notes hidden: only the normal widget remains in the grid. Persisting the layout+ // now must not drop the archived widget's geometry just because it is no longer rendered —+ // otherwise re-showing it would auto-position it instead of restoring its placement.+ const afterHide = reconcilePersistedLayout(shown, { normal });+ expect(afterHide).toEqual({ archived, normal });+ });++ it("applies position changes and additions coming from the grid", () => {+ const previous: WidgetLayouts = { a: normal };+ const next = reconcilePersistedLayout(previous, { a: { ...normal, x: 6 }, b: archived });+ // Moved widgets win over their previous geometry, and brand-new widgets are included.+ expect(next).toEqual({ a: { ...normal, x: 6 }, b: archived });+ });+});+describe("sameLayout", () => {const base: WidgetLayouts = { a: { x: 0, y: 0, w: 4, h: 3 }, b: { x: 4, y: 0, w: 2, h: 2 } };diff --git a/apps/client/src/widgets/collections/dashboard/layout.ts b/apps/client/src/widgets/collections/dashboard/layout.tsindex a8bf346217..21e27b71ac 100644--- a/apps/client/src/widgets/collections/dashboard/layout.ts+++ b/apps/client/src/widgets/collections/dashboard/layout.ts@@ -29,6 +29,18 @@ export function computeDropCell(grid: GridStack, container: HTMLElement, e: { clreturn { x, y };}+/**+ * Merge the geometry of the widgets currently present in the grid (`present`) over the+ * previously-persisted layout (`previous`), producing the layout to persist next.+ *+ * A widget can be absent from `present` not only because it was removed from the dashboard, but+ * also because it was filtered out of view (e.g. archived notes hidden). Its saved position must be+ * retained so that toggling it back restores its placement instead of auto-positioning it.+ */+export function reconcilePersistedLayout(previous: WidgetLayouts, present: WidgetLayouts): WidgetLayouts {+ return { ...previous, ...present };+}+/** Whether two layouts describe the same widgets in the same positions. */export function sameLayout(a: WidgetLayouts, b: WidgetLayouts) {const aKeys = Object.keys(a);