Files
anidev-design-system/components/export-system.js
T

169 lines
4.4 KiB
JavaScript

import { concepts, tokensFor, cssVars } from "../concepts/registry.js";
function zip(files) {
const enc = new TextEncoder();
let offset = 0;
const chunks = [],
central = [];
const u16 = (v, n) => {
v.push(n & 255, (n >>> 8) & 255);
},
u32 = (v, n) => {
u16(v, n);
u16(v, n >>> 16);
};
for (const [name, content] of Object.entries(files)) {
const bytes = enc.encode(content),
nb = enc.encode(name);
let crc = -1;
for (const b of bytes) {
crc ^= b;
for (let k = 0; k < 8; k++)
crc = (crc >>> 1) ^ (crc & 1 ? 0xedb88320 : 0);
}
crc = (crc ^ -1) >>> 0;
const h = [];
u32(h, 0x04034b50);
u16(h, 20);
u16(h, 0x800);
u16(h, 0);
u16(h, 0);
u16(h, 0);
u32(h, crc);
u32(h, bytes.length);
u32(h, bytes.length);
u16(h, nb.length);
u16(h, 0);
h.push(...nb);
chunks.push(new Uint8Array(h), bytes);
const c = [];
u32(c, 0x02014b50);
u16(c, 20);
u16(c, 20);
u16(c, 0x800);
u16(c, 0);
u16(c, 0);
u16(c, 0);
u32(c, crc);
u32(c, bytes.length);
u32(c, bytes.length);
u16(c, nb.length);
u16(c, 0);
u16(c, 0);
u16(c, 0);
u16(c, 0);
u32(c, 0);
u32(c, offset);
c.push(...nb);
central.push(new Uint8Array(c));
offset += h.length + bytes.length;
}
const size = central.reduce((n, c) => n + c.length, 0),
end = [];
u32(end, 0x06054b50);
u16(end, 0);
u16(end, 0);
u16(end, central.length);
u16(end, central.length);
u32(end, size);
u32(end, offset);
u16(end, 0);
return new Blob([...chunks, ...central, new Uint8Array(end)], {
type: "application/zip",
});
}
export async function exportSystem(
{
page = "dashboard",
theme = "light",
density = "compact",
locale = "ru",
accent,
},
notify = () => {},
) {
const c = concepts[0];
const state = {
page,
theme,
density,
locale,
accents: accent ? { dock: accent } : {},
};
const files = {};
const paths = [
"components/system.css",
"components/export-system.js",
"components/account-pages.js",
"patterns/demo-identity.js",
"concepts/registry.js",
"playground/frame.html",
"playground/frame.js",
"tokens/schema.json",
"components/CONTRACT.md",
"patterns/UX.md",
"patterns/IDENTITY.md",
"prompts/SSO.md",
"prompts/README.md",
"layouts/README.md",
"DESIGN.md",
"ANTI-SLOP.md",
];
const fetched = await Promise.all(
paths.map(async (p) => {
const r = await fetch(new URL(p, new URL("../", import.meta.url)));
if (!r.ok) throw Error(p);
return [p, await r.text()];
}),
);
for (const [p, t] of fetched) files[p] = t;
for (const theme of ["light", "dark"]) {
const tokens = tokensFor(
c.id,
theme,
state.accents[c.id] || undefined,
state.density,
);
files[`tokens/${theme}.json`] = JSON.stringify(tokens, null, 2);
files[`tokens/${theme}.css`] = `:root {\n${cssVars(tokens)}\n}\n`;
}
files["selection.json"] = JSON.stringify(
{
version: "0.1.0",
status: "approved-design",
concept: c.id,
page: state.page,
accent,
customAccent: state.accents[c.id] || null,
density: state.density,
theme: state.theme,
locale: state.locale,
},
null,
2,
);
const exportParams = new URLSearchParams({
concept: c.id,
page: state.page,
density: state.density,
theme: state.theme,
locale: state.locale,
});
if (state.accents[c.id]) exportParams.set("accent", state.accents[c.id]);
files["index.html"] =
`<!doctype html><meta charset="utf-8"><title>Anidev ${c.name}</title><script>location.replace('playground/frame.html?${exportParams}')<\/script>`;
files["README.md"] =
`# Anidev / ${c.name}\nApproved Dock design.\nServe this folder over HTTP (for example python -m http.server 8080).\nOpen index.html.\nUse tokens/light.css or tokens/dark.css and components/system.css in any framework.\nSemantic token JSON is the source of values.\nNo upstream implementation is bundled. Reusable Skill packaging is tracked separately.\n`;
const url = URL.createObjectURL(zip(files)),
a = document.createElement("a");
a.href = url;
a.download = `anidev-${c.id}.zip`;
a.click();
setTimeout(() => URL.revokeObjectURL(url), 30000);
notify(
state.locale === "ru"
? "Экспортирована дизайн-система: " + c.name
: "Design system exported: " + c.name,
);
}