@bigmistqke/whiteboard 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +628 -2
- package/bin/whiteboard.ts +488 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +542 -0
- package/dist/client/runtime.js +544 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +46 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +47 -0
- package/dist/plugins/chat/style.css +24 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +65 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +54 -0
- package/dist/plugins/comments/style.css +40 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/forge/README.md +12 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/client.js +1 -0
- package/dist/plugins/iframe/index.js +40 -0
- package/dist/plugins/iframe/style.css +29 -0
- package/dist/plugins/index.js +30 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +35 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +36 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +295 -0
- package/dist/src/client/page/main.js +1228 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +183 -0
- package/dist/src/html.js +34 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +126 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +50 -0
- package/dist/src/pages/index.js +15 -0
- package/dist/src/pages/markdown.js +410 -0
- package/dist/src/pages/render.js +311 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +725 -0
- package/dist/src/status.js +71 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +46 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +93 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +24 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +64 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +492 -0
- package/plugins/comments/index.ts +54 -0
- package/plugins/comments/style.css +40 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/forge/README.md +12 -0
- package/plugins/forge/index.ts +71 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/client.ts +46 -0
- package/plugins/iframe/index.ts +54 -0
- package/plugins/iframe/style.css +29 -0
- package/plugins/index.ts +21 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +40 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +302 -0
- package/solid/index.tsx +59 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +185 -0
- package/src/client/markdown.ts +36 -0
- package/src/client/page/base.css +295 -0
- package/src/client/page/elsewhere.ts +70 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/live.ts +30 -0
- package/src/client/page/main.ts +272 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/sort.ts +38 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +182 -0
- package/src/client/read.ts +73 -0
- package/src/client/runtime.ts +183 -0
- package/src/client/toast.ts +25 -0
- package/src/config.ts +96 -0
- package/src/define.ts +612 -0
- package/src/events.ts +218 -0
- package/src/html.ts +40 -0
- package/src/icons.ts +8 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +112 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +70 -0
- package/src/pages/index.ts +42 -0
- package/src/pages/markdown.ts +364 -0
- package/src/pages/render.ts +233 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +667 -0
- package/src/status.ts +113 -0
|
@@ -0,0 +1,1228 @@
|
|
|
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
|
+
"&": "&",
|
|
441
|
+
"<": "<",
|
|
442
|
+
">": ">",
|
|
443
|
+
"\"": """,
|
|
444
|
+
"'": "'"
|
|
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/status.ts
|
|
680
|
+
/** The states, in the order a reader should see them: what waits on them first. */
|
|
681
|
+
const STATES = [
|
|
682
|
+
"open",
|
|
683
|
+
"pending",
|
|
684
|
+
"decided"
|
|
685
|
+
];
|
|
686
|
+
/** How many things on a page wait for the reader: its open questions and unread messages. */
|
|
687
|
+
const awaiting = (p) => p.open.length + p.unread.length;
|
|
688
|
+
// src/icons.ts
|
|
689
|
+
/** An inbox: what awaits the reader (the header's menu, the overview). */
|
|
690
|
+
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>";
|
|
691
|
+
/** "Awaiting you: 2 things", and where: what the inbox says to a reader who hovers it. */
|
|
692
|
+
const awaitingLabel = (n, where = "") => `Awaiting you: ${n === 1 ? "1 question or message" : `${n} questions and messages`}${where}`;
|
|
693
|
+
// src/client/page/elsewhere.ts
|
|
694
|
+
/** A link to a page that scrolls it to its first waiting question. */
|
|
695
|
+
const WAITING_HASH = "#wb-waiting";
|
|
696
|
+
const box = document.createElement("div");
|
|
697
|
+
box.className = "wb-elsewhere";
|
|
698
|
+
box.hidden = true;
|
|
699
|
+
box.innerHTML = "<button type=\"button\" class=\"wb-elsewhere-toggle wb-awaiting\" aria-haspopup=\"true\" aria-expanded=\"false\"></button><ul class=\"wb-elsewhere-list\" hidden></ul>";
|
|
700
|
+
const toggle = box.querySelector("button");
|
|
701
|
+
toggle.innerHTML = `${INBOX_ICON}<span class="wb-elsewhere-count"></span>`;
|
|
702
|
+
const count = toggle.querySelector("span");
|
|
703
|
+
const list = box.querySelector("ul");
|
|
704
|
+
function open(yes) {
|
|
705
|
+
list.hidden = !yes;
|
|
706
|
+
toggle.setAttribute("aria-expanded", String(yes));
|
|
707
|
+
}
|
|
708
|
+
let current$1 = "";
|
|
709
|
+
async function load() {
|
|
710
|
+
let pages;
|
|
711
|
+
try {
|
|
712
|
+
pages = await (await fetch("/api/status", { cache: "no-store" })).json();
|
|
713
|
+
} catch {
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
const waiting = pages.filter((p) => p.page !== current$1).map((p) => ({
|
|
717
|
+
p,
|
|
718
|
+
n: awaiting(p)
|
|
719
|
+
})).filter(({ n }) => n > 0);
|
|
720
|
+
const total = waiting.reduce((sum, { n }) => sum + n, 0);
|
|
721
|
+
box.hidden = total === 0;
|
|
722
|
+
if (!total) open(false);
|
|
723
|
+
count.textContent = String(total);
|
|
724
|
+
toggle.title = awaitingLabel(total, ` on ${waiting.length === 1 ? "another page" : `${waiting.length} other pages`}`);
|
|
725
|
+
toggle.setAttribute("aria-label", toggle.title);
|
|
726
|
+
list.replaceChildren(...waiting.map(({ p, n }) => {
|
|
727
|
+
const li = document.createElement("li");
|
|
728
|
+
const a = document.createElement("a");
|
|
729
|
+
a.href = `/${p.page.split("/").map(encodeURIComponent).join("/")}${WAITING_HASH}`;
|
|
730
|
+
a.textContent = `${p.title} (${n})`;
|
|
731
|
+
li.append(a);
|
|
732
|
+
return li;
|
|
733
|
+
}));
|
|
734
|
+
}
|
|
735
|
+
let timer = 0;
|
|
736
|
+
const reload = () => {
|
|
737
|
+
clearTimeout(timer);
|
|
738
|
+
timer = window.setTimeout(load, 100);
|
|
739
|
+
};
|
|
740
|
+
function trackElsewhere(root, handle, core) {
|
|
741
|
+
current$1 = handle.id;
|
|
742
|
+
const nav = root.querySelector(".wb-nav");
|
|
743
|
+
if (!nav) return;
|
|
744
|
+
nav.querySelector(".wb-theme")?.before(box);
|
|
745
|
+
toggle.addEventListener("click", () => open(!!list.hidden));
|
|
746
|
+
document.addEventListener("click", (e) => {
|
|
747
|
+
if (!box.contains(e.target)) open(false);
|
|
748
|
+
});
|
|
749
|
+
document.addEventListener("keydown", (e) => {
|
|
750
|
+
if (e.key === "Escape" && !list.hidden) {
|
|
751
|
+
open(false);
|
|
752
|
+
toggle.focus();
|
|
753
|
+
}
|
|
754
|
+
});
|
|
755
|
+
core.onEvents(reload);
|
|
756
|
+
core.onListeners(reload);
|
|
757
|
+
handle.onChange(reload);
|
|
758
|
+
load();
|
|
759
|
+
}
|
|
760
|
+
// src/client/page/waiting.ts
|
|
761
|
+
const ORDER = STATES;
|
|
762
|
+
const LABEL = {
|
|
763
|
+
open: (n) => `${n} waiting for you`,
|
|
764
|
+
pending: (n) => `${n} waiting for the agent`,
|
|
765
|
+
decided: (n) => `${n} decided`
|
|
766
|
+
};
|
|
767
|
+
const messagesLabel = (n) => `${n} unread message${n === 1 ? "" : "s"} from the agent`;
|
|
768
|
+
let main$1;
|
|
769
|
+
let api$1;
|
|
770
|
+
let forms = [];
|
|
771
|
+
const button = document.createElement("button");
|
|
772
|
+
button.type = "button";
|
|
773
|
+
button.className = "wb-waiting-next";
|
|
774
|
+
button.hidden = true;
|
|
775
|
+
const summary = document.createElement("div");
|
|
776
|
+
summary.className = "wb-summary";
|
|
777
|
+
summary.hidden = true;
|
|
778
|
+
const reopened = /* @__PURE__ */ new Map();
|
|
779
|
+
const inState = (state) => forms.filter((f) => f.state === state && f.form.isConnected).map((f) => f.form);
|
|
780
|
+
/** Where an unread message is on the page: its own element, else the one holding it. */
|
|
781
|
+
function messageTarget(id) {
|
|
782
|
+
const own = document.querySelector(`[data-wb-message="${CSS.escape(id)}"]`);
|
|
783
|
+
if (own && own.getClientRects().length) return own;
|
|
784
|
+
return [...document.querySelectorAll("[data-wb-holds]")].find((el) => el.dataset.wbHolds.split(" ").includes(id)) ?? null;
|
|
785
|
+
}
|
|
786
|
+
/** What waits for the reader, top to bottom: open forms and unread messages. */
|
|
787
|
+
function targets(kind = "all") {
|
|
788
|
+
const list = [.../* @__PURE__ */ new Set([...kind === "all" ? inState("open") : [], ...api$1.unread().map((e) => messageTarget(e.id)).filter((el) => !!el)])];
|
|
789
|
+
const top = (el) => el.getBoundingClientRect().top;
|
|
790
|
+
return list.sort((a, b) => top(a) - top(b));
|
|
791
|
+
}
|
|
792
|
+
let messagesToast = null;
|
|
793
|
+
let toastTimer = 0;
|
|
794
|
+
function show(list = forms) {
|
|
795
|
+
forms = list;
|
|
796
|
+
for (const { form, name, state, last } of forms) {
|
|
797
|
+
if (last && reopened.has(name) && reopened.get(name) !== last.id && state === "decided") reopened.delete(name);
|
|
798
|
+
form.toggleAttribute("data-wb-folded", state === "decided" && !reopened.has(name));
|
|
799
|
+
}
|
|
800
|
+
const unread = api$1.unread().length;
|
|
801
|
+
const parts = ORDER.map((state) => [state, inState(state).length]).filter(([, count]) => count).map(([state, count]) => [state, LABEL[state](count)]);
|
|
802
|
+
if (unread) parts.splice(inState("open").length ? 1 : 0, 0, ["messages", messagesLabel(unread)]);
|
|
803
|
+
summary.hidden = !parts.length;
|
|
804
|
+
summary.replaceChildren(...parts.flatMap(([state, text], i) => {
|
|
805
|
+
const link = document.createElement("button");
|
|
806
|
+
link.type = "button";
|
|
807
|
+
link.dataset.state = state;
|
|
808
|
+
link.textContent = text;
|
|
809
|
+
return i ? [document.createTextNode(" · "), link] : [link];
|
|
810
|
+
}));
|
|
811
|
+
const n = inState("open").length + unread;
|
|
812
|
+
button.hidden = n === 0;
|
|
813
|
+
button.textContent = `→ waiting for you (${n})`;
|
|
814
|
+
api$1.badge("waiting", n ? `!${n}` : null, -1);
|
|
815
|
+
if (!unread) {
|
|
816
|
+
messagesToast?.close();
|
|
817
|
+
messagesToast = null;
|
|
818
|
+
clearTimeout(toastTimer);
|
|
819
|
+
} else if (messagesToast) messagesToast.update(messagesLabel(unread));
|
|
820
|
+
else {
|
|
821
|
+
clearTimeout(toastTimer);
|
|
822
|
+
toastTimer = window.setTimeout(() => {
|
|
823
|
+
const left = api$1.unread().length;
|
|
824
|
+
if (left && !messagesToast) messagesToast = api$1.toast(messagesLabel(left), {
|
|
825
|
+
ms: 0,
|
|
826
|
+
onClick: () => jump("messages")
|
|
827
|
+
});
|
|
828
|
+
}, 1e3);
|
|
829
|
+
}
|
|
830
|
+
if (arriving && n) {
|
|
831
|
+
arriving = false;
|
|
832
|
+
history.replaceState(history.state, "", location.pathname + location.search);
|
|
833
|
+
requestAnimationFrame(() => {
|
|
834
|
+
const first = targets()[0];
|
|
835
|
+
if (first) jumpTo(first);
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
let arriving = location.hash === WAITING_HASH;
|
|
840
|
+
setTimeout(() => {
|
|
841
|
+
arriving = false;
|
|
842
|
+
}, 5e3);
|
|
843
|
+
let current = null;
|
|
844
|
+
let scrolledTo = 0;
|
|
845
|
+
function next(list) {
|
|
846
|
+
const i = current ? list.indexOf(current) : -1;
|
|
847
|
+
if (i >= 0 && Math.abs(scrollY - scrolledTo) < 40) return list[(i + 1) % list.length];
|
|
848
|
+
return list.find((f) => f.getBoundingClientRect().top > viewBottom() / 2) ?? list[0];
|
|
849
|
+
}
|
|
850
|
+
function jump(what) {
|
|
851
|
+
const target = next(what === "all" ? targets() : what === "messages" ? targets("messages") : inState(what));
|
|
852
|
+
if (target) jumpTo(target);
|
|
853
|
+
}
|
|
854
|
+
function jumpTo(target) {
|
|
855
|
+
current = target;
|
|
856
|
+
const r = target.getBoundingClientRect();
|
|
857
|
+
scrolledTo = Math.max(0, Math.min(scrollY + r.top + r.height / 2 - viewBottom() / 2, document.documentElement.scrollHeight - innerHeight));
|
|
858
|
+
scrollTo({
|
|
859
|
+
top: scrolledTo,
|
|
860
|
+
behavior: "smooth"
|
|
861
|
+
});
|
|
862
|
+
if (target instanceof HTMLFormElement) {
|
|
863
|
+
if (target.hasAttribute("data-wb-open")) target.querySelector("input:not([type=hidden]), textarea, select, button")?.focus({ preventScroll: true });
|
|
864
|
+
} else target.dispatchEvent(new CustomEvent("wb-reveal", { bubbles: true }));
|
|
865
|
+
}
|
|
866
|
+
function trackWaiting(root, core) {
|
|
867
|
+
main$1 = root;
|
|
868
|
+
api$1 = core;
|
|
869
|
+
api$1.corner().prepend(button);
|
|
870
|
+
button.addEventListener("click", () => jump("all"));
|
|
871
|
+
main$1.before(summary);
|
|
872
|
+
summary.addEventListener("click", (e) => {
|
|
873
|
+
const state = e.target.closest("button[data-state]")?.dataset.state;
|
|
874
|
+
if (state) jump(state);
|
|
875
|
+
});
|
|
876
|
+
main$1.addEventListener("click", (e) => {
|
|
877
|
+
const target = e.target;
|
|
878
|
+
const form = target.closest("form[data-wb-folded]");
|
|
879
|
+
if (!form || target.closest("[data-block=chat-thread]")) return;
|
|
880
|
+
e.preventDefault();
|
|
881
|
+
const name = form.getAttribute("name");
|
|
882
|
+
const last = api$1.last(name);
|
|
883
|
+
if (last) reopened.set(name, last.id);
|
|
884
|
+
show(forms);
|
|
885
|
+
form.querySelector("input:not([type=hidden]), textarea, select")?.focus({ preventScroll: true });
|
|
886
|
+
});
|
|
887
|
+
api$1.onForms(show);
|
|
888
|
+
api$1.onEvents(() => show());
|
|
889
|
+
show(api$1.forms());
|
|
890
|
+
document.addEventListener("submit", (e) => {
|
|
891
|
+
const form = e.target;
|
|
892
|
+
const name = form.getAttribute("name");
|
|
893
|
+
if (!name || !main$1.contains(form) || !forms.some((f) => f.form === form)) return;
|
|
894
|
+
const takes = (fallback) => api$1.listeners().some((l) => !!l.fallback === fallback && (l.type === name || l.type === null));
|
|
895
|
+
if (takes(false)) return;
|
|
896
|
+
const fallback = takes(true);
|
|
897
|
+
const sent = new MutationObserver(() => {
|
|
898
|
+
if (form.dataset.wbState === "sending") return;
|
|
899
|
+
sent.disconnect();
|
|
900
|
+
if (form.dataset.wbState !== "sent") return;
|
|
901
|
+
if (fallback) api$1.toast("Sent. No agent waits on this page; the fallback agent picks it up.");
|
|
902
|
+
else api$1.toast("Sent, but no agent is listening right now. Your answer stays in the log until one checks this page.", { kind: "warn" });
|
|
903
|
+
});
|
|
904
|
+
sent.observe(form, {
|
|
905
|
+
attributes: true,
|
|
906
|
+
attributeFilter: ["data-wb-state"]
|
|
907
|
+
});
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
// src/client/page/live.ts
|
|
911
|
+
function trackLive(root, handle, api) {
|
|
912
|
+
const dot = root.querySelector(".wb-live");
|
|
913
|
+
if (!dot) return;
|
|
914
|
+
const hasAnswers = handle.kind === "page" || handle.kind === "file";
|
|
915
|
+
const show = () => {
|
|
916
|
+
const live = document.documentElement.hasAttribute("data-wb-live");
|
|
917
|
+
const listeners = hasAnswers ? api.listeners() : [];
|
|
918
|
+
const own = listeners.some((l) => !l.fallback);
|
|
919
|
+
const anyPage = listeners.some((l) => l.fallback);
|
|
920
|
+
const agent = live && (own || anyPage);
|
|
921
|
+
dot.toggleAttribute("data-agent", agent);
|
|
922
|
+
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";
|
|
923
|
+
dot.setAttribute("aria-label", dot.title);
|
|
924
|
+
};
|
|
925
|
+
api.onListeners(show);
|
|
926
|
+
new MutationObserver(show).observe(document.documentElement, {
|
|
927
|
+
attributes: true,
|
|
928
|
+
attributeFilter: ["data-wb-live"]
|
|
929
|
+
});
|
|
930
|
+
show();
|
|
931
|
+
}
|
|
932
|
+
// src/client/page/sort.ts
|
|
933
|
+
const KEY = (name) => `wb-sort:${name}`;
|
|
934
|
+
function sortList(select) {
|
|
935
|
+
const list = select.closest("section")?.querySelector("ul");
|
|
936
|
+
if (!list) return;
|
|
937
|
+
const by = select.value;
|
|
938
|
+
const value = (li) => by === "title" ? li.dataset.title ?? "" : Number(by === "updated" ? li.dataset.updated : li.dataset.order);
|
|
939
|
+
const rows = [...list.children];
|
|
940
|
+
rows.sort((a, b) => {
|
|
941
|
+
const x = value(a), y = value(b);
|
|
942
|
+
if (by === "updated") return y - x;
|
|
943
|
+
return typeof x === "string" ? x.localeCompare(y) : x - y;
|
|
944
|
+
});
|
|
945
|
+
list.append(...rows);
|
|
946
|
+
}
|
|
947
|
+
function applySorts() {
|
|
948
|
+
for (const select of document.querySelectorAll("select[data-wb-sort]")) {
|
|
949
|
+
let kept = null;
|
|
950
|
+
try {
|
|
951
|
+
kept = localStorage.getItem(KEY(select.dataset.wbSort));
|
|
952
|
+
} catch {}
|
|
953
|
+
if (kept && [...select.options].some((o) => o.value === kept)) select.value = kept;
|
|
954
|
+
sortList(select);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
function trackSorts() {
|
|
958
|
+
document.addEventListener("change", (e) => {
|
|
959
|
+
const select = e.target;
|
|
960
|
+
if (!select.matches?.("select[data-wb-sort]")) return;
|
|
961
|
+
try {
|
|
962
|
+
localStorage.setItem(KEY(select.dataset.wbSort), select.value);
|
|
963
|
+
} catch {}
|
|
964
|
+
sortList(select);
|
|
965
|
+
});
|
|
966
|
+
applySorts();
|
|
967
|
+
}
|
|
968
|
+
// src/client/page/orphans.ts
|
|
969
|
+
/** A message in a chat: the chat block's event. */
|
|
970
|
+
const isMessage = (e) => e.type === "chat:message" && typeof e.data.chat === "string";
|
|
971
|
+
function trackOrphanThreads(main, api) {
|
|
972
|
+
const box = document.createElement("section");
|
|
973
|
+
box.className = "wb-orphans";
|
|
974
|
+
box.hidden = true;
|
|
975
|
+
main.after(box);
|
|
976
|
+
function draw() {
|
|
977
|
+
const onPage = /* @__PURE__ */ new Set([...[...main.querySelectorAll("form[name]")].map((f) => f.getAttribute("name")), ...[...main.querySelectorAll("[data-chat]")].map((c) => c.getAttribute("data-chat"))]);
|
|
978
|
+
const threads = /* @__PURE__ */ new Map();
|
|
979
|
+
for (const e of api.events()) {
|
|
980
|
+
const chat = String(e.data.chat);
|
|
981
|
+
if (!isMessage(e) || onPage.has(chat)) continue;
|
|
982
|
+
threads.set(chat, [...threads.get(chat) ?? [], e]);
|
|
983
|
+
}
|
|
984
|
+
box.hidden = threads.size === 0;
|
|
985
|
+
if (box.hidden) return box.replaceChildren();
|
|
986
|
+
box.innerHTML = String(html`<h2>Chats no longer on this page</h2>
|
|
987
|
+
${[...threads].map(([chat, messages]) => {
|
|
988
|
+
const replaced = new Set(messages.map((m) => m.data.replaces).filter(Boolean).map(String));
|
|
989
|
+
return html`<div class="wb-orphan" data-chat="${chat}"><h3>${chat}</h3>${messages.filter((m) => !replaced.has(m.id)).map((m) => {
|
|
990
|
+
const who = m.from === "cli" ? "agent" : "you";
|
|
991
|
+
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>`;
|
|
992
|
+
})}</div>`;
|
|
993
|
+
})}`);
|
|
994
|
+
}
|
|
995
|
+
api.onEvents(draw);
|
|
996
|
+
api.onRendered(draw);
|
|
997
|
+
draw();
|
|
998
|
+
}
|
|
999
|
+
// src/client/page/main.ts
|
|
1000
|
+
let main;
|
|
1001
|
+
let page = "";
|
|
1002
|
+
let api;
|
|
1003
|
+
let islands = {};
|
|
1004
|
+
const disposers = /* @__PURE__ */ new Map();
|
|
1005
|
+
async function mountOne(el) {
|
|
1006
|
+
const island = islands[el.dataset.wbIsland];
|
|
1007
|
+
el.dataset.wbMounted = "";
|
|
1008
|
+
if (!island) return;
|
|
1009
|
+
try {
|
|
1010
|
+
const mod = await import(island.client);
|
|
1011
|
+
if (!el.isConnected) return;
|
|
1012
|
+
const mount = mod.default;
|
|
1013
|
+
if (typeof mount !== "function") throw new Error("the client module must export a mount(element, props, api) function as default");
|
|
1014
|
+
el.replaceChildren();
|
|
1015
|
+
const dispose = await mount(el, island.props, api);
|
|
1016
|
+
if (typeof dispose === "function") disposers.set(el, dispose);
|
|
1017
|
+
} catch (err) {
|
|
1018
|
+
console.error(`island ${island.block}:`, err);
|
|
1019
|
+
el.insertAdjacentHTML("afterbegin", String(html`<pre class="wb-error">block "${island.block}" failed to mount: ${err.message}</pre>`));
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
/** Mount islands outermost first; nested islands show up once their parent has rendered. */
|
|
1023
|
+
async function mountIslands(root = main) {
|
|
1024
|
+
for (;;) {
|
|
1025
|
+
const pending = [...root.querySelectorAll("[data-wb-island]:not([data-wb-mounted])")].filter((el) => !el.parentElement?.closest("[data-wb-island]:not([data-wb-mounted])"));
|
|
1026
|
+
if (!pending.length) return;
|
|
1027
|
+
await Promise.all(pending.map(mountOne));
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
function disposeWithin(node) {
|
|
1031
|
+
for (const [el, dispose] of disposers) if (node === el || node.contains(el)) {
|
|
1032
|
+
dispose();
|
|
1033
|
+
disposers.delete(el);
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
const NEXT = {
|
|
1037
|
+
auto: "light",
|
|
1038
|
+
light: "dark",
|
|
1039
|
+
dark: "auto"
|
|
1040
|
+
};
|
|
1041
|
+
const svg = (d) => `<svg viewBox="0 0 16 16" width="13" height="13" aria-hidden="true">${d}</svg>`;
|
|
1042
|
+
const THEME_ICON = {
|
|
1043
|
+
auto: svg("<circle cx=\"8\" cy=\"8\" r=\"5.5\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\"/><path d=\"M8 2.5a5.5 5.5 0 0 1 0 11Z\" fill=\"currentColor\"/>"),
|
|
1044
|
+
light: svg("<circle cx=\"8\" cy=\"8\" r=\"2.8\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\"/><path d=\"M8 1.2v1.8M8 13v1.8M1.2 8H3M13 8h1.8M3.2 3.2l1.3 1.3M11.5 11.5l1.3 1.3M3.2 12.8l1.3-1.3M11.5 4.5l1.3-1.3\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linecap=\"round\"/>"),
|
|
1045
|
+
dark: svg("<path d=\"M13.5 9.6A5.8 5.8 0 0 1 6.4 2.5a5.8 5.8 0 1 0 7.1 7.1Z\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linejoin=\"round\"/>")
|
|
1046
|
+
};
|
|
1047
|
+
const THEME_LABEL = {
|
|
1048
|
+
auto: "Theme: follows the system",
|
|
1049
|
+
light: "Theme: light",
|
|
1050
|
+
dark: "Theme: dark"
|
|
1051
|
+
};
|
|
1052
|
+
function themeToggle(root) {
|
|
1053
|
+
const button = root.querySelector(".wb-theme");
|
|
1054
|
+
if (!button) return;
|
|
1055
|
+
const show = () => {
|
|
1056
|
+
const pref = themePreference();
|
|
1057
|
+
button.innerHTML = THEME_ICON[pref];
|
|
1058
|
+
button.dataset.pref = pref;
|
|
1059
|
+
button.title = `${THEME_LABEL[pref]} (click for ${NEXT[pref]})`;
|
|
1060
|
+
button.setAttribute("aria-label", button.title);
|
|
1061
|
+
};
|
|
1062
|
+
show();
|
|
1063
|
+
button.addEventListener("click", () => {
|
|
1064
|
+
setThemePreference(NEXT[themePreference()]);
|
|
1065
|
+
show();
|
|
1066
|
+
});
|
|
1067
|
+
}
|
|
1068
|
+
function ago(ts) {
|
|
1069
|
+
const s = Math.round((Date.now() - ts) / 1e3);
|
|
1070
|
+
if (s < 10) return "just now";
|
|
1071
|
+
if (s < 60) return `${s}s ago`;
|
|
1072
|
+
if (s < 3600) return `${Math.round(s / 60)}m ago`;
|
|
1073
|
+
if (s < 86400) return `${Math.round(s / 3600)}h ago`;
|
|
1074
|
+
return new Date(ts).toLocaleDateString();
|
|
1075
|
+
}
|
|
1076
|
+
function tickTimes() {
|
|
1077
|
+
for (const t of document.querySelectorAll("time[data-ts]")) {
|
|
1078
|
+
const ts = Number(t.dataset.ts);
|
|
1079
|
+
t.textContent = ago(ts);
|
|
1080
|
+
t.title = new Date(ts).toLocaleString();
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
let blockSigs = [];
|
|
1084
|
+
const FIELD = (n) => n instanceof HTMLInputElement || n instanceof HTMLTextAreaElement || n instanceof HTMLSelectElement;
|
|
1085
|
+
const KEEP_CLASSES = ["wb-new", "wb-new-seen"];
|
|
1086
|
+
/** Patch the page in place: focus, caret, typed values, open <details> and unchanged islands survive. */
|
|
1087
|
+
/**
|
|
1088
|
+
* A <details> element's identity across renders: its summary, the nearest heading above its
|
|
1089
|
+
* top-level block, and how many before it share both. Blocks are matched by position when the page
|
|
1090
|
+
* updates, so a block inserted above an open section would otherwise pair it with a different
|
|
1091
|
+
* element and close it; this finds the same section wherever it moved.
|
|
1092
|
+
*/
|
|
1093
|
+
function detailsKeys(root) {
|
|
1094
|
+
const keys = /* @__PURE__ */ new Map();
|
|
1095
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1096
|
+
for (const el of root.querySelectorAll("details")) {
|
|
1097
|
+
let block = el;
|
|
1098
|
+
while (block.parentElement && block.parentElement !== root) block = block.parentElement;
|
|
1099
|
+
let heading = "";
|
|
1100
|
+
for (let b = block; b; b = b.previousElementSibling) if (/^H[1-6]$/.test(b.tagName)) {
|
|
1101
|
+
heading = b.textContent?.trim() ?? "";
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
const base = `${heading}\u0000${el.querySelector(":scope > summary")?.textContent?.trim() ?? ""}`;
|
|
1105
|
+
const n = seen.get(base) ?? 0;
|
|
1106
|
+
seen.set(base, n + 1);
|
|
1107
|
+
keys.set(el, `${base}\u0000${n}`);
|
|
1108
|
+
}
|
|
1109
|
+
return keys;
|
|
1110
|
+
}
|
|
1111
|
+
function morph(html) {
|
|
1112
|
+
const open = /* @__PURE__ */ new Set();
|
|
1113
|
+
for (const [el, key] of detailsKeys(main)) if (el.open) open.add(key);
|
|
1114
|
+
const next = main.cloneNode(false);
|
|
1115
|
+
next.innerHTML = html;
|
|
1116
|
+
for (const [el, key] of detailsKeys(next)) el.open = open.has(key);
|
|
1117
|
+
morphdom(main, next, {
|
|
1118
|
+
getNodeKey: (n) => n.dataset?.wbKey || n.id || void 0,
|
|
1119
|
+
onBeforeElUpdated(from, to) {
|
|
1120
|
+
if (from.dataset.wbKey) {
|
|
1121
|
+
if (from.dataset.wbKey === to.dataset.wbKey && "wbMounted" in from.dataset) {
|
|
1122
|
+
from.dataset.wbIsland = to.dataset.wbIsland;
|
|
1123
|
+
return false;
|
|
1124
|
+
}
|
|
1125
|
+
disposeWithin(from);
|
|
1126
|
+
}
|
|
1127
|
+
if (FIELD(from)) {
|
|
1128
|
+
if (from === document.activeElement) return false;
|
|
1129
|
+
to.value = from.value;
|
|
1130
|
+
if (from instanceof HTMLInputElement) to.checked = from.checked;
|
|
1131
|
+
}
|
|
1132
|
+
for (const { name, value } of [...from.attributes]) if (name.startsWith("data-wb-") && name !== "data-wb-mounted" && !to.hasAttribute(name)) to.setAttribute(name, value);
|
|
1133
|
+
for (const c of KEEP_CLASSES) if (from.classList.contains(c)) to.classList.add(c);
|
|
1134
|
+
return true;
|
|
1135
|
+
},
|
|
1136
|
+
onBeforeNodeDiscarded(node) {
|
|
1137
|
+
disposeWithin(node);
|
|
1138
|
+
return true;
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
document.documentElement.style.overflowAnchor = "none";
|
|
1143
|
+
/**
|
|
1144
|
+
* The reader's place before an update: the first block in view that the update keeps (by
|
|
1145
|
+
* signature, and which of the blocks with that signature it is), and where it is on screen.
|
|
1146
|
+
*/
|
|
1147
|
+
function placeOf(nextSigs) {
|
|
1148
|
+
const keep = new Set(nextSigs);
|
|
1149
|
+
const blocks = [...main.children];
|
|
1150
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
1151
|
+
const sig = blockSigs[i];
|
|
1152
|
+
if (!keep.has(sig) || blocks[i].getBoundingClientRect().bottom <= 0) continue;
|
|
1153
|
+
return {
|
|
1154
|
+
sig,
|
|
1155
|
+
nth: blockSigs.slice(0, i).filter((s) => s === sig).length,
|
|
1156
|
+
top: blocks[i].getBoundingClientRect().top
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
return null;
|
|
1160
|
+
}
|
|
1161
|
+
/** Scroll so the block of `place` is where it was on screen. */
|
|
1162
|
+
function keepPlace(place, sigs) {
|
|
1163
|
+
if (!place) return;
|
|
1164
|
+
let seen = 0;
|
|
1165
|
+
const i = sigs.findIndex((s) => s === place.sig && seen++ === place.nth);
|
|
1166
|
+
const el = main.children[i];
|
|
1167
|
+
if (el) window.scrollBy(0, el.getBoundingClientRect().top - place.top);
|
|
1168
|
+
}
|
|
1169
|
+
/** The new render of the page, patched in. */
|
|
1170
|
+
async function refresh() {
|
|
1171
|
+
const next = await currentPage.render();
|
|
1172
|
+
const data = next.data;
|
|
1173
|
+
islands = data.islands;
|
|
1174
|
+
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}">`));
|
|
1175
|
+
const content = new DOMParser().parseFromString(next.html, "text/html").getElementById("wb-main")?.innerHTML ?? "";
|
|
1176
|
+
const nextSigs = signaturesOf(content);
|
|
1177
|
+
const place = placeOf(nextSigs);
|
|
1178
|
+
morph(content);
|
|
1179
|
+
keepPlace(place, nextSigs);
|
|
1180
|
+
const updated = document.getElementById("wb-updated");
|
|
1181
|
+
if (updated && data.mtime) updated.dataset.ts = String(data.mtime);
|
|
1182
|
+
if (next.title) core.title(next.title);
|
|
1183
|
+
trackUpdate(main, blockSigs, nextSigs);
|
|
1184
|
+
blockSigs = nextSigs;
|
|
1185
|
+
await mountIslands();
|
|
1186
|
+
keepPlace(place, nextSigs);
|
|
1187
|
+
tickTimes();
|
|
1188
|
+
api.rendered();
|
|
1189
|
+
}
|
|
1190
|
+
let currentPage;
|
|
1191
|
+
let core;
|
|
1192
|
+
const mount = async (root, handle, coreApi) => {
|
|
1193
|
+
currentPage = handle;
|
|
1194
|
+
core = coreApi;
|
|
1195
|
+
page = handle.id;
|
|
1196
|
+
main = root.querySelector("#wb-main");
|
|
1197
|
+
islands = handle.data.islands;
|
|
1198
|
+
api = {
|
|
1199
|
+
...core,
|
|
1200
|
+
theme: currentTheme,
|
|
1201
|
+
onTheme
|
|
1202
|
+
};
|
|
1203
|
+
const isDir = handle.kind === "pages" || handle.kind === "folder";
|
|
1204
|
+
themeToggle(root);
|
|
1205
|
+
setInterval(tickTimes, 15e3);
|
|
1206
|
+
new MutationObserver(() => {
|
|
1207
|
+
if (main.querySelector("[data-wb-island]:not([data-wb-mounted])")) mountIslands();
|
|
1208
|
+
}).observe(main, {
|
|
1209
|
+
childList: true,
|
|
1210
|
+
subtree: true
|
|
1211
|
+
});
|
|
1212
|
+
handle.onChange(() => refresh());
|
|
1213
|
+
if (isDir) core.onEvents(() => refresh());
|
|
1214
|
+
if (isDir) core.onListeners(() => refresh());
|
|
1215
|
+
blockSigs = [...main.children].map(signature);
|
|
1216
|
+
if (!isDir) trackInitial(main, page, core);
|
|
1217
|
+
if (!isDir) trackWaiting(main, core);
|
|
1218
|
+
trackElsewhere(root, handle, core);
|
|
1219
|
+
trackLive(root, handle, core);
|
|
1220
|
+
if (isDir) {
|
|
1221
|
+
trackSorts();
|
|
1222
|
+
core.onRendered(applySorts);
|
|
1223
|
+
}
|
|
1224
|
+
if (!isDir) trackOrphanThreads(main, core);
|
|
1225
|
+
await mountIslands();
|
|
1226
|
+
tickTimes();
|
|
1227
|
+
};
|
|
1228
|
+
export { mount as default };
|