create-pmos 0.424.0 → 0.425.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 (63) hide show
  1. package/package.json +13 -13
  2. package/payload/KIT-VERSION +1 -1
  3. package/payload/planning/evals/eval-calibration.md +1 -1
  4. package/payload/planning/okrs/health-budgets.md +1 -1
  5. package/payload/state/discoveries.md +1 -1
  6. package/workspace/board-programs-test.js +41 -26
  7. package/workspace/briefing-premium-board-test.js +7 -4
  8. package/workspace/briefing-premium-insights-test.js +31 -15
  9. package/workspace/briefing-premium-products-test.js +46 -43
  10. package/workspace/changelog.json +12 -0
  11. package/workspace/guide/briefing-premium.md +3 -2
  12. package/workspace/guide/home.md +2 -2
  13. package/workspace/guide/index.md +1 -0
  14. package/workspace/guide/notes.md +39 -1
  15. package/workspace/guide/present-in-a-meeting.md +57 -0
  16. package/workspace/guide/programs.md +2 -2
  17. package/workspace/lib/briefing-premium-products.js +18 -4
  18. package/workspace/lib/meeting-focus.js +64 -14
  19. package/workspace/lib/routes-briefing-premium.js +19 -0
  20. package/workspace/lib/routes-meeting-focus.js +28 -6
  21. package/workspace/lib/settings-store.js +2 -1
  22. package/workspace/package-lock.json +2 -2
  23. package/workspace/package.json +1 -1
  24. package/workspace/premium-briefing-depth-test.js +1 -1
  25. package/workspace/premium-rec-now-test.js +153 -0
  26. package/workspace/premium-rec-tab-test.js +167 -0
  27. package/workspace/public/pm/cozy-aud.css +9 -0
  28. package/workspace/public/pm/cozy-aud.js +80 -0
  29. package/workspace/public/pm/cozy-live-pill.js +3 -0
  30. package/workspace/public/pm/cozy-pick.css +41 -0
  31. package/workspace/public/pm/cozy-pick.js +134 -0
  32. package/workspace/public/pm/cozy-rec-bridge.js +32 -0
  33. package/workspace/public/pm/cozy-rec-now.css +104 -0
  34. package/workspace/public/pm/cozy-rec-now.js +330 -0
  35. package/workspace/public/pm/cozy-rec-present.css +69 -0
  36. package/workspace/public/pm/cozy-rec-present.js +467 -0
  37. package/workspace/public/pm/cozy-rec-tab.css +90 -0
  38. package/workspace/public/pm/cozy-rec-tab.js +219 -0
  39. package/workspace/public/pm/cozy-settings.js +8 -2
  40. package/workspace/public/pm/home-app.js +37 -22
  41. package/workspace/public/pm/home-list.js +17 -10
  42. package/workspace/public/pm/home-more.js +2 -31
  43. package/workspace/public/pm/home-today.js +4 -4
  44. package/workspace/public/pm/live-model.js +1 -1
  45. package/workspace/public/pm/premium-board.js +56 -25
  46. package/workspace/public/pm/premium-insights.js +42 -24
  47. package/workspace/public/pm/premium-meeting.js +17 -4
  48. package/workspace/public/pm/premium-mtg-present.js +4 -3
  49. package/workspace/public/pm/premium-report-read.js +2 -0
  50. package/workspace/public/pm/premium-vw-mtg.js +15 -7
  51. package/workspace/public/pm/rec-state.js +13 -5
  52. package/workspace/public/pm/rec-tab-core.js +92 -0
  53. package/workspace/public/pm/view-notes.js +9 -0
  54. package/workspace/public/premium-aud.html +22 -0
  55. package/workspace/public/premium-board.html +1 -0
  56. package/workspace/public/premium-insights.html +2 -1
  57. package/workspace/public/premium-rec.html +30 -0
  58. package/workspace/public/premium.html +1 -0
  59. package/workspace/rec-present-test.js +229 -0
  60. package/workspace/server.js +9 -1
  61. package/workspace/test-lists/rec-now.json +6 -0
  62. package/workspace/test-lists/rec-present.json +13 -0
  63. package/workspace/test-lists/recording-tab.json +13 -0
@@ -0,0 +1,69 @@
1
+ /* pm/cozy-rec-present.css — Present in the recording tab (batch 12 item 1, S3: XrtA8Pick, XrtA9Present, XrtA9Window). Added by
2
+ * cozy-rec-present.js. The presenter view sits on the recording tab's dark stage; the picker is a light card above the Present button.
3
+ * Colours are the cozy tokens of pm/cozy.css only. */
4
+
5
+ /* ── the Present button in the bottom bar: a round icon, white while presenting ── */
6
+ .rxp-pb { width: 52px; height: 52px; border-radius: 50%; display: inline-grid; place-items: center; flex: none; border: 0; padding: 0; cursor: pointer;
7
+ background: var(--cz-c-33363c); color: var(--cz-c-ffffff); }
8
+ .rxp-pb svg, .rxp svg, .rxp-pop svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
9
+ .rxp-pb.on { background: var(--cz-c-ffffff); color: var(--cz-c-2a2e35); }
10
+ .rxp-pb:focus-visible, .rxp button:focus-visible, .rxp-pop button:focus-visible { outline: 2px solid var(--cz-blue); outline-offset: 2px; }
11
+ @media (hover: hover) { .rxp-pb:not(.on):hover { background: var(--cz-c-3a3d44); } }
12
+
13
+ /* ── the picker: From the prep, Find in PMOS, File or link, a tab, a window or the screen ── */
14
+ .rxp-pop { position: fixed; z-index: 1100; left: var(--rxp-x, 16px); bottom: var(--rxp-b, 96px); width: min(360px, calc(100vw - 32px)); max-height: calc(100vh - 140px); overflow: auto;
15
+ padding: 8px; border-radius: 18px; background: var(--cz-card); color: var(--cz-ink); box-shadow: var(--cz-lift); font-family: var(--cz-sans); }
16
+ .rxp-pick { display: flex; flex-direction: column; }
17
+ .rxp-ph { margin: 6px 10px 4px; font-size: 13px; font-weight: 700; color: var(--cz-ink2); }
18
+ .rxp-row { width: 100%; display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 6px 10px; border: 0; border-radius: 12px; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; }
19
+ .rxp-row:focus-visible { background: var(--cz-well); }
20
+ @media (hover: hover) { .rxp-row:hover { background: var(--cz-well); } }
21
+ .rxp-fi { width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center; background: var(--cz-well); color: var(--cz-ink2); flex: none; }
22
+ .rxp-w { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: 14px; font-weight: 700; }
23
+ .rxp-w span { font-size: 12px; font-weight: 500; color: var(--cz-ink3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
24
+ .rxp-pick hr { border: 0; border-top: 1px solid var(--cz-hair); margin: 6px 10px; }
25
+ .rxp-in { display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 6px 10px; }
26
+ .rxp-in input { flex: 1; min-width: 0; min-height: 32px; border: 0; background: transparent; font: inherit; font-size: 14px; color: var(--cz-ink); outline: none; }
27
+ .rxp-in input::placeholder { color: var(--cz-ink3); }
28
+ .rxp-none, .rxp-err { margin: 2px 10px 6px 54px; font-size: 12.5px; color: var(--cz-ink3); }
29
+ .rxp-err { color: var(--cz-red); }
30
+ .rxp-pop .rxp-ib { color: var(--cz-ink2); background: var(--cz-well); }
31
+
32
+ /* ── the presenter view on the dark stage, in place of what the stage showed ── */
33
+ .rxp-away { display: none !important; }
34
+ .rxp-host { flex: 1; min-height: 0; display: flex; width: 100%; height: 100%; }
35
+ .rxp { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; gap: 14px; color: var(--cz-c-ffffff); font-family: var(--cz-sans); }
36
+ .rxp p { margin: 0; }
37
+ .rxp-main { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; }
38
+ .rxp-main.rxp-one { grid-template-columns: minmax(0, 1fr); }
39
+ .rxp-they { position: relative; min-width: 0; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 56px 24px 24px; border-radius: 16px; background: var(--cz-c-17191c); overflow: hidden; }
40
+ .rxp-pill { position: absolute; left: 12px; top: 12px; z-index: 1; display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 11px; border-radius: 999px;
41
+ background: var(--cz-c-2546b8); color: var(--cz-c-ffffff); font-size: 12.5px; font-weight: 800; }
42
+ .rxp-audb { position: absolute; right: 12px; top: 10px; z-index: 1; }
43
+ .rxp-ib { width: 40px; height: 40px; border-radius: 50%; display: inline-grid; place-items: center; flex: none; border: 0; padding: 0; cursor: pointer; background: var(--cz-c-2a2d33); color: var(--cz-c-ffffff); }
44
+ .rxp-ib:disabled { opacity: .35; cursor: default; }
45
+ .rxp-slot { width: 100%; height: 100%; display: grid; place-items: center; }
46
+ .rxp-slot .vzx-box, .rxp-nx .vzx-box { cursor: default; border-radius: 6px; }
47
+ .rxp-doc { width: 100%; height: 100%; overflow: auto; padding: 28px 32px; border-radius: 10px; background: var(--cz-c-ffffff); color: var(--cz-c-2a2e35); }
48
+ .rxp-doc .vzx-page { font-size: 16px; }
49
+ .rxp-img { max-width: 100%; max-height: 100%; display: block; }
50
+ .rxp-pdf { width: 100%; height: 100%; border: 0; border-radius: 6px; background: var(--cz-c-ffffff); pointer-events: none; }
51
+ .rxp-video { width: 100%; height: 100%; object-fit: contain; display: block; }
52
+ .rxp-msg { max-width: 520px; display: flex; align-items: center; gap: 14px; font-size: 15px; line-height: 1.5; color: var(--cz-c-d9dce1); }
53
+ .rxp-col { min-height: 0; display: flex; flex-direction: column; gap: 12px; overflow: auto; }
54
+ .rxp-card { padding: 14px 14px 16px; border-radius: 14px; background: var(--cz-c-2a2d33); font-size: 14.5px; line-height: 1.5; }
55
+ .rxp-card h4 { display: flex; align-items: center; gap: 6px; margin: 0 0 8px; font-size: 12.5px; font-weight: 700; color: var(--cz-c-9aa1ab); }
56
+ .rxp-card ul { margin: 0; padding-left: 18px; }
57
+ .rxp-card li + li { margin-top: 8px; }
58
+ .rxp-nx { height: 160px; display: grid; place-items: center; border-radius: 10px; overflow: hidden; }
59
+ .rxp-nav { display: flex; align-items: center; gap: 10px; min-height: 44px; }
60
+ .rxp-count { min-width: 56px; text-align: center; font-size: 14px; font-weight: 800; font-variant-numeric: tabular-nums; }
61
+ .rxp-sp { flex: 1; }
62
+ .rxp-on { font-size: 13px; font-weight: 700; color: var(--cz-c-9aa1ab); font-variant-numeric: tabular-nums; }
63
+ .rxp-shots { display: flex; gap: 8px; }
64
+ .rxp-shots img { width: 56px; height: 32px; object-fit: cover; border-radius: 6px; background: var(--cz-c-33363c); }
65
+
66
+ @media (max-width: 900px) {
67
+ .rxp-main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(220px, 1fr) auto; }
68
+ .rxp-nx { height: 120px; }
69
+ }
@@ -0,0 +1,467 @@
1
+ /* pm/cozy-rec-present.js — Present, in the recording tab (batch 12 item 1, slice S3; canvas frames XrtA8Pick, XrtA9Present, XrtA9Window;
2
+ * rules 9 and 10 of the sticky "A · the rules").
3
+ *
4
+ * The recording tab (premium-rec.html, rec-tab.js) gives three slots: stage, side and bar. This module adds the Present button to the bar
5
+ * and, while something is presented, puts the presenter view in the stage in place of the shared screen. The side (Now) stays beside it.
6
+ * Picker above the button: From the prep (the meeting's material, with its kind and size), Find in PMOS, File or link, and
7
+ * "A tab, a window or your screen". While presenting it also has Stop presenting.
8
+ * Material what PMOS can open (a PMOS deck, document or report, an image, an uploaded PDF) opens in a clean AUDIENCE TAB
9
+ * (premium-aud.html, window name 'pmos-aud', cozy-aud.js) that the PM shares in the call like any tab. The stage shows
10
+ * "They see this" (the page they see), Next, Notes, Previous and Next, "3 of 6" and "2:10 on this slide". A document moves
11
+ * by section. Notes are the slide's speaker notes, else Claude's points from the prep. Page turns go to the audience tab over
12
+ * BroadcastChannel('pmos-present'); PMOS never joins or drives the call app.
13
+ * A Google Doc, Slides or Sheet, a website or an HTML file opens live in the audience tab: PMOS cannot read its pages, so the
14
+ * stage says so, keeps the time and Claude's points from the prep, and the PM moves through it in that tab.
15
+ * Screen a tab, a window or the screen, picked with getDisplayMedia in the press. The stage mirrors it live as "They see this",
16
+ * with Claude's "For this screen" points, the time on this screen and the shots kept (a shot when the picture changes).
17
+ * There is no Previous or Next: the PM moves through it in that window.
18
+ * Deck a PMOS deck's Present on another page, while a meeting records, sends { t: 'present', mat } on the same channel
19
+ * (cozy-rec-bridge.js), and this presenter opens it (rule 10).
20
+ * Writes: a pasted link or an uploaded file is added to the meeting's material (POST /api/meeting-material, a page press), when the meeting
21
+ * is known. Nothing else is written. An empty card is left out.
22
+ *
23
+ * matKind(x) → { id, title, kind: slides|doc|image|pdf|live|none, src, ws, url, sub, host } a material row as Present reads it (pure)
24
+ * sizeOf(m, L) → 'Google Slides · 6 slides, with your notes' (pure) pdfPages(text) → the page count of a PDF's bytes, or 0 (pure)
25
+ * prepPoints(brief, words, max) → [text] Claude's points from the prep, those that share a word with the words first (pure)
26
+ * screenName(settings, label) → 'your Jira window' (pure) presenterModel(S, now) → the view model (pure)
27
+ * pickHtml(P), presenterHtml(V) → markup (pure)
28
+ * mountPresent({ stage, side, bar }, ctx) → { present(mat), stop(), state() }
29
+ * ctx = { ws, note: { id, title }, event: { id, title, series, date }, focus: () => GET /api/meeting-focus body | null,
30
+ * screenPoints?: () => [text], onShot?: (blob) => void } */
31
+ import * as D from './premium-deck.js';
32
+ import * as MP from './premium-mtg-present.js';
33
+ import { toDom } from './cozy-md.js';
34
+ import { pmGet, pmPost, pressFetch } from './http.js';
35
+
36
+ export const CHANNEL = 'pmos-present';
37
+ export const AUD_NAME = 'pmos-aud';
38
+ export const audUrl = (ws) => '/premium-aud.html' + (ws ? '?ws=' + encodeURIComponent(ws) : '');
39
+ const esc = (s) => String(s == null ? '' : s).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
40
+ const clip = (s, n) => { const t = String(s == null ? '' : s).replace(/\s+/g, ' ').trim(); return t.length > n ? t.slice(0, n - 1) + '…' : t; };
41
+ const mss = (ms) => { const s = Math.max(0, Math.floor(ms / 1000)); return Math.floor(s / 60) + ':' + String(s % 60).padStart(2, '0'); };
42
+ const ICO = {
43
+ present: 'M3 4h18v12H3zM12 13V7M9 10l3-3 3 3M8 20h8M12 16v4', deck: 'M4 5h16v11H4zM12 16v4M8 20h8', doc: 'M7 3h7l5 5v13H7zM14 3v5h5',
44
+ image: 'M4 4.5h16v15H4zM9 10a1.6 1.6 0 1 0 0-.01M20 16l-5-5-8 8', link: 'M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1',
45
+ search: 'M11 4a7 7 0 1 0 0 14 7 7 0 0 0 0-14zM20 20l-4-4', plus: 'M12 5v14M5 12h14', screen: 'M3 4h18v12H3zM8 20h8M12 16v4', x: 'M6 6l12 12M18 6L6 18',
46
+ prev: 'M15 6l-6 6 6 6', next: 'M9 6l6 6-6 6', up: 'M12 19V5M6 11l6-6 6 6', upload: 'M12 16V4M7 9l5-5 5 5M4 20h16', eye: 'M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12zM12 9a3 3 0 1 0 0 6 3 3 0 0 0 0-6z',
47
+ spark: 'M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8',
48
+ };
49
+ const ic = (k, w = 18) => `<svg aria-hidden="true" width="${w}" height="${w}" viewBox="0 0 24 24"><path d="${ICO[k]}"/></svg>`;
50
+ const KIND_ICON = { slides: 'deck', doc: 'doc', image: 'image', pdf: 'doc', live: 'link', none: 'doc' };
51
+ const UNIT = { slides: 'slide', doc: 'section', pdf: 'page', image: 'image', live: 'page', none: 'file', screen: 'screen' };
52
+
53
+ /** A material row (GET /api/meeting-material, the focus body's material, a find result, or a deck's own { kind, ref, title, ws }). */
54
+ export function matKind(x) {
55
+ const m = MP.matOf(x || {});
56
+ const k = (x && (x.mkind || x.kind)) || '';
57
+ const pdf = m.kind === 'live' && k === 'file' && (x.file === 'pdf' || /\.pdf$/i.test(String(x.ref || '')));
58
+ const sheets = /^https:\/\/docs\.google\.com\/spreadsheets\//.test(String(x && x.url || ''));
59
+ const sub = pdf ? 'PDF' : sheets ? 'Google Sheets' : m.kind === 'slides' && k === 'pmos-deck' ? 'Slides' : m.sub.replace(/ · live$/, '');
60
+ return { ...m, kind: pdf ? 'pdf' : m.kind, sub };
61
+ }
62
+
63
+ const plural = (n, w) => n + ' ' + w + (n === 1 ? '' : 's');
64
+ /** The picker's second line: the kind, then the size once it is read. L = { deck } | { doc } | { pages } | { error } */
65
+ export function sizeOf(m, L) {
66
+ const bits = [m.sub];
67
+ if (L && L.deck) bits.push(plural(L.deck.slides.length, 'slide') + (L.deck.slides.some((s) => String(s.notes || '').trim()) ? ', with your notes' : ''));
68
+ else if (L && L.doc) bits.push(plural(L.doc.sections.length, 'section'));
69
+ else if (L && L.pages) bits.push(plural(L.pages, 'page'));
70
+ return bits.filter(Boolean).join(' · ');
71
+ }
72
+
73
+ /** The page count of a PDF, read from its bytes as latin1 text: each page object says /Type /Page. 0 when none is found (the pages sit
74
+ * in compressed object streams), and Present then says "page 3" with no total. */
75
+ export function pdfPages(text) {
76
+ const m = String(text || '').match(/\/Type\s*\/Page(?![a-zA-Z])/g);
77
+ return m ? m.length : 0;
78
+ }
79
+
80
+ const STOP = new Set(['the', 'and', 'for', 'with', 'this', 'that', 'from', 'what', 'about', 'into', 'their', 'there', 'have', 'will', 'your', 'when', 'were', 'which']);
81
+ const wordsOf = (s) => String(s || '').toLowerCase().split(/[^a-z0-9]+/).filter((w) => w.length >= 4 && !STOP.has(w));
82
+ /** Claude's points from the prep: the brief's prep notes, what matters and last time's open actions, one line each. The lines that share a
83
+ * word with `words` (the material, the slide, the screen) come first. With `only`, a line that shares none is left out. */
84
+ export function prepPoints(brief, words, max = 3, only = false) {
85
+ const b = brief || {};
86
+ const lines = [];
87
+ for (const l of String(b.prep || b.agent_prep_notes || '').split(/\n+/)) { const t = l.replace(/^\s*(?:[-*•]|\d+[.)])\s*/, '').trim(); if (t) lines.push(t); }
88
+ for (const x of Array.isArray(b.matters) ? b.matters : []) { const t = x && (x.text || x.title); if (t) lines.push(String(t) + (x.tag ? ': ' + String(x.tag).toLowerCase() : '') + '.'); }
89
+ const last = b.last || {};
90
+ for (const a of Array.isArray(last.actions) ? last.actions : []) if (a && a.text) lines.push((a.owner ? a.owner + ' owes ' : 'Open from last time: ') + String(a.text).replace(/\.$/, '') + (a.due ? ', due ' + a.due : '') + '.');
91
+ const want = new Set(wordsOf(Array.isArray(words) ? words.join(' ') : words));
92
+ const seen = new Set(), hit = [], rest = [];
93
+ for (const l of lines) {
94
+ const k = l.toLowerCase(); if (seen.has(k)) continue; seen.add(k);
95
+ (wordsOf(l).some((w) => want.has(w)) ? hit : rest).push(clip(l, 200));
96
+ }
97
+ return (only ? hit : hit.concat(rest)).slice(0, max);
98
+ }
99
+
100
+ /** What the PM shares, in words: 'your screen', 'your Jira window', 'the Viewer sync tab'. */
101
+ export function screenName(settings, label) {
102
+ const s = (settings && settings.displaySurface) || '';
103
+ const short = clip(String(label || '').split(/\s+[-–—|]\s+/).filter((x) => x && !/^(google chrome|chrome|chromium|microsoft edge|firefox|safari)$/i.test(x)).pop() || '', 40);
104
+ if (s === 'monitor' || /^(screen|entire screen)\b/i.test(label || '')) return 'your screen';
105
+ if (s === 'browser') return short ? 'the ' + short + ' tab' : 'a tab';
106
+ return short ? 'your ' + short + ' window' : 'your window';
107
+ }
108
+
109
+ /* ── the view model ── */
110
+ /** S = { mode: 'mat'|'screen'|null, m (matKind), L, at, since, shots, label, points, aud, prep } */
111
+ export function presenterModel(S, now = Date.now()) {
112
+ if (!S || !S.mode) return null;
113
+ const on = mss(now - (Number.isFinite(S.since) ? S.since : now));
114
+ if (S.mode === 'screen') {
115
+ return { mode: 'screen', they: 'They see this · ' + (S.label || 'your screen'), points: (S.points || []).slice(0, 4), shots: (S.shots || []).slice(-3), onThis: on + ' on this screen' };
116
+ }
117
+ const m = S.m, L = S.L || null;
118
+ const pages = L && L.deck ? L.deck.slides.length : L && L.doc ? L.doc.sections.length : L && L.pages ? L.pages : m.kind === 'image' ? 1 : 0;
119
+ const steps = m.kind === 'slides' || m.kind === 'doc' || m.kind === 'pdf';
120
+ const at = Math.max(0, pages ? Math.min(pages - 1, S.at || 0) : S.at || 0);
121
+ const slide = L && L.deck ? L.deck.slides[at] : null;
122
+ const sec = L && L.doc ? L.doc.sections[at] : null;
123
+ const head = slide ? D.slideText(slide).head : sec ? sec.head : '';
124
+ const spoken = String((slide && slide.notes) || '').split('\n').map((x) => x.replace(/^\s*[-*•]\s*/, '').trim()).filter(Boolean).slice(0, 6);
125
+ const notes = spoken.length ? spoken : (S.prep || []).slice(0, 4);
126
+ let next = null;
127
+ if (steps && (!pages || at < pages - 1)) {
128
+ const nx = L && L.deck ? L.deck.slides[at + 1] : L && L.doc ? L.doc.sections[at + 1] : null;
129
+ next = { i: at + 1, label: nx ? clip(L.deck ? D.slideText(nx).head || 'Slide ' + (at + 2) : nx.head, 90) : UNIT[m.kind].replace(/^./, (c) => c.toUpperCase()) + ' ' + (at + 2), slide: !!(L && L.deck) };
130
+ }
131
+ return {
132
+ mode: 'mat', m, kind: m.kind, at, pages, head, steps, error: L && L.error ? L.error : '', loading: !L && (m.kind === 'slides' || m.kind === 'doc'),
133
+ they: 'They see this', aud: !!S.aud, live: m.kind === 'live',
134
+ count: steps ? (pages ? (at + 1) + ' of ' + pages : UNIT[m.kind] + ' ' + (at + 1)) : '',
135
+ canPrev: steps && at > 0, canNext: steps && (!pages || at < pages - 1), next, notes, notesSpoken: spoken.length > 0,
136
+ onThis: on + ' on this ' + UNIT[m.kind],
137
+ };
138
+ }
139
+
140
+ /* ── markup (pure) ── */
141
+ const row = (k, icon, title, sub, extra = '') => `<button type="button" role="menuitem" class="rxp-row" data-rxp="${k}"${extra}><span class="rxp-fi">${ic(icon, 16)}</span><span class="rxp-w">${esc(title)}${sub ? `<span>${esc(sub)}</span>` : ''}</span></button>`;
142
+ /** P = { prep: [{ title, sub, icon }], find: { on, q, found, busy }, link: { on, err, busy }, presenting } */
143
+ export function pickHtml(P) {
144
+ const h = [];
145
+ if (P.prep.length) h.push('<h4 class="rxp-ph">From the prep</h4>' + P.prep.map((x, i) => row('mat', x.icon, x.title, x.sub, ` data-i="${i}"`)).join('') + '<hr>');
146
+ if (P.find.on) {
147
+ h.push(`<label class="rxp-in"><span class="rxp-fi">${ic('search', 16)}</span><input type="search" data-rxp="q" value="${esc(P.find.q)}" placeholder="Find in PMOS" aria-label="Find in PMOS"></label>`);
148
+ if (P.find.found) h.push(P.find.found.length ? P.find.found.map((x, i) => row('found', x.icon, x.title, x.sub, ` data-i="${i}"`)).join('') : `<p class="rxp-none">Nothing in PMOS is called “${esc(P.find.q)}”.</p>`);
149
+ } else h.push(row('find', 'search', 'Find in PMOS', ''));
150
+ if (P.link.on) {
151
+ h.push(`<div class="rxp-in"><span class="rxp-fi">${ic('link', 16)}</span><input type="url" data-rxp="url" placeholder="Paste a link" aria-label="Paste a link to present"><button type="button" class="rxp-ib" data-rxp="upload" aria-label="Upload a file" title="Upload a file (PDF, slides, image)">${ic('upload', 16)}</button><input type="file" data-rxp="file" hidden accept=".pdf,.png,.jpg,.jpeg,.gif,.webp,.html,.htm,.pptx"></div>`);
152
+ if (P.link.err) h.push(`<p class="rxp-err" role="alert">${esc(P.link.err)}</p>`);
153
+ } else h.push(row('link', 'plus', 'File or link', 'PDF, slides, Google Docs, Slides, Sheets'));
154
+ h.push(row('screen', 'screen', 'A tab, a window or your screen', 'Claude follows it here'));
155
+ if (P.presenting) h.push('<hr>' + row('stop', 'x', 'Stop presenting', ''));
156
+ return `<div class="rxp-pick" role="menu" aria-label="Present">${h.join('')}</div>`;
157
+ }
158
+
159
+ /** The presenter in the stage. slideBox: the class of a box a slide is drawn in later (drawn by the page half). */
160
+ export function presenterHtml(V) {
161
+ if (!V) return '';
162
+ if (V.mode === 'screen') {
163
+ const pts = V.points.length ? `<div class="rxp-card rxp-claude"><h4>${ic('spark', 14)}For this screen</h4><ul>${V.points.map((x) => `<li>${esc(x)}</li>`).join('')}</ul></div>` : '';
164
+ const shots = V.shots.length ? `<div class="rxp-shots" aria-label="Shots kept">${V.shots.map((s) => `<img alt="Shot at ${esc(s.at)}" src="${esc(s.src)}">`).join('')}</div>` : '';
165
+ return `<div class="rxp" data-mode="screen"><div class="rxp-main${pts ? '' : ' rxp-one'}"><div class="rxp-they"><span class="rxp-pill">${ic('eye', 14)}${esc(V.they)}</span><video class="rxp-video" data-rxp-video autoplay muted playsinline></video></div>${pts ? `<div class="rxp-col">${pts}</div>` : ''}</div>`
166
+ + `<div class="rxp-nav">${shots}<span class="rxp-sp"></span><span class="rxp-on" data-rxp-on>${esc(V.onThis)}</span></div></div>`;
167
+ }
168
+ const m = V.m;
169
+ const aud = V.aud ? '' : `<button type="button" class="rxp-ib rxp-audb" data-rxp="aud" aria-label="Open the audience tab" title="Open the audience tab, then share it in the call">${ic('present', 15)}</button>`;
170
+ let show;
171
+ if (V.error) show = `<div class="rxp-msg"><p><b>${esc(m.title)}</b> could not be read: ${esc(V.error)}</p></div>`;
172
+ else if (V.loading) show = `<div class="rxp-msg"><p>Reading ${esc(m.title)}.</p></div>`;
173
+ else if (m.kind === 'slides') show = '<div class="rxp-slot"><div class="vzx-box" data-rxp-slide="cur"></div></div>';
174
+ else if (m.kind === 'doc') show = '<div class="rxp-doc" data-rxp-doc></div>';
175
+ else if (m.kind === 'image') show = `<img class="rxp-img" alt="${esc(m.title)}" src="${esc(m.url)}">`;
176
+ else if (m.kind === 'pdf') show = `<iframe class="rxp-pdf" title="${esc(m.title)}, page ${V.at + 1}" src="${esc(m.url + '#page=' + (V.at + 1) + '&toolbar=0&navpanes=0')}" tabindex="-1"></iframe>`;
177
+ else if (m.kind === 'live') show = `<div class="rxp-msg"><p><b>${esc(m.host || m.title)}</b> is live in the audience tab. PMOS cannot read its pages, so move through it there.</p><button type="button" class="rxp-ib" data-rxp="front" aria-label="Bring the audience tab forward" title="Bring the audience tab forward">${ic('present', 16)}</button></div>`;
178
+ else show = '<div class="rxp-msg"><p>PMOS cannot draw a PowerPoint file. Open it in PowerPoint or Keynote and share that window: pick it under “A tab, a window or your screen”.</p></div>';
179
+ const next = V.next ? `<div class="rxp-card"><h4>Next</h4>${V.next.slide ? '<div class="rxp-nx"><div class="vzx-box" data-rxp-slide="next"></div></div>' : `<p><b>${esc(V.next.label)}</b></p>`}</div>` : '';
180
+ const notes = V.notes.length ? `<div class="rxp-card rxp-notes${V.notesSpoken ? '' : ' rxp-claude'}"><h4>${V.notesSpoken ? '' : ic('spark', 14)}${V.notesSpoken ? 'Notes' : 'From the prep'}</h4><ul>${V.notes.map((x) => `<li>${esc(x)}</li>`).join('')}</ul></div>` : '';
181
+ const col = next || notes ? `<div class="rxp-col">${next}${notes}</div>` : '';
182
+ const nav = V.steps ? `<button type="button" class="rxp-ib" data-rxp="prev" aria-label="Previous"${V.canPrev ? '' : ' disabled'}>${ic('prev', 16)}</button><b class="rxp-count">${esc(V.count)}</b><button type="button" class="rxp-ib" data-rxp="next" aria-label="Next"${V.canNext ? '' : ' disabled'}>${ic('next', 16)}</button>` : '';
183
+ return `<div class="rxp" data-mode="mat" data-kind="${esc(m.kind)}"><div class="rxp-main${col ? '' : ' rxp-one'}"><div class="rxp-they"><span class="rxp-pill">${ic('eye', 14)}${esc(V.they)}</span>${aud}${show}</div>${col}</div>`
184
+ + `<div class="rxp-nav">${nav}<span class="rxp-sp"></span><span class="rxp-on" data-rxp-on>${esc(V.onThis)}</span></div></div>`;
185
+ }
186
+
187
+ /* ── the page half ── */
188
+ const CSS = ['./premium-vw-slides.css', './cozy-mtg-present.css', './cozy-rec-present.css'].map((x) => new URL(x, import.meta.url).href);
189
+ function sheets() { for (const h of CSS) if (!document.querySelector(`link[href="${h}"]`)) { const l = document.createElement('link'); l.rel = 'stylesheet'; l.href = h; document.head.append(l); } }
190
+ const q = (o) => new URLSearchParams(Object.entries(o).filter(([, v]) => v != null && v !== '')).toString();
191
+
192
+ /** The audience tab, opened or found, inside the press. A tab that shows something else (a live page) is sent back to the audience page. */
193
+ export function openAudienceTab(ws) {
194
+ let w = null;
195
+ try { w = window.open('', AUD_NAME); } catch { w = null; }
196
+ if (!w) return null;
197
+ let blank = false, ours = false;
198
+ try { const h = w.location.href; blank = h === 'about:blank'; ours = /\/premium-aud\.html/.test(h); } catch { /* a live page of another site */ }
199
+ if (!ours || blank) { try { w.location.href = audUrl(ws); } catch { /* gone */ } }
200
+ try { w.focus(); } catch { /* the browser keeps it behind */ }
201
+ return w;
202
+ }
203
+
204
+ export function mountPresent(slots = {}, ctx = {}) {
205
+ const { stage, bar } = slots;
206
+ if (!stage || !bar) return { present: () => {}, stop: () => {}, state: () => null };
207
+ sheets();
208
+ const ws = ctx.ws || '';
209
+ const ch = typeof BroadcastChannel === 'function' ? new BroadcastChannel(CHANNEL) : null;
210
+ const S = { mode: null, m: null, L: null, at: 0, since: 0, shots: [], label: '', points: [], aud: false, prep: [], pick: false,
211
+ find: { on: false, q: '', found: null }, link: { on: false, err: '' }, mats: [], loaded: {}, stream: null, W: null };
212
+ let slideHtml = null, view = null, pickEl = null, tick = null, shotTimer = null, findTimer = null;
213
+ const hidden = [];
214
+
215
+ /* the button */
216
+ const slot = bar.querySelector('[data-slot="present"]') || bar;
217
+ const btn = document.createElement('button');
218
+ btn.type = 'button'; btn.className = 'rxp-pb'; btn.dataset.rxp = 'present'; btn.setAttribute('aria-label', 'Present'); btn.title = 'Present';
219
+ btn.setAttribute('aria-haspopup', 'menu'); btn.setAttribute('aria-expanded', 'false'); btn.innerHTML = ic('present', 20);
220
+ slot.append(btn);
221
+ btn.onclick = () => (S.pick ? closePick() : openPick());
222
+
223
+ /* the material of the meeting: the focus body's, else read once */
224
+ async function material() {
225
+ const F = typeof ctx.focus === 'function' ? ctx.focus() : null;
226
+ let rows = F && Array.isArray(F.material) ? F.material : null;
227
+ if (!rows && ctx.event && ctx.event.title) {
228
+ const r = await pmGet('/api/meeting-material?' + q({ event: ctx.event.id, title: ctx.event.title, series: ctx.event.series, ws }));
229
+ rows = r && r.ok ? r.items || [] : [];
230
+ }
231
+ return (rows || []).map((x) => ({ row: x, m: matKind({ ...x, ws: x.ws || ws }) }));
232
+ }
233
+ const brief = () => { const F = typeof ctx.focus === 'function' ? ctx.focus() : null; return F ? F.brief : null; };
234
+ async function load(m) {
235
+ if (S.loaded[m.id]) return S.loaded[m.id];
236
+ let L = null;
237
+ try {
238
+ if (m.kind === 'slides') { const d = D.parseDeck(await MP.readDoc(m.ws || ws, m.src), m.title); L = d.slides.length ? { deck: d } : { error: 'the deck has no slides' }; }
239
+ else if (m.kind === 'doc') L = { doc: MP.docSections(await MP.readDoc(m.ws || ws, m.src), m.title) };
240
+ else if (m.kind === 'pdf') { const r = await fetch(m.url, { credentials: 'same-origin' }); const b = new Uint8Array(await r.arrayBuffer()); let t = ''; for (let i = 0; i < b.length; i += 8192) t += String.fromCharCode.apply(null, b.subarray(i, i + 8192)); L = { pages: pdfPages(t) }; }
241
+ else L = {};
242
+ } catch (x) { L = { error: String((x && x.message) || x).slice(0, 160) }; }
243
+ S.loaded[m.id] = L;
244
+ return L;
245
+ }
246
+
247
+ /* the picker */
248
+ function pickModel() {
249
+ return { prep: S.mats.map(({ m }) => ({ title: m.title, sub: sizeOf(m, S.loaded[m.id]), icon: KIND_ICON[m.kind] })), find: S.find, link: S.link, presenting: !!S.mode };
250
+ }
251
+ function drawPick(focusSel) {
252
+ if (!S.pick) return;
253
+ if (!pickEl) { pickEl = document.createElement('div'); pickEl.className = 'rxp-pop cz'; document.body.append(pickEl); pickEl.addEventListener('click', onPick); pickEl.addEventListener('keydown', onPickKey); pickEl.addEventListener('input', onPickInput); pickEl.addEventListener('change', onPickFile); }
254
+ const a = !focusSel && pickEl.contains(document.activeElement) ? document.activeElement : null;
255
+ if (a && a.dataset.rxp) focusSel = `[data-rxp="${a.dataset.rxp}"]` + (a.dataset.i ? `[data-i="${a.dataset.i}"]` : ''); // a redraw keeps the focus where it was
256
+ pickEl.innerHTML = pickHtml(pickModel());
257
+ const r = btn.getBoundingClientRect(), w = Math.min(360, innerWidth - 32);
258
+ pickEl.style.setProperty('--rxp-x', Math.max(16, Math.min(innerWidth - w - 16, r.left + r.width / 2 - w / 2)) + 'px');
259
+ pickEl.style.setProperty('--rxp-b', Math.max(16, innerHeight - r.top + 10) + 'px');
260
+ const f = focusSel && pickEl.querySelector(focusSel);
261
+ if (f) { f.focus(); if (f.type === 'search') f.setSelectionRange(f.value.length, f.value.length); }
262
+ }
263
+ async function openPick() {
264
+ S.pick = true; S.find = { on: false, q: '', found: null }; S.link = { on: false, err: '' };
265
+ btn.setAttribute('aria-expanded', 'true'); btn.classList.add('on');
266
+ S.mats = await material().catch(() => []);
267
+ drawPick();
268
+ const first = pickEl && pickEl.querySelector('[role="menuitem"]'); if (first) first.focus();
269
+ document.addEventListener('pointerdown', outside, true);
270
+ document.addEventListener('keydown', escPick, true);
271
+ for (const { m } of S.mats) if (m.kind === 'slides' || m.kind === 'doc' || m.kind === 'pdf') load(m).then(() => { if (S.pick && !S.find.on && !S.link.on) drawPick(); });
272
+ }
273
+ function closePick(back) {
274
+ S.pick = false; btn.setAttribute('aria-expanded', 'false'); if (!S.mode) btn.classList.remove('on');
275
+ if (pickEl) { pickEl.remove(); pickEl = null; }
276
+ document.removeEventListener('pointerdown', outside, true);
277
+ document.removeEventListener('keydown', escPick, true);
278
+ if (back) btn.focus();
279
+ }
280
+ const escPick = (e) => { if (e.key === 'Escape' && S.pick) { e.preventDefault(); e.stopPropagation(); closePick(true); } };
281
+ const outside = (e) => { if (pickEl && !pickEl.contains(e.target) && !btn.contains(e.target)) closePick(); };
282
+ function onPick(e) {
283
+ const t = e.target.closest('[data-rxp]'); if (!t || t.disabled) return;
284
+ const k = t.dataset.rxp;
285
+ if (k === 'mat') { const x = S.mats[+t.dataset.i]; if (x) { const W = x.m.kind === 'none' ? null : openAudienceTab(ws); closePick(); present(x.m, { W }); } return; }
286
+ if (k === 'found') { const x = (S.find.found || [])[+t.dataset.i]; if (x) { const W = openAudienceTab(ws); closePick(); present(x.m, { W }); } return; }
287
+ if (k === 'find') { S.find = { on: true, q: '', found: null }; return drawPick('[data-rxp="q"]'); }
288
+ if (k === 'link') { S.link = { on: true, err: '' }; return drawPick('[data-rxp="url"]'); }
289
+ if (k === 'upload') { const f = pickEl.querySelector('[data-rxp="file"]'); if (f) f.click(); return; }
290
+ if (k === 'screen') { closePick(); shareScreen(); return; }
291
+ if (k === 'stop') { closePick(true); stop(); }
292
+ }
293
+ function onPickKey(e) {
294
+ if (e.key === 'Enter' && e.target.dataset.rxp === 'url') { e.preventDefault(); addLink(e.target.value); }
295
+ if (/^Arrow(Down|Up)$/.test(e.key) && !/^(INPUT)$/.test(e.target.tagName)) {
296
+ e.preventDefault(); const all = [...pickEl.querySelectorAll('[role="menuitem"]')], i = all.indexOf(document.activeElement);
297
+ const n = all[(i + (e.key === 'ArrowDown' ? 1 : all.length - 1)) % all.length]; if (n) n.focus();
298
+ }
299
+ }
300
+ function onPickInput(e) {
301
+ if (e.target.dataset.rxp !== 'q') return;
302
+ S.find.q = e.target.value; clearTimeout(findTimer);
303
+ findTimer = setTimeout(async () => {
304
+ const text = S.find.q.trim();
305
+ if (text.length < 2) { S.find.found = null; return drawPick('[data-rxp="q"]'); }
306
+ const r = await pmGet('/api/meeting-material/suggest?' + q({ ws, q: text, limit: 8 }));
307
+ if (!S.pick || S.find.q.trim() !== text) return;
308
+ S.find.found = (r && r.ok ? r.found || [] : []).map((x) => { const m = matKind({ ...x, ws: x.ws || ws }); return { m, title: m.title, sub: x.meta || m.sub, icon: KIND_ICON[m.kind] }; });
309
+ drawPick('[data-rxp="q"]');
310
+ }, 220);
311
+ }
312
+ /* a link: kept with the meeting when it is known, then presented. The audience tab opens in the Enter press itself. */
313
+ async function addLink(v) {
314
+ let u = null; try { u = new URL(String(v).trim()); } catch { u = null; }
315
+ if (!u || u.protocol !== 'https:') { S.link.err = 'Only an https link can be presented.'; return drawPick('[data-rxp="url"]'); }
316
+ const W = openAudienceTab(ws);
317
+ closePick();
318
+ let item = { kind: /docs\.google\.com\/presentation\//.test(u.href) ? 'slides' : /docs\.google\.com\/document\//.test(u.href) ? 'gdoc' : 'link', url: u.href, title: u.hostname };
319
+ const ev = ctx.event;
320
+ if (ev && ev.title) { const r = await pmPost('/api/meeting-material', { op: 'add', event: { id: ev.id, title: ev.title, series: ev.series || null }, scope: 'meeting', item: { url: u.href, title: u.hostname } }); const got = r && r.ok && (r.items || []).find((x) => x.url === u.href); if (got) item = got; }
321
+ present(matKind(item), { W });
322
+ }
323
+ async function onPickFile(e) {
324
+ if (e.target.dataset.rxp !== 'file' || !e.target.files || !e.target.files[0]) return;
325
+ const f = e.target.files[0];
326
+ let r = null;
327
+ try { const res = await pressFetch('/api/meeting-material/file?name=' + encodeURIComponent(f.name), { method: 'POST', body: f }); r = await res.json(); } catch (x) { r = { ok: false, error: String((x && x.message) || x) }; }
328
+ if (!r || !r.ok) { S.link.err = 'Not uploaded: ' + ((r && r.error) || 'no answer'); return drawPick('[data-rxp="url"]'); }
329
+ closePick();
330
+ const item = { kind: 'file', ref: r.name, title: f.name, file: r.kind };
331
+ const ev = ctx.event;
332
+ if (ev && ev.title) pmPost('/api/meeting-material', { op: 'add', event: { id: ev.id, title: ev.title, series: ev.series || null }, scope: 'meeting', item: { kind: 'file', ref: r.name, title: f.name } });
333
+ present(matKind(item), { W: null }); // no press is left for a tab: "Open the audience tab" is one press on the stage
334
+ }
335
+
336
+ /* the stage */
337
+ function takeStage() {
338
+ if (view) return;
339
+ for (const c of [...stage.children]) { hidden.push([c, c.hidden]); c.hidden = true; c.classList.add('rxp-away'); } // the class: a stage child may set its own display
340
+ view = document.createElement('div'); view.className = 'rxp-host'; view.addEventListener('click', onStage); stage.append(view);
341
+ btn.classList.add('on');
342
+ document.addEventListener('keydown', onKey);
343
+ }
344
+ function giveStage() {
345
+ if (!view) return;
346
+ view.remove(); view = null;
347
+ for (const [c, h] of hidden.splice(0)) { c.hidden = h; c.classList.remove('rxp-away'); }
348
+ btn.classList.remove('on');
349
+ document.removeEventListener('keydown', onKey);
350
+ }
351
+ function words() { return [S.m ? S.m.title : '', presenterModel(S) ? presenterModel(S).head : ''].join(' '); }
352
+ function draw() {
353
+ if (!view) return;
354
+ if (S.mode === 'mat') S.prep = prepPoints(brief(), words(), 4);
355
+ if (S.mode === 'screen') S.points = typeof ctx.screenPoints === 'function' ? (ctx.screenPoints() || []) : prepPoints(brief(), S.label, 3, true);
356
+ const V = presenterModel(S);
357
+ const vid = view.querySelector('[data-rxp-video]');
358
+ view.innerHTML = presenterHtml(V);
359
+ if (V.mode === 'screen') { const v = view.querySelector('[data-rxp-video]'); if (v && S.stream) { if (vid && vid.srcObject === S.stream) v.replaceWith(vid); else { v.srcObject = S.stream; const p = v.play(); if (p && p.catch) p.catch(() => {}); } } return; }
360
+ const L = S.L;
361
+ if (L && L.deck && slideHtml) {
362
+ for (const b of view.querySelectorAll('[data-rxp-slide]')) {
363
+ const s = L.deck.slides[b.dataset.rxpSlide === 'next' ? V.at + 1 : V.at]; if (!s) continue;
364
+ b.innerHTML = slideHtml(s, L.deck.theme, S.m.ws || ws, false); MP.fit(b, b.parentElement); MP.place(b);
365
+ }
366
+ }
367
+ if (L && L.doc) {
368
+ const host = view.querySelector('[data-rxp-doc]'), sec = L.doc.sections[V.at];
369
+ if (host && sec) { const page = document.createElement('div'); page.className = 'vzx-page'; page.append(...toDom(sec.blocks, document)); host.append(page); }
370
+ }
371
+ }
372
+ function show() {
373
+ if (!ch) return;
374
+ const m = S.mode === 'mat' ? S.m : null;
375
+ ch.postMessage(m ? { t: 'show', show: { mat: { id: m.id, kind: m.kind, title: m.title, src: m.src, ws: m.ws || ws, url: m.url, host: m.host }, at: S.at, pages: (presenterModel(S) || {}).pages || 0, meeting: (ctx.note && ctx.note.title) || (ctx.event && ctx.event.title) || '' } } : { t: 'off' });
376
+ }
377
+ async function present(mat, { W = null } = {}) {
378
+ const m = mat && mat.kind && 'sub' in mat ? mat : matKind(mat);
379
+ endScreen();
380
+ if (!slideHtml) ({ slideHtml } = await import('./premium-vw-slides.js'));
381
+ takeStage();
382
+ S.mode = 'mat'; S.m = m; S.at = 0; S.since = Date.now(); S.L = S.loaded[m.id] || null; if (W) S.W = W;
383
+ draw(); show(); startTick();
384
+ if (m.kind === 'slides' || m.kind === 'doc' || m.kind === 'pdf') { const L = await load(m); if (S.m === m) { S.L = L; draw(); show(); } }
385
+ if (ch) ch.postMessage({ t: 'ping' });
386
+ return { ok: true };
387
+ }
388
+ function go(k) {
389
+ const V = presenterModel(S); if (!V || V.mode !== 'mat' || !V.steps) return;
390
+ const j = Math.max(0, V.pages ? Math.min(V.pages - 1, k) : k); if (j === S.at) return;
391
+ S.at = j; S.since = Date.now(); draw(); show();
392
+ }
393
+ function onStage(e) {
394
+ const t = e.target.closest('[data-rxp]'); if (!t || t.disabled) return;
395
+ const k = t.dataset.rxp;
396
+ if (k === 'prev') return go(S.at - 1);
397
+ if (k === 'next') return go(S.at + 1);
398
+ if (k === 'aud' || k === 'front') { S.W = openAudienceTab(ws); if (ch) ch.postMessage({ t: 'ping' }); }
399
+ }
400
+ function onKey(e) {
401
+ if (S.mode !== 'mat' || e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey) return;
402
+ const t = e.target; if (t && (/^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName) || t.isContentEditable)) return;
403
+ if (e.key === 'ArrowRight' || e.key === 'PageDown') { e.preventDefault(); go(S.at + 1); }
404
+ else if (e.key === 'ArrowLeft' || e.key === 'PageUp') { e.preventDefault(); go(S.at - 1); }
405
+ }
406
+ function startTick() {
407
+ if (tick) return;
408
+ tick = setInterval(() => { const o = view && view.querySelector('[data-rxp-on]'); const V = presenterModel(S); if (o && V) o.textContent = V.onThis; }, 1000);
409
+ }
410
+
411
+ /* a tab, a window or the screen: picked in the press */
412
+ async function shareScreen() {
413
+ let stream = null;
414
+ try { stream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: false }); } catch { return; } // the PM closed the picker
415
+ const tr = stream.getVideoTracks()[0]; if (!tr) return;
416
+ endScreen(); if (S.mode === 'mat' && ch) ch.postMessage({ t: 'off' });
417
+ takeStage();
418
+ S.mode = 'screen'; S.m = null; S.L = null; S.stream = stream; S.shots = []; S.since = Date.now();
419
+ S.label = screenName(tr.getSettings ? tr.getSettings() : {}, tr.label);
420
+ tr.addEventListener('ended', () => { if (S.stream === stream) stop(); });
421
+ draw(); startTick(); startShots(stream);
422
+ }
423
+ function endScreen() {
424
+ if (shotTimer) { clearInterval(shotTimer); shotTimer = null; }
425
+ if (S.stream) { for (const t of S.stream.getTracks()) { try { t.stop(); } catch { /* gone */ } } S.stream = null; }
426
+ }
427
+ /* a shot when the picture changes: a small grey picture every 4 s is compared with the last kept one */
428
+ function startShots(stream) {
429
+ const v = document.createElement('video'); v.muted = true; v.playsInline = true; v.srcObject = stream; const p = v.play(); if (p && p.catch) p.catch(() => {});
430
+ const tiny = document.createElement('canvas'); tiny.width = 32; tiny.height = 18;
431
+ let last = null;
432
+ const take = () => {
433
+ if (!v.videoWidth) return;
434
+ const tc = tiny.getContext('2d', { willReadFrequently: true }); tc.drawImage(v, 0, 0, 32, 18);
435
+ const px = tc.getImageData(0, 0, 32, 18).data, g = new Uint8Array(32 * 18);
436
+ for (let i = 0; i < g.length; i++) g[i] = (px[i * 4] + px[i * 4 + 1] + px[i * 4 + 2]) / 3;
437
+ let diff = last ? 0 : Infinity; if (last) for (let i = 0; i < g.length; i++) diff += Math.abs(g[i] - last[i]);
438
+ if (diff / g.length < 6) return;
439
+ last = g;
440
+ const c = document.createElement('canvas'); c.width = 320; c.height = Math.round(320 * v.videoHeight / v.videoWidth) || 180;
441
+ c.getContext('2d').drawImage(v, 0, 0, c.width, c.height);
442
+ const at = new Date(); S.shots.push({ src: c.toDataURL('image/jpeg', 0.7), at: at.getHours() + ':' + String(at.getMinutes()).padStart(2, '0') }); if (S.shots.length > 12) S.shots.shift();
443
+ if (typeof ctx.onShot === 'function') c.toBlob((b) => { if (b) { try { ctx.onShot(b); } catch { /* the recorder's own */ } } }, 'image/jpeg', 0.8);
444
+ if (S.mode === 'screen') { const nav = view && view.querySelector('.rxp-nav'); if (nav) { const V = presenterModel(S); const tmp = document.createElement('div'); tmp.innerHTML = presenterHtml(V); nav.replaceWith(tmp.querySelector('.rxp-nav')); } }
445
+ };
446
+ v.addEventListener('loadeddata', take, { once: true });
447
+ shotTimer = setInterval(take, 4000);
448
+ }
449
+
450
+ function stop() {
451
+ endScreen();
452
+ if (S.mode === 'mat' && ch) ch.postMessage({ t: 'off' });
453
+ S.mode = null; S.m = null; S.L = null; S.W = null; S.aud = false;
454
+ if (tick) { clearInterval(tick); tick = null; }
455
+ giveStage();
456
+ }
457
+
458
+ /* the channel: the audience tab says hello (and is sent what to show); a deck on another page asks to be presented (rule 10) */
459
+ if (ch) ch.onmessage = (e) => {
460
+ const d = e.data || {};
461
+ if (d.t === 'hello') { const was = S.aud; S.aud = true; if (S.mode === 'mat') { show(); if (!was) draw(); } }
462
+ else if (d.t === 'bye') { if (S.aud) { S.aud = false; if (S.mode === 'mat') draw(); } }
463
+ else if (d.t === 'present' && d.mat) { closePick(); present(d.mat); }
464
+ };
465
+ addEventListener('resize', () => { if (S.mode === 'mat' && S.L && S.L.deck) draw(); });
466
+ return { present, stop, go, state: () => ({ mode: S.mode, at: S.at, m: S.m, aud: S.aud, label: S.label, shots: S.shots.length }) };
467
+ }