-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
875 lines (831 loc) · 25.9 KB
/
main.js
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
/*
* Code originally created by Samuel Mortenson (mortenson.coffee).
* This was written in ~20 hours, so please don't judge too hard!
*/
(function () {
var PLAINTEXT_REGEXP = /(css|js|CNAME|txt|html)$/i;
var globalState = {};
var objectURLs = {
form: [],
preview: [],
};
var globalEditor;
var lastTemplate;
// Wraps object URL methods for memory management reasons.
function createObjectURL(file, context) {
var url = window.URL.createObjectURL(file);
objectURLs[context].push(url);
return url;
}
function revokeObjectURLs(context) {
for (var i in objectURLs) {
window.URL.revokeObjectURL(objectURLs[context][i]);
}
objectURLs[context] = [];
}
// A custom CKEditor plugin that stores uploaded files in global state.
class MyUploadAdapter {
constructor(loader) {
this.loader = loader;
}
upload() {
return this.loader.file.then(function (file) {
return new Promise(function (resolve, reject) {
globalState.files.push(file);
resolve({
default:
createObjectURL(file, "form") + "#filename=".concat(file.name),
});
});
});
}
abort() {
alert("Image failed to upload");
}
}
function MyCustomUploadAdapterPlugin(editor) {
editor.plugins.get("FileRepository").createUploadAdapter = function (
loader
) {
return new MyUploadAdapter(loader);
};
}
// Cute utility functions to mimic jQuery's $.show/hide.
function show(element) {
element.classList.remove("hidden");
}
function hide(element) {
element.classList.add("hidden");
}
// Safely compiles an EJS template.
function ejsSafeCompile(template, data) {
if (template === lastTemplate) {
return "Infinite template include loop detected!";
}
lastTemplate = template;
try {
var compile = ejs.compile(template, { client: true });
return compile(data, null, ejsCallback);
} catch (e) {
return `<pre style="white-space: pre-wrap;">Error when rendering template: ${e.message}</pre>`;
}
}
// Template rendering callback for EJS. Uses the user-configured templates as
// if they were files.
function ejsCallback(path, data) {
var templates = getTemplates();
for (var i in templates) {
if (templates[i].name === path) {
return ejsSafeCompile(templates[i].content, data);
}
}
return `Template ${path} not found.`;
}
// Replaces asset URLs in CKEditor generated content with the file path.
function replaceAssetUrls(html) {
for (var match of html.matchAll(/"(blob[^"]+#filename=([^"]+))"/g)) {
var oldUrl = match[1];
var filename = match[2];
html = html.replace(oldUrl, "/assets/" + filename);
}
return html;
}
// Renders a page for use in the preview or in the final static output.
function renderPage(id) {
var pages = getPages();
var templatePages = [];
for (var i in pages) {
// This prevents modifying the global state during rendering.
templatePages[i] = Object.assign({}, pages[i]);
try {
templatePages[i].extraData = JSON.parse(pages[i].extra);
} catch (e) {
templatePages[i].extraData = {};
}
}
var data = {
page: templatePages[id],
pageIndex: id,
site: {
pages: templatePages,
},
};
return ejsSafeCompile(getTemplate(0).content, data);
}
// Renders a page and places it in the preview iframe.
function renderPreview(id) {
revokeObjectURLs("preview");
var result = renderPage(id);
document.getElementById("preview").contentWindow.document.open();
// Ensure relative asset paths are replaced with object URLs.
for (var match of result.matchAll(/(?<=["'])\/?assets\/([^'"]+)/g)) {
var oldUrl = match[0];
var filename = match[1];
var file;
for (var i in globalState.files) {
if (globalState.files[i].name === filename) {
file = globalState.files[i];
break;
}
}
if (!file) {
continue;
}
result = result.replace(oldUrl, createObjectURL(file, "preview"));
}
result = refreshBlobURLs(result);
document.getElementById("preview").contentWindow.document.write(result);
document.getElementById("preview").contentWindow.document.close();
document.getElementById(
"preview"
).contentWindow.document.body.onclick = function (e) {
var target = e.target;
if (typeof target.href !== "string") {
return;
}
e.preventDefault();
// We could enable local navigation by trapping clicks, but without a URL
// bar or forward/back functionality it feels really bad.
alert("Navigation is disabled in the preview.");
};
}
// Helper function to generate safe clickable links.
function getSidebarItem(index, text) {
var li = document.createElement("li");
var a = document.createElement("a");
a.href = "#main";
a.setAttribute("data-index", index);
a.innerText = text;
li.appendChild(a);
return li;
}
// Renders the sidebar.
function renderSidebar() {
pageList = document.getElementById("page-list");
pageList.innerHTML = "";
getPages().forEach(function (page, index) {
pageList.appendChild(getSidebarItem(index, page.title));
});
templateList = document.getElementById("template-list");
templateList.innerHTML = "";
getTemplates().forEach(function (template, index) {
templateList.appendChild(getSidebarItem(index, template.name));
});
fileList = document.getElementById("file-list");
fileList.innerHTML = "";
var files = getFiles();
files.forEach(function (file, index) {
fileList.appendChild(getSidebarItem(index, file.name));
});
if (!files.length) {
fileList.innerHTML = "No files uploaded.";
}
}
// Refreshes blob URLs that may have been revoked.
function refreshBlobURLs(html) {
for (var match of html.matchAll(/"(blob[^"]+#filename=([^"]+))"/g)) {
var oldUrl = match[1];
var filename = match[2];
var file;
var files = getFiles();
for (var j in files) {
if (files[j].name === filename) {
file = files[j];
break;
}
}
if (!file) {
continue;
}
html = html.replace(
oldUrl,
createObjectURL(file, "form") + `#filename=${file.name}`
);
}
return html;
}
// Long helper function to show and initialize a form based on the currently
// selected thing.
function openForm(type, id) {
revokeObjectURLs("form");
globalState.currentForm = type;
switch (type) {
case "page":
globalState.currentPage = id;
show(document.getElementById("page-form"));
hide(document.getElementById("file-form"));
hide(document.getElementById("template-form"));
var currentPage = getPage(globalState.currentPage);
currentPage.body = refreshBlobURLs(currentPage.body);
globalEditor.setData(currentPage.body);
var title = document.getElementById("title");
title.value = currentPage.title;
document.getElementById("path").value = currentPage.path;
document.getElementById("extra").value = currentPage.extra;
document.getElementById("advanced").removeAttribute("open");
break;
case "template":
globalState.currentTemplate = id;
hide(document.getElementById("page-form"));
hide(document.getElementById("file-form"));
show(document.getElementById("template-form"));
var currentTemplate = getTemplate(globalState.currentTemplate);
var template = document.getElementById("template");
var name = document.getElementById("name");
template.value = currentTemplate.content;
name.value = currentTemplate.name;
name.disabled = currentTemplate.name === "html";
break;
case "file":
globalState.currentFile = id;
hide(document.getElementById("page-form"));
show(document.getElementById("file-form"));
hide(document.getElementById("template-form"));
var currentFile = getFile(globalState.currentFile);
var preview = document.getElementById("file-preview");
var contents = document.getElementById("file-contents");
if (currentFile.name.match(/(png|jpg|jpeg|gif|svg|webp)$/i)) {
preview.innerHTML = `<img src="${createObjectURL(
currentFile,
"form"
)}"></img>`;
} else {
preview.innerHTML = "";
}
if (currentFile.name.match(PLAINTEXT_REGEXP)) {
currentFile.text().then(function (text) {
contents.value = text;
show(document.getElementById("file-contents-wrapper"));
});
} else {
contents.value = "";
hide(document.getElementById("file-contents-wrapper"));
}
document.getElementById("file-name").value = currentFile.name;
var usage = document.getElementById("file-usage");
usage.innerHTML = "";
var usageList = document.createElement("ul");
var pages = getPages();
for (var i in pages) {
if (pages[i].body.match(currentFile.name)) {
var li = document.createElement("li");
li.innerText = pages[i].title;
usageList.append(li);
}
}
var templates = getTemplates();
for (var i in templates) {
if (templates[i].content.match(currentFile.name)) {
var li = document.createElement("li");
li.innerText = templates[i].name;
usageList.append(li);
}
}
if (usageList.children.length) {
usage.innerHTML =
"<div>This file is used by the following pages/templates:</div>";
usage.append(usageList);
}
break;
default:
break;
}
renderSidebar();
}
// Getter/setter functions with the hope of abstracting globalState from
// most of the app. This was nice when I thought I was going to use IndexedDB
// in a structured way instead of slamming all of the state in at once.
function getPage(id) {
return globalState.pages[id];
}
function getPages() {
return globalState.pages;
}
function addPage(title, body, path) {
globalState.pages.push({
title: title,
body: body,
path: path,
extra: "",
});
}
function removePage(id) {
globalState.pages.splice(id, 1);
}
function updatePage(id, title, body, path, extra) {
globalState.pages[id] = {
title: title,
body: body,
path: path,
extra: extra,
};
}
function getTemplate(id) {
return globalState.templates[id];
}
function getTemplates() {
return globalState.templates;
}
function addTemplate(name, content) {
globalState.templates.push({
name: name,
content: content,
});
}
function removeTemplate(id) {
globalState.templates.splice(id, 1);
}
function updateTemplate(id, name, content) {
globalState.templates[id] = {
name: name,
content: content,
};
}
function getFile(id) {
return globalState.files[id];
}
function getFiles() {
return globalState.files;
}
function addFile(file) {
globalState.files.push(file);
}
function removeFile(id) {
globalState.files.splice(id, 1);
}
// Initializes the app - called at the start of page load.
function initApp() {
globalState = {
version: 3,
currentPage: null,
currentTemplate: null,
currentFile: null,
currentForm: null,
pages: [],
files: [],
templates: [],
};
return ClassicEditor.create(document.querySelector("#editor"), {
extraPlugins: [MyCustomUploadAdapterPlugin],
mediaEmbed: {
previewsInData: true,
},
image: {
resizeOptions: [
{
name: "imageResize:original",
label: "Original",
value: null,
},
{
name: "imageResize:50",
label: "50%",
value: "50",
},
{
name: "imageResize:75",
label: "75%",
value: "75",
},
],
toolbar: ["imageResize", "|", "imageTextAlternative", "|", "linkImage"],
},
toolbar: {
items: [
"heading",
"|",
"bold",
"italic",
"underline",
"link",
"bulletedList",
"numberedList",
"alignment",
"|",
"imageInsert",
"mediaEmbed",
"|",
"indent",
"outdent",
"blockQuote",
"insertTable",
"|",
"strikethrough",
"subscript",
"superscript",
"|",
"code",
"codeBlock",
"|",
"removeFormat",
],
},
language: "en",
table: {
contentToolbar: ["tableColumn", "tableRow", "mergeTableCells"],
},
})
.then(function (editor) {
globalEditor = editor;
})
.catch(function (error) {
alert("Error initializing CKEditor.");
console.error(error);
});
}
// Credit https://gist.github.com/peduarte/7ee475dd0fae1940f857582ecbb9dc5f
function debounce(func) {
var wait =
arguments.length <= 1 || arguments[1] === undefined ? 100 : arguments[1];
var timeout = void 0;
return function () {
var _this = this;
for (
var _len = arguments.length, args = Array(_len), _key = 0;
_key < _len;
_key++
) {
args[_key] = arguments[_key];
}
clearTimeout(timeout);
timeout = setTimeout(function () {
func.apply(_this, args);
}, wait);
};
}
// Stores the state using localforage. For most browsers, this should go in
// IndexedDB, which also has support for blobs, which takes care of files.
function storeState() {
localforage.setItem("globalState", globalState);
}
// Saves whatever is in the current form. Called often.
function autoSave() {
switch (globalState.currentForm) {
case "page":
var title = document.getElementById("title");
var path = document.getElementById("path");
var extra = document.getElementById("extra");
updatePage(
globalState.currentPage,
title.value,
globalEditor.getData(),
path.value,
extra.value
);
break;
case "template":
var name = document.getElementById("name");
var content = document.getElementById("template");
updateTemplate(globalState.currentTemplate, name.value, content.value);
break;
case "file":
var name = document.getElementById("file-name").value;
var currentFile = globalState.files[globalState.currentFile];
var contents = document.getElementById("file-contents");
if (currentFile.name.match(PLAINTEXT_REGEXP)) {
var blob = new Blob([contents.value]);
globalState.files[globalState.currentFile] = new File(
[blob],
currentFile.name
);
}
if (name !== currentFile.name) {
var blob = currentFile.slice(0, currentFile.size, currentFile.type);
globalState.files[globalState.currentFile] = new File([blob], name);
}
if (
globalState.files[globalState.currentFile].name.match(
PLAINTEXT_REGEXP
)
) {
show(document.getElementById("file-contents-wrapper"));
} else {
hide(document.getElementById("file-contents-wrapper"));
}
break;
default:
break;
}
renderSidebar();
renderPreview(globalState.currentPage);
storeState();
}
// Sanitizes a path by stripping forward slashes and periods.
function sanitizePath(path) {
path = path.replace(/^[\/]+|[\/]+$/g, "");
path = path.replace(/(\/)\/+/g, "$1");
path = path.replace(/\./g, "");
return path;
}
// Resets the site to its default state.
function resetSite() {
globalState = {
currentPage: null,
currentTemplate: null,
currentFile: null,
currentForm: null,
pages: [],
files: [],
templates: [],
};
// prettier-ignore
addTemplate(
"html",
`<!DOCTYPE html>
<html dir="ltr" lang="en">
<meta charset="utf-8" />
<head>
<title><%= page.title %></title>
<link rel="stylesheet" href="/assets/main.css" />
</head>
<body>
<%- include("header", {page: page, site: site, pageIndex: pageIndex}); %>
<%- include("main", {page: page, site: site, pageIndex: pageIndex}); %>
<%- include("footer", {page: page, site: site, pageIndex: pageIndex}); %>
</body>
</html>`
);
// prettier-ignore
addTemplate(
"header",
`<header>
</header>`
);
// prettier-ignore
addTemplate(
"footer",
`<footer>
</footer>`
);
// prettier-ignore
addTemplate(
"main",
`<main>
<h1><%= page.title %></h1>
<div class="page-body">
<%- page.body %>
</div>
</main>`
);
// prettier-ignore
globalState.files.push(new File([`.page-body img {
max-width: 500px;
width: 100%;
height: auto;
}
`], "main.css"));
addPage("Home", "<p>Welcome to my <strong>home page</strong>!</p>", "/");
openForm("page", getPages().length - 1);
}
// Prompts the user to upload a new file.
function uploadFile() {
var input = document.createElement("input");
input.type = "file";
input.onchange = function () {
globalState.files.push(this.files[0]);
storeState();
renderSidebar();
openForm("file", globalState.files.length - 1);
};
input.click();
}
// Migrates older versions of the app state.
function migrateState(state) {
if (!state.hasOwnProperty("version") || state.version == 1) {
for (var i in state.pages) {
if (!state.pages[i].hasOwnProperty("extra")) {
state.pages[i].extra = "";
}
}
state.version = 2;
}
return state;
}
// Accepts a site archive and sets global state and files appropriately.
function uploadSite() {
var input = document.createElement("input");
input.type = "file";
input.accept = "application/zip";
input.onchange = function () {
var zip = new JSZip();
zip.loadAsync(this.files[0]).then(function (zip) {
var json = zip.file("site.json");
if (json === null) {
alert("No site.json found in archive.");
return;
}
json.async("string").then(function (content) {
try {
var processed = JSON.parse(content);
} catch (e) {
console.error(e);
alert("Unable to parse site.json");
return;
}
if (
!confirm(
"Importing this archive will erase your current site, are you sure you want to proceed?"
)
) {
return;
}
processed = migrateState(processed);
globalState = processed;
globalState.files = [];
var promises = [];
for (var i in zip.files) {
if (!i.match(/^(www\/)?assets/)) {
continue;
}
var file = zip.file(i);
if (!file) {
continue;
}
var promise = file.async("blob");
promises.push(promise);
promise.then(
(function () {
var filename = i.replace(/.*\//, "");
return function (content) {
globalState.files.push(new File([content], filename));
};
})()
);
}
Promise.all(promises).then(function (values) {
storeState();
renderSidebar();
openForm("page", globalState.currentPage);
renderPreview(globalState.currentPage);
});
});
});
};
input.click();
}
// Downloads the site, and its static HTML, as a ZIP.
function downloadSite() {
var zip = new JSZip();
files = getFiles();
for (var i in files) {
zip.file("assets/" + files[i].name, files[i]);
}
pages = getPages();
for (i in pages) {
var html = replaceAssetUrls(renderPage(i));
var path = sanitizePath(pages[i].path);
if (path !== "") {
path += "/";
}
zip.file(path + "index.html", html);
}
zip.file("site.json", JSON.stringify(globalState));
zip.generateAsync({ type: "blob" }).then(function (content) {
// Credit to https://blog.logrocket.com/programmatic-file-downloads-in-the-browser-9a5186298d5c/
var url = window.URL.createObjectURL(content);
var a = document.createElement("a");
a.href = url;
a.download = "tabcms.zip";
var clickHandler = function () {
setTimeout(function () {
window.URL.revokeObjectURL(url);
this.removeEventListener("click", clickHandler);
}, 150);
};
a.addEventListener("click", clickHandler, false);
a.click();
});
}
// Initializes events used by the app.
function initEvents() {
document.getElementById("add-page").onclick = function () {
addPage("Untitled", "", "untitled");
openForm("page", getPages().length - 1);
};
document.getElementById("add-template").onclick = function () {
addTemplate("newTemplate", "");
openForm("template", getTemplates().length - 1);
};
document.getElementById("add-file").onclick = function () {
addFile(new File([""], "untitled.css"));
openForm("file", getFiles().length - 1);
};
document.getElementById("page-list").onclick = function (event) {
if (event.target.dataset.index !== undefined) {
openForm("page", event.target.dataset.index);
}
};
document.getElementById("template-list").onclick = function (event) {
if (event.target.dataset.index !== undefined) {
openForm("template", event.target.dataset.index);
}
};
document.getElementById("file-list").onclick = function (event) {
if (event.target.dataset.index !== undefined) {
openForm("file", event.target.dataset.index);
}
};
document.getElementById("delete-page").onclick = function (e) {
e.preventDefault();
currentPage = getPage(globalState.currentPage);
if (getPages().length === 1) {
alert("You must have at least one page.");
return;
}
if (confirm(`Are you sure you want to delete ${currentPage.title}?`)) {
removePage(globalState.currentPage);
openForm("page", 0);
storeState();
}
};
document.getElementById("delete-template").onclick = function (e) {
e.preventDefault();
currentTemplate = getTemplate(globalState.currentTemplate);
if (currentTemplate.name === "html") {
alert("You cannot delete the html template.");
return;
}
if (confirm(`Are you sure you want to delete ${currentTemplate.name}?`)) {
removeTemplate(globalState.currentTemplate);
openForm("template", 0);
storeState();
}
};
document.getElementById("expand-preview").onclick = function () {
var button = document.getElementById("expand-preview");
if (button.innerText === "Expand") {
hide(document.getElementById("sidebar"));
hide(document.getElementById("main"));
} else {
show(document.getElementById("sidebar"));
show(document.getElementById("main"));
}
button.innerText = button.innerText === "Expand" ? "Collapse" : "Expand";
};
document.getElementById("delete-file").onclick = function (e) {
e.preventDefault();
currentFile = getFile(globalState.currentFile);
if (confirm(`Are you sure you want to delete ${currentFile.name}?`)) {
removeFile(globalState.currentFile);
openForm("page", 0);
storeState();
}
};
document.getElementById("download-site").onclick = function (e) {
downloadSite();
};
document.getElementById("reset-site").onclick = function (e) {
if (
!confirm(
"Are you sure you want to delete your current site and start a new one?"
)
) {
return;
}
resetSite();
};
document.getElementById("upload-site").onclick = function (e) {
uploadSite();
};
document.getElementById("upload-file").onclick = function (e) {
uploadFile();
};
autoSaveDebounce = debounce(autoSave, 500);
document.getElementById("title").onchange = autoSaveDebounce;
document.getElementById("title").onkeyup = autoSaveDebounce;
document.getElementById("path").onchange = autoSaveDebounce;
document.getElementById("path").onkeyup = autoSaveDebounce;
document.getElementById("name").onkeyup = autoSaveDebounce;
document.getElementById("template").onkeyup = autoSaveDebounce;
document.getElementById("extra").onkeyup = autoSaveDebounce;
document.getElementById("file-contents").onkeyup = autoSaveDebounce;
document.getElementById("file-name").onkeyup = autoSaveDebounce;
globalEditor.model.document.on("change:data", autoSaveDebounce);
enableTextareaTabbing(document.getElementById("file-contents"));
enableTextareaTabbing(document.getElementById("template"));
enableTextareaTabbing(document.getElementById("extra"));
}
initApp().then(function () {
initEvents();
storedState = localforage
.getItem("globalState")
.then(function (value) {
if (value) {
value = migrateState(value);
globalState = value;
renderSidebar();
renderPreview(globalState.currentPage);
openForm("page", 0);
return;
}
// Otherwise set up some defaults.
resetSite();
})
.catch(function (err) {
console.error(err);
alert(
"Unable to read the stored state. You may want to backup/delete your IndexedDB for this site."
);
});
});
})();