import { escapeHtml as e } from "../../html.ts"; import { fmt, LANG_NAMES, LANGS, type Dict, type Lang } from "./i18n/index.ts"; import { flatten, label, MAX_DEPTH, type SectionNode } from "./tree.ts"; export const LIBRARY_CSS = `:root{--bg:#f7f6f2;--fg:#1d1d1b;--muted:#5d5d58;--card:#fff;--line:#e2e0d8;--accent:#2f5d50;--warn:#9a3412} @media (prefers-color-scheme:dark){:root{--bg:#141413;--fg:#ecebe6;--muted:#a3a29b;--card:#1e1e1c;--line:#33332f;--accent:#8cc1b0;--warn:#fdba74}} *{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--fg);font:16px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif} main{max-width:880px;margin:0 auto;padding:32px 16px}a{color:var(--accent)}h1{margin:8px 0}.lead,.meta,.note{color:var(--muted)} nav.top{display:flex;gap:16px;flex-wrap:wrap;font-size:.875rem;justify-content:space-between}nav.top span a{margin-left:8px} form.search{display:flex;gap:8px;margin:16px 0 24px}form.search input{flex:1} input,select,textarea,button{font:inherit;padding:6px 10px;border:1px solid var(--line);border-radius:6px;background:var(--card);color:var(--fg)} button{cursor:pointer;border-color:var(--accent);color:var(--accent)}textarea{width:100%;min-height:120px} details{margin:4px 0}details>div,ul.toc ul{margin-left:20px}summary{cursor:pointer;font-weight:600} ul{padding-left:20px}ul.toc{list-style:none;padding-left:0}.items{list-style:"📖 ";padding-left:24px} .card{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:20px;margin:16px 0} .error{border:1px solid var(--warn);color:var(--warn);border-radius:8px;padding:8px 12px}.ok{color:var(--accent)} .tag{display:inline-block;border:1px solid var(--line);border-radius:999px;padding:0 8px;margin:0 4px 4px 0;font-size:.875rem} .btn{display:inline-block;margin:8px 8px 0 0;padding:8px 16px;border:1px solid var(--accent);border-radius:8px;text-decoration:none} .inline{display:inline-flex;gap:6px;margin:2px 8px 2px 0;align-items:center}.hidden{opacity:.55} label{display:block;margin:10px 0 2px}label.check{display:flex;gap:6px;margin:2px 0;font-weight:normal} .f input[type=text],.f input[type=url]{width:100%}table{border-collapse:collapse;width:100%}td,th{border-bottom:1px solid var(--line);padding:6px;text-align:left} .d1{padding-left:20px}.d2{padding-left:40px}.d3{padding-left:60px}.d4{padding-left:80px}`; export interface ItemView { id: string; title: string; author: string; edition: string; language: string; annotation: string; tags: string[]; linkUrl: string | null; fileName: string | null; fileSize: number | null; fileAccess: "public" | "closed"; rightsBasis: "own" | "licensed" | "public_domain" | null; status: "draft" | "published"; sectionIds: string[]; } interface ShellOpts { lang: Lang; title: string; switchPath?: string } function shell(t: Dict, o: ShellOpts, inner: string): string { const langs = o.switchPath ? `${e(t.languages)}:${(Object.keys(LANGS) as Lang[]) .map((l) => (l === o.lang ? ` ${e(LANG_NAMES[l])}` : ` ${e(LANG_NAMES[l])}`)) .join("")}` : ""; return ` ${e(o.title)}
${inner}
`; } const withLang = (path: string, lang: Lang): string => `${path}${path.includes("?") ? "&" : "?"}lang=${lang}`; const err = (msg: string | undefined) => (msg ? `` : ""); // ---------- публичная часть ---------- function itemLink(i: ItemView): string { const by = i.author ? ` — ${e(i.author)}` : ""; return `
  • ${e(i.title)}${by}
  • `; } export function renderCatalog(t: Dict, lang: Lang, tree: SectionNode[], itemsBySection: Map, search?: { q: string; found: ItemView[] }): string { const node = (n: SectionNode): string => { const items = itemsBySection.get(n.id) ?? []; const inner = `${items.length ? `` : ""}${n.children.map(node).join("")}`; const title = e(label(n)); if (!inner) return `
    ${title}
    `; return `${title}
    ${inner}
    `; }; const q = search?.q ?? ""; const form = ``; const body = search ? `

    ${e(fmt(t.searchResults, { q }))}

    ${search.found.length ? `` : `

    ${e(t.nothingFound)}

    `}

    ${e(t.allLibrary)}

    ` : `

    ${e(t.tocHeading)}

    ${tree.map(node).join("")}`; const switchPath = search ? `/library?q=${encodeURIComponent(q)}` : "/library"; return shell(t, { lang, title: t.title, switchPath }, `

    ${e(t.title)}

    ${e(t.lead)}

    ${form}${body}`); } export function renderItem(t: Dict, lang: Lang, i: ItemView, sections: SectionNode[], opts: { editor: boolean }): string { const row = (k: string, v: string) => (v ? `
    ${e(k)}: ${e(v)}
    ` : ""); const tags = i.tags.length ? `
    ${e(t.tags)}: ${i.tags.map((x) => `${e(x)}`).join("")}
    ` : ""; const secs = sections.length ? `
    ${e(t.sections)}:
      ${sections.map((s) => `
    • ${e(label(s))}
    • `).join("")}
    ` : ""; const ann = i.annotation ? `

    ${e(t.annotation)}

    ${i.annotation.split(/\n{2,}/).map((p) => `

    ${e(p.trim())}

    `).join("")}` : ""; const link = i.linkUrl ? `${e(t.openLink)}` : ""; const file = i.fileName ? `${e(t.download)}${i.fileAccess === "closed" ? `

    ${e(t.closedFileNote)}

    ` : ""}` : ""; const draft = i.status === "draft" ? `

    ${e(t.draftNote)}

    ` : ""; const back = opts.editor ? ` · ${e(t.backToEditor)}` : ""; return shell( t, { lang, title: `${i.title} — ${t.title}`, switchPath: `/library/items/${i.id}` }, `${draft}

    ${e(i.title)}

    ${row(t.author, i.author)}${row(t.edition, i.edition)}${row(t.language, i.language)}${tags}${secs}${ann}

    ${link}${file}

    ${e(t.allLibrary)}${back}

    `, ); } export function renderMessage(t: Dict, lang: Lang, title: string, text: string): string { return shell(t, { lang, title }, `

    ${e(title)}

    ${e(text)}

    ${e(t.allLibrary)}

    `); } // ---------- редактор ---------- const csrfInput = (csrf: string) => ``; export function renderLogin(t: Dict, lang: Lang, opts: { error?: string; disabled?: boolean }): string { const form = opts.disabled ? "" : `

    `; return shell(t, { lang, title: t.loginTitle, switchPath: "/library/editor" }, `

    ${e(t.loginTitle)}

    ${e(opts.disabled ? t.editorOff : t.loginLead)}

    ${err(opts.error)}${form}`); } export function renderEditor(t: Dict, lang: Lang, csrf: string, tree: SectionNode[], items: ItemView[], opts: { error?: string; saved?: boolean }): string { const act = (n: SectionNode, action: string, text: string) => `
    ${csrfInput(csrf)}
    `; const node = (n: SectionNode): string => { const addChild = n.depth < MAX_DEPTH - 1 ? `
    ${csrfInput(csrf)}
    ` : ""; const rename = `
    ${csrfInput(csrf)}
    `; const hide = n.selfHidden ? act(n, "restore", t.restore) : act(n, "hide", t.hide); return `