-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
163 lines (141 loc) · 4.15 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
let loaded = false;
function updateButton() {
if (loaded) {
document.getElementById('runbutton').removeAttribute('disabled');
}
}
window.addEventListener("load", function(event) {
loaded = true;
updateButton();
});
const perlDoc = new CodeMirror.Doc('', 'perl6');
const htmlDoc = new CodeMirror.Doc('', 'htmlmixed');
const cssDoc = new CodeMirror.Doc('', 'css');
const editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
theme: 'zenburn',
lineWrapping: true,
cursorHeight: 0.85,
tabSize: 4,
indentUnit: 4,
// Increase the number of lines that are rendered above and before
viewportMargin: 100,
autofocus: false,
autoCloseBrackets: true,
matchBrackets: true,
});
editor.swapDoc(perlDoc);
document.getElementById('runbutton').addEventListener('click', function() { const output = document.getElementById('output');
while (output.firstChild) {
output.removeChild(output.firstChild);
}
const code = perlDoc.getValue();
window.evalP6(code);
});
document.getElementById('sharebutton').addEventListener('click', function() {
window.open('https://github.com/perl6/6pad/wiki/Sharing-Guide', '_blank');
});
class Tabs {
constructor(tabs, defaultIndex) {
this.tabs = tabs;
this.selected = tabs[defaultIndex].button;
for (const tab of tabs) {
tab.button.addEventListener('click', () => {
this.select(tab);
});
}
}
select(tab) {
tab.action();
this.selected.removeAttribute('selected');
tab.button.setAttribute('selected', '');
this.selected = tab.button;
}
}
new Tabs([
{
button: document.getElementById("perltab"),
action: function() {editor.swapDoc(perlDoc)}
},
{
button: document.getElementById("htmltab"),
action: function() {editor.swapDoc(htmlDoc)}
},
{
button: document.getElementById("csstab"),
action: function() {editor.swapDoc(cssDoc)}
},
], 0);
const output = document.getElementById("output");
const frame = document.getElementById("frame");
const outputTabs = new Tabs([
{
button: document.getElementById("resulttab"),
action: function() {
output.style.visibility = 'hidden';
frame.style.visibility = 'visible';
}
},
{
button: document.getElementById("consoletab"),
action: function() {
output.style.visibility = 'visible';
frame.style.visibility = 'hidden';
}
},
], 1);
async function loadGist(gist) {
const response = await fetch("https://api.github.com/gists/" + gist);
const json = await response.json();
for (const fileName in json.files) {
const file = json.files[fileName];
if (fileName === 'main.p6') {
perlDoc.setValue(file.content);
} else if (fileName === 'index.html') {
htmlDoc.setValue(file.content);
outputTabs.select(outputTabs.tabs[0]);
} else if (fileName === 'styles.css') {
cssDoc.setValue(file.content);
} else {
window.NQP_STDOUT('Unrecognized file in gist: ' + fileName);
}
}
}
console.log('document.location.hash', document.location.hash);
if (document.location.hash) {
console.log('hash', document.location.hash);
loadGist(document.location.hash.substr(1));
}
window.NQP_STDOUT = function(str) {
const span = document.createElement('span');
span.innerHTML = str;
document.getElementById('output').appendChild(span);
};
const samples = document.getElementById('samples');
samples.onchange = function() {
if (samples.value && samples.value != '0') {
document.location.hash = samples.value;
loadGist(samples.value);
}
}
class Reconciler {
constructor(source, set) {
this.source = source;
this.targetValue = '';
this.set = set;
window.setInterval(() => {
const newValue = this.source.getValue();
if (newValue != this.targetValue) {
this.set(newValue);
this.targetValue = newValue;
}
});
}
};
new Reconciler(htmlDoc, html => frame.innerHTML = html);
const cssPart = document.getElementById('csspart');
new Reconciler(cssDoc, css => {
while (cssPart.firstChild) {
cssPart.removeChild(cssPart.firstChild);
}
cssPart.appendChild(document.createTextNode(css));
});