forked from ReKindleOS/ReKindle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairtype.html
More file actions
1419 lines (1236 loc) · 53.7 KB
/
Copy pathairtype.html
File metadata and controls
1419 lines (1236 loc) · 53.7 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
<!DOCTYPE html><html lang="en"><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
<meta charset="UTF-8">
<title>AirType</title>
<!-- Firebase SDKs -->
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-database-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-auth-compat.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script src="https://unpkg.com/html5-qrcode" type="text/javascript"></script>
<script src="js/i18n.js"></script>
<style>
/* SYSTEM 7 AESTHETICS */
:root {
--bg-color: #ffffff;
--text-color: #000000;
--border: 2px solid #000000;
--shadow: 4px 4px 0px #000000;
--stripe-pattern: repeating-linear-gradient(0deg, transparent, transparent 2px, #000 3px, #000 4px);
--kindle-font: "Bookerly", "Georgia", serif;
--ui-font: "Geneva", "Verdana", sans-serif;
}
body {
font-family: var(--ui-font);
margin: 0;
padding: 20px;
height: 100vh;
overflow: hidden;
background-color: #e5e5e5;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
user-select: none;
box-sizing: border-box;
/* Pixel Art Rendering */
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
}
/* WINDOW FRAME */
.window {
background: white;
border: 2px solid black;
box-shadow: var(--shadow);
display: flex;
flex-direction: column;
width: 95%;
height: 90vh;
max-width: 800px;
position: relative;
}
/* FOR MONITOR MODE: FULL SCREEN WINDOW */
.window.full-monitor {
width: 100%;
height: 100vh;
max-width: none;
border: none;
box-shadow: none;
padding: 0;
margin: -20px;
/* Counteract body padding */
}
/* TITLE BAR */
.title-bar {
height: 35px;
border-bottom: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
background: white;
padding: 0 10px;
position: relative;
flex-shrink: 0;
}
.title-stripes {
position: absolute;
top: 4px;
bottom: 4px;
left: 4px;
right: 4px;
background-image: var(--stripe-pattern);
z-index: 0;
}
.title-text {
background: white;
padding: 0 15px;
font-weight: bold;
z-index: 1;
font-size: 1.1rem;
}
/* CLOSE BUTTON */
.close-box {
position: absolute;
left: 10px;
width: 18px;
height: 18px;
border: 2px solid black;
background: white;
z-index: 2;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-family: sans-serif;
font-size: 0.9rem;
line-height: 1;
box-shadow: 2px 2px 0 black;
}
.close-box:active {
background: black;
color: white;
transform: translate(1px, 1px);
box-shadow: none;
}
/* SYSTEM BUTTON */
.sys-btn {
background: white;
border: 2px solid black;
padding: 10px 20px;
font-weight: bold;
cursor: pointer;
box-shadow: 2px 2px 0 black;
font-size: 1rem;
font-family: var(--ui-font);
}
.sys-btn:active {
background: black;
color: white;
transform: translate(2px, 2px);
box-shadow: none;
}
/* MINI BTN for Title Bar */
.mini-btn {
background: white;
border: 2px solid black;
padding: 2px 8px;
font-weight: bold;
cursor: pointer;
box-shadow: 2px 2px 0 black;
font-size: 0.8rem;
font-family: var(--ui-font);
height: 24px;
display: flex;
align-items: center;
justify-content: center;
}
.mini-btn:active {
background: black;
color: white;
transform: translate(2px, 2px);
box-shadow: none;
}
/* APP CONTROLS (Right side of Title Bar) */
.app-controls {
position: absolute;
right: 10px;
z-index: 2;
display: none;
/* Hidden by default (Phone) */
gap: 5px;
}
.window.fullscreen-mode {
position: fixed;
top: 0;
left: 0;
width: calc(100vw / var(--rekindle-scale, 1));
height: calc(100vh / var(--rekindle-scale, 1));
max-width: none;
max-height: none;
z-index: 2000;
box-shadow: none;
border: 2px solid black;
box-sizing: border-box;
transform-origin: top left;
}
.sys-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
box-shadow: none;
transform: translate(2px, 2px);
}
.icon-btn {
width: 24px;
height: 24px;
border: 2px solid black;
background: white;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
box-shadow: 2px 2px 0 black;
padding: 0;
flex-shrink: 0;
font-family: var(--ui-font);
}
.icon-btn:active {
background: black;
color: white;
transform: translate(1px, 1px);
box-shadow: none;
}
/* Icon State Toggles */
.icon-contract {
display: none;
}
.window.fullscreen-mode .icon-expand {
display: none;
}
.window.fullscreen-mode .icon-contract {
display: inline;
}
/* CONTENT VIEWS */
.view-content {
flex-grow: 1;
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
}
/* NOTE LIST VIEW (Replaces Landing) */
#note-list-view {
overflow-y: auto;
align-items: stretch;
/* List items take full width */
}
.note-item {
padding: 15px;
border-bottom: 1px solid #ccc;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}
.note-item:hover {
background-color: #f0f0f0;
}
.note-title {
font-weight: bold;
font-size: 1.1rem;
}
.note-date {
font-size: 0.8rem;
color: #666;
}
/* KEYBOARD VIEW */
#keyboard-view {
display: none;
}
.info-bar {
background: #eee;
border-bottom: 2px solid black;
padding: 10px;
font-size: 0.9rem;
display: flex;
justify-content: space-between;
align-items: center;
flex-shrink: 0;
}
.code-box {
font-family: "Courier New", monospace;
font-weight: bold;
font-size: 1.1rem;
background: white;
border: 2px solid black;
padding: 2px 8px;
}
#title-input {
width: 100%;
border: none;
border-bottom: 2px solid black;
font-family: var(--ui-font);
font-size: 1.2rem;
font-weight: bold;
padding: 10px;
box-sizing: border-box;
outline: none;
background: #fff;
}
#editor-wrapper {
flex-grow: 1;
padding: 15px;
display: flex;
flex-direction: column;
}
textarea {
width: 100%;
height: 100%;
border: none;
resize: none;
font-family: var(--kindle-font);
font-size: 1.3rem;
line-height: 1.5;
outline: none;
flex-grow: 1;
/* Expand to fill space */
}
/* CONNECT VIEW */
#connect-view {
display: none;
align-items: center;
justify-content: center;
gap: 20px;
}
.connect-input {
font-size: 2rem;
text-align: center;
letter-spacing: 5px;
border: 2px solid black;
padding: 10px;
width: 200px;
font-family: "Courier New", monospace;
box-shadow: 2px 2px 0 #ccc;
}
/* MONITOR VIEW */
#monitor-view {
display: none;
padding: 40px;
/* Kindle margins */
overflow-y: auto;
background: white;
}
#paper {
font-family: var(--kindle-font);
font-size: 2rem;
/* Big for Kindle */
line-height: 1.4;
max-width: 900px;
margin: 0 auto;
white-space: pre-wrap;
padding-bottom: 80vh;
/* Allow generous scrolling past end */
}
/* CURSOR */
#cursor-caret {
display: inline-block;
width: 12px;
height: 1.8rem;
background: black;
vertical-align: text-bottom;
margin-left: 2px;
animation: blink 1s step-end infinite;
}
/* MODALS */
#modal-overlay {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.9);
z-index: 999;
align-items: center;
justify-content: center;
/* Fix for Title Bar Visibility */
top: 35px;
height: calc(100% - 35px);
}
.modal-box {
background: white;
border: 2px solid black;
box-shadow: 8px 8px 0 black;
padding: 20px;
width: 350px;
text-align: center;
}
.modal-buttons {
display: flex;
justify-content: center;
margin-top: 15px;
}
.modal-buttons > * + * {
margin-left: 10px;
}
@keyframes blink {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0;
}
}
/* GUEST OVERLAY */
#guest-overlay {
position: absolute;
top: 35px;
/* Below title bar */
bottom: 0;
left: 0;
right: 0;
background: rgba(255, 255, 255, 0.95);
z-index: 3000;
display: none;
/* Default hidden */
align-items: center;
justify-content: center;
flex-direction: column;
text-align: center;
padding: 20px;
}
#guest-overlay h2 {
margin-top: 0;
font-size: 1.5rem;
}
</style>
<script src="theme.js"></script>
<script src="pro-gate.js"></script>
</head>
<body>
<div class="window" id="main-window">
<!-- PAYWALL OVERLAY -->
<div id="paywall-overlay" style="display:none; position:absolute; top:35px; left:0; width:100%; height:calc(100% - 35px); background:rgba(255,255,255,0.98); z-index:9000; flex-direction:column; justify-content:center; align-items:center; text-align:center; padding:20px; box-sizing:border-box;">
<h2 style="margin-top:0;" data-i18n="airtype.paywall.title">ReKindle+</h2>
<p data-i18n="airtype.paywall.body">Support ReKindle development and get<br>access to exclusive apps.</p>
<button class="sys-btn" onclick="window.location.href='pay.html'" data-i18n="airtype.paywall.btn">Subscribe
/ Login</button>
<p style="font-size:0.8rem; margin-top:20px; color:#666;" id="paywall-status" data-i18n="airtype.paywall.status">Checking subscription...</p>
</div>
<!-- TITLE BAR -->
<div class="title-bar" id="app-title-bar">
<div class="title-stripes"></div>
<div class="close-box" onclick="tryExit()">X</div>
<span class="title-text">AirType</span>
<!-- RESET DEVICE TYPE BUTTON -->
<button class="mini-btn" style="position:absolute; right:10px; z-index:2;" onclick="resetDeviceType()">Reset</button>
<!-- GLOBAL APP CONTROLS -->
<div class="app-controls" id="top-controls">
<button class="mini-btn" onclick="changeFontSize(-2)">A-</button>
<button class="mini-btn" onclick="changeFontSize(2)">A+</button>
<button class="icon-btn" onclick="toggleFullScreen()" title="Full Screen">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="square" stroke-linejoin="miter">
<path class="icon-expand" d="M15 3h6v6M14 10l6.1-6.1M9 21H3v-6M10 14l-6.1 6.1"></path>
<path class="icon-contract" d="M21 9H15V3M21 3l-6 6M3 15h6v6M3 21l6-6"></path>
</svg>
</button>
</div>
</div>
<!-- VIEW 1: NOTE LIST (DEFAULT) -->
<div id="note-list-view" class="view-content">
<div style="padding:10px; border-bottom:1px solid #ccc; background:#f9f9f9;">
<button class="sys-btn" style="width:100%;" onclick="handleNewNote()" data-i18n="airtype.btn.new_note">+
New Note</button>
</div>
<div id="note-list-container">
<div style="padding:20px; text-align:center; color:#666;" data-i18n="airtype.status.loading">Loading
notes...</div>
</div>
</div>
<!-- VIEW 2: KEYBOARD (HOST) -->
<div id="keyboard-view" class="view-content">
<div class="info-bar">
<span><span data-i18n="airtype.label.session">Session:</span> <span id="host-code" class="code-box">...</span></span>
<span id="sync-indicator" data-i18n="airtype.status.ready">Ready</span>
<span id="ping-ticker" style="font-size: 0.8rem; color: #666; margin-left:10px;"></span>
</div>
<div id="editor-wrapper">
<input type="text" id="title-input" placeholder="Untitled Story" oninput="handleInput()" data-i18n-placeholder="airtype.placeholder.title">
<textarea id="input-area" placeholder="Type here..." oninput="handleInput()" data-i18n-placeholder="airtype.placeholder.body"></textarea>
</div>
</div>
<!-- VIEW 3: CONNECT (MONITOR SETUP) -->
<div id="connect-view" class="view-content">
<h2 data-i18n="airtype.header.enter_code">Enter Code</h2>
<input type="text" id="connect-code-input" class="connect-input" maxlength="6" placeholder="000000" data-i18n-placeholder="airtype.placeholder.code">
<div style="display: flex">
<button class="sys-btn" onclick="connectToSession()" data-i18n="airtype.btn.connect">Connect</button>
<button class="sys-btn" onclick="location.reload()" data-i18n="airtype.btn.back" style="margin-left: 10px">Back</button>
</div>
</div>
<!-- VIEW 3C: SCANNER VIEW -->
<div id="scanner-view" class="view-content" style="display:none; flex-direction:column; align-items:center; justify-content:center;">
<h2 data-i18n="airtype.header.scan">Scan QR Code</h2>
<div id="reader" style="width:300px; height:300px; border:2px solid black;"></div>
<button class="sys-btn" style="margin-top:20px;" onclick="stopScanner()" data-i18n="airtype.btn.cancel">Cancel</button>
</div>
<!-- VIEW 3B: QR CONNECT SETUP -->
<!-- This is now used as an overlay or switched view for Tablets/Monitors -->
<div id="qr-setup-view" class="view-content" style="display:none; align-items:center; justify-content:center; flex-direction:column; gap:20px; text-align:center;">
<h2 id="qr-status-text" data-i18n="airtype.header.qr_setup">Scan to Connect</h2>
<div id="qrcode" style="padding:10px; background:white; border:2px solid black;"></div>
<p style="font-family:var(--kindle-font); max-width:300px;" data-i18n="airtype.body.qr_setup">Scan this with
your phone to start writing
immediately.</p>
<div style="font-size:1.5rem; font-family:'Courier New'; font-weight:bold; letter-spacing:2px;" id="qr-session-code"></div>
<button class="sys-btn" onclick="location.reload()" data-i18n="airtype.btn.cancel">Cancel</button>
</div>
<!-- VIEW 4: MONITOR (PAPER) -->
<div id="monitor-view" class="view-content">
<!-- Content injected here -->
<h1 id="paper-title" style="text-align:center; margin-bottom: 1rem; display:none;"></h1>
<div id="paper"></div>
</div>
<!-- GUEST OVERLAY HTML -->
<div id="guest-overlay">
<h2 data-i18n="airtype.guest.title">You need to be logged in to use AirType</h2>
<br>
<button class="sys-btn" onclick="window.location.href='index?login=true'" data-i18n="airtype.btn.login">Log
In</button>
</div>
<!-- GENERIC MODAL -->
<div id="modal-overlay">
<div class="modal-box" id="modal-content">
<h3 id="modal-title" data-i18n="airtype.modal.alert">Alert</h3>
<p id="modal-text">Message</p>
<div class="modal-buttons" id="modal-actions">
<button class="sys-btn" onclick="closeModal()" data-i18n="airtype.btn.ok">OK</button>
</div>
</div>
</div>
</div>
<!-- SCRIPT -->
<script>
// --- CONFIG ---
const firebaseConfig = {
apiKey: "AIzaSyDY7x7vVmlYUyVZNLuCCmIQYa6PWFVfZqQ",
authDomain: "rekindle-dd1fa.firebaseapp.com",
projectId: "rekindle-dd1fa",
storageBucket: "rekindle-dd1fa.firebasestorage.app",
messagingSenderId: "748026882518",
appId: "1:748026882518:web:6877dd4329318070c11c77",
databaseURL: "https://rekindle-dd1fa-default-rtdb.firebaseio.com/"
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
// Configure for Safari/Mac compatibility (fixes CORS errors)
try {
db.settings({
experimentalForceLongPolling: true,
merge: true
});
} catch (e) {
// This might still error if another script called it first, but we catch it silently
console.warn("Firestore settings already initialized:", e);
}
const rtdb = firebase.database();
const auth = firebase.auth();
// --- STATE ---
let currentUser = null;
let sessionId = null;
let typeTimer = null;
let saveTimer = null; // Separate timer for cloud save
let sessionRef = null; // Ref for Monitor Sync
let noteRef = null; // Ref for Persistent Storage
let baseFontSize = 32; // Default 2rem = 32px approx, keep track in px for easier math
let notes = [];
let html5QrCode = null;
let selectedNoteData = null; // Store note data when clicked
let userDeviceType = null; // 'Phone' or 'Tablet'
let initialJoinAttempted = false;
let isPro = false;
function checkSubscription() {
const overlay = document.getElementById('paywall-overlay');
const status = document.getElementById('paywall-status');
// Buttons to update
const btnDisplay = document.getElementById('btn-startup-display');
const btnInput = document.getElementById('btn-startup-input');
function updateButtons(pro) {
if (btnDisplay) btnDisplay.disabled = !pro;
if (btnInput) btnInput.disabled = !pro;
}
// 1. CACHE CHECK (Instant)
try {
const cachedExpiry = localStorage.getItem('rekindle_pro_expiry');
if (cachedExpiry && parseInt(cachedExpiry) > Date.now()) {
isPro = true;
updateButtons(true);
overlay.style.display = 'none';
}
} catch (e) { }
if (typeof firebase === 'undefined') {
status.innerText = window.t('airtype.modal.error.firebase', "Error: Firebase not loaded.");
overlay.style.display = 'flex';
return;
}
if (!currentUser) {
// Not logged in is handled by guest overlay usually
overlay.style.display = 'none';
return;
}
// Timeout fallback
const timeoutId = setTimeout(() => {
if (!isPro) {
console.warn("Subscription check timed out");
// Don't block, just leave disabled
overlay.style.display = 'none';
}
}, 10000);
db.collection('users').doc(currentUser.uid).get()
.then(doc => {
clearTimeout(timeoutId);
if (doc.exists) {
const data = doc.data();
const now = Date.now();
let expires = 0;
if (data.proExpiresAt) {
expires = typeof data.proExpiresAt.toMillis === 'function'
? data.proExpiresAt.toMillis()
: new Date(data.proExpiresAt).getTime();
}
// UPDATE CACHE
if (expires > now) {
localStorage.setItem('rekindle_pro_expiry', expires.toString());
} else {
localStorage.removeItem('rekindle_pro_expiry');
}
if (expires > now) {
isPro = true;
updateButtons(true);
} else {
isPro = false;
updateButtons(false);
}
} else {
isPro = false;
updateButtons(false);
}
// Hide overlay regardless, access controlled by buttons
overlay.style.display = 'none';
})
.catch(e => {
clearTimeout(timeoutId);
console.error("Error checking sub", e);
// Hide overlay, features disabled
overlay.style.display = 'none';
});
}
// --- AUTH & INIT ---
auth.onAuthStateChanged(user => {
currentUser = user;
// Check for join intent (URL params)
const urlParams = new URLSearchParams(window.location.search);
const modeParam = urlParams.get('mode');
const sessionParam = urlParams.get('session');
const isJoining = (modeParam === 'host' && sessionParam);
if (user) {
document.getElementById('guest-overlay').style.display = 'none';
loadCloudNotes();
checkSubscription();
// If we are joining via URL and haven't done so yet
if (isJoining && !initialJoinAttempted) {
initialJoinAttempted = true;
startHostWithId(sessionParam);
}
} else {
document.getElementById('guest-overlay').style.display = 'flex';
// renderNoteList(); // Disabled for guests
}
});
// --- NAVIGATION ---
function switchView(viewId) {
document.querySelectorAll('.view-content').forEach(el => el.style.display = 'none');
// Flex for everything centered, Block for Paper/Notes
const flexViews = ['connect-view', 'qr-setup-view', 'scanner-view'];
document.getElementById(viewId).style.display = flexViews.includes(viewId) ? 'flex' : 'block';
// Show controls ONLY in Monitor View
const controls = document.getElementById('top-controls');
if (controls) {
controls.style.display = (viewId === 'monitor-view') ? 'flex' : 'none';
}
}
function tryExit() {
window.location.href = 'index';
}
// --- WALLPAPER LOGIC ---
function sanitizeBackgroundImage(imageString) {
if (!imageString) return '';
if (imageString.startsWith('url("data:image/png;base64,')) {
return imageString;
}
if (imageString.startsWith('url("') && imageString.endsWith('")')) {
const url = imageString.substring(5, imageString.length - 2);
if ((url.startsWith('http://') || url.startsWith('https://') || url.startsWith('/')) && !url.includes('javascript:')) {
return imageString;
}
}
return '';
}
// --- MODAL UTILS ---
function showModal(title, text, buttonsHtml) {
document.getElementById('modal-title').innerText = title;
document.getElementById('modal-text').innerText = text;
if (buttonsHtml) {
document.getElementById('modal-actions').innerHTML = buttonsHtml;
} else {
document.getElementById('modal-actions').innerHTML = '<button class="sys-btn" onclick="closeModal()">OK</button>';
}
document.getElementById('modal-overlay').style.display = 'flex';
}
function closeModal() {
document.getElementById('modal-overlay').style.display = 'none';
}
// --- DEVICE DETECTION & ENTRY FLOW ---
function showStartupDetection() {
// Check persistence first
const savedType = localStorage.getItem('rekindle_airtype_device_type');
if (savedType) {
onStartupConfirmed(savedType);
return;
}
// Map internal values: Display -> 'Tablet', Input Device -> 'Phone'
// Added IDs and initial disabled state rooted in isPro logic
const disabledAttr = isPro ? '' : 'disabled';
const displayLabel = window.t('airtype.modal.setup.display', "Display");
const inputLabel = window.t('airtype.modal.setup.input', "Input Device");
const btnDisplay = `<button id="btn-startup-display" class="sys-btn" onclick="onStartupConfirmed('Tablet')" ${disabledAttr}>${displayLabel}</button>`;
const btnInput = `<button id="btn-startup-input" class="sys-btn" onclick="onStartupConfirmed('Phone')" ${disabledAttr}>${inputLabel}</button>`;
const setupTitle = window.t('airtype.modal.setup_title', "Device Setup");
const setupBody = window.t('airtype.modal.setup_body', "Will you use this device as a display or as an input device?");
showModal(setupTitle, setupBody, `${btnDisplay} ${btnInput}`);
}
function onStartupConfirmed(type) {
userDeviceType = type;
localStorage.setItem('rekindle_airtype_device_type', type);
closeModal();
if (type === 'Phone') {
showPhoneOptions();
}
// Tablet just stays on Note List
}
// Reset device type and re-prompt
function resetDeviceType() {
localStorage.removeItem('rekindle_airtype_device_type');
userDeviceType = null;
showStartupDetection();
}
// Triggered by Note Click or New Note
function initEntryFlow(noteData = null) {
selectedNoteData = noteData; // Null means New Note
if (!userDeviceType) {
// Should not happen if flow is correct, but fallback
showStartupDetection();
return;
}
if (userDeviceType === 'Tablet') {
// Tablet logic: Immediate QR
startMonitorQR();
} else {
// Phone logic: Scan or Code
showPhoneOptions();
}
}
function showPhoneOptions() {
// Host Mode Options
const enterLabel = window.t('airtype.header.enter_code', "Enter Code");
const scanLabel = window.t('airtype.header.scan', "Scan QR Code");
const btnEnter = `<button class="sys-btn" onclick="closeModal(); startMonitor();">${enterLabel}</button>`; // Reusing logic but naming it Enter Code
const btnScan = `<button class="sys-btn" onclick="closeModal(); startScanFlow();">${scanLabel}</button>`;
const connectTitle = window.t('airtype.modal.connect_title', "Connect to Display");
const connectBody = window.t('airtype.modal.connect_body', "How do you want to connect?");
showModal(connectTitle, connectBody, `${btnEnter} ${btnScan}`);
}
function startScanFlow() {
switchView('scanner-view');
html5QrCode = new Html5Qrcode("reader");
const config = { fps: 10, qrbox: { width: 250, height: 250 } };
html5QrCode.start({ facingMode: "environment" }, config, onScanSuccess);
}
function onScanSuccess(decodedText, decodedResult) {
// Handle the scanned code
// Expected format: URL?mode=host&session=ID or just URL
// Or maybe just the session ID if we want to be simple?
// The generator makes a full URL. Let's parse it.
stopScanner(false); // Stop scanning, but DO NOT navigate away yet
try {
const url = new URL(decodedText);
const session = url.searchParams.get('session');
if (session) {
startHostWithId(session);
} else {
const errTitle = window.t('airtype.modal.alert', "Error");
const errMsg = window.t('airtype.modal.error.qr_format', "Invalid QR Code format.");
showModal(errTitle, errMsg);
}
} catch (e) {
// Maybe it's just the ID?
if (decodedText.length === 6 && !isNaN(decodedText)) {
startHostWithId(decodedText);
} else {
const errTitle = window.t('airtype.modal.alert', "Error");
const errMsg = window.t('airtype.modal.error.qr_parse', "Could not parse QR Code.");
showModal(errTitle, errMsg);
}
}
}
function stopScanner(shouldNavigateHome = true) {
if (html5QrCode) {
html5QrCode.stop().then(() => {
html5QrCode.clear();
if (shouldNavigateHome) switchView('note-list-view');
}).catch(err => {
console.log("Failed to stop scanner", err);
if (shouldNavigateHome) switchView('note-list-view');
});
} else {
if (shouldNavigateHome) switchView('note-list-view');
}
}
// --- NOTE LIST LOGIC ---
function loadCloudNotes() {
db.collection('users').doc(currentUser.uid).collection('notes')
.orderBy('updated', 'desc')
.onSnapshot((snapshot) => {
notes = [];
snapshot.forEach(doc => {
let data = doc.data();
data.id = doc.id;
notes.push(data);
});
renderNoteList();
}, (error) => {
console.error("Error getting notes: ", error);
// document.getElementById('status-bar').innerText = "Sync Error. Using Local.";
});
}
function renderNoteList() {
const container = document.getElementById('note-list-container');
container.innerHTML = '';
if (notes.length === 0) {
const noneMsg = window.t('airtype.status.none', "No notes found.");
container.innerHTML = `<div style="padding:40px; text-align:center; font-style:italic;">${noneMsg}</div>`;
return;
}
notes.forEach(note => {
const div = document.createElement('div');
div.className = 'note-item';
const title = note.title || window.t('airtype.label.untitled', 'Untitled Note');
const date = note.updated ? new Date(note.updated).toLocaleDateString() : '';
div.innerHTML = `
<span class="note-title">${title}</span>
<span class="note-date">${date}</span>
`;
div.onclick = () => handleNoteClick(note);
container.appendChild(div);
});
}
function handleNewNote() {
initEntryFlow(null);
}
function handleNoteClick(note) {
initEntryFlow(note);
}
function openNote(note) {
// Logic to actually open the note (called after connection or if we were doing solo - but we removed solo option for now per request?)
// Wait, the prompt says "remove write solo". So they MUST connect?
// If they are on Phone and connect, we load this note into the input.
// If they are on Tablet and start Monitor, we technically display this note?
// Let's assume after connection established, we load the content.
// For now, let's keep the load logic in startHost/Monitor flow
}
// --- HOST LOGIC ---
async function startHost() {
if (!currentUser) {
const errTitle = window.t('airtype.modal.error.access', "Access Denied");
const errMsg = window.t('airtype.modal.error.access_body', "Please log in to system first.");
showModal(errTitle, errMsg);
return;
}
sessionId = Math.floor(100000 + Math.random() * 900000).toString();
document.getElementById('host-code').innerText = sessionId;
switchView('keyboard-view');
// 1. Create Cloud Session (Ephemeral / Sync)
sessionRef = rtdb.ref('freewrite_sessions/' + sessionId);
// Pre-populate if note selected
let initialTitle = "";
let initialContent = "";
let originalNoteId = null;
if (selectedNoteData) {
initialTitle = selectedNoteData.title || "";
initialContent = selectedNoteData.content || "";
originalNoteId = selectedNoteData.id;
// Also Update Input Fields locally
document.getElementById('title-input').value = initialTitle;
document.getElementById('input-area').value = initialContent;
// Set noteRef to existing note
noteRef = db.collection('users').doc(currentUser.uid).collection('notes').doc(selectedNoteData.id);