-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbundle.js
69 lines (51 loc) · 1.92 KB
/
bundle.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
(function (jsonSchemaToTypescript) {
'use strict';
const leftInput = document.querySelector('#leftInput textarea');
const rightOutput = document.querySelector('#rightOutput textarea');
const errorIcon = document.getElementById('errorIcon');
leftInput.addEventListener('input', jsonSchemaToTypescript.compile);
const localStorageKey = 'json-schema-to-typescript';
const content = localStorage.getItem(localStorageKey);
if (content) {
leftInput.value = content;
}
const options = {
declareExternallyReferenced: true,
enableConstEnums: true,
unreachableDefinitions: false,
strictIndexSignatures: false
};
// expose options for advance users
window.options = options;
console.info("Welcome! If you'd like to play around with more advance options,", "you can mutate the 'options' object assigned to window :)");
Object.keys(options).forEach(option => {
const optionCheckbox = document.getElementById(option);
if (!(optionCheckbox instanceof HTMLInputElement)) {
console.warn(`"${option}" is missing an config element`);
return;
}
options[option] = optionCheckbox.checked;
optionCheckbox.addEventListener('change', () => {
options[option] = optionCheckbox.checked;
update();
});
});
// initial compile
update();
function update() {
localStorage.setItem(localStorageKey, leftInput.value);
Promise.resolve()
.then(() => JSON.parse(leftInput.value))
.then(json => jsonSchemaToTypescript.compile(json, 'Demo', options))
.then(ts => (rightOutput.value = ts))
.then(() => (errorIcon.style.display = 'none'))
.catch(e => {
console.error(e);
errorIcon.style.display = null;
});
}
document.getElementById('formatButton').addEventListener('click', format);
function format() {
leftInput.value = prettier.format(leftInput.value, { parser: 'json-stringify' });
}
})(jsonSchemaToTypescript);