-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (82 loc) · 2.2 KB
/
index.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
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import './style.css'
import './wasm_exec.js'
let fontSize = '24px';
let defaultJSON = `// This JSON data will be available to
// HCL
{
"reasonsThisIsBad": ["lacks error handling", "has limited tag support", "is slow"]
}`;
let defaultHCL = `h1 {
marquee {
innerText = "This is a bad idea"
}
}
p {
innerText = "Among other things, it..."
}
ul {
dynamic "li" {
for_each = reasonsThisIsBad
innerText = "...\${for_each}"
style = "color: red"
}
}
p {
innerText = "You should only use it if you have at least \${length(reasonsThisIsBad) * 123} good reasons."
}`;
try {
let loadedDoc = JSON.parse(decodeURI(window.location.hash.substr(1)));
defaultJSON = loadedDoc.json;
defaultHCL = loadedDoc.hcl;
} catch(e) {
console.warn("Error loading hcl/json from hash", e);
}
const jsonEditor = monaco.editor.create(document.getElementById('jsonEditor'), {
language: 'json',
theme: 'vs-dark',
automaticLayout: true,
fontSize,
minimap: {
enabled: false
},
value: defaultJSON
});
const hclEditor = monaco.editor.create(document.getElementById('hclEditor'), {
language: 'hcl',
theme: 'vs-dark',
automaticLayout: true,
minimap: {
enabled: false
},
fontSize,
value: defaultHCL
});
const output = document.getElementById('output');
let oldHcl = null;
let oldContext = null;
setInterval(function() {
// It takes a while for the go wasm to download, parse, and execute. Until that happens, any functions it exposes won't be defined
if (typeof parse_hcl === undefined) {
return;
}
try {
const hcl = hclEditor.getValue();
let json = jsonEditor.getValue();
if (oldHcl === hcl && oldContext === json) {
return;
}
oldHcl = hcl;
oldContext = json;
window.location.hash = JSON.stringify({hcl, json});
// JSON doesn't support comments, but it's nice to put a comment explaining the top window. This
// strips out any comments to avoid angering JSON.stringify
json = json.replaceAll(/\/\/.*/g, '')
const context = JSON.parse(json);
console.log('Execution context', context);
console.log('HCL', hcl);
parse_hcl(hcl, context, output)
} catch(e) {
console.error(e);
}
}, 1000);