pi-pocket 0.26.0 → 0.27.0-beta.2
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/CONTRIBUTING.md +1 -1
- package/docs/design/design-system.md +165 -0
- package/docs/design/ds-notes-b.md +61 -0
- package/docs/design/ds-notes-g.md +170 -0
- package/docs/design/ds-notes-t.md +303 -0
- package/docs/design/ds-notes-u.md +220 -0
- package/docs/design/ds-notes-v.md +186 -0
- package/docs/design/ds-rounds.md +160 -0
- package/docs/design/ds-u-bridge.patch +732 -0
- package/docs/examples/design-system/README.md +15 -0
- package/docs/examples/design-system/pocket/.pi/design/components/badge.html +112 -0
- package/docs/examples/design-system/pocket/.pi/design/components/button.html +203 -0
- package/docs/examples/design-system/pocket/.pi/design/components/card.html +125 -0
- package/docs/examples/design-system/pocket/.pi/design/components/code-block.html +155 -0
- package/docs/examples/design-system/pocket/.pi/design/components/input.html +156 -0
- package/docs/examples/design-system/pocket/.pi/design/components/menu-row.html +167 -0
- package/docs/examples/design-system/pocket/.pi/design/components/notice.html +171 -0
- package/docs/examples/design-system/pocket/.pi/design/components/segmented.html +120 -0
- package/docs/examples/design-system/pocket/.pi/design/components/switch.html +117 -0
- package/docs/examples/design-system/pocket/.pi/design/components/tool-step.html +163 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/components.css +730 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/names.md +495 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tailwind.css +1132 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.css +1126 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.dark.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.high-contrast.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.light.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.resolved.json +371 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.studio.json +5915 -0
- package/docs/examples/design-system/pocket/.pi/design/guidelines.md +81 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.dark.json +182 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.high-contrast.json +185 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.json +2034 -0
- package/docs/features.md +6 -2
- package/docs/index.md +1 -0
- package/docs/map.md +34 -25
- package/docs/release-notes.md +1 -1
- package/docs/releases.md +45 -0
- package/package.json +2 -1
- package/src/server/design/check-cli.ts +283 -0
- package/src/server/design/color.ts +654 -0
- package/src/server/design/components.ts +328 -0
- package/src/server/design/docsite-assets.ts +29 -0
- package/src/server/design/docsite-color-section.ts +366 -0
- package/src/server/design/docsite-color.ts +303 -0
- package/src/server/design/docsite-components.ts +89 -0
- package/src/server/design/docsite-core.ts +634 -0
- package/src/server/design/docsite-css-base.ts +397 -0
- package/src/server/design/docsite-css-content.ts +477 -0
- package/src/server/design/docsite-css-phone.ts +251 -0
- package/src/server/design/docsite-css-reference.ts +374 -0
- package/src/server/design/docsite-css-specimens.ts +465 -0
- package/src/server/design/docsite-js-code.ts +96 -0
- package/src/server/design/docsite-js-core.ts +72 -0
- package/src/server/design/docsite-js-demos.ts +24 -0
- package/src/server/design/docsite-js-links.ts +25 -0
- package/src/server/design/docsite-js-nav.ts +73 -0
- package/src/server/design/docsite-js-stages.ts +81 -0
- package/src/server/design/docsite-js-table.ts +81 -0
- package/src/server/design/docsite-markdown.ts +221 -0
- package/src/server/design/docsite-motion.ts +110 -0
- package/src/server/design/docsite-reference.ts +269 -0
- package/src/server/design/docsite-sanitize.ts +193 -0
- package/src/server/design/docsite-shadows.ts +45 -0
- package/src/server/design/docsite-space.ts +289 -0
- package/src/server/design/docsite-type.ts +167 -0
- package/src/server/design/docsite.ts +508 -0
- package/src/server/design/export.ts +86 -9
- package/src/server/design/fix.ts +666 -0
- package/src/server/design/guide.ts +137 -22
- package/src/server/design/index.ts +368 -15
- package/src/server/design/lint-color.ts +293 -0
- package/src/server/design/lint-page-checks.ts +204 -0
- package/src/server/design/lint-page-elements.ts +252 -0
- package/src/server/design/lint-page-focus.ts +165 -0
- package/src/server/design/lint-page-literals.ts +293 -0
- package/src/server/design/lint-page-setup.ts +102 -0
- package/src/server/design/lint-page-states.ts +121 -0
- package/src/server/design/lint-page-values.ts +329 -0
- package/src/server/design/lint-page.ts +101 -0
- package/src/server/design/lint-suggest.ts +340 -0
- package/src/server/design/lint.ts +1136 -0
- package/src/server/design/render.ts +369 -18
- package/src/server/design/scan-braces.ts +115 -0
- package/src/server/design/scan-categorize.ts +187 -0
- package/src/server/design/scan-channels.ts +113 -0
- package/src/server/design/scan-color-base.ts +209 -0
- package/src/server/design/scan-color-names.ts +403 -0
- package/src/server/design/scan-color-palette.ts +91 -0
- package/src/server/design/scan-color-roles.ts +565 -0
- package/src/server/design/scan-colors.ts +22 -0
- package/src/server/design/scan-facts-antd.ts +171 -0
- package/src/server/design/scan-facts-files.ts +300 -0
- package/src/server/design/scan-facts-lists.ts +538 -0
- package/src/server/design/scan-facts-sass.ts +113 -0
- package/src/server/design/scan-facts-theme.ts +400 -0
- package/src/server/design/scan-facts-use.ts +674 -0
- package/src/server/design/scan-facts-wordpress.ts +374 -0
- package/src/server/design/scan-facts.ts +285 -0
- package/src/server/design/scan-files.ts +384 -0
- package/src/server/design/scan-import-convert.ts +277 -0
- package/src/server/design/scan-import-eval.ts +583 -0
- package/src/server/design/scan-import-material.ts +745 -0
- package/src/server/design/scan-import-sd-config.ts +370 -0
- package/src/server/design/scan-import-sd.ts +381 -0
- package/src/server/design/scan-import-studio.ts +242 -0
- package/src/server/design/scan-import.ts +336 -0
- package/src/server/design/scan-infer.ts +313 -0
- package/src/server/design/scan-js-eval.ts +1267 -0
- package/src/server/design/scan-palette.ts +485 -0
- package/src/server/design/scan-parse.ts +1134 -0
- package/src/server/design/scan-prepare.ts +402 -0
- package/src/server/design/scan-render-pages.ts +367 -0
- package/src/server/design/scan-render-probe.ts +229 -0
- package/src/server/design/scan-render.ts +524 -0
- package/src/server/design/scan-review.ts +54 -0
- package/src/server/design/scan-roles.ts +259 -0
- package/src/server/design/scan-sass-colors.ts +275 -0
- package/src/server/design/scan-sass-eval.ts +269 -0
- package/src/server/design/scan-sass-expr.ts +911 -0
- package/src/server/design/scan-sass-modules.ts +353 -0
- package/src/server/design/scan-sass-run.ts +1003 -0
- package/src/server/design/scan-sass-stmt.ts +256 -0
- package/src/server/design/scan-sass-values.ts +886 -0
- package/src/server/design/scan-scales.ts +983 -0
- package/src/server/design/scan-tailwind.ts +34 -0
- package/src/server/design/scan-theme-shapes.ts +579 -0
- package/src/server/design/scan-tree.ts +290 -0
- package/src/server/design/scan-util.ts +44 -0
- package/src/server/design/scan-variables.ts +470 -0
- package/src/server/design/scan.ts +703 -0
- package/src/server/design/system-schema.ts +68 -0
- package/src/server/design/system-types.ts +279 -0
- package/src/server/design/system.ts +510 -82
- package/src/server/design/tokens-base.ts +123 -0
- package/src/server/design/tokens-compile.ts +440 -0
- package/src/server/design/tokens-dtcg.ts +354 -0
- package/src/server/design/tokens-io.ts +417 -0
- package/src/server/design/tokens-model.ts +1163 -0
- package/src/server/design/tokens-names.ts +112 -0
- package/src/server/design/tokens-ramps.ts +112 -0
- package/src/server/design/tokens-summary.ts +317 -0
- package/src/server/design/tokens-tailwind.ts +324 -0
- package/src/server/design/tokens-values.ts +921 -0
- package/src/server/design/tokens.ts +533 -0
- package/src/server/docs.ts +8 -0
- package/src/server/extensions/artifacts.ts +87 -2
- package/src/server/extensions/design/app.js +280 -0
- package/src/server/extensions/design/canvas.js +142 -17
- package/src/server/extensions/design/check.js +656 -0
- package/src/server/extensions/design/color.js +383 -0
- package/src/server/extensions/design/data.js +102 -7
- package/src/server/extensions/design/devices.js +26 -0
- package/src/server/extensions/design/docks.js +117 -3
- package/src/server/extensions/design/index.js +171 -219
- package/src/server/extensions/design/messages.js +49 -0
- package/src/server/extensions/design/parts.js +15 -1
- package/src/server/extensions/design/paths.js +4 -0
- package/src/server/extensions/design/picker.js +123 -0
- package/src/server/extensions/design/specimen.js +55 -0
- package/src/server/extensions/design/state.js +72 -7
- package/src/server/extensions/design/style.css +546 -0
- package/src/server/extensions/design/system-actions.js +214 -0
- package/src/server/extensions/design/system-api.ts +62 -0
- package/src/server/extensions/design/system-data.js +315 -0
- package/src/server/extensions/design/system-preview.js +41 -0
- package/src/server/extensions/design/system-routes.ts +1122 -0
- package/src/server/extensions/design/system-sections.js +1344 -0
- package/src/server/extensions/design/system-ui.js +32 -0
- package/src/server/extensions/design/system-view.js +1196 -0
- package/src/server/extensions/design/system-widgets.js +942 -0
- package/src/server/extensions/design/system.css +2313 -0
- package/src/server/extensions/design/system.js +1479 -0
- package/src/server/extensions/design/unsaved.js +20 -0
- package/src/server/extensions/design/views.js +2 -2
- package/src/server/extensions/design/workspace.js +200 -105
- package/src/server/extensions/design.ts +89 -6
- package/src/server/extensions/updates/index.js +31 -9
- package/src/server/extensions/updates/style.css +11 -0
- package/src/server/extensions/updates.ts +29 -1
- package/src/server/host.ts +4 -1
- package/src/server/http/artifact-inspect.ts +338 -24
- package/src/server/updates.ts +235 -53
- package/src/server/versions.ts +71 -0
- package/web/artifact-bridge.js +34 -1
- package/npm-shrinkwrap.json +0 -3162
|
@@ -0,0 +1,983 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scales of the design system scan (stream T): the type scale, fonts, weights, line heights, spacing, radii, shadows,
|
|
3
|
+
* motion, and breakpoints that a project does not name in variables, read from how it uses them. Each function adds
|
|
4
|
+
* tokens to the `Book` and notes to the result; none runs when the project already names that kind of token.
|
|
5
|
+
*/
|
|
6
|
+
import { colorsIn, commaParts, parts, px } from "./scan-parse.ts";
|
|
7
|
+
import type { RoleName } from "./scan-colors.ts";
|
|
8
|
+
import type { Category, Facts, SizeFact } from "./scan-facts.ts";
|
|
9
|
+
|
|
10
|
+
type Obj = Record<string, unknown>;
|
|
11
|
+
|
|
12
|
+
export interface Draft {
|
|
13
|
+
tokens: Obj;
|
|
14
|
+
modes: Record<string, Obj>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** One role as the report shows it. */
|
|
18
|
+
export interface RoleInfo {
|
|
19
|
+
token: string;
|
|
20
|
+
value: string;
|
|
21
|
+
dark?: string;
|
|
22
|
+
basis: "name" | "use";
|
|
23
|
+
source: string;
|
|
24
|
+
/** How sure the scan is: a name that says the role is high; a guess from how colors are used is low. */
|
|
25
|
+
confidence: "high" | "medium" | "low";
|
|
26
|
+
/** Where the choice comes from, as `file:line`, when the scan knows. */
|
|
27
|
+
at?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The scan's conclusions, for the report and for the tests. */
|
|
31
|
+
export interface Inferred {
|
|
32
|
+
draft: Draft;
|
|
33
|
+
roles: Partial<Record<RoleName, RoleInfo>>;
|
|
34
|
+
/** Palette families and their sizes. */
|
|
35
|
+
palette: Record<string, number>;
|
|
36
|
+
typeScale: { base?: string; sizes: string[]; ratio?: number };
|
|
37
|
+
fonts: Record<string, string>;
|
|
38
|
+
spacing: { base?: number; steps: number[]; off: number[] };
|
|
39
|
+
radii: string[];
|
|
40
|
+
shadows: number;
|
|
41
|
+
motion: { durations: string[]; easings: string[] };
|
|
42
|
+
breakpoints: string[];
|
|
43
|
+
/** Whether the project has a second mode (a dark mode in most; a light one in a dark-first project). */
|
|
44
|
+
hasDark: boolean;
|
|
45
|
+
/** The names of the modes: the default, and the other one when there is one. */
|
|
46
|
+
modes: { default: string; alt: string };
|
|
47
|
+
/** What the scan could not decide, or left out. */
|
|
48
|
+
notes: string[];
|
|
49
|
+
/** How many tokens came from a token file instead of a guess. */
|
|
50
|
+
imported: number;
|
|
51
|
+
/** Tokens the engine refused, dropped from the draft. */
|
|
52
|
+
dropped: string[];
|
|
53
|
+
/** What the scan read as it is, not as a guess: a Material 3 export, for the top of the report. */
|
|
54
|
+
sources: string[];
|
|
55
|
+
/** The scales that are a thin guess, one line each, for the top of the report. */
|
|
56
|
+
review: string[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const BEZIER: Record<string, number[]> = {
|
|
60
|
+
ease: [0.25, 0.1, 0.25, 1],
|
|
61
|
+
"ease-in": [0.42, 0, 1, 1],
|
|
62
|
+
"ease-out": [0, 0, 0.58, 1],
|
|
63
|
+
"ease-in-out": [0.42, 0, 0.58, 1],
|
|
64
|
+
linear: [0, 0, 1, 1],
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** An easing as a DTCG cubicBezier value (an array of four numbers), or undefined. */
|
|
68
|
+
export function bezier(text: string): number[] | undefined {
|
|
69
|
+
const keyword = BEZIER[text.trim().toLowerCase()];
|
|
70
|
+
|
|
71
|
+
if (keyword !== undefined) {
|
|
72
|
+
return keyword;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const match =
|
|
76
|
+
/^cubic-bezier\(\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)\s*\)$/i.exec(
|
|
77
|
+
text.trim(),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (match === null) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const numbers = match.slice(1, 5).map(Number);
|
|
85
|
+
|
|
86
|
+
return numbers.every(Number.isFinite) &&
|
|
87
|
+
numbers[0]! >= 0 &&
|
|
88
|
+
numbers[0]! <= 1 &&
|
|
89
|
+
numbers[2]! >= 0 &&
|
|
90
|
+
numbers[2]! <= 1
|
|
91
|
+
? numbers
|
|
92
|
+
: undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A CSS shadow as DTCG layers; the text itself when it cannot be taken apart. */
|
|
96
|
+
export function shadowValue(text: string): unknown {
|
|
97
|
+
const layers: Obj[] = [];
|
|
98
|
+
|
|
99
|
+
for (const layer of commaParts(text)) {
|
|
100
|
+
const found = colorsIn(layer, true);
|
|
101
|
+
const color = found[0];
|
|
102
|
+
const rest = layer.replace(color?.text ?? "", " ");
|
|
103
|
+
const lengths = parts(rest)
|
|
104
|
+
.filter((part) => part !== "inset")
|
|
105
|
+
.map((part) => px(part));
|
|
106
|
+
|
|
107
|
+
if (
|
|
108
|
+
color === undefined ||
|
|
109
|
+
lengths.length < 2 ||
|
|
110
|
+
lengths.length > 4 ||
|
|
111
|
+
lengths.some((length) => length === undefined)
|
|
112
|
+
) {
|
|
113
|
+
return text;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const dimension = (value: number | undefined) =>
|
|
117
|
+
`${Math.round((value ?? 0) * 100) / 100}px`;
|
|
118
|
+
|
|
119
|
+
layers.push({
|
|
120
|
+
color: color.text.replace(/\s+/g, " "),
|
|
121
|
+
offsetX: dimension(lengths[0]),
|
|
122
|
+
offsetY: dimension(lengths[1]),
|
|
123
|
+
blur: dimension(lengths[2]),
|
|
124
|
+
...(lengths[3] === undefined ? {} : { spread: dimension(lengths[3]) }),
|
|
125
|
+
...(/\binset\b/.test(layer) ? { inset: true } : {}),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return layers.length === 1 ? layers[0] : layers;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A shadow's size, for ordering: blur and offset of its largest layer. */
|
|
133
|
+
export function elevation(value: unknown): number {
|
|
134
|
+
const layers = Array.isArray(value)
|
|
135
|
+
? (value as Obj[])
|
|
136
|
+
: typeof value === "object" && value !== null
|
|
137
|
+
? [value as Obj]
|
|
138
|
+
: [];
|
|
139
|
+
|
|
140
|
+
return Math.max(
|
|
141
|
+
0,
|
|
142
|
+
...layers.map(
|
|
143
|
+
(layer) =>
|
|
144
|
+
(px(String(layer.blur ?? "0")) ?? 0) +
|
|
145
|
+
Math.abs(px(String(layer.offsetY ?? "0")) ?? 0),
|
|
146
|
+
),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Tokens being gathered: by path, with a version for the dark mode. */
|
|
151
|
+
export class Book {
|
|
152
|
+
tokens = new Map<string, { path: string[]; node: Obj }>();
|
|
153
|
+
dark = new Map<string, { path: string[]; node: Obj }>();
|
|
154
|
+
|
|
155
|
+
has(path: string[]) {
|
|
156
|
+
return this.tokens.has(path.join("."));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
add(path: string[], node: Obj) {
|
|
160
|
+
this.tokens.set(path.join("."), { path, node });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
addDark(path: string[], node: Obj) {
|
|
164
|
+
this.dark.set(path.join("."), { path, node });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
taken() {
|
|
168
|
+
return new Set(this.tokens.keys());
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export const WEIGHT_NAMES: Record<number, string> = {
|
|
173
|
+
100: "thin",
|
|
174
|
+
200: "extralight",
|
|
175
|
+
300: "light",
|
|
176
|
+
400: "regular",
|
|
177
|
+
500: "medium",
|
|
178
|
+
600: "semibold",
|
|
179
|
+
700: "bold",
|
|
180
|
+
800: "extrabold",
|
|
181
|
+
900: "black",
|
|
182
|
+
};
|
|
183
|
+
/** Tailwind's own sizes, for the utility classes (`text-sm`, `rounded-lg`) that a project uses without defining them. */
|
|
184
|
+
const TAILWIND_TEXT: Record<string, string> = {
|
|
185
|
+
xs: "0.75rem",
|
|
186
|
+
sm: "0.875rem",
|
|
187
|
+
base: "1rem",
|
|
188
|
+
lg: "1.125rem",
|
|
189
|
+
xl: "1.25rem",
|
|
190
|
+
"2xl": "1.5rem",
|
|
191
|
+
"3xl": "1.875rem",
|
|
192
|
+
"4xl": "2.25rem",
|
|
193
|
+
"5xl": "3rem",
|
|
194
|
+
"6xl": "3.75rem",
|
|
195
|
+
"7xl": "4.5rem",
|
|
196
|
+
"8xl": "6rem",
|
|
197
|
+
"9xl": "8rem",
|
|
198
|
+
};
|
|
199
|
+
const TAILWIND_RADIUS: Record<string, string> = {
|
|
200
|
+
xs: "0.125rem",
|
|
201
|
+
sm: "0.25rem",
|
|
202
|
+
md: "0.375rem",
|
|
203
|
+
lg: "0.5rem",
|
|
204
|
+
xl: "0.75rem",
|
|
205
|
+
"2xl": "1rem",
|
|
206
|
+
"3xl": "1.5rem",
|
|
207
|
+
full: "9999px",
|
|
208
|
+
};
|
|
209
|
+
const TAILWIND_WEIGHT: Record<string, [string, number]> = {
|
|
210
|
+
thin: ["thin", 100],
|
|
211
|
+
extralight: ["extralight", 200],
|
|
212
|
+
light: ["light", 300],
|
|
213
|
+
normal: ["regular", 400],
|
|
214
|
+
medium: ["medium", 500],
|
|
215
|
+
semibold: ["semibold", 600],
|
|
216
|
+
bold: ["bold", 700],
|
|
217
|
+
extrabold: ["extrabold", 800],
|
|
218
|
+
black: ["black", 900],
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
export const SIZE_BELOW = ["sm", "xs", "2xs"];
|
|
222
|
+
export const SIZE_ABOVE = ["lg", "xl", "2xl", "3xl", "4xl", "5xl", "6xl", "7xl"];
|
|
223
|
+
|
|
224
|
+
export const rounded = (value: number) => Math.round(value * 100) / 100;
|
|
225
|
+
export const topBy = <T>(list: T[], key: (item: T) => number, limit: number) =>
|
|
226
|
+
[...list].sort((a, b) => key(b) - key(a)).slice(0, limit);
|
|
227
|
+
|
|
228
|
+
// --- Scales --------------------------------------------------------------------------------------------------------
|
|
229
|
+
|
|
230
|
+
export function fontScale(
|
|
231
|
+
facts: Facts,
|
|
232
|
+
book: Book,
|
|
233
|
+
explicit: Set<Category>,
|
|
234
|
+
result: Inferred,
|
|
235
|
+
): void {
|
|
236
|
+
const stacks = [...facts.fonts.values()];
|
|
237
|
+
const named = [...book.tokens.values()].filter(
|
|
238
|
+
(entry) =>
|
|
239
|
+
entry.path[0] === "font" &&
|
|
240
|
+
!entry.path[1]!.startsWith("heading") &&
|
|
241
|
+
!entry.path[1]!.startsWith("body"),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const kindOf = (stack: string[]) =>
|
|
245
|
+
stack.some((name) => /mono|code|courier|consolas|menlo|monaco/i.test(name)) ||
|
|
246
|
+
stack.includes("monospace")
|
|
247
|
+
? "mono"
|
|
248
|
+
: stack.includes("serif") && !stack.includes("sans-serif")
|
|
249
|
+
? "serif"
|
|
250
|
+
: "sans";
|
|
251
|
+
|
|
252
|
+
for (const entry of named) {
|
|
253
|
+
result.fonts[entry.path.join(".")] = (entry.node.$value as string[]).join(", ");
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (explicit.has("font")) {
|
|
257
|
+
// A monospace family the project uses in a rule while its named stacks have none.
|
|
258
|
+
const covered = new Set(
|
|
259
|
+
named.map((entry) => (entry.node.$value as string[])[0]?.toLowerCase()),
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
for (const fact of [...stacks].sort((a, b) => b.count - a.count).slice(0, 8)) {
|
|
263
|
+
const first = fact.stack[0]?.toLowerCase();
|
|
264
|
+
const kind = kindOf(fact.stack);
|
|
265
|
+
|
|
266
|
+
if (
|
|
267
|
+
first !== undefined &&
|
|
268
|
+
kind === "mono" &&
|
|
269
|
+
!covered.has(first) &&
|
|
270
|
+
!named.some((entry) => kindOf(entry.node.$value as string[]) === "mono") &&
|
|
271
|
+
!book.has(["font", kind])
|
|
272
|
+
) {
|
|
273
|
+
book.add(["font", kind], { $value: fact.stack, $type: "fontFamily" });
|
|
274
|
+
result.fonts[`font.${kind}`] = fact.stack.join(", ");
|
|
275
|
+
covered.add(first);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const sans = named.find((entry) => /sans|body|base|text/.test(entry.path[1]!)) ?? named[0];
|
|
280
|
+
const heading = named.find((entry) => /heading|display|title/.test(entry.path[1]!));
|
|
281
|
+
|
|
282
|
+
if (sans !== undefined && !book.has(["font", "body"])) {
|
|
283
|
+
book.add(["font", "body"], { $value: `{${sans.path.join(".")}}`, $type: "fontFamily" });
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (heading !== undefined && !book.has(["font", "heading"])) {
|
|
287
|
+
book.add(["font", "heading"], {
|
|
288
|
+
$value: `{${heading.path.join(".")}}`,
|
|
289
|
+
$type: "fontFamily",
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (stacks.length === 0) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const ranked = [...stacks].sort((a, b) => b.count - a.count);
|
|
301
|
+
const used = new Map<string, FontPick>();
|
|
302
|
+
|
|
303
|
+
interface FontPick {
|
|
304
|
+
path: string[];
|
|
305
|
+
stack: string[];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const add = (kind: string, stack: string[]) => {
|
|
309
|
+
const path = ["font", kind];
|
|
310
|
+
|
|
311
|
+
if (book.has(path)) {
|
|
312
|
+
return undefined;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// A system stack with no named family still gets a role.
|
|
316
|
+
book.add(path, { $value: stack, $type: "fontFamily" });
|
|
317
|
+
used.set(kind, { path, stack });
|
|
318
|
+
result.fonts[path.join(".")] = stack.join(", ");
|
|
319
|
+
|
|
320
|
+
return path;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const body = [...ranked]
|
|
324
|
+
.filter((fact) => kindOf(fact.stack) !== "mono")
|
|
325
|
+
.sort((a, b) => b.body * 3 + b.count - (a.body * 3 + a.count))[0];
|
|
326
|
+
const bodyKind = body === undefined ? undefined : kindOf(body.stack);
|
|
327
|
+
const bodyPath = body === undefined ? undefined : add(bodyKind!, body.stack);
|
|
328
|
+
const mono = [...ranked]
|
|
329
|
+
.filter((fact) => kindOf(fact.stack) === "mono")
|
|
330
|
+
.sort((a, b) => b.code + b.count - (a.code + a.count))[0];
|
|
331
|
+
|
|
332
|
+
if (mono !== undefined) {
|
|
333
|
+
add("mono", mono.stack);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const heading = [...ranked]
|
|
337
|
+
.filter((fact) => fact !== body && fact.heading > 0 && kindOf(fact.stack) !== "mono")
|
|
338
|
+
.sort((a, b) => b.heading - a.heading)[0];
|
|
339
|
+
let headingPath: string[] | undefined;
|
|
340
|
+
|
|
341
|
+
if (heading !== undefined) {
|
|
342
|
+
const kind = kindOf(heading.stack);
|
|
343
|
+
|
|
344
|
+
headingPath = add(book.has(["font", kind]) ? "display" : kind, heading.stack);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const other = ranked.find(
|
|
348
|
+
(fact) =>
|
|
349
|
+
fact !== body &&
|
|
350
|
+
fact !== mono &&
|
|
351
|
+
fact !== heading &&
|
|
352
|
+
kindOf(fact.stack) === "serif" &&
|
|
353
|
+
!book.has(["font", "serif"]),
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
if (other !== undefined) {
|
|
357
|
+
add("serif", other.stack);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (bodyPath !== undefined) {
|
|
361
|
+
book.add(["font", "body"], { $value: `{${bodyPath.join(".")}}`, $type: "fontFamily" });
|
|
362
|
+
book.add(["font", "heading"], {
|
|
363
|
+
$value: `{${(headingPath ?? bodyPath).join(".")}}`,
|
|
364
|
+
$type: "fontFamily",
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export function sizeScale(
|
|
370
|
+
facts: Facts,
|
|
371
|
+
book: Book,
|
|
372
|
+
explicit: Set<Category>,
|
|
373
|
+
result: Inferred,
|
|
374
|
+
): void {
|
|
375
|
+
const sizes = [...facts.sizes].filter(([size]) => size >= 9 && size <= 96);
|
|
376
|
+
|
|
377
|
+
// Tailwind's own sizes, named as Tailwind names them: the ones a project uses and does not define itself.
|
|
378
|
+
if (facts.tailwind && facts.classText.size >= 1 && (explicit.has("text") || sizes.length < 3)) {
|
|
379
|
+
for (const [name] of topBy([...facts.classText], ([, count]) => count, 9)) {
|
|
380
|
+
if (TAILWIND_TEXT[name] !== undefined && !book.has(["text", name])) {
|
|
381
|
+
book.add(["text", name], { $value: TAILWIND_TEXT[name], $type: "dimension" });
|
|
382
|
+
explicit.add("text");
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Sizes a pixel apart are one size (11px and 12px): the one used more stands for both.
|
|
388
|
+
const merged: [number, SizeFact][] = [];
|
|
389
|
+
|
|
390
|
+
for (const [size, fact] of [...sizes].sort(
|
|
391
|
+
(a, b) => b[1].count + b[1].body * 2 - (a[1].count + a[1].body * 2) || a[0] - b[0],
|
|
392
|
+
)) {
|
|
393
|
+
const near = merged.find(([other]) => Math.abs(other - size) <= 1);
|
|
394
|
+
|
|
395
|
+
if (near === undefined) {
|
|
396
|
+
merged.push([size, { ...fact, forms: new Map(fact.forms) }]);
|
|
397
|
+
} else {
|
|
398
|
+
near[1].count += fact.count;
|
|
399
|
+
near[1].body += fact.body;
|
|
400
|
+
near[1].heading += fact.heading;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (explicit.has("text")) {
|
|
405
|
+
// The project names some sizes; the others it uses join them, named by their size (`text.40` is 40px).
|
|
406
|
+
const named = [...book.tokens.values()]
|
|
407
|
+
.filter((entry) => entry.path[0] === "text")
|
|
408
|
+
.map((entry) => px(String(entry.node.$value)) ?? -99);
|
|
409
|
+
|
|
410
|
+
for (const [size, fact] of topBy(merged, ([, each]) => each.count + each.body * 2, 8)) {
|
|
411
|
+
const path = ["text", String(rounded(size)).replace(".", "-")];
|
|
412
|
+
|
|
413
|
+
if (named.every((value) => Math.abs(value - size) > 1) && !book.has(path)) {
|
|
414
|
+
const form = [...fact.forms].sort((a, b) => b[1] - a[1])[0]?.[0] ?? `${size}px`;
|
|
415
|
+
|
|
416
|
+
book.add(path, {
|
|
417
|
+
$value: /^-?[\d.]+(?:px|rem)$/.test(form) ? form : `${rounded(size)}px`,
|
|
418
|
+
$type: "dimension",
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
result.typeScale = {
|
|
424
|
+
sizes: [...book.tokens.values()]
|
|
425
|
+
.filter((entry) => entry.path[0] === "text")
|
|
426
|
+
.sort((a, b) => (px(String(a.node.$value)) ?? 0) - (px(String(b.node.$value)) ?? 0))
|
|
427
|
+
.map((entry) => `${entry.path[1]} ${String(entry.node.$value)}`),
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (sizes.length === 0) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// The sizes used most, in order of size.
|
|
438
|
+
let kept = topBy(merged, ([, fact]) => fact.count + fact.body * 2 + fact.heading, 10).sort(
|
|
439
|
+
(a, b) => a[0] - b[0],
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
const bodyAt = (list: typeof kept) => {
|
|
443
|
+
const withBody = list.filter(([, fact]) => fact.body > 0);
|
|
444
|
+
|
|
445
|
+
return withBody.length > 0
|
|
446
|
+
? list.indexOf(topBy(withBody, ([, fact]) => fact.body, 1)[0]!)
|
|
447
|
+
: list.indexOf(topBy(list, ([size]) => -Math.abs(size - 16), 1)[0]!);
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
let base = bodyAt(kept);
|
|
451
|
+
|
|
452
|
+
// Only as many sizes as there are names: drop the least used at the ends.
|
|
453
|
+
while (base > SIZE_BELOW.length || kept.length - base - 1 > SIZE_ABOVE.length) {
|
|
454
|
+
const first = kept[0]!;
|
|
455
|
+
const last = kept.at(-1)!;
|
|
456
|
+
|
|
457
|
+
kept =
|
|
458
|
+
base > SIZE_BELOW.length &&
|
|
459
|
+
(kept.length - base - 1 <= SIZE_ABOVE.length || first[1].count <= last[1].count)
|
|
460
|
+
? kept.slice(1)
|
|
461
|
+
: kept.slice(0, -1);
|
|
462
|
+
base = bodyAt(kept);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const names = kept.map((_, at) =>
|
|
466
|
+
at === base ? "base" : at < base ? SIZE_BELOW[base - at - 1]! : SIZE_ABOVE[at - base - 1]!,
|
|
467
|
+
);
|
|
468
|
+
|
|
469
|
+
kept.forEach(([size, fact], at) => {
|
|
470
|
+
const form = [...fact.forms].sort((a, b) => b[1] - a[1])[0]?.[0] ?? `${size}px`;
|
|
471
|
+
|
|
472
|
+
book.add(["text", names[at]!], {
|
|
473
|
+
$value: /^-?[\d.]+(?:px|rem)$/.test(form) ? form : `${rounded(size)}px`,
|
|
474
|
+
$type: "dimension",
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
const above = kept.slice(base).map(([size]) => size);
|
|
479
|
+
const ratios = above.slice(1).map((size, at) => size / above[at]!);
|
|
480
|
+
|
|
481
|
+
result.typeScale = {
|
|
482
|
+
base: `${rounded(kept[base]![0])}px`,
|
|
483
|
+
sizes: kept.map(([size], at) => `${names[at]} ${rounded(size)}px`),
|
|
484
|
+
...(ratios.length > 0
|
|
485
|
+
? {
|
|
486
|
+
ratio: rounded(
|
|
487
|
+
Math.exp(
|
|
488
|
+
ratios.reduce((sum, ratio) => sum + Math.log(ratio), 0) / ratios.length,
|
|
489
|
+
),
|
|
490
|
+
),
|
|
491
|
+
}
|
|
492
|
+
: {}),
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export function detailScales(facts: Facts, book: Book, explicit: Set<Category>): void {
|
|
497
|
+
if (!explicit.has("weight")) {
|
|
498
|
+
const counts = new Map(facts.weights);
|
|
499
|
+
|
|
500
|
+
if (facts.tailwind) {
|
|
501
|
+
for (const [name, count] of facts.classWeight) {
|
|
502
|
+
const weight = TAILWIND_WEIGHT[name]?.[1];
|
|
503
|
+
|
|
504
|
+
if (weight !== undefined) {
|
|
505
|
+
counts.set(weight, (counts.get(weight) ?? 0) + count);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const used = topBy([...counts], ([, count]) => count, 5).sort((a, b) => a[0] - b[0]);
|
|
511
|
+
|
|
512
|
+
if (used.length >= 2) {
|
|
513
|
+
for (const [weight] of used) {
|
|
514
|
+
const name = WEIGHT_NAMES[weight];
|
|
515
|
+
|
|
516
|
+
if (name !== undefined) {
|
|
517
|
+
book.add(["weight", name], { $value: weight, $type: "fontWeight" });
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (!explicit.has("leading")) {
|
|
524
|
+
const used = topBy([...facts.leadings], ([, count]) => count, 3).sort(
|
|
525
|
+
(a, b) => a[0] - b[0],
|
|
526
|
+
);
|
|
527
|
+
const names =
|
|
528
|
+
used.length === 1
|
|
529
|
+
? ["normal"]
|
|
530
|
+
: used.length === 2
|
|
531
|
+
? ["tight", "normal"]
|
|
532
|
+
: ["tight", "normal", "relaxed"];
|
|
533
|
+
|
|
534
|
+
used.forEach(([value], at) =>
|
|
535
|
+
book.add(["leading", names[at]!], { $value: value, $type: "number" }),
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (!explicit.has("tracking")) {
|
|
540
|
+
const used = [...facts.tracking].sort((a, b) => b[1] - a[1]);
|
|
541
|
+
const tight = used.find(([text]) => text.startsWith("-"));
|
|
542
|
+
const wide = used.find(([text]) => !text.startsWith("-"));
|
|
543
|
+
|
|
544
|
+
if (tight !== undefined) {
|
|
545
|
+
book.add(["tracking", "tight"], { $value: tight[0], $type: "dimension" });
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (wide !== undefined) {
|
|
549
|
+
book.add(["tracking", "wide"], { $value: wide[0], $type: "dimension" });
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
export function spacingScale(
|
|
555
|
+
facts: Facts,
|
|
556
|
+
book: Book,
|
|
557
|
+
explicit: Set<Category>,
|
|
558
|
+
result: Inferred,
|
|
559
|
+
): void {
|
|
560
|
+
const named = () =>
|
|
561
|
+
[...book.tokens.values()]
|
|
562
|
+
.filter((entry) => entry.path[0] === "space")
|
|
563
|
+
.map((entry) => px(String(entry.node.$value)))
|
|
564
|
+
.filter((value): value is number => value !== undefined && value > 0);
|
|
565
|
+
const counts = new Map(facts.spacing);
|
|
566
|
+
|
|
567
|
+
// Tailwind classes are steps of 4px; they count only for a project that uses Tailwind.
|
|
568
|
+
if (facts.tailwind) {
|
|
569
|
+
for (const [size, count] of facts.classSpacing) {
|
|
570
|
+
counts.set(size, (counts.get(size) ?? 0) + count);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const total = [...counts.values()].reduce((sum, count) => sum + count, 0);
|
|
575
|
+
|
|
576
|
+
if (total === 0) {
|
|
577
|
+
const steps = [...new Set(named())].sort((a, b) => a - b);
|
|
578
|
+
|
|
579
|
+
result.spacing = { base: steps[0], steps, off: [] };
|
|
580
|
+
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
const share = (unit: number) =>
|
|
585
|
+
[...counts]
|
|
586
|
+
.filter(([size]) => size % unit === 0)
|
|
587
|
+
.reduce((sum, [, count]) => sum + count, 0) / total;
|
|
588
|
+
const base = share(8) >= 0.85 ? 8 : share(4) >= 0.75 ? 4 : share(2) >= 0.75 ? 2 : 4;
|
|
589
|
+
|
|
590
|
+
// Values a pixel apart are one value (15px and 16px; 4px and 5px): the one used more stands for both. Below 4px
|
|
591
|
+
// every pixel counts.
|
|
592
|
+
const clusters: [number, number][] = [];
|
|
593
|
+
|
|
594
|
+
for (const [size, count] of [...counts]
|
|
595
|
+
.filter(([size]) => size >= 1 && size <= 192)
|
|
596
|
+
.sort((a, b) => b[1] - a[1] || a[0] - b[0])) {
|
|
597
|
+
const near = clusters.find(
|
|
598
|
+
([other]) => Math.abs(other - size) <= 1 && Math.min(other, size) >= 4,
|
|
599
|
+
);
|
|
600
|
+
|
|
601
|
+
if (near === undefined) {
|
|
602
|
+
clusters.push([size, count]);
|
|
603
|
+
} else {
|
|
604
|
+
near[1] += count;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// A value used once that is off the grid (6px, 10px in a 4px grid) or under 4px (a hairline) is a one-off, not a step.
|
|
609
|
+
const kept = topBy(
|
|
610
|
+
clusters.filter(
|
|
611
|
+
([size, count]) =>
|
|
612
|
+
size >= 2 &&
|
|
613
|
+
(count >= 2 || (size >= 4 && size % Math.min(base, 4) === 0)) &&
|
|
614
|
+
(size >= 4 || count >= 3),
|
|
615
|
+
),
|
|
616
|
+
([, count]) => count,
|
|
617
|
+
12,
|
|
618
|
+
).sort((a, b) => a[0] - b[0]);
|
|
619
|
+
// A project that names some of its spacing keeps those names; the other values it uses join them.
|
|
620
|
+
const own = explicit.has("space") ? named() : [];
|
|
621
|
+
const fresh = kept.filter(([size]) =>
|
|
622
|
+
own.every((value) => Math.abs(value - size) > 1 || size < 4),
|
|
623
|
+
);
|
|
624
|
+
|
|
625
|
+
for (const [size] of fresh) {
|
|
626
|
+
// Named in steps of 4px, as Tailwind does: 16px is `space.4`, 2px is `space.0-5`, 10px is `space.2-5`.
|
|
627
|
+
const path = ["space", String(rounded(size / 4)).replace(".", "-")];
|
|
628
|
+
|
|
629
|
+
if (!book.has(path)) {
|
|
630
|
+
book.add(path, { $value: `${rounded(size)}px`, $type: "dimension" });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
const steps = [...new Set(named().map((value) => rounded(value)))].sort((a, b) => a - b);
|
|
635
|
+
|
|
636
|
+
result.spacing = {
|
|
637
|
+
base: explicit.has("space") ? steps[0] : base,
|
|
638
|
+
steps,
|
|
639
|
+
off: steps.filter((size) => size % 4 !== 0).slice(0, 6),
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export function radiusScale(
|
|
644
|
+
facts: Facts,
|
|
645
|
+
book: Book,
|
|
646
|
+
explicit: Set<Category>,
|
|
647
|
+
result: Inferred,
|
|
648
|
+
): void {
|
|
649
|
+
// Tailwind's `rounded-lg` and the like: the names a project uses and does not define itself.
|
|
650
|
+
if (
|
|
651
|
+
facts.tailwind &&
|
|
652
|
+
facts.classRadius.size > 0 &&
|
|
653
|
+
(explicit.has("radius") || facts.radii.size === 0)
|
|
654
|
+
) {
|
|
655
|
+
for (const [name] of topBy([...facts.classRadius], ([, count]) => count, 6)) {
|
|
656
|
+
const value = TAILWIND_RADIUS[name];
|
|
657
|
+
|
|
658
|
+
if (value !== undefined && !book.has(["radius", name])) {
|
|
659
|
+
book.add(["radius", name], { $value: value, $type: "dimension" });
|
|
660
|
+
explicit.add("radius");
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// A radius the project uses that its own tokens do not cover (within a pixel) joins them.
|
|
666
|
+
const has = (key: string) =>
|
|
667
|
+
[...book.tokens.values()].some(
|
|
668
|
+
(entry) =>
|
|
669
|
+
entry.path[0] === "radius" &&
|
|
670
|
+
(key === "full"
|
|
671
|
+
? (px(String(entry.node.$value)) ?? 0) >= 100
|
|
672
|
+
: Math.abs((px(String(entry.node.$value)) ?? -99) - Number(key)) <= 1),
|
|
673
|
+
);
|
|
674
|
+
|
|
675
|
+
if (explicit.has("radius")) {
|
|
676
|
+
const extra: [string, { count: number; text: string }][] = [];
|
|
677
|
+
|
|
678
|
+
// Extras a pixel apart are one (3px and 4px).
|
|
679
|
+
for (const [key, fact] of [...facts.radii]
|
|
680
|
+
.filter(([each]) => !has(each) && (each === "full" || Number(each) >= 2))
|
|
681
|
+
.sort((a, b) => b[1].count - a[1].count)) {
|
|
682
|
+
const near = extra.find(
|
|
683
|
+
([other]) =>
|
|
684
|
+
other !== "full" &&
|
|
685
|
+
key !== "full" &&
|
|
686
|
+
Math.abs(Number(other) - Number(key)) <= 1,
|
|
687
|
+
);
|
|
688
|
+
|
|
689
|
+
if (near === undefined) {
|
|
690
|
+
extra.push([key, { ...fact }]);
|
|
691
|
+
} else {
|
|
692
|
+
near[1].count += fact.count;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const taken = new Set(
|
|
697
|
+
[...book.tokens.values()]
|
|
698
|
+
.filter((entry) => entry.path[0] === "radius")
|
|
699
|
+
.map((entry) => entry.path[1]),
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
for (const [key, fact] of extra.slice(0, 6)) {
|
|
703
|
+
// Named by their size, as spacing is: `radius.4` is 4px.
|
|
704
|
+
const name = key === "full" ? "full" : key.replace(".", "-");
|
|
705
|
+
|
|
706
|
+
if (!taken.has(name)) {
|
|
707
|
+
book.add(["radius", name], {
|
|
708
|
+
$value:
|
|
709
|
+
key === "full"
|
|
710
|
+
? /^[\d.]+(?:px|rem|em)$/.test(fact.text)
|
|
711
|
+
? fact.text
|
|
712
|
+
: "9999px"
|
|
713
|
+
: /^[\d.]+(?:px|rem|em)$/.test(fact.text)
|
|
714
|
+
? fact.text
|
|
715
|
+
: `${key}px`,
|
|
716
|
+
$type: "dimension",
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
result.radii = [...book.tokens.values()]
|
|
722
|
+
.filter((entry) => entry.path[0] === "radius")
|
|
723
|
+
.sort((a, b) => (px(String(a.node.$value)) ?? 0) - (px(String(b.node.$value)) ?? 0))
|
|
724
|
+
.map((entry) => `${entry.path[1]} ${String(entry.node.$value)}`);
|
|
725
|
+
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// Radii a pixel apart are one radius (6px and 7px): the more used one stands for both.
|
|
730
|
+
const clusters: [string, { count: number; text: string }][] = [];
|
|
731
|
+
|
|
732
|
+
for (const [key, fact] of [...facts.radii]
|
|
733
|
+
.filter(([each]) => each !== "full")
|
|
734
|
+
.sort((a, b) => b[1].count - a[1].count)) {
|
|
735
|
+
const near = clusters.find(([other]) => Math.abs(Number(other) - Number(key)) <= 1);
|
|
736
|
+
|
|
737
|
+
if (near === undefined) {
|
|
738
|
+
clusters.push([key, { ...fact }]);
|
|
739
|
+
} else {
|
|
740
|
+
near[1].count += fact.count;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const kept = topBy(clusters, ([, fact]) => fact.count, 5).sort(
|
|
745
|
+
(a, b) => Number(a[0]) - Number(b[0]),
|
|
746
|
+
);
|
|
747
|
+
const ladders: Record<number, string[]> = {
|
|
748
|
+
1: ["md"],
|
|
749
|
+
2: ["sm", "lg"],
|
|
750
|
+
3: ["sm", "md", "lg"],
|
|
751
|
+
4: ["sm", "md", "lg", "xl"],
|
|
752
|
+
5: ["xs", "sm", "md", "lg", "xl"],
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
kept.forEach(([key, fact], at) => {
|
|
756
|
+
const form = /^-?[\d.]+(?:px|rem|em)$/.test(fact.text) ? fact.text : `${key}px`;
|
|
757
|
+
|
|
758
|
+
book.add(["radius", ladders[kept.length]![at]!], { $value: form, $type: "dimension" });
|
|
759
|
+
result.radii.push(`${ladders[kept.length]![at]} ${form}`);
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
if (facts.radii.has("full")) {
|
|
763
|
+
// The pill value the project wrote (`999px`), as it wrote it.
|
|
764
|
+
const text = facts.radii.get("full")!.text;
|
|
765
|
+
const value = /^[\d.]+(?:px|rem|em)$/.test(text) ? text : "9999px";
|
|
766
|
+
|
|
767
|
+
book.add(["radius", "full"], { $value: value, $type: "dimension" });
|
|
768
|
+
result.radii.push(`full ${value}`);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** Whether two shadows are alike: the same layers, offsets within a pixel, blur within two, and the same color. */
|
|
773
|
+
function sameShadow(a: unknown, b: unknown): boolean {
|
|
774
|
+
const layers = (value: unknown) => (Array.isArray(value) ? value : [value]) as Obj[];
|
|
775
|
+
const first = layers(a);
|
|
776
|
+
const second = layers(b);
|
|
777
|
+
const length = (value: unknown) => px(String(value ?? "0")) ?? 0;
|
|
778
|
+
|
|
779
|
+
const alpha = (value: unknown) => {
|
|
780
|
+
const color = typeof value === "string" ? colorsIn(value)[0] : undefined;
|
|
781
|
+
|
|
782
|
+
return color === undefined ? undefined : color.hex;
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
return (
|
|
786
|
+
first.length === second.length &&
|
|
787
|
+
first.every((layer, at) => {
|
|
788
|
+
const other = second[at]!;
|
|
789
|
+
|
|
790
|
+
return (
|
|
791
|
+
(layer.inset === true) === (other.inset === true) &&
|
|
792
|
+
Math.abs(length(layer.offsetX) - length(other.offsetX)) <= 1 &&
|
|
793
|
+
Math.abs(length(layer.offsetY) - length(other.offsetY)) <= 1 &&
|
|
794
|
+
Math.abs(length(layer.blur) - length(other.blur)) <= 2 &&
|
|
795
|
+
Math.abs(length(layer.spread) - length(other.spread)) <= 1 &&
|
|
796
|
+
alpha(layer.color) === alpha(other.color)
|
|
797
|
+
);
|
|
798
|
+
})
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
export function shadowScale(
|
|
803
|
+
facts: Facts,
|
|
804
|
+
book: Book,
|
|
805
|
+
explicit: Set<Category>,
|
|
806
|
+
result: Inferred,
|
|
807
|
+
): void {
|
|
808
|
+
// Shadows that differ by a pixel or two (`0 2px 4px` and `0 2px 5px`) are one: the more used stands for both.
|
|
809
|
+
const clusters: { text: string; value: unknown; count: number }[] = [];
|
|
810
|
+
|
|
811
|
+
for (const [text, count] of [...facts.shadows].sort((a, b) => b[1] - a[1])) {
|
|
812
|
+
const value = shadowValue(text);
|
|
813
|
+
|
|
814
|
+
if (typeof value === "string") {
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
const near = clusters.find((other) => sameShadow(other.value, value));
|
|
819
|
+
|
|
820
|
+
if (near === undefined) {
|
|
821
|
+
clusters.push({ text, value, count });
|
|
822
|
+
} else {
|
|
823
|
+
near.count += count;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// A ring or an inset line is not a step of elevation: it gets a name of its own.
|
|
828
|
+
const rings: typeof clusters = [];
|
|
829
|
+
const lifts: typeof clusters = [];
|
|
830
|
+
|
|
831
|
+
// A project that names some of its shadows keeps them; the others it uses join them.
|
|
832
|
+
const own = [...book.tokens.values()]
|
|
833
|
+
.filter((entry) => entry.path[0] === "shadow")
|
|
834
|
+
.map((entry) => entry.node.$value);
|
|
835
|
+
|
|
836
|
+
for (const cluster of topBy(
|
|
837
|
+
clusters.filter((each) => !own.some((value) => sameShadow(value, each.value))),
|
|
838
|
+
(each) => each.count,
|
|
839
|
+
8,
|
|
840
|
+
)) {
|
|
841
|
+
const layers = (Array.isArray(cluster.value) ? cluster.value : [cluster.value]) as Obj[];
|
|
842
|
+
const inset = layers.every((layer) => layer.inset === true);
|
|
843
|
+
const ring = layers.every(
|
|
844
|
+
(layer) =>
|
|
845
|
+
(px(String(layer.blur ?? "0")) ?? 0) === 0 &&
|
|
846
|
+
(px(String(layer.offsetY ?? "0")) ?? 0) === 0,
|
|
847
|
+
);
|
|
848
|
+
|
|
849
|
+
(inset || ring ? rings : lifts).push(cluster);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
lifts.sort((a, b) => elevation(a.value) - elevation(b.value));
|
|
853
|
+
|
|
854
|
+
const ladder = ["xs", "sm", "md", "lg", "xl", "2xl", "3xl", "4xl"];
|
|
855
|
+
const names: Record<number, string[]> = {
|
|
856
|
+
1: ["md"],
|
|
857
|
+
2: ["sm", "lg"],
|
|
858
|
+
3: ["sm", "md", "lg"],
|
|
859
|
+
4: ["sm", "md", "lg", "xl"],
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
const free = (name: string) => !book.has(["shadow", name]);
|
|
863
|
+
|
|
864
|
+
lifts.forEach((entry, at) => {
|
|
865
|
+
const wanted = (names[lifts.length] ?? ladder)[at]!;
|
|
866
|
+
// A name the project already gave to another shadow is not taken: the next free step of the ladder is.
|
|
867
|
+
const name = free(wanted) ? wanted : (ladder.find(free) ?? `extra-${at + 1}`);
|
|
868
|
+
|
|
869
|
+
book.add(["shadow", name], { $value: entry.value, $type: "shadow" });
|
|
870
|
+
});
|
|
871
|
+
|
|
872
|
+
const seen = new Map<string, number>();
|
|
873
|
+
|
|
874
|
+
for (const entry of rings) {
|
|
875
|
+
const layers = (Array.isArray(entry.value) ? entry.value : [entry.value]) as Obj[];
|
|
876
|
+
const kind = layers.every((layer) => layer.inset === true) ? "inset" : "ring";
|
|
877
|
+
const count = (seen.get(kind) ?? 0) + 1;
|
|
878
|
+
|
|
879
|
+
seen.set(kind, count);
|
|
880
|
+
|
|
881
|
+
const name = count === 1 ? kind : `${kind}-${count}`;
|
|
882
|
+
|
|
883
|
+
if (free(name)) {
|
|
884
|
+
book.add(["shadow", name], { $value: entry.value, $type: "shadow" });
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
result.shadows = [...book.tokens.values()].filter((entry) => entry.path[0] === "shadow").length;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
export function motionScale(
|
|
892
|
+
facts: Facts,
|
|
893
|
+
book: Book,
|
|
894
|
+
explicit: Set<Category>,
|
|
895
|
+
result: Inferred,
|
|
896
|
+
): void {
|
|
897
|
+
if (!explicit.has("duration")) {
|
|
898
|
+
const used = topBy([...facts.durations], ([, count]) => count, 4).sort(
|
|
899
|
+
(a, b) => a[0] - b[0],
|
|
900
|
+
);
|
|
901
|
+
const ladders: Record<number, string[]> = {
|
|
902
|
+
1: ["base"],
|
|
903
|
+
2: ["fast", "slow"],
|
|
904
|
+
3: ["fast", "base", "slow"],
|
|
905
|
+
4: ["instant", "fast", "base", "slow"],
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
used.forEach(([ms], at) => {
|
|
909
|
+
book.add(["duration", ladders[used.length]![at]!], {
|
|
910
|
+
$value: `${ms}ms`,
|
|
911
|
+
$type: "duration",
|
|
912
|
+
});
|
|
913
|
+
result.motion.durations.push(`${ladders[used.length]![at]} ${ms}ms`);
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
if (!explicit.has("ease")) {
|
|
918
|
+
const NAMES: Record<string, string> = {
|
|
919
|
+
ease: "standard",
|
|
920
|
+
"ease-in": "in",
|
|
921
|
+
"ease-out": "out",
|
|
922
|
+
"ease-in-out": "in-out",
|
|
923
|
+
linear: "linear",
|
|
924
|
+
};
|
|
925
|
+
const used = topBy([...facts.easings], ([, count]) => count, 4);
|
|
926
|
+
let custom = 0;
|
|
927
|
+
|
|
928
|
+
for (const [text] of used) {
|
|
929
|
+
const value = bezier(text);
|
|
930
|
+
|
|
931
|
+
if (value === undefined) {
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const key = JSON.stringify(value);
|
|
936
|
+
const known = Object.entries(BEZIER).find(([, each]) => JSON.stringify(each) === key);
|
|
937
|
+
const name =
|
|
938
|
+
NAMES[text] ??
|
|
939
|
+
(value[0] === 0.4 && value[1] === 0 && value[2] === 0.2 && value[3] === 1
|
|
940
|
+
? "standard"
|
|
941
|
+
: known === undefined
|
|
942
|
+
? `custom-${++custom}`
|
|
943
|
+
: NAMES[known[0]]!);
|
|
944
|
+
|
|
945
|
+
if (!book.has(["ease", name])) {
|
|
946
|
+
book.add(["ease", name], { $value: value, $type: "cubicBezier" });
|
|
947
|
+
result.motion.easings.push(`${name} ${text}`);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
export function breakpointScale(
|
|
954
|
+
facts: Facts,
|
|
955
|
+
book: Book,
|
|
956
|
+
explicit: Set<Category>,
|
|
957
|
+
result: Inferred,
|
|
958
|
+
): void {
|
|
959
|
+
if (explicit.has("breakpoint")) {
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
// 1100px and 1101px are one breakpoint: the more used stands for both.
|
|
964
|
+
const clusters: [number, number][] = [];
|
|
965
|
+
|
|
966
|
+
for (const [size, count] of [...facts.breakpoints].sort((a, b) => b[1] - a[1])) {
|
|
967
|
+
const near = clusters.find(([other]) => Math.abs(other - size) <= Math.max(2, size * 0.02));
|
|
968
|
+
|
|
969
|
+
if (near === undefined) {
|
|
970
|
+
clusters.push([size, count]);
|
|
971
|
+
} else {
|
|
972
|
+
near[1] += count;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
const used = topBy(clusters, ([, count]) => count, 5).sort((a, b) => a[0] - b[0]);
|
|
977
|
+
const names = ["sm", "md", "lg", "xl", "2xl"];
|
|
978
|
+
|
|
979
|
+
used.forEach(([size], at) => {
|
|
980
|
+
book.add(["breakpoint", names[at]!], { $value: `${size}px`, $type: "dimension" });
|
|
981
|
+
result.breakpoints.push(`${names[at]} ${size}px`);
|
|
982
|
+
});
|
|
983
|
+
}
|