Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions electron/preload/frameGuide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,7 +136,6 @@ function measureGuide(body: HTMLElement) {
counts: Object.fromEntries(
menuVariables.map((name) => [name, countOf(rootStyle.getPropertyValue(name))]),
),
...columnCountOf(body, rootStyle),
};
}

Expand All @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions src/features/preview/guideModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Record<string, number | undefined>>;
/** The same names read as column counts; absent unless a whole number. */
Expand Down
28 changes: 1 addition & 27 deletions src/features/preview/previewMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
| {
Expand Down Expand Up @@ -209,7 +203,6 @@ function parseGuideMetrics(value: Readonly<Record<string, unknown>>): 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,
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 2 additions & 12 deletions test/renderer/preview/guideModel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const LUMOS_WIDE = {
marginPx: 48,
gutterPx: 32,
widthMaxPx: 1440,
columns: { count: 12, variable: '--_column-count' },
variablesPx: {},
variableCounts: {},
};
Expand All @@ -43,7 +42,6 @@ const NOTHING_DECLARED = {
marginPx: undefined,
gutterPx: undefined,
widthMaxPx: undefined,
columns: undefined,
variablesPx: {},
variableCounts: {},
};
Expand Down Expand Up @@ -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');
Expand All @@ -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');
}
Expand Down
15 changes: 0 additions & 15 deletions test/renderer/preview/previewMessages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
};
Expand All @@ -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 },
},
Expand All @@ -205,26 +202,14 @@ 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 },
{ widthMax: 100_001 },
{ viewportWidth: '1440' },
{ rootFontSize: 0 },
{ rootFontSize: 1_001 },
{ columnCount: 2.5 },
{ columnVariable: '--anything' },
{ margin: PLATFORM_NULL },
{ variables: undefined },
{ variables: { '--pad': -1 } },
Expand Down
Loading