From 0ec14b5db7368291403302348351bcd2c112277d Mon Sep 17 00:00:00 2001 From: SylvainDruaux Date: Tue, 20 Oct 2026 14:12:59 +0200 Subject: [PATCH] refactor(preview): remove the unused column count detection The column guide reads --site-columns through the menu's variable counts, so the frame's own detection of --site-column-count, --site-columns and --_column-count is no longer used. Remove it from frameGuide.ts, the guide message parser and GuideMetrics, with its tests. The frame no longer adds a .grid-autofit probe to the page on each measurement. --- electron/preload/frameGuide.ts | 29 ------------------- src/features/preview/guideModel.ts | 9 ------ src/features/preview/previewMessages.ts | 28 +----------------- test/renderer/preview/guideModel.test.js | 14 ++------- test/renderer/preview/previewMessages.test.js | 15 ---------- 5 files changed, 3 insertions(+), 92 deletions(-) diff --git a/electron/preload/frameGuide.ts b/electron/preload/frameGuide.ts index 7eef82b3..36009a89 100644 --- a/electron/preload/frameGuide.ts +++ b/electron/preload/frameGuide.ts @@ -24,13 +24,6 @@ const GUIDE_VARIABLES = { gutter: '--site-gutter', widthMax: '--max-width-main', } as const; -// A site-wide column count on :root, tried in this order. The app's parser -// accepts exactly these names (guideModel.ts). -const SITE_COLUMN_COUNT_VARIABLES = ['--site-column-count', '--site-columns'] as const; -// Lumos keeps its column count on its auto grid rather than on :root, so a -// page with no site-wide count is asked through an element of that class. -const GRID_CLASS = 'grid-autofit'; -const GRID_COLUMN_COUNT_VARIABLE = '--_column-count'; const COLUMN_COUNT_PATTERN = /^\d{1,3}$/; // The names the app may ask about: one per menu value (margin, gutter, maximum // width and column count), each a custom property name within the app's own @@ -143,7 +136,6 @@ function measureGuide(body: HTMLElement) { counts: Object.fromEntries( menuVariables.map((name) => [name, countOf(rootStyle.getPropertyValue(name))]), ), - ...columnCountOf(body, rootStyle), }; } @@ -166,27 +158,6 @@ function lengthOf( } } -// The site-wide count when the page declares one, else Lumos's grid count. -function columnCountOf(body: HTMLElement, rootStyle: CSSStyleDeclaration) { - for (const variable of SITE_COLUMN_COUNT_VARIABLES) { - const site = countOf(rootStyle.getPropertyValue(variable)); - if (site !== undefined) { - return { columnCount: site, columnVariable: variable }; - } - } - const probe = appendProbe(body, ''); - probe.className = GRID_CLASS; - try { - const grid = countOf(getComputedStyle(probe).getPropertyValue(GRID_COLUMN_COUNT_VARIABLE)); - if (grid !== undefined) { - return { columnCount: grid, columnVariable: GRID_COLUMN_COUNT_VARIABLE }; - } - return { columnCount: undefined, columnVariable: undefined }; - } finally { - probe.remove(); - } -} - // An invisible, zero-height element the canvas owns: its own writes never // start a page walk (mutationKind in frameReports.ts). function appendProbe(body: HTMLElement, style: string): HTMLElement { diff --git a/src/features/preview/guideModel.ts b/src/features/preview/guideModel.ts index f58af99b..7bcfa46f 100644 --- a/src/features/preview/guideModel.ts +++ b/src/features/preview/guideModel.ts @@ -39,14 +39,6 @@ export const GUIDE_COLUMN_VARIABLE_DEFAULT = '--site-columns'; /** The numbers are the column colour, this much more opaque than the column. */ export const GUIDE_NUMBER_OPACITY = 0.5; -/** The variables a project count is read from, in the order they are tried. */ -export const GUIDE_COLUMN_VARIABLES = [ - '--site-column-count', - '--site-columns', - '--_column-count', -] as const; -export type GuideColumnVariable = (typeof GUIDE_COLUMN_VARIABLES)[number]; - export type GuideLengthUnit = 'px' | 'rem'; export interface GuideLength { readonly amount: number; @@ -85,7 +77,6 @@ export interface GuideMetrics { readonly marginPx: number | undefined; readonly gutterPx: number | undefined; readonly widthMaxPx: number | undefined; - readonly columns: { readonly count: number; readonly variable: GuideColumnVariable } | undefined; /** The menu's own variables, by name; a name the page does not declare is absent. */ readonly variablesPx: Readonly>; /** The same names read as column counts; absent unless a whole number. */ diff --git a/src/features/preview/previewMessages.ts b/src/features/preview/previewMessages.ts index 2ecc82f3..580c2c3d 100644 --- a/src/features/preview/previewMessages.ts +++ b/src/features/preview/previewMessages.ts @@ -12,13 +12,7 @@ import { } from '../../../shared/core/boundary'; import type { Digest } from '../../../shared/core/brand'; import { parsePreviewRender, type PreviewRender } from '../../../shared/page/previewToken'; -import { - GUIDE_COLUMN_VARIABLES, - GUIDE_LIMITS, - parseGuideVariableName, - type GuideColumnVariable, - type GuideMetrics, -} from './guideModel'; +import { GUIDE_LIMITS, parseGuideVariableName, type GuideMetrics } from './guideModel'; export type PreviewMessage = | { @@ -209,7 +203,6 @@ function parseGuideMetrics(value: Readonly>): GuideMetri marginPx: optional(guideLength)(value['margin']), gutterPx: optional(guideLength)(value['gutter']), widthMaxPx: optional(guideLength)(value['widthMax']), - columns: parseGuideColumns(value['columnCount'], value['columnVariable']), variablesPx: Object.fromEntries( guideVariableEntries(value['variables']).map(([name, px]) => [ name, @@ -245,31 +238,12 @@ function guideLength(input: unknown): number { return value; } -// The count the page found and the variable it came from; undefined when the -// page declares none. -function parseGuideColumns(countInput: unknown, variableInput: unknown): GuideMetrics['columns'] { - if (countInput === undefined) { - return undefined; - } - const variable = parseGuideColumnVariable(variableInput); - const columnCount = declaredCount(countInput); - return columnCount === undefined ? undefined : { count: columnCount, variable }; -} - // A whole number as the page read it. Whether the guide can use it is the // model's to decide, so invalid variable values remain distinguishable from counts. function declaredCount(input: unknown): number | undefined { return optional(count)(input); } -function parseGuideColumnVariable(input: unknown): GuideColumnVariable { - const variable = GUIDE_COLUMN_VARIABLES.find((name) => name === input); - if (variable === undefined) { - throw new Error('Preview guide: unknown column variable'); - } - return variable; -} - function parseReloadReason(input: unknown): PreviewReloadReason { const found = PREVIEW_RELOAD_REASONS.find((reason) => reason === input); if (found === undefined) { diff --git a/test/renderer/preview/guideModel.test.js b/test/renderer/preview/guideModel.test.js index a856963a..e13ca82a 100644 --- a/test/renderer/preview/guideModel.test.js +++ b/test/renderer/preview/guideModel.test.js @@ -33,7 +33,6 @@ const LUMOS_WIDE = { marginPx: 48, gutterPx: 32, widthMaxPx: 1440, - columns: { count: 12, variable: '--_column-count' }, variablesPx: {}, variableCounts: {}, }; @@ -43,7 +42,6 @@ const NOTHING_DECLARED = { marginPx: undefined, gutterPx: undefined, widthMaxPx: undefined, - columns: undefined, variablesPx: {}, variableCounts: {}, }; @@ -167,11 +165,7 @@ test('automatic columns follow site-columns as it changes and show the binding', test('automatic columns fall back to 12 for missing or unusable site-columns', () => { assert.equal(guideColumnText(GUIDE_DEFAULTS.columns), '12'); for (const count of [undefined, 0, 3, 25, 4.5, Number.NaN, Infinity]) { - const metrics = { - ...LUMOS_WIDE, - columns: { count: 8, variable: '--site-column-count' }, - variableCounts: { '--site-columns': count }, - }; + const metrics = { ...LUMOS_WIDE, variableCounts: { '--site-columns': count } }; const guide = resolveGuide(metrics, GUIDE_DEFAULTS); assert.equal(guide.columnCount, 12); assert.equal(guide.columnSource, 'stacki'); @@ -194,11 +188,7 @@ test('a connected column variable follows the page and updates with its value', test('unusable column variables fall back to 12, never another project variable', () => { const settings = { ...GUIDE_DEFAULTS, columns: variable('--cols') }; for (const count of [undefined, 0, 3, 25, 4.5, Number.NaN, Infinity]) { - const metrics = { - ...LUMOS_WIDE, - columns: { count: 8, variable: '--site-columns' }, - variableCounts: { '--cols': count }, - }; + const metrics = { ...LUMOS_WIDE, variableCounts: { '--cols': count, '--site-columns': 8 } }; assert.equal(resolveGuide(metrics, settings).columnCount, 12); assert.equal(resolveGuide(metrics, settings).columnSource, 'stacki'); } diff --git a/test/renderer/preview/previewMessages.test.js b/test/renderer/preview/previewMessages.test.js index 9bebb17e..54383492 100644 --- a/test/renderer/preview/previewMessages.test.js +++ b/test/renderer/preview/previewMessages.test.js @@ -183,8 +183,6 @@ test('guide measurements parse, absent values stay absent, and bounds hold', () margin: 48, gutter: 32, widthMax: undefined, - columnCount: 12, - columnVariable: '--_column-count', variables: { '--pad': 40, '--missing': undefined }, counts: { '--cols': 10, '--pad': undefined }, }; @@ -196,7 +194,6 @@ test('guide measurements parse, absent values stay absent, and bounds hold', () marginPx: 48, gutterPx: 32, widthMaxPx: undefined, - columns: { count: 12, variable: '--_column-count' }, variablesPx: { '--pad': 40, '--missing': undefined }, variableCounts: { '--cols': 10, '--pad': undefined }, }, @@ -205,17 +202,7 @@ test('guide measurements parse, absent values stay absent, and bounds hold', () for (const count of [0, 49, 140, 999]) { const counts = { '--cols': count }; assert.equal(parsePreviewMessage({ ...guide, counts }).metrics.variableCounts['--cols'], count); - const site = { ...guide, columnCount: count }; - assert.equal(parsePreviewMessage(site).metrics.columns.count, count); } - for (const variable of ['--site-column-count', '--site-columns', '--_column-count']) { - const site = { ...guide, columnCount: 10, columnVariable: variable }; - assert.deepEqual(parsePreviewMessage(site).metrics.columns, { count: 10, variable }); - } - assert.equal( - parsePreviewMessage({ ...guide, columnCount: undefined }).metrics.columns, - undefined, - ); for (const bad of [ { margin: -1 }, { gutter: Number.NaN }, @@ -223,8 +210,6 @@ test('guide measurements parse, absent values stay absent, and bounds hold', () { viewportWidth: '1440' }, { rootFontSize: 0 }, { rootFontSize: 1_001 }, - { columnCount: 2.5 }, - { columnVariable: '--anything' }, { margin: PLATFORM_NULL }, { variables: undefined }, { variables: { '--pad': -1 } },