Edit the whole dashboard, and let the grid follow its canvas

The editor only ever arranged the first section, so the demo's Home
dashboard lost 16 of its 25 widgets the moment it was edited. A page's
sections are now read as one arrangement — each pushed below the one
before it — and written back as one, which is the shape a dashboard was
already heading for: one dashboard, one canvas, and the panel rail for
the several-dashboards story. The page tabs that story made dead are
gone; PageDef/SectionDef stay in the schema and a page the editor does
not show round-trips untouched.

The row height derives from the canvas as the column width already did,
so the same arrangement is the same picture on a 7" panel as on a 4K
one. The uPlot rules move beside UplotChart, where a chart on Health is
styled without a dashboard having been visited first, and the bar's
readout travels on one property instead of jumping sides at 30%.
This commit is contained in:
2026-08-22 12:22:37 +02:00
parent 33b9443592
commit f696e19886
6 changed files with 216 additions and 162 deletions
@@ -31,15 +31,9 @@ export const CANVAS_PRESETS = [
{ label: "Full HD, portrait", width: 1080, height: 1920 },
]
/** One grid row, in pixels — the unit widget heights are multiples of. */
export const ROW_HEIGHT = 80
/** The gap between widgets, in pixels. Matches the `gap-3` view mode uses. */
export const GRID_GAP = 12
/** Distance between two row origins — what a widget's `y` is counted in. */
export const ROW_PITCH = ROW_HEIGHT + GRID_GAP
/**
* The generated client marks every list optional, because the server fills
* them in. These three keep that from spreading through the components.
@@ -56,9 +50,43 @@ export const canvasOf = (dashboard: Dashboard) => ({
height: dashboard.canvas_height || DEFAULT_CANVAS.height,
})
/** One grid row, in pixels, on the canvas the grid was first drawn for. */
const DEFAULT_ROW_HEIGHT = 80
/** A column's own width on that canvas, gaps taken off. */
const DEFAULT_COLUMN_WIDTH =
(DEFAULT_CANVAS.width + GRID_GAP) / DEFAULT_COLUMNS - GRID_GAP
/**
* How much wider a cell is than it is tall.
*
* A column's width already follows the canvas — it is the canvas cut into
* `columns` — while the row height was a fixed 80px, so the same arrangement
* came out as a different picture on every panel: cells taller than wide on a
* 7" one, twice as wide as tall on a 4K one. Holding the proportion instead is
* what makes the canvas worth scaling to fit. The number is the one the
* default canvas already had rather than a new one, so a panel that says
* nothing about its size keeps exactly the 80px rows it had.
*/
const CELL_ASPECT = DEFAULT_COLUMN_WIDTH / DEFAULT_ROW_HEIGHT
const columnWidthOf = (dashboard: Dashboard) =>
(canvasOf(dashboard).width + GRID_GAP) / columnsOf(dashboard) - GRID_GAP
/** One grid row, in pixels — the unit widget heights are multiples of. */
export const rowHeightOf = (dashboard: Dashboard) =>
Math.round(columnWidthOf(dashboard) / CELL_ASPECT)
/** Distance between two row origins — what a widget's `y` is counted in. */
export const rowPitchOf = (dashboard: Dashboard) =>
rowHeightOf(dashboard) + GRID_GAP
/** How many rows of the grid fit in the canvas; the rest is off the panel. */
export const rowsOf = (dashboard: Dashboard) =>
Math.max(1, Math.floor((canvasOf(dashboard).height + GRID_GAP) / ROW_PITCH))
Math.max(
1,
Math.floor((canvasOf(dashboard).height + GRID_GAP) / rowPitchOf(dashboard)),
)
/**
* The dashboard's own surface: exactly the panel's pixel size, scaled to fit
@@ -116,7 +144,7 @@ export function CanvasSurface({
// One dot per cell corner. The column pitch the grid snaps to is
// (width + gap) / columns; a row is one row plus the gap.
"--dot-x": `${(width + GRID_GAP) / columnsOf(dashboard)}px`,
"--dot-y": `${ROW_PITCH}px`,
"--dot-y": `${rowPitchOf(dashboard)}px`,
} as React.CSSProperties
}
>
@@ -164,79 +192,73 @@ export const isPlaced = (widgets: WidgetDef[]) =>
return x > 0 || y > 0
})
export function SectionGrid({
section,
dashboard,
columns = DEFAULT_COLUMNS,
renderWidget,
stacked,
className,
}: {
section: SectionDef_Output
/** Which dashboard this is, so an input widget can name itself. */
dashboard: string
columns?: number
renderWidget?: (widget: WidgetDef) => React.ReactNode
/** One column at the viewport's width, for a phone. */
stacked?: boolean
className?: string
}) {
const all = widgetsOf(section)
// Stacked, the arrangement becomes a reading order, so it follows the rows
// the panel shows rather than the order widgets happened to be added in.
const widgets = stacked
? [...all].sort((a, b) => {
const left = placement(a)
const right = placement(b)
return (left.y ?? 0) - (right.y ?? 0) || (left.x ?? 0) - (right.x ?? 0)
})
: all
return (
<section className="grid gap-3">
{section.title ? (
<h2 className="text-sm font-medium text-muted-foreground">
{section.title}
</h2>
) : null}
<div
className={cn("widget-grid", stacked && "widget-stacked", className)}
data-placed={isPlaced(widgets) || undefined}
style={{ "--widget-cols": columns } as React.CSSProperties}
>
{widgets.map((widget) => (
<div
key={widget.id}
style={widgetStyle(widget, columns)}
className="widget-cell"
>
{renderWidget ? (
renderWidget(widget)
) : (
<WidgetFrame title={widget.title} issue={widgetIssue(widget)}>
<WidgetBody widget={widget} dashboard={dashboard} />
</WidgetFrame>
)}
</div>
))}
</div>
</section>
)
/**
* The page's widgets as one arrangement.
*
* Sections used to be drawn as separate grids stacked down the page, each
* starting at its own row zero, so a document written with several of them is
* read as one grid by pushing each below the one before it. That is what lets
* the editor and the panel show the same thing: the editor arranges one grid,
* and it now arranges all of them.
*
* An unarranged document is left alone — every widget still sits at 0,0 there,
* so there is no arrangement to keep apart and the grid auto-flows instead.
*
* ponytail: a section's own heading stops being drawn with it. No UI ever
* wrote one, so only a seeded document has any — restoring them means giving
* a group a box on the canvas, which is a widget, not a section.
*/
export function flatWidgets(page: PageDef_Output): WidgetDef[] {
const sections = sectionsOf(page)
const all = sections.flatMap(widgetsOf)
if (sections.length < 2 || !isPlaced(all)) return all
const flat: WidgetDef[] = []
let offset = 0
for (const section of sections) {
let bottom = 0
for (const widget of widgetsOf(section)) {
const placed = placement(widget)
const y = Math.max(0, placed.y ?? 0)
bottom = Math.max(bottom, y + Math.max(1, placed.h ?? 2))
flat.push(
offset === 0
? widget
: {
...widget,
layout: {
...(widget.layout ?? {}),
lg: { ...placed, y: y + offset },
},
},
)
}
offset += bottom
}
return flat
}
/**
* One page, drawn as the single grid the panel shows.
*
* A dashboard is one canvas — several dashboards on a panel is what the rail
* is for — so the page's sections are one arrangement rather than a stack of
* headed grids. `SectionDef` stays in the schema, and the editor writes what
* it arranged back into the first section.
*/
export function DashboardView({
dashboard,
pageId,
renderWidget,
stacked,
}: {
dashboard: Dashboard
pageId?: string
renderWidget?: (widget: WidgetDef) => React.ReactNode
/** One column at the viewport's width, for a phone. */
stacked?: boolean
}) {
const pages = pagesOf(dashboard)
const page = pages.find((candidate) => candidate.id === pageId) ?? pages[0]
// ponytail: the first page only. A panel carries several whole dashboards
// — that is what the rail is for — so nothing writes a second page.
const page = pagesOf(dashboard)[0]
if (!page) {
return (
@@ -246,10 +268,8 @@ export function DashboardView({
)
}
const empty = sectionsOf(page).every(
(section) => widgetsOf(section).length === 0,
)
if (empty) {
const all = flatWidgets(page)
if (all.length === 0) {
return (
<p
className="text-sm text-muted-foreground"
@@ -260,17 +280,44 @@ export function DashboardView({
)
}
// Stacked, the arrangement becomes a reading order, so it follows the rows
// the panel shows rather than the order widgets happened to be added in.
const widgets = stacked
? [...all].sort((a, b) => {
const left = placement(a)
const right = placement(b)
return (left.y ?? 0) - (right.y ?? 0) || (left.x ?? 0) - (right.x ?? 0)
})
: all
const columns = columnsOf(dashboard)
return (
<div className="grid gap-6">
{sectionsOf(page).map((section) => (
<SectionGrid
key={section.id}
section={section}
dashboard={dashboard.name}
columns={columnsOf(dashboard)}
renderWidget={renderWidget}
stacked={stacked}
/>
<div
className={cn("widget-grid", stacked && "widget-stacked")}
data-placed={isPlaced(widgets) || undefined}
style={
{
"--widget-cols": columns,
// The row height follows the canvas, so the CSS grid and the
// editor's grid library cannot drift apart.
"--row-height": `${rowHeightOf(dashboard)}px`,
} as React.CSSProperties
}
>
{widgets.map((widget) => (
<div
key={widget.id}
style={widgetStyle(widget, columns)}
className="widget-cell"
>
{renderWidget ? (
renderWidget(widget)
) : (
<WidgetFrame title={widget.title} issue={widgetIssue(widget)}>
<WidgetBody widget={widget} dashboard={dashboard.name} />
</WidgetFrame>
)}
</div>
))}
</div>
)