ShtukbaseStandard
файл пакета→ .shtukbase/packages/live/templates/tasks-app.html

templates/tasks-app.html

139 строк, 7.1 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. <!doctype html>
  2. <!--
  3. An example of an interactive page for the live server (package live). Plain HTML, no build, no libraries:
  4. the buttons call the server API through fetch, the list refreshes itself on the /api/events events.
  5. Copy and change: the collection is COLLECTION, the buttons are ACTIONS, the check is CHECK.
  6. -->
  7. <html lang="en">
  8. <head>
  9. <meta charset="utf-8">
  10. <meta name="viewport" content="width=device-width, initial-scale=1">
  11. <title>Tasks - live page</title>
  12. <style>
  13. :root { --bg: #f7f6f3; --card: #fff; --ink: #37352f; --mute: #787774; --line: #e9e9e7; --blue: #2f80ed; --red: #e03e3e; --green: #0f7b6c; }
  14. @media (prefers-color-scheme: dark) { :root { --bg: #191919; --card: #252525; --ink: #e6e6e4; --mute: #9b9a97; --line: #373737; } }
  15. * { box-sizing: border-box; }
  16. body { margin: 0; font: 14px/1.45 -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
  17. header { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; padding: 16px 24px; border-bottom: 1px solid var(--line); }
  18. h1 { font-size: 18px; margin: 0 12px 0 0; }
  19. .state { color: var(--mute); font-size: 12.5px; }
  20. .state.ro { color: var(--red); }
  21. form { display: flex; gap: 6px; }
  22. input { font: inherit; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--ink); min-width: 240px; }
  23. button { font: inherit; padding: 6px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--ink); cursor: pointer; }
  24. button.main { background: var(--blue); border-color: var(--blue); color: #fff; }
  25. button:disabled { opacity: .5; cursor: default; }
  26. main { display: flex; gap: 14px; padding: 18px 24px; overflow-x: auto; align-items: flex-start; }
  27. .col { flex: 0 0 250px; background: color-mix(in srgb, var(--line) 45%, transparent); border-radius: 12px; padding: 10px; }
  28. .col h2 { font-size: 13px; margin: 2px 4px 10px; display: flex; justify-content: space-between; color: var(--mute); }
  29. .card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 8px; }
  30. .card .t { font-weight: 600; margin-bottom: 8px; }
  31. .card .row { display: flex; gap: 6px; flex-wrap: wrap; }
  32. .err { color: var(--red); font-size: 12.5px; margin-top: 6px; }
  33. pre { margin: 0 24px 24px; padding: 12px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; max-height: 280px; overflow: auto; font-size: 12px; white-space: pre-wrap; }
  34. pre:empty { display: none; }
  35. </style>
  36. </head>
  37. <body>
  38. <header>
  39. <h1>Tasks</h1>
  40. <form id="new"><input name="title" placeholder="New task - title" required><button class="main">Create</button></form>
  41. <button id="check">Check project</button>
  42. <span id="state" class="state">…</span>
  43. </header>
  44. <main id="board"></main>
  45. <pre id="out"></pre>
  46. <script>
  47. // Which collection and which buttons. A button shows on a task in the state `from` and moves it to `to`.
  48. const COLLECTION = "/work/tasks";
  49. const ACTIONS = [
  50. { from: "todo", to: "doing", label: "Take" },
  51. { from: "doing", to: "done", label: "Done" },
  52. ];
  53. // An action from the allowlist (run: of the page /base/live.md): "shtuk check" runs all checks of the project.
  54. const CHECK = { package: "shtuk", action: "check" };
  55. const $ = (s) => document.querySelector(s);
  56. const h = (tag, attrs, ...kids) => { const el = Object.assign(document.createElement(tag), attrs || {}); el.append(...kids.filter((k) => k != null)); return el; };
  57. let hello = { write: false, run: [] };
  58. /** An API call: JSON both ways; a server error becomes an exception with its text. */
  59. async function api(method, path, body) {
  60. const r = await fetch(path, { method, headers: body ? { "Content-Type": "application/json" } : {}, body: body ? JSON.stringify(body) : undefined });
  61. const data = await r.json().catch(() => ({}));
  62. if (!r.ok) throw new Error(data.error || r.status + " " + r.statusText);
  63. return data;
  64. }
  65. async function load() {
  66. const { collection, records } = await api("GET", "/api/records?collection=" + encodeURIComponent(COLLECTION));
  67. const phases = collection.phases.status || [];
  68. const board = $("#board");
  69. board.replaceChildren();
  70. for (const ph of phases) {
  71. const items = records.filter((r) => r.fields.status === ph.name);
  72. const col = h("section", { className: "col" }, h("h2", {}, h("span", {}, ph.title), h("span", {}, String(items.length))));
  73. for (const r of items) col.append(card(r));
  74. board.append(col);
  75. }
  76. }
  77. function card(r) {
  78. const err = h("div", { className: "err" });
  79. const row = h("div", { className: "row" });
  80. for (const a of ACTIONS.filter((a) => a.from === r.fields.status)) {
  81. row.append(h("button", { textContent: a.label, disabled: !hello.write, onclick: async () => {
  82. err.textContent = "";
  83. try { await api("PATCH", "/api/record", { path: r.path, set: { status: a.to }, etag: r.etag }); }
  84. catch (e) { err.textContent = e.message; }
  85. } }));
  86. }
  87. return h("div", { className: "card" }, h("div", { className: "t", textContent: r.title }), row, err);
  88. }
  89. $("#new").onsubmit = async (e) => {
  90. e.preventDefault();
  91. const input = e.target.title;
  92. try { await api("POST", "/api/records", { collection: COLLECTION, fields: { title: input.value } }); input.value = ""; }
  93. catch (err) { alert(err.message); }
  94. };
  95. $("#check").onclick = async () => {
  96. const out = $("#out");
  97. out.textContent = "";
  98. // stream: true - the output arrives line by line (NDJSON) while the action runs
  99. const r = await fetch("/api/run", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...CHECK, stream: true }) });
  100. if (!r.ok) { out.textContent = (await r.json()).error; return; }
  101. const reader = r.body.getReader(), dec = new TextDecoder();
  102. let rest = "";
  103. for (;;) {
  104. const { done, value } = await reader.read();
  105. if (done) break;
  106. rest += dec.decode(value, { stream: true });
  107. const lines = rest.split("\n");
  108. rest = lines.pop();
  109. for (const l of lines) {
  110. const ev = JSON.parse(l);
  111. if (ev.type === "out") out.textContent += ev.text + "\n";
  112. if (ev.type === "end") out.textContent += (ev.code === 0 ? "PASS " : "FAIL ") + ev.step + "\n";
  113. if (ev.type === "exit") out.textContent += ev.code === 0 ? "\nAll checks passed.\n" : "\nThere are failures.\n";
  114. }
  115. out.scrollTop = out.scrollHeight;
  116. }
  117. };
  118. (async () => {
  119. try { hello = await api("GET", "/api/hello"); }
  120. catch { $("#state").textContent = "No live server: open the page through shtuk run live serve."; return; }
  121. $("#state").textContent = hello.write ? "Editing is on" : "Read only: open the server link with the token";
  122. $("#state").classList.toggle("ro", !hello.write);
  123. $("#new button").disabled = !hello.write;
  124. $("#check").disabled = !hello.write || !hello.run.includes(CHECK.package + " " + CHECK.action);
  125. if (hello.write && $("#check").disabled) $("#check").title = "Add \"shtuk check\" to run: of the page /base/live.md";
  126. await load();
  127. // the project files changed (here, in another tab or in an editor): read them again
  128. new EventSource("/api/events").addEventListener("change", () => load());
  129. })();
  130. </script>
  131. </body>
  132. </html>