Files
DC_CMS/ui/build.mjs
T
2026-11-05 16:04:35 +03:00

55 lines
7.4 KiB
JavaScript

// CMS application adapter. Shared component implementation belongs to @nodedc packages.
import React from "react";
import { renderToStaticMarkup as html } from "react-dom/server";
import { readFile, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { fileURLToPath } from "node:url";
import { build } from "esbuild";
import { AppHeader, HeaderNavigation, HeaderProfile, HeaderAvatar, ApplicationShell, ApplicationPanel, AdminNavigationPanel, Button, IconButton, Icon, StatusBadge, FieldFrame, TextField, Switch, SettingsCard, GlassSurface, MediaSourceField, DragHandle } from "@nodedc/ui-react";
const h = React.createElement, noop = () => {
}, root = fileURLToPath(new URL("../", import.meta.url));
const read = (p) => readFile(new URL(p, import.meta.url), "utf8");
const lock = JSON.parse(await read("design-system.lock.json"));
for (const [name, hash] of Object.entries(lock.packages)) {
if (createHash("sha256").update(await readFile(new URL(`vendor/${name}`, import.meta.url))).digest("hex") !== hash) throw Error(`Design package mismatch: ${name}`);
}
const icon = (name) => h(Icon, { name });
const btn = (id, label, variant = "secondary") => h(Button, { id, variant, tone: "neutral" }, label);
const ib = (id, label, name) => h(IconButton, { id, label }, icon(name));
const header = h(AppHeader, {
brand: h("img", { src: "./nodedc-logo.svg", alt: "NODE.DC" }),
brandMonochrome: true,
left: ib("toggle-navigation", "\u041F\u043E\u043A\u0430\u0437\u0430\u0442\u044C \u0440\u0430\u0437\u0434\u0435\u043B\u044B", "panel"),
center: h(HeaderNavigation, { label: "\u0420\u0430\u0437\u0434\u0435\u043B\u044B CMS", value: "content", onChange: noop, items: [["content", "\u041A\u043E\u043D\u0442\u0435\u043D\u0442"], ["seo", "SEO"], ["knowledge", "\u0411\u0430\u0437\u0430 \u0437\u043D\u0430\u043D\u0438\u0439"]].map(([value, label]) => ({ value, label: h("span", { "data-cms-mode": value }, label) })) }),
right: h("span", { id: "cms-profile", className: "cms-ui-island" })
});
const siteContext = h("div", { className: "cms-site-context" },
h("select", { id: "site-selector", "aria-label": "Выбрать сайт" }),
h("a", { id: "site-preview", className: "nodedc-icon-button", "aria-label": "Открыть сайт", title: "Открыть сайт", target: "_blank", rel: "noopener" }, icon("external")),
ib("project-settings", "Настройки сайта", "settings"),
h("span", { id: "workspace-name", hidden: true }, "NODE.DC"),
h("span", { id: "workspace-role", hidden: true }, "Администратор сайта"),
h("span", { id: "cms-add-block-parking", hidden: true }, ib("add-block", "Добавить блок", "plus"))
);
const nav = h(AdminNavigationPanel, { eyebrow: "NODE.DC / CMS", title: h("span", { id: "sidebar-title" }, "\u0410\u0434\u043C\u0438\u043D\u0438\u0441\u0442\u0440\u0438\u0440\u043E\u0432\u0430\u043D\u0438\u0435"), headerActions: ib("add-project", "\u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u0441\u0430\u0439\u0442", "plus"), contextSlot: siteContext, items: [], onClose: noop, onItemChange: noop });
const actions = h("div", { className: "cms-editor-actions" }, h(StatusBadge, { id: "dirty-state", "data-dirty": "false" }, "\u0421\u0438\u043D\u0445\u0440\u043E\u043D\u0438\u0437\u0438\u0440\u043E\u0432\u0430\u043D\u043E"), ib("reload", "\u041F\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044C \u0434\u0430\u043D\u043D\u044B\u0435", "refresh"), btn("save", "\u0421\u043E\u0445\u0440\u0430\u043D\u0438\u0442\u044C \u043C\u043E\u0434\u0435\u043B\u044C", "primary"), btn("render", "\u041E\u0431\u043D\u043E\u0432\u0438\u0442\u044C \u0441\u0430\u0439\u0442"), h(Button, { id: "publish", className: "hidden", tone: "neutral" }, "\u041E\u043F\u0443\u0431\u043B\u0438\u043A\u043E\u0432\u0430\u0442\u044C"));
const content = h(ApplicationPanel, { eyebrow: h("span", { id: "editor-heading" }, "\u041D\u0430\u0441\u0442\u0440\u043E\u0439\u043A\u0438 \u0441\u0430\u0439\u0442\u0430"), title: "\u041A\u043E\u043D\u0442\u0435\u043D\u0442", description: "\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u0440\u0430\u0437\u0434\u0435\u043B", headerTools: actions, expanded: true, onClose: noop }, h("div", { className: "cms-editor", dangerouslySetInnerHTML: { __html: await read("editor.html") } }));
const shell = html(h(ApplicationShell, { id: "cms-shell", header, stage: h("div", { className: "cms-stage" }, h("p", null, "\u0423\u043F\u0440\u0430\u0432\u043B\u0435\u043D\u0438\u0435 \u0441\u0430\u0439\u0442\u0430\u043C\u0438 NODE.DC"), btn("open-navigation", "\u041E\u0442\u043A\u0440\u044B\u0442\u044C \u0440\u0430\u0437\u0434\u0435\u043B\u044B")), navigation: nav, content, navigationOpen: true, contentOpen: true, contentExpanded: true }));
const icons = ["close", "plus", "chevron-up", "chevron-down", "copy", "clipboard", "trash", "eye", "eye-off", "layers", "panel", "file", "settings", "folder", "image", "video", "refresh", "move", "edit"];
const templates = Object.entries({ nestedSurface: h(GlassSurface, {tone:"group", padding:"md", materialRim:false}, h("span")), dragHandle: h(DragHandle, {label:"Перетащить блок"}), button: h(Button, null, "\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435"), iconButton: h(IconButton, { label: "\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435" }, icon("plus")), field: h(FieldFrame, { label: "\u041F\u043E\u043B\u0435" }, h("span", { "data-slot": "control" })), text: h(TextField, { id: "template-field", label: "\u041F\u043E\u043B\u0435" }), card: h(SettingsCard, { eyebrow: "\u0420\u0430\u0437\u0434\u0435\u043B", title: "\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435", description: "\u041E\u043F\u0438\u0441\u0430\u043D\u0438\u0435" }, h("div", { "data-slot": "body" })), media: h(MediaSourceField, { label: "\u041C\u0435\u0434\u0438\u0430", source: "file", url: "", onSourceChange: noop, onUrlChange: noop, onFileChange: noop }), ...Object.fromEntries(icons.map((name) => ["icon-" + name, icon(name)])) }).map(([name, node]) => `<template id="ds-${name}">${html(node)}</template>`).join("\n");
const page = `<!doctype html>
<html lang="ru" data-nodedc-ui="true" data-nodedc-theme="dark" data-nodedc-appearance="mission" data-nodedc-edges="flat"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="design-system-commit" content="${lock.sourceCommit}"><title>NODE.DC \u2014 CMS</title><link rel="stylesheet" href="./admin.css"></head><body>${shell}
${await read("modals.html")}
${templates}
<script type="module" src="./admin.js"><\/script></body></html>
`;
const js = await build({ absWorkingDir: root, entryPoints: ["ui/src/entry.mjs"], bundle: true, write: false, format: "esm", target: ["es2022"], minify: true, legalComments: "inline", define: { "process.env.NODE_ENV": '"production"' } });
const css = await build({ absWorkingDir: root, entryPoints: ["ui/src/app.css"], bundle: true, write: false, minify: true, loader: { ".woff2": "dataurl", ".woff": "dataurl" }, logLevel: "warning" });
const outputs = { "admin/index.html": page, "admin/admin.js": js.outputFiles[0].text, "admin/admin.css": css.outputFiles[0].text };
for (const [path, data] of Object.entries(outputs)) {
if (process.argv.includes("--check")) {
if (await readFile(root + path, "utf8") !== data) throw Error(`Stale generated UI: ${path}`);
} else await writeFile(root + path, data);
}
console.log(`CMS UI ${process.argv.includes("--check") ? "verified" : "built"} from Design Guidelines ${lock.sourceCommit}`);