ShtukbaseStandard
包文件→ .shtukbase/packages/shtukbase-icons/lib/sbicons.ts

lib/sbicons.ts

74 行, 4.2 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // The Shtukbase icon set in a project: its registry, its flat drawings, its IconStyle pages, and what the build makes of
  2. // them (the set rendered in a style, the Iconify JSON). Reads from the project root; the renderer is the icons package's.
  3. import { existsSync, readFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { type IconStyle, iconStylePages, readIconStyle, settings } from '../../icons/lib/iconlib.ts';
  6. import type { IconRecord, Registry } from '../../icons/lib/registry.ts';
  7. // lib/render.js is plain JS shared with the browser (it sets module.exports under Bun).
  8. // eslint-disable-next-line @typescript-eslint/no-require-imports
  9. export const R: {
  10. render(src: string, params: Record<string, unknown> | null, palette: Record<string, unknown>, opts?: { idPrefix?: string }): string;
  11. canonical(src: string, palette: Record<string, unknown>): { svg: string; notes: string[] };
  12. parse(src: string): { root: [string, string][]; els: { tag: string; attrs: [string, string][] }[] };
  13. snap(c: string, palette: Record<string, unknown>): { fill: string; hue: string | null; tone: string } | null;
  14. toneOf(c: string, palette: Record<string, unknown>): { fill: string; hue: string | null; tone: string } | null;
  15. flatColors(palette: Record<string, unknown>): string[];
  16. norm(c: string): string;
  17. } = require('../../icons/lib/render.js');
  18. export const SET_DIR = '/design/shtukbase-icons';
  19. export const REGISTRY = `${SET_DIR}/registry.json`;
  20. export const ICONIFY = `${SET_DIR}/iconify.json`;
  21. export const BASE_STYLE = '79';
  22. const abs = (root: string, p: string) => join(root, p.replace(/^\/+/, ''));
  23. export const readText = (root: string, p: string): string | null => (existsSync(abs(root, p)) ? readFileSync(abs(root, p), 'utf8') : null);
  24. export function registry(root: string): Registry {
  25. const t = readText(root, REGISTRY);
  26. if (t === null) throw new Error(`${REGISTRY}: not found (is shtukbase-icons installed?)`);
  27. return JSON.parse(t) as Registry;
  28. }
  29. /** The styles of this set: the IconStyle pages under /design/shtukbase-icons/styles/ (or that name this registry). */
  30. export function setStyles(root: string): IconStyle[] {
  31. return iconStylePages(root).map((p) => readIconStyle(root, p)).filter((s): s is IconStyle => !!s && (s.set === REGISTRY || s.page.startsWith(`${SET_DIR}/styles/`)));
  32. }
  33. export function styleById(root: string, id: string): IconStyle {
  34. const all = setStyles(root);
  35. const s = all.find((x) => x.id === id);
  36. if (!s) throw new Error(`no icon style "${id}" in ${SET_DIR}/styles/ (have: ${all.map((x) => x.id).join(', ') || 'none'})`);
  37. if (!s.render) throw new Error(`${s.page}: no render block`);
  38. return s;
  39. }
  40. /** One icon rendered in a style. */
  41. export function renderIcon(root: string, rec: IconRecord, style: IconStyle, idPrefix = ''): string {
  42. const src = readText(root, rec.file);
  43. if (src === null) throw new Error(`${rec.file}: not found`);
  44. return R.render(src, style.render, style.rawPalette, idPrefix ? { idPrefix } : undefined);
  45. }
  46. /** The folder the rendered set goes to: the `set` setting of the icons package (default /assets/icons/set). */
  47. export const outDir = (root: string) => settings(root).set;
  48. /** The set as an Iconify icon set (https://iconify.design/docs/types/iconify-json.html), rendered in a style. */
  49. export function iconify(root: string, reg: Registry, style: IconStyle): Record<string, unknown> {
  50. const icons: Record<string, { body: string }> = {};
  51. const aliases: Record<string, { parent: string }> = {};
  52. const names = new Set(reg.icons.map((r) => r.name));
  53. for (const rec of reg.icons) {
  54. const svg = renderIcon(root, rec, style, `${rec.name}-`);
  55. const body = svg.replace(/^[\s\S]*?<svg[^>]*>/, '').replace(/<\/svg>\s*$/, '');
  56. icons[rec.name] = { body };
  57. for (const a of rec.aliases ?? []) if (/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(a) && !names.has(a) && !aliases[a]) aliases[a] = { parent: rec.name };
  58. }
  59. return {
  60. prefix: reg.prefix || 'sb',
  61. info: { name: 'Shtukbase icons', total: reg.icons.length, author: { name: 'Shtukbase' }, height: 96, palette: true, category: `style ${style.id}` },
  62. lastModified: 0,
  63. icons, aliases, width: 96, height: 96,
  64. };
  65. }
  66. export const iconifyText = (j: Record<string, unknown>) => JSON.stringify(j, null, 1) + '\n';