@astryxdesign/cli 0.7.14-canary.g3825554 → 0.7.14-canary.g63bbda1
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/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/palette/generate/color.d.mts +2 -0
- package/api/theme/palette/generate/color.mjs +1 -1
- package/api/theme-edit/_check.mjs +22 -5
- package/api/theme-edit/_color.d.mts +15 -0
- package/api/theme-edit/_color.mjs +37 -9
- package/api/theme-edit/_compiler.d.mts +6 -2
- package/api/theme-edit/_compiler.mjs +43 -27
- package/api/theme-edit/_context.d.mts +6 -2
- package/api/theme-edit/_context.mjs +4 -2
- package/api/theme-edit/_edit.mjs +117 -15
- package/api/theme-edit/_palette.mjs +57 -3
- package/api/theme-edit/_parts.mjs +26 -3
- package/api/theme-edit/_preview-app.mjs +115 -92
- package/api/theme-edit/_preview.d.mts +4 -2
- package/api/theme-edit/_preview.mjs +19 -12
- package/api/theme-edit/_settings.d.mts +10 -4
- package/api/theme-edit/_settings.mjs +140 -41
- package/api/theme-edit/_tables.mjs +2 -2
- package/api/theme-edit/theme-edit.type.d.mts +4 -4
- package/api/theme-edit/theme-edit.type.mjs +1 -1
- package/api/theme-edit/themeEditHow.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +2 -7
- package/api/theme-edit/themeEditShow.doc.mjs +1 -1
- package/api/theme-edit/themeEditUndo.doc.mjs +2 -2
- package/assets/discover/official-packages.json +4 -0
- package/clients/cli/commands/experimental/theme.mjs +14 -14
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +2 -8
- package/foundation/response/error-codes.doc.mjs +1 -1
- package/foundation/response/error-codes.mjs +1 -1
- package/foundation/response/response-types.doc.mjs +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The catalog's component and hook entries, each with the keywords that name
|
|
6
|
+
* another entry left out (see the file comment). Entries with nothing left
|
|
7
|
+
* out are returned as they are.
|
|
8
|
+
* @param {import('./_lexical.mjs').Candidate[]} all
|
|
9
|
+
* @param {import('./_lexical.mjs').Candidate[]} [named] - the entries whose
|
|
10
|
+
* names count: `all` itself, unless the caller holds a wider catalog
|
|
11
|
+
* (`all` cut to one `type`)
|
|
12
|
+
* @returns {import('./_lexical.mjs').Candidate[]}
|
|
13
|
+
*/
|
|
14
|
+
export function withoutOtherNames(all: import("./_lexical.mjs").Candidate[], named?: import("./_lexical.mjs").Candidate[]): import("./_lexical.mjs").Candidate[];
|
|
15
|
+
/**
|
|
16
|
+
* The gathered catalog as search reads it: its entries with
|
|
17
|
+
* {@link withoutOtherNames} applied. Names come from the catalog and from
|
|
18
|
+
* `others`, further gathered catalogs (a search for one `type` passes the
|
|
19
|
+
* other of components and hooks, so `type` never changes which keywords
|
|
20
|
+
* count). A reused gather, with the same others, gets the same view back.
|
|
21
|
+
* @template {{all: import('./_lexical.mjs').Candidate[], components: import('./_lexical.mjs').Candidate[]}} T
|
|
22
|
+
* @param {T} catalog
|
|
23
|
+
* @param {{all: import('./_lexical.mjs').Candidate[]}[]} [others]
|
|
24
|
+
* @returns {T}
|
|
25
|
+
*/
|
|
26
|
+
export function searchView<T extends {
|
|
27
|
+
all: import("./_lexical.mjs").Candidate[];
|
|
28
|
+
components: import("./_lexical.mjs").Candidate[];
|
|
29
|
+
}>(catalog: T, others?: {
|
|
30
|
+
all: import("./_lexical.mjs").Candidate[];
|
|
31
|
+
}[]): T;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Search's keyword rule for keywords that name another entry. A
|
|
5
|
+
* component or hook doc's declared keyword that is exactly another entry's
|
|
6
|
+
* whole name (NumberInput declaring `spinner` while a Spinner component
|
|
7
|
+
* exists) points the reader at that other entry; it is not a promise that this
|
|
8
|
+
* entry answers the word. Search does not count such a keyword as a match,
|
|
9
|
+
* unless the entry's own description uses the word too, or the other entry is
|
|
10
|
+
* its own family member or its hook/component pair (useHoverCard and
|
|
11
|
+
* HoverCard). Names are read as the ranker reads them: joined (`joined`,
|
|
12
|
+
* _words.mjs) and either plural (`pluralOf`, _query.mjs).
|
|
13
|
+
* @input the gathered catalog, and the catalogs whose components and hooks
|
|
14
|
+
* name entries (every component and hook, whatever `type` a search asks for)
|
|
15
|
+
* @output the same catalog with those keywords left out of its component and
|
|
16
|
+
* hook entries
|
|
17
|
+
* @position api/search; three places apply it: search() (search.mjs),
|
|
18
|
+
* rankSearchIndex() (search-index.mjs), and searchCore() (search.mjs) when
|
|
19
|
+
* it is passed `ownKeywords`, as search's own comparisons do. searchCore
|
|
20
|
+
* without it and build read keywords as gathered, so build's output does
|
|
21
|
+
* not move.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {pluralOf} from './_query.mjs';
|
|
25
|
+
import {joined} from './_words.mjs';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A joined word with its plural or singular, by search's plural rule
|
|
29
|
+
* (`pluralOf`): `step` and `steps`, `box` and `boxes`.
|
|
30
|
+
* @param {string} word
|
|
31
|
+
* @returns {string[]}
|
|
32
|
+
*/
|
|
33
|
+
function forms(word) {
|
|
34
|
+
const out = [word];
|
|
35
|
+
for (const plural of [`${word}s`, `${word}es`]) {
|
|
36
|
+
if (pluralOf(plural, word)) out.push(plural);
|
|
37
|
+
}
|
|
38
|
+
for (const single of [word.slice(0, -1), word.slice(0, -2)]) {
|
|
39
|
+
if (pluralOf(word, single)) out.push(single);
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Whether a joined word is a run of adjacent words of a text: `tablecell`
|
|
46
|
+
* is in "hundreds of table cell observers", `visuallyhidden` in "a
|
|
47
|
+
* visually-hidden region", and `ton` is not in "button".
|
|
48
|
+
* @param {string[]} words - the text's words, lowercased
|
|
49
|
+
* @param {string} word
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
function inWords(words, word) {
|
|
53
|
+
for (let i = 0; i < words.length; i++) {
|
|
54
|
+
if (!word.startsWith(words[i])) continue;
|
|
55
|
+
let at = 0;
|
|
56
|
+
for (let j = i; j < words.length && word.startsWith(words[j], at); j++) {
|
|
57
|
+
at += words[j].length;
|
|
58
|
+
if (at === word.length) return true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Whether two entries belong together, so one may name the other in its
|
|
66
|
+
* keywords: one is the other's family, they share a family, or one is the
|
|
67
|
+
* other's hook (`useHoverCard` and `HoverCard`).
|
|
68
|
+
* @param {import('./_lexical.mjs').Candidate} a
|
|
69
|
+
* @param {import('./_lexical.mjs').Candidate} b
|
|
70
|
+
* @returns {boolean}
|
|
71
|
+
*/
|
|
72
|
+
function related(a, b) {
|
|
73
|
+
if (a._family && (a._family === b.name || a._family === b._family)) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (b._family === a.name) return true;
|
|
77
|
+
const x = joined(a.name);
|
|
78
|
+
const y = joined(b.name);
|
|
79
|
+
return x === `use${y}` || y === `use${x}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The domains whose keywords are a doc's declared keywords. */
|
|
83
|
+
const DECLARING = new Set(['component', 'hook']);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The catalog's component and hook entries, each with the keywords that name
|
|
87
|
+
* another entry left out (see the file comment). Entries with nothing left
|
|
88
|
+
* out are returned as they are.
|
|
89
|
+
* @param {import('./_lexical.mjs').Candidate[]} all
|
|
90
|
+
* @param {import('./_lexical.mjs').Candidate[]} [named] - the entries whose
|
|
91
|
+
* names count: `all` itself, unless the caller holds a wider catalog
|
|
92
|
+
* (`all` cut to one `type`)
|
|
93
|
+
* @returns {import('./_lexical.mjs').Candidate[]}
|
|
94
|
+
*/
|
|
95
|
+
export function withoutOtherNames(all, named = all) {
|
|
96
|
+
/** @type {Map<string, import('./_lexical.mjs').Candidate[]>} */
|
|
97
|
+
const byName = new Map();
|
|
98
|
+
for (const c of named) {
|
|
99
|
+
if (!DECLARING.has(c.domain)) continue;
|
|
100
|
+
for (const name of [c.name, ...(c.aliases ?? [])]) {
|
|
101
|
+
const key = joined(name);
|
|
102
|
+
if (!key) continue;
|
|
103
|
+
const list = byName.get(key);
|
|
104
|
+
if (list) list.push(c);
|
|
105
|
+
else byName.set(key, [c]);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return all.map(c => {
|
|
109
|
+
if (!DECLARING.has(c.domain) || !c.keywords?.length) return c;
|
|
110
|
+
/** @type {string[] | null} */
|
|
111
|
+
let described = null;
|
|
112
|
+
const keywords = c.keywords.filter(keyword => {
|
|
113
|
+
const word = joined(keyword);
|
|
114
|
+
if (!word) return true;
|
|
115
|
+
const wordForms = forms(word);
|
|
116
|
+
/** @type {import('./_lexical.mjs').Candidate[]} */
|
|
117
|
+
const others = [];
|
|
118
|
+
for (const form of wordForms) {
|
|
119
|
+
for (const o of byName.get(form) ?? []) {
|
|
120
|
+
if (o !== c && o.name !== c.name) others.push(o);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (others.length === 0) return true;
|
|
124
|
+
described ??=
|
|
125
|
+
String(c.description ?? '')
|
|
126
|
+
.toLowerCase()
|
|
127
|
+
.match(/[a-z0-9]+/g) ?? [];
|
|
128
|
+
const words = described;
|
|
129
|
+
if (wordForms.some(form => inWords(words, form))) return true;
|
|
130
|
+
return others.some(o => related(c, o));
|
|
131
|
+
});
|
|
132
|
+
return keywords.length === c.keywords.length ? c : {...c, keywords};
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Each gathered catalog's search views, one per set of naming catalogs.
|
|
138
|
+
* @type {WeakMap<object, {others: object[], view: object}[]>}
|
|
139
|
+
*/
|
|
140
|
+
const views = new WeakMap();
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The gathered catalog as search reads it: its entries with
|
|
144
|
+
* {@link withoutOtherNames} applied. Names come from the catalog and from
|
|
145
|
+
* `others`, further gathered catalogs (a search for one `type` passes the
|
|
146
|
+
* other of components and hooks, so `type` never changes which keywords
|
|
147
|
+
* count). A reused gather, with the same others, gets the same view back.
|
|
148
|
+
* @template {{all: import('./_lexical.mjs').Candidate[], components: import('./_lexical.mjs').Candidate[]}} T
|
|
149
|
+
* @param {T} catalog
|
|
150
|
+
* @param {{all: import('./_lexical.mjs').Candidate[]}[]} [others]
|
|
151
|
+
* @returns {T}
|
|
152
|
+
*/
|
|
153
|
+
export function searchView(catalog, others = []) {
|
|
154
|
+
const seen = views.get(catalog) ?? [];
|
|
155
|
+
const hit = seen.find(
|
|
156
|
+
v =>
|
|
157
|
+
v.others.length === others.length &&
|
|
158
|
+
v.others.every((o, i) => o === others[i]),
|
|
159
|
+
);
|
|
160
|
+
if (hit) return /** @type {T} */ (hit.view);
|
|
161
|
+
const all = withoutOtherNames(
|
|
162
|
+
catalog.all,
|
|
163
|
+
others.length === 0
|
|
164
|
+
? catalog.all
|
|
165
|
+
: [catalog.all, ...others.map(o => o.all)].flat(),
|
|
166
|
+
);
|
|
167
|
+
const swap = new Map(catalog.all.map((c, i) => [c, all[i]]));
|
|
168
|
+
const view = {
|
|
169
|
+
...catalog,
|
|
170
|
+
all,
|
|
171
|
+
components: catalog.components.map(c => swap.get(c) ?? c),
|
|
172
|
+
};
|
|
173
|
+
// Only the latest view per catalog is kept: a search reuses one set of
|
|
174
|
+
// naming catalogs until one of them gathers again.
|
|
175
|
+
views.set(catalog, [{others, view}]);
|
|
176
|
+
return view;
|
|
177
|
+
}
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
} from './_adapter.mjs';
|
|
29
29
|
import {checkSearchArgs} from './_result.mjs';
|
|
30
30
|
import {SEARCH_TREE} from './tree.mjs';
|
|
31
|
+
import {searchView} from './_named-keywords.mjs';
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Fields a gathered candidate holds that point into the folder it was read
|
|
@@ -185,8 +186,14 @@ export async function rankSearchIndex(query, indexes, options = {}) {
|
|
|
185
186
|
all.push(...index.candidates);
|
|
186
187
|
}
|
|
187
188
|
const {type} = options;
|
|
189
|
+
// Keywords read as search() reads them (_named-keywords.mjs). The names
|
|
190
|
+
// come from every stored entry, before the `type` cut.
|
|
191
|
+
const view = searchView({
|
|
192
|
+
all,
|
|
193
|
+
components: all.filter(candidate => candidate.domain === 'component'),
|
|
194
|
+
}).all;
|
|
188
195
|
const wanted =
|
|
189
|
-
type == null ?
|
|
196
|
+
type == null ? view : view.filter(candidate => candidate.domain === type);
|
|
190
197
|
const {searchedComponents, coreSkipped} = coreFlags(list, type);
|
|
191
198
|
const response = /** @type {import('./search.type.mjs').SearchResponse} */ (
|
|
192
199
|
await runTree(
|
package/api/search/search.d.mts
CHANGED
|
@@ -22,14 +22,19 @@ export function search(query: string, options?: {
|
|
|
22
22
|
* same way. A later query in the same folder, for the same `type`, gathers
|
|
23
23
|
* again only when an input changed.
|
|
24
24
|
*
|
|
25
|
+
* `ownKeywords` ranks as `search` reads keywords: a doc keyword that is
|
|
26
|
+
* another entry's name is not a match (_named-keywords.mjs). Only search's own
|
|
27
|
+
* comparisons set it; build never does, so build reads every keyword.
|
|
28
|
+
*
|
|
25
29
|
* @param {string} query
|
|
26
|
-
* @param {{cwd?: string, type?: 'component'|'hook'|'doc'|'template'|'theme', limit?: number}} [options]
|
|
30
|
+
* @param {{cwd?: string, type?: 'component'|'hook'|'doc'|'template'|'theme', limit?: number, ownKeywords?: boolean}} [options]
|
|
27
31
|
* @returns {Promise<import('./search.type.mjs').SearchResponse>}
|
|
28
32
|
*/
|
|
29
33
|
export function searchCore(query: string, options?: {
|
|
30
34
|
cwd?: string;
|
|
31
35
|
type?: "component" | "hook" | "doc" | "template" | "theme";
|
|
32
36
|
limit?: number;
|
|
37
|
+
ownKeywords?: boolean;
|
|
33
38
|
}): Promise<import("./search.type.mjs").SearchResponse>;
|
|
34
39
|
export { scoreCandidate } from "./_candidate-score.mjs";
|
|
35
40
|
export { scoreQuery } from "./_score.mjs";
|
package/api/search/search.mjs
CHANGED
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
* block), and themes. The adapter gathers every candidate (_adapter.mjs),
|
|
12
12
|
* and search's tree (tree.mjs) picks the searcher that answers.
|
|
13
13
|
* `searchCore(query)` is the lexical ranker alone (_lexical.mjs); `build`
|
|
14
|
-
* reads it, so build's output does not move when search's tree does.
|
|
14
|
+
* reads it, so build's output does not move when search's tree does. A doc
|
|
15
|
+
* keyword that is another entry's name is not a match (_named-keywords.mjs)
|
|
16
|
+
* in three places: `search`, `rankSearchIndex` (search-index.mjs), and
|
|
17
|
+
* `searchCore` with `ownKeywords`. `searchCore` without it, which build
|
|
18
|
+
* reads, reads every keyword as gathered.
|
|
15
19
|
*/
|
|
16
20
|
|
|
17
21
|
import {
|
|
@@ -21,6 +25,7 @@ import {
|
|
|
21
25
|
import {withListingCache} from '../../foundation/fs/listing-cache.mjs';
|
|
22
26
|
import {gatherCandidatesReused, loadSearchData} from './_adapter.mjs';
|
|
23
27
|
import {rankLexical} from './_lexical.mjs';
|
|
28
|
+
import {searchView} from './_named-keywords.mjs';
|
|
24
29
|
import {checkSearchArgs} from './_result.mjs';
|
|
25
30
|
import {runTree} from '../../foundation/route/route.mjs';
|
|
26
31
|
import {SEARCH_TREE} from './tree.mjs';
|
|
@@ -73,7 +78,10 @@ async function searchRead(query, options) {
|
|
|
73
78
|
SEARCH_TREE,
|
|
74
79
|
{query, options},
|
|
75
80
|
{
|
|
76
|
-
|
|
81
|
+
// A keyword that names another entry is not a match here
|
|
82
|
+
// (_named-keywords.mjs); searchCore, which build reads,
|
|
83
|
+
// keeps every keyword.
|
|
84
|
+
...(await searchCatalog(options)),
|
|
77
85
|
data: loadSearchData(),
|
|
78
86
|
},
|
|
79
87
|
)
|
|
@@ -84,14 +92,41 @@ async function searchRead(query, options) {
|
|
|
84
92
|
);
|
|
85
93
|
}
|
|
86
94
|
|
|
95
|
+
/**
|
|
96
|
+
* The other declaring domain, whose names a search for one `type` still reads.
|
|
97
|
+
* @type {Record<string, string>}
|
|
98
|
+
*/
|
|
99
|
+
const NAMING_DOMAIN = {component: 'hook', hook: 'component'};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The gathered catalog for `options`, with keywords as search reads them
|
|
103
|
+
* (_named-keywords.mjs). The names come from every component and hook, so a
|
|
104
|
+
* search for one `type` keeps and drops the keywords an open search does.
|
|
105
|
+
* @param {{cwd?: string, type?: string}} options
|
|
106
|
+
*/
|
|
107
|
+
async function searchCatalog(options) {
|
|
108
|
+
const gathered = await gatherCandidatesReused(options);
|
|
109
|
+
const other = options.type == null ? null : NAMING_DOMAIN[options.type];
|
|
110
|
+
return searchView(
|
|
111
|
+
gathered,
|
|
112
|
+
other == null
|
|
113
|
+
? []
|
|
114
|
+
: [await gatherCandidatesReused({cwd: options.cwd, type: other})],
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
87
118
|
/**
|
|
88
119
|
* The lexical ranker alone: keyword and fuzzy scoring of every candidate.
|
|
89
120
|
* `build` ranks from it. It ranks the candidates search gathers, reused the
|
|
90
121
|
* same way. A later query in the same folder, for the same `type`, gathers
|
|
91
122
|
* again only when an input changed.
|
|
92
123
|
*
|
|
124
|
+
* `ownKeywords` ranks as `search` reads keywords: a doc keyword that is
|
|
125
|
+
* another entry's name is not a match (_named-keywords.mjs). Only search's own
|
|
126
|
+
* comparisons set it; build never does, so build reads every keyword.
|
|
127
|
+
*
|
|
93
128
|
* @param {string} query
|
|
94
|
-
* @param {{cwd?: string, type?: 'component'|'hook'|'doc'|'template'|'theme', limit?: number}} [options]
|
|
129
|
+
* @param {{cwd?: string, type?: 'component'|'hook'|'doc'|'template'|'theme', limit?: number, ownKeywords?: boolean}} [options]
|
|
95
130
|
* @returns {Promise<import('./search.type.mjs').SearchResponse>}
|
|
96
131
|
*/
|
|
97
132
|
export async function searchCore(query, options = {}) {
|
|
@@ -108,10 +143,16 @@ export async function searchCore(query, options = {}) {
|
|
|
108
143
|
* @param {any} options
|
|
109
144
|
*/
|
|
110
145
|
async function searchCoreRead(query, options) {
|
|
111
|
-
return withListingCache(async () =>
|
|
112
|
-
linkSearchResponse(
|
|
113
|
-
rankLexical(
|
|
146
|
+
return withListingCache(async () => {
|
|
147
|
+
return linkSearchResponse(
|
|
148
|
+
rankLexical(
|
|
149
|
+
query,
|
|
150
|
+
options,
|
|
151
|
+
options.ownKeywords
|
|
152
|
+
? await searchCatalog(options)
|
|
153
|
+
: await gatherCandidatesReused(options),
|
|
154
|
+
),
|
|
114
155
|
options.cwd ?? process.cwd(),
|
|
115
|
-
)
|
|
116
|
-
);
|
|
156
|
+
);
|
|
157
|
+
});
|
|
117
158
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
2
|
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
3
|
|
|
4
|
+
/** @param {number} lightness @param {number} a @param {number} b @returns {ColorTriple} */
|
|
5
|
+
export function oklabToLinearRgb(lightness: number, a: number, b: number): ColorTriple;
|
|
4
6
|
/** @param {unknown} input @returns {string} */
|
|
5
7
|
export function normalizeColor(input: unknown): string;
|
|
6
8
|
/** @param {string} hex @returns {{L: number, C: number, H: number}} */
|
|
@@ -63,7 +63,7 @@ function linearRgbToOklab(red, green, blue) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/** @param {number} lightness @param {number} a @param {number} b @returns {ColorTriple} */
|
|
66
|
-
function oklabToLinearRgb(lightness, a, b) {
|
|
66
|
+
export function oklabToLinearRgb(lightness, a, b) {
|
|
67
67
|
const lRoot = lightness + 0.3963377774 * a + 0.2158037573 * b;
|
|
68
68
|
const mRoot = lightness - 0.1055613458 * a - 0.0638541728 * b;
|
|
69
69
|
const sRoot = lightness - 0.0894841775 * a - 1.291485548 * b;
|
|
@@ -800,11 +800,28 @@ export function answerHow(ctx, question) {
|
|
|
800
800
|
!(/\bwhite\b/.test(text) && !whiteText)
|
|
801
801
|
)
|
|
802
802
|
intents = intents.filter(g => g !== 'background');
|
|
803
|
-
// "border color" is the border's color, not a fill; a
|
|
804
|
-
//
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
803
|
+
// "border color" or "a gray border" is the border's color, not a fill; a
|
|
804
|
+
// color word for anything else ("a dark card with a border", "the card
|
|
805
|
+
// color and its border", "colored buttons") is the fill's.
|
|
806
|
+
const fillWords = [...COLOR_WORDS, 'dark', 'light', 'colou?r(ed)?'].join('|');
|
|
807
|
+
// Take out every color word that belongs to the border, the text or the
|
|
808
|
+
// mode ("a light gray border", "the color of the border", "in dark
|
|
809
|
+
// mode"); a color word left over is the fill's.
|
|
810
|
+
const rest = text
|
|
811
|
+
.replace(/\b(dark|light) (mode|theme)\b/g, ' ')
|
|
812
|
+
.replace(
|
|
813
|
+
/\bcolou?r of (the |its |a )?(border|outline|stroke|edge|text|labels?)s?\b/g,
|
|
814
|
+
' ',
|
|
815
|
+
)
|
|
816
|
+
.replace(/\b(border|outline|stroke|edge|text)s? colou?r\b/g, ' ')
|
|
817
|
+
.replace(
|
|
818
|
+
new RegExp(
|
|
819
|
+
`\\b((${fillWords}|thin|thick|subtle|bold)\\s+)+(border|outline|stroke|edge|text|labels?)s?\\b`,
|
|
820
|
+
'g',
|
|
821
|
+
),
|
|
822
|
+
' ',
|
|
823
|
+
);
|
|
824
|
+
const looseColor = new RegExp(`\\b(${fillWords})\\b`).test(rest);
|
|
808
825
|
if (
|
|
809
826
|
intents.includes('border') &&
|
|
810
827
|
intents.includes('background') &&
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
2
|
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The opacity of a CSS color, 0 to 1: the alpha of an 8- or 4-digit hex,
|
|
6
|
+
* `rgba()`/`hsla()`, or a `/ alpha` part; 0 for `transparent`; 1 otherwise.
|
|
7
|
+
* @param {unknown} value
|
|
8
|
+
* @returns {number}
|
|
9
|
+
*/
|
|
10
|
+
export function alphaOf(value: unknown): number;
|
|
11
|
+
/**
|
|
12
|
+
* A color drawn at `alpha` over `under`, as the opaque color the eye sees.
|
|
13
|
+
* @param {Rgb} over
|
|
14
|
+
* @param {number} alpha
|
|
15
|
+
* @param {Rgb} under
|
|
16
|
+
* @returns {Rgb}
|
|
17
|
+
*/
|
|
18
|
+
export function compositeOver(over: Rgb, alpha: number, under: Rgb): Rgb;
|
|
4
19
|
/**
|
|
5
20
|
* The RGB of a fixed CSS color: a hex, a named color, or rgb(), hsl(),
|
|
6
21
|
* hwb(), lab(), lch(), oklab() or oklch(). Null for a value with no fixed
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* @position api/theme-edit — pure helpers
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {luminance} from '../theme/palette/generate/color.mjs';
|
|
10
|
+
import {luminance, oklabToLinearRgb} from '../theme/palette/generate/color.mjs';
|
|
11
11
|
|
|
12
12
|
/** @typedef {[number, number, number]} Rgb */
|
|
13
13
|
|
|
@@ -259,14 +259,7 @@ function labToRgb(l, a, b) {
|
|
|
259
259
|
|
|
260
260
|
/** @param {number} l @param {number} a @param {number} b OKLab @returns {Rgb} */
|
|
261
261
|
function oklabToRgb(l, a, b) {
|
|
262
|
-
|
|
263
|
-
const m1 = (l - 0.1055613458 * a - 0.0638541728 * b) ** 3;
|
|
264
|
-
const s1 = (l - 0.0894841775 * a - 1.291485548 * b) ** 3;
|
|
265
|
-
return fromLinear([
|
|
266
|
-
4.0767416621 * l1 - 3.3077115913 * m1 + 0.2309699292 * s1,
|
|
267
|
-
-1.2684380046 * l1 + 2.6097574011 * m1 - 0.3413193965 * s1,
|
|
268
|
-
-0.0041960863 * l1 - 0.7034186147 * m1 + 1.707614701 * s1,
|
|
269
|
-
]);
|
|
262
|
+
return fromLinear(oklabToLinearRgb(l, a, b));
|
|
270
263
|
}
|
|
271
264
|
|
|
272
265
|
/** @param {number} c @param {number} h degrees @returns {[number, number]} */
|
|
@@ -275,6 +268,41 @@ const polar = (c, h) => [
|
|
|
275
268
|
c * Math.sin((h * Math.PI) / 180),
|
|
276
269
|
];
|
|
277
270
|
|
|
271
|
+
/**
|
|
272
|
+
* The opacity of a CSS color, 0 to 1: the alpha of an 8- or 4-digit hex,
|
|
273
|
+
* `rgba()`/`hsla()`, or a `/ alpha` part; 0 for `transparent`; 1 otherwise.
|
|
274
|
+
* @param {unknown} value
|
|
275
|
+
* @returns {number}
|
|
276
|
+
*/
|
|
277
|
+
export function alphaOf(value) {
|
|
278
|
+
const s = String(value ?? '').trim();
|
|
279
|
+
if (/^transparent$/i.test(s)) return 0;
|
|
280
|
+
const h = /^#([0-9a-f]{4}|[0-9a-f]{8})$/i.exec(s);
|
|
281
|
+
if (h) {
|
|
282
|
+
const a = h[1].length === 4 ? h[1][3].repeat(2) : h[1].slice(6);
|
|
283
|
+
return parseInt(a, 16) / 255;
|
|
284
|
+
}
|
|
285
|
+
const slash = /\/\s*(-?[\d.]+)(%?)\s*\)$/.exec(s);
|
|
286
|
+
const comma = /^(?:rgba|hsla)\((?:[^,]*,){3}\s*(-?[\d.]+)(%?)\s*\)$/i.exec(s);
|
|
287
|
+
const m = slash ?? comma;
|
|
288
|
+
if (!m) return 1;
|
|
289
|
+
const n = Number(m[1]) / (m[2] ? 100 : 1);
|
|
290
|
+
return Math.max(0, Math.min(1, n));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* A color drawn at `alpha` over `under`, as the opaque color the eye sees.
|
|
295
|
+
* @param {Rgb} over
|
|
296
|
+
* @param {number} alpha
|
|
297
|
+
* @param {Rgb} under
|
|
298
|
+
* @returns {Rgb}
|
|
299
|
+
*/
|
|
300
|
+
export function compositeOver(over, alpha, under) {
|
|
301
|
+
return /** @type {Rgb} */ (
|
|
302
|
+
over.map((c, i) => c * alpha + under[i] * (1 - alpha))
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
278
306
|
/**
|
|
279
307
|
* The RGB of a fixed CSS color: a hex, a named color, or rgb(), hsl(),
|
|
280
308
|
* hwb(), lab(), lch(), oklab() or oklch(). Null for a value with no fixed
|
|
@@ -31,9 +31,12 @@ export function makeCompiler(project: import("./_project.mjs").ThemeProject, par
|
|
|
31
31
|
checkTokenOn: (theme: string, target: string, token: string, value: string, P: string) => CheckFailure | null;
|
|
32
32
|
checkTokenGlobal: (token: string, P: string) => CheckFailure | null;
|
|
33
33
|
variantValues: (target: string) => Record<string, string[]>;
|
|
34
|
-
contrast: (value: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, baseValue: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, modes: string[], { parseColor, ratio }: {
|
|
35
|
-
parseColor: (v: unknown) => number
|
|
34
|
+
contrast: (value: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, baseValue: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, modes: string[], { parseColor, ratio, alphaOf, compositeOver, page }: {
|
|
35
|
+
parseColor: (v: unknown) => [number, number, number] | null;
|
|
36
36
|
ratio: (a: number[], b: number[]) => number;
|
|
37
|
+
alphaOf: (v: unknown) => number;
|
|
38
|
+
compositeOver: (over: [number, number, number], alpha: number, under: [number, number, number]) => [number, number, number];
|
|
39
|
+
page: (mode: string) => string | null | undefined;
|
|
37
40
|
}) => {
|
|
38
41
|
problems: {
|
|
39
42
|
part: string;
|
|
@@ -44,6 +47,7 @@ export function makeCompiler(project: import("./_project.mjs").ThemeProject, par
|
|
|
44
47
|
bgPart: string | null;
|
|
45
48
|
bg: string;
|
|
46
49
|
}[];
|
|
50
|
+
more: number;
|
|
47
51
|
unchecked: string[];
|
|
48
52
|
};
|
|
49
53
|
};
|
|
@@ -11,21 +11,6 @@
|
|
|
11
11
|
|
|
12
12
|
import {DIRECT, GROUPS, HOW} from './_tables.mjs';
|
|
13
13
|
|
|
14
|
-
/**
|
|
15
|
-
* Whether a color is see-through: its contrast depends on what is under it,
|
|
16
|
-
* so it is not judged alone.
|
|
17
|
-
* @param {unknown} v
|
|
18
|
-
*/
|
|
19
|
-
function hasAlpha(v) {
|
|
20
|
-
const s = String(v ?? '').trim();
|
|
21
|
-
const h = /^#(?:[0-9a-f]{4}|[0-9a-f]{8})$/i.exec(s);
|
|
22
|
-
if (h) return s.length === 5 ? !/f$/i.test(s) : !/ff$/i.test(s);
|
|
23
|
-
const m = /\/\s*([\d.]+%?)\s*\)$|^rgba\([^)]*,\s*([\d.]+)\s*\)$/i.exec(s);
|
|
24
|
-
if (!m) return /^transparent$/i.test(s);
|
|
25
|
-
const a = m[1] ?? m[2];
|
|
26
|
-
return a.endsWith('%') ? parseFloat(a) < 100 : parseFloat(a) < 1;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
14
|
/** Only these token types may differ between light and dark. */
|
|
30
15
|
export const MODAL = new Set(['color', 'shadow']);
|
|
31
16
|
|
|
@@ -386,9 +371,34 @@ export function makeCompiler(project, parts) {
|
|
|
386
371
|
* @param {(pair: import('./_parts.mjs').TextPair, side: 'fg' | 'bg', mode: string) => string | null | undefined} value
|
|
387
372
|
* @param {(pair: import('./_parts.mjs').TextPair, side: 'fg' | 'bg', mode: string) => string | null | undefined} baseValue
|
|
388
373
|
* @param {string[]} modes
|
|
389
|
-
* @param {{parseColor: (v: unknown) => number
|
|
374
|
+
* @param {{parseColor: (v: unknown) => [number, number, number] | null, ratio: (a: number[], b: number[]) => number, alphaOf: (v: unknown) => number, compositeOver: (over: [number, number, number], alpha: number, under: [number, number, number]) => [number, number, number], page: (mode: string) => string | null | undefined}} helpers
|
|
375
|
+
* `page` is the page color in a mode: a see-through background is drawn
|
|
376
|
+
* over it, and see-through text over that background, before measuring.
|
|
390
377
|
*/
|
|
391
|
-
function contrast(
|
|
378
|
+
function contrast(
|
|
379
|
+
value,
|
|
380
|
+
baseValue,
|
|
381
|
+
modes,
|
|
382
|
+
{parseColor, ratio, alphaOf, compositeOver, page},
|
|
383
|
+
) {
|
|
384
|
+
/**
|
|
385
|
+
* The opaque colors the eye sees for a pair, or null with no fixed color.
|
|
386
|
+
* @param {unknown} fg @param {unknown} bg @param {string} m
|
|
387
|
+
*/
|
|
388
|
+
const drawn = (fg, bg, m) => {
|
|
389
|
+
/** @type {[number, number, number]} */
|
|
390
|
+
const ground =
|
|
391
|
+
parseColor(page(m)) ?? (m === 'dark' ? [0, 0, 0] : [255, 255, 255]);
|
|
392
|
+
const bgAlpha = alphaOf(bg);
|
|
393
|
+
const back = parseColor(bg);
|
|
394
|
+
const under =
|
|
395
|
+
bgAlpha === 0
|
|
396
|
+
? ground
|
|
397
|
+
: back && compositeOver(back, bgAlpha, ground);
|
|
398
|
+
const top = parseColor(fg);
|
|
399
|
+
if (!under || !top) return null;
|
|
400
|
+
return [compositeOver(top, alphaOf(fg), under), under];
|
|
401
|
+
};
|
|
392
402
|
/** @type {Array<{part: string, mode: string, ratio: number, sample: string, fg: string, bgPart: string | null, bg: string}>} */
|
|
393
403
|
const out = [];
|
|
394
404
|
const seen = new Set();
|
|
@@ -405,23 +415,25 @@ export function makeCompiler(project, parts) {
|
|
|
405
415
|
const fg = value(p, 'fg', m);
|
|
406
416
|
const bg = value(p, 'bg', m);
|
|
407
417
|
if (/gradient|url\(/.test(String(bg))) continue;
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
const b = parseColor(bg);
|
|
411
|
-
if (!a || !b) {
|
|
418
|
+
const pair = drawn(fg, bg, m);
|
|
419
|
+
if (!pair) {
|
|
412
420
|
for (const [v, side] of /** @type {const} */ ([
|
|
413
421
|
[fg, 'fg'],
|
|
414
422
|
[bg, 'bg'],
|
|
415
423
|
]))
|
|
416
|
-
if (
|
|
424
|
+
if (
|
|
425
|
+
v &&
|
|
426
|
+
!parseColor(v) &&
|
|
427
|
+
alphaOf(v) !== 0 &&
|
|
428
|
+
v !== baseValue(p, side, m)
|
|
429
|
+
)
|
|
417
430
|
unchecked.add(String(v));
|
|
418
431
|
continue;
|
|
419
432
|
}
|
|
420
|
-
const r = ratio(
|
|
433
|
+
const r = ratio(pair[0], pair[1]);
|
|
421
434
|
if (r >= 4.5) continue;
|
|
422
|
-
const
|
|
423
|
-
|
|
424
|
-
if (a0 && b0 && ratio(a0, b0) <= r + 0.05) continue;
|
|
435
|
+
const was = drawn(baseValue(p, 'fg', m), baseValue(p, 'bg', m), m);
|
|
436
|
+
if (was && ratio(was[0], was[1]) <= r + 0.05) continue;
|
|
425
437
|
const key = `${p.text}|${fg}|${bg}|${m}`;
|
|
426
438
|
if (seen.has(key)) continue;
|
|
427
439
|
seen.add(key);
|
|
@@ -436,7 +448,11 @@ export function makeCompiler(project, parts) {
|
|
|
436
448
|
});
|
|
437
449
|
}
|
|
438
450
|
}
|
|
439
|
-
return {
|
|
451
|
+
return {
|
|
452
|
+
problems: out.slice(0, 8),
|
|
453
|
+
more: Math.max(0, out.length - 8),
|
|
454
|
+
unchecked: [...unchecked],
|
|
455
|
+
};
|
|
440
456
|
}
|
|
441
457
|
|
|
442
458
|
/**
|
|
@@ -39,9 +39,12 @@ export function context(cwd: string): Promise<{
|
|
|
39
39
|
checkTokenOn: (theme: string, target: string, token: string, value: string, P: string) => CheckFailure | null;
|
|
40
40
|
checkTokenGlobal: (token: string, P: string) => CheckFailure | null;
|
|
41
41
|
variantValues: (target: string) => Record<string, string[]>;
|
|
42
|
-
contrast: (value: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, baseValue: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, modes: string[], { parseColor, ratio }: {
|
|
43
|
-
parseColor: (v: unknown) => number
|
|
42
|
+
contrast: (value: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, baseValue: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, modes: string[], { parseColor, ratio, alphaOf, compositeOver, page }: {
|
|
43
|
+
parseColor: (v: unknown) => [number, number, number] | null;
|
|
44
44
|
ratio: (a: number[], b: number[]) => number;
|
|
45
|
+
alphaOf: (v: unknown) => number;
|
|
46
|
+
compositeOver: (over: [number, number, number], alpha: number, under: [number, number, number]) => [number, number, number];
|
|
47
|
+
page: (mode: string) => string | null | undefined;
|
|
45
48
|
}) => {
|
|
46
49
|
problems: {
|
|
47
50
|
part: string;
|
|
@@ -52,6 +55,7 @@ export function context(cwd: string): Promise<{
|
|
|
52
55
|
bgPart: string | null;
|
|
53
56
|
bg: string;
|
|
54
57
|
}[];
|
|
58
|
+
more: number;
|
|
55
59
|
unchecked: string[];
|
|
56
60
|
};
|
|
57
61
|
};
|
|
@@ -258,8 +258,10 @@ export async function previewAfter(ctx, slug, data, png) {
|
|
|
258
258
|
try {
|
|
259
259
|
preview = await renderPreview(ctx, slug, data);
|
|
260
260
|
} catch (e) {
|
|
261
|
-
|
|
262
|
-
cause ??=
|
|
261
|
+
// The change is already saved: any failure here leaves it unchecked.
|
|
262
|
+
cause ??= isRenderFailure(e)
|
|
263
|
+
? renderCause(ctx.project, /** @type {Error} */ (e).message)
|
|
264
|
+
: `checking it failed: ${e instanceof Error ? e.message : String(e)}`;
|
|
263
265
|
}
|
|
264
266
|
const f = themeFiles(ctx.project.cwd, slug);
|
|
265
267
|
const shots =
|