-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.js
More file actions
2498 lines (2192 loc) · 92.4 KB
/
app.js
File metadata and controls
2498 lines (2192 loc) · 92.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ============================================
// NAVIGATION
// ============================================
// Page map for multi-file navigation
const _PAGES = {
library: 'index.html',
editor: 'editor.html',
teleprompter: 'teleprompter.html',
golive: 'golive.html',
settings: 'settings.html',
rsvp: 'rsvp.html'
};
function navigateTo(screenId) {
if (_PAGES[screenId]) {
window.location.href = _PAGES[screenId];
}
}
function updateAppHeader(screenId) {
const center = document.getElementById('app-header-center');
const right = document.getElementById('app-header-right');
if (!center || !right) return;
const backBtn = `<button class="btn-back" onclick="navigateTo('library')"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15,18 9,12 15,6"/></svg></button>`;
const goLiveBtn = `<button class="btn-golive-header" onclick="navigateTo('golive')">
<span class="btn-golive-dot"></span>
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="23,7 16,12 23,17"/><rect x="1" y="5" width="15" height="14" rx="2" ry="2"/></svg>
GO LIVE
</button>`;
const settingsBtn = `<button class="btn-ghost btn-settings-gear" onclick="navigateTo('settings')">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>
</button>`;
switch (screenId) {
case 'library':
center.innerHTML = `
<div class="lib-breadcrumb">
<span class="bc-item">All Scripts</span>
<span class="bc-sep">/</span>
<span class="bc-item bc-current">Presentations</span>
</div>`;
right.innerHTML = `
<div class="lib-search">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/></svg>
<input type="text" placeholder="Search..." />
</div>
${goLiveBtn}
<button class="btn-create" onclick="navigateTo('editor')">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
New Script
</button>`;
break;
case 'editor':
center.innerHTML = `${backBtn}<span class="top-bar-title">Product Launch</span>`;
right.innerHTML = `
<button class="btn-ghost" onclick="navigateTo('rsvp')">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/><path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/></svg>
Learn
</button>
<button class="btn-gold" onclick="navigateTo('teleprompter')">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="5,3 19,12 5,21"/></svg>
Read
</button>
${goLiveBtn}`;
break;
case 'settings':
center.innerHTML = `${backBtn}<span class="top-bar-title">Settings</span>`;
right.innerHTML = `${goLiveBtn}`;
break;
case 'rsvp':
center.innerHTML = `${backBtn}<span class="top-bar-title">Product Launch</span><span style="color:var(--text-4);font-size:12px">Intro / Opening Block</span>`;
right.innerHTML = `<span class="top-bar-title" style="font-size:13px">250 WPM</span>${goLiveBtn}`;
break;
case 'teleprompter':
center.innerHTML = `${backBtn}<span class="top-bar-title">Product Launch</span><span style="color:var(--text-4);font-size:12px" id="rd-header-segment">Intro · Opening Block</span>`;
right.innerHTML = `${goLiveBtn}`;
break;
case 'golive':
center.innerHTML = '';
right.innerHTML = '';
break;
default:
center.innerHTML = '';
right.innerHTML = '';
}
}
// ============================================
// RSVP (Learn mode — simple word-by-word)
// ============================================
let rsvpSpeed = 250;
let rsvpPlaying = true;
function changeRsvpSpeed(delta) {
rsvpSpeed = Math.max(100, Math.min(600, rsvpSpeed + delta));
const el = document.getElementById('rsvp-speed');
if (el) el.textContent = rsvpSpeed;
const badge = document.getElementById('rsvp-wpm-badge');
if (badge) badge.textContent = rsvpSpeed + ' WPM';
}
function toggleRsvp() {
rsvpPlaying = !rsvpPlaying;
const btn = document.getElementById('rsvp-play-btn');
if (rsvpPlaying) {
btn.innerHTML = '<svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="6" y="4" width="4" height="16"/><rect x="14" y="4" width="4" height="16"/></svg>';
} else {
btn.innerHTML = '<svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="5,3 19,12 5,21"/></svg>';
}
}
const rsvpWords = [
'Good', 'morning', 'everyone,', 'and', 'welcome', 'to', 'what', 'I', 'believe',
'will', 'be', 'a', 'transformative', 'moment', 'for', 'our', 'company.',
'Today,', "we're", 'not', 'just', 'launching', 'a', 'product', '—',
"we're", 'introducing', 'a', 'solution', 'that', 'will', 'revolutionize',
'how', 'our', 'customers', 'interact', 'with', 'technology.'
];
let rsvpIndex = 12;
function getORP(word) {
const len = word.replace(/[^a-zA-Z]/g, '').length;
if (len <= 1) return 0;
if (len <= 5) return 1;
if (len <= 9) return 2;
return 3;
}
function renderRsvpWord() {
const word = rsvpWords[rsvpIndex];
const orp = getORP(word);
const container = document.getElementById('rsvp-word');
if (!container) return;
// Render focus word with ORP
let html = '';
for (let i = 0; i < word.length; i++) {
html += `<span${i === orp ? ' class="orp"' : ''}>${word[i]}</span>`;
}
container.innerHTML = html;
// Position the word so the ORP letter aligns with the center vertical line
const orpEl = container.querySelector('.orp');
if (orpEl) {
const containerRect = container.parentElement.getBoundingClientRect();
const orpRect = orpEl.getBoundingClientRect();
const containerCenter = containerRect.left + containerRect.width / 2;
const orpCenter = orpRect.left + orpRect.width / 2;
const shift = containerCenter - orpCenter;
container.style.transform = `translateX(${shift}px)`;
}
// Left context (5 words)
const leftEl = document.getElementById('rsvp-ctx-l');
if (leftEl) {
const leftWords = rsvpWords.slice(Math.max(0, rsvpIndex - 5), rsvpIndex);
leftEl.innerHTML = leftWords.map(w => `<span>${w}</span>`).join('');
}
// Right context (5 words)
const rightEl = document.getElementById('rsvp-ctx-r');
if (rightEl) {
const rightWords = rsvpWords.slice(rsvpIndex + 1, rsvpIndex + 6);
rightEl.innerHTML = rightWords.map(w => `<span>${w}</span>`).join('');
}
const fill = document.querySelector('.rsvp-progress-fill');
if (fill) fill.style.width = ((rsvpIndex + 1) / rsvpWords.length * 100) + '%';
}
// Step RSVP by N words (negative = back)
function stepRsvpWord(n) {
rsvpIndex = (rsvpIndex + n + rsvpWords.length) % rsvpWords.length;
renderRsvpWord();
}
setInterval(() => {
if (rsvpPlaying && document.getElementById('screen-rsvp')?.classList.contains('active')) {
rsvpIndex = (rsvpIndex + 1) % rsvpWords.length;
renderRsvpWord();
}
}, 800);
// ============================================
// READER — card-by-card with word highlighting
// ============================================
let tpSpeed = 140;
let tpPlaying = false; // Start paused — user presses play
let readerWordIndex = -1;
let readerCardIndex = 0;
let countdownActive = false;
function changeTpSpeed(delta) {
tpSpeed = Math.max(80, Math.min(220, tpSpeed + delta));
const el = document.getElementById('tp-speed');
if (el) el.textContent = tpSpeed;
}
function toggleTp() {
if (countdownActive) return; // Ignore during countdown
if (tpPlaying) {
// Pause — keep current word highlighted, just stop advancing
tpPlaying = false;
clearTimeout(readerTimer);
const btn = document.getElementById('tp-play-btn');
if (btn) btn.innerHTML = '<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="5,3 19,12 5,21"/></svg>';
} else if (readerWordIndex > 0) {
// Resume from pause — no countdown, just continue
startPlaying();
} else {
// First start — full countdown 3-2-1
startCountdown();
}
}
function startCountdown() {
countdownActive = true;
const overlay = document.getElementById('rd-countdown');
if (!overlay) return startPlaying();
// Show overlay with dark backdrop first (pre-pause)
overlay.textContent = '';
overlay.classList.add('active');
overlay.classList.remove('rd-pulse');
// Flow: 3 → 2 → 1 → overlay gone → empty beat (no highlight) → first word lights up
setTimeout(() => {
let count = 3;
overlay.textContent = count;
const tick = setInterval(() => {
count--;
if (count > 0) {
overlay.textContent = count;
} else {
// "1" shown → clear overlay instantly
clearInterval(tick);
overlay.textContent = '';
overlay.classList.remove('active');
countdownActive = false;
// Empty beat — text visible but NO word highlighted
setTimeout(() => {
// First word lights up → startPlaying schedules
// the next word after a full 600ms interval
highlightFirstWord();
startPlaying();
}, 700);
}
}, 700);
}, 600);
}
// Light up the first word — visual cue that reading begins.
// Don't re-center: preCenterCard already positioned text for word 0.
function highlightFirstWord() {
const cards = getReaderCards();
const card = cards[readerCardIndex];
if (!card) return;
const words = card.querySelectorAll('.rd-w');
if (!words.length) return;
readerWordIndex = 0;
words[0].classList.add('rd-now');
const g = words[0].closest('.rd-g');
if (g) g.classList.add('rd-g-active');
}
function startPlaying() {
tpPlaying = true;
countdownActive = false;
const btn = document.getElementById('tp-play-btn');
if (btn) btn.innerHTML = '<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="6" y="4" width="4" height="16"/><rect x="14" y="4" width="4" height="16"/></svg>';
scheduleNextWord();
}
function getReaderCards() {
return document.querySelectorAll('.rd-cluster-wrap .rd-card');
}
function showCard(index) {
const cards = getReaderCards();
if (!cards.length) return;
cards.forEach((card, i) => {
card.classList.remove('rd-card-active', 'rd-card-prev', 'rd-card-next');
if (i === index) {
card.classList.add('rd-card-active');
} else if (i === index - 1) {
card.classList.add('rd-card-prev');
} else if (i === index + 1) {
card.classList.add('rd-card-next');
} else if (i < index) {
card.classList.add('rd-card-prev');
} else {
card.classList.add('rd-card-next');
}
});
// Update segment info and block indicator
goToSegment(index);
updateBlockIndicator();
}
// Center the active word's line at screen center
// Center the active word at the focal point (30% from top)
// smooth=true uses CSS transition, smooth=false is instant (no flash)
function centerActiveLine(smooth) {
const word = document.querySelector('.rd-card-active .rd-w.rd-now');
if (!word) return;
const text = word.closest('.rd-cluster-text');
if (!text) return;
const stage = document.querySelector('.rd-stage');
if (!stage) return;
// Disable transition to measure natural position without flash
text.style.transition = 'none';
const prevTy = text.style.transform;
text.style.transform = 'none';
void text.offsetHeight;
const stageRect = stage.getBoundingClientRect();
const wordRect = word.getBoundingClientRect();
const focalPoint = stageRect.top + stageRect.height * (rdFocalPct / 100);
const wordCenter = wordRect.top + wordRect.height / 2;
const ty = focalPoint - wordCenter;
if (smooth) {
// Restore previous position instantly, then animate to new
text.style.transform = prevTy || 'none';
void text.offsetHeight;
text.style.transition = '';
text.style.transform = `translateY(${ty}px)`;
} else {
// Set instantly (for new card entry — no jump)
text.style.transform = `translateY(${ty}px)`;
void text.offsetHeight;
requestAnimationFrame(() => { text.style.transition = ''; });
}
}
// Pre-position text on a card BEFORE it becomes visible
// so it slides in already centered — no jump after transition
function preCenterCard(card) {
if (!card) return;
const firstWord = card.querySelector('.rd-w');
if (!firstWord) return;
const text = card.querySelector('.rd-cluster-text');
if (!text) return;
const stage = document.querySelector('.rd-stage');
if (!stage) return;
// Temporarily make card visible but transparent to measure
const prevOpacity = card.style.opacity;
const prevTransform = card.style.transform;
card.style.opacity = '0';
card.style.transition = 'none';
card.style.transform = 'translateY(0)';
text.style.transition = 'none';
text.style.transform = 'none';
firstWord.classList.add('rd-now');
void text.offsetHeight;
const stageRect = stage.getBoundingClientRect();
const wordRect = firstWord.getBoundingClientRect();
const focalPoint = stageRect.top + stageRect.height * (rdFocalPct / 100);
const ty = focalPoint - (wordRect.top + wordRect.height / 2);
// Set text position and restore card to off-screen
text.style.transform = `translateY(${ty}px)`;
card.style.transform = prevTransform;
card.style.opacity = prevOpacity;
void card.offsetHeight;
card.style.transition = '';
text.style.transition = '';
}
function advanceReaderWord() {
const cards = getReaderCards();
if (!cards.length) return;
const activeCard = cards[readerCardIndex];
if (!activeCard) return;
const words = activeCard.querySelectorAll('.rd-w');
if (!words.length) return;
readerWordIndex++;
// If we've gone past all words in this card, move to next card
if (readerWordIndex >= words.length) {
readerWordIndex = -1;
readerCardIndex++;
if (readerCardIndex >= cards.length) {
readerCardIndex = 0;
cards.forEach(c => {
c.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-read', 'rd-now'));
c.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
const t = c.querySelector('.rd-cluster-text');
if (t) { t.style.transform = ''; t.dataset.ty = '0'; }
});
}
// Pre-center next card BEFORE it slides in — no jump
const nextCard = cards[readerCardIndex];
if (nextCard) preCenterCard(nextCard);
const wasPaused = !tpPlaying;
tpPlaying = false;
showCard(readerCardIndex);
// Wait for slide transition, then resume
setTimeout(() => {
readerWordIndex = 0;
if (!wasPaused) tpPlaying = true;
}, 850);
return;
}
words.forEach((w, i) => {
w.classList.remove('rd-now');
if (i < readerWordIndex) {
w.classList.add('rd-read');
} else if (i === readerWordIndex) {
w.classList.add('rd-now');
w.classList.remove('rd-read');
} else {
w.classList.remove('rd-read');
}
});
// Phrase-group highlighting: elevate the entire thought-group
// containing the current word so peripheral vision can preview
// upcoming words within the same semantic unit
activeCard.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
const currentWord = words[readerWordIndex];
if (currentWord) {
const parentGroup = currentWord.closest('.rd-g');
if (parentGroup) parentGroup.classList.add('rd-g-active');
}
// Center the active line on screen (smooth scroll)
centerActiveLine(true);
// Progress (global across all cards)
let totalWords = 0, totalRead = 0;
cards.forEach((c, ci) => {
const cw = c.querySelectorAll('.rd-w').length;
totalWords += cw;
if (ci < readerCardIndex) totalRead += cw;
else if (ci === readerCardIndex) totalRead += readerWordIndex;
});
const progress = totalWords ? (totalRead / totalWords * 100) : 0;
const fill = document.getElementById('rd-progress-fill');
if (fill) fill.style.width = progress + '%';
const timeEl = document.querySelector('.rd-time');
if (timeEl) {
const sec = Math.round((progress / 100) * 147);
timeEl.textContent = `${Math.floor(sec/60)}:${String(sec%60).padStart(2,'0')} / 2:27`;
}
}
// Font size control
let rdFontSize = 36;
function changeFontSize(delta) {
rdFontSize = Math.max(24, Math.min(56, rdFontSize + delta));
const wrap = document.querySelector('.rd-cluster-wrap');
if (wrap) wrap.style.setProperty('--rd-font-size', rdFontSize + 'px');
const label = document.getElementById('rd-font-label');
if (label) label.textContent = rdFontSize;
}
// Focal point control (vertical position of reading line)
let rdFocalPct = 30;
let focalGuideTimer = null;
function changeFocalPoint(val) {
rdFocalPct = parseInt(val);
const label = document.getElementById('rd-focal-val');
if (label) label.textContent = rdFocalPct + '%';
// Show horizontal guide line at the focal point
const guide = document.getElementById('rd-guide-h');
if (guide) {
guide.style.top = rdFocalPct + '%';
guide.classList.add('active');
clearTimeout(focalGuideTimer);
focalGuideTimer = setTimeout(() => guide.classList.remove('active'), 800);
}
// Re-center the active line at the new focal point
centerActiveLine(true);
}
// Text width control
let rdTextWidth = 750;
let widthGuideTimer = null;
function changeTextWidth(val) {
rdTextWidth = parseInt(val);
const wrap = document.querySelector('.rd-cluster-wrap');
if (wrap) wrap.style.maxWidth = rdTextWidth + 'px';
const label = document.getElementById('rd-width-val');
if (label) label.textContent = rdTextWidth;
// Show vertical guide lines at text edges
const stage = document.querySelector('.rd-stage');
const guideL = document.getElementById('rd-guide-v-l');
const guideR = document.getElementById('rd-guide-v-r');
if (stage && wrap && guideL && guideR) {
const stageRect = stage.getBoundingClientRect();
const wrapRect = wrap.getBoundingClientRect();
guideL.style.left = (wrapRect.left - stageRect.left) + 'px';
guideR.style.left = (wrapRect.right - stageRect.left) + 'px';
guideL.classList.add('active');
guideR.classList.add('active');
clearTimeout(widthGuideTimer);
widthGuideTimer = setTimeout(() => {
guideL.classList.remove('active');
guideR.classList.remove('active');
}, 800);
}
// Re-center after width change (line positions may shift)
centerActiveLine(true);
}
// Step forward/back by one word (manual word navigation)
function stepWord(direction) {
const cards = getReaderCards();
if (!cards.length) return;
const activeCard = cards[readerCardIndex];
if (!activeCard) return;
const words = activeCard.querySelectorAll('.rd-w');
if (!words.length) return;
if (direction > 0) {
// Forward — same as advanceReaderWord but manual
advanceReaderWord();
} else {
// Back — go to previous word
if (readerWordIndex > 0) {
// Un-read current word
const cur = words[readerWordIndex];
if (cur) cur.classList.remove('rd-now');
readerWordIndex--;
// Update states
words.forEach((w, i) => {
w.classList.remove('rd-now', 'rd-read');
if (i < readerWordIndex) w.classList.add('rd-read');
else if (i === readerWordIndex) w.classList.add('rd-now');
});
// Update phrase group
activeCard.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
const cw = words[readerWordIndex];
if (cw) {
const pg = cw.closest('.rd-g');
if (pg) pg.classList.add('rd-g-active');
}
centerActiveLine(true);
}
}
}
// Jump to next/prev card
function jumpCard(direction) {
const cards = getReaderCards();
// ── Back button: first press → start of current block,
// second press (if already at start) → previous block.
// Like a music player: tap back = restart song, tap again = prev song.
if (direction < 0) {
if (readerWordIndex > 1) {
// Not at start yet — rewind to beginning of current block
const card = cards[readerCardIndex];
if (card) {
card.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-read', 'rd-now'));
card.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
const t = card.querySelector('.rd-cluster-text');
if (t) { t.style.transform = ''; t.dataset.ty = '0'; }
preCenterCard(card);
// Remove rd-now from preCenterCard measurement
card.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-now'));
card.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
}
readerWordIndex = 0;
highlightFirstWord();
return;
}
// Already at start — fall through to jump to previous block
}
const newIndex = readerCardIndex + direction;
if (newIndex < 0 || newIndex >= cards.length) return;
// Reset words and text position in current card
const currentCard = cards[readerCardIndex];
if (currentCard) {
currentCard.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-read', 'rd-now'));
currentCard.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
const t = currentCard.querySelector('.rd-cluster-text');
if (t) { t.style.transform = ''; t.dataset.ty = '0'; }
}
// Pre-center target card before it slides in
const targetCard = cards[newIndex];
if (targetCard) {
targetCard.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-read', 'rd-now'));
targetCard.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
const t = targetCard.querySelector('.rd-cluster-text');
if (t) { t.style.transform = ''; t.dataset.ty = '0'; }
preCenterCard(targetCard);
// Remove rd-now from preCenterCard measurement
targetCard.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-now'));
}
const wasPaused = !tpPlaying;
tpPlaying = false;
readerCardIndex = newIndex;
readerWordIndex = 0;
showCard(readerCardIndex);
// Wait for slide transition, then resume
setTimeout(() => {
if (!wasPaused) tpPlaying = true;
}, 850);
}
// Update block indicator
function updateBlockIndicator() {
const el = document.getElementById('rd-block-indicator');
const cards = getReaderCards();
if (el) el.textContent = (readerCardIndex + 1) + ' / ' + cards.length;
}
function resetReader() {
readerWordIndex = -1;
readerCardIndex = 0;
tpPlaying = false; // Start paused
countdownActive = false;
const cards = getReaderCards();
cards.forEach(c => {
c.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-read', 'rd-now'));
c.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
const t = c.querySelector('.rd-cluster-text');
if (t) { t.style.transform = ''; t.dataset.ty = '0'; }
});
// Pre-center first card, then show it — no jump
const firstCard = cards[0];
if (firstCard) preCenterCard(firstCard);
// Remove rd-now that preCenterCard added for measurement —
// no word should be bright until countdown finishes
cards.forEach(c => {
c.querySelectorAll('.rd-w').forEach(w => w.classList.remove('rd-now'));
c.querySelectorAll('.rd-g').forEach(g => g.classList.remove('rd-g-active'));
});
showCard(0);
// Show play button (paused state)
const btn = document.getElementById('tp-play-btn');
if (btn) btn.innerHTML = '<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="5,3 19,12 5,21"/></svg>';
// Reset progress
const fill = document.getElementById('rd-progress-fill');
if (fill) fill.style.width = '0%';
const timeEl = document.querySelector('.rd-time');
if (timeEl) timeEl.textContent = '0:00 / 2:27';
}
// Auto-advance reader — timeout chain guarantees each word gets full duration.
// setInterval would fire on a fixed clock, cutting words short on resume.
let readerTimer = null;
function scheduleNextWord() {
clearTimeout(readerTimer);
readerTimer = setTimeout(() => {
if (tpPlaying && document.getElementById('screen-teleprompter')?.classList.contains('active')) {
advanceReaderWord();
}
if (tpPlaying) scheduleNextWord();
}, 600);
}
// Camera toggle
function toggleReaderCamera() {
const cam = document.getElementById('rd-camera');
const tint = document.getElementById('rd-camera-tint');
const btn = document.getElementById('rd-cam-btn');
cam.classList.toggle('active');
tint.classList.toggle('active');
btn.classList.toggle('active');
}
// Focus mode toggle
function toggleFocusMode() {
const btn = document.getElementById('rd-focus-btn');
btn.classList.toggle('active');
// Could hide header/footer for immersive mode
}
// ============================================
// KEYBOARD SHORTCUTS
// ============================================
// ── Segment navigation ──
// Demo segments for prototype — in production these come from the TPS parser
const demoSegments = [
{ name: 'Intro', emotion: 'warm', wpm: 140, color: '#F59E0B', bg: 'warm' },
{ name: 'Problem', emotion: 'concerned', wpm: 150, color: '#EF4444', bg: 'concerned' },
{ name: 'Solution', emotion: 'focused', wpm: 160, color: '#10B981', bg: 'focused' },
];
let currentSegmentIndex = 0;
function goToSegment(index) {
if (index < 0) index = 0;
if (index >= demoSegments.length) index = demoSegments.length - 1;
currentSegmentIndex = index;
const seg = demoSegments[index];
// Update gradient background with smooth transition
const gradient = document.getElementById('rd-gradient');
if (gradient) {
gradient.className = 'rd-gradient';
if (seg.bg) gradient.classList.add(seg.bg);
}
// Update edge section label
const sectionEl = document.querySelector('.rd-edge-section');
if (sectionEl) sectionEl.textContent = seg.name;
// Update header subtitle
const headerSeg = document.getElementById('rd-header-segment');
if (headerSeg) headerSeg.textContent = seg.name + ' · ' + seg.emotion.charAt(0).toUpperCase() + seg.emotion.slice(1);
// Update segment indicator colors
const segs = document.querySelectorAll('.rd-edge-segs > div');
segs.forEach((s, i) => {
s.style.opacity = i === index ? '1' : '0.4';
});
}
function nextSegment() { goToSegment(currentSegmentIndex + 1); }
function prevSegment() { goToSegment(currentSegmentIndex - 1); }
// ── Keyboard shortcuts ──
document.addEventListener('keydown', (e) => {
const isTp = document.getElementById('screen-teleprompter')?.classList.contains('active');
const isRsvp = document.getElementById('screen-rsvp')?.classList.contains('active');
if (e.key === 'Escape') {
const active = document.querySelector('.screen.active');
if (active && active.id !== 'screen-library') {
if (isTp || isRsvp) {
navigateTo('editor');
} else {
navigateTo('library');
}
}
return;
}
// Only process shortcuts when in teleprompter or RSVP mode
if (!isTp && !isRsvp) return;
if (e.target.matches('input, [contenteditable]')) return;
switch (e.key) {
// ── Play / Pause ──
case ' ':
e.preventDefault();
if (isRsvp) toggleRsvp();
if (isTp) toggleTp();
break;
// ── Segment navigation ──
case 'ArrowRight':
case 'PageDown':
e.preventDefault();
if (isTp) nextSegment();
break;
case 'ArrowLeft':
case 'PageUp':
e.preventDefault();
if (isTp) prevSegment();
break;
// ── Speed ──
case 'ArrowUp':
e.preventDefault();
if (isTp) changeTpSpeed(10);
if (isRsvp) changeRsvpSpeed(10);
break;
case 'ArrowDown':
e.preventDefault();
if (isTp) changeTpSpeed(-10);
if (isRsvp) changeRsvpSpeed(-10);
break;
// ── Camera toggle ──
case 'c':
case 'C':
if (isTp) toggleReaderCamera();
break;
// ── Focus mode ──
case 'f':
case 'F':
if (isTp) toggleFocusMode();
break;
}
});
// ============================================
// SETTINGS — section switching
// ============================================
function showSetSection(id) {
document.querySelectorAll('.set-panel').forEach(p => p.style.display = 'none');
const target = document.getElementById('set-' + id);
if (target) target.style.display = '';
document.querySelectorAll('.set-nav-item').forEach(n => n.classList.remove('active'));
event.currentTarget.classList.add('active');
}
// ============================================
// SETTINGS — Save / Discard banner
// ============================================
let _settingsDirty = false;
function markSettingsDirty() {
if (_settingsDirty) return;
_settingsDirty = true;
const banner = document.getElementById('set-save-banner');
if (banner) banner.classList.add('visible');
}
function saveSettings() {
_settingsDirty = false;
const banner = document.getElementById('set-save-banner');
if (banner) banner.classList.remove('visible');
// Flash "Saved" feedback on banner before hiding
const textEl = banner && banner.querySelector('.set-save-banner-text');
if (textEl) {
textEl.textContent = 'Changes saved';
setTimeout(() => { textEl.textContent = 'Unsaved changes'; }, 1500);
}
}
function discardSettings() {
_settingsDirty = false;
const banner = document.getElementById('set-save-banner');
if (banner) banner.classList.remove('visible');
}
// ============================================
// EDITOR — Structure navigation
// ============================================
function edNavTo(el) {
const nav = el.dataset.nav;
if (!nav) return;
const parts = nav.split('-');
const segs = document.querySelectorAll('.ed-content .ed-seg');
let target = null;
if (parts[0] === 'seg') {
const segIdx = parseInt(parts[1]);
target = segs[segIdx];
} else if (parts[0] === 'blk') {
const segIdx = parseInt(parts[1]);
const blkIdx = parseInt(parts[2]);
if (segs[segIdx]) {
const blocks = segs[segIdx].querySelectorAll('.ed-blk');
target = blocks[blkIdx];
}
}
if (target) {
const container = document.querySelector('.ed-content');
if (container) {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// Flash highlight
target.classList.add('ed-nav-flash');
setTimeout(() => target.classList.remove('ed-nav-flash'), 1200);
}
// Update active state in tree
document.querySelectorAll('.ed-tree-seg').forEach(s => s.classList.remove('active'));
document.querySelectorAll('.ed-tree-block').forEach(b => b.classList.remove('active'));
el.classList.add('active');
// If clicked on a block, also activate its parent segment
if (parts[0] === 'blk') {
const parentSeg = document.querySelector(`[data-nav="seg-${parts[1]}"]`);
if (parentSeg) parentSeg.classList.add('active');
}
}
// ============================================
// GO-LIVE (Director Studio)
// ============================================
let glRecording = false;
let glStreaming = false;
let glStudioMode = 'director'; // 'director' or 'studio'
// ── Studio Mode (Director / Studio) ──
function setStudioMode(mode) {
glStudioMode = mode;
// Update buttons
document.querySelectorAll('.gl-mode-btn').forEach(btn => {
btn.classList.remove('active');
});
const activeBtn = document.querySelector(`.gl-mode-${mode}`);
if (activeBtn) activeBtn.classList.add('active');
// Show/hide director controls (only in director mode - room management)
const roomSection = document.querySelector('.gl-room-section');
if (roomSection) {
roomSection.style.display = mode === 'director' ? '' : 'none';
}
// In studio mode, simplify camera list
const camCards = document.querySelectorAll('.gl-cam-card:not(.gl-src-screen)');
camCards.forEach((card, i) => {
if (mode === 'studio') {
// In studio mode, only show first camera (your camera) + screen share always visible
card.style.display = i === 0 ? '' : 'none';
} else {
card.style.display = '';
}
});
// Update sources header
const sourcesHeader = document.querySelector('.gl-sources-header .section-label');
if (sourcesHeader) {
sourcesHeader.textContent = mode === 'director' ? 'CAMERAS' : 'SOURCES';
}
}
// ── Room Code Management ──
const roomJoinPathPrefix = '/join/';
function buildRoomInviteUrl(code) {
return new URL(`${roomJoinPathPrefix}${code}`, window.location.origin).toString();
}
function generateRoomCode() {
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
let code = '';
for (let i = 0; i < 3; i++) code += chars[Math.floor(Math.random() * chars.length)];
code += '-';
for (let i = 0; i < 3; i++) code += chars[Math.floor(Math.random() * chars.length)];
return code;
}
function copyRoomCode() {
const code = document.getElementById('gl-room-code')?.textContent;
if (code) {
navigator.clipboard.writeText(buildRoomInviteUrl(code)).then(() => {
const btn = document.querySelector('.gl-room-copy-btn');
if (btn) {
btn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg>';
setTimeout(() => {
btn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>';
}, 2000);
}
});
}
}
function createRoom() {
// Generate room code
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
let code = '';
for (let i = 0; i < 3; i++) code += chars[Math.floor(Math.random() * chars.length)];
code += '-';
for (let i = 0; i < 3; i++) code += chars[Math.floor(Math.random() * chars.length)];
const codeEl = document.getElementById('gl-room-code');
if (codeEl) codeEl.textContent = code;
// Toggle states
document.getElementById('gl-room-empty').style.display = 'none';
document.getElementById('gl-room-active').style.display = 'flex';
}
function endRoom() {
document.getElementById('gl-room-active').style.display = 'none';
document.getElementById('gl-room-empty').style.display = 'flex';
}
function copyRoomInvite() {
const code = document.getElementById('gl-room-code')?.textContent;
if (code) {
navigator.clipboard.writeText(buildRoomInviteUrl(code)).catch(() => {});
const btn = document.querySelector('.gl-room-invite-btn');
if (btn) {
const orig = btn.innerHTML;
btn.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg> Link Copied!';
setTimeout(() => { btn.innerHTML = orig; }, 2000);