包文件→ .shtukbase/packages/shtukbase-ui-html/lib/build.ts
lib/build.ts
88 行, 4.6 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // What `build` writes: ui.css (every component's CSS joined, in level then id order) and demo.html (every component in
- // every state and control value, and a package map made of the components). Pure: text in, text out.
- import { readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { type Component, type Control, LIB } from './components.ts';
- export function uiCss(root: string, comps: Component[]): string {
- const parts = [
- `/* ${LIB}/ui.css: every component's CSS joined, built by \`shtuk run ${LIB} build\`. Do not edit: edit the component's\n` +
- ` own .css under components/ and build again. Needs a style's tokens.css (the token contract v1) before it. */\n`,
- ];
- for (const c of comps) {
- if (!c.css) continue;
- parts.push(`\n/* ---- ${c.header.level}/${c.header.id} ---- */\n${readFileSync(join(root, c.css), 'utf8').trim()}\n`);
- }
- return parts.join('');
- }
- /** The root element of a template with one attribute set (null value: removed). */
- export function setAttr(markup: string, name: string, value: string | null): string {
- const m = /^<([a-z][a-z0-9-]*)\b([^>]*?)(\/?)>/i.exec(markup);
- if (!m) return markup;
- let attrs = m[2].replace(new RegExp(`\\s${name}(="[^"]*")?(?=[\\s/>]|$)`), '');
- if (value !== null) attrs += value === '' ? ` ${name}` : ` ${name}="${value}"`;
- return `<${m[1]}${attrs}${m[3]}>` + markup.slice(m[0].length);
- }
- /** The root element with one custom property set in its style attribute. */
- export function setProp(markup: string, prop: string, value: string): string {
- const m = /^<([a-z][a-z0-9-]*)\b([^>]*?)>/i.exec(markup);
- if (!m) return markup;
- const style = /\sstyle="([^"]*)"/.exec(m[2]);
- const decls = (style?.[1] ?? '').split(';').map((s) => s.trim()).filter((s) => s && !s.startsWith(`${prop}:`));
- decls.push(`${prop}: ${value}`);
- const attrs = (style ? m[2].replace(style[0], '') : m[2]) + ` style="${decls.join('; ')}"`;
- return `<${m[1]}${attrs}>` + markup.slice(m[0].length);
- }
- /** Card presets are bundles: the layout and size each preset goes with (the mock's PRESETS). */
- export const PRESET_BUNDLES: Record<string, { layout: string; size: string }> = {
- base: { layout: 'side', size: 'm' },
- compact: { layout: 'side', size: 's' },
- editorial: { layout: 'side', size: 'l' },
- cover: { layout: 'top', size: 'm' },
- tiles: { layout: 'badge', size: 'm' },
- };
- /** Controls the demo and the overflow test leave out: the grid's preset only sizes its columns, the cards keep their own. */
- export const CONTROL_SKIP: Record<string, string[]> = { 'package-grid': ['preset', 'layout', 'size'] };
- /** The picture of an icon button: an icon in place of the text, the text as its accessible name. */
- const PLUS = '<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M8 3.5v9M3.5 8h9"/></svg>';
- function iconButton(markup: string): string {
- const m = /^(<button\b[^>]*?)>([^<]*)<\/button>/.exec(markup);
- return m ? `${m[1]} aria-label="${m[2].trim()}">${PLUS}</button>` + markup.slice(m[0].length) : markup;
- }
- /** One picture per value of a control: [label, markup]. */
- export function variants(c: Component, name: string, ctl: Control): [string, string][] {
- const id = c.header.id;
- let base = c.templates.default ?? Object.values(c.templates)[0] ?? '';
- if (id === 'package-card' && name === 'cover') base = setAttr(setAttr(base, 'data-preset', 'cover'), 'data-layout', 'top');
- if (id === 'package-card' && name.startsWith('glow')) base = setAttr(setAttr(base, 'data-fill', 'faint'), 'data-glow', name === 'glow' ? null : 'under');
- if (id === 'package-card' && name === 'glow') base = setAttr(base, 'data-fill', 'faint');
- const one = (v: string): string => {
- let m = setAttr(base, `data-${name}`, v);
- if (id === 'package-card' && name === 'preset' && PRESET_BUNDLES[v])
- m = setAttr(setAttr(m, 'data-layout', PRESET_BUNDLES[v].layout), 'data-size', PRESET_BUNDLES[v].size);
- if (id === 'button' && name === 'size' && v === 'icon') m = iconButton(m);
- return m;
- };
- switch (ctl.kind) {
- case 'select':
- case 'segment':
- return (ctl.options ?? []).map((o) => [String(o), one(String(o))]);
- case 'swatch':
- return (ctl.options ?? []).filter((_, i) => i % 3 === 0).map((o) => [String(o), one(String(o))]);
- case 'bool':
- return [['off', setAttr(base, `data-${name}`, null)], ['on', setAttr(base, `data-${name}`, '')]];
- case 'range': {
- const vals = [...new Set([ctl.min, ctl.default, ctl.max].filter((v) => v !== undefined))] as number[];
- return vals.map((v) => [`${v}${ctl.unit ?? ''}`, setProp(base, `--${id}-${name}`, `${v}${ctl.unit ?? ''}`)]);
- }
- default:
- return [];
- }
- }