файл пакета→ .shtukbase/packages/lenses-records/lens/record.html
lens/record.html
101 строка, 4.1 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- <!doctype html>
- <html lang="ru" data-skin="desktop">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Запись</title>
- <link rel="stylesheet" href="/_kit/kit.css">
- <link rel="stylesheet" href="/_kit/skins/calm.css">
- <link rel="stylesheet" href="/_kit/skins/desktop.css">
- <link rel="stylesheet" href="lens.css">
- <script src="/_pkg/layout-basic/lens.js"></script>
- </head>
- <body>
- <p class="lens-error" role="alert" hidden>Не удалось загрузить запись.</p>
- <main class="record-page"></main>
- <script>
- const page = document.querySelector("main");
- const error = document.querySelector(".lens-error");
- const dateFormat = new Intl.DateTimeFormat("ru-RU", { dateStyle: "short", timeZone: "UTC" });
- let revision = 0;
- let ready = false;
- async function collectionProperties(folder) {
- const tree = await shtuk.call("tree");
- const collections = tree.filter(({ type }) => type === "Collection");
- const sibling = collections.find(({ path }) => path === folder + ".md");
- const matches = async ({ path }) => {
- const record = await shtuk.call("records.read", { path }).catch(() => null);
- return record?.meta.items?.path?.replace(/\/+$/, "") === folder.replace(/\/+$/, "");
- };
- let collection = sibling && await matches(sibling) ? sibling : undefined;
- if (!collection) {
- const others = collections.filter((entry) => entry !== sibling);
- const matched = await Promise.all(others.map(matches));
- collection = others[matched.indexOf(true)];
- }
- if (!collection) return {};
- const result = await shtuk.call("records.list", { collection: collection.path });
- return result.collection.properties ?? {};
- }
- function display(value, property) {
- if (value == null || value === "") return "";
- if (property?.kind === "date") {
- const date = new Date(value);
- if (Number.isFinite(date.getTime())) return dateFormat.format(date);
- }
- const option = property?.options?.find(({ name }) => name === value);
- if (option) return option.label ?? value;
- if (Array.isArray(value)) return value.join(", ");
- return typeof value === "object" ? JSON.stringify(value) : String(value);
- }
- async function draw() {
- const current = ++revision;
- try {
- const folder = shtuk.path.slice(0, shtuk.path.lastIndexOf("/")) || "/";
- const [{ path, meta, body }, properties] = await Promise.all([
- shtuk.call("records.read", { path: shtuk.path }),
- collectionProperties(folder).catch(() => ({})),
- ]);
- if (current !== revision) return;
- const heading = document.createElement("h1");
- heading.textContent = meta.title ?? path.split("/").at(-1).replace(/\.md$/, "");
- const list = document.createElement("dl");
- list.className = "record-fields";
- for (const [key, value] of Object.entries(meta)) {
- if (key === "title" || key === "type" || key === "icon") continue;
- const property = Object.hasOwn(properties, key) ? properties[key] : undefined;
- const label = document.createElement("dt");
- const field = document.createElement("dd");
- label.textContent = property?.label ?? key;
- field.textContent = display(value, property);
- list.append(label, field);
- }
- const content = document.createElement("div");
- content.className = "lx-md record-body";
- for (const text of body.trim().split(/\r?\n(?:[ \t]*\r?\n)+/)) {
- if (!text) continue;
- const paragraph = document.createElement("p");
- paragraph.textContent = text;
- content.append(paragraph);
- }
- page.replaceChildren(heading, ...(list.childElementCount ? [list] : []), content);
- error.hidden = true;
- } catch {
- if (current === revision) error.hidden = false;
- } finally {
- if (current === revision && !ready) {
- ready = true;
- shtuk.ready();
- }
- }
- }
- shtuk.on("changed", draw);
- draw();
- </script>
- </body>
- </html>