@bigmistqke/whiteboard 0.0.0-stage → 0.1.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 (138) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +609 -2
  3. package/bin/whiteboard.ts +431 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/whiteboard.js +471 -0
  6. package/dist/client/runtime.js +391 -0
  7. package/dist/define.js +7 -0
  8. package/dist/plugins/callout/README.md +13 -0
  9. package/dist/plugins/callout/index.js +27 -0
  10. package/dist/plugins/callout/style.css +12 -0
  11. package/dist/plugins/chat/README.md +45 -0
  12. package/dist/plugins/chat/client.js +3 -0
  13. package/dist/plugins/chat/index.js +46 -0
  14. package/dist/plugins/chat/style.css +17 -0
  15. package/dist/plugins/claude-code/index.js +120 -0
  16. package/dist/plugins/code/README.md +42 -0
  17. package/dist/plugins/code/diff.js +64 -0
  18. package/dist/plugins/code/index.js +77 -0
  19. package/dist/plugins/code/style.css +29 -0
  20. package/dist/plugins/comments/client.js +10 -0
  21. package/dist/plugins/comments/index.js +51 -0
  22. package/dist/plugins/comments/style.css +32 -0
  23. package/dist/plugins/details/README.md +17 -0
  24. package/dist/plugins/details/index.js +17 -0
  25. package/dist/plugins/details/style.css +3 -0
  26. package/dist/plugins/form/README.md +111 -0
  27. package/dist/plugins/form/index.js +102 -0
  28. package/dist/plugins/form/style.css +41 -0
  29. package/dist/plugins/gallery/README.md +30 -0
  30. package/dist/plugins/gallery/client.js +1 -0
  31. package/dist/plugins/gallery/index.js +74 -0
  32. package/dist/plugins/gallery/style.css +27 -0
  33. package/dist/plugins/grid/README.md +33 -0
  34. package/dist/plugins/grid/index.js +33 -0
  35. package/dist/plugins/grid/style.css +25 -0
  36. package/dist/plugins/index.js +26 -0
  37. package/dist/plugins/mermaid/README.md +21 -0
  38. package/dist/plugins/mermaid/index.js +34 -0
  39. package/dist/plugins/mermaid/style.css +16 -0
  40. package/dist/plugins/quiet/README.md +26 -0
  41. package/dist/plugins/quiet/index.js +17 -0
  42. package/dist/plugins/terminal/client.js +41 -0
  43. package/dist/plugins/terminal/host.js +332 -0
  44. package/dist/plugins/terminal/index.js +171 -0
  45. package/dist/plugins/terminal/modes.js +32 -0
  46. package/dist/plugins/terminal/server.js +256 -0
  47. package/dist/plugins/terminal/sizes.js +57 -0
  48. package/dist/plugins/terminal/style.css +357 -0
  49. package/dist/solid/index.js +35 -0
  50. package/dist/src/auth.js +54 -0
  51. package/dist/src/client/page/base.css +274 -0
  52. package/dist/src/client/page/main.js +1004 -0
  53. package/dist/src/config.js +76 -0
  54. package/dist/src/events.js +162 -0
  55. package/dist/src/html.js +34 -0
  56. package/dist/src/load.js +77 -0
  57. package/dist/src/loader.js +120 -0
  58. package/dist/src/package.js +33 -0
  59. package/dist/src/pages/blocks.js +49 -0
  60. package/dist/src/pages/index.js +14 -0
  61. package/dist/src/pages/markdown.js +307 -0
  62. package/dist/src/pages/render.js +257 -0
  63. package/dist/src/plugins.js +238 -0
  64. package/dist/src/running.js +24 -0
  65. package/dist/src/serve.js +71 -0
  66. package/dist/src/server.js +700 -0
  67. package/dist/src/status.js +43 -0
  68. package/package.json +131 -4
  69. package/plugins/callout/README.md +13 -0
  70. package/plugins/callout/index.ts +25 -0
  71. package/plugins/callout/style.css +12 -0
  72. package/plugins/chat/README.md +45 -0
  73. package/plugins/chat/autosize.ts +101 -0
  74. package/plugins/chat/client.tsx +81 -0
  75. package/plugins/chat/index.ts +61 -0
  76. package/plugins/chat/style.css +17 -0
  77. package/plugins/claude-code/index.ts +136 -0
  78. package/plugins/code/README.md +42 -0
  79. package/plugins/code/diff.ts +58 -0
  80. package/plugins/code/index.ts +94 -0
  81. package/plugins/code/style.css +29 -0
  82. package/plugins/comments/client.ts +448 -0
  83. package/plugins/comments/index.ts +52 -0
  84. package/plugins/comments/style.css +32 -0
  85. package/plugins/details/README.md +17 -0
  86. package/plugins/details/index.ts +21 -0
  87. package/plugins/details/style.css +3 -0
  88. package/plugins/form/README.md +111 -0
  89. package/plugins/form/index.ts +104 -0
  90. package/plugins/form/style.css +41 -0
  91. package/plugins/gallery/README.md +30 -0
  92. package/plugins/gallery/client.tsx +64 -0
  93. package/plugins/gallery/index.ts +67 -0
  94. package/plugins/gallery/style.css +27 -0
  95. package/plugins/grid/README.md +33 -0
  96. package/plugins/grid/index.ts +36 -0
  97. package/plugins/grid/style.css +25 -0
  98. package/plugins/index.ts +19 -0
  99. package/plugins/mermaid/README.md +21 -0
  100. package/plugins/mermaid/index.ts +39 -0
  101. package/plugins/mermaid/style.css +16 -0
  102. package/plugins/quiet/README.md +26 -0
  103. package/plugins/quiet/index.ts +27 -0
  104. package/plugins/terminal/client.ts +317 -0
  105. package/plugins/terminal/host.ts +326 -0
  106. package/plugins/terminal/index.ts +138 -0
  107. package/plugins/terminal/modes.ts +30 -0
  108. package/plugins/terminal/server.ts +215 -0
  109. package/plugins/terminal/sizes.ts +60 -0
  110. package/plugins/terminal/style.css +71 -0
  111. package/skills/whiteboard/SKILL.md +292 -0
  112. package/solid/index.tsx +56 -0
  113. package/src/auth.ts +66 -0
  114. package/src/client/forms.ts +186 -0
  115. package/src/client/page/base.css +274 -0
  116. package/src/client/page/fresh.ts +205 -0
  117. package/src/client/page/main.ts +249 -0
  118. package/src/client/page/orphans.ts +46 -0
  119. package/src/client/page/theme.ts +32 -0
  120. package/src/client/page/view.ts +9 -0
  121. package/src/client/page/waiting.ts +109 -0
  122. package/src/client/runtime.ts +170 -0
  123. package/src/config.ts +96 -0
  124. package/src/define.ts +546 -0
  125. package/src/events.ts +194 -0
  126. package/src/html.ts +40 -0
  127. package/src/load.ts +89 -0
  128. package/src/loader.ts +109 -0
  129. package/src/package.ts +39 -0
  130. package/src/pages/blocks.ts +67 -0
  131. package/src/pages/index.ts +36 -0
  132. package/src/pages/markdown.ts +276 -0
  133. package/src/pages/render.ts +196 -0
  134. package/src/plugins.ts +197 -0
  135. package/src/running.ts +22 -0
  136. package/src/serve.ts +52 -0
  137. package/src/server.ts +644 -0
  138. package/src/status.ts +69 -0
@@ -0,0 +1,1004 @@
1
+ // node_modules/morphdom/dist/morphdom-esm.js
2
+ var DOCUMENT_FRAGMENT_NODE = 11;
3
+ function morphAttrs(fromNode, toNode) {
4
+ var toNodeAttrs = toNode.attributes;
5
+ var attr;
6
+ var attrName;
7
+ var attrNamespaceURI;
8
+ var attrValue;
9
+ var fromValue;
10
+ if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE || fromNode.nodeType === DOCUMENT_FRAGMENT_NODE) return;
11
+ for (var i = toNodeAttrs.length - 1; i >= 0; i--) {
12
+ attr = toNodeAttrs[i];
13
+ attrName = attr.name;
14
+ attrNamespaceURI = attr.namespaceURI;
15
+ attrValue = attr.value;
16
+ if (attrNamespaceURI) {
17
+ attrName = attr.localName || attrName;
18
+ fromValue = fromNode.getAttributeNS(attrNamespaceURI, attrName);
19
+ if (fromValue !== attrValue) {
20
+ if (attr.prefix === "xmlns") attrName = attr.name;
21
+ fromNode.setAttributeNS(attrNamespaceURI, attrName, attrValue);
22
+ }
23
+ } else {
24
+ fromValue = fromNode.getAttribute(attrName);
25
+ if (fromValue !== attrValue) fromNode.setAttribute(attrName, attrValue);
26
+ }
27
+ }
28
+ var fromNodeAttrs = fromNode.attributes;
29
+ for (var d = fromNodeAttrs.length - 1; d >= 0; d--) {
30
+ attr = fromNodeAttrs[d];
31
+ attrName = attr.name;
32
+ attrNamespaceURI = attr.namespaceURI;
33
+ if (attrNamespaceURI) {
34
+ attrName = attr.localName || attrName;
35
+ if (!toNode.hasAttributeNS(attrNamespaceURI, attrName)) fromNode.removeAttributeNS(attrNamespaceURI, attrName);
36
+ } else if (!toNode.hasAttribute(attrName)) fromNode.removeAttribute(attrName);
37
+ }
38
+ }
39
+ var range;
40
+ var NS_XHTML = "http://www.w3.org/1999/xhtml";
41
+ var doc = typeof document === "undefined" ? void 0 : document;
42
+ var HAS_TEMPLATE_SUPPORT = !!doc && "content" in doc.createElement("template");
43
+ var HAS_RANGE_SUPPORT = !!doc && doc.createRange && "createContextualFragment" in doc.createRange();
44
+ function createFragmentFromTemplate(str) {
45
+ var template = doc.createElement("template");
46
+ template.innerHTML = str;
47
+ return template.content.childNodes[0];
48
+ }
49
+ function createFragmentFromRange(str) {
50
+ if (!range) {
51
+ range = doc.createRange();
52
+ range.selectNode(doc.body);
53
+ }
54
+ return range.createContextualFragment(str).childNodes[0];
55
+ }
56
+ function createFragmentFromWrap(str) {
57
+ var fragment = doc.createElement("body");
58
+ fragment.innerHTML = str;
59
+ return fragment.childNodes[0];
60
+ }
61
+ /**
62
+ * This is about the same
63
+ * var html = new DOMParser().parseFromString(str, 'text/html');
64
+ * return html.body.firstChild;
65
+ *
66
+ * @method toElement
67
+ * @param {String} str
68
+ */
69
+ function toElement(str) {
70
+ str = str.trim();
71
+ if (HAS_TEMPLATE_SUPPORT) return createFragmentFromTemplate(str);
72
+ else if (HAS_RANGE_SUPPORT) return createFragmentFromRange(str);
73
+ return createFragmentFromWrap(str);
74
+ }
75
+ /**
76
+ * Returns true if two node's names are the same.
77
+ *
78
+ * NOTE: We don't bother checking `namespaceURI` because you will never find two HTML elements with the same
79
+ * nodeName and different namespace URIs.
80
+ *
81
+ * @param {Element} a
82
+ * @param {Element} b The target element
83
+ * @return {boolean}
84
+ */
85
+ function compareNodeNames(fromEl, toEl) {
86
+ var fromNodeName = fromEl.nodeName;
87
+ var toNodeName = toEl.nodeName;
88
+ var fromCodeStart, toCodeStart;
89
+ if (fromNodeName === toNodeName) return true;
90
+ fromCodeStart = fromNodeName.charCodeAt(0);
91
+ toCodeStart = toNodeName.charCodeAt(0);
92
+ if (fromCodeStart <= 90 && toCodeStart >= 97) return fromNodeName === toNodeName.toUpperCase();
93
+ else if (toCodeStart <= 90 && fromCodeStart >= 97) return toNodeName === fromNodeName.toUpperCase();
94
+ else return false;
95
+ }
96
+ /**
97
+ * Create an element, optionally with a known namespace URI.
98
+ *
99
+ * @param {string} name the element name, e.g. 'div' or 'svg'
100
+ * @param {string} [namespaceURI] the element's namespace URI, i.e. the value of
101
+ * its `xmlns` attribute or its inferred namespace.
102
+ *
103
+ * @return {Element}
104
+ */
105
+ function createElementNS(name, namespaceURI) {
106
+ return !namespaceURI || namespaceURI === NS_XHTML ? doc.createElement(name) : doc.createElementNS(namespaceURI, name);
107
+ }
108
+ /**
109
+ * Copies the children of one DOM element to another DOM element
110
+ */
111
+ function moveChildren(fromEl, toEl) {
112
+ var curChild = fromEl.firstChild;
113
+ while (curChild) {
114
+ var nextChild = curChild.nextSibling;
115
+ toEl.appendChild(curChild);
116
+ curChild = nextChild;
117
+ }
118
+ return toEl;
119
+ }
120
+ function syncBooleanAttrProp(fromEl, toEl, name) {
121
+ if (fromEl[name] !== toEl[name]) {
122
+ fromEl[name] = toEl[name];
123
+ if (fromEl[name]) fromEl.setAttribute(name, "");
124
+ else fromEl.removeAttribute(name);
125
+ }
126
+ }
127
+ var specialElHandlers = {
128
+ OPTION: function(fromEl, toEl) {
129
+ var parentNode = fromEl.parentNode;
130
+ if (parentNode) {
131
+ var parentName = parentNode.nodeName.toUpperCase();
132
+ if (parentName === "OPTGROUP") {
133
+ parentNode = parentNode.parentNode;
134
+ parentName = parentNode && parentNode.nodeName.toUpperCase();
135
+ }
136
+ if (parentName === "SELECT" && !parentNode.hasAttribute("multiple")) {
137
+ if (fromEl.hasAttribute("selected") && !toEl.selected) {
138
+ fromEl.setAttribute("selected", "selected");
139
+ fromEl.removeAttribute("selected");
140
+ }
141
+ parentNode.selectedIndex = -1;
142
+ }
143
+ }
144
+ syncBooleanAttrProp(fromEl, toEl, "selected");
145
+ },
146
+ /**
147
+ * The "value" attribute is special for the <input> element since it sets
148
+ * the initial value. Changing the "value" attribute without changing the
149
+ * "value" property will have no effect since it is only used to the set the
150
+ * initial value. Similar for the "checked" attribute, and "disabled".
151
+ */
152
+ INPUT: function(fromEl, toEl) {
153
+ syncBooleanAttrProp(fromEl, toEl, "checked");
154
+ syncBooleanAttrProp(fromEl, toEl, "disabled");
155
+ if (fromEl.value !== toEl.value) fromEl.value = toEl.value;
156
+ if (!toEl.hasAttribute("value")) fromEl.removeAttribute("value");
157
+ },
158
+ TEXTAREA: function(fromEl, toEl) {
159
+ var newValue = toEl.value;
160
+ if (fromEl.value !== newValue) fromEl.value = newValue;
161
+ var firstChild = fromEl.firstChild;
162
+ if (firstChild) {
163
+ var oldValue = firstChild.nodeValue;
164
+ if (oldValue == newValue || !newValue && oldValue == fromEl.placeholder) return;
165
+ firstChild.nodeValue = newValue;
166
+ }
167
+ },
168
+ SELECT: function(fromEl, toEl) {
169
+ if (!toEl.hasAttribute("multiple")) {
170
+ var selectedIndex = -1;
171
+ var i = 0;
172
+ var curChild = fromEl.firstChild;
173
+ var optgroup;
174
+ var nodeName;
175
+ while (curChild) {
176
+ nodeName = curChild.nodeName && curChild.nodeName.toUpperCase();
177
+ if (nodeName === "OPTGROUP") {
178
+ optgroup = curChild;
179
+ curChild = optgroup.firstChild;
180
+ if (!curChild) {
181
+ curChild = optgroup.nextSibling;
182
+ optgroup = null;
183
+ }
184
+ } else {
185
+ if (nodeName === "OPTION") {
186
+ if (curChild.hasAttribute("selected")) {
187
+ selectedIndex = i;
188
+ break;
189
+ }
190
+ i++;
191
+ }
192
+ curChild = curChild.nextSibling;
193
+ if (!curChild && optgroup) {
194
+ curChild = optgroup.nextSibling;
195
+ optgroup = null;
196
+ }
197
+ }
198
+ }
199
+ fromEl.selectedIndex = selectedIndex;
200
+ }
201
+ }
202
+ };
203
+ var ELEMENT_NODE = 1;
204
+ var DOCUMENT_FRAGMENT_NODE$1 = 11;
205
+ var TEXT_NODE = 3;
206
+ var COMMENT_NODE = 8;
207
+ function noop() {}
208
+ function defaultGetNodeKey(node) {
209
+ if (node) return node.getAttribute && node.getAttribute("id") || node.id;
210
+ }
211
+ function morphdomFactory(morphAttrs) {
212
+ return function morphdom(fromNode, toNode, options) {
213
+ if (!options) options = {};
214
+ if (typeof toNode === "string") {
215
+ if (fromNode.nodeName === "#document" || fromNode.nodeName === "HTML" || fromNode.nodeName === "BODY") {
216
+ var toNodeHtml = toNode;
217
+ toNode = doc.createElement("html");
218
+ toNode.innerHTML = toNodeHtml;
219
+ } else toNode = toElement(toNode);
220
+ } else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE$1) toNode = toNode.firstElementChild;
221
+ var getNodeKey = options.getNodeKey || defaultGetNodeKey;
222
+ var onBeforeNodeAdded = options.onBeforeNodeAdded || noop;
223
+ var onNodeAdded = options.onNodeAdded || noop;
224
+ var onBeforeElUpdated = options.onBeforeElUpdated || noop;
225
+ var onElUpdated = options.onElUpdated || noop;
226
+ var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop;
227
+ var onNodeDiscarded = options.onNodeDiscarded || noop;
228
+ var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop;
229
+ var skipFromChildren = options.skipFromChildren || noop;
230
+ var addChild = options.addChild || function(parent, child) {
231
+ return parent.appendChild(child);
232
+ };
233
+ var childrenOnly = options.childrenOnly === true;
234
+ var fromNodesLookup = Object.create(null);
235
+ var keyedRemovalList = [];
236
+ function addKeyedRemoval(key) {
237
+ keyedRemovalList.push(key);
238
+ }
239
+ function walkDiscardedChildNodes(node, skipKeyedNodes) {
240
+ if (node.nodeType === ELEMENT_NODE) {
241
+ var curChild = node.firstChild;
242
+ while (curChild) {
243
+ var key = void 0;
244
+ if (skipKeyedNodes && (key = getNodeKey(curChild))) addKeyedRemoval(key);
245
+ else {
246
+ onNodeDiscarded(curChild);
247
+ if (curChild.firstChild) walkDiscardedChildNodes(curChild, skipKeyedNodes);
248
+ }
249
+ curChild = curChild.nextSibling;
250
+ }
251
+ }
252
+ }
253
+ /**
254
+ * Removes a DOM node out of the original DOM
255
+ *
256
+ * @param {Node} node The node to remove
257
+ * @param {Node} parentNode The nodes parent
258
+ * @param {Boolean} skipKeyedNodes If true then elements with keys will be skipped and not discarded.
259
+ * @return {undefined}
260
+ */
261
+ function removeNode(node, parentNode, skipKeyedNodes) {
262
+ if (onBeforeNodeDiscarded(node) === false) return;
263
+ if (parentNode) parentNode.removeChild(node);
264
+ onNodeDiscarded(node);
265
+ walkDiscardedChildNodes(node, skipKeyedNodes);
266
+ }
267
+ function indexTree(node) {
268
+ if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) {
269
+ var curChild = node.firstChild;
270
+ while (curChild) {
271
+ var key = getNodeKey(curChild);
272
+ if (key) fromNodesLookup[key] = curChild;
273
+ indexTree(curChild);
274
+ curChild = curChild.nextSibling;
275
+ }
276
+ }
277
+ }
278
+ indexTree(fromNode);
279
+ function handleNodeAdded(el) {
280
+ onNodeAdded(el);
281
+ var curChild = el.firstChild;
282
+ while (curChild) {
283
+ var nextSibling = curChild.nextSibling;
284
+ var key = getNodeKey(curChild);
285
+ if (key) {
286
+ var unmatchedFromEl = fromNodesLookup[key];
287
+ if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) {
288
+ curChild.parentNode.replaceChild(unmatchedFromEl, curChild);
289
+ morphEl(unmatchedFromEl, curChild);
290
+ } else handleNodeAdded(curChild);
291
+ } else handleNodeAdded(curChild);
292
+ curChild = nextSibling;
293
+ }
294
+ }
295
+ function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) {
296
+ while (curFromNodeChild) {
297
+ var fromNextSibling = curFromNodeChild.nextSibling;
298
+ if (curFromNodeKey = getNodeKey(curFromNodeChild)) addKeyedRemoval(curFromNodeKey);
299
+ else removeNode(curFromNodeChild, fromEl, true);
300
+ curFromNodeChild = fromNextSibling;
301
+ }
302
+ }
303
+ function morphEl(fromEl, toEl, childrenOnly) {
304
+ var toElKey = getNodeKey(toEl);
305
+ if (toElKey) delete fromNodesLookup[toElKey];
306
+ if (!childrenOnly) {
307
+ var beforeUpdateResult = onBeforeElUpdated(fromEl, toEl);
308
+ if (beforeUpdateResult === false) return;
309
+ else if (beforeUpdateResult instanceof HTMLElement) {
310
+ fromEl = beforeUpdateResult;
311
+ indexTree(fromEl);
312
+ }
313
+ morphAttrs(fromEl, toEl);
314
+ onElUpdated(fromEl);
315
+ if (onBeforeElChildrenUpdated(fromEl, toEl) === false) return;
316
+ }
317
+ if (fromEl.nodeName !== "TEXTAREA") morphChildren(fromEl, toEl);
318
+ else specialElHandlers.TEXTAREA(fromEl, toEl);
319
+ }
320
+ function morphChildren(fromEl, toEl) {
321
+ var skipFrom = skipFromChildren(fromEl, toEl);
322
+ var curToNodeChild = toEl.firstChild;
323
+ var curFromNodeChild = fromEl.firstChild;
324
+ var curToNodeKey;
325
+ var curFromNodeKey;
326
+ var fromNextSibling;
327
+ var toNextSibling;
328
+ var matchingFromEl;
329
+ outer: while (curToNodeChild) {
330
+ toNextSibling = curToNodeChild.nextSibling;
331
+ curToNodeKey = getNodeKey(curToNodeChild);
332
+ while (!skipFrom && curFromNodeChild) {
333
+ fromNextSibling = curFromNodeChild.nextSibling;
334
+ if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) {
335
+ curToNodeChild = toNextSibling;
336
+ curFromNodeChild = fromNextSibling;
337
+ continue outer;
338
+ }
339
+ curFromNodeKey = getNodeKey(curFromNodeChild);
340
+ var curFromNodeType = curFromNodeChild.nodeType;
341
+ var isCompatible = void 0;
342
+ if (curFromNodeType === curToNodeChild.nodeType) {
343
+ if (curFromNodeType === ELEMENT_NODE) {
344
+ if (curToNodeKey) {
345
+ if (curToNodeKey !== curFromNodeKey) {
346
+ if (matchingFromEl = fromNodesLookup[curToNodeKey]) {
347
+ if (fromNextSibling === matchingFromEl) isCompatible = false;
348
+ else {
349
+ fromEl.insertBefore(matchingFromEl, curFromNodeChild);
350
+ if (curFromNodeKey) addKeyedRemoval(curFromNodeKey);
351
+ else removeNode(curFromNodeChild, fromEl, true);
352
+ curFromNodeChild = matchingFromEl;
353
+ curFromNodeKey = getNodeKey(curFromNodeChild);
354
+ }
355
+ } else isCompatible = false;
356
+ }
357
+ } else if (curFromNodeKey) isCompatible = false;
358
+ isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild);
359
+ if (isCompatible) morphEl(curFromNodeChild, curToNodeChild);
360
+ } else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) {
361
+ isCompatible = true;
362
+ if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) curFromNodeChild.nodeValue = curToNodeChild.nodeValue;
363
+ }
364
+ }
365
+ if (isCompatible) {
366
+ curToNodeChild = toNextSibling;
367
+ curFromNodeChild = fromNextSibling;
368
+ continue outer;
369
+ }
370
+ if (curFromNodeKey) addKeyedRemoval(curFromNodeKey);
371
+ else removeNode(curFromNodeChild, fromEl, true);
372
+ curFromNodeChild = fromNextSibling;
373
+ }
374
+ if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) {
375
+ if (!skipFrom) addChild(fromEl, matchingFromEl);
376
+ morphEl(matchingFromEl, curToNodeChild);
377
+ } else {
378
+ var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild);
379
+ if (onBeforeNodeAddedResult !== false) {
380
+ if (onBeforeNodeAddedResult) curToNodeChild = onBeforeNodeAddedResult;
381
+ if (curToNodeChild.actualize) curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc);
382
+ addChild(fromEl, curToNodeChild);
383
+ handleNodeAdded(curToNodeChild);
384
+ }
385
+ }
386
+ curToNodeChild = toNextSibling;
387
+ curFromNodeChild = fromNextSibling;
388
+ }
389
+ cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey);
390
+ var specialElHandler = specialElHandlers[fromEl.nodeName];
391
+ if (specialElHandler) specialElHandler(fromEl, toEl);
392
+ }
393
+ var morphedNode = fromNode;
394
+ var morphedNodeType = morphedNode.nodeType;
395
+ var toNodeType = toNode.nodeType;
396
+ if (!childrenOnly) {
397
+ if (morphedNodeType === ELEMENT_NODE) {
398
+ if (toNodeType === ELEMENT_NODE) {
399
+ if (!compareNodeNames(fromNode, toNode)) {
400
+ onNodeDiscarded(fromNode);
401
+ morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI));
402
+ }
403
+ } else morphedNode = toNode;
404
+ } else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) {
405
+ if (toNodeType === morphedNodeType) {
406
+ if (morphedNode.nodeValue !== toNode.nodeValue) morphedNode.nodeValue = toNode.nodeValue;
407
+ return morphedNode;
408
+ } else morphedNode = toNode;
409
+ }
410
+ }
411
+ if (morphedNode === toNode) onNodeDiscarded(fromNode);
412
+ else {
413
+ if (toNode.isSameNode && toNode.isSameNode(morphedNode)) return;
414
+ morphEl(morphedNode, toNode, childrenOnly);
415
+ if (keyedRemovalList) for (var i = 0, len = keyedRemovalList.length; i < len; i++) {
416
+ var elToRemove = fromNodesLookup[keyedRemovalList[i]];
417
+ if (elToRemove) removeNode(elToRemove, elToRemove.parentNode, false);
418
+ }
419
+ }
420
+ if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) {
421
+ if (morphedNode.actualize) morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc);
422
+ fromNode.parentNode.replaceChild(morphedNode, fromNode);
423
+ }
424
+ return morphedNode;
425
+ };
426
+ }
427
+ var morphdom = morphdomFactory(morphAttrs);
428
+ // src/html.ts
429
+ /** A piece of HTML that is not escaped again. `String(piece)` is its markup. */
430
+ var Html = class {
431
+ value;
432
+ constructor(value) {
433
+ this.value = value;
434
+ }
435
+ toString() {
436
+ return this.value;
437
+ }
438
+ };
439
+ const ESCAPE = {
440
+ "&": "&amp;",
441
+ "<": "&lt;",
442
+ ">": "&gt;",
443
+ "\"": "&quot;",
444
+ "'": "&#39;"
445
+ };
446
+ /** Text → HTML that shows it as text. */
447
+ const escape = (value) => String(value).replace(/[&<>"']/g, (c) => ESCAPE[c]);
448
+ function part(value) {
449
+ if (value == null || value === false) return "";
450
+ if (value instanceof Html) return value.value;
451
+ if (Array.isArray(value)) return value.map(part).join("");
452
+ return escape(value);
453
+ }
454
+ function html(strings, ...values) {
455
+ let out = strings[0];
456
+ for (let i = 0; i < values.length; i++) out += part(values[i]) + strings[i + 1];
457
+ return new Html(out);
458
+ }
459
+ // src/client/page/view.ts
460
+ /** The bottom of the visible part of the page, in window coordinates. */
461
+ function viewBottom() {
462
+ const inset = parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--wb-inset-bottom")) || 0;
463
+ return Math.max(0, innerHeight - inset);
464
+ }
465
+ // src/client/page/fresh.ts
466
+ const DWELL_MS = 600;
467
+ const FADE_MS = 2500;
468
+ const sigOf = /* @__PURE__ */ new WeakMap();
469
+ const unseen = /* @__PURE__ */ new Set();
470
+ const timers = /* @__PURE__ */ new WeakMap();
471
+ let storeKey = "";
472
+ let core$1;
473
+ /**
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.
476
+ */
477
+ function signature(el) {
478
+ 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");
483
+ }
484
+ const s = clone.outerHTML;
485
+ let h = 2166136261;
486
+ for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619);
487
+ return (h >>> 0).toString(36);
488
+ }
489
+ /** Signatures of the top-level blocks in an HTML string, in order. */
490
+ function signaturesOf(html) {
491
+ const t = document.createElement("template");
492
+ t.innerHTML = html;
493
+ return [...t.content.children].map(signature);
494
+ }
495
+ function loadSeen() {
496
+ try {
497
+ const raw = localStorage.getItem(storeKey);
498
+ return raw ? new Set(JSON.parse(raw)) : null;
499
+ } catch {
500
+ return null;
501
+ }
502
+ }
503
+ function saveSeen(main) {
504
+ try {
505
+ const seen = [...main.children].filter((el) => !unseen.has(el)).map((el) => sigOf.get(el)).filter(Boolean);
506
+ localStorage.setItem(storeKey, JSON.stringify(seen));
507
+ } catch {}
508
+ }
509
+ let mainEl;
510
+ /** In view enough to count as read: half the block, or half the screen for blocks taller than that. */
511
+ function inView(el) {
512
+ const r = el.getBoundingClientRect();
513
+ const bottom = viewBottom();
514
+ const overlap = Math.min(r.bottom, bottom) - Math.max(r.top, 0);
515
+ return overlap > 0 && overlap >= Math.min(r.height, bottom) * .5;
516
+ }
517
+ /** Start the dwell timer of unseen blocks in view; stop it for blocks that left the view. */
518
+ function check() {
519
+ const active = document.visibilityState === "visible";
520
+ for (const el of unseen) {
521
+ if (!el.isConnected) {
522
+ unseen.delete(el);
523
+ continue;
524
+ }
525
+ const pending = timers.has(el);
526
+ if (active && inView(el)) {
527
+ if (!pending) timers.set(el, window.setTimeout(() => {
528
+ timers.delete(el);
529
+ markSeen(el);
530
+ }, DWELL_MS));
531
+ } else if (pending) {
532
+ clearTimeout(timers.get(el));
533
+ timers.delete(el);
534
+ }
535
+ }
536
+ }
537
+ let checkFrame = 0;
538
+ const scheduleCheck = () => {
539
+ cancelAnimationFrame(checkFrame);
540
+ checkFrame = requestAnimationFrame(check);
541
+ };
542
+ addEventListener("scroll", scheduleCheck, { passive: true });
543
+ addEventListener("resize", scheduleCheck);
544
+ document.addEventListener("visibilitychange", scheduleCheck);
545
+ setInterval(() => {
546
+ if (unseen.size) check();
547
+ }, 500);
548
+ function markSeen(el) {
549
+ if (!unseen.delete(el)) return;
550
+ el.classList.add("wb-new-seen");
551
+ setTimeout(() => el.classList.remove("wb-new", "wb-new-seen"), FADE_MS);
552
+ saveSeen(mainEl);
553
+ update$1();
554
+ }
555
+ function markNew(el) {
556
+ if (el.matches("[data-wb-quiet]")) return;
557
+ el.classList.remove("wb-new-seen");
558
+ el.classList.add("wb-new");
559
+ unseen.add(el);
560
+ scheduleCheck();
561
+ }
562
+ const nav = document.createElement("div");
563
+ nav.className = "wb-fresh";
564
+ nav.innerHTML = String(html`${["up", "down"].map((dir) => html`<button type="button" data-dir="${dir}"><span class="wb-fresh-label"></span><span class="wb-fresh-close" title="Mark everything new as seen" aria-label="Mark everything new as seen">✕</span></button>`)}`);
565
+ const [upBtn, downBtn] = nav.querySelectorAll("button");
566
+ nav.hidden = upBtn.hidden = downBtn.hidden = true;
567
+ function split() {
568
+ const above = [], below = [];
569
+ for (const el of unseen) {
570
+ if (!el.isConnected) {
571
+ unseen.delete(el);
572
+ continue;
573
+ }
574
+ const r = el.getBoundingClientRect();
575
+ if (r.bottom < 60) above.push(el);
576
+ else if (r.top > viewBottom()) below.push(el);
577
+ }
578
+ const top = (el) => el.getBoundingClientRect().top;
579
+ above.sort((a, b) => top(b) - top(a));
580
+ below.sort((a, b) => top(a) - top(b));
581
+ return {
582
+ above,
583
+ below
584
+ };
585
+ }
586
+ let raf = 0;
587
+ function update$1() {
588
+ cancelAnimationFrame(raf);
589
+ raf = requestAnimationFrame(() => {
590
+ const { above, below } = split();
591
+ upBtn.hidden = !above.length;
592
+ downBtn.hidden = !below.length;
593
+ upBtn.querySelector(".wb-fresh-label").textContent = `↑ new (${above.length})`;
594
+ downBtn.querySelector(".wb-fresh-label").textContent = `↓ new (${below.length})`;
595
+ nav.hidden = !above.length && !below.length;
596
+ core$1?.badge("new", unseen.size ? String(unseen.size) : null);
597
+ });
598
+ }
599
+ nav.addEventListener("click", (e) => {
600
+ const dir = e.target.closest("button")?.dataset.dir;
601
+ if (!dir) return;
602
+ if (e.target.closest(".wb-fresh-close")) {
603
+ for (const el of [...unseen]) markSeen(el);
604
+ return;
605
+ }
606
+ const { above, below } = split();
607
+ (dir === "up" ? above : below)[0]?.scrollIntoView({
608
+ behavior: "smooth",
609
+ block: "start"
610
+ });
611
+ });
612
+ addEventListener("scroll", update$1, { passive: true });
613
+ addEventListener("resize", update$1);
614
+ /** Below this share of blocks recognised from the last visit, the page counts as new to the reader. */
615
+ const RECOGNISED_MIN = .25;
616
+ /**
617
+ * First render: blocks the reader has not seen on a previous visit are new. On a first visit, or
618
+ * when so little is recognised that the page is effectively a different one (a rewrite, say),
619
+ * nothing is marked: marking every block tells the reader nothing.
620
+ */
621
+ function trackInitial(main, page, api) {
622
+ core$1 = api;
623
+ api.corner().append(nav);
624
+ mainEl = main;
625
+ storeKey = `wb-seen:${page}`;
626
+ const blocks = [...main.children];
627
+ const sigs = blocks.map(signature);
628
+ blocks.forEach((el, i) => sigOf.set(el, sigs[i]));
629
+ const seen = loadSeen();
630
+ const recognised = seen ? sigs.filter((s) => seen.has(s)).length : 0;
631
+ if (seen && recognised >= blocks.length * RECOGNISED_MIN) blocks.forEach((el, i) => {
632
+ if (!seen.has(sigs[i])) markNew(el);
633
+ });
634
+ saveSeen(main);
635
+ update$1();
636
+ }
637
+ /** After a live update: blocks whose content was not on the page before are new. */
638
+ function trackUpdate(main, previous, next) {
639
+ const before = /* @__PURE__ */ new Map();
640
+ for (const s of previous) before.set(s, (before.get(s) ?? 0) + 1);
641
+ [...main.children].forEach((el, i) => {
642
+ const sig = next[i];
643
+ sigOf.set(el, sig);
644
+ const left = before.get(sig) ?? 0;
645
+ if (left > 0) before.set(sig, left - 1);
646
+ else markNew(el);
647
+ });
648
+ for (const el of [...unseen]) if (!el.isConnected) unseen.delete(el);
649
+ saveSeen(main);
650
+ update$1();
651
+ }
652
+ // src/client/page/theme.ts
653
+ const media = matchMedia("(prefers-color-scheme: dark)");
654
+ const listeners = /* @__PURE__ */ new Set();
655
+ const resolve = () => document.documentElement.dataset.theme ?? (media.matches ? "dark" : "light");
656
+ let theme = resolve();
657
+ function update() {
658
+ const next = resolve();
659
+ if (next === theme) return;
660
+ theme = next;
661
+ for (const fn of listeners) fn(theme);
662
+ }
663
+ media.addEventListener("change", update);
664
+ const currentTheme = () => theme;
665
+ function onTheme(fn) {
666
+ listeners.add(fn);
667
+ return () => listeners.delete(fn);
668
+ }
669
+ const themePreference = () => document.documentElement.dataset.theme ?? "auto";
670
+ function setThemePreference(pref) {
671
+ if (pref === "auto") delete document.documentElement.dataset.theme;
672
+ else document.documentElement.dataset.theme = pref;
673
+ try {
674
+ if (pref === "auto") localStorage.removeItem("wb-theme");
675
+ else localStorage.setItem("wb-theme", pref);
676
+ } catch {}
677
+ update();
678
+ }
679
+ // src/client/page/waiting.ts
680
+ const ORDER = [
681
+ "waiting",
682
+ "unheard",
683
+ "pending",
684
+ "decided"
685
+ ];
686
+ const LABEL = {
687
+ waiting: (n) => `${n} waiting for you`,
688
+ unheard: (n) => `${n} nobody listening`,
689
+ pending: (n) => `${n} waiting for the agent`,
690
+ decided: (n) => `${n} decided`
691
+ };
692
+ let main$1;
693
+ let api$1;
694
+ let forms = [];
695
+ const button = document.createElement("button");
696
+ button.type = "button";
697
+ button.className = "wb-waiting-next";
698
+ button.hidden = true;
699
+ const summary = document.createElement("div");
700
+ summary.className = "wb-summary";
701
+ summary.hidden = true;
702
+ const reopened = /* @__PURE__ */ new Map();
703
+ const inState = (state) => forms.filter((f) => f.state === state && f.form.isConnected).map((f) => f.form);
704
+ function show(list) {
705
+ forms = list;
706
+ for (const { form, name, state, last } of forms) {
707
+ if (last && reopened.has(name) && reopened.get(name) !== last.id && state === "decided") reopened.delete(name);
708
+ form.toggleAttribute("data-wb-folded", state === "decided" && !reopened.has(name));
709
+ }
710
+ const parts = ORDER.map((state) => [state, inState(state).length]).filter(([, count]) => count);
711
+ summary.hidden = !parts.length;
712
+ summary.replaceChildren(...parts.flatMap(([state, count], i) => {
713
+ const link = document.createElement("button");
714
+ link.type = "button";
715
+ link.dataset.state = state;
716
+ link.textContent = LABEL[state](count);
717
+ return i ? [document.createTextNode(" · "), link] : [link];
718
+ }));
719
+ const n = inState("waiting").length;
720
+ button.hidden = n === 0;
721
+ button.textContent = `→ waiting for you (${n})`;
722
+ api$1.badge("waiting", n ? `!${n}` : null, -1);
723
+ }
724
+ let current = null;
725
+ let scrolledTo = 0;
726
+ function next(state) {
727
+ const list = inState(state);
728
+ const i = current ? list.indexOf(current) : -1;
729
+ if (i >= 0 && Math.abs(scrollY - scrolledTo) < 40) return list[(i + 1) % list.length];
730
+ return list.find((f) => f.getBoundingClientRect().top > viewBottom() / 2) ?? list[0];
731
+ }
732
+ function jump(state) {
733
+ const form = next(state);
734
+ if (!form) return;
735
+ current = form;
736
+ const r = form.getBoundingClientRect();
737
+ scrolledTo = Math.max(0, Math.min(scrollY + r.top + r.height / 2 - viewBottom() / 2, document.documentElement.scrollHeight - innerHeight));
738
+ scrollTo({
739
+ top: scrolledTo,
740
+ behavior: "smooth"
741
+ });
742
+ if (state === "waiting") form.querySelector("input:not([type=hidden]), textarea, select, button")?.focus({ preventScroll: true });
743
+ }
744
+ function trackWaiting(root, core) {
745
+ main$1 = root;
746
+ api$1 = core;
747
+ api$1.corner().prepend(button);
748
+ button.addEventListener("click", () => jump("waiting"));
749
+ main$1.before(summary);
750
+ summary.addEventListener("click", (e) => {
751
+ const state = e.target.closest("button[data-state]")?.dataset.state;
752
+ if (state) jump(state);
753
+ });
754
+ main$1.addEventListener("click", (e) => {
755
+ const target = e.target;
756
+ const form = target.closest("form[data-wb-folded]");
757
+ if (!form || target.closest("[data-block=chat-thread]")) return;
758
+ e.preventDefault();
759
+ const name = form.getAttribute("name");
760
+ const last = api$1.last(name);
761
+ if (last) reopened.set(name, last.id);
762
+ show(forms);
763
+ form.querySelector("input:not([type=hidden]), textarea, select")?.focus({ preventScroll: true });
764
+ });
765
+ api$1.onForms(show);
766
+ show(api$1.forms());
767
+ }
768
+ // src/client/page/orphans.ts
769
+ /** A message in a chat: the chat block's event. */
770
+ const isMessage = (e) => e.type === "chat:message" && typeof e.data.chat === "string";
771
+ function trackOrphanThreads(main, api) {
772
+ const box = document.createElement("section");
773
+ box.className = "wb-orphans";
774
+ box.hidden = true;
775
+ main.after(box);
776
+ function draw() {
777
+ const onPage = /* @__PURE__ */ new Set([...[...main.querySelectorAll("form[name]")].map((f) => f.getAttribute("name")), ...[...main.querySelectorAll("[data-chat]")].map((c) => c.getAttribute("data-chat"))]);
778
+ const threads = /* @__PURE__ */ new Map();
779
+ for (const e of api.events()) {
780
+ const chat = String(e.data.chat);
781
+ if (!isMessage(e) || onPage.has(chat)) continue;
782
+ threads.set(chat, [...threads.get(chat) ?? [], e]);
783
+ }
784
+ box.hidden = threads.size === 0;
785
+ if (box.hidden) return box.replaceChildren();
786
+ box.innerHTML = String(html`<h2>Chats no longer on this page</h2>
787
+ ${[...threads].map(([chat, messages]) => {
788
+ const replaced = new Set(messages.map((m) => m.data.replaces).filter(Boolean).map(String));
789
+ return html`<div class="wb-orphan" data-chat="${chat}"><h3>${chat}</h3>${messages.filter((m) => !replaced.has(m.id)).map((m) => {
790
+ const who = m.from === "cli" ? "agent" : "you";
791
+ 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>`;
792
+ })}</div>`;
793
+ })}`);
794
+ }
795
+ api.onEvents(draw);
796
+ api.onRendered(draw);
797
+ draw();
798
+ }
799
+ // src/client/page/main.ts
800
+ let main;
801
+ let page = "";
802
+ let api;
803
+ let islands = {};
804
+ const disposers = /* @__PURE__ */ new Map();
805
+ async function mountOne(el) {
806
+ const island = islands[el.dataset.wbIsland];
807
+ el.dataset.wbMounted = "";
808
+ if (!island) return;
809
+ try {
810
+ const mod = await import(island.client);
811
+ if (!el.isConnected) return;
812
+ const mount = mod.default;
813
+ if (typeof mount !== "function") throw new Error("the client module must export a mount(element, props, api) function as default");
814
+ el.replaceChildren();
815
+ const dispose = await mount(el, island.props, api);
816
+ if (typeof dispose === "function") disposers.set(el, dispose);
817
+ } catch (err) {
818
+ console.error(`island ${island.block}:`, err);
819
+ el.insertAdjacentHTML("afterbegin", String(html`<pre class="wb-error">block "${island.block}" failed to mount: ${err.message}</pre>`));
820
+ }
821
+ }
822
+ /** Mount islands outermost first; nested islands show up once their parent has rendered. */
823
+ async function mountIslands(root = main) {
824
+ for (;;) {
825
+ const pending = [...root.querySelectorAll("[data-wb-island]:not([data-wb-mounted])")].filter((el) => !el.parentElement?.closest("[data-wb-island]:not([data-wb-mounted])"));
826
+ if (!pending.length) return;
827
+ await Promise.all(pending.map(mountOne));
828
+ }
829
+ }
830
+ function disposeWithin(node) {
831
+ for (const [el, dispose] of disposers) if (node === el || node.contains(el)) {
832
+ dispose();
833
+ disposers.delete(el);
834
+ }
835
+ }
836
+ const NEXT = {
837
+ auto: "light",
838
+ light: "dark",
839
+ dark: "auto"
840
+ };
841
+ function themeToggle(root) {
842
+ const button = root.querySelector(".wb-theme");
843
+ if (!button) return;
844
+ button.textContent = themePreference();
845
+ button.addEventListener("click", () => {
846
+ setThemePreference(NEXT[themePreference()]);
847
+ button.textContent = themePreference();
848
+ });
849
+ }
850
+ function ago(ts) {
851
+ const s = Math.round((Date.now() - ts) / 1e3);
852
+ if (s < 10) return "just now";
853
+ if (s < 60) return `${s}s ago`;
854
+ if (s < 3600) return `${Math.round(s / 60)}m ago`;
855
+ if (s < 86400) return `${Math.round(s / 3600)}h ago`;
856
+ return new Date(ts).toLocaleDateString();
857
+ }
858
+ function tickTimes() {
859
+ for (const t of document.querySelectorAll("time[data-ts]")) {
860
+ const ts = Number(t.dataset.ts);
861
+ t.textContent = ago(ts);
862
+ t.title = new Date(ts).toLocaleString();
863
+ }
864
+ }
865
+ let blockSigs = [];
866
+ const FIELD = (n) => n instanceof HTMLInputElement || n instanceof HTMLTextAreaElement || n instanceof HTMLSelectElement;
867
+ const KEEP_CLASSES = ["wb-new", "wb-new-seen"];
868
+ /** Patch the page in place: focus, caret, typed values, open <details> and unchanged islands survive. */
869
+ /**
870
+ * A <details> element's identity across renders: its summary, the nearest heading above its
871
+ * top-level block, and how many before it share both. Blocks are matched by position when the page
872
+ * updates, so a block inserted above an open section would otherwise pair it with a different
873
+ * element and close it; this finds the same section wherever it moved.
874
+ */
875
+ function detailsKeys(root) {
876
+ const keys = /* @__PURE__ */ new Map();
877
+ const seen = /* @__PURE__ */ new Map();
878
+ for (const el of root.querySelectorAll("details")) {
879
+ let block = el;
880
+ while (block.parentElement && block.parentElement !== root) block = block.parentElement;
881
+ let heading = "";
882
+ for (let b = block; b; b = b.previousElementSibling) if (/^H[1-6]$/.test(b.tagName)) {
883
+ heading = b.textContent?.trim() ?? "";
884
+ break;
885
+ }
886
+ const base = `${heading}\u0000${el.querySelector(":scope > summary")?.textContent?.trim() ?? ""}`;
887
+ const n = seen.get(base) ?? 0;
888
+ seen.set(base, n + 1);
889
+ keys.set(el, `${base}\u0000${n}`);
890
+ }
891
+ return keys;
892
+ }
893
+ function morph(html) {
894
+ const open = /* @__PURE__ */ new Set();
895
+ for (const [el, key] of detailsKeys(main)) if (el.open) open.add(key);
896
+ const next = main.cloneNode(false);
897
+ next.innerHTML = html;
898
+ for (const [el, key] of detailsKeys(next)) el.open = open.has(key);
899
+ morphdom(main, next, {
900
+ getNodeKey: (n) => n.dataset?.wbKey || n.id || void 0,
901
+ onBeforeElUpdated(from, to) {
902
+ if (from.dataset.wbKey) {
903
+ if (from.dataset.wbKey === to.dataset.wbKey && "wbMounted" in from.dataset) {
904
+ from.dataset.wbIsland = to.dataset.wbIsland;
905
+ return false;
906
+ }
907
+ disposeWithin(from);
908
+ }
909
+ if (FIELD(from)) {
910
+ if (from === document.activeElement) return false;
911
+ to.value = from.value;
912
+ if (from instanceof HTMLInputElement) to.checked = from.checked;
913
+ }
914
+ for (const { name, value } of [...from.attributes]) if (name.startsWith("data-wb-") && name !== "data-wb-mounted" && !to.hasAttribute(name)) to.setAttribute(name, value);
915
+ for (const c of KEEP_CLASSES) if (from.classList.contains(c)) to.classList.add(c);
916
+ return true;
917
+ },
918
+ onBeforeNodeDiscarded(node) {
919
+ disposeWithin(node);
920
+ return true;
921
+ }
922
+ });
923
+ }
924
+ document.documentElement.style.overflowAnchor = "none";
925
+ /**
926
+ * The reader's place before an update: the first block in view that the update keeps (by
927
+ * signature, and which of the blocks with that signature it is), and where it is on screen.
928
+ */
929
+ function placeOf(nextSigs) {
930
+ const keep = new Set(nextSigs);
931
+ const blocks = [...main.children];
932
+ for (let i = 0; i < blocks.length; i++) {
933
+ const sig = blockSigs[i];
934
+ if (!keep.has(sig) || blocks[i].getBoundingClientRect().bottom <= 0) continue;
935
+ return {
936
+ sig,
937
+ nth: blockSigs.slice(0, i).filter((s) => s === sig).length,
938
+ top: blocks[i].getBoundingClientRect().top
939
+ };
940
+ }
941
+ return null;
942
+ }
943
+ /** Scroll so the block of `place` is where it was on screen. */
944
+ function keepPlace(place, sigs) {
945
+ if (!place) return;
946
+ let seen = 0;
947
+ const i = sigs.findIndex((s) => s === place.sig && seen++ === place.nth);
948
+ const el = main.children[i];
949
+ if (el) window.scrollBy(0, el.getBoundingClientRect().top - place.top);
950
+ }
951
+ /** The new render of the page, patched in. */
952
+ async function refresh() {
953
+ const next = await currentPage.render();
954
+ const data = next.data;
955
+ islands = data.islands;
956
+ 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}">`));
957
+ const content = new DOMParser().parseFromString(next.html, "text/html").getElementById("wb-main")?.innerHTML ?? "";
958
+ const nextSigs = signaturesOf(content);
959
+ const place = placeOf(nextSigs);
960
+ morph(content);
961
+ keepPlace(place, nextSigs);
962
+ const updated = document.getElementById("wb-updated");
963
+ if (updated && data.mtime) updated.dataset.ts = String(data.mtime);
964
+ if (next.title) core.title(next.title);
965
+ trackUpdate(main, blockSigs, nextSigs);
966
+ blockSigs = nextSigs;
967
+ await mountIslands();
968
+ keepPlace(place, nextSigs);
969
+ tickTimes();
970
+ api.rendered();
971
+ }
972
+ let currentPage;
973
+ let core;
974
+ const mount = async (root, handle, coreApi) => {
975
+ currentPage = handle;
976
+ core = coreApi;
977
+ page = handle.id;
978
+ main = root.querySelector("#wb-main");
979
+ islands = handle.data.islands;
980
+ api = {
981
+ ...core,
982
+ theme: currentTheme,
983
+ onTheme
984
+ };
985
+ const isDir = handle.kind === "pages" || handle.kind === "folder";
986
+ themeToggle(root);
987
+ setInterval(tickTimes, 15e3);
988
+ new MutationObserver(() => {
989
+ if (main.querySelector("[data-wb-island]:not([data-wb-mounted])")) mountIslands();
990
+ }).observe(main, {
991
+ childList: true,
992
+ subtree: true
993
+ });
994
+ handle.onChange(() => refresh());
995
+ if (isDir) core.onEvents(() => refresh());
996
+ if (isDir) core.onListeners(() => refresh());
997
+ blockSigs = [...main.children].map(signature);
998
+ if (!isDir) trackInitial(main, page, core);
999
+ if (!isDir) trackWaiting(main, core);
1000
+ if (!isDir) trackOrphanThreads(main, core);
1001
+ await mountIslands();
1002
+ tickTimes();
1003
+ };
1004
+ export { mount as default };