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,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dist/names.md` (stream T): for every token, its name in each generated file: the variable in `tokens.css`, the variable
|
|
3
|
+
* in `tailwind.css`, and the utilities that Tailwind makes from it. Generated, so a person who reads a class (`bg-surface`)
|
|
4
|
+
* finds the token (`color.bg.surface`) and the other way round, and so that Pi can write a class that exists.
|
|
5
|
+
*/
|
|
6
|
+
import type { ResolvedToken } from "./system-types.ts";
|
|
7
|
+
import type { CompileInput } from "./tokens-compile.ts";
|
|
8
|
+
import { namespaceOf, norm, tailwindNames } from "./tokens-tailwind.ts";
|
|
9
|
+
|
|
10
|
+
/** The utility that a role's group names (`color.bg.*` is for `bg-`): the first word of the path after `color`. */
|
|
11
|
+
const GROUP_UTILITY: Record<string, string> = {
|
|
12
|
+
bg: "bg",
|
|
13
|
+
background: "bg",
|
|
14
|
+
text: "text",
|
|
15
|
+
fg: "text",
|
|
16
|
+
foreground: "text",
|
|
17
|
+
border: "border",
|
|
18
|
+
outline: "outline",
|
|
19
|
+
ring: "ring",
|
|
20
|
+
fill: "fill",
|
|
21
|
+
stroke: "stroke",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** The utilities of a token, from the namespace of its Tailwind variable. */
|
|
25
|
+
function utilitiesOf(token: ResolvedToken, variable: string): string {
|
|
26
|
+
const namespace = namespaceOf(variable);
|
|
27
|
+
|
|
28
|
+
if (namespace === undefined) {
|
|
29
|
+
return `no utility: \`var(${variable})\``;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const rest = variable.slice(namespace.length + 3);
|
|
33
|
+
const one = (prefix: string) => `\`${prefix}-${rest}\``;
|
|
34
|
+
|
|
35
|
+
switch (namespace) {
|
|
36
|
+
case "color": {
|
|
37
|
+
// Only the group of the role says which utility it is for (`color.bg.*`); `color.accent.fill` is not for `fill-`.
|
|
38
|
+
const word = GROUP_UTILITY[norm(token.path[1] ?? "")];
|
|
39
|
+
|
|
40
|
+
return word !== undefined && token.path.length > 2
|
|
41
|
+
? one(word)
|
|
42
|
+
: ["bg", "text", "border"].map(one).join(" ");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
case "font":
|
|
46
|
+
case "font-weight":
|
|
47
|
+
return one("font");
|
|
48
|
+
case "spacing":
|
|
49
|
+
return ["p", "m", "gap"].map(one).join(" ");
|
|
50
|
+
case "radius":
|
|
51
|
+
return one("rounded");
|
|
52
|
+
case "breakpoint":
|
|
53
|
+
return `\`${rest}:\``;
|
|
54
|
+
case "container":
|
|
55
|
+
return `${one("max-w")} \`@${rest}:\``;
|
|
56
|
+
default:
|
|
57
|
+
return one(namespace);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const cell = (text: string) => text.replace(/\|/g, "\\|");
|
|
62
|
+
|
|
63
|
+
/** The Markdown of `dist/names.md`. */
|
|
64
|
+
export function namesMarkdown(system: CompileInput): string {
|
|
65
|
+
const names = tailwindNames(system);
|
|
66
|
+
const groups = new Map<string, ResolvedToken[]>();
|
|
67
|
+
|
|
68
|
+
for (const token of system.tokens) {
|
|
69
|
+
const group = token.path[0]!;
|
|
70
|
+
|
|
71
|
+
groups.set(group, [...(groups.get(group) ?? []), token]);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const lines = [
|
|
75
|
+
`# Names: ${system.name}`,
|
|
76
|
+
"",
|
|
77
|
+
"Generated by Pi Pocket from `.pi/design/`. Do not edit by hand: change the tokens, then compile again.",
|
|
78
|
+
"",
|
|
79
|
+
"Each token has a name in three places: the variable in `tokens.css`, the variable in `tailwind.css`, and the utilities that Tailwind makes from that variable. Use the variable in CSS (`var(--color-bg-surface)`) and the class in Tailwind markup (`bg-surface`).",
|
|
80
|
+
"",
|
|
81
|
+
"## How the names come out",
|
|
82
|
+
"",
|
|
83
|
+
"- **`tokens.css`**: the path of the token joined by `-`, with `--` in front. `color.bg.surface` is `--color-bg-surface`. Nothing else is added or dropped.",
|
|
84
|
+
"- **`tailwind.css`**: the variable is in the namespace of Tailwind v4 that fits the kind of the token, so that Tailwind makes the right utilities: `--color-*`, `--font-*` (families), `--text-*`, `--font-weight-*`, `--tracking-*`, `--leading-*`, `--breakpoint-*`, `--container-*`, `--spacing-*`, `--radius-*`, `--shadow-*`, `--inset-shadow-*`, `--drop-shadow-*`, `--blur-*`, `--perspective-*`, `--aspect-*`, `--ease-*`. A token that fits none keeps its `tokens.css` name (`--button-height`), and one whose name would be read as a namespace of another kind gets `--ds-` in front.",
|
|
85
|
+
"- **Color roles** drop the word that the utility already says: `color.bg.surface` is `--color-surface`, so the class is `bg-surface`, not `bg-bg-surface`; `color.text.muted` is `text-muted`; `color.border.subtle` is `border-subtle`.",
|
|
86
|
+
"- **A role that is only `default`, `primary`, `secondary`, `tertiary`, `base`, `main`, `normal`, `regular`, or `standard`** says nothing alone (`primary` is also the brand), so it takes the word of its group: `color.border.default` is `--color-border`, `color.text.primary` is `--color-text`, `color.bg.base` is `--color-bg`. Outside those groups a trailing `default` goes: `color.accent.default` is `--color-accent`.",
|
|
87
|
+
"- **Two tokens that want one name**: the first in the file keeps it, and the other keeps its full name (`color.text.secondary` is `--color-text-secondary`).",
|
|
88
|
+
"- Every color makes `bg-`, `text-`, `border-`, `ring-`, `fill-`, and `stroke-` utilities. The table shows the one that the group of the role names; the others work too.",
|
|
89
|
+
"",
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
for (const [group, tokens] of groups) {
|
|
93
|
+
lines.push(
|
|
94
|
+
`## ${group}`,
|
|
95
|
+
"",
|
|
96
|
+
"| Token | `tokens.css` | `tailwind.css` | Utilities |",
|
|
97
|
+
"| --- | --- | --- | --- |",
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
for (const token of tokens) {
|
|
101
|
+
const variable = names.get(token.name) ?? token.cssVar;
|
|
102
|
+
|
|
103
|
+
lines.push(
|
|
104
|
+
`| \`${cell(token.name)}\` | \`${cell(token.cssVar)}\` | \`${cell(variable)}\` | ${cell(utilitiesOf(token, variable))} |`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
lines.push("");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return `${lines.join("\n").trimEnd()}\n`;
|
|
112
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The check of numbered color ramps (stream T): a group whose children are numbers (`color.blue.100` to `color.blue.900`)
|
|
3
|
+
* promises that the steps go steadily from light to dark (or dark to light). A step that breaks that makes a role that
|
|
4
|
+
* points at it look wrong in one mode, and no one sees why. The check compares Oklch lightness, which follows what the
|
|
5
|
+
* eye sees, so it finds a step that a contrast check does not.
|
|
6
|
+
*/
|
|
7
|
+
import { parseColor, toOklch } from "./color.ts";
|
|
8
|
+
import type { ResolvedToken } from "./system-types.ts";
|
|
9
|
+
|
|
10
|
+
/** A ramp that is not steady in one mode. */
|
|
11
|
+
export interface RampProblem {
|
|
12
|
+
/** The group of the ramp: `color.blue`. */
|
|
13
|
+
group: string;
|
|
14
|
+
mode: string;
|
|
15
|
+
/** The two steps, as token names: the first has the lower number. */
|
|
16
|
+
first: string;
|
|
17
|
+
second: string;
|
|
18
|
+
message: string;
|
|
19
|
+
fix: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Lightness steps smaller than this are not a break: two steps may be the same lightness. */
|
|
23
|
+
const TOLERANCE = 0.004;
|
|
24
|
+
/** A ramp whose first and last step differ by less than this is flat: it has no direction. */
|
|
25
|
+
const FLAT = 0.05;
|
|
26
|
+
|
|
27
|
+
const percent = (lightness: number) => `${Math.round(lightness * 100)}%`;
|
|
28
|
+
|
|
29
|
+
/** The ramps of the tokens that are not steady in lightness, one problem for each ramp and mode that breaks. */
|
|
30
|
+
export function rampProblems(tokens: ResolvedToken[], modes: string[]): RampProblem[] {
|
|
31
|
+
const groups = new Map<string, ResolvedToken[]>();
|
|
32
|
+
|
|
33
|
+
for (const token of tokens) {
|
|
34
|
+
const step = token.path.at(-1)!;
|
|
35
|
+
|
|
36
|
+
if (token.type === "color" && token.path.length >= 2 && /^\d{1,4}$/.test(step)) {
|
|
37
|
+
const group = token.path.slice(0, -1).join(".");
|
|
38
|
+
|
|
39
|
+
groups.set(group, [...(groups.get(group) ?? []), token]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const problems: RampProblem[] = [];
|
|
44
|
+
|
|
45
|
+
for (const [group, steps] of groups) {
|
|
46
|
+
if (steps.length < 3) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ordered = [...steps].sort((a, b) => Number(a.path.at(-1)) - Number(b.path.at(-1)));
|
|
51
|
+
// The first mode that breaks is named; a ramp that breaks in every mode says it once.
|
|
52
|
+
const broken = new Set<string>();
|
|
53
|
+
|
|
54
|
+
for (const mode of modes) {
|
|
55
|
+
const lights: { token: ResolvedToken; l: number }[] = [];
|
|
56
|
+
|
|
57
|
+
for (const token of ordered) {
|
|
58
|
+
const text = token.modes[mode]?.hex;
|
|
59
|
+
const color = text === undefined ? undefined : parseColor(text);
|
|
60
|
+
|
|
61
|
+
// A translucent step has the lightness of what is behind it: it is not part of a ramp of solid colors.
|
|
62
|
+
if (color !== undefined && color.a >= 0.999) {
|
|
63
|
+
lights.push({ token, l: toOklch(color).l });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (lights.length < 3) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const span = lights.at(-1)!.l - lights[0]!.l;
|
|
72
|
+
|
|
73
|
+
if (Math.abs(span) < FLAT) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const direction = Math.sign(span);
|
|
78
|
+
|
|
79
|
+
for (let at = 1; at < lights.length; at++) {
|
|
80
|
+
const before = lights[at - 1]!;
|
|
81
|
+
const after = lights[at]!;
|
|
82
|
+
|
|
83
|
+
if ((after.l - before.l) * direction < -TOLERANCE) {
|
|
84
|
+
const key = `${before.token.name}|${after.token.name}`;
|
|
85
|
+
|
|
86
|
+
if (broken.has(key)) {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
broken.add(key);
|
|
91
|
+
|
|
92
|
+
const inMode = mode === modes[0] ? "" : ` in the ${mode} mode`;
|
|
93
|
+
const word = direction > 0 ? "darker" : "lighter";
|
|
94
|
+
const trend = direction > 0 ? "lighter" : "darker";
|
|
95
|
+
|
|
96
|
+
problems.push({
|
|
97
|
+
group,
|
|
98
|
+
mode,
|
|
99
|
+
first: before.token.name,
|
|
100
|
+
second: after.token.name,
|
|
101
|
+
message: `The ramp "${group}" is not steady in lightness${inMode}: step ${after.token.path.at(-1)} (${percent(after.l)}) is ${word} than step ${before.token.path.at(-1)} (${percent(before.l)}), but the ramp gets ${trend} as its numbers rise.`,
|
|
102
|
+
fix: `Swap the values of ${before.token.name} and ${after.token.name}, or recolor one of them so that lightness changes in one direction along "${group}".`,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return problems;
|
|
112
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The design system as compact text for the prompt (stream T): the variables Pi may use, with their values, grouped by
|
|
3
|
+
* what they are for, then the components and the rules. It is bounded, and the most useful lines come first: how to
|
|
4
|
+
* link it, the semantic tokens (the roles), the components, the scales, the guidelines, and last the raw palette and
|
|
5
|
+
* the tokens that belong to one component. What does not fit is named, not guessed. Inside a group, a token that
|
|
6
|
+
* components and other tokens use more comes first. Project text (descriptions, component names, guidelines) is data:
|
|
7
|
+
* it is cut to one line, stripped of invisible characters, and made safe to put between tags.
|
|
8
|
+
*/
|
|
9
|
+
import { defang } from "../fence.ts";
|
|
10
|
+
import type { DesignSystem, ResolvedToken } from "./system-types.ts";
|
|
11
|
+
|
|
12
|
+
const plain = (text: string) =>
|
|
13
|
+
defang(
|
|
14
|
+
text
|
|
15
|
+
.replace(/[\p{Cc}\p{Cf}\p{Cs}\p{Co}\p{Zl}\p{Zp}]+/gu, " ")
|
|
16
|
+
.replace(/\s+/g, " ")
|
|
17
|
+
.trim(),
|
|
18
|
+
);
|
|
19
|
+
const clip = (text: string, max: number) =>
|
|
20
|
+
text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
21
|
+
|
|
22
|
+
/** What a token reads like in a list: the value in the default mode, and in each other mode where it differs. */
|
|
23
|
+
function valueLine(system: DesignSystem, token: ResolvedToken): string {
|
|
24
|
+
const [base, ...others] = system.modes as [string, ...string[]];
|
|
25
|
+
|
|
26
|
+
const show = (mode: string) => {
|
|
27
|
+
const value = token.modes[mode]!;
|
|
28
|
+
|
|
29
|
+
return clip(value.hex ?? value.literal ?? value.css, 70);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const first = show(base);
|
|
33
|
+
const changes = others
|
|
34
|
+
.filter((mode) => show(mode) !== first)
|
|
35
|
+
.map((mode) => `${mode} ${show(mode)}`);
|
|
36
|
+
|
|
37
|
+
return `${first}${changes.length > 0 ? ` (${changes.join("; ")})` : ""}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A group heading for a token: its first path segment, so `color.bg.surface` is under `color`. */
|
|
41
|
+
const headingOf = (token: ResolvedToken) => token.path[0]!;
|
|
42
|
+
|
|
43
|
+
/** Collapses `color.blue.50` … `color.blue.900` into `color.blue.{50, 100 … 900}`. */
|
|
44
|
+
function palettes(tokens: ResolvedToken[]): string[] {
|
|
45
|
+
const groups = new Map<string, string[]>();
|
|
46
|
+
|
|
47
|
+
for (const token of tokens) {
|
|
48
|
+
const parent = token.path.slice(0, -1).join(".") || token.name;
|
|
49
|
+
const list = groups.get(parent) ?? [];
|
|
50
|
+
|
|
51
|
+
list.push(token.path.at(-1)!);
|
|
52
|
+
groups.set(parent, list);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return [...groups].map(([parent, leaves]) =>
|
|
56
|
+
leaves.length === 1 && parent === leaves[0]
|
|
57
|
+
? `${parent}`
|
|
58
|
+
: `${parent}.{${clip(leaves.join(", "), 80)}}`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A class name that is safe to show: letters, digits, and the marks that Tailwind and BEM use. */
|
|
63
|
+
const CLASS_NAME = /^[\w:.[\]%/#-]{1,48}$/;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The root element of a markup, and the class names of its states: `<article class="pk-card">` and the other classes
|
|
67
|
+
* the states use (`pk-card--selected`), so that Pi can reuse the markup without reading the file. Project text: only
|
|
68
|
+
* safe class names are kept.
|
|
69
|
+
*/
|
|
70
|
+
function rootOf(component: DesignSystem["components"][number]): string {
|
|
71
|
+
const classesIn = (tag: string) =>
|
|
72
|
+
(
|
|
73
|
+
/\bclass\s*=\s*(?:"([^"]*)"|'([^']*)')/i
|
|
74
|
+
.exec(tag)
|
|
75
|
+
?.slice(1)
|
|
76
|
+
.find((each) => each !== undefined) ?? ""
|
|
77
|
+
)
|
|
78
|
+
.split(/\s+/)
|
|
79
|
+
.filter((name) => CLASS_NAME.test(name));
|
|
80
|
+
const first = component.markup[component.states[0]!] ?? "";
|
|
81
|
+
const opening = /<([a-z][a-z0-9-]*)\b[^>]*>/i.exec(first);
|
|
82
|
+
|
|
83
|
+
if (opening === null) {
|
|
84
|
+
return "";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const tag = opening[1]!.toLowerCase();
|
|
88
|
+
const rootClasses = classesIn(opening[0]);
|
|
89
|
+
const seen = new Set(rootClasses);
|
|
90
|
+
const others: string[] = [];
|
|
91
|
+
|
|
92
|
+
for (const state of component.states) {
|
|
93
|
+
for (const element of (component.markup[state] ?? "").matchAll(/<[a-z][^>]*>/gi)) {
|
|
94
|
+
for (const name of classesIn(element[0])) {
|
|
95
|
+
if (!seen.has(name)) {
|
|
96
|
+
seen.add(name);
|
|
97
|
+
others.push(name);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return `root <${tag}${rootClasses.length === 0 ? "" : ` class="${rootClasses.join(" ")}"`}>${others.length === 0 ? "" : `; other classes ${others.slice(0, 8).join(" ")}${others.length > 8 ? " …" : ""}`}`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** How much each token is used: by components (their `tokens`), and by other tokens that are aliases of it. */
|
|
107
|
+
function useCounts(system: DesignSystem): Map<string, number> {
|
|
108
|
+
const uses = new Map<string, number>();
|
|
109
|
+
const add = (name: string) => uses.set(name, (uses.get(name) ?? 0) + 1);
|
|
110
|
+
|
|
111
|
+
for (const component of system.components) {
|
|
112
|
+
new Set(component.tokens).forEach(add);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const token of system.tokens) {
|
|
116
|
+
new Set(Object.values(token.modes).flatMap((value) => value.alias ?? [])).forEach(add);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return uses;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The headings of a list of tokens, in the order they first appear; colors come first, since they are the roles. */
|
|
123
|
+
function headingsOf(tokens: ResolvedToken[]): string[] {
|
|
124
|
+
const found = [...new Set(tokens.map(headingOf))];
|
|
125
|
+
|
|
126
|
+
return [
|
|
127
|
+
...found.filter((heading) => heading === "color"),
|
|
128
|
+
...found.filter((heading) => heading !== "color"),
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The summary of a system, at most `maxBytes` bytes. In order: how to link it, the semantic tokens with values, the
|
|
134
|
+
* components, the scales, the guidelines, the palette, the component tokens. Lines that do not fit are named.
|
|
135
|
+
*/
|
|
136
|
+
export function systemSummary(system: DesignSystem, maxBytes: number): string {
|
|
137
|
+
const uses = useCounts(system);
|
|
138
|
+
const errors = system.problems.filter((problem) => problem.level === "error").length;
|
|
139
|
+
const warnings = system.problems.length - errors;
|
|
140
|
+
const modes = system.modes
|
|
141
|
+
.map((mode, at) => (at === 0 ? `${mode} (default)` : mode))
|
|
142
|
+
.join(", ");
|
|
143
|
+
|
|
144
|
+
// A palette is collapsed when roles name it; a system with only raw colors lists them as the roles.
|
|
145
|
+
const roles = system.tokens.some(
|
|
146
|
+
(token) => token.type === "color" && token.tier !== "primitive",
|
|
147
|
+
);
|
|
148
|
+
const palette = roles
|
|
149
|
+
? system.tokens.filter((token) => token.type === "color" && token.tier === "primitive")
|
|
150
|
+
: [];
|
|
151
|
+
const rest = system.tokens.filter((token) => !palette.includes(token));
|
|
152
|
+
const ranked = (list: ResolvedToken[]) =>
|
|
153
|
+
list
|
|
154
|
+
.map((token, at) => ({ token, at }))
|
|
155
|
+
.sort(
|
|
156
|
+
(a, b) =>
|
|
157
|
+
(uses.get(b.token.name) ?? 0) - (uses.get(a.token.name) ?? 0) || a.at - b.at,
|
|
158
|
+
)
|
|
159
|
+
.map((each) => each.token);
|
|
160
|
+
// The semantic tokens, and the raw colors when nothing names them; then the scales; then what belongs to one component.
|
|
161
|
+
const semantic = rest.filter(
|
|
162
|
+
(token) =>
|
|
163
|
+
token.tier === "semantic" || (token.type === "color" && token.tier === "primitive"),
|
|
164
|
+
);
|
|
165
|
+
const scales = rest.filter((token) => token.tier === "primitive" && !semantic.includes(token));
|
|
166
|
+
const parts = rest.filter((token) => token.tier === "component");
|
|
167
|
+
|
|
168
|
+
// `notes.left` is how many descriptions the lines may still carry: the summary keeps them for the tokens that come first.
|
|
169
|
+
const tokenLines = (list: ResolvedToken[], notes: { left: number }): string[] => {
|
|
170
|
+
const lines: string[] = [];
|
|
171
|
+
|
|
172
|
+
for (const heading of headingsOf(list)) {
|
|
173
|
+
lines.push(`${heading}:`);
|
|
174
|
+
|
|
175
|
+
for (const token of ranked(list.filter((each) => headingOf(each) === heading))) {
|
|
176
|
+
const note =
|
|
177
|
+
token.deprecated === undefined
|
|
178
|
+
? ""
|
|
179
|
+
: ` [deprecated${typeof token.deprecated === "string" ? `: ${clip(plain(token.deprecated), 50)}` : ""}]`;
|
|
180
|
+
const why =
|
|
181
|
+
token.description === undefined || notes.left <= 0
|
|
182
|
+
? ""
|
|
183
|
+
: ` // ${clip(plain(token.description), 50)}`;
|
|
184
|
+
|
|
185
|
+
if (why !== "") {
|
|
186
|
+
notes.left -= 1;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
lines.push(` ${token.cssVar} ${valueLine(system, token)}${note}${why}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return lines;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const intro: string[] = [
|
|
197
|
+
`Design system "${plain(system.name).slice(0, 60)}": ${system.tokens.length} tokens, modes ${modes}, ${system.components.length} component${system.components.length === 1 ? "" : "s"}${errors + warnings > 0 ? `; ${errors} error${errors === 1 ? "" : "s"}, ${warnings} warning${warnings === 1 ? "" : "s"} (design_system validate lists them)` : ""}.`,
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
if (system.description !== undefined) {
|
|
201
|
+
intro.push(plain(system.description).slice(0, 200));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
intro.push(
|
|
205
|
+
'Link it in every design: <link rel="stylesheet" href="ds/tokens.css">' +
|
|
206
|
+
(system.components.length > 0
|
|
207
|
+
? ' and <link rel="stylesheet" href="ds/components.css">'
|
|
208
|
+
: "") +
|
|
209
|
+
". Use only these variables as var(--name); do not write raw colors, font sizes, or pixel spacing. Modes change by themselves" +
|
|
210
|
+
(system.modes.length > 1 ? `, or set <html data-theme="${system.modes[1]}">.` : "."),
|
|
211
|
+
'Set a variable in a design\'s tweaks manifest with its "--" name to let people change it live.',
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const componentLines: string[] = [];
|
|
215
|
+
|
|
216
|
+
if (system.components.length > 0) {
|
|
217
|
+
componentLines.push(
|
|
218
|
+
"",
|
|
219
|
+
"Components (design_system read file=components/<name>.html has the markup):",
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
for (const component of system.components) {
|
|
223
|
+
const root = rootOf(component);
|
|
224
|
+
|
|
225
|
+
componentLines.push(
|
|
226
|
+
` ${component.slug}: ${clip(plain(component.name), 40)}${root === "" ? "" : `; ${root}`}; states ${component.states.join(", ")}${component.description === "" ? "" : `; ${clip(plain(component.description), 80)}`}`,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const guidelineLines: string[] = [];
|
|
232
|
+
const guidelines = system.guidelines.trim();
|
|
233
|
+
|
|
234
|
+
if (guidelines !== "") {
|
|
235
|
+
guidelineLines.push("", "Guidelines (project rules):");
|
|
236
|
+
guidelineLines.push(
|
|
237
|
+
...defang(guidelines)
|
|
238
|
+
.replace(/[\p{Cc}\p{Cf}\p{Cs}\p{Co}]+/gu, (run) =>
|
|
239
|
+
run.includes("\n") ? "\n" : " ",
|
|
240
|
+
)
|
|
241
|
+
.split("\n")
|
|
242
|
+
.map((line) => ` ${line.trimEnd()}`)
|
|
243
|
+
.filter((line) => line.trim() !== "")
|
|
244
|
+
.slice(0, 40),
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// The sections in the order that they are kept when the summary is cut.
|
|
249
|
+
const sections = (described: number) => {
|
|
250
|
+
const notes = { left: described };
|
|
251
|
+
const first = [
|
|
252
|
+
...intro,
|
|
253
|
+
...(semantic.length > 0 ? ["", ...tokenLines(semantic, notes)] : []),
|
|
254
|
+
...componentLines,
|
|
255
|
+
];
|
|
256
|
+
const later = [
|
|
257
|
+
...(scales.length > 0
|
|
258
|
+
? ["", "Scales (raw values):", ...tokenLines(scales, notes)]
|
|
259
|
+
: []),
|
|
260
|
+
...guidelineLines,
|
|
261
|
+
...(palette.length > 0
|
|
262
|
+
? [
|
|
263
|
+
"",
|
|
264
|
+
`Palette (raw colors; prefer the roles above): ${palettes(palette).join("; ")}`,
|
|
265
|
+
]
|
|
266
|
+
: []),
|
|
267
|
+
...(parts.length > 0
|
|
268
|
+
? ["", "Component tokens (set by one component):", ...tokenLines(parts, notes)]
|
|
269
|
+
: []),
|
|
270
|
+
];
|
|
271
|
+
|
|
272
|
+
return { first, all: [...first, ...later] };
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// Fit: whole lines, then a note that names what was left out.
|
|
276
|
+
const bytes = (text: string) => Buffer.byteLength(text) + 1;
|
|
277
|
+
const total = (lines: string[]) => lines.reduce((sum, line) => sum + bytes(line), 0);
|
|
278
|
+
const note = (left: number) =>
|
|
279
|
+
`… ${left} more line${left === 1 ? "" : "s"} not shown: call design_system read for all of it.`;
|
|
280
|
+
const everything = system.tokens.length;
|
|
281
|
+
const full = sections(everything);
|
|
282
|
+
|
|
283
|
+
if (total(full.all) <= maxBytes) {
|
|
284
|
+
return full.all.join("\n");
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// The roles and the components matter most, then the scales and the rules: descriptions go from the last token to
|
|
288
|
+
// the first until the roles and the components take four fifths of the space.
|
|
289
|
+
let low = 0;
|
|
290
|
+
let high = everything;
|
|
291
|
+
|
|
292
|
+
while (low < high) {
|
|
293
|
+
const middle = Math.ceil((low + high) / 2);
|
|
294
|
+
|
|
295
|
+
if (total(sections(middle).first) <= maxBytes * 0.8) {
|
|
296
|
+
low = middle;
|
|
297
|
+
} else {
|
|
298
|
+
high = middle - 1;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const lines = sections(low).all;
|
|
303
|
+
const budget = maxBytes - bytes(note(lines.length));
|
|
304
|
+
const kept: string[] = [];
|
|
305
|
+
let used = 0;
|
|
306
|
+
|
|
307
|
+
for (const line of lines) {
|
|
308
|
+
if (used + bytes(line) > budget) {
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
kept.push(line);
|
|
313
|
+
used += bytes(line);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return [...kept, note(lines.length - kept.length)].join("\n");
|
|
317
|
+
}
|