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
client/content manager/space usage: tweak the treemap appearance
Adorian Doran1c3af1da22ac
diff --git a/apps/client/src/widgets/react/charts/Treemap.css b/apps/client/src/widgets/react/charts/Treemap.cssindex 2c6e2760e7..ac933c13b4 100644--- a/apps/client/src/widgets/react/charts/Treemap.css+++ b/apps/client/src/widgets/react/charts/Treemap.css@@ -6,9 +6,10 @@/* Painted through a variable, not a competing rule: consumers set `--treemap-cell-background`and never race this stylesheet's cascade position. */+ /* The corner rounding is per cell — a cell rounds only the corners its block owns — so it is+ computed with the geometry rather than set here. */.treemap-cell {position: absolute;- border-radius: 4px;background: var(--treemap-cell-background, var(--accented-background-color));cursor: pointer;}diff --git a/apps/client/src/widgets/react/charts/Treemap.spec.tsx b/apps/client/src/widgets/react/charts/Treemap.spec.tsxindex 6e603f31f9..68af2cb8ca 100644--- a/apps/client/src/widgets/react/charts/Treemap.spec.tsx+++ b/apps/client/src/widgets/react/charts/Treemap.spec.tsx@@ -46,7 +46,7 @@ const ROOT: TreemapItem<string> = {data: "big",attributes: { "data-href": "#root/folder/big" }},- { id: "small", value: 100, data: "small" }+ { id: "small", value: 100, data: "small", attributes: { id: "cell-small" } }]},{ id: "bucket", value: 100, className: "bucket-cell", tooltip: "A crowd of notes" },@@ -131,6 +131,42 @@ describe("Treemap", () => {expect(probe.querySelector(".chart-tooltip")).toBeNull();});+ it("insets the whole map, so an edge cell's hover outline has room", () => {+ const probe = renderTreemap(ROOT);++ for (const cell of probe.querySelectorAll<HTMLElement>(".treemap-cell")) {+ const { left, top, width, height } = cell.style;+ expect(parseFloat(left)).toBeGreaterThanOrEqual(2);+ expect(parseFloat(top)).toBeGreaterThanOrEqual(2);+ // The stubbed container is 400×300.+ expect(parseFloat(left) + parseFloat(width)).toBeLessThanOrEqual(398);+ expect(parseFloat(top) + parseFloat(height)).toBeLessThanOrEqual(298);+ }+ });++ it("rounds each block's own corners, leaving the cuts inside a block square", () => {+ const probe = renderTreemap(ROOT);+ // Read as longhands: the shorthand collapses "4px 4px 4px 4px" down to "4px".+ const cornersOf = (selector: string) => {+ const style = probe.querySelector<HTMLElement>(selector)?.style;+ return [+ style?.borderTopLeftRadius, style?.borderTopRightRadius,+ style?.borderBottomRightRadius, style?.borderBottomLeftRadius+ ];+ };++ // A cell that is a block by itself owns all four corners.+ expect(cornersOf(".bucket-cell")).toEqual([ "4px", "4px", "4px", "4px" ]);++ // The two cells sharing a block split it, so between them they own its four corners while+ // the edge where they meet stays square.+ const grouped = [ '[data-href="#root/folder/big"]', "#cell-small" ].map(cornersOf);+ expect(grouped.flat().filter((corner) => corner === "4px").length).toBe(4);+ for (const corners of grouped) {+ expect(corners.filter((corner) => corner === "0px").length).toBeGreaterThan(0);+ }+ });+it("gives bigger values proportionally bigger areas", () => {const probe = renderTreemap(ROOT);const areaOf = (selector: string) => {diff --git a/apps/client/src/widgets/react/charts/Treemap.tsx b/apps/client/src/widgets/react/charts/Treemap.tsxindex a2dd475b6f..7ec2607a94 100644--- a/apps/client/src/widgets/react/charts/Treemap.tsx+++ b/apps/client/src/widgets/react/charts/Treemap.tsx@@ -1,7 +1,7 @@import "./Treemap.css";import clsx from "clsx";-import { hierarchy, treemap, treemapSquarify } from "d3-hierarchy";+import { hierarchy, type HierarchyRectangularNode, treemap, treemapSquarify } from "d3-hierarchy";import { useMemo } from "preact/hooks";import { useElementSize } from "../hooks";@@ -37,7 +37,20 @@ interface TreemapProps<T> {className?: string;}-const CELL_GAP = 3;+/** Between the top-level blocks — a group of related cells, or a lone cell standing on its own. */+const BLOCK_GAP = 4;++/** Between cells within one block, which read as one surface split up rather than separate things. */+const CELL_GAP = 2;++const BLOCK_CORNER_RADIUS = 4;++/** Breathing room at the map's edge: a hovered cell's outline straddles its border and would+ * otherwise be shaved off by the container's clipping along the outermost rows and columns. */+const MAP_INSET = 2;++/** Layout coordinates are floats; edges meant to coincide can differ in the last bit. */+const EDGE_EPSILON = 0.5;/*** A treemap over an arbitrary {@link TreemapItem} hierarchy, sized to fill its container and@@ -48,6 +61,10 @@ const CELL_GAP = 3;* no cell of their own, so nothing sits between the pointer and the leaf it is over. Identity is* explored by hovering, via the {@link TreemapItem.attributes} tooltips.*+ * Each top-level block is drawn as one rounded card: its cells sit {@link CELL_GAP} apart and square,+ * rounding only the corners they share with the block, so a group reads as a single surface divided+ * up rather than as loose tiles. Blocks stand {@link BLOCK_GAP} apart from each other.+ ** d3 computes the geometry only; the cells are plain absolutely-positioned divs so hover and* tooltips behave like everywhere else in the app. The hue tint is mixed into the theme's own* surface color, which keeps the map coherent under any user theme.@@ -71,7 +88,12 @@ export default function Treemap<T>({ root, onItemClick, className }: TreemapPropconst layout = treemap<TreemapItem<T>>().tile(treemapSquarify).size([ width, height ])- .paddingInner(CELL_GAP);+ // Invoked per node with children, separating that node's own children: the root spaces+ // the blocks apart, everything below it spaces cells within a block.+ .paddingInner((node) => node.depth === 0 ? BLOCK_GAP : CELL_GAP)+ // Only the root insets its children. A block must keep filling its own rect exactly,+ // or no cell would touch its corners and the block would lose its rounding.+ .paddingOuter((node) => node.depth === 0 ? MAP_INSET : 0);return layout(weighted).leaves().filter((leaf) => leaf.depth > 0);}, [ root, width, height ]);@@ -97,6 +119,7 @@ export default function Treemap<T>({ root, onItemClick, className }: TreemapProptop: cell.y0,width: cellWidth,height: cellHeight,+ borderRadius: blockCornerRadius(cell),...(item.hue !== undefined && { "--treemap-hue": String(item.hue) })}}onClick={onItemClick && (() => onItemClick(item))}@@ -110,3 +133,28 @@ export default function Treemap<T>({ root, onItemClick, className }: TreemapProp</div>);}++/**+ * The rounding a cell needs so its block is a rounded card: a cell rounds a corner only where that+ * corner is the block's own, leaving the cuts inside a block square. A cell that is a block by+ * itself therefore rounds all four.+ */+function blockCornerRadius<T>(cell: HierarchyRectangularNode<TreemapItem<T>>) {+ const block = cell.ancestors().find((node) => node.depth === 1) ?? cell;+ const atLeft = isSameEdge(cell.x0, block.x0);+ const atRight = isSameEdge(cell.x1, block.x1);+ const atTop = isSameEdge(cell.y0, block.y0);+ const atBottom = isSameEdge(cell.y1, block.y1);+ const corner = (isBlockCorner: boolean) => isBlockCorner ? `${BLOCK_CORNER_RADIUS}px` : "0";++ return [+ corner(atTop && atLeft),+ corner(atTop && atRight),+ corner(atBottom && atRight),+ corner(atBottom && atLeft)+ ].join(" ");+}++function isSameEdge(a: number, b: number) {+ return Math.abs(a - b) < EDGE_EPSILON;+}