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/collections/board: performance improvements
Adorian Doran097702a6dffe
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex fa5d656408..dcaa11c991 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1096,10 +1096,6 @@ table.promoted-attributes-in-tooltip th {color: orange;}-.multiplicity {- font-size: 1.3em;-}-/* this is because bootstrap (?) sets code color to red for some reason */code {color: inherit !important;diff --git a/apps/client/src/stylesheets/theme-next/shell.css b/apps/client/src/stylesheets/theme-next/shell.cssindex eb8eef78ed..d920e0abbc 100644--- a/apps/client/src/stylesheets/theme-next/shell.css+++ b/apps/client/src/stylesheets/theme-next/shell.css@@ -1539,8 +1539,7 @@ div.promoted-attribute-cell {}/* A promoted attribute card (boolean attribute) */-div.promoted-attribute-cell:has(input[type="checkbox"]):not(:has(.multiplicity > span)) {- /* Checbox attribute, without multiplicity */+div.promoted-attribute-cell:has(input[type="checkbox"]) {padding-inline-end: var(--pa-card-padding-inline-start);}@@ -1614,22 +1613,6 @@ div.promoted-attribute-cell.promoted-attribute-label-boolean > div:first-of-typemargin-inline-end: 0.5em;}-/* The element containing the "new attribute" and "remove this attribute button" */-div.promoted-attribute-cell .multiplicity:has(span) {- --icon-button-size: 24px;-- margin-inline-start: 8px;- margin-inline-end: calc(var(--pa-card-padding-inline-start) - var(--pa-card-padding-inline-end));- font-size: 0; /* Prevent whitespaces creating a gap between buttons */- display: flex;-}--div.promoted-attribute-cell .multiplicity:has(span) span {- display: flex;- align-items: center;- justify-content: center;-}-div.promoted-attribute-cell.promoted-attribute-label-color {justify-content: space-between;}diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex d3e2b5057c..d5fc6dbfa1 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -519,6 +519,10 @@ body.mobile .board-view-container .board-add-column {overscroll-behavior-y: contain;/* What the gap is placed against, and what a card's `offsetTop` is then counted from. */position: relative;+ /* Pinned rather than left to stretch: a column animates its own width open, and a body that+ follows it lays out every card in it on every frame, their titles rewrapping as it grows.+ Matches the column's content box, so a column standing still is drawn no differently. */+ width: calc(var(--board-column-width) - 2 * var(--board-column-border-width));}/* And while something is carried, the end does not spring back either: the copy is positioned@@ -976,19 +980,27 @@ body.mobile .column-drop-placeholder {/* The children are named rather than matched with `> *`: a universal selector cannot be narrowedinto an invalidation set, so the class going on and off restyles the whole column. */.board-view-container .board-column.expanding > h3,-.board-view-container .board-column.expanding > .board-column-content,.board-view-container .board-column.expanding > .board-new-item {width: calc(var(--board-column-width) - 2 * var(--board-column-border-width));}-/* The heading stays where it is, and is revealed with the column; only the cards wait. They fade- in as the class is dropped, through the transition each of them already carries: the scrolling- body cannot, its own `transition` being the scroll fade's. */-.board-view-container .board-column.expanding .board-note,+/* The heading stays where it is, and is revealed with the column; only the cards wait. Hidden by+ one property on the body that scrolls them rather than by one on each card: a rule reaching+ every card restyles the whole column. */+.board-view-container .board-column.expanding > .board-column-content,.board-view-container .board-column.expanding > .board-new-item {opacity: 0;}+/* Carries the fade the cards are revealed by. Restated rather than added to, `transition` being a+ single property: the two fade entries must match `.scroll-fade` in scroll_fade.css. */+.board-view-container .board-column-content {+ transition:+ --scroll-fade-start var(--scroll-fade-duration, 300ms) ease,+ --scroll-fade-end var(--scroll-fade-duration, 300ms) ease,+ opacity 0.15s ease;+}+@media (prefers-reduced-motion: reduce) {/* Matching the selectors above, which these have to outweigh rather than merely follow. */.board-view-container .board-column.collapsed:not(.board-drag-preview),@@ -997,10 +1009,15 @@ body.mobile .column-drop-placeholder {}/* The column is at its width already, so there is nothing for the cards to wait for. */- .board-view-container .board-column.expanding .board-note,+ .board-view-container .board-column.expanding > .board-column-content,.board-view-container .board-column.expanding > .board-new-item {opacity: 1;}++ /* `.scroll-fade` turns its own off here, and the rule above outweighs it. */+ .board-view-container .board-column-content {+ transition: none;+ }}/* On the column, not the header: `overflow: hidden` above would clip a descendant's outline. */diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.cssindex c3edf6055b..2c18a06cef 100644--- a/apps/client/src/widgets/react/Card.css+++ b/apps/client/src/widgets/react/Card.css@@ -177,10 +177,8 @@ body[dir=rtl] .tn-card-option::after {}/* On a narrow card a text box, a combo or any other wide control has no room left beside its label,- so it takes the line below it instead. Small controls stay beside the label: they leave it room,- and moving a switch down would cost a whole row and break the line the labels are read down. A- bare figure is small enough to stay; one carrying a unit beside it is not. A chevron is no- control at all but the mark of a row that leads somewhere, and keeps its place at every width. */+ so it takes the line below it instead. `OptionCardSection` reads which control a row holds and+ marks the wide ones; `SMALL_CONTROLS` there is the list that stays beside the label. */@container (max-width: 500px) {/* On a narrow card the name reads as a field label, set apart by weight rather than by size.Every row gets it, or a card would mix two kinds of label between its rows. */@@ -194,10 +192,7 @@ body[dir=rtl] .tn-card-option::after {--card-padding-block: 12px;}- .tn-card-option:not(:has(> :is(- .switch-widget, .icon-action, .tn-card-option-actions, select, .dropdown,- .tn-card-section-chevron, input[type="number"]- ))) {+ .tn-card-option-wide {flex-direction: column;align-items: stretch;/* Down the page the label already leads its control, so they need less room between. */diff --git a/apps/client/src/widgets/react/Card.spec.tsx b/apps/client/src/widgets/react/Card.spec.tsxindex ca8696a2b2..bb5866ee71 100644--- a/apps/client/src/widgets/react/Card.spec.tsx+++ b/apps/client/src/widgets/react/Card.spec.tsx@@ -1,3 +1,5 @@+import { ComponentChild } from "preact";+import { act } from "preact/test-utils";import { describe, expect, it, vi } from "vitest";import { renderInto } from "../../test/render";@@ -109,6 +111,30 @@ describe("OptionCardSection", () => {const stacked = renderInto(<OptionCardSection label="Address" stacked><input /></OptionCardSection>);expect(stacked.querySelector(".tn-card-option")?.className).toContain("tn-card-option-stacked");});++ /**+ * Which control a row holds is read off the row rather than matched by a `:has()` rule, a rule+ * of that shape costing a restyle of the whole document on every change made anywhere in it.+ */+ it("marks a row whose control has no room to stand beside its label", () => {+ // The row is read once it stands in the page, so the draw has to have settled.+ const draw = (node: ComponentChild) => {+ let container: HTMLElement | undefined;+ act(() => { container = renderInto(node); });+ return container?.querySelector(".tn-card-option")?.className ?? "";+ };++ expect(draw(<OptionCardSection label="Address"><input type="text" /></OptionCardSection>))+ .toContain("tn-card-option-wide");+ expect(draw(<OptionCardSection label="Enabled"><span className="switch-widget" /></OptionCardSection>))+ .not.toContain("tn-card-option-wide");+ expect(draw(<OptionCardSection label="Copies"><input type="number" /></OptionCardSection>))+ .not.toContain("tn-card-option-wide");+ // A row that leads somewhere carries a chevron rather than a control, and keeps its place+ // at every width.+ expect(draw(<OptionCardSection label="Backup" href="#root/_hidden/_options" />))+ .not.toContain("tn-card-option-wide");+ });});describe("a segment that leads somewhere", () => {diff --git a/apps/client/src/widgets/react/Card.tsx b/apps/client/src/widgets/react/Card.tsxindex d8b387bb94..b966f66dc9 100644--- a/apps/client/src/widgets/react/Card.tsx+++ b/apps/client/src/widgets/react/Card.tsx@@ -1,7 +1,7 @@import "./Card.css";import { cloneElement, ComponentChildren, createContext, isValidElement } from "preact";import { JSX, HTMLAttributes } from "preact";-import { useContext } from "preact/hooks";+import { useCallback, useContext, useLayoutEffect, useRef, useState } from "preact/hooks";import clsx from "clsx";import {@@ -117,6 +117,8 @@ export interface CardSectionProps {* as any of them is (see {@link FilterRole}).*/filterRole?: FilterRole;+ /** Hands back the element the section is drawn as, for a caller that reads what it holds. */+ elementRef?: (element: HTMLElement | null) => void;}interface CardSectionContextType {@@ -139,6 +141,7 @@ export function CardSection(props: {children: ComponentChildren} & CardSectionPrreturn <>{props.href? <a className={clsx(className, "tn-card-section-link", "no-tooltip-preview")}+ ref={props.elementRef}style={style}href={props.href}data-no-contained-navigation={props.noContainedNavigation ? "" : undefined}@@ -147,6 +150,7 @@ export function CardSection(props: {children: ComponentChildren} & CardSectionPr<span className="tn-card-section-chevron" /></a>: <section className={className}+ ref={props.elementRef}style={style}onClick={props.onAction}>{props.children}@@ -205,11 +209,24 @@ export function OptionCardSection(props: OptionCardSectionProps) {const filtering = useIsFiltering();const id = useUniqueName(name);const bound = !!name && isValidElement(children);+ const row = useRef<HTMLElement | null>(null);+ const keepRow = useCallback((element: HTMLElement | null) => { row.current = element; }, []);+ const [ wide, setWide ] = useState(false);++ // Whether the control is one of the wide ones, which stack under the label on a narrow card.+ // Read off the row rather than matched by a `:has()` rule: Chrome restyles the whole document+ // on every structural change anywhere in it for as long as such a rule is loaded.+ useLayoutEffect(() => {+ const element = row.current;+ setWide(!!element && !holdsSmallControl(element));+ });if (!matched && !subSections) return null;- return <CardSection className={clsx("tn-card-option", className, {- "tn-card-option-stacked": stacked+ return <CardSection elementRef={keepRow}+ className={clsx("tn-card-option", className, {+ "tn-card-option-stacked": stacked,+ "tn-card-option-wide": wide})}// Kept for its details rather than itself, which is what a companion is.filterRole={filtering ? (matched ? "match" : "companion") : undefined}@@ -229,4 +246,24 @@ export function OptionCardSection(props: OptionCardSectionProps) {</CardSection>;}+/**+ * Controls small enough to stay beside their label on a narrow card.+ *+ * A switch moved below its label would cost a whole row and break the line the labels are read+ * down. A bare figure is small enough to stay; one carrying a unit beside it is not. A chevron is+ * no control at all but the mark of a row that leads somewhere, and keeps its place at every width.+ */+const SMALL_CONTROLS = ".switch-widget, .icon-action, .tn-card-option-actions, select, .dropdown,"+ + " .tn-card-section-chevron, input[type=\"number\"]";++function holdsSmallControl(element: HTMLElement) {+ for (const child of element.children) {+ if (child.matches(SMALL_CONTROLS)) {+ return true;+ }+ }++ return false;+}+// #endregion\ No newline at end of file