ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/lenses-records/lens/record.html

lens/record.html

101 líneas, 4.1 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. <!doctype html>
  2. <html lang="ru" data-skin="desktop">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Запись</title>
  7. <link rel="stylesheet" href="/_kit/kit.css">
  8. <link rel="stylesheet" href="/_kit/skins/calm.css">
  9. <link rel="stylesheet" href="/_kit/skins/desktop.css">
  10. <link rel="stylesheet" href="lens.css">
  11. <script src="/_pkg/layout-basic/lens.js"></script>
  12. </head>
  13. <body>
  14. <p class="lens-error" role="alert" hidden>Не удалось загрузить запись.</p>
  15. <main class="record-page"></main>
  16. <script>
  17. const page = document.querySelector("main");
  18. const error = document.querySelector(".lens-error");
  19. const dateFormat = new Intl.DateTimeFormat("ru-RU", { dateStyle: "short", timeZone: "UTC" });
  20. let revision = 0;
  21. let ready = false;
  22. async function collectionProperties(folder) {
  23. const tree = await shtuk.call("tree");
  24. const collections = tree.filter(({ type }) => type === "Collection");
  25. const sibling = collections.find(({ path }) => path === folder + ".md");
  26. const matches = async ({ path }) => {
  27. const record = await shtuk.call("records.read", { path }).catch(() => null);
  28. return record?.meta.items?.path?.replace(/\/+$/, "") === folder.replace(/\/+$/, "");
  29. };
  30. let collection = sibling && await matches(sibling) ? sibling : undefined;
  31. if (!collection) {
  32. const others = collections.filter((entry) => entry !== sibling);
  33. const matched = await Promise.all(others.map(matches));
  34. collection = others[matched.indexOf(true)];
  35. }
  36. if (!collection) return {};
  37. const result = await shtuk.call("records.list", { collection: collection.path });
  38. return result.collection.properties ?? {};
  39. }
  40. function display(value, property) {
  41. if (value == null || value === "") return "";
  42. if (property?.kind === "date") {
  43. const date = new Date(value);
  44. if (Number.isFinite(date.getTime())) return dateFormat.format(date);
  45. }
  46. const option = property?.options?.find(({ name }) => name === value);
  47. if (option) return option.label ?? value;
  48. if (Array.isArray(value)) return value.join(", ");
  49. return typeof value === "object" ? JSON.stringify(value) : String(value);
  50. }
  51. async function draw() {
  52. const current = ++revision;
  53. try {
  54. const folder = shtuk.path.slice(0, shtuk.path.lastIndexOf("/")) || "/";
  55. const [{ path, meta, body }, properties] = await Promise.all([
  56. shtuk.call("records.read", { path: shtuk.path }),
  57. collectionProperties(folder).catch(() => ({})),
  58. ]);
  59. if (current !== revision) return;
  60. const heading = document.createElement("h1");
  61. heading.textContent = meta.title ?? path.split("/").at(-1).replace(/\.md$/, "");
  62. const list = document.createElement("dl");
  63. list.className = "record-fields";
  64. for (const [key, value] of Object.entries(meta)) {
  65. if (key === "title" || key === "type" || key === "icon") continue;
  66. const property = Object.hasOwn(properties, key) ? properties[key] : undefined;
  67. const label = document.createElement("dt");
  68. const field = document.createElement("dd");
  69. label.textContent = property?.label ?? key;
  70. field.textContent = display(value, property);
  71. list.append(label, field);
  72. }
  73. const content = document.createElement("div");
  74. content.className = "lx-md record-body";
  75. for (const text of body.trim().split(/\r?\n(?:[ \t]*\r?\n)+/)) {
  76. if (!text) continue;
  77. const paragraph = document.createElement("p");
  78. paragraph.textContent = text;
  79. content.append(paragraph);
  80. }
  81. page.replaceChildren(heading, ...(list.childElementCount ? [list] : []), content);
  82. error.hidden = true;
  83. } catch {
  84. if (current === revision) error.hidden = false;
  85. } finally {
  86. if (current === revision && !ready) {
  87. ready = true;
  88. shtuk.ready();
  89. }
  90. }
  91. }
  92. shtuk.on("changed", draw);
  93. draw();
  94. </script>
  95. </body>
  96. </html>