@nullplatform/ui-plugin-sdk 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/contract/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { PROTOCOL_VERSION, validateManifest, validatePublishableManifest, unnamedContributions, contributionId, RESERVED_PLUGIN_ID, isSectionSlot, ManifestError, type PluginManifest, type Contribution, type ApiPermission, type HttpPermission, type HttpMethod, } from './manifest.js';
|
|
2
2
|
export { PLACE_ROOTS, missingContext, contextSatisfies, readPlaceRoots, type PlaceRoot } from './context.js';
|
|
3
3
|
export { pageOf, layoutConflicts, type LayoutConflict } from './layout.js';
|
|
4
|
+
export { PAGE_AREAS, REQUIRED_PAGE_AREA, MIN_PAGE_AREA_WIDTH, pageTemplateProblems, isPageTemplateDefinition, type PageArea, type PageTemplateBreakpoint, type PageTemplateDefinition, type PageTemplateResponsive, type PageTemplateTracks, } from './pageTemplate.js';
|
|
4
5
|
export { isApiRequestAllowed, isHttpRequestAllowed, isRequestAllowed, normalizePath, canonicalizePath } from './allowlist.js';
|
|
5
6
|
export { matchFixture, type Fixtures, type FixtureMatch } from './fixtures.js';
|
|
6
7
|
export { CONTEXT_ROOTS, extractRoots, validateWhen, evaluateWhen, rolloutIncludes, WhenError, type WhenQuery, type ContextRoot, type Rollout, } from './when.js';
|
package/dist/contract/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { PROTOCOL_VERSION, validateManifest, validatePublishableManifest, unnamedContributions, contributionId, RESERVED_PLUGIN_ID, isSectionSlot, ManifestError, } from './manifest.js';
|
|
2
2
|
export { PLACE_ROOTS, missingContext, contextSatisfies, readPlaceRoots } from './context.js';
|
|
3
3
|
export { pageOf, layoutConflicts } from './layout.js';
|
|
4
|
+
export { PAGE_AREAS, REQUIRED_PAGE_AREA, MIN_PAGE_AREA_WIDTH, pageTemplateProblems, isPageTemplateDefinition, } from './pageTemplate.js';
|
|
4
5
|
export { isApiRequestAllowed, isHttpRequestAllowed, isRequestAllowed, normalizePath, canonicalizePath } from './allowlist.js';
|
|
5
6
|
export { matchFixture } from './fixtures.js';
|
|
6
7
|
export { CONTEXT_ROOTS, extractRoots, validateWhen, evaluateWhen, rolloutIncludes, WhenError, } from './when.js';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page templates an organization saves (proposal P-1): a grid of named areas, in the grammar of `GridLayout` of
|
|
3
|
+
* react-ui-components (`columns` and `areas`, each a value or one per breakpoint). Each area of a page is a region
|
|
4
|
+
* named `<page>.<area>`, so the areas a template may name are the regions a page can have. Shared by the API, which
|
|
5
|
+
* refuses an invalid definition at write, and the host, which drops one it reads.
|
|
6
|
+
*/
|
|
7
|
+
/** The areas of a page, each the last segment of a region's name (`application.overview.sidebar`). */
|
|
8
|
+
export declare const PAGE_AREAS: readonly ["top", "main", "sidebar", "bottom"];
|
|
9
|
+
export type PageArea = (typeof PAGE_AREAS)[number];
|
|
10
|
+
/** The area every template has: the page's blocks need somewhere to go. */
|
|
11
|
+
export declare const REQUIRED_PAGE_AREA: PageArea;
|
|
12
|
+
/**
|
|
13
|
+
* The narrowest an area of a page is drawn, in px. An editor that resizes a page's columns stacks the side column below
|
|
14
|
+
* `main` rather than draw either one under it, so a plugin can rely on at least this width wherever it lands.
|
|
15
|
+
*/
|
|
16
|
+
export declare const MIN_PAGE_AREA_WIDTH = 240;
|
|
17
|
+
declare const BREAKPOINTS: readonly ["xs", "sm", "md", "lg", "xl"];
|
|
18
|
+
export type PageTemplateBreakpoint = (typeof BREAKPOINTS)[number];
|
|
19
|
+
/** A value, or one per breakpoint (`{ xs: 1, lg: 12 }`): a value holds from its breakpoint up. */
|
|
20
|
+
export type PageTemplateResponsive<T> = T | Partial<Record<PageTemplateBreakpoint, T>>;
|
|
21
|
+
/** A column count (1 to 12, equal columns), or a track list (`['2fr', '1fr']`). */
|
|
22
|
+
export type PageTemplateTracks = number | readonly string[];
|
|
23
|
+
/** A saved template's definition: `GridLayout`'s `columns` and `areas`, and nothing else. */
|
|
24
|
+
export interface PageTemplateDefinition {
|
|
25
|
+
columns?: PageTemplateResponsive<PageTemplateTracks>;
|
|
26
|
+
areas: PageTemplateResponsive<readonly string[]>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* What makes a template definition invalid, or nothing. GridLayout's own checks (shapes, one cell per column in
|
|
30
|
+
* effect, rectangles, every area at every breakpoint from `xs` up), plus the page's: only the page's areas, `main`
|
|
31
|
+
* among them, and no option but `columns` and `areas`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function pageTemplateProblems(definition: unknown): string[];
|
|
34
|
+
/** Whether a definition is valid: what a host checks before drawing a saved template. */
|
|
35
|
+
export declare function isPageTemplateDefinition(definition: unknown): definition is PageTemplateDefinition;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page templates an organization saves (proposal P-1): a grid of named areas, in the grammar of `GridLayout` of
|
|
3
|
+
* react-ui-components (`columns` and `areas`, each a value or one per breakpoint). Each area of a page is a region
|
|
4
|
+
* named `<page>.<area>`, so the areas a template may name are the regions a page can have. Shared by the API, which
|
|
5
|
+
* refuses an invalid definition at write, and the host, which drops one it reads.
|
|
6
|
+
*/
|
|
7
|
+
/** The areas of a page, each the last segment of a region's name (`application.overview.sidebar`). */
|
|
8
|
+
export const PAGE_AREAS = ['top', 'main', 'sidebar', 'bottom'];
|
|
9
|
+
/** The area every template has: the page's blocks need somewhere to go. */
|
|
10
|
+
export const REQUIRED_PAGE_AREA = 'main';
|
|
11
|
+
/**
|
|
12
|
+
* The narrowest an area of a page is drawn, in px. An editor that resizes a page's columns stacks the side column below
|
|
13
|
+
* `main` rather than draw either one under it, so a plugin can rely on at least this width wherever it lands.
|
|
14
|
+
*/
|
|
15
|
+
export const MIN_PAGE_AREA_WIDTH = 240;
|
|
16
|
+
const BREAKPOINTS = ['xs', 'sm', 'md', 'lg', 'xl'];
|
|
17
|
+
/** GridLayout's limits (sdui-spec): 12 columns, tracks and rows at most; 12 equal columns where none are set. */
|
|
18
|
+
const MAX = 12;
|
|
19
|
+
const DEFAULT_COLUMNS = 12;
|
|
20
|
+
/** 12 cells of a name of up to 20 characters: far over the page's areas, and short enough to check cheaply. */
|
|
21
|
+
const MAX_ROW_LENGTH = 12 * 21;
|
|
22
|
+
// sdui-spec's `gridTrack` and `gridAreasRow`, verbatim.
|
|
23
|
+
const TRACK_PATTERN = /^(?:\d+(?:\.\d+)?(?:fr|px|%|rem)|auto|min-content|max-content)$/;
|
|
24
|
+
const AREAS_ROW_PATTERN = /^(?:[a-z][a-z0-9_-]*|\.)(?: (?:[a-z][a-z0-9_-]*|\.))*$/;
|
|
25
|
+
const EMPTY_CELL = '.';
|
|
26
|
+
const OPTIONS = ['columns', 'areas'];
|
|
27
|
+
const isPlainObject = (value) => typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype;
|
|
28
|
+
const isTracks = (value) => (Number.isInteger(value) && value >= 1 && value <= MAX) ||
|
|
29
|
+
(Array.isArray(value) &&
|
|
30
|
+
value.length >= 1 &&
|
|
31
|
+
value.length <= MAX &&
|
|
32
|
+
value.every((track) => typeof track === 'string' && TRACK_PATTERN.test(track)));
|
|
33
|
+
const isRows = (value) => Array.isArray(value) &&
|
|
34
|
+
value.length >= 1 &&
|
|
35
|
+
value.length <= MAX &&
|
|
36
|
+
value.every((row) => typeof row === 'string' && row.length <= MAX_ROW_LENGTH && AREAS_ROW_PATTERN.test(row));
|
|
37
|
+
/** A responsive value as one entry per breakpoint it names; `undefined` when it is not one. */
|
|
38
|
+
function perBreakpoint(value, isValue) {
|
|
39
|
+
if (isValue(value))
|
|
40
|
+
return { xs: value };
|
|
41
|
+
if (!isPlainObject(value))
|
|
42
|
+
return undefined;
|
|
43
|
+
const valid = Object.entries(value).every(([breakpoint, entry]) => BREAKPOINTS.includes(breakpoint) && isValue(entry));
|
|
44
|
+
return valid ? value : undefined;
|
|
45
|
+
}
|
|
46
|
+
const namesIn = (rows) => rows.flatMap((row) => row.split(' ')).filter((name) => name !== EMPTY_CELL);
|
|
47
|
+
const columnCount = (tracks) => (typeof tracks === 'number' ? tracks : tracks.length);
|
|
48
|
+
/**
|
|
49
|
+
* What a template of rows breaks at one breakpoint: a row with the wrong cell count, an area that is no rectangle,
|
|
50
|
+
* or one another breakpoint names and this one leaves out (its region would land outside the grid).
|
|
51
|
+
*/
|
|
52
|
+
function rowProblems(rows, columns, breakpoint, allNames) {
|
|
53
|
+
// A row with the wrong cell count is the problem to fix first: the shapes are read only when every row fits.
|
|
54
|
+
const misfit = rows.map((row) => [row, row.split(' ').length]).filter(([, cells]) => cells !== columns);
|
|
55
|
+
if (misfit.length)
|
|
56
|
+
return misfit.map(([row, cells]) => `areas at ${breakpoint}: the row "${row}" has ${cells} cells for ${columns} columns`);
|
|
57
|
+
const problems = [];
|
|
58
|
+
const cells = new Map();
|
|
59
|
+
rows.forEach((row, rowIndex) => {
|
|
60
|
+
row.split(' ').forEach((name, column) => {
|
|
61
|
+
if (name === EMPTY_CELL)
|
|
62
|
+
return;
|
|
63
|
+
if (!cells.has(name))
|
|
64
|
+
cells.set(name, []);
|
|
65
|
+
cells.get(name).push([rowIndex, column]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
for (const [name, at] of cells) {
|
|
69
|
+
const rowsOf = at.map(([row]) => row);
|
|
70
|
+
const columnsOf = at.map(([, column]) => column);
|
|
71
|
+
const height = Math.max(...rowsOf) - Math.min(...rowsOf) + 1;
|
|
72
|
+
const width = Math.max(...columnsOf) - Math.min(...columnsOf) + 1;
|
|
73
|
+
if (height * width !== at.length)
|
|
74
|
+
problems.push(`areas at ${breakpoint}: "${name}" is not a rectangle`);
|
|
75
|
+
}
|
|
76
|
+
for (const name of allNames)
|
|
77
|
+
if (!cells.has(name))
|
|
78
|
+
problems.push(`areas at ${breakpoint}: "${name}" is missing, though another breakpoint has it`);
|
|
79
|
+
return problems;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* What makes a template definition invalid, or nothing. GridLayout's own checks (shapes, one cell per column in
|
|
83
|
+
* effect, rectangles, every area at every breakpoint from `xs` up), plus the page's: only the page's areas, `main`
|
|
84
|
+
* among them, and no option but `columns` and `areas`.
|
|
85
|
+
*/
|
|
86
|
+
export function pageTemplateProblems(definition) {
|
|
87
|
+
if (!isPlainObject(definition))
|
|
88
|
+
return ['a template definition is a plain object'];
|
|
89
|
+
const problems = Object.keys(definition)
|
|
90
|
+
.filter((name) => !OPTIONS.includes(name))
|
|
91
|
+
.map((name) => `"${name}" is no template option: a template has columns and areas`);
|
|
92
|
+
const columns = definition.columns === undefined ? {} : perBreakpoint(definition.columns, isTracks);
|
|
93
|
+
if (!columns)
|
|
94
|
+
problems.push(`columns is a count (1 to ${MAX}) or 1 to ${MAX} tracks, or one per breakpoint`);
|
|
95
|
+
const areas = perBreakpoint(definition.areas, isRows);
|
|
96
|
+
if (!areas)
|
|
97
|
+
problems.push(`areas is 1 to ${MAX} rows of area names, or one per breakpoint`);
|
|
98
|
+
if (!columns || !areas)
|
|
99
|
+
return problems;
|
|
100
|
+
const allNames = new Set(namesIn(BREAKPOINTS.flatMap((breakpoint) => areas[breakpoint] ?? [])));
|
|
101
|
+
const unknown = [...allNames].filter((name) => !PAGE_AREAS.includes(name));
|
|
102
|
+
if (unknown.length)
|
|
103
|
+
problems.push(`areas name ${unknown.map((name) => `"${name}"`).join(', ')}: a page's areas are ${PAGE_AREAS.join(', ')}`);
|
|
104
|
+
if (!allNames.has(REQUIRED_PAGE_AREA))
|
|
105
|
+
problems.push(`areas have no "${REQUIRED_PAGE_AREA}": every template has it`);
|
|
106
|
+
if (areas.xs === undefined)
|
|
107
|
+
problems.push('areas start above xs: below their first breakpoint nothing places the areas');
|
|
108
|
+
// Each in effect from its breakpoint up: a row is held to the columns in effect where either changes.
|
|
109
|
+
let count = DEFAULT_COLUMNS;
|
|
110
|
+
let rows;
|
|
111
|
+
for (const breakpoint of BREAKPOINTS) {
|
|
112
|
+
const tracks = columns[breakpoint];
|
|
113
|
+
if (tracks !== undefined)
|
|
114
|
+
count = columnCount(tracks);
|
|
115
|
+
if (areas[breakpoint] !== undefined)
|
|
116
|
+
rows = areas[breakpoint];
|
|
117
|
+
if (rows && (tracks !== undefined || areas[breakpoint] !== undefined))
|
|
118
|
+
problems.push(...rowProblems(rows, count, breakpoint, allNames));
|
|
119
|
+
}
|
|
120
|
+
return problems;
|
|
121
|
+
}
|
|
122
|
+
/** Whether a definition is valid: what a host checks before drawing a saved template. */
|
|
123
|
+
export function isPageTemplateDefinition(definition) {
|
|
124
|
+
return pageTemplateProblems(definition).length === 0;
|
|
125
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assertTopicValue = exports.redactBySchema = exports.projectFields = exports.canProvideTopic = exports.consumeGrant = exports.isFieldPath = exports.topicOwner = exports.isHostTopic = exports.isTopicName = exports.TOPIC_VALUE_MAX_CHARS = exports.resolveAssetUrl = exports.validateStorageUrl = exports.PLUGIN_STORAGE_ORIGIN = exports.WhenError = exports.rolloutIncludes = exports.evaluateWhen = exports.validateWhen = exports.extractRoots = exports.CONTEXT_ROOTS = exports.matchFixture = exports.canonicalizePath = exports.normalizePath = exports.isRequestAllowed = exports.isHttpRequestAllowed = exports.isApiRequestAllowed = exports.layoutConflicts = exports.pageOf = exports.readPlaceRoots = exports.contextSatisfies = exports.missingContext = exports.PLACE_ROOTS = exports.ManifestError = exports.isSectionSlot = exports.RESERVED_PLUGIN_ID = exports.contributionId = exports.unnamedContributions = exports.validatePublishableManifest = exports.validateManifest = exports.PROTOCOL_VERSION = void 0;
|
|
3
|
+
exports.assertTopicValue = exports.redactBySchema = exports.projectFields = exports.canProvideTopic = exports.consumeGrant = exports.isFieldPath = exports.topicOwner = exports.isHostTopic = exports.isTopicName = exports.TOPIC_VALUE_MAX_CHARS = exports.resolveAssetUrl = exports.validateStorageUrl = exports.PLUGIN_STORAGE_ORIGIN = exports.WhenError = exports.rolloutIncludes = exports.evaluateWhen = exports.validateWhen = exports.extractRoots = exports.CONTEXT_ROOTS = exports.matchFixture = exports.canonicalizePath = exports.normalizePath = exports.isRequestAllowed = exports.isHttpRequestAllowed = exports.isApiRequestAllowed = exports.isPageTemplateDefinition = exports.pageTemplateProblems = exports.MIN_PAGE_AREA_WIDTH = exports.REQUIRED_PAGE_AREA = exports.PAGE_AREAS = exports.layoutConflicts = exports.pageOf = exports.readPlaceRoots = exports.contextSatisfies = exports.missingContext = exports.PLACE_ROOTS = exports.ManifestError = exports.isSectionSlot = exports.RESERVED_PLUGIN_ID = exports.contributionId = exports.unnamedContributions = exports.validatePublishableManifest = exports.validateManifest = exports.PROTOCOL_VERSION = void 0;
|
|
4
4
|
var manifest_1 = require("./manifest");
|
|
5
5
|
Object.defineProperty(exports, "PROTOCOL_VERSION", { enumerable: true, get: function () { return manifest_1.PROTOCOL_VERSION; } });
|
|
6
6
|
Object.defineProperty(exports, "validateManifest", { enumerable: true, get: function () { return manifest_1.validateManifest; } });
|
|
@@ -18,6 +18,12 @@ Object.defineProperty(exports, "readPlaceRoots", { enumerable: true, get: functi
|
|
|
18
18
|
var layout_1 = require("./layout");
|
|
19
19
|
Object.defineProperty(exports, "pageOf", { enumerable: true, get: function () { return layout_1.pageOf; } });
|
|
20
20
|
Object.defineProperty(exports, "layoutConflicts", { enumerable: true, get: function () { return layout_1.layoutConflicts; } });
|
|
21
|
+
var pageTemplate_1 = require("./pageTemplate");
|
|
22
|
+
Object.defineProperty(exports, "PAGE_AREAS", { enumerable: true, get: function () { return pageTemplate_1.PAGE_AREAS; } });
|
|
23
|
+
Object.defineProperty(exports, "REQUIRED_PAGE_AREA", { enumerable: true, get: function () { return pageTemplate_1.REQUIRED_PAGE_AREA; } });
|
|
24
|
+
Object.defineProperty(exports, "MIN_PAGE_AREA_WIDTH", { enumerable: true, get: function () { return pageTemplate_1.MIN_PAGE_AREA_WIDTH; } });
|
|
25
|
+
Object.defineProperty(exports, "pageTemplateProblems", { enumerable: true, get: function () { return pageTemplate_1.pageTemplateProblems; } });
|
|
26
|
+
Object.defineProperty(exports, "isPageTemplateDefinition", { enumerable: true, get: function () { return pageTemplate_1.isPageTemplateDefinition; } });
|
|
21
27
|
var allowlist_1 = require("./allowlist");
|
|
22
28
|
Object.defineProperty(exports, "isApiRequestAllowed", { enumerable: true, get: function () { return allowlist_1.isApiRequestAllowed; } });
|
|
23
29
|
Object.defineProperty(exports, "isHttpRequestAllowed", { enumerable: true, get: function () { return allowlist_1.isHttpRequestAllowed; } });
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Page templates an organization saves (proposal P-1): a grid of named areas, in the grammar of `GridLayout` of
|
|
4
|
+
* react-ui-components (`columns` and `areas`, each a value or one per breakpoint). Each area of a page is a region
|
|
5
|
+
* named `<page>.<area>`, so the areas a template may name are the regions a page can have. Shared by the API, which
|
|
6
|
+
* refuses an invalid definition at write, and the host, which drops one it reads.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.MIN_PAGE_AREA_WIDTH = exports.REQUIRED_PAGE_AREA = exports.PAGE_AREAS = void 0;
|
|
10
|
+
exports.pageTemplateProblems = pageTemplateProblems;
|
|
11
|
+
exports.isPageTemplateDefinition = isPageTemplateDefinition;
|
|
12
|
+
/** The areas of a page, each the last segment of a region's name (`application.overview.sidebar`). */
|
|
13
|
+
exports.PAGE_AREAS = ['top', 'main', 'sidebar', 'bottom'];
|
|
14
|
+
/** The area every template has: the page's blocks need somewhere to go. */
|
|
15
|
+
exports.REQUIRED_PAGE_AREA = 'main';
|
|
16
|
+
/**
|
|
17
|
+
* The narrowest an area of a page is drawn, in px. An editor that resizes a page's columns stacks the side column below
|
|
18
|
+
* `main` rather than draw either one under it, so a plugin can rely on at least this width wherever it lands.
|
|
19
|
+
*/
|
|
20
|
+
exports.MIN_PAGE_AREA_WIDTH = 240;
|
|
21
|
+
const BREAKPOINTS = ['xs', 'sm', 'md', 'lg', 'xl'];
|
|
22
|
+
/** GridLayout's limits (sdui-spec): 12 columns, tracks and rows at most; 12 equal columns where none are set. */
|
|
23
|
+
const MAX = 12;
|
|
24
|
+
const DEFAULT_COLUMNS = 12;
|
|
25
|
+
/** 12 cells of a name of up to 20 characters: far over the page's areas, and short enough to check cheaply. */
|
|
26
|
+
const MAX_ROW_LENGTH = 12 * 21;
|
|
27
|
+
// sdui-spec's `gridTrack` and `gridAreasRow`, verbatim.
|
|
28
|
+
const TRACK_PATTERN = /^(?:\d+(?:\.\d+)?(?:fr|px|%|rem)|auto|min-content|max-content)$/;
|
|
29
|
+
const AREAS_ROW_PATTERN = /^(?:[a-z][a-z0-9_-]*|\.)(?: (?:[a-z][a-z0-9_-]*|\.))*$/;
|
|
30
|
+
const EMPTY_CELL = '.';
|
|
31
|
+
const OPTIONS = ['columns', 'areas'];
|
|
32
|
+
const isPlainObject = (value) => typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype;
|
|
33
|
+
const isTracks = (value) => (Number.isInteger(value) && value >= 1 && value <= MAX) ||
|
|
34
|
+
(Array.isArray(value) &&
|
|
35
|
+
value.length >= 1 &&
|
|
36
|
+
value.length <= MAX &&
|
|
37
|
+
value.every((track) => typeof track === 'string' && TRACK_PATTERN.test(track)));
|
|
38
|
+
const isRows = (value) => Array.isArray(value) &&
|
|
39
|
+
value.length >= 1 &&
|
|
40
|
+
value.length <= MAX &&
|
|
41
|
+
value.every((row) => typeof row === 'string' && row.length <= MAX_ROW_LENGTH && AREAS_ROW_PATTERN.test(row));
|
|
42
|
+
/** A responsive value as one entry per breakpoint it names; `undefined` when it is not one. */
|
|
43
|
+
function perBreakpoint(value, isValue) {
|
|
44
|
+
if (isValue(value))
|
|
45
|
+
return { xs: value };
|
|
46
|
+
if (!isPlainObject(value))
|
|
47
|
+
return undefined;
|
|
48
|
+
const valid = Object.entries(value).every(([breakpoint, entry]) => BREAKPOINTS.includes(breakpoint) && isValue(entry));
|
|
49
|
+
return valid ? value : undefined;
|
|
50
|
+
}
|
|
51
|
+
const namesIn = (rows) => rows.flatMap((row) => row.split(' ')).filter((name) => name !== EMPTY_CELL);
|
|
52
|
+
const columnCount = (tracks) => (typeof tracks === 'number' ? tracks : tracks.length);
|
|
53
|
+
/**
|
|
54
|
+
* What a template of rows breaks at one breakpoint: a row with the wrong cell count, an area that is no rectangle,
|
|
55
|
+
* or one another breakpoint names and this one leaves out (its region would land outside the grid).
|
|
56
|
+
*/
|
|
57
|
+
function rowProblems(rows, columns, breakpoint, allNames) {
|
|
58
|
+
// A row with the wrong cell count is the problem to fix first: the shapes are read only when every row fits.
|
|
59
|
+
const misfit = rows.map((row) => [row, row.split(' ').length]).filter(([, cells]) => cells !== columns);
|
|
60
|
+
if (misfit.length)
|
|
61
|
+
return misfit.map(([row, cells]) => `areas at ${breakpoint}: the row "${row}" has ${cells} cells for ${columns} columns`);
|
|
62
|
+
const problems = [];
|
|
63
|
+
const cells = new Map();
|
|
64
|
+
rows.forEach((row, rowIndex) => {
|
|
65
|
+
row.split(' ').forEach((name, column) => {
|
|
66
|
+
if (name === EMPTY_CELL)
|
|
67
|
+
return;
|
|
68
|
+
if (!cells.has(name))
|
|
69
|
+
cells.set(name, []);
|
|
70
|
+
cells.get(name).push([rowIndex, column]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
for (const [name, at] of cells) {
|
|
74
|
+
const rowsOf = at.map(([row]) => row);
|
|
75
|
+
const columnsOf = at.map(([, column]) => column);
|
|
76
|
+
const height = Math.max(...rowsOf) - Math.min(...rowsOf) + 1;
|
|
77
|
+
const width = Math.max(...columnsOf) - Math.min(...columnsOf) + 1;
|
|
78
|
+
if (height * width !== at.length)
|
|
79
|
+
problems.push(`areas at ${breakpoint}: "${name}" is not a rectangle`);
|
|
80
|
+
}
|
|
81
|
+
for (const name of allNames)
|
|
82
|
+
if (!cells.has(name))
|
|
83
|
+
problems.push(`areas at ${breakpoint}: "${name}" is missing, though another breakpoint has it`);
|
|
84
|
+
return problems;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* What makes a template definition invalid, or nothing. GridLayout's own checks (shapes, one cell per column in
|
|
88
|
+
* effect, rectangles, every area at every breakpoint from `xs` up), plus the page's: only the page's areas, `main`
|
|
89
|
+
* among them, and no option but `columns` and `areas`.
|
|
90
|
+
*/
|
|
91
|
+
function pageTemplateProblems(definition) {
|
|
92
|
+
if (!isPlainObject(definition))
|
|
93
|
+
return ['a template definition is a plain object'];
|
|
94
|
+
const problems = Object.keys(definition)
|
|
95
|
+
.filter((name) => !OPTIONS.includes(name))
|
|
96
|
+
.map((name) => `"${name}" is no template option: a template has columns and areas`);
|
|
97
|
+
const columns = definition.columns === undefined ? {} : perBreakpoint(definition.columns, isTracks);
|
|
98
|
+
if (!columns)
|
|
99
|
+
problems.push(`columns is a count (1 to ${MAX}) or 1 to ${MAX} tracks, or one per breakpoint`);
|
|
100
|
+
const areas = perBreakpoint(definition.areas, isRows);
|
|
101
|
+
if (!areas)
|
|
102
|
+
problems.push(`areas is 1 to ${MAX} rows of area names, or one per breakpoint`);
|
|
103
|
+
if (!columns || !areas)
|
|
104
|
+
return problems;
|
|
105
|
+
const allNames = new Set(namesIn(BREAKPOINTS.flatMap((breakpoint) => areas[breakpoint] ?? [])));
|
|
106
|
+
const unknown = [...allNames].filter((name) => !exports.PAGE_AREAS.includes(name));
|
|
107
|
+
if (unknown.length)
|
|
108
|
+
problems.push(`areas name ${unknown.map((name) => `"${name}"`).join(', ')}: a page's areas are ${exports.PAGE_AREAS.join(', ')}`);
|
|
109
|
+
if (!allNames.has(exports.REQUIRED_PAGE_AREA))
|
|
110
|
+
problems.push(`areas have no "${exports.REQUIRED_PAGE_AREA}": every template has it`);
|
|
111
|
+
if (areas.xs === undefined)
|
|
112
|
+
problems.push('areas start above xs: below their first breakpoint nothing places the areas');
|
|
113
|
+
// Each in effect from its breakpoint up: a row is held to the columns in effect where either changes.
|
|
114
|
+
let count = DEFAULT_COLUMNS;
|
|
115
|
+
let rows;
|
|
116
|
+
for (const breakpoint of BREAKPOINTS) {
|
|
117
|
+
const tracks = columns[breakpoint];
|
|
118
|
+
if (tracks !== undefined)
|
|
119
|
+
count = columnCount(tracks);
|
|
120
|
+
if (areas[breakpoint] !== undefined)
|
|
121
|
+
rows = areas[breakpoint];
|
|
122
|
+
if (rows && (tracks !== undefined || areas[breakpoint] !== undefined))
|
|
123
|
+
problems.push(...rowProblems(rows, count, breakpoint, allNames));
|
|
124
|
+
}
|
|
125
|
+
return problems;
|
|
126
|
+
}
|
|
127
|
+
/** Whether a definition is valid: what a host checks before drawing a saved template. */
|
|
128
|
+
function isPageTemplateDefinition(definition) {
|
|
129
|
+
return pageTemplateProblems(definition).length === 0;
|
|
130
|
+
}
|