@astryxdesign/cli 0.7.14-canary.gfdb9213 → 0.7.14
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/CHANGELOG.md +197 -0
- package/api/theme/_adapter.d.mts +33 -0
- package/api/theme/_adapter.mjs +352 -16
- package/api/theme/build/build.mjs +261 -10
- package/api/theme/eject/eject.mjs +13 -1
- package/api/theme/list/list.mjs +11 -2
- package/api/theme/template/template.mjs +139 -6
- package/api/theme/theme.type.d.mts +15 -1
- package/api/theme/theme.type.mjs +9 -3
- package/api/theme/themeAdd.doc.mjs +1 -1
- package/api/theme/themeBuild.doc.mjs +10 -2
- package/api/theme/themeEject.doc.mjs +2 -2
- package/api/theme/themeListAvailable.doc.mjs +1 -1
- package/api/theme/themeRemove.doc.mjs +1 -1
- package/api/theme/themeTemplate.doc.mjs +8 -0
- package/api/theme/themeUse.doc.mjs +1 -1
- package/api/theme-edit/_adapter.d.mts +10 -2
- package/api/theme-edit/_adapter.mjs +52 -31
- package/api/theme-edit/_check.d.mts +28 -25
- package/api/theme-edit/_check.mjs +586 -548
- package/api/theme-edit/_color.d.mts +28 -2
- package/api/theme-edit/_color.mjs +306 -68
- package/api/theme-edit/_compiler.d.mts +8 -4
- package/api/theme-edit/_compiler.mjs +30 -27
- package/api/theme-edit/_context.d.mts +10 -4
- package/api/theme-edit/_context.mjs +21 -4
- package/api/theme-edit/_edit.mjs +300 -43
- package/api/theme-edit/_how-commands.d.mts +91 -0
- package/api/theme-edit/_how-commands.mjs +269 -0
- package/api/theme-edit/_how-question.d.mts +57 -0
- package/api/theme-edit/_how-question.mjs +2574 -0
- package/api/theme-edit/_how.d.mts +25 -17
- package/api/theme-edit/_how.mjs +727 -305
- package/api/theme-edit/_palette.d.mts +127 -19
- package/api/theme-edit/_palette.mjs +1313 -399
- package/api/theme-edit/_parts.mjs +36 -3
- package/api/theme-edit/_png.d.mts +18 -6
- package/api/theme-edit/_png.mjs +135 -25
- package/api/theme-edit/_preview-app.d.mts +26 -7
- package/api/theme-edit/_preview-app.mjs +1611 -280
- package/api/theme-edit/_preview.d.mts +4 -2
- package/api/theme-edit/_preview.mjs +18 -7
- package/api/theme-edit/_settings.d.mts +56 -5
- package/api/theme-edit/_settings.mjs +197 -49
- package/api/theme-edit/_tables.d.mts +0 -12
- package/api/theme-edit/_tables.mjs +24 -97
- package/api/theme-edit/theme-edit.type.d.mts +22 -5
- package/api/theme-edit/theme-edit.type.mjs +17 -6
- package/api/theme-edit/themeEditHow.doc.mjs +13 -3
- package/api/theme-edit/themeEditNew.doc.mjs +2 -1
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +16 -4
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +12 -4
- package/api/theme-edit/themeEditPreviewList.doc.mjs +9 -1
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +27 -2
- package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
- package/assets/docs.generated.json +1 -0
- package/assets/preview-app/gitignore +3 -0
- package/assets/templates/components.generated.json +1 -0
- package/clients/cli/bin/astryx.mjs +37 -5
- package/clients/cli/commands/experimental/theme.mjs +231 -31
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +14 -5
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +0 -1
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +3 -2
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-targets.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme.doc.mjs +2 -2
- package/clients/cli/commands/theme-build.doc.mjs +1 -1
- package/clients/cli/commands/theme-eject.doc.mjs +3 -3
- package/clients/cli/commands/theme-remove.doc.mjs +2 -2
- package/clients/cli/commands/theme-template.doc.mjs +11 -4
- package/clients/cli/commands/theme.mjs +104 -40
- package/clients/cli/commands.generated.json +1 -0
- package/clients/cli/index.mjs +96 -4
- package/clients/cli/lib/json-shim.mjs +29 -1
- package/foundation/config/project.d.mts +16 -2
- package/foundation/config/project.mjs +40 -6
- package/foundation/config/theme-state.d.mts +9 -0
- package/foundation/config/theme-state.mjs +20 -1
- package/foundation/discovery/template-adapter.d.mts +12 -1
- package/foundation/discovery/template-adapter.mjs +24 -1
- package/foundation/discovery/template-needs.d.mts +7 -0
- package/foundation/discovery/template-needs.mjs +117 -8
- package/foundation/discovery/template-source-tree.d.mts +5 -1
- package/foundation/discovery/template-source-tree.mjs +6 -4
- package/foundation/discovery/theme-discovery.d.mts +14 -3
- package/foundation/discovery/theme-discovery.mjs +45 -12
- package/foundation/doc-compiler/import.mjs +68 -3
- package/foundation/doc-compiler/stored-docs.d.mts +27 -0
- package/foundation/doc-compiler/stored-docs.mjs +65 -0
- package/foundation/fs/fs-error.d.mts +17 -0
- package/foundation/fs/fs-error.mjs +34 -0
- package/package.json +6 -6
|
@@ -4,37 +4,37 @@
|
|
|
4
4
|
* @file The type check of one `set` or `undo <theme> <thing>`: the part, the variant, the
|
|
5
5
|
* state, the mode, the token's type, and whether the part (or anything in
|
|
6
6
|
* it) reads the token. A change that cannot take effect is refused, and the
|
|
7
|
-
* refusal leads with the command that works.
|
|
8
|
-
* plain-words question with exact commands from the same part data.
|
|
7
|
+
* refusal leads with the command that works.
|
|
9
8
|
*
|
|
10
|
-
* @position api/theme-edit — checks
|
|
9
|
+
* @position api/theme-edit — checks over the part data
|
|
11
10
|
*/
|
|
12
11
|
|
|
13
12
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
13
|
+
import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';
|
|
14
14
|
import {CSS_PROPERTIES} from './_css-properties.mjs';
|
|
15
15
|
import {
|
|
16
16
|
AUTO_SPREAD,
|
|
17
17
|
COLOR_PROPS,
|
|
18
18
|
COMMON_STATES,
|
|
19
19
|
CORNERS,
|
|
20
|
-
DESIGN,
|
|
21
20
|
DIALS,
|
|
22
|
-
DIRECT,
|
|
23
21
|
FAMILIES,
|
|
22
|
+
FONTS,
|
|
24
23
|
GROUPS,
|
|
25
24
|
HEADING_TRACKING,
|
|
26
25
|
HEADING_WEIGHTS,
|
|
27
|
-
INTENTS,
|
|
28
26
|
LIKE,
|
|
29
27
|
HOW,
|
|
28
|
+
OPEN_FONTS,
|
|
30
29
|
P,
|
|
31
|
-
PART_WORDS,
|
|
32
30
|
SPACING,
|
|
33
31
|
TEXT_SIZE,
|
|
34
32
|
THING_HINTS,
|
|
35
33
|
familyOf,
|
|
36
34
|
} from './_tables.mjs';
|
|
37
35
|
import {
|
|
36
|
+
COLOR_NAMES,
|
|
37
|
+
colorProblem,
|
|
38
38
|
isColorValue,
|
|
39
39
|
isNumber,
|
|
40
40
|
isSafeValue,
|
|
@@ -61,6 +61,59 @@ import {
|
|
|
61
61
|
|
|
62
62
|
const E = ERROR_CODES;
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* The value to carry into a suggestion for another dial: the one given when
|
|
66
|
+
* that dial takes it, else the dial's example.
|
|
67
|
+
* @param {string} dial @param {string} v
|
|
68
|
+
*/
|
|
69
|
+
function valueFor(dial, v) {
|
|
70
|
+
if (PALETTE_DIALS.includes(dial)) return colorProblem(v) ? '#7c3aed' : v;
|
|
71
|
+
if (dial === 'vibrancy')
|
|
72
|
+
return /^(more|less)$/.test(v) || (/^\d{1,3}$/.test(v) && Number(v) <= 100)
|
|
73
|
+
? v
|
|
74
|
+
: '60';
|
|
75
|
+
return v;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The other words for each color dial and vibrancy (gray, brand, error,
|
|
80
|
+
* saturation), from the dial tables, without words that name a part, a CSS
|
|
81
|
+
* property or a plain color.
|
|
82
|
+
*/
|
|
83
|
+
const COLOR_DIAL_WORDS = (() => {
|
|
84
|
+
const generic = new Set([
|
|
85
|
+
'color',
|
|
86
|
+
'colour',
|
|
87
|
+
'text',
|
|
88
|
+
'background',
|
|
89
|
+
'border',
|
|
90
|
+
'surface',
|
|
91
|
+
'surfaces',
|
|
92
|
+
'main',
|
|
93
|
+
'key',
|
|
94
|
+
'link',
|
|
95
|
+
'selection',
|
|
96
|
+
'seed',
|
|
97
|
+
'status',
|
|
98
|
+
'tint',
|
|
99
|
+
'highlight',
|
|
100
|
+
'muted',
|
|
101
|
+
'red',
|
|
102
|
+
'green',
|
|
103
|
+
'blue',
|
|
104
|
+
'yellow',
|
|
105
|
+
'orange',
|
|
106
|
+
]);
|
|
107
|
+
/** @type {Map<string, string[]>} */
|
|
108
|
+
const out = new Map();
|
|
109
|
+
for (const d of [...PALETTE_DIALS, 'vibrancy'])
|
|
110
|
+
for (const w of String(DIALS[d]?.words ?? '').split(/\s+/)) {
|
|
111
|
+
if (!w || w === d || generic.has(w)) continue;
|
|
112
|
+
out.set(w, [...(out.get(w) ?? []), d]);
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
})();
|
|
116
|
+
|
|
64
117
|
/**
|
|
65
118
|
* @typedef {object} SetFlags
|
|
66
119
|
* @property {string} [on]
|
|
@@ -70,6 +123,75 @@ const E = ERROR_CODES;
|
|
|
70
123
|
* @property {boolean} [only]
|
|
71
124
|
*/
|
|
72
125
|
|
|
126
|
+
/**
|
|
127
|
+
* The first exact token setting whose value the type rules refuse now (a
|
|
128
|
+
* hand edit, or a value an earlier release took), with why; null when all
|
|
129
|
+
* pass. `skip` is the thing the command changes or removes.
|
|
130
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
131
|
+
* @param {string} slug
|
|
132
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
133
|
+
* @param {string} [skip]
|
|
134
|
+
* @returns {{setting: import('./_settings.mjs').Setting, why: string} | null}
|
|
135
|
+
*/
|
|
136
|
+
export function storedTokenProblem(ctx, slug, data, skip = '') {
|
|
137
|
+
const TOKENS = ctx.project.core.tokenDefaults;
|
|
138
|
+
for (const s of data.settings) {
|
|
139
|
+
if (s.source !== 'token' || s.thing === skip || !(s.thing in TOKENS))
|
|
140
|
+
continue;
|
|
141
|
+
try {
|
|
142
|
+
checkTypedValue(
|
|
143
|
+
ctx.C.typeOf(s.thing),
|
|
144
|
+
s.value,
|
|
145
|
+
s.thing,
|
|
146
|
+
TOKENS,
|
|
147
|
+
ctx.C.typeOf,
|
|
148
|
+
slug,
|
|
149
|
+
);
|
|
150
|
+
checkCycle(s.thing, s.value, TOKENS, data);
|
|
151
|
+
} catch (e) {
|
|
152
|
+
return {
|
|
153
|
+
setting: s,
|
|
154
|
+
why:
|
|
155
|
+
e instanceof Error
|
|
156
|
+
? e.message.replace(/ Nothing was changed\.$/, '')
|
|
157
|
+
: String(e),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* A value of a type to put in a suggestion, so a suggested command runs.
|
|
166
|
+
* @param {string} type
|
|
167
|
+
* @param {string} name
|
|
168
|
+
*/
|
|
169
|
+
export function exampleOf(type, name) {
|
|
170
|
+
return (
|
|
171
|
+
{
|
|
172
|
+
color: '#1f2937',
|
|
173
|
+
size: '6px',
|
|
174
|
+
number: /weight/.test(name) ? '600' : '1.4',
|
|
175
|
+
time: '150ms',
|
|
176
|
+
easing: 'cubic-bezier(0.2, 0, 0, 1)',
|
|
177
|
+
font: 'Inter, sans-serif',
|
|
178
|
+
shadow: '0 2px 4px rgb(0 0 0 / 0.2)',
|
|
179
|
+
}[type] ?? (/style/.test(name) ? 'solid' : '0')
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The value type a CSS property takes, for an example value.
|
|
185
|
+
* @param {string} prop
|
|
186
|
+
*/
|
|
187
|
+
function groupOfPropType(prop) {
|
|
188
|
+
if (COLOR_PROPS.test(prop)) return 'color';
|
|
189
|
+
if (/shadow/.test(prop)) return 'shadow';
|
|
190
|
+
if (/weight|line-height|opacity/.test(prop)) return 'number';
|
|
191
|
+
if (/family/.test(prop)) return 'font';
|
|
192
|
+
return 'size';
|
|
193
|
+
}
|
|
194
|
+
|
|
73
195
|
/**
|
|
74
196
|
* Refuse a value that does not parse as the token's type.
|
|
75
197
|
* @param {string} type @param {string} value @param {string} name
|
|
@@ -89,11 +211,11 @@ function checkTypedValue(type, value, name, TOKENS, typeOf, slug) {
|
|
|
89
211
|
refuse(
|
|
90
212
|
E.ERR_THEME_BAD_VALUE,
|
|
91
213
|
`"${value}" cannot be a CSS value: it has a ; { } < > or a newline, a comment, !important, or an unclosed bracket or quote.`,
|
|
92
|
-
{run: [`${P} set ${slug} ${name}
|
|
214
|
+
{run: [`${P} set ${slug} ${name} ${q(exampleOf(type, name))}`]},
|
|
93
215
|
);
|
|
94
216
|
// A var() naming a design token must name one of the same kind; the
|
|
95
217
|
// app's own variables are its business.
|
|
96
|
-
const ref = /^var\((--[a-z0-9-]+)\)$/i.exec(value.trim());
|
|
218
|
+
const ref = /^var\(\s*(--[a-z0-9-]+)\s*(,.*)?\)$/i.exec(value.trim());
|
|
97
219
|
if (ref) {
|
|
98
220
|
if (ref[1] in TOKENS && typeOf(ref[1]) !== type && type !== 'other')
|
|
99
221
|
refuse(
|
|
@@ -111,16 +233,10 @@ function checkTypedValue(type, value, name, TOKENS, typeOf, slug) {
|
|
|
111
233
|
if (type === 'size' && !isSize(value, name)) bad('a size', '6px');
|
|
112
234
|
if (type === 'number' && !isNumber(value, name))
|
|
113
235
|
bad('a number', /weight/.test(name) ? '600' : '1.4');
|
|
114
|
-
if (type === 'time' &&
|
|
115
|
-
|
|
116
|
-
if (
|
|
117
|
-
type === 'easing' &&
|
|
118
|
-
!/^(linear|ease|ease-in|ease-out|ease-in-out|step-start|step-end)$|^(cubic-bezier|steps|linear)\([^()]*\)$/.test(
|
|
119
|
-
value.trim(),
|
|
120
|
-
)
|
|
121
|
-
)
|
|
236
|
+
if (type === 'time' && !isTime(value)) bad('a time up to 60s', '150ms');
|
|
237
|
+
if (type === 'easing' && !isEasing(value))
|
|
122
238
|
bad('an easing', 'cubic-bezier(0.2, 0, 0, 1)');
|
|
123
|
-
if (type === 'font' &&
|
|
239
|
+
if (type === 'font' && !isFamilyList(value))
|
|
124
240
|
bad('a font family list', 'Inter, sans-serif');
|
|
125
241
|
if (type === 'shadow' && !isShadow(value))
|
|
126
242
|
bad('a shadow', '0 2px 4px rgb(0 0 0 / 0.2)');
|
|
@@ -139,17 +255,131 @@ function checkTypedValue(type, value, name, TOKENS, typeOf, slug) {
|
|
|
139
255
|
* functions and keywords it cannot read but CSS can; never a CSS-wide
|
|
140
256
|
* keyword or a function with broken insides.
|
|
141
257
|
* @param {string} value
|
|
258
|
+
* @returns {boolean}
|
|
142
259
|
*/
|
|
143
260
|
function isFixedColor(value) {
|
|
144
261
|
const v = value.trim().toLowerCase();
|
|
145
262
|
if (['inherit', 'initial', 'unset', 'revert', 'revert-layer'].includes(v))
|
|
146
263
|
return false;
|
|
264
|
+
if (/^var\(/.test(v)) return true;
|
|
265
|
+
if (SYSTEM_COLORS.includes(v)) return true;
|
|
266
|
+
const inner = (/** @type {string} */ fn) =>
|
|
267
|
+
splitTop(v.slice(fn.length + 1, -1), ',').map(x => x.trim());
|
|
268
|
+
// Each mode's value, and each mixed color, must be a color itself.
|
|
269
|
+
if (/^light-dark\(.*\)$/.test(v)) {
|
|
270
|
+
const args = inner('light-dark');
|
|
271
|
+
return args.length === 2 && args.every(isFixedColor);
|
|
272
|
+
}
|
|
273
|
+
if (/^color-mix\(.*\)$/.test(v)) {
|
|
274
|
+
const [space, ...colors] = inner('color-mix');
|
|
275
|
+
return (
|
|
276
|
+
/^in\s+[a-z-]+/.test(space ?? '') &&
|
|
277
|
+
colors.length === 2 &&
|
|
278
|
+
colors.every(c => isFixedColor(c.replace(/\s+\d*\.?\d+%$/, '')))
|
|
279
|
+
);
|
|
280
|
+
}
|
|
147
281
|
if (!isColorValue(v)) return false;
|
|
282
|
+
// A relative color or channels from variables are read by the browser.
|
|
283
|
+
if (/\bfrom\b|var\(|calc\(/.test(v)) return true;
|
|
148
284
|
if (/^(rgb|rgba|hsl|hsla|hwb|lab|lch|oklab|oklch)\(/.test(v))
|
|
149
285
|
return parseColor(v) != null;
|
|
286
|
+
if (/^color\(/.test(v))
|
|
287
|
+
return /^color\((srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|xyz|xyz-d50|xyz-d65)\s/.test(
|
|
288
|
+
v,
|
|
289
|
+
);
|
|
150
290
|
return true;
|
|
151
291
|
}
|
|
152
292
|
|
|
293
|
+
/** CSS system colors, which the browser resolves. */
|
|
294
|
+
const SYSTEM_COLORS = [
|
|
295
|
+
'canvas',
|
|
296
|
+
'canvastext',
|
|
297
|
+
'linktext',
|
|
298
|
+
'visitedtext',
|
|
299
|
+
'activetext',
|
|
300
|
+
'buttonface',
|
|
301
|
+
'buttontext',
|
|
302
|
+
'buttonborder',
|
|
303
|
+
'field',
|
|
304
|
+
'fieldtext',
|
|
305
|
+
'highlight',
|
|
306
|
+
'highlighttext',
|
|
307
|
+
'selecteditem',
|
|
308
|
+
'selecteditemtext',
|
|
309
|
+
'mark',
|
|
310
|
+
'marktext',
|
|
311
|
+
'graytext',
|
|
312
|
+
'accentcolor',
|
|
313
|
+
'accentcolortext',
|
|
314
|
+
];
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Whether a value is a time the browser takes, at most a minute.
|
|
318
|
+
* @param {string} value
|
|
319
|
+
*/
|
|
320
|
+
function isTime(value) {
|
|
321
|
+
const v = value.trim().toLowerCase();
|
|
322
|
+
if (/^(calc|min|max|clamp)\(/.test(v)) return true;
|
|
323
|
+
const m = /^\+?(\d*\.?\d+(?:e[+-]?\d+)?)(ms|s)$/.exec(v);
|
|
324
|
+
if (!m) return false;
|
|
325
|
+
const ms = Number(m[1]) * (m[2] === 's' ? 1000 : 1);
|
|
326
|
+
return ms <= 60_000;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Whether a value is an easing the browser takes: a keyword, a cubic-bezier
|
|
331
|
+
* with four numbers (x1 and x2 from 0 to 1), steps(n), or linear() with two
|
|
332
|
+
* stops or more.
|
|
333
|
+
* @param {string} value
|
|
334
|
+
*/
|
|
335
|
+
function isEasing(value) {
|
|
336
|
+
const v = value.trim().toLowerCase();
|
|
337
|
+
if (
|
|
338
|
+
/^(linear|ease|ease-in|ease-out|ease-in-out|step-start|step-end)$/.test(v)
|
|
339
|
+
)
|
|
340
|
+
return true;
|
|
341
|
+
const m = /^(cubic-bezier|steps|linear)\((.*)\)$/.exec(v);
|
|
342
|
+
if (!m) return false;
|
|
343
|
+
const args = splitTop(m[2], ',').map(x => x.trim());
|
|
344
|
+
if (args.some(a => /var\(/.test(a))) return true;
|
|
345
|
+
const num = (/** @type {string} */ a) => /^[+-]?\d*\.?\d+$/.test(a);
|
|
346
|
+
if (m[1] === 'cubic-bezier')
|
|
347
|
+
return (
|
|
348
|
+
args.length === 4 &&
|
|
349
|
+
args.every(num) &&
|
|
350
|
+
[0, 2].every(i => Number(args[i]) >= 0 && Number(args[i]) <= 1)
|
|
351
|
+
);
|
|
352
|
+
if (m[1] === 'steps')
|
|
353
|
+
return (
|
|
354
|
+
/^\d+$/.test(args[0] ?? '') &&
|
|
355
|
+
Number(args[0]) >= 1 &&
|
|
356
|
+
args.length <= 2 &&
|
|
357
|
+
(args.length === 1 ||
|
|
358
|
+
/^(jump-start|jump-end|jump-none|jump-both|start|end)$/.test(
|
|
359
|
+
args[1],
|
|
360
|
+
)) &&
|
|
361
|
+
!(args[1] === 'jump-none' && Number(args[0]) < 2)
|
|
362
|
+
);
|
|
363
|
+
return args.length >= 2 && args.every(a => /^[+-]?\d*\.?\d+(\s|$)/.test(a));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Whether a value is a font family list: each family a quoted name, a name
|
|
368
|
+
* of words that do not start with a digit, or a var().
|
|
369
|
+
* @param {string} value
|
|
370
|
+
*/
|
|
371
|
+
function isFamilyList(value) {
|
|
372
|
+
return splitTop(value.trim(), ',').every(f => {
|
|
373
|
+
const t = f.trim();
|
|
374
|
+
return (
|
|
375
|
+
/^"[^"]+"$/.test(t) ||
|
|
376
|
+
/^'[^']+'$/.test(t) ||
|
|
377
|
+
/^var\(/i.test(t) ||
|
|
378
|
+
/^[\p{L}_-][\p{L}\p{N}_-]*(\s+[\p{L}_-][\p{L}\p{N}_-]*)*$/u.test(t)
|
|
379
|
+
);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
153
383
|
/**
|
|
154
384
|
* Whether a value is a shadow list: each layer is `inset`, two to four
|
|
155
385
|
* lengths, and a color, in any order; or `none`.
|
|
@@ -160,18 +390,38 @@ function isShadow(value) {
|
|
|
160
390
|
if (v === 'none') return true;
|
|
161
391
|
const layers = splitTop(v, ',');
|
|
162
392
|
// light-dark() takes colors: a whole shadow per mode inside it is dropped.
|
|
163
|
-
if (
|
|
393
|
+
if (
|
|
394
|
+
layers.length === 1 &&
|
|
395
|
+
splitTop(v, ' ').length === 1 &&
|
|
396
|
+
!/^var\(/i.test(v)
|
|
397
|
+
)
|
|
398
|
+
return false;
|
|
164
399
|
return layers.every(layer => {
|
|
165
400
|
const words = splitTop(layer.trim(), ' ');
|
|
166
|
-
|
|
167
|
-
|
|
401
|
+
// One var() stacks a shadow token in the list.
|
|
402
|
+
if (words.length === 1 && /^var\(/i.test(words[0])) return true;
|
|
403
|
+
const insets = words.filter(w => w.toLowerCase() === 'inset').length;
|
|
404
|
+
const nums = words.filter(w =>
|
|
405
|
+
/^[+-]?\d*\.?\d+(px|rem|em|pt|pc|cm|mm|in|vw|vh|vmin|vmax|ch|ex|lh)?$|^(calc|min|max|clamp)\(/i.test(
|
|
406
|
+
w,
|
|
407
|
+
),
|
|
408
|
+
);
|
|
409
|
+
const vars = words.filter(w => /^var\(/i.test(w));
|
|
410
|
+
const rest = words.filter(
|
|
411
|
+
w =>
|
|
412
|
+
!nums.includes(w) && !vars.includes(w) && w.toLowerCase() !== 'inset',
|
|
168
413
|
);
|
|
169
|
-
|
|
414
|
+
// A blur (the third length) is never negative.
|
|
415
|
+
if (nums.length >= 3 && nums[2].startsWith('-')) return false;
|
|
416
|
+
// A var() fills a length or the color, whichever is missing.
|
|
417
|
+
const color = rest.length ? 0 : Math.min(1, vars.length);
|
|
418
|
+
const lengths = nums.length + vars.length - color;
|
|
170
419
|
return (
|
|
171
|
-
|
|
172
|
-
lengths
|
|
420
|
+
insets <= 1 &&
|
|
421
|
+
lengths >= 2 &&
|
|
422
|
+
lengths <= 4 &&
|
|
173
423
|
rest.length <= 1 &&
|
|
174
|
-
rest.every(w =>
|
|
424
|
+
rest.every(w => isFixedColor(w))
|
|
175
425
|
);
|
|
176
426
|
});
|
|
177
427
|
}
|
|
@@ -218,7 +468,7 @@ function checkCycle(thing, value, TOKENS, data) {
|
|
|
218
468
|
'',
|
|
219
469
|
);
|
|
220
470
|
const refs = (/** @type {string} */ v) =>
|
|
221
|
-
[...v.matchAll(/var\((--[a-z0-9-]+)/gi)].map(m => m[1]);
|
|
471
|
+
[...v.matchAll(/var\(\s*(--[a-z0-9-]+)/gi)].map(m => m[1]);
|
|
222
472
|
/** @type {string[]} */
|
|
223
473
|
const path = [];
|
|
224
474
|
const seen = new Set();
|
|
@@ -296,6 +546,25 @@ export function checkSetting(
|
|
|
296
546
|
run: [`${P} ${unset ? 'undo' : 'set'} --help`],
|
|
297
547
|
});
|
|
298
548
|
const v = (value ?? '').trim();
|
|
549
|
+
// A var() that misspells a design token would read nothing.
|
|
550
|
+
if (!unset)
|
|
551
|
+
for (const [, name] of v.matchAll(/var\(\s*(--[a-z0-9-]+)/gi)) {
|
|
552
|
+
if (name in TOKENS || name.startsWith('--_')) continue;
|
|
553
|
+
const near = nearest(name, Object.keys(TOKENS)).filter(
|
|
554
|
+
n => levenshteinDistance(n, name) <= 2,
|
|
555
|
+
);
|
|
556
|
+
if (near.length)
|
|
557
|
+
refuse(
|
|
558
|
+
E.ERR_THEME_UNKNOWN_TOKEN,
|
|
559
|
+
`${name} is not a design token, so var(${name}) would read nothing.`,
|
|
560
|
+
{
|
|
561
|
+
didYouMean: near,
|
|
562
|
+
run: [
|
|
563
|
+
`${HOW} --list tokens ${near[0].replace(/^--/, '').split('-')[0]}`,
|
|
564
|
+
],
|
|
565
|
+
},
|
|
566
|
+
);
|
|
567
|
+
}
|
|
299
568
|
// A placeholder copied from a suggestion is not a value.
|
|
300
569
|
if (!unset && /<[a-z][\w -]*>/i.test(v))
|
|
301
570
|
refuse(
|
|
@@ -305,9 +574,9 @@ export function checkSetting(
|
|
|
305
574
|
);
|
|
306
575
|
/** @type {import('./_settings.mjs').Setting} */
|
|
307
576
|
const s = {thing, value: v, source: 'dial'};
|
|
308
|
-
if (flags.on) s.on = flags.on;
|
|
309
|
-
if (flags.when) s.when = flags.when;
|
|
310
|
-
if (flags.state) s.state = flags.state;
|
|
577
|
+
if (flags.on) s.on = flags.on.trim();
|
|
578
|
+
if (flags.when) s.when = flags.when.trim();
|
|
579
|
+
if (flags.state) s.state = flags.state.trim();
|
|
311
580
|
if (flags.only) s.only = true;
|
|
312
581
|
if (flags.mode) {
|
|
313
582
|
const mode = flags.mode.toLowerCase();
|
|
@@ -320,7 +589,8 @@ export function checkSetting(
|
|
|
320
589
|
s.mode = mode;
|
|
321
590
|
}
|
|
322
591
|
const onFlag = s.on ? ` --on ${s.on}` : '';
|
|
323
|
-
|
|
592
|
+
// A setting saved for a mode the theme no longer has can still be removed.
|
|
593
|
+
if (s.mode && data.mode !== 'both' && !unset)
|
|
324
594
|
refuse(
|
|
325
595
|
E.ERR_THEME_NO_MODE,
|
|
326
596
|
`This theme is ${data.mode}; --mode is for themes with both modes.`,
|
|
@@ -328,7 +598,7 @@ export function checkSetting(
|
|
|
328
598
|
);
|
|
329
599
|
if ((s.when || s.state) && !s.on)
|
|
330
600
|
refuse(E.ERR_MISSING_ARGUMENT, '--when and --state need --on <part>.', {
|
|
331
|
-
run: [`${HOW}
|
|
601
|
+
run: [`${HOW} --list parts`],
|
|
332
602
|
});
|
|
333
603
|
if (s.only) {
|
|
334
604
|
const fam = s.on ? familyOf(s.on) : null;
|
|
@@ -365,15 +635,15 @@ export function checkSetting(
|
|
|
365
635
|
...Object.keys(FAMILIES),
|
|
366
636
|
]),
|
|
367
637
|
run: [
|
|
368
|
-
`${HOW} ${s.on.split('-')[0]}
|
|
369
|
-
`${HOW}
|
|
638
|
+
`${HOW} --list parts ${s.on.split('-')[0]}`,
|
|
639
|
+
`${HOW} ${q(s.on)}`,
|
|
370
640
|
],
|
|
371
641
|
},
|
|
372
642
|
);
|
|
373
643
|
}
|
|
374
644
|
if (s.when) {
|
|
375
|
-
const [p, wv] = s.when.split(':');
|
|
376
|
-
if (!wv)
|
|
645
|
+
const [p, wv, ...more] = s.when.split(':');
|
|
646
|
+
if (!wv || more.length)
|
|
377
647
|
refuse(
|
|
378
648
|
E.ERR_THEME_BAD_VALUE,
|
|
379
649
|
'--when takes <prop>:<value>, such as variant:primary.',
|
|
@@ -401,6 +671,37 @@ export function checkSetting(
|
|
|
401
671
|
{didYouMean: nearest(s.state, states)},
|
|
402
672
|
);
|
|
403
673
|
}
|
|
674
|
+
// On a group, a variant or state must belong to at least one member.
|
|
675
|
+
if (s.on && FAMILIES[s.on] && (s.when || s.state)) {
|
|
676
|
+
const members =
|
|
677
|
+
/** @type {Array<[string, import('../../foundation/discovery/theming-targets.mjs').ThemingTarget]>} */ (
|
|
678
|
+
FAMILIES[s.on].map(m => [m, TARGET.get(m)]).filter(([, t]) => t)
|
|
679
|
+
);
|
|
680
|
+
if (s.when) {
|
|
681
|
+
const [p, wv, ...more] = s.when.split(':');
|
|
682
|
+
const fits = members.some(([m, t]) => {
|
|
683
|
+
if (!wv || more.length || !t.props.includes(p)) return false;
|
|
684
|
+
const vals = C.variantValues(m)[p] ?? [];
|
|
685
|
+
return !vals.length || vals.includes(wv);
|
|
686
|
+
});
|
|
687
|
+
if (!fits)
|
|
688
|
+
refuse(
|
|
689
|
+
E.ERR_THEME_UNKNOWN_VARIANT,
|
|
690
|
+
`No member of the ${s.on} group has ${s.when}.`,
|
|
691
|
+
{run: [`${HOW} ${s.on}`]},
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
const state = s.state;
|
|
695
|
+
if (
|
|
696
|
+
state &&
|
|
697
|
+
!members.some(([, t]) => [...t.states, ...COMMON_STATES].includes(state))
|
|
698
|
+
)
|
|
699
|
+
refuse(
|
|
700
|
+
E.ERR_THEME_UNKNOWN_STATE,
|
|
701
|
+
`"${s.state}" is not a state of any member of the ${s.on} group.`,
|
|
702
|
+
{run: [`${HOW} ${s.on}`]},
|
|
703
|
+
);
|
|
704
|
+
}
|
|
404
705
|
const step = v === 'more' || v === 'less';
|
|
405
706
|
if (DIALS[thing]) {
|
|
406
707
|
s.source = 'dial';
|
|
@@ -423,12 +724,18 @@ export function checkSetting(
|
|
|
423
724
|
);
|
|
424
725
|
if (unset) return s;
|
|
425
726
|
const ok = DIALS[thing].values.split('|');
|
|
426
|
-
|
|
727
|
+
const lower = v.toLowerCase();
|
|
728
|
+
if (!ok.includes(v)) {
|
|
729
|
+
const near = ok.includes(lower) ? [lower] : nearest(lower, ok);
|
|
427
730
|
refuse(
|
|
428
731
|
E.ERR_THEME_BAD_VALUE,
|
|
429
732
|
`${thing} takes ${DIALS[thing].values}, not "${v}".`,
|
|
430
|
-
{
|
|
733
|
+
{
|
|
734
|
+
didYouMean: near,
|
|
735
|
+
run: [`${P} set ${slug} ${thing} ${near[0] ?? ok[0]}`],
|
|
736
|
+
},
|
|
431
737
|
);
|
|
738
|
+
}
|
|
432
739
|
return s;
|
|
433
740
|
}
|
|
434
741
|
if (thing === 'headings') {
|
|
@@ -442,11 +749,27 @@ export function checkSetting(
|
|
|
442
749
|
],
|
|
443
750
|
},
|
|
444
751
|
);
|
|
752
|
+
if (s.mode)
|
|
753
|
+
refuse(
|
|
754
|
+
E.ERR_THEME_NO_MODE,
|
|
755
|
+
'headings is the same in light and dark; drop --mode.',
|
|
756
|
+
{
|
|
757
|
+
run: [
|
|
758
|
+
`${P} ${unset ? 'undo' : 'set'} ${slug} headings${v ? ` ${q(v)}` : ''}`,
|
|
759
|
+
],
|
|
760
|
+
},
|
|
761
|
+
);
|
|
445
762
|
if (unset) {
|
|
763
|
+
// With no word, the one headings setting the theme has.
|
|
764
|
+
const held = data.settings.filter(x => x.thing === 'headings');
|
|
446
765
|
s.axis =
|
|
447
766
|
HEADING_TRACKING[v] !== undefined || v === 'tracking'
|
|
448
767
|
? 'tracking'
|
|
449
|
-
: 'weight'
|
|
768
|
+
: HEADING_WEIGHTS[v] !== undefined || v === 'weight'
|
|
769
|
+
? 'weight'
|
|
770
|
+
: held.length === 1
|
|
771
|
+
? (held[0].axis ?? 'weight')
|
|
772
|
+
: 'weight';
|
|
450
773
|
return s;
|
|
451
774
|
}
|
|
452
775
|
if (HEADING_WEIGHTS[v] !== undefined) s.axis = 'weight';
|
|
@@ -467,11 +790,13 @@ export function checkSetting(
|
|
|
467
790
|
if (unset) return s;
|
|
468
791
|
/** @param {Array<[string, number]>} L @param {string} def */
|
|
469
792
|
const ladder = (L, def) => {
|
|
470
|
-
|
|
793
|
+
// The value in effect for the target: its own setting, else the
|
|
794
|
+
// whole-theme one, else the starting point.
|
|
795
|
+
const own = (/** @type {string} */ on) =>
|
|
471
796
|
[...data.settings]
|
|
472
797
|
.reverse()
|
|
473
|
-
.find(x => x.thing === thing && (x.on ?? '') ===
|
|
474
|
-
|
|
798
|
+
.find(x => x.thing === thing && (x.on ?? '') === on)?.value;
|
|
799
|
+
const cur = own(s.on ?? '') ?? (s.on ? own('') : undefined) ?? def;
|
|
475
800
|
let i = L.findIndex(([n]) => n === cur);
|
|
476
801
|
if (i < 0) {
|
|
477
802
|
const px = parseFloat(cur);
|
|
@@ -484,14 +809,36 @@ export function checkSetting(
|
|
|
484
809
|
};
|
|
485
810
|
if (PALETTE_DIALS.includes(thing) && (s.on || s.mode)) {
|
|
486
811
|
const token = paletteToken(thing);
|
|
812
|
+
const value = q(v || '#7c3aed');
|
|
813
|
+
/** @type {string[]} */
|
|
814
|
+
let run = [
|
|
815
|
+
`${P} set ${slug} ${token} ${value}${onFlag}${s.mode ? ` --mode ${s.mode}` : ''}`,
|
|
816
|
+
];
|
|
817
|
+
// The token is suggested only where the part reads it; otherwise the
|
|
818
|
+
// part's own route, as a token set on the part would be refused.
|
|
819
|
+
if (s.on && !ctx.unchecked) {
|
|
820
|
+
/** @param {string} t */
|
|
821
|
+
const works = t => {
|
|
822
|
+
try {
|
|
823
|
+
checkSetting(ctx, slug, data, t, v || '#7c3aed', {
|
|
824
|
+
on: s.on,
|
|
825
|
+
when: s.when,
|
|
826
|
+
state: s.state,
|
|
827
|
+
});
|
|
828
|
+
return true;
|
|
829
|
+
} catch {
|
|
830
|
+
return false;
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
const direct = `${P} set ${slug} background-color ${value}${onFlag}`;
|
|
834
|
+
// The neutral color is a surface: on a part, that is its background.
|
|
835
|
+
if (thing === 'neutral') run = [direct, ...run];
|
|
836
|
+
else if (!works(token) && works('background-color')) run = [direct];
|
|
837
|
+
}
|
|
487
838
|
refuse(
|
|
488
839
|
E.ERR_THEME_BAD_VALUE,
|
|
489
|
-
`${thing} generates the colors of the whole theme, light and dark from one color; drop ${s.on ? '--on' : '--mode'}. For an exact color on one part or in one mode, set the token itself.`,
|
|
490
|
-
{
|
|
491
|
-
run: [
|
|
492
|
-
`${P} set ${slug} ${token} ${q(v || '#7c3aed')}${onFlag}${s.mode ? ` --mode ${s.mode}` : ''}`,
|
|
493
|
-
],
|
|
494
|
-
},
|
|
840
|
+
`${thing} generates the colors of the whole theme, light and dark from one color; drop ${s.on ? '--on' : '--mode'}. For an exact color on one part or in one mode, set ${thing === 'neutral' && s.on ? "the part's background, or " : ''}the token itself.`,
|
|
841
|
+
{run},
|
|
495
842
|
);
|
|
496
843
|
}
|
|
497
844
|
if (s.mode)
|
|
@@ -501,9 +848,18 @@ export function checkSetting(
|
|
|
501
848
|
{run: [`${P} set ${slug} ${thing} ${q(v)}${onFlag}`]},
|
|
502
849
|
);
|
|
503
850
|
if (thing === 'corners') {
|
|
504
|
-
if (s.on) refuseInvisibleCorners(ctx, slug, s);
|
|
505
|
-
|
|
506
|
-
|
|
851
|
+
if (s.on) refuseInvisibleCorners(ctx, slug, s, data);
|
|
852
|
+
// With no corners setting yet, a step starts from the base theme's
|
|
853
|
+
// own radius.
|
|
854
|
+
if (step) {
|
|
855
|
+
const px = Number.parseFloat(ctx.base?.light['--radius-element'] ?? '');
|
|
856
|
+
const from = Number.isFinite(px)
|
|
857
|
+
? CORNERS.reduce((a, b) =>
|
|
858
|
+
Math.abs(b[1] - px) < Math.abs(a[1] - px) ? b : a,
|
|
859
|
+
)[0]
|
|
860
|
+
: 'medium';
|
|
861
|
+
s.value = ladder(CORNERS, from);
|
|
862
|
+
} else if (!CORNERS.some(([n]) => n === v) && !/^\d+(\.\d+)?px$/.test(v))
|
|
507
863
|
refuse(
|
|
508
864
|
E.ERR_THEME_BAD_VALUE,
|
|
509
865
|
`corners takes ${DIALS.corners.values}, not "${v}".`,
|
|
@@ -551,18 +907,36 @@ export function checkSetting(
|
|
|
551
907
|
refuse(E.ERR_THEME_BAD_VALUE, `${thing} takes a color, not a step.`, {
|
|
552
908
|
run: [`${P} set ${slug} ${thing} "#7c3aed"`],
|
|
553
909
|
});
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
910
|
+
const problem = colorProblem(v);
|
|
911
|
+
if (problem) {
|
|
912
|
+
// What the value most likely meant: without quotes, a trailing `;`
|
|
913
|
+
// or `!important`, a bare hex with its #, the opaque color, or the
|
|
914
|
+
// nearest color name.
|
|
915
|
+
const cleaned = v
|
|
916
|
+
.replace(/^(["'])(.*)\1$/, '$2')
|
|
917
|
+
.replace(/\s*!important\s*$/i, '')
|
|
918
|
+
.replace(/;+\s*$/, '')
|
|
919
|
+
.trim()
|
|
920
|
+
.replace(/^([0-9a-f]{3}|[0-9a-f]{6})$/i, '#$1')
|
|
921
|
+
// One hex digit short or over: repeat or drop the last one.
|
|
922
|
+
.replace(/^#([0-9a-f]{4})([0-9a-f])$/i, '#$1$2$2')
|
|
923
|
+
.replace(/^#([0-9a-f]{6})[0-9a-f]$/i, '#$1');
|
|
924
|
+
const near =
|
|
925
|
+
problem.kind === 'unknown'
|
|
926
|
+
? nearest(v.toLowerCase(), COLOR_NAMES)
|
|
927
|
+
: [];
|
|
928
|
+
const better =
|
|
929
|
+
problem.opaque ??
|
|
930
|
+
(cleaned !== v && !colorProblem(cleaned) ? cleaned : near[0]);
|
|
559
931
|
refuse(
|
|
560
932
|
E.ERR_THEME_BAD_VALUE,
|
|
561
|
-
`"${v}"
|
|
933
|
+
`"${v}" ${problem.message}.${problem.kind === 'unknown' || problem.kind === 'malformed' ? ' Give a hex, a color name, or an rgb(), hsl(), hwb(), lab(), lch(), oklab(), oklch() or color() color.' : ''}`,
|
|
562
934
|
{
|
|
563
|
-
|
|
935
|
+
didYouMean: better ? [better] : near,
|
|
936
|
+
run: [`${P} set ${slug} ${thing} ${q(better ?? '#7c3aed')}`],
|
|
564
937
|
},
|
|
565
938
|
);
|
|
939
|
+
}
|
|
566
940
|
} else if (thing === 'vibrancy') {
|
|
567
941
|
if (s.on)
|
|
568
942
|
refuse(
|
|
@@ -571,20 +945,41 @@ export function checkSetting(
|
|
|
571
945
|
{run: [`${P} set ${slug} vibrancy ${q(v)}`]},
|
|
572
946
|
);
|
|
573
947
|
if (step) {
|
|
574
|
-
const
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
948
|
+
const saved = [...data.settings]
|
|
949
|
+
.reverse()
|
|
950
|
+
.find(x => x.thing === 'vibrancy')?.value;
|
|
951
|
+
// A value edited by hand that is not one steps from the default.
|
|
952
|
+
const cur =
|
|
953
|
+
saved != null && /^\d{1,3}$/.test(saved) && Number(saved) <= 100
|
|
954
|
+
? Number(saved)
|
|
955
|
+
: VIBRANCY_DEFAULT;
|
|
956
|
+
const next = Math.max(
|
|
957
|
+
0,
|
|
958
|
+
Math.min(100, cur + (v === 'more' ? 10 : -10)),
|
|
580
959
|
);
|
|
581
|
-
|
|
960
|
+
if (next === cur)
|
|
961
|
+
refuse(
|
|
962
|
+
E.ERR_THEME_NO_EFFECT,
|
|
963
|
+
`vibrancy is already ${cur}, the ${cur >= 100 ? 'most vivid' : 'grayest'}, so ${v} changes nothing.`,
|
|
964
|
+
{
|
|
965
|
+
run: [
|
|
966
|
+
`${P} set ${slug} vibrancy ${v === 'more' ? 'less' : 'more'}`,
|
|
967
|
+
],
|
|
968
|
+
},
|
|
969
|
+
);
|
|
970
|
+
s.value = String(next);
|
|
971
|
+
} else if (!/^\d{1,3}$/.test(v) || Number(v) > 100) {
|
|
972
|
+
// A number out of range suggests the nearest one in range.
|
|
973
|
+
const n = Number(v);
|
|
974
|
+
const near = /^[+-]?(\d+\.?\d*|\.\d+)$/.test(v)
|
|
975
|
+
? Math.max(0, Math.min(100, Math.round(n)))
|
|
976
|
+
: 60;
|
|
582
977
|
refuse(
|
|
583
978
|
E.ERR_THEME_BAD_VALUE,
|
|
584
979
|
`vibrancy takes a whole number from 0 (gray) to 100 (most vivid), or more or less, not "${v}".`,
|
|
585
|
-
{run: [`${P} set ${slug} vibrancy
|
|
980
|
+
{run: [`${P} set ${slug} vibrancy ${near}`]},
|
|
586
981
|
);
|
|
587
|
-
else s.value = String(Number(v));
|
|
982
|
+
} else s.value = String(Number(v));
|
|
588
983
|
const colors = data.settings.filter(
|
|
589
984
|
x => x.source === 'dial' && PALETTE_DIALS.includes(x.thing),
|
|
590
985
|
);
|
|
@@ -598,6 +993,16 @@ export function checkSetting(
|
|
|
598
993
|
{run: [`${P} set ${slug} accent "#7c3aed"`]},
|
|
599
994
|
);
|
|
600
995
|
} else if (thing === 'font') {
|
|
996
|
+
if (s.when || s.state)
|
|
997
|
+
refuse(
|
|
998
|
+
E.ERR_INVALID_ARGUMENT,
|
|
999
|
+
'font sets a role (body, heading, code) for the whole theme; it takes no --when or --state. For one part use font-family.',
|
|
1000
|
+
{
|
|
1001
|
+
run: [
|
|
1002
|
+
`${P} set ${slug} font-family ${q(v)} --on heading${s.when ? ` --when ${s.when}` : ''}${s.state ? ` --state ${s.state}` : ''}`,
|
|
1003
|
+
],
|
|
1004
|
+
},
|
|
1005
|
+
);
|
|
601
1006
|
if (s.on && !isRole)
|
|
602
1007
|
refuse(
|
|
603
1008
|
E.ERR_THEME_BAD_VALUE,
|
|
@@ -611,13 +1016,22 @@ export function checkSetting(
|
|
|
611
1016
|
{fix: `the values are ${DIALS.font.values}`},
|
|
612
1017
|
);
|
|
613
1018
|
// A family list: names, quoted or not, split by commas; nothing that
|
|
614
|
-
// could end the CSS declaration or the preview's style block.
|
|
1019
|
+
// could end the CSS declaration or the preview's style block. A
|
|
1020
|
+
// preset typed in another case or spacing is the preset.
|
|
615
1021
|
s.value = v.trim();
|
|
1022
|
+
const key = s.value.toLowerCase().replace(/[\s_-]+/g, '');
|
|
1023
|
+
const preset = [...Object.keys(FONTS), ...Object.keys(OPEN_FONTS)].find(
|
|
1024
|
+
k => k.toLowerCase().replace(/[\s_-]+/g, '') === key,
|
|
1025
|
+
);
|
|
1026
|
+
if (preset) s.value = preset;
|
|
1027
|
+
// Each family is quoted whole, or a name that may hold an apostrophe
|
|
1028
|
+
// (O'Reilly Sans); the dial quotes it when it writes it.
|
|
1029
|
+
const family = (/** @type {string} */ f) =>
|
|
1030
|
+
/^"[^"]+"$/.test(f) || /^'[^']+'$/.test(f) || /^[^"',\s][^"]*$/.test(f);
|
|
616
1031
|
if (
|
|
617
1032
|
!s.value ||
|
|
618
1033
|
/[;{}<>\\\n]/.test(s.value) ||
|
|
619
|
-
|
|
620
|
-
(s.value.match(/'/g) ?? []).length % 2
|
|
1034
|
+
!s.value.split(',').every(f => family(f.trim()))
|
|
621
1035
|
)
|
|
622
1036
|
refuse(
|
|
623
1037
|
E.ERR_THEME_BAD_VALUE,
|
|
@@ -658,7 +1072,7 @@ export function checkSetting(
|
|
|
658
1072
|
refuse(
|
|
659
1073
|
E.ERR_THEME_PRIVATE_TOKEN,
|
|
660
1074
|
`"${thing}" is private to a component; themes cannot set it.`,
|
|
661
|
-
{run: [`${HOW}
|
|
1075
|
+
{run: [`${HOW} ${q(thing)}`]},
|
|
662
1076
|
);
|
|
663
1077
|
if (!(thing in TOKENS)) {
|
|
664
1078
|
refuse(E.ERR_THEME_UNKNOWN_TOKEN, `"${thing}" is not a design token.`, {
|
|
@@ -744,7 +1158,11 @@ export function checkSetting(
|
|
|
744
1158
|
.filter(([, r]) => !r)
|
|
745
1159
|
.map(([t]) => t)
|
|
746
1160
|
.join(', ')} read ${thing}`;
|
|
747
|
-
} else if (
|
|
1161
|
+
} else if (
|
|
1162
|
+
recorded.length &&
|
|
1163
|
+
hard.length === recorded.length &&
|
|
1164
|
+
!statusTwin(C, recorded, thing, s.when)
|
|
1165
|
+
) {
|
|
748
1166
|
const [t0, r0] =
|
|
749
1167
|
/** @type {[string, import('./_compiler.mjs').CheckFailure]} */ (
|
|
750
1168
|
hard[0]
|
|
@@ -784,8 +1202,12 @@ export function checkSetting(
|
|
|
784
1202
|
s.note = `no part reads ${thing} itself, but ${fed.slice(0, 4).join(', ')} ${fed.length > 1 ? 'are' : 'is'} built from it, so the change shows wherever ${fed.length > 1 ? 'those are' : 'that is'} used${fed.some(k => k.startsWith('--color-syntax-')) ? ' (such as diff rows in code blocks)' : ''}`;
|
|
785
1203
|
else if (r && dialWrites(thing))
|
|
786
1204
|
s.note = `no part of this Core reads ${thing}, and no token it feeds is read, so it changes nothing you see now; it is set as asked because a dial writes it too`;
|
|
1205
|
+
// The editor's record of what parts read comes from one render of
|
|
1206
|
+
// each part, so it misses variants it did not render: a whole-theme
|
|
1207
|
+
// token is set as asked, and the result says nothing was seen
|
|
1208
|
+
// reading it.
|
|
787
1209
|
else if (r)
|
|
788
|
-
|
|
1210
|
+
s.note = `no part the editor rendered reads ${thing} (each part is rendered once, with its default props, so a variant that reads it may be missed); it is set as asked, and reaches any part that reads it`;
|
|
789
1211
|
}
|
|
790
1212
|
return s;
|
|
791
1213
|
}
|
|
@@ -797,7 +1219,7 @@ export function checkSetting(
|
|
|
797
1219
|
`${prop} is a CSS property, so it needs --on <part|group>. For the whole theme use a dial or a token.`,
|
|
798
1220
|
{
|
|
799
1221
|
run: [
|
|
800
|
-
`${P} set ${slug} ${prop} ${q(v ||
|
|
1222
|
+
`${P} set ${slug} ${prop} ${q(v || exampleOf(groupOfPropType(prop), prop))} --on card`,
|
|
801
1223
|
`${HOW} "${prop}"`,
|
|
802
1224
|
],
|
|
803
1225
|
},
|
|
@@ -805,13 +1227,42 @@ export function checkSetting(
|
|
|
805
1227
|
s.source = 'property';
|
|
806
1228
|
s.thing = prop;
|
|
807
1229
|
if (unset) return s;
|
|
1230
|
+
// One mode on a part whose variants each read their own value: the
|
|
1231
|
+
// other mode cannot keep each variant's value in one rule.
|
|
1232
|
+
if (s.mode && !s.when) {
|
|
1233
|
+
const g = C.groupOfProp(
|
|
1234
|
+
prop === 'background' ? 'background-color' : prop,
|
|
1235
|
+
);
|
|
1236
|
+
for (const t of partsOf(ctx, s)) {
|
|
1237
|
+
const reads = g
|
|
1238
|
+
? C.partReads(t, g).filter(r => r.props.includes(prop))
|
|
1239
|
+
: [];
|
|
1240
|
+
const variants = reads
|
|
1241
|
+
.flatMap(r => r.when)
|
|
1242
|
+
.filter(w => /variant=/.test(w));
|
|
1243
|
+
if (
|
|
1244
|
+
reads.length &&
|
|
1245
|
+
!reads.some(r => r.when.includes('')) &&
|
|
1246
|
+
variants.length
|
|
1247
|
+
)
|
|
1248
|
+
refuse(
|
|
1249
|
+
E.ERR_THEME_NO_MODE,
|
|
1250
|
+
`Each variant of ${t} has its own ${prop}, so one mode cannot be set for all of them at once; name the variant.`,
|
|
1251
|
+
{
|
|
1252
|
+
run: [
|
|
1253
|
+
`${P} set ${slug} ${prop} ${q(v)} --on ${s.on} --when ${variants[0].split(',')[0].replace('=', ':')} --mode ${s.mode}`,
|
|
1254
|
+
],
|
|
1255
|
+
},
|
|
1256
|
+
);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
808
1259
|
if (!isSafeValue(v))
|
|
809
1260
|
refuse(
|
|
810
1261
|
E.ERR_THEME_BAD_VALUE,
|
|
811
1262
|
`"${v}" cannot be a CSS value: it has a ; { } < > or a newline, a comment, !important, or an unclosed bracket or quote.`,
|
|
812
1263
|
{run: [`${HOW} ${prop}`]},
|
|
813
1264
|
);
|
|
814
|
-
if (/radius$/.test(prop)) refuseInvisibleCorners(ctx, slug, s);
|
|
1265
|
+
if (/radius$/.test(prop)) refuseInvisibleCorners(ctx, slug, s, data);
|
|
815
1266
|
if (step)
|
|
816
1267
|
refuse(
|
|
817
1268
|
E.ERR_THEME_BAD_VALUE,
|
|
@@ -866,6 +1317,22 @@ export function checkSetting(
|
|
|
866
1317
|
/** @type {((v: string, on: string | undefined) => string[]) | undefined} */ (
|
|
867
1318
|
THING_HINTS[thing.toLowerCase()]
|
|
868
1319
|
);
|
|
1320
|
+
// A dial by another name or case: Accent, gray, saturation, error.
|
|
1321
|
+
const lower = thing.toLowerCase();
|
|
1322
|
+
const aliases = DIALS[lower] ? [lower] : (COLOR_DIAL_WORDS.get(lower) ?? []);
|
|
1323
|
+
if (!hint && aliases.length)
|
|
1324
|
+
return refuse(
|
|
1325
|
+
E.ERR_THEME_UNKNOWN_THING,
|
|
1326
|
+
`"${thing}" is not a dial, a token, or a CSS property. ${aliases.length > 1 ? `The dials ${aliases.join(' and ')} mean that` : `The dial for that is ${aliases[0]}`}.`,
|
|
1327
|
+
{
|
|
1328
|
+
didYouMean: aliases,
|
|
1329
|
+
run: aliases.map(d =>
|
|
1330
|
+
unset
|
|
1331
|
+
? `${P} undo ${slug} ${d}${onFlag}`
|
|
1332
|
+
: `${P} set ${slug} ${d} ${q(valueFor(d, v))}${onFlag}`,
|
|
1333
|
+
),
|
|
1334
|
+
},
|
|
1335
|
+
);
|
|
869
1336
|
return refuse(
|
|
870
1337
|
E.ERR_THEME_UNKNOWN_THING,
|
|
871
1338
|
`"${thing}" is not a dial, a token, or a CSS property.`,
|
|
@@ -886,11 +1353,20 @@ export function checkSetting(
|
|
|
886
1353
|
* @param {import('./_settings.mjs').Ctx} ctx
|
|
887
1354
|
* @param {string} slug
|
|
888
1355
|
* @param {import('./_settings.mjs').Setting} s
|
|
1356
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
889
1357
|
*/
|
|
890
|
-
function refuseInvisibleCorners(ctx, slug, s) {
|
|
1358
|
+
function refuseInvisibleCorners(ctx, slug, s, data) {
|
|
891
1359
|
if (ctx.unchecked) return;
|
|
892
1360
|
const parts = partsOf(ctx, s);
|
|
893
1361
|
if (!parts.length || parts.some(t => ctx.C.showsCorners(t))) return;
|
|
1362
|
+
// An edge the theme gives the part itself shows its corners too.
|
|
1363
|
+
const edged = data.settings.some(
|
|
1364
|
+
x =>
|
|
1365
|
+
x.source === 'property' &&
|
|
1366
|
+
/^(background|border|box-shadow|outline)/.test(x.thing) &&
|
|
1367
|
+
partsOf(ctx, x).some(t => parts.includes(t)),
|
|
1368
|
+
);
|
|
1369
|
+
if (edged) return;
|
|
894
1370
|
const kids = (ctx.C.MAP[parts[0]]?.contains ?? [])
|
|
895
1371
|
.filter(k => ctx.C.showsCorners(k) && ctx.C.MAP[k])
|
|
896
1372
|
.slice(0, 3);
|
|
@@ -907,6 +1383,41 @@ function refuseInvisibleCorners(ctx, slug, s) {
|
|
|
907
1383
|
);
|
|
908
1384
|
}
|
|
909
1385
|
|
|
1386
|
+
/**
|
|
1387
|
+
* Whether a token is the twin of one the part reads for another variant
|
|
1388
|
+
* value: the editor rendered each part once (a banner as info, an icon in
|
|
1389
|
+
* its default color), so `--color-error-muted` on a banner, or
|
|
1390
|
+
* `--color-icon-pink` on an icon, is read as surely as the recorded
|
|
1391
|
+
* `--color-info-muted` or `--color-icon-blue`.
|
|
1392
|
+
* @param {import('./_compiler.mjs').Compiler} C
|
|
1393
|
+
* @param {Array<[string, unknown]>} recorded
|
|
1394
|
+
* @param {string} thing
|
|
1395
|
+
* @param {string | undefined} when
|
|
1396
|
+
*/
|
|
1397
|
+
function statusTwin(C, recorded, thing, when) {
|
|
1398
|
+
const [wp, wv] = (when ?? '').split(':');
|
|
1399
|
+
return recorded.some(([t]) =>
|
|
1400
|
+
Object.entries(C.variantValues(t)).some(([p, values]) => {
|
|
1401
|
+
if (wp && wp !== p) return false;
|
|
1402
|
+
// Only the part's own values: a button's destructive variant makes no
|
|
1403
|
+
// --color-info its twin.
|
|
1404
|
+
const all = values;
|
|
1405
|
+
return all.some(
|
|
1406
|
+
w =>
|
|
1407
|
+
(!wv || w === wv) &&
|
|
1408
|
+
new RegExp(`-${w}(-|$)`).test(thing) &&
|
|
1409
|
+
all.some(
|
|
1410
|
+
o =>
|
|
1411
|
+
o !== w &&
|
|
1412
|
+
C.allReads(t).has(
|
|
1413
|
+
thing.replace(new RegExp(`-${w}(-|$)`), `-${o}$1`),
|
|
1414
|
+
),
|
|
1415
|
+
),
|
|
1416
|
+
);
|
|
1417
|
+
}),
|
|
1418
|
+
);
|
|
1419
|
+
}
|
|
1420
|
+
|
|
910
1421
|
/**
|
|
911
1422
|
* Whether a dial writes the token: a color, a heading weight (type,
|
|
912
1423
|
* headings), a radius (corners), a spacing or control size (spacing), or a
|
|
@@ -919,478 +1430,5 @@ const dialWrites = token =>
|
|
|
919
1430
|
token,
|
|
920
1431
|
);
|
|
921
1432
|
|
|
922
|
-
/** Hues a question can name, each with or without light, dark or pale. */
|
|
923
|
-
const HUES = [
|
|
924
|
-
'gray',
|
|
925
|
-
'grey',
|
|
926
|
-
'red',
|
|
927
|
-
'blue',
|
|
928
|
-
'green',
|
|
929
|
-
'yellow',
|
|
930
|
-
'orange',
|
|
931
|
-
'purple',
|
|
932
|
-
'pink',
|
|
933
|
-
'navy',
|
|
934
|
-
];
|
|
935
|
-
/** Color words that can name a part's fill, longest first. */
|
|
936
|
-
const COLOR_WORDS = [
|
|
937
|
-
...HUES.flatMap(h => ['light', 'dark', 'pale'].map(s => `${s} ${h}`)),
|
|
938
|
-
...HUES,
|
|
939
|
-
'white',
|
|
940
|
-
'black',
|
|
941
|
-
'cream',
|
|
942
|
-
'beige',
|
|
943
|
-
];
|
|
944
|
-
/** Fills for the plain grays, so the command has a real value. */
|
|
945
|
-
const NAMED_FILLS = /** @type {Record<string, string>} */ ({
|
|
946
|
-
'light gray': '#e5e7eb',
|
|
947
|
-
'light grey': '#e5e7eb',
|
|
948
|
-
gray: '#9ca3af',
|
|
949
|
-
grey: '#9ca3af',
|
|
950
|
-
'dark gray': '#374151',
|
|
951
|
-
'dark grey': '#374151',
|
|
952
|
-
'light blue': '#dbeafe',
|
|
953
|
-
'pale blue': '#eff6ff',
|
|
954
|
-
white: '#ffffff',
|
|
955
|
-
black: '#000000',
|
|
956
|
-
});
|
|
957
|
-
/** Nouns for parts that have a fill. */
|
|
958
|
-
const PART_NOUNS = [
|
|
959
|
-
'button',
|
|
960
|
-
'card',
|
|
961
|
-
'panel',
|
|
962
|
-
'banner',
|
|
963
|
-
'header',
|
|
964
|
-
'input',
|
|
965
|
-
'field',
|
|
966
|
-
'badge',
|
|
967
|
-
'tab',
|
|
968
|
-
'chip',
|
|
969
|
-
'pill',
|
|
970
|
-
'toast',
|
|
971
|
-
'tooltip',
|
|
972
|
-
'dialog',
|
|
973
|
-
'modal',
|
|
974
|
-
'sidebar',
|
|
975
|
-
'row',
|
|
976
|
-
];
|
|
977
|
-
|
|
978
|
-
/**
|
|
979
|
-
* Answer a plain-words question with exact commands: the parts it names,
|
|
980
|
-
* the aspects it asks about, and what each part really reads for them.
|
|
981
|
-
* @param {import('./_settings.mjs').Ctx} ctx
|
|
982
|
-
* @param {string} question
|
|
983
|
-
* @returns {{parts: string[], aspects: string[], results: Array<{text: string, commands: string[]}>}}
|
|
984
|
-
*/
|
|
985
|
-
export function answerHow(ctx, question) {
|
|
986
|
-
const {C} = ctx;
|
|
987
|
-
const TARGET = ctx.project.target;
|
|
988
|
-
const TOKENS = ctx.project.core.tokenDefaults;
|
|
989
|
-
const text = question.toLowerCase();
|
|
990
|
-
/** @type {Array<string | null>} */
|
|
991
|
-
let intents = INTENTS.filter(([, re]) => re.test(text)).map(([g]) => g);
|
|
992
|
-
// "pill" is a corner shape when the question is about corners.
|
|
993
|
-
const partText = intents.includes('corners')
|
|
994
|
-
? text.replace(/\bpills?\b/g, '')
|
|
995
|
-
: text;
|
|
996
|
-
/** @type {string[]} */
|
|
997
|
-
let parts = [];
|
|
998
|
-
for (const [re, ts] of PART_WORDS)
|
|
999
|
-
if (re.test(partText))
|
|
1000
|
-
for (const t of ts) if (!parts.includes(t)) parts.push(t);
|
|
1001
|
-
// A variant of a part ("secondary button") answers for the part.
|
|
1002
|
-
parts = parts.filter(p => !parts.some(x => x.startsWith(`${p}:`)));
|
|
1003
|
-
const whiteText =
|
|
1004
|
-
/\bwhite (text|labels?|writing|words)\b|\b(text|labels?|writing|words) (in |is |are )?white\b/.test(
|
|
1005
|
-
text,
|
|
1006
|
-
);
|
|
1007
|
-
// A color word right before a part ("light gray button") is the part's
|
|
1008
|
-
// fill, so it keeps the background aspect beside text.
|
|
1009
|
-
const fillMatch =
|
|
1010
|
-
new RegExp(
|
|
1011
|
-
`\\b(${COLOR_WORDS.join('|')})\\s+(?:${PART_NOUNS.join('|')})s?\\b`,
|
|
1012
|
-
).exec(text) ??
|
|
1013
|
-
new RegExp(
|
|
1014
|
-
`\\b(?:${PART_NOUNS.join('|')})s?\\s+(?:with\\s+an?\\s+|in\\s+|is\\s+)?(${COLOR_WORDS.join('|')})\\b(?!\\s+(?:text|labels?|writing|words))`,
|
|
1015
|
-
).exec(text);
|
|
1016
|
-
const fillWord = Boolean(fillMatch);
|
|
1017
|
-
// A fill named outright ("a blue tint") keeps the background aspect.
|
|
1018
|
-
const namedFill =
|
|
1019
|
-
/background|fill|gradient|glossy|\btint(ed)?\b|\bbg\b/.test(text) ||
|
|
1020
|
-
fillWord;
|
|
1021
|
-
if (
|
|
1022
|
-
intents.includes('text') &&
|
|
1023
|
-
intents.includes('background') &&
|
|
1024
|
-
!namedFill &&
|
|
1025
|
-
!(/\bwhite\b/.test(text) && !whiteText)
|
|
1026
|
-
)
|
|
1027
|
-
intents = intents.filter(g => g !== 'background');
|
|
1028
|
-
// "border color" or "a gray border" is the border's color, not a fill; a
|
|
1029
|
-
// color word for anything else ("a dark card with a border", "the card
|
|
1030
|
-
// color and its border", "colored buttons") is the fill's.
|
|
1031
|
-
const fillWords = [...COLOR_WORDS, 'dark', 'light', 'colou?r(ed)?'].join('|');
|
|
1032
|
-
// Take out every color word that belongs to the border, the text or the
|
|
1033
|
-
// mode ("a light gray border", "the color of the border", "in dark
|
|
1034
|
-
// mode"); a color word left over is the fill's.
|
|
1035
|
-
const rest = text
|
|
1036
|
-
.replace(/\b(dark|light) (mode|theme)\b/g, ' ')
|
|
1037
|
-
.replace(
|
|
1038
|
-
/\bcolou?r of (the |its |a )?(border|outline|stroke|edge|text|labels?)s?\b/g,
|
|
1039
|
-
' ',
|
|
1040
|
-
)
|
|
1041
|
-
.replace(/\b(border|outline|stroke|edge|text)s? colou?r\b/g, ' ')
|
|
1042
|
-
.replace(
|
|
1043
|
-
new RegExp(
|
|
1044
|
-
`\\b((${fillWords}|thin|thick|subtle|bold)\\s+)+(border|outline|stroke|edge|text|labels?)s?\\b`,
|
|
1045
|
-
'g',
|
|
1046
|
-
),
|
|
1047
|
-
' ',
|
|
1048
|
-
);
|
|
1049
|
-
const looseColor = new RegExp(`\\b(${fillWords})\\b`).test(rest);
|
|
1050
|
-
if (
|
|
1051
|
-
intents.includes('border') &&
|
|
1052
|
-
intents.includes('background') &&
|
|
1053
|
-
!namedFill &&
|
|
1054
|
-
!looseColor
|
|
1055
|
-
)
|
|
1056
|
-
intents = intents.filter(g => g !== 'background');
|
|
1057
|
-
if (!intents.length && parts.length) intents = [null];
|
|
1058
|
-
const design = Object.keys(DESIGN).filter(w => text.includes(w));
|
|
1059
|
-
const tracking = /letter ?spacing|tracking|kerning/.test(text);
|
|
1060
|
-
// Letter spacing is a font answer, so a spacing intent it raised goes.
|
|
1061
|
-
if (tracking && intents.includes('font'))
|
|
1062
|
-
intents = intents.filter(g => g !== 'spacing');
|
|
1063
|
-
const weight = /\b(bold|weight|heav\w*|light(er)?|thin)\b/.test(text);
|
|
1064
|
-
const px = /\b(\d+(?:\.\d+)?px)\b/.exec(text)?.[1];
|
|
1065
|
-
/** @type {Array<{text: string, commands: string[]}>} */
|
|
1066
|
-
const results = [];
|
|
1067
|
-
/** @param {string | null} g */
|
|
1068
|
-
const V = g => {
|
|
1069
|
-
const bg = design.find(d => ['gradient', 'glossy'].includes(d));
|
|
1070
|
-
const sh = design.find(d =>
|
|
1071
|
-
['bevel', 'raised', 'flat', 'sunken'].includes(d),
|
|
1072
|
-
);
|
|
1073
|
-
if (g === 'background' && bg) return DESIGN[bg];
|
|
1074
|
-
if (g === 'shadow' && sh) return DESIGN[sh];
|
|
1075
|
-
if (g === 'font' && tracking)
|
|
1076
|
-
return /tight/.test(text) ? '-0.02em' : '0.04em';
|
|
1077
|
-
if (g === 'font' && /bold|heav/.test(text)) return '700';
|
|
1078
|
-
if (g === 'font' && /\b(light(er)?|thin)\b/.test(text)) return '300';
|
|
1079
|
-
if (g === 'text' && whiteText) return '"#ffffff"';
|
|
1080
|
-
if (g === 'text' && /\bdark (text|labels?|writing|words)\b/.test(text))
|
|
1081
|
-
return '"#111827"';
|
|
1082
|
-
if (g === 'background' && fillMatch) {
|
|
1083
|
-
const named = NAMED_FILLS[fillMatch[1]];
|
|
1084
|
-
if (named) return `"${named}"`;
|
|
1085
|
-
}
|
|
1086
|
-
if (g === 'border')
|
|
1087
|
-
return `"${/\bthick\b/.test(text) ? '2px' : '1px'} solid <color>"`;
|
|
1088
|
-
if (g === 'text' && /\baccent[- ]?(colou?red )?(text|labels?)\b/.test(text))
|
|
1089
|
-
return '"var(--color-text-accent)"';
|
|
1090
|
-
if (g === 'background' && /\bwhite\b/.test(text) && !whiteText)
|
|
1091
|
-
return '"#ffffff"';
|
|
1092
|
-
return '<value>';
|
|
1093
|
-
};
|
|
1094
|
-
/**
|
|
1095
|
-
* A value for one token, of its own type: a whole border value suits only
|
|
1096
|
-
* the border property, so a color token takes a color and a width token a
|
|
1097
|
-
* width.
|
|
1098
|
-
* @param {string} token @param {string} group
|
|
1099
|
-
*/
|
|
1100
|
-
const tokenValue = (token, group) => {
|
|
1101
|
-
const v = V(group);
|
|
1102
|
-
const whole = /\s(solid|dashed|dotted)\s/.test(v);
|
|
1103
|
-
if (!whole) return v;
|
|
1104
|
-
const type = C.typeOf(token);
|
|
1105
|
-
if (type === 'color') return '"<color>"';
|
|
1106
|
-
if (type === 'size' || /width/.test(token))
|
|
1107
|
-
return /\b2px\b/.test(v) ? '2px' : '1px';
|
|
1108
|
-
return '<value>';
|
|
1109
|
-
};
|
|
1110
|
-
for (const raw of parts.slice(0, 3)) {
|
|
1111
|
-
let [part, wp, wv] = raw.split(':');
|
|
1112
|
-
const named = Boolean(wp);
|
|
1113
|
-
const one = FAMILIES[part] ? FAMILIES[part][0] : part;
|
|
1114
|
-
if (!TARGET.has(one)) continue;
|
|
1115
|
-
// A size in the question ("small buttons") is a --when condition.
|
|
1116
|
-
if (!wp) {
|
|
1117
|
-
const sizes = C.variantValues(one).size ?? [];
|
|
1118
|
-
const word = /\b(small|sm)\b/.test(text)
|
|
1119
|
-
? ['sm', 'small']
|
|
1120
|
-
: /\b(large|lg|big)\b/.test(text)
|
|
1121
|
-
? ['lg', 'large']
|
|
1122
|
-
: /\b(medium|md)\b/.test(text)
|
|
1123
|
-
? ['md', 'medium']
|
|
1124
|
-
: [];
|
|
1125
|
-
const hit = word.find(w => sizes.includes(w));
|
|
1126
|
-
if (hit) [wp, wv] = ['size', hit];
|
|
1127
|
-
}
|
|
1128
|
-
const when = wp ? ` --when ${wp}:${wv}` : '';
|
|
1129
|
-
// A part with variants: a change without --when reaches every variant.
|
|
1130
|
-
const variants = Object.entries(C.variantValues(one)).filter(
|
|
1131
|
-
([p, vals]) =>
|
|
1132
|
-
['status', 'variant', 'tone', 'color'].includes(p) && vals.length > 1,
|
|
1133
|
-
);
|
|
1134
|
-
for (const g of intents.slice(0, 3)) {
|
|
1135
|
-
if (g === null) {
|
|
1136
|
-
results.push({
|
|
1137
|
-
text: `what ${one} reads`,
|
|
1138
|
-
commands: [`${HOW} ${one}`],
|
|
1139
|
-
});
|
|
1140
|
-
continue;
|
|
1141
|
-
}
|
|
1142
|
-
if (g === 'corners' && !named) {
|
|
1143
|
-
// Each clause with its own size ("small 6px, large stay pill").
|
|
1144
|
-
const sizes = C.variantValues(one).size ?? [];
|
|
1145
|
-
const clauses = text
|
|
1146
|
-
.split(/,|;|\bbut\b|\band\b/)
|
|
1147
|
-
.map(c => {
|
|
1148
|
-
const size = /\b(small|sm)\b/.test(c)
|
|
1149
|
-
? 'sm'
|
|
1150
|
-
: /\b(large|lg|big)\b/.test(c)
|
|
1151
|
-
? 'lg'
|
|
1152
|
-
: /\b(medium|md)\b/.test(c)
|
|
1153
|
-
? 'md'
|
|
1154
|
-
: null;
|
|
1155
|
-
const value =
|
|
1156
|
-
/\b(\d+(?:\.\d+)?px)\b/.exec(c)?.[1] ??
|
|
1157
|
-
(/pill|circ/.test(c)
|
|
1158
|
-
? 'pill'
|
|
1159
|
-
: /sharp|square/.test(c)
|
|
1160
|
-
? 'square'
|
|
1161
|
-
: null);
|
|
1162
|
-
return {size, value};
|
|
1163
|
-
})
|
|
1164
|
-
.filter(c => c.size && sizes.includes(c.size) && c.value);
|
|
1165
|
-
if (clauses.length > 1) {
|
|
1166
|
-
results.push({
|
|
1167
|
-
text: `corners on ${part}, by size`,
|
|
1168
|
-
commands: clauses.map(c =>
|
|
1169
|
-
/px$/.test(/** @type {string} */ (c.value))
|
|
1170
|
-
? `${P} set <theme> border-radius ${c.value} --on ${part} --when size:${c.size}`
|
|
1171
|
-
: `${P} set <theme> corners ${c.value} --on ${part} --when size:${c.size}`,
|
|
1172
|
-
),
|
|
1173
|
-
});
|
|
1174
|
-
continue;
|
|
1175
|
-
}
|
|
1176
|
-
}
|
|
1177
|
-
if (g === 'corners') {
|
|
1178
|
-
results.push({
|
|
1179
|
-
text: `corners on ${part}${when ? ` (${wp} ${wv})` : ''}`,
|
|
1180
|
-
commands: [
|
|
1181
|
-
`${P} set <theme> corners ${/pill|circ/.test(text) ? 'pill' : /sharp|square/.test(text) ? 'square' : 'round'} --on ${part}${when}`,
|
|
1182
|
-
`${P} set <theme> border-radius ${px ?? '6px'} --on ${part}${when}`,
|
|
1183
|
-
],
|
|
1184
|
-
});
|
|
1185
|
-
continue;
|
|
1186
|
-
}
|
|
1187
|
-
if (g === 'shadow') {
|
|
1188
|
-
const shadows = Object.keys(ctx.project.core.tokenDefaults).filter(k =>
|
|
1189
|
-
/^--shadow-(low|med|high)$/.test(k),
|
|
1190
|
-
);
|
|
1191
|
-
const value =
|
|
1192
|
-
V('shadow') !== '<value>'
|
|
1193
|
-
? V('shadow')
|
|
1194
|
-
: shadows.length
|
|
1195
|
-
? `"var(${/soft|subtle|light/.test(text) ? shadows[0] : /strong|deep|heavy/.test(text) ? shadows[shadows.length - 1] : shadows[Math.min(1, shadows.length - 1)]})"`
|
|
1196
|
-
: '"0 2px 6px rgb(0 0 0 / 0.2)"';
|
|
1197
|
-
results.push({
|
|
1198
|
-
text: `shadow on ${part}${when ? ` (${wp} ${wv})` : ''}: box-shadow${shadows.length ? `; the theme's shadow tokens are ${shadows.join(', ')}` : ''}`,
|
|
1199
|
-
commands: [
|
|
1200
|
-
`${P} set <theme> box-shadow ${value} --on ${part}${when}`,
|
|
1201
|
-
...(shadows.length
|
|
1202
|
-
? [
|
|
1203
|
-
`${P} set <theme> ${shadows[0]} "0 1px 3px rgb(0 0 0 / 0.12)" # change the shared shadow everywhere`,
|
|
1204
|
-
]
|
|
1205
|
-
: []),
|
|
1206
|
-
],
|
|
1207
|
-
});
|
|
1208
|
-
continue;
|
|
1209
|
-
}
|
|
1210
|
-
if (g === 'spacing' && !tracking) {
|
|
1211
|
-
results.push({
|
|
1212
|
-
text: `room inside ${part}`,
|
|
1213
|
-
commands: [
|
|
1214
|
-
`${P} set <theme> spacing ${/dense|compact|tight|cramped/.test(text) ? 'compact' : 'roomy'} --on ${part}`,
|
|
1215
|
-
`${P} set <theme> padding ${px ?? '12px'} --on ${part}${when}`,
|
|
1216
|
-
],
|
|
1217
|
-
});
|
|
1218
|
-
continue;
|
|
1219
|
-
}
|
|
1220
|
-
const group = g === 'spacing' ? 'font' : g;
|
|
1221
|
-
if (group === 'font' && tracking && weight)
|
|
1222
|
-
results.push({
|
|
1223
|
-
text: `${part} weight: font-weight on the part${part === 'heading' ? ', or the headings dial for every heading' : ''}`,
|
|
1224
|
-
commands: [
|
|
1225
|
-
`${P} set <theme> font-weight ${/\b(light(er)?|thin)\b/.test(text) ? 300 : 700} --on ${part}${when}`,
|
|
1226
|
-
...(part === 'heading'
|
|
1227
|
-
? [
|
|
1228
|
-
`${P} set <theme> headings ${/\b(light(er)?|thin)\b/.test(text) ? 'light' : 'bold'}`,
|
|
1229
|
-
]
|
|
1230
|
-
: []),
|
|
1231
|
-
],
|
|
1232
|
-
});
|
|
1233
|
-
const reads = C.partReads(one, group);
|
|
1234
|
-
const filtered = wv
|
|
1235
|
-
? reads.filter(
|
|
1236
|
-
r =>
|
|
1237
|
-
r.when.some(w => w.includes(`${wp}=${wv}`)) ||
|
|
1238
|
-
r.when.includes(''),
|
|
1239
|
-
)
|
|
1240
|
-
: reads;
|
|
1241
|
-
// Weight and tracking questions name only the tokens for them.
|
|
1242
|
-
const aspect =
|
|
1243
|
-
group === 'font' && tracking
|
|
1244
|
-
? /letter|tracking/
|
|
1245
|
-
: group === 'font' && weight
|
|
1246
|
-
? /weight/
|
|
1247
|
-
: null;
|
|
1248
|
-
const fitting = aspect
|
|
1249
|
-
? (filtered.length ? filtered : reads).filter(r => aspect.test(r.token))
|
|
1250
|
-
: filtered.length
|
|
1251
|
-
? filtered
|
|
1252
|
-
: reads;
|
|
1253
|
-
// Text with no color token of its own takes the page's text color.
|
|
1254
|
-
const inherited =
|
|
1255
|
-
!fitting.length && group === 'text' ? '--color-text-primary' : null;
|
|
1256
|
-
const list = fitting.slice(0, 4);
|
|
1257
|
-
const direct =
|
|
1258
|
-
group === 'font' && tracking
|
|
1259
|
-
? 'letter-spacing'
|
|
1260
|
-
: group === 'font' && /bold|weight|heav/.test(text)
|
|
1261
|
-
? 'font-weight'
|
|
1262
|
-
: group === 'font'
|
|
1263
|
-
? 'font-family'
|
|
1264
|
-
: DIRECT[group];
|
|
1265
|
-
const restingBorder =
|
|
1266
|
-
group === 'border' && list.every(r => r.when.some(w => /:/.test(w)));
|
|
1267
|
-
const tokenRoute =
|
|
1268
|
-
(group === 'text' || group === 'background' || group === 'border') &&
|
|
1269
|
-
!restingBorder &&
|
|
1270
|
-
!/gradient|inset/.test(V(group));
|
|
1271
|
-
results.push({
|
|
1272
|
-
text: `${part} ${aspect ? (tracking ? 'letter spacing' : 'weight') : group}${when ? ` (${wp} ${wv})` : ''}: ${restingBorder ? `the resting border is the border property (no token sets it)${list.length ? `; in states it reads ${list.map(r => r.token + C.fmtWhen(r.when)).join(', ')}` : ''}` : list.length ? `comes from ${list.map(r => r.token + C.fmtWhen(r.when) + C.statusNote(one, r.token)).join(', ')}` : inherited ? `inherits the page's text color (${inherited} for the whole theme); set the property for this part alone` : 'no token of its own; set the property directly'}`,
|
|
1273
|
-
commands: [
|
|
1274
|
-
`${P} set <theme> ${direct} ${V(group)} --on ${part}${when} # direct: always works`,
|
|
1275
|
-
...(inherited
|
|
1276
|
-
? [`${P} set <theme> ${inherited} ${V(group)} # the whole theme`]
|
|
1277
|
-
: []),
|
|
1278
|
-
...(tokenRoute
|
|
1279
|
-
? list
|
|
1280
|
-
.slice(0, 2)
|
|
1281
|
-
.map(
|
|
1282
|
-
r =>
|
|
1283
|
-
`${P} set <theme> ${r.token} ${tokenValue(r.token, group)} --on ${part} # via the token${C.fmtWhen(r.when)}`,
|
|
1284
|
-
)
|
|
1285
|
-
: []),
|
|
1286
|
-
],
|
|
1287
|
-
});
|
|
1288
|
-
if (
|
|
1289
|
-
!when &&
|
|
1290
|
-
variants.length &&
|
|
1291
|
-
['text', 'background', 'border'].includes(group)
|
|
1292
|
-
) {
|
|
1293
|
-
const [vp, vals] = variants[0];
|
|
1294
|
-
results.push({
|
|
1295
|
-
text: `${part} has ${vp} ${vals.join('|')}: without --when the change reaches every ${vp}. To change one, add --when ${vp}:<value>.`,
|
|
1296
|
-
commands: [
|
|
1297
|
-
`${P} set <theme> ${direct} ${V(group)} --on ${part} --when ${vp}:${vals[0]}`,
|
|
1298
|
-
],
|
|
1299
|
-
});
|
|
1300
|
-
}
|
|
1301
|
-
const kids = (C.MAP[one]?.contains ?? [])
|
|
1302
|
-
.filter(
|
|
1303
|
-
k =>
|
|
1304
|
-
C.partReads(k, group).length && !(FAMILIES[part] ?? []).includes(k),
|
|
1305
|
-
)
|
|
1306
|
-
.slice(0, 3);
|
|
1307
|
-
if (kids.length && group === 'text') {
|
|
1308
|
-
results.push({
|
|
1309
|
-
text: `parts inside ${part} with their own text color: ${kids.join(', ')}. A text token set --on ${part} reaches them; a color property does not.`,
|
|
1310
|
-
commands: kids.slice(0, 1).map(k => `${HOW} ${k}`),
|
|
1311
|
-
});
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
}
|
|
1315
|
-
if (!parts.length) {
|
|
1316
|
-
if (tracking)
|
|
1317
|
-
results.push({
|
|
1318
|
-
text: 'letter spacing is a CSS property on a part; for headings there is a dial',
|
|
1319
|
-
commands: [
|
|
1320
|
-
`${P} set <theme> letter-spacing 0.02em --on <part>`,
|
|
1321
|
-
`${P} set <theme> headings tight`,
|
|
1322
|
-
],
|
|
1323
|
-
});
|
|
1324
|
-
for (const g of /** @type {string[]} */ (intents.filter(Boolean)).slice(
|
|
1325
|
-
0,
|
|
1326
|
-
2,
|
|
1327
|
-
)) {
|
|
1328
|
-
const dial = /** @type {Record<string, string>} */ ({
|
|
1329
|
-
corners: 'corners',
|
|
1330
|
-
spacing: 'spacing',
|
|
1331
|
-
font: 'font',
|
|
1332
|
-
size: 'text-size',
|
|
1333
|
-
})[g];
|
|
1334
|
-
if (dial && !(tracking && (g === 'spacing' || g === 'font')))
|
|
1335
|
-
results.push({
|
|
1336
|
-
text: DIALS[dial].help,
|
|
1337
|
-
commands: DIALS[dial].examples.map(e => `${P} ${e}`),
|
|
1338
|
-
});
|
|
1339
|
-
// Text and background colors come from their tokens first; the brand
|
|
1340
|
-
// color after.
|
|
1341
|
-
results.push(...wholeThemeTokens(ctx, g));
|
|
1342
|
-
if (g === 'background' || g === 'text')
|
|
1343
|
-
results.push({
|
|
1344
|
-
text: DIALS.accent.help,
|
|
1345
|
-
commands: DIALS.accent.examples.map(e => `${P} ${e}`),
|
|
1346
|
-
});
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
if (!results.length) {
|
|
1350
|
-
for (const d of Object.values(DIALS)) {
|
|
1351
|
-
if (d.words.split(' ').some(w => w.length > 3 && text.includes(w)))
|
|
1352
|
-
results.push({
|
|
1353
|
-
text: d.help,
|
|
1354
|
-
commands: d.examples.map(e => `${P} ${e}`),
|
|
1355
|
-
});
|
|
1356
|
-
}
|
|
1357
|
-
}
|
|
1358
|
-
void TOKENS;
|
|
1359
|
-
const seen = new Set();
|
|
1360
|
-
return {
|
|
1361
|
-
parts,
|
|
1362
|
-
aspects: /** @type {string[]} */ (intents.filter(Boolean)),
|
|
1363
|
-
results: results
|
|
1364
|
-
.filter(r => {
|
|
1365
|
-
const key = `${r.text}\n${r.commands.join('\n')}`;
|
|
1366
|
-
if (seen.has(key)) return false;
|
|
1367
|
-
seen.add(key);
|
|
1368
|
-
return true;
|
|
1369
|
-
})
|
|
1370
|
-
.slice(0, 10),
|
|
1371
|
-
};
|
|
1372
|
-
}
|
|
1373
|
-
|
|
1374
|
-
/**
|
|
1375
|
-
* The tokens most parts read for one aspect, for a whole-theme change.
|
|
1376
|
-
* @param {import('./_settings.mjs').Ctx} ctx
|
|
1377
|
-
* @param {string} aspect
|
|
1378
|
-
* @returns {Array<{text: string, commands: string[]}>}
|
|
1379
|
-
*/
|
|
1380
|
-
export function wholeThemeTokens(ctx, aspect) {
|
|
1381
|
-
const {C} = ctx;
|
|
1382
|
-
const toks = Object.keys(ctx.project.core.tokenDefaults)
|
|
1383
|
-
.filter(k => C.groupOfToken(k) === aspect && C.anyReader(k).length)
|
|
1384
|
-
.sort((a, b) => C.anyReader(b).length - C.anyReader(a).length)
|
|
1385
|
-
.slice(0, 6);
|
|
1386
|
-
if (!toks.length) return [];
|
|
1387
|
-
return [
|
|
1388
|
-
{
|
|
1389
|
-
text: `whole-theme tokens for ${aspect}, most used first: ${toks.map(k => `${k} (${C.anyReader(k).length} parts)`).join(', ')}`,
|
|
1390
|
-
commands: [`${P} set <theme> ${toks[0]} <value>`],
|
|
1391
|
-
},
|
|
1392
|
-
];
|
|
1393
|
-
}
|
|
1394
|
-
|
|
1395
1433
|
/** The aspects `how` answers at the whole-theme level. */
|
|
1396
1434
|
export const ASPECTS = [...Object.keys(GROUPS), 'colors'];
|