@bigmistqke/whiteboard 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/README.md +67 -17
  2. package/bin/whiteboard.ts +268 -71
  3. package/dist/client/runtime.js +88 -41
  4. package/dist/define.js +21 -2
  5. package/dist/plugins/callout/index.js +3 -1
  6. package/dist/plugins/callout/public/style.css +30 -0
  7. package/dist/plugins/chat/index.js +7 -4
  8. package/dist/plugins/chat/{client.js → public/client.js} +1 -1
  9. package/dist/plugins/chat/public/style.css +98 -0
  10. package/dist/plugins/code/index.js +3 -1
  11. package/dist/plugins/code/public/style.css +68 -0
  12. package/dist/plugins/comments/index.js +4 -5
  13. package/dist/plugins/comments/public/client.js +19 -0
  14. package/dist/plugins/comments/public/style.css +185 -0
  15. package/dist/plugins/details/index.js +7 -2
  16. package/dist/plugins/details/public/style.css +7 -0
  17. package/dist/plugins/form/index.js +37 -8
  18. package/dist/plugins/form/public/style.css +126 -0
  19. package/dist/plugins/gallery/index.js +4 -3
  20. package/dist/plugins/gallery/public/client.js +1 -0
  21. package/dist/plugins/gallery/public/style.css +90 -0
  22. package/dist/plugins/grid/index.js +9 -3
  23. package/dist/plugins/grid/public/style.css +71 -0
  24. package/dist/plugins/iframe/index.js +13 -14
  25. package/dist/plugins/iframe/public/style.css +13 -0
  26. package/dist/plugins/index.js +3 -1
  27. package/dist/plugins/mermaid/index.js +3 -1
  28. package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
  29. package/dist/plugins/terminal/index.js +10 -9
  30. package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
  31. package/dist/plugins/toc/README.md +12 -0
  32. package/dist/plugins/toc/index.js +9 -5
  33. package/dist/plugins/toc/public/client.js +1 -0
  34. package/dist/plugins/toc/public/style.css +93 -0
  35. package/dist/plugins/window/README.md +29 -0
  36. package/dist/plugins/window/index.js +64 -0
  37. package/dist/solid/index.js +14 -11
  38. package/dist/src/client/page/base.css +1078 -199
  39. package/dist/src/client/page/main.js +174 -32
  40. package/dist/src/loader.js +7 -2
  41. package/dist/src/package.js +3 -6
  42. package/dist/src/pages/blocks.js +8 -13
  43. package/dist/src/pages/markdown.js +1 -1
  44. package/dist/src/pages/render.js +73 -13
  45. package/dist/src/pages/window.js +30 -0
  46. package/dist/src/public.js +24 -0
  47. package/dist/src/server.js +37 -15
  48. package/package.json +3 -1
  49. package/plugins/callout/index.ts +3 -1
  50. package/plugins/callout/public/style.css +30 -0
  51. package/plugins/chat/autosize.ts +43 -9
  52. package/plugins/chat/client.tsx +22 -5
  53. package/plugins/chat/index.ts +22 -8
  54. package/plugins/chat/public/style.css +98 -0
  55. package/plugins/claude-code/index.ts +49 -14
  56. package/plugins/code/diff.ts +28 -8
  57. package/plugins/code/index.ts +30 -20
  58. package/plugins/code/public/style.css +68 -0
  59. package/plugins/comments/client.ts +139 -71
  60. package/plugins/comments/index.ts +9 -7
  61. package/plugins/comments/public/style.css +185 -0
  62. package/plugins/details/index.ts +7 -2
  63. package/plugins/details/public/style.css +7 -0
  64. package/plugins/forge/index.ts +16 -3
  65. package/plugins/form/index.ts +75 -27
  66. package/plugins/form/public/style.css +126 -0
  67. package/plugins/gallery/client.tsx +9 -3
  68. package/plugins/gallery/index.ts +31 -9
  69. package/plugins/gallery/public/style.css +90 -0
  70. package/plugins/grid/index.ts +19 -5
  71. package/plugins/grid/public/style.css +71 -0
  72. package/plugins/iframe/index.ts +20 -23
  73. package/plugins/iframe/public/style.css +13 -0
  74. package/plugins/index.ts +16 -1
  75. package/plugins/mermaid/index.ts +3 -1
  76. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  77. package/plugins/terminal/client.ts +105 -48
  78. package/plugins/terminal/host.ts +131 -29
  79. package/plugins/terminal/index.ts +84 -25
  80. package/plugins/terminal/public/style.css +225 -0
  81. package/plugins/terminal/server.ts +64 -13
  82. package/plugins/terminal/sizes.ts +13 -3
  83. package/plugins/toc/README.md +12 -0
  84. package/plugins/toc/client.ts +58 -29
  85. package/plugins/toc/index.ts +22 -5
  86. package/plugins/toc/public/style.css +93 -0
  87. package/plugins/window/README.md +29 -0
  88. package/plugins/window/index.ts +85 -0
  89. package/skills/whiteboard/SKILL.md +3 -1
  90. package/solid/index.tsx +24 -9
  91. package/src/auth.ts +25 -7
  92. package/src/client/forms.ts +74 -28
  93. package/src/client/markdown.ts +15 -4
  94. package/src/client/page/base.css +1078 -199
  95. package/src/client/page/elsewhere.ts +32 -15
  96. package/src/client/page/fresh.ts +91 -23
  97. package/src/client/page/hold.ts +11 -2
  98. package/src/client/page/live.ts +10 -6
  99. package/src/client/page/main.ts +106 -34
  100. package/src/client/page/orphans.ts +25 -10
  101. package/src/client/page/sort.ts +14 -4
  102. package/src/client/page/sticky.ts +11 -3
  103. package/src/client/page/theme.ts +11 -7
  104. package/src/client/page/waiting.ts +141 -34
  105. package/src/client/page/window.ts +40 -0
  106. package/src/client/reactive.ts +19 -0
  107. package/src/client/read.ts +17 -10
  108. package/src/client/runtime.ts +130 -35
  109. package/src/client/scroll.ts +14 -4
  110. package/src/client/toast.ts +19 -5
  111. package/src/config.ts +20 -5
  112. package/src/define.ts +67 -23
  113. package/src/events.ts +111 -24
  114. package/src/html.ts +6 -2
  115. package/src/icons.ts +4 -2
  116. package/src/load.ts +10 -2
  117. package/src/loader.ts +75 -11
  118. package/src/package.ts +9 -8
  119. package/src/pages/blocks.ts +20 -14
  120. package/src/pages/markdown.ts +113 -34
  121. package/src/pages/render.ts +198 -37
  122. package/src/pages/window.ts +71 -0
  123. package/src/plugins.ts +90 -24
  124. package/src/public.ts +39 -0
  125. package/src/running.ts +8 -2
  126. package/src/serve.ts +29 -7
  127. package/src/server.ts +351 -108
  128. package/src/status.ts +15 -9
  129. package/dist/plugins/callout/style.css +0 -12
  130. package/dist/plugins/chat/style.css +0 -24
  131. package/dist/plugins/code/style.css +0 -29
  132. package/dist/plugins/comments/client.js +0 -10
  133. package/dist/plugins/comments/style.css +0 -40
  134. package/dist/plugins/details/style.css +0 -3
  135. package/dist/plugins/form/style.css +0 -44
  136. package/dist/plugins/gallery/client.js +0 -1
  137. package/dist/plugins/gallery/style.css +0 -27
  138. package/dist/plugins/grid/style.css +0 -25
  139. package/dist/plugins/iframe/client.js +0 -1
  140. package/dist/plugins/iframe/style.css +0 -29
  141. package/dist/plugins/toc/client.js +0 -1
  142. package/dist/plugins/toc/style.css +0 -19
  143. package/plugins/callout/style.css +0 -12
  144. package/plugins/chat/style.css +0 -24
  145. package/plugins/code/style.css +0 -29
  146. package/plugins/comments/style.css +0 -40
  147. package/plugins/details/style.css +0 -3
  148. package/plugins/form/style.css +0 -44
  149. package/plugins/gallery/style.css +0 -27
  150. package/plugins/grid/style.css +0 -25
  151. package/plugins/iframe/client.ts +0 -46
  152. package/plugins/iframe/style.css +0 -29
  153. package/plugins/terminal/style.css +0 -71
  154. package/plugins/toc/style.css +0 -19
  155. /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
@@ -470,18 +470,33 @@ const unseen = /* @__PURE__ */ new Set();
470
470
  const timers = /* @__PURE__ */ new WeakMap();
471
471
  let storeKey = "";
472
472
  let core$1;
473
+ const SERVER_MARKS = /* @__PURE__ */ new Set([
474
+ "data-wb-at",
475
+ "data-wb-key",
476
+ "data-wb-maximize",
477
+ "data-wb-other",
478
+ "data-wb-other-text",
479
+ "data-wb-quiet",
480
+ "data-wb-self",
481
+ "data-wb-sort"
482
+ ]);
473
483
  /**
474
- * FNV-1a over the block's HTML, ignoring per-render attributes and anything quiet: content inside
475
- * [data-wb-quiet] can appear, change or go without the block counting as changed.
484
+ * FNV-1a over the block's HTML as the server sent it, ignoring anything quiet: content inside
485
+ * [data-wb-quiet] can appear, change or go without the block counting as changed. What the browser
486
+ * added is left out, so a block in the page and the same block in a new render have one signature:
487
+ * an island's content (it draws itself; its props are in its key), the page's own elements (a folded
488
+ * form's card), its marks (data-wb-* the server doesn't write: a form's state) and its classes for
489
+ * new content.
476
490
  */
477
491
  function signature(el) {
478
492
  const clone = el.cloneNode(true);
479
- for (const q of clone.querySelectorAll("[data-wb-quiet]")) q.remove();
480
- for (const n of [clone, ...clone.querySelectorAll("[data-wb-island],[data-wb-mounted]")]) {
481
- n.removeAttribute("data-wb-island");
482
- n.removeAttribute("data-wb-mounted");
493
+ for (const q of clone.querySelectorAll("[data-wb-quiet], .wb-folded")) q.remove();
494
+ for (const n of [clone, ...clone.querySelectorAll("*")]) {
495
+ for (const { name } of [...n.attributes]) if (name.startsWith("data-wb-") && !SERVER_MARKS.has(name)) n.removeAttribute(name);
496
+ n.classList.remove("wb-new", "wb-new-seen");
497
+ if (!n.classList.length) n.removeAttribute("class");
483
498
  }
484
- clone.removeAttribute("data-wb-block");
499
+ for (const island of clone.querySelectorAll("[data-wb-key]")) island.replaceChildren();
485
500
  const s = clone.outerHTML;
486
501
  let h = 2166136261;
487
502
  for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619);
@@ -671,23 +686,35 @@ function trackUpdate(main, previous, next) {
671
686
  saveSeen(main);
672
687
  update$1();
673
688
  }
689
+ // src/client/reactive.ts
690
+ function reactive(get) {
691
+ const listeners = /* @__PURE__ */ new Set();
692
+ const value = (() => get());
693
+ value.on = (fn) => {
694
+ listeners.add(fn);
695
+ return () => {
696
+ listeners.delete(fn);
697
+ };
698
+ };
699
+ value.notify = () => {
700
+ const now = get();
701
+ for (const fn of [...listeners]) fn(now);
702
+ };
703
+ return value;
704
+ }
674
705
  // src/client/page/theme.ts
675
706
  const media = matchMedia("(prefers-color-scheme: dark)");
676
- const listeners = /* @__PURE__ */ new Set();
677
707
  const resolve = () => document.documentElement.dataset.theme ?? (media.matches ? "dark" : "light");
678
708
  let theme = resolve();
679
709
  function update() {
680
710
  const next = resolve();
681
711
  if (next === theme) return;
682
712
  theme = next;
683
- for (const fn of listeners) fn(theme);
713
+ currentTheme.notify();
684
714
  }
685
715
  media.addEventListener("change", update);
686
- const currentTheme = () => theme;
687
- function onTheme(fn) {
688
- listeners.add(fn);
689
- return () => listeners.delete(fn);
690
- }
716
+ /** The theme in effect, as it changes (api.theme). */
717
+ const currentTheme = reactive(() => theme);
691
718
  const themePreference = () => document.documentElement.dataset.theme ?? "auto";
692
719
  function setThemePreference(pref) {
693
720
  if (pref === "auto") delete document.documentElement.dataset.theme;
@@ -707,6 +734,30 @@ const STATES = [
707
734
  ];
708
735
  /** How many things on a page wait for the reader: its open questions and unread messages. */
709
736
  const awaiting = (p) => p.open.length + p.unread.length;
737
+ // src/client/forms.ts
738
+ /** One line quoting an answer: what was chosen, then what was written. */
739
+ /**
740
+ * An answer, told apart: the button it was sent with (none for a plain Send), what was chosen
741
+ * (options, short values), and what was written.
742
+ */
743
+ function answerParts(data, action) {
744
+ const chosen = [], written = [];
745
+ for (const v of Object.values(data)) {
746
+ if (v == null || v === "" || v === false) continue;
747
+ if (Array.isArray(v)) {
748
+ if (v.length) chosen.push(v.join(", "));
749
+ } else if (v === true) continue;
750
+ else if (String(v).length > 60 || String(v).includes("\n")) written.push(String(v));
751
+ else chosen.push(String(v));
752
+ }
753
+ return {
754
+ action: action && !["Send", "Submit"].includes(action) ? action : null,
755
+ chosen,
756
+ written
757
+ };
758
+ }
759
+ let current$2 = [];
760
+ reactive(() => current$2);
710
761
  // src/icons.ts
711
762
  /** An inbox: what awaits the reader (the header's menu, the overview). */
712
763
  const INBOX_ICON = "<svg viewBox=\"0 0 16 16\" width=\"13\" height=\"13\" aria-hidden=\"true\"><path d=\"M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linejoin=\"round\"/></svg>";
@@ -774,8 +825,8 @@ function trackElsewhere(root, handle, core) {
774
825
  toggle.focus();
775
826
  }
776
827
  });
777
- core.onEvents(reload);
778
- core.onListeners(reload);
828
+ core.events.on(reload);
829
+ core.listeners.on(reload);
779
830
  handle.onChange(reload);
780
831
  load();
781
832
  }
@@ -811,11 +862,50 @@ let waitingToast;
811
862
  let unreadToast;
812
863
  let toastTimer = 0;
813
864
  let unreadShown = false;
865
+ const chatsOpen = /* @__PURE__ */ new Set();
866
+ function card(form, name, last) {
867
+ let el = form.querySelector(":scope > .wb-folded");
868
+ if (!el) {
869
+ el = document.createElement("div");
870
+ el.className = "wb-folded";
871
+ el.innerHTML = "<div class=\"wb-folded-head\"><span class=\"wb-folded-choice\"></span><span class=\"wb-folded-change\"></span></div><ul class=\"wb-folded-choices\"></ul><div class=\"wb-folded-text\"></div><button type=\"button\" class=\"wb-folded-chat\"></button>";
872
+ form.prepend(el);
873
+ }
874
+ const { action, chosen, written } = answerParts(last.data, last.action);
875
+ const list = action || chosen.length > 1 ? chosen : [];
876
+ const head = action ?? (chosen.length > 1 ? "answered" : chosen[0] ?? written.shift() ?? "answered");
877
+ el.querySelector(".wb-folded-choice").textContent = `✓ ${head}`;
878
+ const choices = el.querySelector(".wb-folded-choices");
879
+ choices.replaceChildren(...list.map((c) => Object.assign(document.createElement("li"), { textContent: c })));
880
+ choices.hidden = !list.length;
881
+ el.querySelector(".wb-folded-change").textContent = form.hasAttribute("data-wb-just-sent") ? "✓ the agent has it" : "decided · change";
882
+ const text = el.querySelector(".wb-folded-text");
883
+ text.textContent = written.join("\n\n");
884
+ text.hidden = !written.length;
885
+ const messages = api$1.events().filter((e) => e.type === "chat:message" && e.data.chat === name).length;
886
+ const thread = form.querySelector(":scope > [data-block=chat-thread]");
887
+ const unreadHere = !!thread && api$1.unread().some((m) => thread.querySelector(`[data-wb-message="${CSS.escape(m.id)}"]`) || m.type === "chat:message" && m.data.chat === name);
888
+ const box = thread?.querySelector("textarea");
889
+ const writing = !!box && (document.activeElement === box || box.value.trim() !== "");
890
+ if (unreadHere || writing) chatsOpen.add(name);
891
+ const open = chatsOpen.has(name);
892
+ form.toggleAttribute("data-wb-chat-open", open);
893
+ const chat = el.querySelector(".wb-folded-chat");
894
+ chat.hidden = !messages;
895
+ chat.textContent = `💬 ${messages} message${messages === 1 ? "" : "s"} ${open ? "▾" : "▸"}`;
896
+ chat.setAttribute("aria-expanded", String(open));
897
+ }
814
898
  function show(list = forms) {
815
899
  forms = list;
816
900
  for (const { form, name, state, last } of forms) {
817
901
  if (last && reopened.has(name) && reopened.get(name) !== last.id && state === "decided") reopened.delete(name);
818
- form.toggleAttribute("data-wb-folded", state === "decided" && !reopened.has(name));
902
+ const folded = state === "decided" && !reopened.has(name);
903
+ form.toggleAttribute("data-wb-folded", folded);
904
+ if (folded && last) card(form, name, last);
905
+ else {
906
+ form.querySelector(":scope > .wb-folded")?.remove();
907
+ form.removeAttribute("data-wb-chat-open");
908
+ }
819
909
  }
820
910
  const unread = api$1.unread().length;
821
911
  const parts = ORDER.map((state) => [state, inState(state).length]).filter(([, count]) => count).map(([state, count]) => [state, LABEL[state](count)]);
@@ -909,14 +999,23 @@ function trackWaiting(root, core) {
909
999
  const form = target.closest("form[data-wb-folded]");
910
1000
  if (!form || target.closest("[data-block=chat-thread]")) return;
911
1001
  e.preventDefault();
1002
+ if (target.closest(".wb-folded-chat")) {
1003
+ const name = form.getAttribute("name");
1004
+ if (chatsOpen.has(name) || form.hasAttribute("data-wb-chat-open")) {
1005
+ chatsOpen.delete(name);
1006
+ form.removeAttribute("data-wb-chat-open");
1007
+ } else chatsOpen.add(name);
1008
+ show(forms);
1009
+ return;
1010
+ }
912
1011
  const name = form.getAttribute("name");
913
1012
  const last = api$1.last(name);
914
1013
  if (last) reopened.set(name, last.id);
915
1014
  show(forms);
916
1015
  form.querySelector("input:not([type=hidden]), textarea, select")?.focus({ preventScroll: true });
917
1016
  });
918
- api$1.onForms(show);
919
- api$1.onEvents(() => show());
1017
+ api$1.forms.on(show);
1018
+ api$1.events.on(() => show());
920
1019
  show(api$1.forms());
921
1020
  document.addEventListener("submit", (e) => {
922
1021
  const form = e.target;
@@ -956,7 +1055,7 @@ function trackLive(root, handle, api) {
956
1055
  dot.title = !live ? "Reconnecting… the page may be out of date" : !hasAnswers ? "Live" : own ? "Live · an agent is listening on this page" : anyPage ? "Live · an agent is listening on every page" : "Live · no agent is listening right now; answers wait in the log";
957
1056
  dot.setAttribute("aria-label", dot.title);
958
1057
  };
959
- api.onListeners(show);
1058
+ api.listeners.on(show);
960
1059
  new MutationObserver(show).observe(document.documentElement, {
961
1060
  attributes: true,
962
1061
  attributeFilter: ["data-wb-live"]
@@ -1067,6 +1166,40 @@ function trackHold() {
1067
1166
  });
1068
1167
  addEventListener("resize", note);
1069
1168
  }
1169
+ // src/client/page/window.ts
1170
+ const windows = () => [...document.querySelectorAll("#wb-main .wb-window")];
1171
+ let maximized = null;
1172
+ function apply() {
1173
+ const all = windows();
1174
+ if (maximized != null && !all[maximized]) maximized = null;
1175
+ all.forEach((win, i) => {
1176
+ const on = i === maximized;
1177
+ win.toggleAttribute("data-wb-maximized", on);
1178
+ const button = win.querySelector("[data-wb-maximize]");
1179
+ if (button) {
1180
+ button.title = on ? "Back into the page (Esc)" : "Fill the window";
1181
+ button.setAttribute("aria-label", button.title);
1182
+ button.setAttribute("aria-pressed", String(on));
1183
+ }
1184
+ });
1185
+ document.documentElement.toggleAttribute("data-wb-window-maximized", maximized != null);
1186
+ }
1187
+ function trackWindows(api) {
1188
+ document.addEventListener("click", (e) => {
1189
+ const win = e.target.closest?.("[data-wb-maximize]")?.closest(".wb-window");
1190
+ if (!win) return;
1191
+ const i = windows().indexOf(win);
1192
+ maximized = maximized === i ? null : i;
1193
+ apply();
1194
+ });
1195
+ document.addEventListener("keydown", (e) => {
1196
+ if (e.key === "Escape" && maximized != null) {
1197
+ maximized = null;
1198
+ apply();
1199
+ }
1200
+ });
1201
+ api.onRendered(apply);
1202
+ }
1070
1203
  // src/client/page/orphans.ts
1071
1204
  /** A message in a chat: the chat block's event. */
1072
1205
  const isMessage = (e) => e.type === "chat:message" && typeof e.data.chat === "string";
@@ -1086,15 +1219,21 @@ function trackOrphanThreads(main, api) {
1086
1219
  box.hidden = threads.size === 0;
1087
1220
  if (box.hidden) return box.replaceChildren();
1088
1221
  box.innerHTML = String(html`<h2>Chats no longer on this page</h2>
1089
- ${[...threads].map(([chat, messages]) => {
1222
+ ${[...threads].map(([chat, messages]) => {
1090
1223
  const replaced = new Set(messages.map((m) => m.data.replaces).filter(Boolean).map(String));
1091
- return html`<div class="wb-orphan" data-chat="${chat}"><h3>${chat}</h3>${messages.filter((m) => !replaced.has(m.id)).map((m) => {
1224
+ return html`<div class="wb-orphan" data-chat="${chat}">
1225
+ <h3>${chat}</h3>
1226
+ ${messages.filter((m) => !replaced.has(m.id)).map((m) => {
1092
1227
  const who = m.from === "cli" ? "agent" : "you";
1093
- return html`<div class="wb-orphan-message" data-from="${who}"><span class="wb-orphan-who">${who}</span><span class="wb-orphan-text">${String(m.data.text ?? "")}</span></div>`;
1094
- })}</div>`;
1228
+ return html`<div class="wb-orphan-message" data-from="${who}">
1229
+ <span class="wb-orphan-who">${who}</span
1230
+ ><span class="wb-orphan-text">${String(m.data.text ?? "")}</span>
1231
+ </div>`;
1232
+ })}
1233
+ </div>`;
1095
1234
  })}`);
1096
1235
  }
1097
- api.onEvents(draw);
1236
+ api.events.on(draw);
1098
1237
  api.onRendered(draw);
1099
1238
  draw();
1100
1239
  }
@@ -1328,12 +1467,15 @@ async function refresh() {
1328
1467
  try {
1329
1468
  next = await currentPage.render();
1330
1469
  } catch (err) {
1331
- if (/: 404$/.test(err.message)) return location.reload();
1470
+ if (/: 404$/.test(err.message)) {
1471
+ if (currentPage.data()?.mtime == null) return;
1472
+ return location.reload();
1473
+ }
1332
1474
  throw err;
1333
1475
  }
1334
1476
  const data = next.data;
1335
1477
  islands = data.islands;
1336
- for (const href of data.styles) if (![...document.querySelectorAll("link[rel=stylesheet]")].some((l) => new URL(l.href).pathname === new URL(href, location.href).pathname)) document.head.insertAdjacentHTML("beforeend", String(html`<link rel="stylesheet" href="${href}">`));
1478
+ for (const href of data.styles) if (![...document.querySelectorAll("link[rel=stylesheet]")].some((l) => new URL(l.href).pathname === new URL(href, location.href).pathname)) document.head.insertAdjacentHTML("beforeend", String(html`<link rel="stylesheet" href="${href}" />`));
1337
1479
  const content = new DOMParser().parseFromString(next.html, "text/html").getElementById("wb-main")?.innerHTML ?? "";
1338
1480
  const nextSigs = signaturesOf(content);
1339
1481
  const place = placeOf(nextSigs);
@@ -1367,11 +1509,10 @@ const mount = async (root, handle, coreApi) => {
1367
1509
  core = coreApi;
1368
1510
  page = handle.id;
1369
1511
  main = root.querySelector("#wb-main");
1370
- islands = handle.data.islands;
1512
+ islands = handle.data().islands;
1371
1513
  api = {
1372
1514
  ...core,
1373
- theme: currentTheme,
1374
- onTheme
1515
+ theme: currentTheme
1375
1516
  };
1376
1517
  const isDir = handle.kind === "pages" || handle.kind === "folder";
1377
1518
  themeToggle(root);
@@ -1383,8 +1524,8 @@ const mount = async (root, handle, coreApi) => {
1383
1524
  subtree: true
1384
1525
  });
1385
1526
  handle.onChange(() => refresh());
1386
- if (isDir) core.onEvents(() => refresh());
1387
- if (isDir) core.onListeners(() => refresh());
1527
+ if (isDir) core.events.on(() => refresh());
1528
+ if (isDir) core.listeners.on(() => refresh());
1388
1529
  blockSigs = [...main.children].map(signature);
1389
1530
  if (!isDir) trackInitial(main, page, core);
1390
1531
  if (!isDir) trackWaiting(main, core);
@@ -1396,6 +1537,7 @@ const mount = async (root, handle, coreApi) => {
1396
1537
  }
1397
1538
  trackSticky(main, (fn) => core.onRendered(fn));
1398
1539
  trackHold();
1540
+ trackWindows(core);
1399
1541
  if (!isDir) trackOrphanThreads(main, core);
1400
1542
  await mountIslands();
1401
1543
  tickTimes();
@@ -1,6 +1,7 @@
1
1
  import { importFresh, importOwn } from "./load.js";
2
2
  import { createPages } from "./pages/index.js";
3
3
  import { pluginCommands } from "./plugins.js";
4
+ import { inPublic, publicDirOf } from "./public.js";
4
5
  import fs from "node:fs";
5
6
  import path from "node:path";
6
7
  import { fileURLToPath } from "node:url";
@@ -84,12 +85,14 @@ function createConfigLoader(proj, { pages: withPages = true } = {}) {
84
85
  if (builtin.has(e.name)) plugins[plugins.findIndex((p) => p.name === e.name)] = e;
85
86
  else plugins.push(e);
86
87
  }
88
+ const publicDirs = new Map(plugins.map((p) => [p.name, publicDirOf(p, proj.wb)]));
87
89
  const blocks = plugins.flatMap((p) => Object.entries(p.blocks ?? {}).map(([name, block]) => ({
88
90
  name,
89
91
  block,
90
- plugin: p.name
92
+ plugin: p.name,
93
+ public: publicDirs.get(p.name)
91
94
  })));
92
- const styles = plugins.map((p) => p.style).filter((s) => !!s);
95
+ const styles = plugins.filter((p) => p.style).map((p) => inPublic(publicDirs.get(p.name), p.style, `plugin ${p.name}: style`));
93
96
  const links = plugins.flatMap((p) => p.links ? [p.links.bind(p)] : []);
94
97
  const pages = withPages ? createPages({
95
98
  blocks,
@@ -103,6 +106,7 @@ function createConfigLoader(proj, { pages: withPages = true } = {}) {
103
106
  plugins,
104
107
  handedOut: declared("handOut"),
105
108
  forReader: declared("forReader"),
109
+ publicDirs,
106
110
  files: config?.files !== false
107
111
  };
108
112
  }
@@ -115,6 +119,7 @@ function createConfigLoader(proj, { pages: withPages = true } = {}) {
115
119
  plugins: [],
116
120
  handedOut: /* @__PURE__ */ new Set(),
117
121
  forReader: /* @__PURE__ */ new Set(),
122
+ publicDirs: /* @__PURE__ */ new Map(),
118
123
  files: false,
119
124
  errors: [error]
120
125
  }));
@@ -25,9 +25,6 @@ const FROM_SOURCE = fileURLToPath(import.meta.url).endsWith(".ts");
25
25
  * dist/. The same whether the module runs from source (plugins/form/index.ts gives
26
26
  * dist/plugins/form/client.js) or from dist/ itself.
27
27
  */
28
- function builtFile(moduleUrl, name) {
29
- const rel = path.relative(PACKAGE_DIR, path.dirname(fileURLToPath(moduleUrl)));
30
- const inDist = rel === "dist" || rel.startsWith("dist" + path.sep);
31
- return path.join(PACKAGE_DIR, inDist ? "" : "dist", rel, name);
32
- }
33
- export { FROM_SOURCE, PACKAGE_DIR, builtFile, packagePath };
28
+ /** A built-in plugin's public folder: its built browser modules and copied styles (build.ts). */
29
+ const builtinPublic = (name) => packagePath("dist", "plugins", name, "public");
30
+ export { FROM_SOURCE, PACKAGE_DIR, builtinPublic, packagePath };
@@ -1,22 +1,17 @@
1
+ import { inPublic } from "../public.js";
1
2
  import fs from "node:fs";
2
- import path from "node:path";
3
- import { fileURLToPath } from "node:url";
3
+ import "node:path";
4
+ import "node:url";
4
5
  // src/pages/blocks.ts
5
6
  const NAME = /^[\w-]+$/;
6
- /** A block's file reference (URL, file: URL string, or absolute path) → absolute path. */
7
- function toPath(ref, field, block) {
8
- if (ref == null) return null;
9
- if (ref instanceof URL || typeof ref === "string" && ref.startsWith("file:")) return fileURLToPath(ref);
10
- if (typeof ref === "string" && path.isAbsolute(ref)) return ref;
11
- throw new Error(`block "${block}": ${field} must be a URL, e.g. new URL('./${field}.js', import.meta.url)`);
12
- }
13
- /** Check and resolve one block into what the engine uses. */
14
- function normalize({ name, block: b, plugin }) {
7
+ /** Check and resolve one block into what the engine uses: its files in its plugin's public folder. */
8
+ function normalize({ name, block: b, plugin, public: dir }) {
15
9
  if (typeof b !== "object" || !b) throw new Error(`plugin ${plugin}: block "${name}" is not an object`);
16
10
  if (!NAME.test(name)) throw new Error(`plugin ${plugin}: block name ${JSON.stringify(name)}: use letters, digits, - and _`);
11
+ const file = (ref, field) => ref == null ? null : inPublic(dir, ref, `plugin ${plugin}, block "${name}": ${field}`);
17
12
  const files = {
18
- client: toPath(b.client, "client", name),
19
- style: toPath(b.style, "style", name)
13
+ client: file(b.client, "client"),
14
+ style: file(b.style, "style")
20
15
  };
21
16
  for (const [field, file] of Object.entries(files)) if (file && !fs.existsSync(file)) throw new Error(`plugin ${plugin}, block "${name}": ${field} file not found: ${file}`);
22
17
  return {
@@ -404,7 +404,7 @@ async function renderMarkdown(source, { page, file, resolve, code = null, linkFo
404
404
  html: stringify(tree.children ?? []),
405
405
  title,
406
406
  islands,
407
- styles: [...used.values()].map((t) => t.style).filter((s) => !!s)
407
+ styles: [...new Set([...used.values()].map((t) => t.style).filter((s) => !!s))]
408
408
  };
409
409
  }
410
410
  export { formNames, parseAttrs, renderMarkdown };
@@ -40,11 +40,26 @@ function titleOf(file, fallback) {
40
40
  }
41
41
  function crumbs(id, name) {
42
42
  const parts = id ? id.split("/") : [];
43
- return html`<a href="/">${name}</a>${parts.map((p, i) => html` / <a href="/${parts.slice(0, i + 1).join("/")}">${p}</a>`)}`;
43
+ return html`<a href="/">${name}</a
44
+ >${parts.map((p, i) => html` / <a href="/${parts.slice(0, i + 1).join("/")}">${p}</a>`)}`;
44
45
  }
45
46
  const time = (ts) => html`<time data-ts="${ts}"></time>`;
46
47
  function pageList(pages) {
47
- return html`<ul class="wb-index">${pages.map((p) => html`<li><a href="/${p.id}">${titleOf(p.file, path.basename(p.id))}</a>${p.awaiting > 0 && html` <a class="wb-awaiting" href="/${p.id}#wb-waiting" title="${awaitingLabel(p.awaiting)}" aria-label="${awaitingLabel(p.awaiting)}">${raw("<svg viewBox=\"0 0 16 16\" width=\"13\" height=\"13\" aria-hidden=\"true\"><path d=\"M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linejoin=\"round\"/></svg>")}${p.awaiting}</a>`} <span class="wb-meta"><code>${p.id}</code> · edited ${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}</span></li>`)}</ul>`;
48
+ return html`<ul class="wb-index">
49
+ ${pages.map((p) => html`<li>
50
+ <a href="/${p.id}">${titleOf(p.file, path.basename(p.id))}</a>${p.awaiting > 0 && html` <a
51
+ class="wb-awaiting"
52
+ href="/${p.id}#wb-waiting"
53
+ title="${awaitingLabel(p.awaiting)}"
54
+ aria-label="${awaitingLabel(p.awaiting)}"
55
+ >${raw("<svg viewBox=\"0 0 16 16\" width=\"13\" height=\"13\" aria-hidden=\"true\"><path d=\"M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linejoin=\"round\"/></svg>")}${p.awaiting}</a
56
+ >`}
57
+ <span class="wb-meta"
58
+ ><code>${p.id}</code> · edited
59
+ ${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}</span
60
+ >
61
+ </li>`)}
62
+ </ul>`;
48
63
  }
49
64
  /** The folder's README, when it has one. */
50
65
  function readmeOf(page) {
@@ -137,9 +152,29 @@ function documentList(ctx) {
137
152
  try {
138
153
  mtime = fs.statSync(abs).mtimeMs;
139
154
  } catch {}
140
- return html`<li data-order="${i}" data-updated="${mtime}" data-title="${title.toLowerCase()}"><a href="${ctx.urlFor(abs) ?? `/-/${f}`}">${title}</a> <span class="wb-meta">${where !== "." && html`<code>${where === f ? f : `${where}/`}</code> · `}edited ${time(mtime)}</span></li>`;
155
+ return html`<li data-order="${i}" data-updated="${mtime}" data-title="${title.toLowerCase()}">
156
+ <a href="${ctx.urlFor(abs) ?? `/-/${f}`}">${title}</a>
157
+ <span class="wb-meta"
158
+ >${where !== "." && html`<code>${where === f ? f : `${where}/`}</code> · `}edited ${time(mtime)}</span
159
+ >
160
+ </li>`;
141
161
  });
142
- return html`<section class="wb-documents"><div class="wb-list-head"><h2>Documents</h2><label class="wb-sort">sort <select data-wb-sort="documents" aria-label="Sort the documents"><option value="order">by folder</option><option value="updated">last updated</option><option value="title">by title</option></select></label></div><ul class="wb-index">${rows}</ul></section>`;
162
+ return html`<section class="wb-documents">
163
+ <div class="wb-list-head">
164
+ <h2>Documents</h2>
165
+ <label class="wb-sort"
166
+ >sort
167
+ <select data-wb-sort="documents" aria-label="Sort the documents">
168
+ <option value="order">by folder</option>
169
+ <option value="updated">last updated</option>
170
+ <option value="title">by title</option>
171
+ </select></label
172
+ >
173
+ </div>
174
+ <ul class="wb-index">
175
+ ${rows}
176
+ </ul>
177
+ </section>`;
143
178
  }
144
179
  /** The plugins' text links for a project folder, asked once per folder. */
145
180
  function textLinksOf(links) {
@@ -203,7 +238,14 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
203
238
  case "pages": {
204
239
  const list = page.pages.length ? pageList(page.pages) : html`<p class="wb-meta">No pages yet.</p>`;
205
240
  return {
206
- html: page.id ? html`<h1>${page.id}</h1>${list}<p class="wb-meta">${page.file}</p>` : html`<h1>${project.name}</h1>${list}${project.files && documentList(ctx)}<p class="wb-meta"><code>${project.dir}</code>${project.files && html` · <a href="/-/">files</a>`} · <a href="/_/docs">how to write pages</a></p>`,
241
+ html: page.id ? html`<h1>${page.id}</h1>
242
+ ${list}
243
+ <p class="wb-meta">${page.file}</p>` : html`<h1>${project.name}</h1>
244
+ ${list}${project.files && documentList(ctx)}
245
+ <p class="wb-meta">
246
+ <code>${project.dir}</code>${project.files && html` · <a href="/-/">files</a>`} ·
247
+ <a href="/_/docs">how to write pages</a>
248
+ </p>`,
207
249
  title: page.id || project.name,
208
250
  islands: {},
209
251
  styles: []
@@ -211,8 +253,14 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
211
253
  }
212
254
  case "folder": {
213
255
  const rel = page.id.slice(2);
214
- const rows = page.entries.map((e) => html`<li><a href="${encodeURIComponent(e.name) + (e.dir ? "/" : "")}">${e.name + (e.dir ? "/" : "")}</a></li>`);
215
- const listing = html`<h1>${rel || project.name}</h1><ul class="wb-files">${rel && html`<li><a href="../">../</a></li>`}${rows}</ul><p class="wb-meta">${page.file} · <code>${page.id}</code></p>`;
256
+ const rows = page.entries.map((e) => html`<li>
257
+ <a href="${encodeURIComponent(e.name) + (e.dir ? "/" : "")}">${e.name + (e.dir ? "/" : "")}</a>
258
+ </li>`);
259
+ const listing = html`<h1>${rel || project.name}</h1>
260
+ <ul class="wb-files">
261
+ ${rel && html`<li><a href="../">../</a></li>`}${rows}
262
+ </ul>
263
+ <p class="wb-meta">${page.file} · <code>${page.id}</code></p>`;
216
264
  const readme = readmeOf(page);
217
265
  if (!readme) return {
218
266
  html: listing,
@@ -223,7 +271,9 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
223
271
  const r = await markdown(readme.source, page.id, readme.file, ctx);
224
272
  return {
225
273
  ...r,
226
- html: html`${raw(r.html)}<hr>${listing}`
274
+ html: html`${raw(r.html)}
275
+ <hr />
276
+ ${listing}`
227
277
  };
228
278
  }
229
279
  case "file": {
@@ -246,7 +296,8 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
246
296
  linkFor: ctx.urlFor
247
297
  });
248
298
  return {
249
- html: html`<h1 class="wb-code-title wb-wide">${page.id.slice(2)}</h1><div class="wb-code wb-wide">${raw(out.html)}</div>`,
299
+ html: html`<h1 class="wb-code-title wb-wide">${page.id.slice(2)}</h1>
300
+ <div class="wb-code wb-wide">${raw(out.html)}</div>`,
250
301
  title: path.basename(page.file),
251
302
  islands: out.islands,
252
303
  styles: out.styles
@@ -270,7 +321,16 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
270
321
  client: ctx.asset(island.client)
271
322
  }]));
272
323
  const styles = out.styles.map((s) => ctx.asset(s));
273
- const nav = html`<nav class="wb-nav"><span>${crumbs(page.id, ctx.project.name)}</span>${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<button type="button" class="wb-theme" title="Theme: follow the system, light or dark" aria-label="Theme"></button><span class="wb-live" role="img" title="Connecting…" aria-label="Connecting…"></span></nav>`;
324
+ const nav = html`<nav class="wb-nav">
325
+ <span>${crumbs(page.id, ctx.project.name)}</span
326
+ >${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<button
327
+ type="button"
328
+ class="wb-theme"
329
+ title="Theme: follow the system, light or dark"
330
+ aria-label="Theme"
331
+ ></button
332
+ ><span class="wb-live" role="img" title="Connecting…" aria-label="Connecting…"></span>
333
+ </nav>`;
274
334
  const links = [
275
335
  ctx.asset(BASE_CSS),
276
336
  ...PLUGIN_STYLES.map((f) => ctx.asset(f)),
@@ -279,9 +339,9 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
279
339
  ];
280
340
  return {
281
341
  title: out.title || page.id || ctx.project.name,
282
- head: html`${THEME_SCRIPT}
283
- ${links.map((href) => html`<link rel="stylesheet" href="${href}">`)}`,
284
- html: html`${nav}<main id="wb-main">${raw(out.html)}</main>`,
342
+ head: html`${THEME_SCRIPT} ${links.map((href) => html`<link rel="stylesheet" href="${href}" />`)}`,
343
+ html: html`${nav}
344
+ <main id="wb-main">${raw(out.html)}</main>`,
285
345
  data: {
286
346
  islands,
287
347
  styles,
@@ -0,0 +1,30 @@
1
+ import { html, raw } from "../html.js";
2
+ // src/pages/window.ts
3
+ const svg = (d) => raw(`<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round">${d}</svg>`);
4
+ const OPEN = svg("<path d=\"M9 2.5h4.5V7M13.5 2.5 7.5 8.5M11.5 9.5v3a1 1 0 0 1-1 1h-7a1 1 0 0 1-1-1v-7a1 1 0 0 1 1-1h3\"/>");
5
+ const MAXIMIZE = svg("<path d=\"M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10\"/>");
6
+ const RESTORE = svg("<path d=\"M6 2.5V6H2.5M13.5 6H10V2.5M10 13.5V10h3.5M2.5 10H6v3.5\"/>");
7
+ /** A window around a block's HTML: see the top of this file. */
8
+ function windowFrame({ body, label = null, open = null, wide = false, bare = false, bodyClass = "", height }) {
9
+ const name = label != null && String(label) !== "" ? html`<span class="wb-window-title">${raw(String(label))}</span>` : open ? html`<span class="wb-window-address">${open}</span>` : html``;
10
+ const header = !bare && html`<div class="wb-window-bar">
11
+ <span class="wb-window-name" ${open ? html` title="${open}"` : ""}>${name}</span>
12
+ ${open && html`<a class="wb-window-button" href="${open}" target="_blank" rel="noopener" title="Open on its own" aria-label="Open on its own">${OPEN}</a>`}
13
+ <button
14
+ type="button"
15
+ class="wb-window-button"
16
+ data-wb-maximize
17
+ title="Fill the window"
18
+ aria-label="Fill the window"
19
+ >
20
+ <span class="wb-window-max">${MAXIMIZE}</span><span class="wb-window-restore">${RESTORE}</span>
21
+ </button>
22
+ </div>`;
23
+ return html`<div class="wb-window${wide && raw(" wb-wide")}">
24
+ ${header}
25
+ <div class="wb-window-body${bodyClass && raw(" " + bodyClass)}" ${height ? html` style="height: ${height}"` : ""}>
26
+ ${raw(String(body))}
27
+ </div>
28
+ </div>`;
29
+ }
30
+ export { windowFrame };
@@ -0,0 +1,24 @@
1
+ import path from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ // src/public.ts
4
+ const asPath = (ref) => ref instanceof URL || ref.startsWith("file:") ? fileURLToPath(ref) : ref;
5
+ /** The folder a plugin's browser files are served from. */
6
+ function publicDirOf(plugin, wb) {
7
+ return path.resolve(plugin.public != null ? asPath(plugin.public) : path.join(wb, "public"));
8
+ }
9
+ /** Is `file` inside `dir`? */
10
+ const within = (file, dir) => {
11
+ const rel = path.relative(dir, file);
12
+ return !!rel && !rel.startsWith("..") && !path.isAbsolute(rel);
13
+ };
14
+ /**
15
+ * A file a plugin names, as a path: a name within its public folder (`style.css`,
16
+ * `client/main.js`), or a URL or absolute path, which must be inside it. Anything else is an
17
+ * error, said when the config loads, rather than a file that quietly isn't served.
18
+ */
19
+ function inPublic(dir, ref, what) {
20
+ const file = typeof ref === "string" && !ref.startsWith("file:") && !path.isAbsolute(ref) ? path.resolve(dir, ref) : path.resolve(asPath(ref));
21
+ if (!within(file, dir)) throw new Error(`${what}: ${file} is not in its public folder (${dir}); a plugin's browser files live there`);
22
+ return file;
23
+ }
24
+ export { inPublic, publicDirOf, within };