@astryxdesign/cli 0.7.14-canary.gfb823d9 → 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/component/_adapter.d.mts +15 -0
- package/api/component/_adapter.mjs +76 -0
- package/api/component/component.mjs +8 -1
- package/api/search/_named-keywords.d.mts +31 -0
- package/api/search/_named-keywords.mjs +177 -0
- package/api/search/search-index.mjs +8 -1
- package/api/search/search.d.mts +6 -1
- package/api/search/search.mjs +49 -8
- 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/palette/generate/color.d.mts +2 -0
- package/api/theme/palette/generate/color.mjs +1 -1
- 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 +36 -2
- package/api/theme-edit/_adapter.mjs +340 -12
- package/api/theme-edit/_check.d.mts +28 -25
- package/api/theme-edit/_check.mjs +876 -308
- package/api/theme-edit/_color.d.mts +53 -4
- package/api/theme-edit/_color.mjs +406 -79
- package/api/theme-edit/_compiler.d.mts +14 -6
- package/api/theme-edit/_compiler.mjs +89 -59
- package/api/theme-edit/_context.d.mts +28 -8
- package/api/theme-edit/_context.mjs +142 -19
- package/api/theme-edit/_edit.d.mts +3 -1
- package/api/theme-edit/_edit.mjs +746 -76
- 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 +27 -8
- package/api/theme-edit/_how.mjs +776 -175
- package/api/theme-edit/_overview.d.mts +9 -0
- package/api/theme-edit/_overview.mjs +296 -0
- package/api/theme-edit/_palette.d.mts +155 -15
- package/api/theme-edit/_palette.mjs +1446 -262
- package/api/theme-edit/_parts.mjs +61 -5
- 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 +32 -11
- package/api/theme-edit/_preview-app.mjs +1976 -285
- package/api/theme-edit/_preview.d.mts +39 -8
- package/api/theme-edit/_preview.mjs +367 -41
- package/api/theme-edit/_settings.d.mts +147 -11
- package/api/theme-edit/_settings.mjs +908 -99
- package/api/theme-edit/_tables.d.mts +4 -16
- package/api/theme-edit/_tables.mjs +51 -117
- package/api/theme-edit/theme-edit.type.d.mts +102 -66
- package/api/theme-edit/theme-edit.type.mjs +58 -40
- package/api/theme-edit/themeEditHow.doc.mjs +17 -6
- package/api/theme-edit/themeEditNew.doc.mjs +21 -5
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +18 -5
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +12 -9
- package/api/theme-edit/themeEditPreviewList.doc.mjs +11 -2
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +29 -3
- package/api/theme-edit/themeEditSet.doc.mjs +14 -8
- package/api/theme-edit/themeEditShow.doc.mjs +10 -5
- package/api/theme-edit/themeEditUndo.doc.mjs +9 -4
- package/assets/discover/official-packages.json +4 -0
- package/assets/docs/tree/icon-sets.doc.mjs +3 -3
- package/assets/docs.generated.json +1 -0
- package/assets/icons/definitions/bookmarkAdd.doc.mjs +9 -0
- package/assets/icons/definitions/documentSearch.doc.mjs +9 -0
- package/assets/icons/definitions/keyboard.doc.mjs +9 -0
- package/assets/icons/definitions/paintBrush.doc.mjs +9 -0
- package/assets/icons/definitions/wallet.doc.mjs +9 -0
- package/assets/preview-app/gitignore +3 -0
- package/assets/templates/blocks/components/ButtonGroup/ButtonGroupShowcase.doc.mjs +1 -1
- package/assets/templates/blocks/components/Lightbox/LightboxGallery.tsx +1 -1
- package/assets/templates/components.generated.json +1 -0
- package/assets/templates/pages/gallery-hero/page.tsx +8 -2
- package/assets/templates/pages/settings-dialog/template.doc.mjs +3 -3
- package/assets/templates/pages/side-gallery/page.tsx +1 -1
- package/assets/templates/pages/table-filter/template.doc.mjs +2 -2
- package/assets/templates/pages/theme-showcase/template.doc.mjs +1 -1
- package/clients/cli/bin/astryx.mjs +37 -5
- package/clients/cli/commands/experimental/theme.mjs +422 -57
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +22 -9
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +103 -4
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +13 -8
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +5 -5
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +5 -5
- package/clients/cli/commands/experimental-theme-editor.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 -8
- 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 +3 -4
- 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 +145 -46
- package/clients/cli/commands.generated.json +1 -0
- package/clients/cli/index.mjs +96 -5
- 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 +29 -4
- package/foundation/discovery/template-icons.d.mts +13 -0
- package/foundation/discovery/template-icons.mjs +76 -0
- 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/foundation/response/error-codes.d.mts +3 -1
- package/foundation/response/error-codes.doc.mjs +13 -1
- package/foundation/response/error-codes.mjs +11 -1
- package/foundation/response/response-types.doc.mjs +6 -6
- package/package.json +6 -7
- package/clients/cli/commands/experimental-theme-palette-generate.doc.mjs +0 -73
- package/clients/cli/commands/experimental-theme-palette.doc.mjs +0 -37
|
@@ -4,39 +4,51 @@
|
|
|
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
|
-
import {
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
import {
|
|
36
|
+
COLOR_NAMES,
|
|
37
|
+
colorProblem,
|
|
38
|
+
isColorValue,
|
|
39
|
+
isNumber,
|
|
40
|
+
isSafeValue,
|
|
41
|
+
isSize,
|
|
42
|
+
parseColor,
|
|
43
|
+
} from './_color.mjs';
|
|
44
|
+
import {
|
|
45
|
+
PALETTE_DIALS,
|
|
46
|
+
PALETTE_TOKENS,
|
|
47
|
+
VIBRANCY_DEFAULT,
|
|
48
|
+
isGraySeed,
|
|
49
|
+
paletteToken,
|
|
50
|
+
seedOf,
|
|
51
|
+
} from './_palette.mjs';
|
|
40
52
|
import {
|
|
41
53
|
camel,
|
|
42
54
|
kebab,
|
|
@@ -49,6 +61,59 @@ import {
|
|
|
49
61
|
|
|
50
62
|
const E = ERROR_CODES;
|
|
51
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
|
+
|
|
52
117
|
/**
|
|
53
118
|
* @typedef {object} SetFlags
|
|
54
119
|
* @property {string} [on]
|
|
@@ -58,28 +123,374 @@ const E = ERROR_CODES;
|
|
|
58
123
|
* @property {boolean} [only]
|
|
59
124
|
*/
|
|
60
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
|
+
|
|
61
195
|
/**
|
|
62
196
|
* Refuse a value that does not parse as the token's type.
|
|
63
197
|
* @param {string} type @param {string} value @param {string} name
|
|
198
|
+
* @param {Record<string, unknown>} TOKENS
|
|
199
|
+
* @param {(token: string) => string} typeOf
|
|
200
|
+
* @param {string} slug
|
|
64
201
|
*/
|
|
65
|
-
function checkTypedValue(type, value, name) {
|
|
66
|
-
|
|
202
|
+
function checkTypedValue(type, value, name, TOKENS, typeOf, slug) {
|
|
203
|
+
/** @param {string} what @param {string} example */
|
|
204
|
+
const bad = (what, example) =>
|
|
67
205
|
refuse(
|
|
68
206
|
E.ERR_THEME_BAD_VALUE,
|
|
69
|
-
`${name} is
|
|
70
|
-
{
|
|
207
|
+
`${name} is ${what}; "${value}" is not one.`,
|
|
208
|
+
{run: [`${P} set ${slug} ${name} ${q(example)}`]},
|
|
71
209
|
);
|
|
72
|
-
if (
|
|
210
|
+
if (!isSafeValue(value))
|
|
73
211
|
refuse(
|
|
74
212
|
E.ERR_THEME_BAD_VALUE,
|
|
75
|
-
|
|
76
|
-
{
|
|
213
|
+
`"${value}" cannot be a CSS value: it has a ; { } < > or a newline, a comment, !important, or an unclosed bracket or quote.`,
|
|
214
|
+
{run: [`${P} set ${slug} ${name} ${q(exampleOf(type, name))}`]},
|
|
215
|
+
);
|
|
216
|
+
// A var() naming a design token must name one of the same kind; the
|
|
217
|
+
// app's own variables are its business.
|
|
218
|
+
const ref = /^var\(\s*(--[a-z0-9-]+)\s*(,.*)?\)$/i.exec(value.trim());
|
|
219
|
+
if (ref) {
|
|
220
|
+
if (ref[1] in TOKENS && typeOf(ref[1]) !== type && type !== 'other')
|
|
221
|
+
refuse(
|
|
222
|
+
E.ERR_THEME_BAD_VALUE,
|
|
223
|
+
`${name} is a ${type}; ${ref[1]} is a ${typeOf(ref[1])}.`,
|
|
224
|
+
{
|
|
225
|
+
run: [
|
|
226
|
+
`${HOW} --list tokens ${name.replace(/^--/, '').split('-')[0]}`,
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (type === 'color' && !isFixedColor(value)) bad('a color', '#1f2937');
|
|
233
|
+
if (type === 'size' && !isSize(value, name)) bad('a size', '6px');
|
|
234
|
+
if (type === 'number' && !isNumber(value, name))
|
|
235
|
+
bad('a number', /weight/.test(name) ? '600' : '1.4');
|
|
236
|
+
if (type === 'time' && !isTime(value)) bad('a time up to 60s', '150ms');
|
|
237
|
+
if (type === 'easing' && !isEasing(value))
|
|
238
|
+
bad('an easing', 'cubic-bezier(0.2, 0, 0, 1)');
|
|
239
|
+
if (type === 'font' && !isFamilyList(value))
|
|
240
|
+
bad('a font family list', 'Inter, sans-serif');
|
|
241
|
+
if (type === 'shadow' && !isShadow(value))
|
|
242
|
+
bad('a shadow', '0 2px 4px rgb(0 0 0 / 0.2)');
|
|
243
|
+
if (
|
|
244
|
+
/^--focus-outline-style$/.test(name) &&
|
|
245
|
+
!/^(none|solid|dashed|dotted|double|groove|ridge|inset|outset|auto)$/.test(
|
|
246
|
+
value.trim(),
|
|
247
|
+
)
|
|
248
|
+
)
|
|
249
|
+
bad('an outline style', 'solid');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Whether a value is a color that works on its own and inside
|
|
254
|
+
* `light-dark()`: a color this editor can read, or one of the color
|
|
255
|
+
* functions and keywords it cannot read but CSS can; never a CSS-wide
|
|
256
|
+
* keyword or a function with broken insides.
|
|
257
|
+
* @param {string} value
|
|
258
|
+
* @returns {boolean}
|
|
259
|
+
*/
|
|
260
|
+
function isFixedColor(value) {
|
|
261
|
+
const v = value.trim().toLowerCase();
|
|
262
|
+
if (['inherit', 'initial', 'unset', 'revert', 'revert-layer'].includes(v))
|
|
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
|
+
}
|
|
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;
|
|
284
|
+
if (/^(rgb|rgba|hsl|hsla|hwb|lab|lch|oklab|oklch)\(/.test(v))
|
|
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
|
+
);
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
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)
|
|
77
351
|
);
|
|
78
|
-
if (
|
|
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
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Whether a value is a shadow list: each layer is `inset`, two to four
|
|
385
|
+
* lengths, and a color, in any order; or `none`.
|
|
386
|
+
* @param {string} value
|
|
387
|
+
*/
|
|
388
|
+
function isShadow(value) {
|
|
389
|
+
const v = value.trim();
|
|
390
|
+
if (v === 'none') return true;
|
|
391
|
+
const layers = splitTop(v, ',');
|
|
392
|
+
// light-dark() takes colors: a whole shadow per mode inside it is dropped.
|
|
393
|
+
if (
|
|
394
|
+
layers.length === 1 &&
|
|
395
|
+
splitTop(v, ' ').length === 1 &&
|
|
396
|
+
!/^var\(/i.test(v)
|
|
397
|
+
)
|
|
398
|
+
return false;
|
|
399
|
+
return layers.every(layer => {
|
|
400
|
+
const words = splitTop(layer.trim(), ' ');
|
|
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',
|
|
413
|
+
);
|
|
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;
|
|
419
|
+
return (
|
|
420
|
+
insets <= 1 &&
|
|
421
|
+
lengths >= 2 &&
|
|
422
|
+
lengths <= 4 &&
|
|
423
|
+
rest.length <= 1 &&
|
|
424
|
+
rest.every(w => isFixedColor(w))
|
|
425
|
+
);
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* A value split at `sep` outside brackets: `a(b, c), d` by `,` is
|
|
431
|
+
* [`a(b, c)`, ` d`]. A space separator also splits runs of spaces.
|
|
432
|
+
* @param {string} v
|
|
433
|
+
* @param {',' | ' '} sep
|
|
434
|
+
* @returns {string[]}
|
|
435
|
+
*/
|
|
436
|
+
function splitTop(v, sep) {
|
|
437
|
+
/** @type {string[]} */
|
|
438
|
+
const out = [];
|
|
439
|
+
let depth = 0;
|
|
440
|
+
let cur = '';
|
|
441
|
+
for (const c of v) {
|
|
442
|
+
if (c === '(') depth++;
|
|
443
|
+
if (c === ')') depth--;
|
|
444
|
+
if (depth === 0 && (sep === ' ' ? /\s/.test(c) : c === sep)) {
|
|
445
|
+
if (cur.trim() || sep === ',') out.push(cur);
|
|
446
|
+
cur = '';
|
|
447
|
+
} else cur += c;
|
|
448
|
+
}
|
|
449
|
+
if (cur.trim() || sep === ',') out.push(cur);
|
|
450
|
+
return out.filter(x => sep === ',' || x.trim());
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Refuse a value that would read the token itself, through other tokens.
|
|
455
|
+
* @param {string} thing
|
|
456
|
+
* @param {string} value
|
|
457
|
+
* @param {Record<string, unknown>} TOKENS
|
|
458
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
459
|
+
*/
|
|
460
|
+
function checkCycle(thing, value, TOKENS, data) {
|
|
461
|
+
/** @param {string} t */
|
|
462
|
+
const valueOf = t =>
|
|
463
|
+
String(
|
|
464
|
+
[...data.settings]
|
|
465
|
+
.reverse()
|
|
466
|
+
.find(s => s.source === 'token' && s.thing === t && !s.on)?.value ??
|
|
467
|
+
TOKENS[t] ??
|
|
468
|
+
'',
|
|
469
|
+
);
|
|
470
|
+
const refs = (/** @type {string} */ v) =>
|
|
471
|
+
[...v.matchAll(/var\(\s*(--[a-z0-9-]+)/gi)].map(m => m[1]);
|
|
472
|
+
/** @type {string[]} */
|
|
473
|
+
const path = [];
|
|
474
|
+
const seen = new Set();
|
|
475
|
+
/** @param {string} t @returns {boolean} */
|
|
476
|
+
const reaches = t => {
|
|
477
|
+
if (t === thing) return true;
|
|
478
|
+
if (seen.has(t)) return false;
|
|
479
|
+
seen.add(t);
|
|
480
|
+
path.push(t);
|
|
481
|
+
if (refs(valueOf(t)).some(reaches)) return true;
|
|
482
|
+
path.pop();
|
|
483
|
+
return false;
|
|
484
|
+
};
|
|
485
|
+
if (refs(value).some(reaches))
|
|
79
486
|
refuse(
|
|
80
487
|
E.ERR_THEME_BAD_VALUE,
|
|
81
|
-
`${
|
|
82
|
-
{
|
|
488
|
+
`${thing} would read itself${path.length ? ` through ${path.join(', ')}` : ''}, so it would have no value at all.`,
|
|
489
|
+
{
|
|
490
|
+
run: [
|
|
491
|
+
`${P} set ${data.name} ${thing} <a value that does not read ${thing}>`,
|
|
492
|
+
],
|
|
493
|
+
},
|
|
83
494
|
);
|
|
84
495
|
}
|
|
85
496
|
|
|
@@ -110,27 +521,76 @@ export function checkSetting(
|
|
|
110
521
|
refuse(E.ERR_MISSING_ARGUMENT, 'Name the thing to change.', {
|
|
111
522
|
run: [`${P} ${unset ? 'undo' : 'set'} --help`],
|
|
112
523
|
});
|
|
113
|
-
|
|
524
|
+
// `--token=value` is one word to the shell: say how to write it.
|
|
525
|
+
if (thing.startsWith('--') && thing.includes('=')) {
|
|
526
|
+
const [name, ...rest] = thing.split('=');
|
|
527
|
+
refuse(
|
|
528
|
+
E.ERR_MISSING_ARGUMENT,
|
|
529
|
+
`Give the token and its value as two words: ${name} ${rest.join('=')}.`,
|
|
530
|
+
{
|
|
531
|
+
run: [
|
|
532
|
+
`${P} ${unset ? 'undo' : 'set'} ${slug} ${name}${unset ? '' : ` ${q(rest.join('='))}`}`,
|
|
533
|
+
],
|
|
534
|
+
},
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
if (!unset && (value === undefined || value.trim() === ''))
|
|
114
538
|
refuse(E.ERR_MISSING_ARGUMENT, `Give a value for ${thing}.`, {
|
|
115
539
|
run: [`${P} set --help`],
|
|
116
540
|
});
|
|
117
|
-
|
|
541
|
+
// An empty flag is never read as no flag: --state '' would reach every
|
|
542
|
+
// state.
|
|
543
|
+
for (const k of /** @type {const} */ (['on', 'when', 'state', 'mode']))
|
|
544
|
+
if (flags[k] === '')
|
|
545
|
+
refuse(E.ERR_MISSING_ARGUMENT, `--${k} was given no value.`, {
|
|
546
|
+
run: [`${P} ${unset ? 'undo' : 'set'} --help`],
|
|
547
|
+
});
|
|
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
|
+
}
|
|
568
|
+
// A placeholder copied from a suggestion is not a value.
|
|
569
|
+
if (!unset && /<[a-z][\w -]*>/i.test(v))
|
|
570
|
+
refuse(
|
|
571
|
+
E.ERR_THEME_BAD_VALUE,
|
|
572
|
+
`"${v}" is a placeholder: put a real value in place of ${/<[a-z][\w -]*>/i.exec(v)?.[0]}.`,
|
|
573
|
+
{run: [`${HOW} ${thing.replace(/^--/, '')}`]},
|
|
574
|
+
);
|
|
118
575
|
/** @type {import('./_settings.mjs').Setting} */
|
|
119
576
|
const s = {thing, value: v, source: 'dial'};
|
|
120
|
-
if (flags.on) s.on = flags.on;
|
|
121
|
-
if (flags.when) s.when = flags.when;
|
|
122
|
-
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();
|
|
123
580
|
if (flags.only) s.only = true;
|
|
124
581
|
if (flags.mode) {
|
|
125
|
-
|
|
582
|
+
const mode = flags.mode.toLowerCase();
|
|
583
|
+
if (mode !== 'light' && mode !== 'dark')
|
|
126
584
|
refuse(
|
|
127
585
|
E.ERR_THEME_BAD_VALUE,
|
|
128
586
|
`--mode takes light or dark, not "${flags.mode}".`,
|
|
587
|
+
{didYouMean: nearest(mode, ['light', 'dark'])},
|
|
129
588
|
);
|
|
130
|
-
s.mode =
|
|
589
|
+
s.mode = mode;
|
|
131
590
|
}
|
|
132
591
|
const onFlag = s.on ? ` --on ${s.on}` : '';
|
|
133
|
-
|
|
592
|
+
// A setting saved for a mode the theme no longer has can still be removed.
|
|
593
|
+
if (s.mode && data.mode !== 'both' && !unset)
|
|
134
594
|
refuse(
|
|
135
595
|
E.ERR_THEME_NO_MODE,
|
|
136
596
|
`This theme is ${data.mode}; --mode is for themes with both modes.`,
|
|
@@ -138,20 +598,20 @@ export function checkSetting(
|
|
|
138
598
|
);
|
|
139
599
|
if ((s.when || s.state) && !s.on)
|
|
140
600
|
refuse(E.ERR_MISSING_ARGUMENT, '--when and --state need --on <part>.', {
|
|
141
|
-
run: [`${HOW}
|
|
601
|
+
run: [`${HOW} --list parts`],
|
|
142
602
|
});
|
|
143
603
|
if (s.only) {
|
|
144
604
|
const fam = s.on ? familyOf(s.on) : null;
|
|
145
605
|
if (!fam || FAMILIES[/** @type {string} */ (s.on)])
|
|
146
606
|
refuse(
|
|
147
607
|
E.ERR_INVALID_ARGUMENT,
|
|
148
|
-
'--only is for one member of a
|
|
149
|
-
{run: [`${HOW} --list
|
|
608
|
+
'--only is for one member of a group, such as text-input (how --list groups).',
|
|
609
|
+
{run: [`${HOW} --list groups`]},
|
|
150
610
|
);
|
|
151
611
|
if (!AUTO_SPREAD.includes(/** @type {string} */ (fam)))
|
|
152
612
|
refuse(
|
|
153
613
|
E.ERR_INVALID_ARGUMENT,
|
|
154
|
-
`${s.on} is styled on its own already: only ${AUTO_SPREAD.join(' and ')} spread to their
|
|
614
|
+
`${s.on} is styled on its own already: only ${AUTO_SPREAD.join(' and ')} spread to their group, so drop --only.`,
|
|
155
615
|
{
|
|
156
616
|
run: [
|
|
157
617
|
unset
|
|
@@ -175,15 +635,15 @@ export function checkSetting(
|
|
|
175
635
|
...Object.keys(FAMILIES),
|
|
176
636
|
]),
|
|
177
637
|
run: [
|
|
178
|
-
`${HOW} ${s.on.split('-')[0]}
|
|
179
|
-
`${HOW}
|
|
638
|
+
`${HOW} --list parts ${s.on.split('-')[0]}`,
|
|
639
|
+
`${HOW} ${q(s.on)}`,
|
|
180
640
|
],
|
|
181
641
|
},
|
|
182
642
|
);
|
|
183
643
|
}
|
|
184
644
|
if (s.when) {
|
|
185
|
-
const [p, wv] = s.when.split(':');
|
|
186
|
-
if (!wv)
|
|
645
|
+
const [p, wv, ...more] = s.when.split(':');
|
|
646
|
+
if (!wv || more.length)
|
|
187
647
|
refuse(
|
|
188
648
|
E.ERR_THEME_BAD_VALUE,
|
|
189
649
|
'--when takes <prop>:<value>, such as variant:primary.',
|
|
@@ -211,6 +671,37 @@ export function checkSetting(
|
|
|
211
671
|
{didYouMean: nearest(s.state, states)},
|
|
212
672
|
);
|
|
213
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
|
+
}
|
|
214
705
|
const step = v === 'more' || v === 'less';
|
|
215
706
|
if (DIALS[thing]) {
|
|
216
707
|
s.source = 'dial';
|
|
@@ -233,12 +724,18 @@ export function checkSetting(
|
|
|
233
724
|
);
|
|
234
725
|
if (unset) return s;
|
|
235
726
|
const ok = DIALS[thing].values.split('|');
|
|
236
|
-
|
|
727
|
+
const lower = v.toLowerCase();
|
|
728
|
+
if (!ok.includes(v)) {
|
|
729
|
+
const near = ok.includes(lower) ? [lower] : nearest(lower, ok);
|
|
237
730
|
refuse(
|
|
238
731
|
E.ERR_THEME_BAD_VALUE,
|
|
239
732
|
`${thing} takes ${DIALS[thing].values}, not "${v}".`,
|
|
240
|
-
{
|
|
733
|
+
{
|
|
734
|
+
didYouMean: near,
|
|
735
|
+
run: [`${P} set ${slug} ${thing} ${near[0] ?? ok[0]}`],
|
|
736
|
+
},
|
|
241
737
|
);
|
|
738
|
+
}
|
|
242
739
|
return s;
|
|
243
740
|
}
|
|
244
741
|
if (thing === 'headings') {
|
|
@@ -252,11 +749,27 @@ export function checkSetting(
|
|
|
252
749
|
],
|
|
253
750
|
},
|
|
254
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
|
+
);
|
|
255
762
|
if (unset) {
|
|
763
|
+
// With no word, the one headings setting the theme has.
|
|
764
|
+
const held = data.settings.filter(x => x.thing === 'headings');
|
|
256
765
|
s.axis =
|
|
257
766
|
HEADING_TRACKING[v] !== undefined || v === 'tracking'
|
|
258
767
|
? 'tracking'
|
|
259
|
-
: 'weight'
|
|
768
|
+
: HEADING_WEIGHTS[v] !== undefined || v === 'weight'
|
|
769
|
+
? 'weight'
|
|
770
|
+
: held.length === 1
|
|
771
|
+
? (held[0].axis ?? 'weight')
|
|
772
|
+
: 'weight';
|
|
260
773
|
return s;
|
|
261
774
|
}
|
|
262
775
|
if (HEADING_WEIGHTS[v] !== undefined) s.axis = 'weight';
|
|
@@ -277,11 +790,13 @@ export function checkSetting(
|
|
|
277
790
|
if (unset) return s;
|
|
278
791
|
/** @param {Array<[string, number]>} L @param {string} def */
|
|
279
792
|
const ladder = (L, def) => {
|
|
280
|
-
|
|
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) =>
|
|
281
796
|
[...data.settings]
|
|
282
797
|
.reverse()
|
|
283
|
-
.find(x => x.thing === thing && (x.on ?? '') ===
|
|
284
|
-
|
|
798
|
+
.find(x => x.thing === thing && (x.on ?? '') === on)?.value;
|
|
799
|
+
const cur = own(s.on ?? '') ?? (s.on ? own('') : undefined) ?? def;
|
|
285
800
|
let i = L.findIndex(([n]) => n === cur);
|
|
286
801
|
if (i < 0) {
|
|
287
802
|
const px = parseFloat(cur);
|
|
@@ -294,14 +809,36 @@ export function checkSetting(
|
|
|
294
809
|
};
|
|
295
810
|
if (PALETTE_DIALS.includes(thing) && (s.on || s.mode)) {
|
|
296
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
|
+
}
|
|
297
838
|
refuse(
|
|
298
839
|
E.ERR_THEME_BAD_VALUE,
|
|
299
|
-
`${thing}
|
|
300
|
-
{
|
|
301
|
-
run: [
|
|
302
|
-
`${P} set ${slug} ${token} ${q(v || '#7c3aed')}${onFlag}${s.mode ? ` --mode ${s.mode}` : ''}`,
|
|
303
|
-
],
|
|
304
|
-
},
|
|
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},
|
|
305
842
|
);
|
|
306
843
|
}
|
|
307
844
|
if (s.mode)
|
|
@@ -311,9 +848,18 @@ export function checkSetting(
|
|
|
311
848
|
{run: [`${P} set ${slug} ${thing} ${q(v)}${onFlag}`]},
|
|
312
849
|
);
|
|
313
850
|
if (thing === 'corners') {
|
|
314
|
-
if (s.on) refuseInvisibleCorners(ctx, slug, s);
|
|
315
|
-
|
|
316
|
-
|
|
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))
|
|
317
863
|
refuse(
|
|
318
864
|
E.ERR_THEME_BAD_VALUE,
|
|
319
865
|
`corners takes ${DIALS.corners.values}, not "${v}".`,
|
|
@@ -345,29 +891,118 @@ export function checkSetting(
|
|
|
345
891
|
{run: [`${P} set ${slug} font-size 13px --on ${s.on}`]},
|
|
346
892
|
);
|
|
347
893
|
if (step) s.value = ladder(TEXT_SIZE, 'medium');
|
|
348
|
-
else if (
|
|
894
|
+
else if (
|
|
895
|
+
!TEXT_SIZE.some(([n]) => n === v) &&
|
|
896
|
+
!(/^\d+(\.\d+)?$/.test(v.trim()) && Number(v) >= 10 && Number(v) <= 32)
|
|
897
|
+
)
|
|
349
898
|
refuse(
|
|
350
899
|
E.ERR_THEME_BAD_VALUE,
|
|
351
|
-
`text-size takes ${DIALS['text-size'].values}, not "${v}".`,
|
|
900
|
+
`text-size takes ${DIALS['text-size'].values} (a number is the body size in pixels, 10 to 32), not "${v}".`,
|
|
901
|
+
{run: [`${P} set ${slug} text-size 16`]},
|
|
352
902
|
);
|
|
903
|
+
else if (!TEXT_SIZE.some(([n]) => n === v))
|
|
904
|
+
s.value = String(Number(v.trim()));
|
|
353
905
|
} else if (PALETTE_DIALS.includes(thing)) {
|
|
354
906
|
if (step)
|
|
355
907
|
refuse(E.ERR_THEME_BAD_VALUE, `${thing} takes a color, not a step.`, {
|
|
356
908
|
run: [`${P} set ${slug} ${thing} "#7c3aed"`],
|
|
357
909
|
});
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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]);
|
|
363
931
|
refuse(
|
|
364
932
|
E.ERR_THEME_BAD_VALUE,
|
|
365
|
-
`"${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.' : ''}`,
|
|
366
934
|
{
|
|
367
|
-
|
|
935
|
+
didYouMean: better ? [better] : near,
|
|
936
|
+
run: [`${P} set ${slug} ${thing} ${q(better ?? '#7c3aed')}`],
|
|
368
937
|
},
|
|
369
938
|
);
|
|
939
|
+
}
|
|
940
|
+
} else if (thing === 'vibrancy') {
|
|
941
|
+
if (s.on)
|
|
942
|
+
refuse(
|
|
943
|
+
E.ERR_THEME_BAD_VALUE,
|
|
944
|
+
'vibrancy applies to every generated color of the theme; drop --on.',
|
|
945
|
+
{run: [`${P} set ${slug} vibrancy ${q(v)}`]},
|
|
946
|
+
);
|
|
947
|
+
if (step) {
|
|
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)),
|
|
959
|
+
);
|
|
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;
|
|
977
|
+
refuse(
|
|
978
|
+
E.ERR_THEME_BAD_VALUE,
|
|
979
|
+
`vibrancy takes a whole number from 0 (gray) to 100 (most vivid), or more or less, not "${v}".`,
|
|
980
|
+
{run: [`${P} set ${slug} vibrancy ${near}`]},
|
|
981
|
+
);
|
|
982
|
+
} else s.value = String(Number(v));
|
|
983
|
+
const colors = data.settings.filter(
|
|
984
|
+
x => x.source === 'dial' && PALETTE_DIALS.includes(x.thing),
|
|
985
|
+
);
|
|
986
|
+
// A gray has no color to saturate, so vibrancy changes nothing.
|
|
987
|
+
if (!colors.some(x => !isGraySeed(seedOf(x.value))))
|
|
988
|
+
refuse(
|
|
989
|
+
E.ERR_THEME_NO_EFFECT,
|
|
990
|
+
colors.length
|
|
991
|
+
? 'vibrancy sets how saturated the generated colors are, and every color this theme sets is a gray, so it would change nothing. Set a color first.'
|
|
992
|
+
: 'vibrancy sets how saturated the generated colors are, and this theme sets no color yet. Set one first.',
|
|
993
|
+
{run: [`${P} set ${slug} accent "#7c3aed"`]},
|
|
994
|
+
);
|
|
370
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
|
+
);
|
|
371
1006
|
if (s.on && !isRole)
|
|
372
1007
|
refuse(
|
|
373
1008
|
E.ERR_THEME_BAD_VALUE,
|
|
@@ -380,6 +1015,29 @@ export function checkSetting(
|
|
|
380
1015
|
'font takes a preset or a family name, not a step.',
|
|
381
1016
|
{fix: `the values are ${DIALS.font.values}`},
|
|
382
1017
|
);
|
|
1018
|
+
// A family list: names, quoted or not, split by commas; nothing that
|
|
1019
|
+
// could end the CSS declaration or the preview's style block. A
|
|
1020
|
+
// preset typed in another case or spacing is the preset.
|
|
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);
|
|
1031
|
+
if (
|
|
1032
|
+
!s.value ||
|
|
1033
|
+
/[;{}<>\\\n]/.test(s.value) ||
|
|
1034
|
+
!s.value.split(',').every(f => family(f.trim()))
|
|
1035
|
+
)
|
|
1036
|
+
refuse(
|
|
1037
|
+
E.ERR_THEME_BAD_VALUE,
|
|
1038
|
+
`"${v}" is not a font: give a preset (${DIALS.font.values}) or a family list such as "Inter, sans-serif".`,
|
|
1039
|
+
{run: [`${P} set ${slug} font inter`]},
|
|
1040
|
+
);
|
|
383
1041
|
} else if (thing === 'base') {
|
|
384
1042
|
if (s.on)
|
|
385
1043
|
refuse(
|
|
@@ -414,22 +1072,34 @@ export function checkSetting(
|
|
|
414
1072
|
refuse(
|
|
415
1073
|
E.ERR_THEME_PRIVATE_TOKEN,
|
|
416
1074
|
`"${thing}" is private to a component; themes cannot set it.`,
|
|
417
|
-
{run: [`${HOW}
|
|
1075
|
+
{run: [`${HOW} ${q(thing)}`]},
|
|
418
1076
|
);
|
|
419
1077
|
if (!(thing in TOKENS)) {
|
|
420
1078
|
refuse(E.ERR_THEME_UNKNOWN_TOKEN, `"${thing}" is not a design token.`, {
|
|
421
1079
|
didYouMean: nearest(thing, Object.keys(TOKENS)),
|
|
422
1080
|
run: [
|
|
423
|
-
`${HOW}
|
|
1081
|
+
`${HOW} --list tokens ${thing
|
|
1082
|
+
.replace(/^-+(color-)?/, '')
|
|
1083
|
+
.split('-')[0]
|
|
1084
|
+
.trim()}`.trim(),
|
|
424
1085
|
],
|
|
425
1086
|
});
|
|
426
1087
|
}
|
|
427
1088
|
const type = C.typeOf(thing);
|
|
428
|
-
|
|
1089
|
+
// Only colors differ by mode: a shadow list cannot sit inside
|
|
1090
|
+
// light-dark(), so a shadow per mode would remove it in both.
|
|
1091
|
+
// undo still removes a setting an earlier release saved for one mode.
|
|
1092
|
+
if (s.mode && type !== 'color' && !unset)
|
|
429
1093
|
refuse(
|
|
430
1094
|
E.ERR_THEME_NO_MODE,
|
|
431
|
-
`${thing} is a ${type}, so it is the same in light and dark; drop --mode. Only colors
|
|
432
|
-
{
|
|
1095
|
+
`${thing} is ${/^[aeiou]/.test(type) ? 'an' : 'a'} ${type === 'other' ? 'value of its own kind' : type}, so it is the same in light and dark; drop --mode. Only colors differ by mode.`,
|
|
1096
|
+
{
|
|
1097
|
+
run: [
|
|
1098
|
+
unset
|
|
1099
|
+
? `${P} undo ${slug} ${thing}${onFlag}`
|
|
1100
|
+
: `${P} set ${slug} ${thing} ${q(v)}${onFlag}`,
|
|
1101
|
+
],
|
|
1102
|
+
},
|
|
433
1103
|
);
|
|
434
1104
|
s.source = 'token';
|
|
435
1105
|
if (unset) return s;
|
|
@@ -438,7 +1108,10 @@ export function checkSetting(
|
|
|
438
1108
|
E.ERR_THEME_BAD_VALUE,
|
|
439
1109
|
`"more"/"less" work only on dials (${Object.keys(DIALS).join(', ')}). Give ${thing} a ${type} value.`,
|
|
440
1110
|
);
|
|
441
|
-
checkTypedValue(type, v, thing);
|
|
1111
|
+
checkTypedValue(type, v, thing, TOKENS, C.typeOf, slug);
|
|
1112
|
+
checkCycle(thing, v, TOKENS, data);
|
|
1113
|
+
// With no part data, what reads the token is unknown: save, unchecked.
|
|
1114
|
+
if (ctx.unchecked) return s;
|
|
442
1115
|
const parts = partsOf(ctx, s);
|
|
443
1116
|
if (parts.length) {
|
|
444
1117
|
const results = parts.map(
|
|
@@ -453,8 +1126,8 @@ export function checkSetting(
|
|
|
453
1126
|
const spread = Boolean(
|
|
454
1127
|
FAMILIES[s.on ?? ''] || (familyOf(s.on ?? '') && parts.length > 1),
|
|
455
1128
|
);
|
|
456
|
-
// A part with no record is judged by its
|
|
457
|
-
// member that has a record reads the token, as the
|
|
1129
|
+
// A part with no record is judged by its group: refused when no
|
|
1130
|
+
// member that has a record reads the token, as the group form is.
|
|
458
1131
|
const fam = s.on ? familyOf(s.on) : null;
|
|
459
1132
|
if (!recorded.length && fam && !spread) {
|
|
460
1133
|
const siblings = FAMILIES[fam].filter(t => C.MAP[t]);
|
|
@@ -471,7 +1144,7 @@ export function checkSetting(
|
|
|
471
1144
|
);
|
|
472
1145
|
refuse(
|
|
473
1146
|
E.ERR_THEME_NO_EFFECT,
|
|
474
|
-
`No part of the ${fam}
|
|
1147
|
+
`No part of the ${fam} group reads ${thing}, so ${s.on} cannot either. ${r.message}`,
|
|
475
1148
|
{
|
|
476
1149
|
run: r.run.map(x =>
|
|
477
1150
|
x.replace(`--on ${famResults[0][0]}`, `--on ${s.on}`),
|
|
@@ -481,11 +1154,15 @@ export function checkSetting(
|
|
|
481
1154
|
);
|
|
482
1155
|
}
|
|
483
1156
|
if (famResults.length)
|
|
484
|
-
s.note = `${s.on} has no compiled record; checked against the ${fam}
|
|
1157
|
+
s.note = `${s.on} has no compiled record; checked against the ${fam} group, where ${famResults
|
|
485
1158
|
.filter(([, r]) => !r)
|
|
486
1159
|
.map(([t]) => t)
|
|
487
1160
|
.join(', ')} read ${thing}`;
|
|
488
|
-
} else if (
|
|
1161
|
+
} else if (
|
|
1162
|
+
recorded.length &&
|
|
1163
|
+
hard.length === recorded.length &&
|
|
1164
|
+
!statusTwin(C, recorded, thing, s.when)
|
|
1165
|
+
) {
|
|
489
1166
|
const [t0, r0] =
|
|
490
1167
|
/** @type {[string, import('./_compiler.mjs').CheckFailure]} */ (
|
|
491
1168
|
hard[0]
|
|
@@ -500,7 +1177,7 @@ export function checkSetting(
|
|
|
500
1177
|
s.note = `no effect on ${hard.map(([t]) => t).join(', ')} (they never read ${thing})`;
|
|
501
1178
|
if (!s.note && recorded.length) {
|
|
502
1179
|
const fed = C.feeds(recorded[0][0], thing);
|
|
503
|
-
if (fed.length) s.note = `feeds ${C.fmtFeeds(fed)}`;
|
|
1180
|
+
if (fed.length) s.note = `feeds ${C.fmtFeeds(fed, recorded[0][0])}`;
|
|
504
1181
|
}
|
|
505
1182
|
const soft = results.filter(([, r]) => r?.soft);
|
|
506
1183
|
if (!s.note && soft.length && soft.length === results.length)
|
|
@@ -509,8 +1186,28 @@ export function checkSetting(
|
|
|
509
1186
|
).message;
|
|
510
1187
|
} else {
|
|
511
1188
|
const r = C.checkTokenGlobal(thing, P);
|
|
512
|
-
|
|
513
|
-
|
|
1189
|
+
// Tokens whose default is built from this one: a part that reads
|
|
1190
|
+
// them sees the change.
|
|
1191
|
+
const fed = r
|
|
1192
|
+
? Object.entries(TOKENS)
|
|
1193
|
+
.filter(
|
|
1194
|
+
([k, v]) =>
|
|
1195
|
+
k !== thing && new RegExp(`var\\(${thing}[,)]`).test(String(v)),
|
|
1196
|
+
)
|
|
1197
|
+
.map(([k]) => k)
|
|
1198
|
+
: [];
|
|
1199
|
+
// A token a dial writes is always accepted, so a dial's notes never
|
|
1200
|
+
// name a token `set` refuses.
|
|
1201
|
+
if (r && fed.length)
|
|
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)' : ''}`;
|
|
1203
|
+
else if (r && dialWrites(thing))
|
|
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.
|
|
1209
|
+
else if (r)
|
|
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`;
|
|
514
1211
|
}
|
|
515
1212
|
return s;
|
|
516
1213
|
}
|
|
@@ -519,10 +1216,10 @@ export function checkSetting(
|
|
|
519
1216
|
if (!s.on)
|
|
520
1217
|
refuse(
|
|
521
1218
|
E.ERR_MISSING_ARGUMENT,
|
|
522
|
-
`${prop} is a CSS property, so it needs --on <part|
|
|
1219
|
+
`${prop} is a CSS property, so it needs --on <part|group>. For the whole theme use a dial or a token.`,
|
|
523
1220
|
{
|
|
524
1221
|
run: [
|
|
525
|
-
`${P} set ${slug} ${prop} ${q(v ||
|
|
1222
|
+
`${P} set ${slug} ${prop} ${q(v || exampleOf(groupOfPropType(prop), prop))} --on card`,
|
|
526
1223
|
`${HOW} "${prop}"`,
|
|
527
1224
|
],
|
|
528
1225
|
},
|
|
@@ -530,7 +1227,42 @@ export function checkSetting(
|
|
|
530
1227
|
s.source = 'property';
|
|
531
1228
|
s.thing = prop;
|
|
532
1229
|
if (unset) return s;
|
|
533
|
-
|
|
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
|
+
}
|
|
1259
|
+
if (!isSafeValue(v))
|
|
1260
|
+
refuse(
|
|
1261
|
+
E.ERR_THEME_BAD_VALUE,
|
|
1262
|
+
`"${v}" cannot be a CSS value: it has a ; { } < > or a newline, a comment, !important, or an unclosed bracket or quote.`,
|
|
1263
|
+
{run: [`${HOW} ${prop}`]},
|
|
1264
|
+
);
|
|
1265
|
+
if (/radius$/.test(prop)) refuseInvisibleCorners(ctx, slug, s, data);
|
|
534
1266
|
if (step)
|
|
535
1267
|
refuse(
|
|
536
1268
|
E.ERR_THEME_BAD_VALUE,
|
|
@@ -585,6 +1317,22 @@ export function checkSetting(
|
|
|
585
1317
|
/** @type {((v: string, on: string | undefined) => string[]) | undefined} */ (
|
|
586
1318
|
THING_HINTS[thing.toLowerCase()]
|
|
587
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
|
+
);
|
|
588
1336
|
return refuse(
|
|
589
1337
|
E.ERR_THEME_UNKNOWN_THING,
|
|
590
1338
|
`"${thing}" is not a dial, a token, or a CSS property.`,
|
|
@@ -605,10 +1353,20 @@ export function checkSetting(
|
|
|
605
1353
|
* @param {import('./_settings.mjs').Ctx} ctx
|
|
606
1354
|
* @param {string} slug
|
|
607
1355
|
* @param {import('./_settings.mjs').Setting} s
|
|
1356
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
608
1357
|
*/
|
|
609
|
-
function refuseInvisibleCorners(ctx, slug, s) {
|
|
1358
|
+
function refuseInvisibleCorners(ctx, slug, s, data) {
|
|
1359
|
+
if (ctx.unchecked) return;
|
|
610
1360
|
const parts = partsOf(ctx, s);
|
|
611
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;
|
|
612
1370
|
const kids = (ctx.C.MAP[parts[0]]?.contains ?? [])
|
|
613
1371
|
.filter(k => ctx.C.showsCorners(k) && ctx.C.MAP[k])
|
|
614
1372
|
.slice(0, 3);
|
|
@@ -626,241 +1384,51 @@ function refuseInvisibleCorners(ctx, slug, s) {
|
|
|
626
1384
|
}
|
|
627
1385
|
|
|
628
1386
|
/**
|
|
629
|
-
*
|
|
630
|
-
* the
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
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
|
|
634
1396
|
*/
|
|
635
|
-
|
|
636
|
-
const
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
const results = [];
|
|
658
|
-
/** @param {string | null} g */
|
|
659
|
-
const V = g => {
|
|
660
|
-
const bg = design.find(d => ['gradient', 'glossy'].includes(d));
|
|
661
|
-
const sh = design.find(d =>
|
|
662
|
-
['bevel', 'raised', 'flat', 'sunken'].includes(d),
|
|
663
|
-
);
|
|
664
|
-
if (g === 'background' && bg) return DESIGN[bg];
|
|
665
|
-
if (g === 'shadow' && sh) return DESIGN[sh];
|
|
666
|
-
if (g === 'font' && tracking)
|
|
667
|
-
return /tight/.test(text) ? '-0.02em' : '0.04em';
|
|
668
|
-
if (g === 'font' && /bold|heav/.test(text)) return '700';
|
|
669
|
-
if (g === 'text' && /white/.test(text)) return '"#ffffff"';
|
|
670
|
-
return '<value>';
|
|
671
|
-
};
|
|
672
|
-
for (const raw of parts.slice(0, 3)) {
|
|
673
|
-
const [part, wp, wv] = raw.split(':');
|
|
674
|
-
const when = wp ? ` --when ${wp}:${wv}` : '';
|
|
675
|
-
const one = FAMILIES[part] ? FAMILIES[part][0] : part;
|
|
676
|
-
if (!TARGET.has(one)) continue;
|
|
677
|
-
// A part with variants: a change without --when reaches every variant.
|
|
678
|
-
const variants = Object.entries(C.variantValues(one)).filter(
|
|
679
|
-
([p, vals]) =>
|
|
680
|
-
['status', 'variant', 'tone', 'color'].includes(p) && vals.length > 1,
|
|
681
|
-
);
|
|
682
|
-
for (const g of intents.slice(0, 3)) {
|
|
683
|
-
if (g === null) {
|
|
684
|
-
results.push({
|
|
685
|
-
text: `what ${one} reads`,
|
|
686
|
-
commands: [`${HOW} ${one}`],
|
|
687
|
-
});
|
|
688
|
-
continue;
|
|
689
|
-
}
|
|
690
|
-
if (g === 'corners') {
|
|
691
|
-
results.push({
|
|
692
|
-
text: `corners on ${part}${when ? ` (${wp} ${wv})` : ''}`,
|
|
693
|
-
commands: [
|
|
694
|
-
`${P} set <theme> corners ${/pill|circ/.test(text) ? 'pill' : /sharp|square/.test(text) ? 'square' : 'round'} --on ${part}${when}`,
|
|
695
|
-
`${P} set <theme> border-radius 6px --on ${part}${when}`,
|
|
696
|
-
],
|
|
697
|
-
});
|
|
698
|
-
continue;
|
|
699
|
-
}
|
|
700
|
-
if (g === 'shadow') {
|
|
701
|
-
const shadows = Object.keys(ctx.project.core.tokenDefaults).filter(k =>
|
|
702
|
-
/^--shadow-(low|med|high)$/.test(k),
|
|
703
|
-
);
|
|
704
|
-
const value =
|
|
705
|
-
V('shadow') !== '<value>'
|
|
706
|
-
? V('shadow')
|
|
707
|
-
: shadows.length
|
|
708
|
-
? `"var(${/soft|subtle|light/.test(text) ? shadows[0] : /strong|deep|heavy/.test(text) ? shadows[shadows.length - 1] : shadows[Math.min(1, shadows.length - 1)]})"`
|
|
709
|
-
: '"0 2px 6px rgb(0 0 0 / 0.2)"';
|
|
710
|
-
results.push({
|
|
711
|
-
text: `shadow on ${part}${when ? ` (${wp} ${wv})` : ''}: box-shadow${shadows.length ? `; the theme's shadow tokens are ${shadows.join(', ')}` : ''}`,
|
|
712
|
-
commands: [
|
|
713
|
-
`${P} set <theme> box-shadow ${value} --on ${part}${when}`,
|
|
714
|
-
...(shadows.length
|
|
715
|
-
? [
|
|
716
|
-
`${P} set <theme> ${shadows[0]} "0 1px 3px rgb(0 0 0 / 0.12)" # change the shared shadow everywhere`,
|
|
717
|
-
]
|
|
718
|
-
: []),
|
|
719
|
-
],
|
|
720
|
-
});
|
|
721
|
-
continue;
|
|
722
|
-
}
|
|
723
|
-
if (g === 'spacing' && !tracking) {
|
|
724
|
-
results.push({
|
|
725
|
-
text: `room inside ${part}`,
|
|
726
|
-
commands: [
|
|
727
|
-
`${P} set <theme> spacing ${/dense|compact|tight|cramped/.test(text) ? 'compact' : 'roomy'} --on ${part}`,
|
|
728
|
-
`${P} set <theme> padding 12px --on ${part}${when}`,
|
|
729
|
-
],
|
|
730
|
-
});
|
|
731
|
-
continue;
|
|
732
|
-
}
|
|
733
|
-
const group = g === 'spacing' ? 'font' : g;
|
|
734
|
-
const reads = C.partReads(one, group);
|
|
735
|
-
const filtered = wv
|
|
736
|
-
? reads.filter(
|
|
737
|
-
r =>
|
|
738
|
-
r.when.some(w => w.includes(`${wp}=${wv}`)) ||
|
|
739
|
-
r.when.includes(''),
|
|
740
|
-
)
|
|
741
|
-
: reads;
|
|
742
|
-
const list = (filtered.length ? filtered : reads).slice(0, 4);
|
|
743
|
-
const direct =
|
|
744
|
-
group === 'font' && tracking
|
|
745
|
-
? 'letter-spacing'
|
|
746
|
-
: group === 'font' && /bold|weight|heav/.test(text)
|
|
747
|
-
? 'font-weight'
|
|
748
|
-
: group === 'font'
|
|
749
|
-
? 'font-family'
|
|
750
|
-
: DIRECT[group];
|
|
751
|
-
const tokenRoute =
|
|
752
|
-
(group === 'text' || group === 'background' || group === 'border') &&
|
|
753
|
-
!/gradient|inset/.test(V(group));
|
|
754
|
-
results.push({
|
|
755
|
-
text: `${part} ${group}${when ? ` (${wp} ${wv})` : ''}: ${list.length ? `comes from ${list.map(r => r.token + C.fmtWhen(r.when) + C.statusNote(one, r.token)).join(', ')}` : 'no token; set the property directly'}`,
|
|
756
|
-
commands: [
|
|
757
|
-
`${P} set <theme> ${direct} ${V(group)} --on ${part}${when} # direct: always works`,
|
|
758
|
-
...(tokenRoute
|
|
759
|
-
? list
|
|
760
|
-
.slice(0, 2)
|
|
761
|
-
.map(
|
|
762
|
-
r =>
|
|
763
|
-
`${P} set <theme> ${r.token} ${V(group)} --on ${part} # via the token${C.fmtWhen(r.when)}`,
|
|
764
|
-
)
|
|
765
|
-
: []),
|
|
766
|
-
],
|
|
767
|
-
});
|
|
768
|
-
if (
|
|
769
|
-
!when &&
|
|
770
|
-
variants.length &&
|
|
771
|
-
['text', 'background', 'border'].includes(group)
|
|
772
|
-
) {
|
|
773
|
-
const [vp, vals] = variants[0];
|
|
774
|
-
results.push({
|
|
775
|
-
text: `${part} has ${vp} ${vals.join('|')}: without --when the change reaches every ${vp}. To change one, add --when ${vp}:<value>.`,
|
|
776
|
-
commands: [
|
|
777
|
-
`${P} set <theme> ${direct} ${V(group)} --on ${part} --when ${vp}:${vals[0]}`,
|
|
778
|
-
],
|
|
779
|
-
});
|
|
780
|
-
}
|
|
781
|
-
const kids = (C.MAP[one]?.contains ?? [])
|
|
782
|
-
.filter(
|
|
783
|
-
k =>
|
|
784
|
-
C.partReads(k, group).length && !(FAMILIES[part] ?? []).includes(k),
|
|
785
|
-
)
|
|
786
|
-
.slice(0, 3);
|
|
787
|
-
if (kids.length && group === 'text') {
|
|
788
|
-
results.push({
|
|
789
|
-
text: `parts inside ${part} with their own text color: ${kids.join(', ')}. A text token set --on ${part} reaches them; a color property does not.`,
|
|
790
|
-
commands: kids.slice(0, 1).map(k => `${HOW} ${k}`),
|
|
791
|
-
});
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
if (!parts.length) {
|
|
796
|
-
if (tracking)
|
|
797
|
-
results.push({
|
|
798
|
-
text: 'letter spacing is a CSS property on a part; for headings there is a dial',
|
|
799
|
-
commands: [
|
|
800
|
-
`${P} set <theme> letter-spacing 0.02em --on <part>`,
|
|
801
|
-
`${P} set <theme> headings tight`,
|
|
802
|
-
],
|
|
803
|
-
});
|
|
804
|
-
for (const g of /** @type {string[]} */ (intents.filter(Boolean)).slice(
|
|
805
|
-
0,
|
|
806
|
-
2,
|
|
807
|
-
)) {
|
|
808
|
-
const dial = /** @type {Record<string, string>} */ ({
|
|
809
|
-
corners: 'corners',
|
|
810
|
-
spacing: 'spacing',
|
|
811
|
-
font: 'font',
|
|
812
|
-
size: 'text-size',
|
|
813
|
-
})[g];
|
|
814
|
-
if (dial && !(tracking && (g === 'spacing' || g === 'font')))
|
|
815
|
-
results.push({
|
|
816
|
-
text: DIALS[dial].help,
|
|
817
|
-
commands: DIALS[dial].examples.map(e => `${P} ${e}`),
|
|
818
|
-
});
|
|
819
|
-
if (g === 'background' || g === 'text')
|
|
820
|
-
results.push({
|
|
821
|
-
text: DIALS.accent.help,
|
|
822
|
-
commands: DIALS.accent.examples.map(e => `${P} ${e}`),
|
|
823
|
-
});
|
|
824
|
-
results.push(...wholeThemeTokens(ctx, g));
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
if (!results.length) {
|
|
828
|
-
for (const d of Object.values(DIALS)) {
|
|
829
|
-
if (d.words.split(' ').some(w => w.length > 3 && text.includes(w)))
|
|
830
|
-
results.push({
|
|
831
|
-
text: d.help,
|
|
832
|
-
commands: d.examples.map(e => `${P} ${e}`),
|
|
833
|
-
});
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
void TOKENS;
|
|
837
|
-
return {
|
|
838
|
-
parts,
|
|
839
|
-
aspects: /** @type {string[]} */ (intents.filter(Boolean)),
|
|
840
|
-
results: results.slice(0, 10),
|
|
841
|
-
};
|
|
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
|
+
);
|
|
842
1419
|
}
|
|
843
1420
|
|
|
844
1421
|
/**
|
|
845
|
-
*
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
* @
|
|
1422
|
+
* Whether a dial writes the token: a color, a heading weight (type,
|
|
1423
|
+
* headings), a radius (corners), a spacing or control size (spacing), or a
|
|
1424
|
+
* font size (type, text-size).
|
|
1425
|
+
* @param {string} token
|
|
849
1426
|
*/
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
.slice(0, 6);
|
|
856
|
-
if (!toks.length) return [];
|
|
857
|
-
return [
|
|
858
|
-
{
|
|
859
|
-
text: `whole-theme tokens for ${aspect}, most used first: ${toks.map(k => `${k} (${C.anyReader(k).length} parts)`).join(', ')}`,
|
|
860
|
-
commands: [`${P} set <theme> ${toks[0]} <value>`],
|
|
861
|
-
},
|
|
862
|
-
];
|
|
863
|
-
}
|
|
1427
|
+
const dialWrites = token =>
|
|
1428
|
+
PALETTE_TOKENS.has(token) ||
|
|
1429
|
+
/^--(text-(heading|display)-\d-weight|radius-|spacing-|size-element-|font-size-)/.test(
|
|
1430
|
+
token,
|
|
1431
|
+
);
|
|
864
1432
|
|
|
865
1433
|
/** The aspects `how` answers at the whole-theme level. */
|
|
866
1434
|
export const ASPECTS = [...Object.keys(GROUPS), 'colors'];
|