pi-pocket 0.25.0 → 0.26.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 (85) hide show
  1. package/AGENTS.md +2 -0
  2. package/README.md +5 -3
  3. package/docs/architecture.md +21 -0
  4. package/docs/design/design-mode.md +108 -0
  5. package/docs/design/notes-a.md +106 -0
  6. package/docs/design/notes-b.md +123 -0
  7. package/docs/design/notes-c.md +92 -0
  8. package/docs/design/notes-d.md +78 -0
  9. package/docs/design/release-notes-design.md +46 -0
  10. package/docs/extensions/browser-modules.md +2 -0
  11. package/docs/extensions/policies/api-stability.md +2 -0
  12. package/docs/extensions/reference/api-versions.md +1 -0
  13. package/docs/extensions/reference/host.md +1 -1
  14. package/docs/extensions/reference/limits.md +4 -0
  15. package/docs/extensions/reference/names.md +2 -2
  16. package/docs/extensions/reference/pocket.md +38 -0
  17. package/docs/features.md +19 -5
  18. package/docs/map.md +75 -54
  19. package/docs/release-notes.md +1 -1
  20. package/docs/self-editing.md +1 -1
  21. package/docs/systemd.md +1 -1
  22. package/npm-shrinkwrap.json +2 -2
  23. package/package.json +1 -1
  24. package/src/launcher/main.ts +20 -9
  25. package/src/server/app.ts +113 -4
  26. package/src/server/background.ts +3 -3
  27. package/src/server/cache-keeper.ts +605 -0
  28. package/src/server/compaction.ts +68 -15
  29. package/src/server/design/bundle.ts +65 -0
  30. package/src/server/design/export.ts +118 -0
  31. package/src/server/design/files.ts +49 -0
  32. package/src/server/design/guide.ts +56 -0
  33. package/src/server/design/index.ts +232 -0
  34. package/src/server/design/pptx.ts +304 -0
  35. package/src/server/design/render.ts +593 -0
  36. package/src/server/design/system.ts +248 -0
  37. package/src/server/docs.ts +55 -0
  38. package/src/server/extensions/artifacts.ts +202 -18
  39. package/src/server/extensions/design/canvas.js +304 -0
  40. package/src/server/extensions/design/comments.js +15 -0
  41. package/src/server/extensions/design/data.js +263 -0
  42. package/src/server/extensions/design/devices.js +70 -0
  43. package/src/server/extensions/design/docks.js +647 -0
  44. package/src/server/extensions/design/edits.js +37 -0
  45. package/src/server/extensions/design/index.js +326 -0
  46. package/src/server/extensions/design/messages.js +92 -0
  47. package/src/server/extensions/design/parts.js +160 -0
  48. package/src/server/extensions/design/state.js +146 -0
  49. package/src/server/extensions/design/style.css +1332 -0
  50. package/src/server/extensions/design/tweaks.js +161 -0
  51. package/src/server/extensions/design/views.js +375 -0
  52. package/src/server/extensions/design/workspace.js +723 -0
  53. package/src/server/extensions/design.ts +620 -0
  54. package/src/server/extensions/plan.ts +11 -9
  55. package/src/server/extensions/prompt.ts +11 -3
  56. package/src/server/extensions/subagents.ts +7 -1
  57. package/src/server/extensions/updates/index.js +16 -6
  58. package/src/server/extensions/updates.ts +2 -2
  59. package/src/server/http/artifact-assets.ts +216 -0
  60. package/src/server/http/artifact-inspect.ts +1281 -0
  61. package/src/server/http/artifacts.ts +89 -14
  62. package/src/server/http/conversation-routes.ts +46 -1
  63. package/src/server/http.ts +12 -4
  64. package/src/server/main.ts +4 -0
  65. package/src/server/npm-install.ts +82 -0
  66. package/src/server/npm-update.ts +100 -0
  67. package/src/server/reload.ts +5 -0
  68. package/src/server/room.ts +2 -0
  69. package/src/server/spend.ts +73 -9
  70. package/src/server/update-state.ts +257 -8
  71. package/src/server/updates.ts +268 -33
  72. package/src/server/zip.ts +163 -0
  73. package/types/pocket.d.ts +32 -0
  74. package/web/activity-fold.js +28 -0
  75. package/web/artifact-bridge.js +506 -82
  76. package/web/extension-ui.js +21 -1
  77. package/web/pip.js +46 -9
  78. package/web/sessions.js +1 -1
  79. package/web/sheets/appearance.js +10 -0
  80. package/web/sheets/viewers.js +26 -1
  81. package/web/style.css +11 -0
  82. package/web/taps.js +26 -0
  83. package/web/theme.js +6 -0
  84. package/web/transcript.js +19 -10
  85. package/src/server/http/public.ts +0 -30
@@ -0,0 +1,1281 @@
1
+ /**
2
+ * The page side of Design mode (`docs/design/design-mode.md`, stream B): inspect, text edit, tweaks, pins, and decks.
3
+ * `ARTIFACT_INSPECT` is a function expression, as a string, that `ARTIFACT_BRIDGE` (`artifacts.ts`) calls as it starts,
4
+ * with the line to the app. It changes nothing in the page until the app turns a mode on (inspect, text edit, pins), and
5
+ * undoes all of it when the app turns it off. Two things work from the page's own markup, with no mode: it reads the
6
+ * tweaks manifest (`<script type="application/json" id="pocket-tweaks">`) and tells the app, and it shows a deck
7
+ * (`<meta name="pocket-kind" content="deck">`, slides as `<section class="slide">`) one slide at a time.
8
+ *
9
+ * Nothing here is reachable from the page: the code runs in a closure, and the only line to the app is the one the
10
+ * bridge made. What it sends is data about the page's own elements, which the app checks (`web/artifact-bridge.js`).
11
+ * Styles go in by CSSOM and constructable sheets, and nodes by DOM calls, never by markup or `innerHTML`, so a page
12
+ * that sets a strict CSP or Trusted Types still works. The code has no backtick, and no dollar-brace but the one that
13
+ * sets the slide size, as it sits in a script tag inside a template string.
14
+ */
15
+
16
+ /** Deck slides are laid out at this size (16:9) and scaled to fit; `<meta name="pocket-slide-size" content="1280x720">` changes it. */
17
+ export const DECK_SLIDE_SIZE = { width: 1920, height: 1080 };
18
+
19
+ export const ARTIFACT_INSPECT = String.raw`(send, standalone) => {
20
+ "use strict";
21
+
22
+ const doc = document;
23
+ const win = window;
24
+ const raf = (callback) => win.requestAnimationFrame(callback);
25
+ const later = (callback, ms) => win.setTimeout(callback, ms);
26
+ const unlater = (id) => win.clearTimeout(id);
27
+ const fire = (event) => win.dispatchEvent(event);
28
+ const CustomEvt = (name, detail) => new win.CustomEvent(name, { detail });
29
+ const now = performance.now.bind(performance);
30
+ // The page's own scripts have not run yet: keep the native listener functions, so a page that replaces
31
+ // addEventListener cannot get hold of the handlers and call them with events it made up.
32
+ const real = typeof EventTarget === "function" && win instanceof EventTarget;
33
+ const addListener = real ? EventTarget.prototype.addEventListener.bind(win) : (...args) => win.addEventListener(...args);
34
+ const removeListener = real ? EventTarget.prototype.removeEventListener.bind(win) : (...args) => win.removeEventListener(...args);
35
+ const ACCENT = "#7aa2f7";
36
+ const SLIDE_W = ${DECK_SLIDE_SIZE.width};
37
+ const SLIDE_H = ${DECK_SLIDE_SIZE.height};
38
+ const STYLE_PROPS = ["color", "background-color", "font-family", "font-size", "font-weight", "line-height", "padding", "margin", "border-radius", "gap"];
39
+ const INLINE = new Set(["a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "del", "dfn", "em", "i", "ins", "kbd", "label", "mark", "q", "s", "samp", "small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr"]);
40
+ const NOT_TEXT = new Set(["script", "style", "noscript", "textarea", "select", "option", "svg", "canvas", "video", "audio", "iframe", "object", "embed", "input", "template", "head", "meta", "link", "title", "pocket-overlay"]);
41
+ const KNOB_TYPES = ["range", "color", "select", "toggle", "text"];
42
+ const squash = (text) => String(text).replace(/\s+/g, " ").trim();
43
+ const clip = (text, max) => String(text).slice(0, max);
44
+ const isEl = (node) => node instanceof Element;
45
+ const css = (el, props) => {
46
+ for (const name in props) {
47
+ el.style.setProperty(name, props[name]);
48
+ }
49
+
50
+ return el;
51
+ };
52
+
53
+ /** Window listeners that one part sets and clears together. */
54
+ const group = () => {
55
+ const held = [];
56
+
57
+ return {
58
+ add(type, handler, options) {
59
+ addListener(type, handler, options);
60
+ held.push([type, handler, options]);
61
+ },
62
+ clear() {
63
+ for (const [type, handler, options] of held.splice(0)) {
64
+ removeListener(type, handler, options);
65
+ }
66
+ },
67
+ };
68
+ };
69
+
70
+ // ---------------------------------------------------------------------------------------------------------------
71
+ // What an element is, as data: a short unique selector, its look, and where it sits.
72
+
73
+ const unique = (selector) => {
74
+ try {
75
+ return doc.querySelectorAll(selector).length === 1;
76
+ } catch {
77
+ return false;
78
+ }
79
+ };
80
+
81
+ const idSelector = (el) => (el.id ? "#" + CSS.escape(el.id) : "");
82
+ const pidSelector = (value) => "[data-pid=" + JSON.stringify(value) + "]";
83
+
84
+ /** One step of a selector: the tag, up to two plain classes, and a place among its siblings when those do not tell it apart. */
85
+ const step = (el) => {
86
+ const classes = [...el.classList].filter((name) => name.length <= 30 && !/\d{3,}/.test(name)).slice(0, 2);
87
+ let selector = el.localName + classes.map((name) => "." + CSS.escape(name)).join("");
88
+ const parent = el.parentElement;
89
+
90
+ if (parent) {
91
+ const same = [...parent.children].filter((child) => child.matches(selector));
92
+
93
+ if (same.length > 1) {
94
+ const ofType = [...parent.children].filter((child) => child.localName === el.localName);
95
+
96
+ selector += ":nth-of-type(" + (ofType.indexOf(el) + 1) + ")";
97
+ }
98
+ }
99
+
100
+ return selector;
101
+ };
102
+
103
+ const selectorOf = (el) => {
104
+ if (el === doc.documentElement) {
105
+ return "html";
106
+ }
107
+
108
+ if (el === doc.body) {
109
+ return "body";
110
+ }
111
+
112
+ if (el.id && unique(idSelector(el))) {
113
+ return idSelector(el);
114
+ }
115
+
116
+ const pid = el.getAttribute("data-pid");
117
+
118
+ if (pid && unique(pidSelector(pid))) {
119
+ return pidSelector(pid);
120
+ }
121
+
122
+ const parts = [];
123
+
124
+ for (let node = el; isEl(node) && node !== doc.documentElement; node = node.parentElement) {
125
+ if (node !== el && node.id && unique(idSelector(node))) {
126
+ parts.unshift(idSelector(node));
127
+ break;
128
+ }
129
+
130
+ parts.unshift(step(node));
131
+
132
+ if (unique(parts.join(" > "))) {
133
+ break;
134
+ }
135
+ }
136
+
137
+ while (parts.join(" > ").length > 500 && parts.length > 1) {
138
+ parts.shift();
139
+ }
140
+
141
+ return parts.join(" > ");
142
+ };
143
+
144
+ const records = new Map();
145
+
146
+ const targetOf = (el) => {
147
+ const rect = el.getBoundingClientRect();
148
+ const computed = win.getComputedStyle(el);
149
+ const styles = {};
150
+ const near = el.closest("[data-pid]");
151
+ const round = (value) => Math.round(value * 10) / 10;
152
+ const copy = el.cloneNode(true);
153
+ const was = records.get(el);
154
+
155
+ for (const name of STYLE_PROPS) {
156
+ styles[name] = clip(computed.getPropertyValue(name), 200);
157
+ }
158
+
159
+ // The page as it was written: not the attributes that text edit mode put on.
160
+ if (was && isEl(copy)) {
161
+ if (was.ce === null) {
162
+ copy.removeAttribute("contenteditable");
163
+ } else {
164
+ copy.setAttribute("contenteditable", was.ce);
165
+ }
166
+
167
+ if (was.sc === null) {
168
+ copy.removeAttribute("spellcheck");
169
+ } else {
170
+ copy.setAttribute("spellcheck", was.sc);
171
+ }
172
+ }
173
+
174
+ const target = {
175
+ selector: selectorOf(el),
176
+ tag: el.localName,
177
+ text: clip(squash(el.textContent), 200),
178
+ html: clip(copy.outerHTML ?? "", 1000),
179
+ rect: {
180
+ x: round(rect.x + win.scrollX),
181
+ y: round(rect.y + win.scrollY),
182
+ width: round(rect.width),
183
+ height: round(rect.height),
184
+ },
185
+ styles,
186
+ };
187
+
188
+ if (near) {
189
+ target.pid = clip(near.getAttribute("data-pid"), 200);
190
+ }
191
+
192
+ return target;
193
+ };
194
+
195
+ // ---------------------------------------------------------------------------------------------------------------
196
+ // The overlay: outlines and pins, in a closed shadow root on a fixed layer that takes no pointer. It is in the page
197
+ // only while it shows something.
198
+
199
+ let overlay = null;
200
+ let hoverEl = null;
201
+ let pickedEl = null;
202
+ let pins = [];
203
+ let looping = false;
204
+
205
+ const makeBox = (parent, dashed) => {
206
+ const box = css(doc.createElement("div"), {
207
+ position: "absolute",
208
+ display: "none",
209
+ "box-sizing": "border-box",
210
+ border: "2px " + (dashed ? "dashed" : "solid") + " " + ACCENT,
211
+ background: dashed ? "rgba(122,162,247,.08)" : "rgba(122,162,247,.16)",
212
+ "border-radius": "2px",
213
+ });
214
+ const label = css(doc.createElement("div"), {
215
+ position: "absolute",
216
+ left: "-2px",
217
+ bottom: "100%",
218
+ "white-space": "nowrap",
219
+ font: "11px/1.4 ui-monospace, monospace",
220
+ color: "#1a1b26",
221
+ background: ACCENT,
222
+ padding: "0 5px",
223
+ "border-radius": "3px 3px 0 0",
224
+ });
225
+
226
+ box.append(label);
227
+ parent.append(box);
228
+
229
+ return { box, label };
230
+ };
231
+
232
+ const ensureOverlay = () => {
233
+ if (overlay && overlay.host.isConnected) {
234
+ return overlay;
235
+ }
236
+
237
+ const host = css(doc.createElement("pocket-overlay"), {
238
+ all: "initial",
239
+ position: "fixed",
240
+ inset: "0",
241
+ "pointer-events": "none",
242
+ "z-index": "2147483647",
243
+ display: "block",
244
+ });
245
+ const root = host.attachShadow({ mode: "closed" });
246
+ const hover = makeBox(root, true);
247
+ const picked = makeBox(root, false);
248
+ const layer = doc.createElement("div");
249
+
250
+ root.append(layer);
251
+ doc.documentElement.append(host);
252
+ overlay = { host, hover, picked, layer };
253
+
254
+ return overlay;
255
+ };
256
+
257
+ const labelOf = (el) => {
258
+ const rect = el.getBoundingClientRect();
259
+
260
+ return (
261
+ el.localName +
262
+ (el.id ? "#" + el.id : "") +
263
+ ([...el.classList][0] ? "." + [...el.classList][0] : "") +
264
+ " " +
265
+ Math.round(rect.width) +
266
+ "\u00d7" +
267
+ Math.round(rect.height)
268
+ );
269
+ };
270
+
271
+ const place = (shape, el, keepLabel) => {
272
+ if (!el || !el.isConnected) {
273
+ shape.box.style.setProperty("display", "none");
274
+
275
+ return;
276
+ }
277
+
278
+ const rect = el.getBoundingClientRect();
279
+
280
+ css(shape.box, {
281
+ display: rect.width === 0 && rect.height === 0 ? "none" : "block",
282
+ left: rect.left + "px",
283
+ top: rect.top + "px",
284
+ width: rect.width + "px",
285
+ height: rect.height + "px",
286
+ });
287
+ shape.label.style.setProperty("top", rect.top < 20 ? "100%" : "auto");
288
+ shape.label.style.setProperty("bottom", rect.top < 20 ? "auto" : "100%");
289
+
290
+ if (keepLabel) {
291
+ shape.label.textContent = labelOf(el);
292
+ }
293
+ };
294
+
295
+ const findPinTarget = (target) => {
296
+ try {
297
+ const found = target.selector ? doc.querySelector(target.selector) : null;
298
+
299
+ if (found) {
300
+ return found;
301
+ }
302
+
303
+ return target.pid ? doc.querySelector(pidSelector(target.pid)) : null;
304
+ } catch {
305
+ return null;
306
+ }
307
+ };
308
+
309
+ const update = () => {
310
+ if (!hoverEl && !pickedEl && pins.length === 0) {
311
+ if (overlay) {
312
+ overlay.host.remove();
313
+ overlay = null;
314
+ }
315
+
316
+ return false;
317
+ }
318
+
319
+ const shown = ensureOverlay();
320
+
321
+ place(shown.hover, hoverEl && hoverEl !== pickedEl ? hoverEl : null, true);
322
+ place(shown.picked, pickedEl, true);
323
+
324
+ for (const pin of pins) {
325
+ if (!pin.el || !pin.el.isConnected) {
326
+ pin.el = findPinTarget(pin.target);
327
+ }
328
+
329
+ const rect = pin.el ? pin.el.getBoundingClientRect() : null;
330
+ const visible = rect !== null && (rect.width > 0 || rect.height > 0);
331
+
332
+ css(pin.node, {
333
+ display: visible ? "flex" : "none",
334
+ left: visible ? Math.min(Math.max(rect.left - 9, 2), win.innerWidth - 24) + "px" : "0",
335
+ top: visible ? Math.min(Math.max(rect.top - 9, 2), win.innerHeight - 24) + "px" : "0",
336
+ });
337
+
338
+ if (pin.outline) {
339
+ css(pin.outline, {
340
+ display: visible ? "block" : "none",
341
+ left: visible ? rect.left + "px" : "0",
342
+ top: visible ? rect.top + "px" : "0",
343
+ width: visible ? rect.width + "px" : "0",
344
+ height: visible ? rect.height + "px" : "0",
345
+ });
346
+ }
347
+ }
348
+
349
+ return true;
350
+ };
351
+
352
+ const loop = () => {
353
+ looping = false;
354
+
355
+ if (update()) {
356
+ looping = true;
357
+ raf(loop);
358
+ }
359
+ };
360
+
361
+ const paint = () => {
362
+ if (update() && !looping) {
363
+ looping = true;
364
+ raf(loop);
365
+ }
366
+ };
367
+
368
+ const setHover = (el) => {
369
+ const next = isEl(el) && el.localName !== "pocket-overlay" ? el : null;
370
+
371
+ if (next !== hoverEl) {
372
+ hoverEl = next;
373
+ paint();
374
+ }
375
+ };
376
+
377
+ const highlight = (targets) => {
378
+ pins = [];
379
+
380
+ if (overlay) {
381
+ overlay.layer.replaceChildren();
382
+ }
383
+
384
+ const list = Array.isArray(targets) ? targets.slice(0, 100) : [];
385
+
386
+ if (list.length > 0) {
387
+ const shown = ensureOverlay();
388
+
389
+ list.forEach((target, index) => {
390
+ if (typeof target !== "object" || target === null || typeof target.selector !== "string") {
391
+ return;
392
+ }
393
+
394
+ const text = typeof target.label === "string" || typeof target.label === "number" ? clip(target.label, 8) : String(index + 1);
395
+ const outline = css(doc.createElement("div"), {
396
+ position: "absolute",
397
+ display: "none",
398
+ "box-sizing": "border-box",
399
+ border: "1px solid " + ACCENT,
400
+ "border-radius": "2px",
401
+ opacity: ".55",
402
+ });
403
+ const node = css(doc.createElement("div"), {
404
+ position: "absolute",
405
+ display: "none",
406
+ "align-items": "center",
407
+ "justify-content": "center",
408
+ "box-sizing": "border-box",
409
+ "min-width": "22px",
410
+ height: "22px",
411
+ padding: "0 6px",
412
+ "border-radius": "11px",
413
+ background: ACCENT,
414
+ color: "#1a1b26",
415
+ font: "700 12px/1 system-ui, sans-serif",
416
+ "box-shadow": "0 1px 4px rgba(0,0,0,.45)",
417
+ });
418
+
419
+ node.textContent = text;
420
+ shown.layer.append(outline, node);
421
+ pins.push({ target: { selector: clip(target.selector, 500), pid: typeof target.pid === "string" ? clip(target.pid, 200) : "" }, el: null, node, outline });
422
+ });
423
+ }
424
+
425
+ paint();
426
+ };
427
+
428
+ // ---------------------------------------------------------------------------------------------------------------
429
+ // Inspect mode: outline the element under the pointer; a click or tap picks it, and the page's handlers do not run.
430
+
431
+ let inspectOn = false;
432
+
433
+ const BLOCKED = ["click", "dblclick", "auxclick", "contextmenu", "mousedown", "mouseup", "pointerdown", "pointerup", "submit"];
434
+
435
+ const onInspect = (event) => {
436
+ if (!inspectOn || !event.isTrusted) {
437
+ return;
438
+ }
439
+
440
+ const type = event.type;
441
+
442
+ if (type === "pointermove" || type === "pointerover") {
443
+ setHover(event.target);
444
+
445
+ return;
446
+ }
447
+
448
+ if (type === "pointerdown") {
449
+ setHover(event.target);
450
+ }
451
+
452
+ if (type !== "pointerdown" && type !== "pointerup") {
453
+ event.preventDefault();
454
+ }
455
+
456
+ event.stopImmediatePropagation();
457
+
458
+ if (type === "click" && isEl(event.target) && event.target.localName !== "pocket-overlay") {
459
+ pickedEl = event.target;
460
+ paint();
461
+ send({ type: "pocket-picked", target: targetOf(event.target) });
462
+ }
463
+ };
464
+
465
+ const setInspect = (on) => {
466
+ inspectOn = on;
467
+
468
+ if (on) {
469
+ setEdit(false);
470
+ } else {
471
+ hoverEl = null;
472
+ pickedEl = null;
473
+ paint();
474
+ }
475
+ };
476
+
477
+ // ---------------------------------------------------------------------------------------------------------------
478
+ // Text edit mode: text blocks become editable (plain text only); a change is reported when the block loses focus.
479
+
480
+ let editOn = false;
481
+ let hint = null;
482
+
483
+ /** A faint dashed line round what can be edited, so a phone (which has no hover) shows it. A constructable sheet, so a page CSP cannot stop it. */
484
+ const hintEdits = (on) => {
485
+ if (on && hint === null) {
486
+ try {
487
+ hint = new CSSStyleSheet();
488
+ hint.replaceSync("[contenteditable]:not(:focus){outline:1px dashed rgba(122,162,247,.8)!important;outline-offset:2px!important}");
489
+ doc.adoptedStyleSheets = [...doc.adoptedStyleSheets, hint];
490
+ } catch {
491
+ hint = null;
492
+ }
493
+ } else if (!on && hint !== null) {
494
+ doc.adoptedStyleSheets = doc.adoptedStyleSheets.filter((sheet) => sheet !== hint);
495
+ hint = null;
496
+ }
497
+ };
498
+
499
+ const editableOf = (node) => {
500
+ if (records.size === 0) {
501
+ return null;
502
+ }
503
+
504
+ for (let el = isEl(node) ? node : node && node.parentElement; el; el = el.parentElement) {
505
+ if (records.has(el)) {
506
+ return el;
507
+ }
508
+ }
509
+
510
+ return null;
511
+ };
512
+
513
+ const textBlocks = () => {
514
+ const found = [];
515
+
516
+ const walk = (el) => {
517
+ if (found.length >= 2000 || NOT_TEXT.has(el.localName) || el.isContentEditable) {
518
+ return;
519
+ }
520
+
521
+ if (el.getClientRects().length === 0) {
522
+ return;
523
+ }
524
+
525
+ const own = [...el.childNodes].some((node) => node.nodeType === 3 && node.nodeValue.trim() !== "");
526
+
527
+ if (own && [...el.querySelectorAll("*")].every((child) => INLINE.has(child.localName))) {
528
+ if (squash(el.textContent).length <= 2000) {
529
+ found.push(el);
530
+ }
531
+
532
+ return;
533
+ }
534
+
535
+ for (const child of el.children) {
536
+ walk(child);
537
+ }
538
+ };
539
+
540
+ if (doc.body) {
541
+ walk(doc.body);
542
+ }
543
+
544
+ return found;
545
+ };
546
+
547
+ const commit = (el) => {
548
+ const record = records.get(el);
549
+
550
+ if (!record) {
551
+ return;
552
+ }
553
+
554
+ const after = squash(el.textContent);
555
+
556
+ if (after === record.last || after.length > 2000) {
557
+ return;
558
+ }
559
+
560
+ record.last = after;
561
+ send({ type: "pocket-text-edited", target: targetOf(el), before: record.before, after });
562
+ };
563
+
564
+ const onEditKey = (event) => {
565
+ const el = editableOf(event.target);
566
+
567
+ if (!el) {
568
+ return;
569
+ }
570
+
571
+ event.stopImmediatePropagation();
572
+
573
+ if (event.type !== "keydown") {
574
+ return;
575
+ }
576
+
577
+ if (event.key === "Enter") {
578
+ event.preventDefault();
579
+ el.blur();
580
+ } else if (event.key === "Escape") {
581
+ const record = records.get(el);
582
+
583
+ event.preventDefault();
584
+
585
+ if (record && record.nodes) {
586
+ el.replaceChildren(...record.nodes.map((node) => node.cloneNode(true)));
587
+ }
588
+
589
+ el.blur();
590
+ } else if (event.key === " " && (el.localName === "button" || el.localName === "summary")) {
591
+ event.preventDefault();
592
+ doc.execCommand("insertText", false, " ");
593
+ }
594
+ };
595
+
596
+ const onEditFocus = (event) => {
597
+ const el = editableOf(event.target);
598
+ const record = el && records.get(el);
599
+
600
+ if (record) {
601
+ record.nodes = [...el.childNodes].map((node) => node.cloneNode(true));
602
+ }
603
+ };
604
+
605
+ const onEditBlur = (event) => {
606
+ const el = editableOf(event.target);
607
+
608
+ if (el) {
609
+ commit(el);
610
+ }
611
+ };
612
+
613
+ const onEditPaste = (event) => {
614
+ if (editableOf(event.target)) {
615
+ event.preventDefault();
616
+ doc.execCommand("insertText", false, (event.clipboardData ? event.clipboardData.getData("text/plain") : "").replace(/\s*\n\s*/g, " "));
617
+ }
618
+ };
619
+
620
+ const onEditPointer = (event) => {
621
+ if (!editOn || !event.isTrusted) {
622
+ return;
623
+ }
624
+
625
+ if (event.type === "pointermove" || event.type === "pointerover") {
626
+ setHover(editableOf(event.target));
627
+
628
+ return;
629
+ }
630
+
631
+ // The page's own click handlers (a link, a button's action) do not run while text is edited.
632
+ event.stopImmediatePropagation();
633
+
634
+ if (event.type === "click" || event.type === "submit" || event.type === "drop" || event.type === "dragstart") {
635
+ event.preventDefault();
636
+ }
637
+ };
638
+
639
+ /** Make the text blocks that show now editable: also those of a slide that came into view after the mode went on. */
640
+ const makeEditable = () => {
641
+ for (const el of textBlocks()) {
642
+ if (records.has(el)) {
643
+ continue;
644
+ }
645
+
646
+ records.set(el, { ce: el.getAttribute("contenteditable"), sc: el.getAttribute("spellcheck"), before: squash(el.textContent), last: squash(el.textContent), nodes: null });
647
+ el.setAttribute("contenteditable", "plaintext-only");
648
+ el.setAttribute("spellcheck", "false");
649
+
650
+ // A browser without plain-text editing makes it plain editing.
651
+ if (el.contentEditable !== "plaintext-only") {
652
+ el.setAttribute("contenteditable", "true");
653
+ }
654
+ }
655
+ };
656
+
657
+ const setEdit = (on) => {
658
+ hintEdits(on);
659
+
660
+ if (!on) {
661
+ for (const [el, record] of [...records]) {
662
+ commit(el);
663
+
664
+ if (record.ce === null) {
665
+ el.removeAttribute("contenteditable");
666
+ } else {
667
+ el.setAttribute("contenteditable", record.ce);
668
+ }
669
+
670
+ if (record.sc === null) {
671
+ el.removeAttribute("spellcheck");
672
+ } else {
673
+ el.setAttribute("spellcheck", record.sc);
674
+ }
675
+ }
676
+
677
+ records.clear();
678
+
679
+ if (editOn) {
680
+ hoverEl = null;
681
+ paint();
682
+ }
683
+
684
+ editOn = false;
685
+
686
+ return;
687
+ }
688
+
689
+ if (inspectOn) {
690
+ setInspect(false);
691
+ }
692
+
693
+ editOn = true;
694
+
695
+ makeEditable();
696
+ };
697
+
698
+ // ---------------------------------------------------------------------------------------------------------------
699
+ // The listeners of both modes sit on the window in the capture phase from the start, so they run before any the page
700
+ // adds. Each does nothing, and changes nothing, until its mode is on.
701
+
702
+ const always = group();
703
+
704
+ const onTouch = (event) => {
705
+ if (deck) {
706
+ onDeckTouch(event);
707
+ }
708
+
709
+ // A touch handler of the page does not run while inspecting; scrolling is not touched.
710
+ if (inspectOn && event.isTrusted) {
711
+ event.stopImmediatePropagation();
712
+ }
713
+ };
714
+
715
+ const install = () => {
716
+ always.clear();
717
+
718
+ for (const type of ["pointermove", "pointerover", ...BLOCKED]) {
719
+ always.add(type, onInspect, true);
720
+ }
721
+
722
+ for (const type of ["keydown", "keyup", "keypress"]) {
723
+ always.add(type, onEditKey, true);
724
+ }
725
+
726
+ always.add("focusin", onEditFocus, true);
727
+ always.add("focusout", onEditBlur, true);
728
+ always.add("paste", onEditPaste, true);
729
+
730
+ for (const type of ["pointermove", "pointerover", "click", "dblclick", "auxclick", "submit", "drop", "dragstart"]) {
731
+ always.add(type, onEditPointer, true);
732
+ }
733
+
734
+ for (const type of ["touchstart", "touchend"]) {
735
+ always.add(type, onTouch, { capture: true, passive: true });
736
+ }
737
+ };
738
+
739
+ // ---------------------------------------------------------------------------------------------------------------
740
+ // Tweaks: the page's manifest goes to the app; the app's values go into the page as custom properties and events.
741
+
742
+ let knobs = [];
743
+ let manifestText = null;
744
+
745
+ const knobOf = (raw) => {
746
+ if (typeof raw !== "object" || raw === null || typeof raw.name !== "string" || raw.name === "" || raw.name.length > 80) {
747
+ return null;
748
+ }
749
+
750
+ const type = KNOB_TYPES.includes(raw.type) ? raw.type : "text";
751
+ const scalar = (value) => (typeof value === "string" ? clip(value, 200) : typeof value === "boolean" ? value : Number.isFinite(value) ? value : "");
752
+ const knob = { name: raw.name, type, value: scalar(raw.value) };
753
+ const number = (value) => (typeof value === "number" && Number.isFinite(value) ? value : undefined);
754
+
755
+ if (typeof raw.label === "string") {
756
+ knob.label = clip(raw.label, 80);
757
+ }
758
+
759
+ for (const key of ["min", "max", "step"]) {
760
+ if (number(raw[key]) !== undefined) {
761
+ knob[key] = raw[key];
762
+ }
763
+ }
764
+
765
+ if (typeof raw.unit === "string") {
766
+ knob.unit = clip(raw.unit, 10);
767
+ }
768
+
769
+ if (Array.isArray(raw.options)) {
770
+ knob.options = raw.options
771
+ .slice(0, 50)
772
+ .map((option) => (typeof option === "object" && option !== null ? option.value : option))
773
+ .filter((option) => typeof option === "string" || typeof option === "number")
774
+ .map((option) => clip(option, 80));
775
+ }
776
+
777
+ return knob;
778
+ };
779
+
780
+ const readTweaks = () => {
781
+ const node = doc.getElementById("pocket-tweaks");
782
+ const text = node ? node.textContent : null;
783
+
784
+ if (text === manifestText) {
785
+ return;
786
+ }
787
+
788
+ const first = manifestText === null;
789
+
790
+ manifestText = text;
791
+
792
+ let list = [];
793
+
794
+ try {
795
+ const parsed = text === null ? [] : JSON.parse(text);
796
+
797
+ list = Array.isArray(parsed) ? parsed : [];
798
+ } catch {
799
+ list = [];
800
+ }
801
+
802
+ const seen = new Set();
803
+
804
+ knobs = list.slice(0, 40).map(knobOf).filter((knob) => {
805
+ if (knob === null || seen.has(knob.name)) {
806
+ return false;
807
+ }
808
+
809
+ seen.add(knob.name);
810
+
811
+ return true;
812
+ });
813
+
814
+ if (!first || knobs.length > 0) {
815
+ send({ type: "pocket-tweaks", knobs });
816
+ }
817
+ };
818
+
819
+ const setTweaks = (values) => {
820
+ if (typeof values !== "object" || values === null) {
821
+ return;
822
+ }
823
+
824
+ for (const name of Object.keys(values).slice(0, 100)) {
825
+ const value = values[name];
826
+
827
+ if (name.length > 80 || !(value === null || ["string", "number", "boolean"].includes(typeof value))) {
828
+ continue;
829
+ }
830
+
831
+ if (/^--[A-Za-z0-9_-]{1,78}$/.test(name)) {
832
+ const knob = knobs.find((each) => each.name === name);
833
+
834
+ if (value === null) {
835
+ doc.documentElement.style.removeProperty(name);
836
+ } else if (typeof value === "number") {
837
+ if (Number.isFinite(value)) {
838
+ doc.documentElement.style.setProperty(name, value + (knob && knob.unit ? knob.unit : ""));
839
+ }
840
+ } else {
841
+ doc.documentElement.style.setProperty(name, typeof value === "boolean" ? (value ? "1" : "0") : clip(value, 200));
842
+ }
843
+ }
844
+
845
+ fire(CustomEvt("pocket:tweak", { name, value }));
846
+ }
847
+ };
848
+
849
+ // ---------------------------------------------------------------------------------------------------------------
850
+ // Decks: one slide at a time, at a size of its own, scaled to fit; arrow keys and swipes move between slides.
851
+
852
+ const deckEvents = group();
853
+ let deck = null;
854
+ let touch = null;
855
+
856
+ const isDeck = () => {
857
+ const meta = doc.querySelector('meta[name="pocket-kind"]');
858
+
859
+ return meta !== null && meta.getAttribute("content") === "deck";
860
+ };
861
+
862
+ const slideSize = () => {
863
+ const meta = doc.querySelector('meta[name="pocket-slide-size"]');
864
+ const match = meta && /^\s*(\d{3,4})\s*[x×]\s*(\d{3,4})\s*$/i.exec(meta.getAttribute("content") || "");
865
+ const width = match ? Number(match[1]) : 0;
866
+ const height = match ? Number(match[2]) : 0;
867
+
868
+ // The same sizes as the exports take (parseSlideSize in design/render.ts): 320 to 3840 on each side.
869
+ return width >= 320 && height >= 320 && width <= 3840 && height <= 3840 ? { width, height } : { width: SLIDE_W, height: SLIDE_H };
870
+ };
871
+
872
+ const deckSheet = () => {
873
+ const { width, height } = deck.size;
874
+ const scale = Math.min(win.innerWidth / width, win.innerHeight / height) || 1;
875
+ return (
876
+ "html,body{margin:0!important;padding:0!important;width:100%!important;height:100%!important;overflow:hidden!important}" +
877
+ (deck.bare ? "html{background:#000}" : "") +
878
+ "section.slide:not([data-pocket-current]){display:none!important}" +
879
+ "section.slide[data-pocket-current]{position:fixed!important;left:50%!important;top:50%!important;width:" + width + "px!important;height:" + height +
880
+ "px!important;min-width:0!important;max-width:none!important;min-height:0!important;max-height:none!important;margin:0!important;box-sizing:border-box!important;overflow:hidden!important;" +
881
+ "transform-origin:center center!important;transform:translate(-50%,-50%) scale(" + scale + ")!important}" +
882
+ "@media print{@page{size:" + width + "px " + height + "px;margin:0}html,body{overflow:visible!important;height:auto!important}" +
883
+ "section.slide:not([data-pocket-current]),section.slide[data-pocket-current]{display:block!important;position:relative!important;left:auto!important;top:auto!important;" +
884
+ "transform:none!important;break-after:page;page-break-after:always}}"
885
+ );
886
+ };
887
+
888
+ const paintDeck = () => {
889
+ if (!deck) {
890
+ return;
891
+ }
892
+
893
+ deck.slides.forEach((slide, index) => {
894
+ if (index === deck.index) {
895
+ slide.setAttribute("data-pocket-current", "");
896
+ } else {
897
+ slide.removeAttribute("data-pocket-current");
898
+ unshow(slide);
899
+ }
900
+ });
901
+
902
+ if (deck.sheet) {
903
+ deck.sheet.replaceSync(deckSheet());
904
+ } else {
905
+ deck.node.textContent = deckSheet();
906
+ }
907
+
908
+ // A page that hides its other slides itself (".slide:not(:first-child){display:none}"): the current one must
909
+ // still show, in the layout the page gives its slides (flex, grid), so it gets that display inline until it goes.
910
+ const current = deck.slides[deck.index];
911
+
912
+ if (current && !deck.shownInline.has(current) && win.getComputedStyle(current).display === "none") {
913
+ deck.shownInline.set(current, {
914
+ value: current.style.getPropertyValue("display"),
915
+ priority: current.style.getPropertyPriority("display"),
916
+ });
917
+ current.style.setProperty("display", deck.display, "important");
918
+ }
919
+
920
+ // Text edit mode goes on to the slide that shows now.
921
+ if (editOn) {
922
+ makeEditable();
923
+ }
924
+ };
925
+
926
+ /** Give back a slide's own inline display, if the deck changed it to show the slide. */
927
+ const unshow = (slide) => {
928
+ const before = deck && deck.shownInline.get(slide);
929
+
930
+ if (!before) {
931
+ return;
932
+ }
933
+
934
+ deck.shownInline.delete(slide);
935
+
936
+ if (before.value === "") {
937
+ slide.style.removeProperty("display");
938
+ } else {
939
+ slide.style.setProperty("display", before.value, before.priority);
940
+ }
941
+ };
942
+
943
+ const tellDeck = () => {
944
+ send({ type: "pocket-deck", slides: deck.slides.length, current: deck.index });
945
+ fire(CustomEvt("pocket:slide", { index: deck.index, count: deck.slides.length }));
946
+ };
947
+
948
+ const goTo = (index) => {
949
+ if (!deck || !Number.isFinite(index)) {
950
+ return;
951
+ }
952
+
953
+ const next = Math.min(Math.max(Math.trunc(index), 0), deck.slides.length - 1);
954
+
955
+ if (next !== deck.index) {
956
+ deck.index = next;
957
+ paintDeck();
958
+ tellDeck();
959
+ }
960
+ };
961
+
962
+ const onDeckKey = (event) => {
963
+ const target = event.target;
964
+
965
+ if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey || editOn) {
966
+ return;
967
+ }
968
+
969
+ if (isEl(target) && (target.isContentEditable || ["input", "textarea", "select"].includes(target.localName))) {
970
+ return;
971
+ }
972
+
973
+ const key = event.key;
974
+ const plain = target === doc.body || target === doc.documentElement || !isEl(target);
975
+ const to = { ArrowRight: deck.index + 1, ArrowDown: deck.index + 1, PageDown: deck.index + 1, ArrowLeft: deck.index - 1, ArrowUp: deck.index - 1, PageUp: deck.index - 1, Home: 0, End: deck.slides.length - 1 }[key];
976
+
977
+ if (to !== undefined) {
978
+ event.preventDefault();
979
+ goTo(to);
980
+ } else if (key === " " && plain) {
981
+ event.preventDefault();
982
+ goTo(deck.index + (event.shiftKey ? -1 : 1));
983
+ }
984
+ };
985
+
986
+ const onDeckTouch = (event) => {
987
+ if (event.type === "touchstart") {
988
+ touch = event.touches.length === 1 ? { x: event.touches[0].clientX, y: event.touches[0].clientY, at: now() } : null;
989
+
990
+ return;
991
+ }
992
+
993
+ const swipe = touch;
994
+
995
+ touch = null;
996
+
997
+ if (!swipe || editOn || event.changedTouches.length !== 1) {
998
+ return;
999
+ }
1000
+
1001
+ const dx = event.changedTouches[0].clientX - swipe.x;
1002
+ const dy = event.changedTouches[0].clientY - swipe.y;
1003
+
1004
+ if (Math.abs(dx) > 50 && Math.abs(dx) > Math.abs(dy) * 1.5 && now() - swipe.at < 800) {
1005
+ goTo(deck.index + (dx < 0 ? 1 : -1));
1006
+ }
1007
+ };
1008
+
1009
+ const stopDeck = () => {
1010
+ deckEvents.clear();
1011
+
1012
+ if (!deck) {
1013
+ return;
1014
+ }
1015
+
1016
+ for (const slide of deck.slides) {
1017
+ slide.removeAttribute("data-pocket-current");
1018
+ unshow(slide);
1019
+ }
1020
+
1021
+ if (deck.sheet) {
1022
+ doc.adoptedStyleSheets = doc.adoptedStyleSheets.filter((sheet) => sheet !== deck.sheet);
1023
+ } else {
1024
+ deck.node.remove();
1025
+ }
1026
+
1027
+ if (deck.view) {
1028
+ deck.view.remove();
1029
+ }
1030
+
1031
+ deck = null;
1032
+ };
1033
+
1034
+ const syncDeck = () => {
1035
+ const slides = isDeck() ? [...doc.querySelectorAll("section.slide")] : [];
1036
+
1037
+ if (slides.length === 0) {
1038
+ stopDeck();
1039
+
1040
+ return;
1041
+ }
1042
+
1043
+ if (deck && deck.slides.length === slides.length && deck.slides.every((slide, index) => slide === slides[index])) {
1044
+ return;
1045
+ }
1046
+
1047
+ const started = deck === null;
1048
+
1049
+ if (started) {
1050
+ // Black around the slide, unless the page paints its own ground (read before the sheet is in).
1051
+ const bare = [doc.documentElement, doc.body].every((el) => {
1052
+ const color = win.getComputedStyle(el).backgroundColor;
1053
+
1054
+ return color === "rgba(0, 0, 0, 0)" || color === "transparent";
1055
+ });
1056
+
1057
+ // The display the page gives its slides, read before the sheet is in: the first one that shows, else block.
1058
+ const shown = slides.map((slide) => win.getComputedStyle(slide).display).find((each) => each !== "none");
1059
+
1060
+ deck = {
1061
+ slides,
1062
+ index: 0,
1063
+ size: slideSize(),
1064
+ bare,
1065
+ sheet: null,
1066
+ node: null,
1067
+ view: null,
1068
+ display: shown || "block",
1069
+ shownInline: new Map(),
1070
+ };
1071
+
1072
+ // A phone lays a page with no viewport out at 980 pixels wide: a deck needs the real width to fit it.
1073
+ if (!doc.querySelector('meta[name="viewport"]') && doc.head) {
1074
+ deck.view = doc.createElement("meta");
1075
+ deck.view.setAttribute("name", "viewport");
1076
+ deck.view.setAttribute("content", "width=device-width, initial-scale=1");
1077
+ doc.head.append(deck.view);
1078
+ }
1079
+
1080
+ try {
1081
+ deck.sheet = new CSSStyleSheet();
1082
+ doc.adoptedStyleSheets = [...doc.adoptedStyleSheets, deck.sheet];
1083
+ } catch {
1084
+ deck.sheet = null;
1085
+ deck.node = doc.createElement("style");
1086
+ (doc.head || doc.documentElement).append(deck.node);
1087
+ }
1088
+
1089
+ deckEvents.add("keydown", onDeckKey);
1090
+ deckEvents.add("resize", paintDeck);
1091
+ } else {
1092
+ deck.slides = slides;
1093
+ deck.index = Math.min(deck.index, slides.length - 1);
1094
+ }
1095
+
1096
+ paintDeck();
1097
+ tellDeck();
1098
+ };
1099
+
1100
+ // ---------------------------------------------------------------------------------------------------------------
1101
+ // Watching the page's own markup for a manifest or slides that come, change, or go.
1102
+
1103
+ let watcher = null;
1104
+ let pending = null;
1105
+
1106
+ const watch = () => {
1107
+ if (pending !== null) {
1108
+ return;
1109
+ }
1110
+
1111
+ pending = later(() => {
1112
+ pending = null;
1113
+
1114
+ if (!standalone) {
1115
+ readTweaks();
1116
+ }
1117
+
1118
+ syncDeck();
1119
+ }, 100);
1120
+ };
1121
+
1122
+ const start = () => {
1123
+ watch();
1124
+
1125
+ if (watcher === null) {
1126
+ watcher = new MutationObserver(watch);
1127
+ watcher.observe(doc.documentElement, { childList: true, subtree: true, characterData: true });
1128
+ }
1129
+ };
1130
+
1131
+ // What the app asked for, so a replaced document (a reply's HTML run again) can be given it again.
1132
+ const desired = { inspect: false, edit: false, targets: [], tweaks: {} };
1133
+ // The app may speak before the page is parsed (it says hello as the page starts): what needs the page waits.
1134
+ let ready = false;
1135
+ let backlog = [];
1136
+
1137
+ const apply = (data) => {
1138
+ switch (data.type) {
1139
+ case "pocket-inspect":
1140
+ desired.inspect = data.on === true;
1141
+ desired.edit = desired.edit && !desired.inspect;
1142
+ setInspect(desired.inspect);
1143
+ break;
1144
+ case "pocket-edit-text":
1145
+ desired.edit = data.on === true;
1146
+ desired.inspect = desired.inspect && !desired.edit;
1147
+ setEdit(desired.edit);
1148
+ break;
1149
+ case "pocket-tweak-set":
1150
+ if (typeof data.values === "object" && data.values !== null) {
1151
+ for (const name of Object.keys(data.values).slice(0, 100)) {
1152
+ if (data.values[name] === null) {
1153
+ delete desired.tweaks[name];
1154
+ } else {
1155
+ desired.tweaks[name] = data.values[name];
1156
+ }
1157
+ }
1158
+ }
1159
+
1160
+ setTweaks(data.values);
1161
+ break;
1162
+ case "pocket-highlight":
1163
+ desired.targets = Array.isArray(data.targets) ? data.targets : [];
1164
+ highlight(desired.targets);
1165
+ break;
1166
+ case "pocket-goto":
1167
+ goTo(data.slide);
1168
+ break;
1169
+ }
1170
+ };
1171
+
1172
+ const begin = () => {
1173
+ if (!standalone) {
1174
+ readTweaks();
1175
+ }
1176
+
1177
+ syncDeck();
1178
+ start();
1179
+ ready = true;
1180
+
1181
+ for (const data of backlog.splice(0)) {
1182
+ apply(data);
1183
+ }
1184
+ };
1185
+
1186
+ const whenParsed = () => {
1187
+ if (doc.readyState === "loading") {
1188
+ addListener("DOMContentLoaded", begin, { once: true });
1189
+ } else {
1190
+ begin();
1191
+ }
1192
+ };
1193
+
1194
+ install();
1195
+ whenParsed();
1196
+
1197
+ return {
1198
+ /** True when the message is one of the app's design messages (it is then not the page's to see). */
1199
+ message(data) {
1200
+ if (standalone || typeof data !== "object" || data === null) {
1201
+ return false;
1202
+ }
1203
+
1204
+ if (
1205
+ !["pocket-inspect", "pocket-edit-text", "pocket-tweak-set", "pocket-highlight", "pocket-goto"].includes(data.type)
1206
+ ) {
1207
+ return false;
1208
+ }
1209
+
1210
+ if (ready) {
1211
+ apply(data);
1212
+ } else {
1213
+ backlog.push(data);
1214
+ }
1215
+
1216
+ return true;
1217
+ },
1218
+
1219
+ /** The page's document was replaced (a reply's HTML run again): put the listeners back, and what was asked for. */
1220
+ restart() {
1221
+ if (pending !== null) {
1222
+ unlater(pending);
1223
+ pending = null;
1224
+ }
1225
+
1226
+ if (watcher !== null) {
1227
+ watcher.disconnect();
1228
+ watcher = null;
1229
+ }
1230
+
1231
+ if (overlay) {
1232
+ overlay.host.remove();
1233
+ overlay = null;
1234
+ }
1235
+
1236
+ stopDeck();
1237
+ hintEdits(false);
1238
+ records.clear();
1239
+ manifestText = null;
1240
+ knobs = [];
1241
+ hoverEl = null;
1242
+ pickedEl = null;
1243
+ pins = [];
1244
+ inspectOn = false;
1245
+ editOn = false;
1246
+ ready = false;
1247
+ touch = null;
1248
+ backlog = [
1249
+ { type: "pocket-tweak-set", values: { ...desired.tweaks } },
1250
+ { type: "pocket-highlight", targets: desired.targets },
1251
+ { type: "pocket-inspect", on: desired.inspect },
1252
+ { type: "pocket-edit-text", on: desired.edit },
1253
+ ];
1254
+ install();
1255
+ whenParsed();
1256
+ },
1257
+ };
1258
+ }`;
1259
+
1260
+ /** A page that declares itself a deck, in its markup: `<meta name="pocket-kind" content="deck">`. */
1261
+ export function isDeckPage(html: string): boolean {
1262
+ return [...html.matchAll(/<meta\b[^>]*>/gi)].some(
1263
+ ([tag]) =>
1264
+ /\bname\s*=\s*["']?pocket-kind\b/i.test(tag) &&
1265
+ /\bcontent\s*=\s*["']?deck\b/i.test(tag),
1266
+ );
1267
+ }
1268
+
1269
+ /**
1270
+ * A deck as its own page (a tab of its own, a published link): the deck part of the inspector, with no line to an app,
1271
+ * so it shows one slide at a time with keys and swipes, and prints one slide to a page. Any other page stays as written.
1272
+ */
1273
+ export function withDeck(html: string): string {
1274
+ if (!isDeckPage(html)) {
1275
+ return html;
1276
+ }
1277
+
1278
+ const start = /^\uFEFF?[\t\n\f\r ]*(?:<!doctype[^>]*>)?/i.exec(html)![0];
1279
+
1280
+ return `${start}<script>(${ARTIFACT_INSPECT})(function () {}, true);</script>${html.slice(start.length)}`;
1281
+ }