-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathexet-storage.js
More file actions
944 lines (907 loc) · 31.2 KB
/
Copy pathexet-storage.js
File metadata and controls
944 lines (907 loc) · 31.2 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
/*
MIT License
Copyright (c) 2022 Viresh Ratnakar
See the full Exet license notice in exet.js.
*/
/**
* Code related to managing localStorage and IndexedDB for Exet.
* localStorage is stored for puzzle revisions state and IndexedDB
* is used for custom lexicons (word lists).
*/
function ExetRev(id, title, revNum, revType, timestamp, details="") {
this.id = id;
this.title = title
this.revNum = revNum;
this.revType = revType;
this.timestamp = timestamp;
this.details = details;
// prefix, suffix, exolve should be set directly.
};
function ExetRevManager() {
this.REV_LOADED_FROM_FILE = 1;
this.REV_CREATED_BLANK = 2;
this.REV_CREATED_AUTOBLOCK = 3;
this.REV_JUMPED_TO_REV = 10;
this.REV_GRID_CHANGE = 20;
this.REV_LIGHT_REVERSAL = 24;
this.REV_AUTOFILL_GRIDFILL_CHANGE = 28;
this.REV_GRIDFILL_CHANGE = 30;
this.REV_ENUM_CHANGE = 40;
this.REV_CLUE_CHANGE = 50;
this.REV_METADATA_CHANGE = 60;
this.REV_FILL_OPTIONS_CHANGE = 65;
this.REV_PREFLEX_CHANGE = 70;
this.REV_OPTIONS_CHANGE = 80;
this.REV_RESAVE = 90;
this.revMsgs = {};
this.revMsgs[this.REV_LOADED_FROM_FILE] = "Loaded from a file";
this.revMsgs[this.REV_CREATED_BLANK] = "Created a blank grid";
this.revMsgs[this.REV_CREATED_AUTOBLOCK] = "Created a blank grid " +
"with automagic blocks";
this.revMsgs[this.REV_JUMPED_TO_REV] = "Jumped to a previous revision";
this.revMsgs[this.REV_GRID_CHANGE] = "Grid change";
this.revMsgs[this.REV_LIGHT_REVERSAL] = "Light reversal";
this.revMsgs[this.REV_AUTOFILL_GRIDFILL_CHANGE] = "Autofilled grid-fill " +
"change";
this.revMsgs[this.REV_GRIDFILL_CHANGE] = "Grid-fill change";
this.revMsgs[this.REV_ENUM_CHANGE] = "Enum change";
this.revMsgs[this.REV_CLUE_CHANGE] = "Clue or anno change";
this.revMsgs[this.REV_METADATA_CHANGE] = "Metadata change";
this.revMsgs[this.REV_FILL_OPTIONS_CHANGE] = "Change in options " +
"for suggested fills";
this.revMsgs[this.REV_PREFLEX_CHANGE] = "Change in the list of or options " +
"for preferred words";
this.revMsgs[this.REV_OPTIONS_CHANGE] = "Crossword options change";
this.revMsgs[this.REV_RESAVE] = "Resaved (to be safe) with local storage freed up";
/* State for throttled revision-saving */
this.throttleRevTimer = null;
this.saveLagMS = 5000
this.throttlingLastRev = 0;
/**
* Special localStorage key for storing preferences and state,
* qualified by non-default lexicon properties.
*/
this.SPECIAL_KEY_PREFIX = '42-exet-42';
this.SPECIAL_KEY = this.SPECIAL_KEY_PREFIX;
const maxCharCodes = exetConfig.maxCharCodes ?? 1;
if ('en' != exetConfig.language || 'Latin' != exetConfig.script ||
maxCharCodes != 1) {
this.SPECIAL_KEY += `-${exetLexicon.language}-${exetLexicon.script}` +
`-${maxCharCodes}`;
}
/* Id for previews */
this.previewId = `exet-preview-${Math.random().toString(36).substring(2, 8)}`;
};
ExetRevManager.prototype.skippableKey = function(id) {
return (id.startsWith(this.SPECIAL_KEY_PREFIX) ||
id.startsWith('xlvstate:') ||
id == '42-xlvp-player-state');
}
ExetRevManager.prototype.sizeOfPrefUnpref = function(id) {
let sz = 0;
for (isPref of [true, false]) {
const key = this.keyPrefUnpref(id, isPref);
let data = window.localStorage.getItem(key);
if (data) {
sz += data.length;
}
}
return sz;
}
/**
* params object should have these fields (missing = false/null):
* forStorage: true/false
* onlyPuz: null/current-puzzle (if not null, only used changing rev)
* startId: null or can be specified when onlyPuz is null, for starting id.
* elt: the element in which to render
* callback: null or fn. to call after selection, passing rev
* sortBy: null or 'timestamp'/'title'/'space'/'id'
* sortOrder: null or 'increasing'/'decreasing'
* sortBy/sortOrder are ignored when onlyPuz is not null.
*/
ExetRevManager.prototype.choosePuzRev = function(params) {
this.params = params;
let choices = [];
if (params.onlyPuz) {
let lsUsed = 0;
let stored = window.localStorage.getItem(params.onlyPuz.id);
if (lsUsed) {
lsUsed = stored.length + this.sizeOfPrefUnpref(params.onlyPuz.id);
}
choices = [{id: params.onlyPuz.id, title: params.onlyPuz.title, space: lsUsed, timestamp: 0}];
} else {
for (let idx = 0; idx < window.localStorage.length; idx++) {
const id = window.localStorage.key(idx);
if (this.skippableKey(id)) {
continue;
}
const storedJson = window.localStorage.getItem(id);
let lsUsed = storedJson.length + this.sizeOfPrefUnpref(id);
let stored = '';
try {
stored = JSON.parse(storedJson);
} catch (err) {
console.log('Unparseable stored item for id [' + id + ']:' + storedJson);
continue;
}
if (!stored || !stored["id"] || !stored["revs"] || !stored["maxRevNum"]) {
console.log('Weird stored item for id [' + id + ']:' + storedJson);
continue;
}
let title = '';
let timestamp = 0;
if (stored.revs.length > 0) {
const lastRev = stored.revs[stored.revs.length - 1];
title = lastRev.title;
timestamp = lastRev.timestamp;
}
choices.push({id: stored.id, title: title, space: lsUsed, timestamp: timestamp});
}
}
if (params.sortBy) {
const cmp = (a, b) => {
const mult = (params.sortOrder == 'increasing' ? 1 : -1);
if (a < b) {
return -1 * mult;
} else if (b < a) {
return 1 * mult;
} else {
return 0;
}
};
const sorter = (c1, c2) => {
return cmp(c1[params.sortBy], c2[params.sortBy]);
};
choices.sort(sorter);
}
exet.checkStorage();
let sortingHTML = '';
if (!params.onlyPuz) {
const sorters = ['timestamp', 'title', 'id', 'space'];
sortingHTML = ', sort order: <select id="xet-choose-puz-sort-by">';
for (const sorter of sorters) {
sortingHTML += '<option' + (sorter == params.sortBy ? ' selected': '') +
'>' + sorter + '</option>';
}
sortingHTML += '</select> <select id="xet-choose-puz-sort-order">';
const orders = [
{value: 'increasing', symbol: '▲'},
{value: 'decreasing', symbol: '▼'},
];
for (const order of orders) {
sortingHTML += '<option value="' + order.value + '"' +
' title="' + order.value + '"' +
(order.value == params.sortOrder ? ' selected': '') +
'>' + order.symbol + '</option>';
}
sortingHTML += '</select>';
}
let html = `
<table>
<tr>
<td class="xet-pad-top">
Select puzzle${sortingHTML}:
</td>
<td class="xet-pad-top">
Select revision:
</td>
</tr>
<tr>
<td>
<div class="xet-choices-box" id="xet-choose-id">
<table class="xet-choices" id="xet-id-choices">`
for (let i = 0; i < choices.length; i++) {
html = html + `
<tr id="xet-id-choice-${i}">
<td>
<b>${choices[i].id}</b>
${choices[i].timestamp > 0 ?
'<br><span class="xet-small-action">Last change: ' +
(new Date(choices[i].timestamp)).toLocaleString() +
'</span>' : ''}
</td>
<td>${choices[i].title}</td>
<td>${exet.inMB(choices[i].space)} MB</td></tr>`
}
html = html + `
</table>
</div>
</td>
<td>
<div class="xet-choices-box" id="xet-choose-rev">
<table class="xet-choices" id="xet-rev-choices">
</table>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div>
<span>Space used = ${exet.lsUsedSpan.innerText} MB</span>,
<span>Space available ≈
<span ${exet.lsLeftIsAmple ? '' :
'class="xet-red"'}>${exet.lsFreeSpan.innerText}</span>
MB</span>
<button id="xet-puz-rev-deleter"
style="float:right;margin: 0 16px; display: none"
class="xlv-button">Delete rev</button>
<button id="xet-puz-prior-deleter"
style="float:right;margin: 0 16px; display: none"
class="xlv-button">Delete older revs!</button>
<button id="xet-puz-deleter"
style="float:right;margin: 0 16px; display: none"
class="xlv-button">Delete!</button>
<button id="xet-puz-rev-selector"
style="float:right;margin: 0 16px; display: none"
class="xlv-button">Open</button>
</div>
<div id="xet-preview" class="xet-preview">
</div>
</td>
</tr>
</table>
`;
params.elt.innerHTML = html;
const idChoicesBox = document.getElementById('xet-choose-id');
idChoicesBox.style.width = '385px';
idChoicesBox.style.height = '200px';
this.idChoices = document.getElementById('xet-id-choices');
const revChoicesBox = document.getElementById('xet-choose-rev');
revChoicesBox.style.width = '385px';
revChoicesBox.style.height = '200px';
this.revChoices = document.getElementById('xet-rev-choices');
this.preview = document.getElementById('xet-preview');
this.idChoice = '';
this.revChoice = -1;
this.puzDeleter = document.getElementById('xet-puz-deleter');
this.puzPriorDeleter = document.getElementById('xet-puz-prior-deleter');
this.puzRevDeleter = document.getElementById('xet-puz-rev-deleter');
this.puzRevSelector = document.getElementById('xet-puz-rev-selector');
this.sortByElt = document.getElementById('xet-choose-puz-sort-by');
this.sortOrderElt = document.getElementById('xet-choose-puz-sort-order');
if (this.sortByElt) {
const resorter = (e => {
const m = exetRevManager;
const params = m.params;
params.sortBy = m.sortByElt.value;
params.sortOrder = m.sortOrderElt.value;
params.startId = m.idChoice;
m.choosePuzRev(params);
});
this.sortByElt.addEventListener('change', resorter);
this.sortOrderElt.addEventListener('change', resorter);
}
if (params.forStorage) {
this.puzDeleter.style.display = '';
this.puzPriorDeleter.style.display = '';
this.puzRevDeleter.style.display = '';
this.puzDeleter.disabled = true;
this.puzPriorDeleter.disabled = true;
this.puzRevDeleter.disabled = true;
const deleter = (types, e) => {
let newRevsNotAll = [];
if (types != 'all') {
if (this.revChoice < 0 || !this.storedRevs ||
this.storedRevs.revs.length == 0 ||
this.revChoice >= this.storedRevs.revs.length) {
console.log('Weird, did not find revChoice/storedRevs to delete from');
return;
}
let lastToDelete = this.revChoice;
if (types == 'prior') lastToDelete--;
let numToDelete = (types == 'prior' ? lastToDelete + 1 : 1);
let keptRevs = [];
if (lastToDelete - numToDelete >= 0) {
keptRevs = this.storedRevs.revs.slice(
0, lastToDelete - numToDelete + 1);
}
newRevsNotAll = keptRevs.concat(
this.storedRevs.revs.slice(lastToDelete + 1));
if (newRevsNotAll.length == 0) {
/** The user has indirectly chosen all revisions to be deleted. */
types = 'all';
}
}
if (!confirm('Are you sure you want to delete ' + types +
' revision(s)?')) {
return;
}
this.idChoices.className = 'xet-choices';
this.revChoices.className = 'xet-choices';
if (types == 'all') {
window.localStorage.removeItem(this.idChoice);
window.localStorage.removeItem(
this.keyPrefUnpref(this.idChoice, true));
window.localStorage.removeItem(
this.keyPrefUnpref(this.idChoice, false));
this.idChoice = '';
} else {
this.storedRevs.revs = newRevsNotAll;
this.saveLocal(this.idChoice, JSON.stringify(this.storedRevs));
this.savePrefUnpref(this.idChoice, this.storedRevs.revs, true);
}
this.params.startId = this.idChoice;
this.choosePuzRev(this.params);
e.stopPropagation();
}
this.puzDeleter.addEventListener('click', deleter.bind(this, 'all'));
this.puzPriorDeleter.addEventListener('click', deleter.bind(this, 'prior'));
this.puzRevDeleter.addEventListener('click', deleter.bind(this, 'this'));
} else {
this.puzRevSelector.style.display = '';
this.puzRevSelector.disabled = true;
this.puzRevSelector.addEventListener('click', e => {
if (this.revChoice < 0 || !this.storedRevs ||
this.storedRevs.revs.length == 0 ||
this.revChoice >= this.storedRevs.revs.length) {
console.log('Hmm: bad selection! Check ExetRevManager:');
console.log(this);
return;
}
exetModals.hide();
this.idSelectors = [];
this.revSelectors = [];
this.preview.innerHTML = '';
if (exolvePuzzles[this.previewId]) {
exolvePuzzles[this.previewId].destroy();
}
if (params.callback) {
params.callback(this.storedRevs.revs[this.revChoice]);
}
})
}
this.idSelectors = [];
this.revSelectors = [];
this.storedRevs = null;
if (params.onlyPuz) {
this.idChoice = params.onlyPuz.id;
document.getElementById("xet-id-choice-0").classList.add('xet-chosen');
this.chooseRev();
return;
}
const selectIndex = (idx => {
this.preview.innerHTML = '';
if (this.previewId && exolvePuzzles[this.previewId]) {
exolvePuzzles[this.previewId].destroy();
}
this.puzDeleter.disabled = true;
this.puzPriorDeleter.disabled = true;
this.puzRevDeleter.disabled = true;
this.revChoices.innerHTML = '';
this.revChoice = -1;
this.revSelectors = [];
this.storedRevs = null;
this.puzRevSelector.disabled = true;
if (choices[idx].id == this.idChoice) {
this.idChoice = '';
this.idSelectors[idx].classList.remove('xet-chosen');
} else {
for (let j = 0; j < choices.length; j++) {
if (j != idx) {
this.idSelectors[j].className = '';
}
}
this.idChoice = choices[idx].id;
this.puzDeleter.disabled = false;
this.idSelectors[idx].classList.add('xet-chosen');
this.chooseRev();
}
});
let startIdIndex = -1;
for (let i = 0; i < choices.length; i++) {
let selector = document.getElementById(`xet-id-choice-${i}`);
this.idSelectors.push(selector);
const id = choices[i].id;
if (id == params.startId) {
startIdIndex = i;
}
selector.addEventListener('click', e => selectIndex(i));
}
if (startIdIndex >= 0) {
selectIndex(startIdIndex);
}
};
ExetRevManager.prototype.chooseRev = function() {
let stored = window.localStorage.getItem(this.idChoice);
if (!stored) {
return;
}
this.storedRevs = JSON.parse(stored);
let html = '';
for (let idx = this.storedRevs.revs.length - 1; idx >= 0; idx--) {
let rev = this.storedRevs.revs[idx];
let revTime = new Date(rev.timestamp);
html = html + `
<tr id="xet-rev-choice-${idx}">
<td>${rev.title}</td>
<td>#${rev.revNum}</td>
<td>${revTime.toLocaleString()}</td>
<td>${exetRevManager.revMsgs[rev.revType]}</td>
<td>${rev.details}</td>
</tr>`;
}
this.revChoices.innerHTML = html;
this.revSelectors = [];
this.revChoice = -1;
for (let i = 0; i < this.storedRevs.revs.length; i++) {
let selector = document.getElementById(`xet-rev-choice-${i}`);
this.revSelectors.push(selector);
selector.addEventListener('click', e => {
if (!this.storedRevs) {
return;
}
this.puzPriorDeleter.disabled = true;
this.puzRevDeleter.disabled = true;
this.puzRevSelector.disabled = true;
this.preview.innerHTML = '';
if (exolvePuzzles[this.previewId]) {
exolvePuzzles[this.previewId].destroy();
}
if (i == this.revChoice) {
this.revChoice = -1;
selector.classList.remove('xet-chosen');
} else {
for (let j = 0; j < this.revSelectors.length; j++) {
if (j != i) {
this.revSelectors[j].className = '';
}
}
this.revChoice = i;
selector.classList.add('xet-chosen');
let exolve = this.storedRevs.revs[i].exolve.replace(
/exolve-id:[^\n]*/, `exolve-id: ${this.previewId}`);
exet.renderPreview(exolve, "xet-preview");
this.puzPriorDeleter.disabled = (i <= 0);
this.puzRevDeleter.disabled = false;
this.puzRevSelector.disabled = false;
}
});
}
};
ExetRevManager.prototype.saveLocal = function(k, v) {
try {
window.localStorage.setItem(k, v);
} catch (err) {
this.checkStorage();
alert('No available local storage left. Please use the ' +
'"Manage local storage" menu option to free up some space.');
console.log('Could not save value of length ' + v.length + ' for key: ' + k)
return false;
}
return true;
}
ExetRevManager.prototype.keyPrefUnpref = function(id, isPref) {
return this.SPECIAL_KEY_PREFIX +
(isPref ? '-preflex-' : '-unpreflex-') + id;
}
ExetRevManager.prototype.hashPrefUnpref = function(arr) {
if (!arr || arr.length == 0) {
return null;
}
return exetLexicon.javaHash(JSON.stringify(arr));
}
ExetRevManager.prototype.savePrefUnpref = function(id, revs, doGC=false) {
const isCurrPuz = (exet.puz && exet.puz.id && exet.puz.id == id);
console.assert(doGC || isCurrPuz, doGC, isCurrPuz);
for (isPref of [true, false]) {
const hashName = isPref ? 'preflexHash' : 'unpreflexHash';
if (!doGC && isCurrPuz && !exet[hashName]) {
continue;
}
let changed = false;
const key = this.keyPrefUnpref(id, isPref);
let data = window.localStorage.getItem(key);
if (data) {
data = JSON.parse(data);
} else {
data = {};
}
if (isCurrPuz && exet[hashName] && !data.hasOwnProperty(exet[hashName])) {
data[exet[hashName]] = isPref ? exet.preflex : exet.unpreflex;
changed = true;
}
if (doGC) {
/** Don't use a set for usedHashes to avoid int/str issue! */
const usedHashes = {};
if (isCurrPuz && exet[hashName]) {
usedHashes[exet[hashName]] = true;
}
for (const rev of revs) {
if (rev[hashName]) {
usedHashes[rev[hashName]] = true;
}
}
for (const hash in data) {
if (!usedHashes.hasOwnProperty(hash)) {
delete data[hash];
changed = true;
}
}
}
if (changed) {
if (Object.keys(data).length == 0) {
window.localStorage.removeItem(key);
} else {
this.saveLocal(key, JSON.stringify(data));
}
}
}
}
ExetRevManager.prototype.retrievePrefUnpref = function(rev) {
for (isPref of [true, false]) {
const hashName = isPref ? 'preflexHash' : 'unpreflexHash';
const objName = isPref ? 'preflex' : 'unpreflex';
let obj = null;
if (rev[objName]) {
/** old way */
obj = rev[objName];
if (!isPref) {
const pList = Object.keys(obj);
obj = [];
for (const p of pList) {
if (p < 0 || p >= exetLexicon.lexicon.length) {
console.log('Skipping out-of-bounds old "unpreflex" entry: ' + p);
continue;
}
obj.push(exetLexicon.getLex(p));
}
}
} else if (rev[hashName]) {
const key = this.keyPrefUnpref(rev.id, isPref);
let data = window.localStorage.getItem(key);
if (data) {
data = JSON.parse(data);
} else {
data = {};
}
obj = data[rev[hashName]] || null;
if (!obj) {
console.log('Missing data for ' + hashName + ' = ' + rev[hashName]);
}
}
if (!obj) {
obj = [];
}
if (isPref) {
exet.setPreflex(obj);
} else {
exet.setUnpreflex(obj);
}
}
}
/**
* Compare exolve format strings after removing timestamps.
*/
ExetRevManager.prototype.equalExolves = function(x1, x2) {
const r = /Timestamp: .*/g;
const x1mod = x1.replaceAll(r, '');
const x2mod = x2.replaceAll(r, '');
return x1mod == x2mod;
}
ExetRevManager.prototype.saveRev = function(revType, details="") {
if (!exet || !exet.puz || !exet.puz.id) {
console.log('Cannot save revision when there is no puzzle!');
return;
}
let stored = window.localStorage.getItem(exet.puz.id);
if (!stored) {
stored = {
id: exet.puz.id,
maxRevNum: 0,
revs: []
};
} else {
stored = JSON.parse(stored);
}
/**
* Do garbage collection of unused pref/unpref data when saving,
* to catch potentially unsaved versions or when saving preflex
* data updates.
*/
const doPrefUnprefGC = (revType == this.REV_RESAVE ||
revType == this.REV_PREFLEX_CHANGE);
this.savePrefUnpref(exet.puz.id, stored.revs, doPrefUnprefGC);
const exolve = exet.getExolve();
if (stored.revs.length > 0) {
const lastRev = stored.revs[stored.revs.length - 1];
if (this.equalExolves(lastRev.exolve, exolve) &&
lastRev.prefix == exet.prefix && lastRev.suffix == exet.suffix &&
lastRev.scratchPad == exet.puz.scratchPad.value &&
lastRev.hasOwnProperty('preflexHash') && lastRev.preflexHash == exet.preflexHash &&
lastRev.hasOwnProperty('unpreflexHash') && lastRev.unpreflexHash == exet.unpreflexHash &&
lastRev.noProperNouns == exet.noProperNouns &&
lastRev.noStemDupes == exet.noStemDupes &&
lastRev.hasOwnProperty('region') && lastRev.region == exet.region &&
lastRev.tryReversals == exet.tryReversals &&
lastRev.minpop == exet.minpop &&
lastRev.hasOwnProperty('minscore') && lastRev.minscore == exet.minscore &&
lastRev.hasOwnProperty('lexId') && lastRev.lexId == exetLexicon.id &&
lastRev.hasOwnProperty('requireEnums') && lastRev.requireEnums == exet.requireEnums &&
lastRev.hasOwnProperty('lightRegexps') && JSON.stringify(lastRev.lightRegexps) == JSON.stringify(exet.lightRegexps) &&
lastRev.asymOK == exet.asymOK) {
return;
}
}
stored.maxRevNum++;
let exetRev = new ExetRev(exet.puz.id, (exet.puz.title ? exet.puz.title : ''),
stored.maxRevNum, revType, Date.now(), details);
exetRev.maxRevNum = stored.maxRevNum;
exetRev.prefix = exet.prefix;
exetRev.suffix = exet.suffix;
exetRev.exolve = exolve;
exetRev.scratchPad = exet.puz.scratchPad.value;
exetRev.navState = [exet.puz.currDir, exet.puz.currRow, exet.puz.currCol];
exetRev.preflexHash = exet.preflexHash;
exetRev.unpreflexHash = exet.unpreflexHash;
exetRev.noProperNouns = exet.noProperNouns;
exetRev.noStemDupes = exet.noStemDupes;
exetRev.region = exet.region;
exetRev.asymOK = exet.asymOK;
exetRev.tryReversals = exet.tryReversals;
exetRev.minpop = exet.minpop;
exetRev.minscore = exet.minscore;
exetRev.lexId = exetLexicon.id;
exetRev.requireEnums = exet.requireEnums;
exetRev.lightRegexps = exet.lightRegexps;
stored.revs.push(exetRev);
this.saveLocal(exet.puz.id, JSON.stringify(stored));
}
ExetRevManager.prototype.throttledSaveRev = function(revType, details="") {
let urgent = revType <= 10;
if (this.throttleRevTimer) {
clearTimeout(this.throttleRevTimer);
if (this.throttlingRevType > 0 && revType < this.throttlingRevType) {
urgent = true
}
}
this.throttleRevTimer = null;
this.throttlingRevType = 0;
if (urgent) {
this.saveRev(revType, details)
return
}
this.throttlingRevType = revType;
this.throttleRevTimer = setTimeout(() => {
this.saveRev(revType, details)
this.throttleRevTimer = null;
this.throttlingRevType = 0;
}, this.saveLagMS);
}
ExetRevManager.prototype.saveAllRevisions = function() {
const storage = {};
for (let idx = 0; idx < window.localStorage.length; idx++) {
const id = window.localStorage.key(idx);
if (this.skippableKey(id)) {
continue;
}
const storedJson = window.localStorage.getItem(id);
let storedRevs = null;
try {
storedRevs = JSON.parse(storedJson);
} catch (err) {
console.log('Unparseable stored item for id [' + id + ']:' + storedJson);
continue;
}
if (!storedRevs || !storedRevs['revs']) {
console.log('Weird stored item for id [' + id + ']:' + storedJson);
continue;
}
storage[id] = storedRevs;
this.savePrefUnpref(id, storedRevs.revs, true);
for (isPref of [true, false]) {
const key = this.keyPrefUnpref(id, isPref);
const data = window.localStorage.getItem(key);
if (data) {
storage[key] = JSON.parse(data);
}
}
}
const json = JSON.stringify(storage, null, 2);
const filename = `exet-backup-${(new Date()).toISOString()}.json`;
Exolve.prototype.fileDownload(json, "text/json", filename);
exetState.lastBackup = Date.now();
exetRevManager.saveLocal(exetRevManager.SPECIAL_KEY,
JSON.stringify(exetState));
exet.checkStorage();
exetModals.hide();
}
ExetRevManager.prototype.mergeRevisionsFile = function() {
exetModals.hide();
let fr = new FileReader();
fr.onload = function(){
let allSavedRevs = {}
try {
allSavedRevs = JSON.parse(fr.result);
} catch (err) {
alert('Could not parse the saved revisions file');
return;
}
existingRevs = {};
for (let idx = 0; idx < window.localStorage.length; idx++) {
const id = window.localStorage.key(idx);
if (exetRevManager.skippableKey(id)) {
continue;
}
const storedJson = window.localStorage.getItem(id);
let storedRevs = null;
try {
storedRevs = JSON.parse(storedJson);
} catch (err) {
console.log('Unparseable stored item for id [' + id + ']:' + storedJson);
continue;
}
if (!storedRevs || !storedRevs['revs']) {
console.log('Weird stored item for id [' + id + ']:' + storedJson);
continue;
}
for (rev of storedRevs['revs']) {
const revHash = exetLexicon.javaHash(JSON.stringify(rev));
existingRevs[revHash] = true;
}
}
let numRevs = 0;
let numRevsMerged = 0;
let numDupRevs = 0;
let numNonLatest = 0;
const mergeOnlyLatest = document.getElementById(
'xet-merge-only-latest-revs').checked ? true : false;
for (let id in allSavedRevs) {
if (id.startsWith(this.SPECIAL_KEY_PREFIX)) {
continue;
}
savedRevs = allSavedRevs[id]['revs'];
if (!savedRevs || savedRevs.length == 0) {
continue;
}
const start = mergeOnlyLatest ? savedRevs.length - 1 : 0;
if (mergeOnlyLatest) {
numNonLatest += savedRevs.length - 1;
}
revsToSplice = [];
for (let i = start; i < savedRevs.length; i++) {
numRevs++;
const rev = savedRevs[i];
const revHash = exetLexicon.javaHash(JSON.stringify(rev));
if (existingRevs[revHash]) {
numDupRevs++;
continue;
}
revsToSplice.push(rev);
}
if (revsToSplice.length == 0) {
continue;
}
let stored = window.localStorage.getItem(id);
const prefKey = exetRevManager.keyPrefUnpref(id, true);
let storedPref = window.localStorage.getItem(prefKey);
const unprefKey = exetRevManager.keyPrefUnpref(id, true);
let storedUnpref = window.localStorage.getItem(unprefKey);
try {
if (stored) {
stored = JSON.parse(stored);
}
if (storedPref) {
storedPref = JSON.parse(storedPref);
}
if (storedUnpref) {
storedUnpref = JSON.parse(storedUnpref);
}
} catch (err) {
console.log('Skipped id in merging as JSON.parse() failed, id: ' + id);
continue;
}
if (!stored) {
stored = { id: id, maxRevNum: 0, revs: [] };
}
if (!storedPref) {
storedPref = {};
}
if (!storedUnpref) {
storedUnpref = {};
}
let addedPref = false;
let addedUnpref = false;
for (rev of revsToSplice) {
stored['revs'].push(rev);
if (rev.preflexHash && allSavedRevs[prefKey] &&
allSavedRevs[prefKey][rev.preflexHash]) {
storedPref[rev.preflexHash] = allSavedRevs[prefKey][rev.preflexHash];
addedPref = true;
}
if (rev.unpreflexHash && allSavedRevs[unprefKey] &&
allSavedRevs[unprefKey][rev.unpreflexHash]) {
storedUnpref[rev.unpreflexHash] = allSavedRevs[unprefKey][rev.unpreflexHash];
addedUnpref = true;
}
}
stored['revs'].sort((r1, r2) => r1.timestamp - r2.timestamp);
for (rev of stored['revs']) {
if (rev.revNum > stored.maxRevNum) {
stored.maxRevNum = rev.revNum;
}
}
if (!exetRevManager.saveLocal(id, JSON.stringify(stored))) {
break;
}
if (addedPref &&
!exetRevManager.saveLocal(prefKey, JSON.stringify(storedPref))) {
break;
}
if (addedUnpref &&
!exetRevManager.saveLocal(unprefKey, JSON.stringify(storedUnpref))) {
break;
}
numRevsMerged += revsToSplice.length;
}
const ignored = (numNonLatest > 0) ?
`Ignored ${numNonLatest} non-latest revisions.` : '';
alert(`From ${numRevs} revisions considered across ` +
`${Object.keys(allSavedRevs).length} crosswords, merged ` +
`${numRevsMerged} revisions. There were ${numDupRevs} revisions ` +
`that already existed. ${ignored}`);
}
const f = document.getElementById('xet-merge-revs-file').files[0];
fr.readAsText(f);
}
ExetRevManager.prototype.autofree = function() {
this.saveAllRevisions();
const SAVE_LAST_THESE_MANY = 25;
const SAVE_LAST_THESE_MANY_HOURS = 1;
const tsCutoffMillis =
Date.now() - (SAVE_LAST_THESE_MANY_HOURS * 60 * 60 * 1000);
let bytesUsed = 0;
let itemsPurged = 0;
for (let idx = 0; idx < window.localStorage.length; idx++) {
const id = window.localStorage.key(idx);
const storedJson = window.localStorage.getItem(id);
bytesUsed += storedJson.length;
if (this.skippableKey(id)) {
continue;
}
let stored = '';
try {
stored = JSON.parse(storedJson);
} catch (err) {
console.log('Unparseable stored item for id [' + id + ']:' + storedJson);
continue;
}
if (!stored || !stored["id"] || !stored["revs"] || !stored["maxRevNum"]) {
console.log('Weird stored item for id [' + id + ']:' + storedJson);
continue;
}
const revs = stored.revs;
if (revs.length <= 0) {
console.log('No revisions in crossword with id ' + id + ': should be deleted');
continue;
}
const limit = revs.length - SAVE_LAST_THESE_MANY;
const revsToKeep = [];
for (let r = 0; r < revs.length; r++) {
const rev = revs[r];
revsToKeep.push(rev);
if ((r < limit) &&
((r % 2) == 1) &&
(rev.timestamp < tsCutoffMillis)) {
revsToKeep.pop();
itemsPurged++;
}
}
if (revsToKeep.length < revs.length) {
stored.revs = revsToKeep;
this.saveLocal(id, JSON.stringify(stored));
this.savePrefUnpref(id, stored.revs, true);
}
}
/**
* Call checkStorage() to update displayed numbers/warnings, and to
* get the return value from checkLocalStorage().
*/
const ampleLeft = exet.checkStorage();
if (itemsPurged == 0 && !ampleLeft) {
alert('Auto-Free could not find any old revisions to purge, and you ' +
'are running very low on available storage. This probably means ' +
'that you have excessively many active crosswords. Use the ' +
'"Manage local storage" menu option to manually delete some old ' +
'crosswords, perhaps.');
}
}