-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
100 lines (87 loc) · 3.24 KB
/
scripts.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
document.addEventListener("DOMContentLoaded", function() {
const currentTheme = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', currentTheme);
const faders = document.querySelectorAll('.fade-in-section');
const appearOptions = {
threshold: 0.1,
rootMargin: "0px 0px -100px 0px"
};
const appearOnScroll = new IntersectionObserver(function(entries, appearOnScroll) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('is-visible');
} else {
entry.target.classList.remove('is-visible');
}
});
}, appearOptions);
faders.forEach(fader => {
appearOnScroll.observe(fader);
});
const codeTextArea = document.getElementById('code');
const codeEditor = CodeMirror.fromTextArea(codeTextArea, {
mode: 'text/x-csrc',
lineNumbers: true,
theme: 'monokai'
});
const outputTextArea = document.getElementById('output');
const outputEditor = CodeMirror.fromTextArea(outputTextArea, {
mode: 'shell',
lineNumbers: true,
theme: 'monokai',
readOnly: true
});
// make both textareas larger
codeEditor.setSize(null, 500);
outputEditor.setSize(null, 500);
// Function to load example code into the editor
function loadExample(filename) {
fetch("examples/" + filename).then(response => response.text()).then(text => {
codeEditor.setValue(text);
}).catch(error => {
console.error('Error loading file:', error);
});
}
// Load the initial example
loadExample('sum.c');
// On click of fileselect load the selected file
document.getElementById('fileSelect').addEventListener('change', function() {
const selectedFile = this.value;
loadExample(selectedFile);
});
function c_to_shell(c_code) {
stdoutArr = [];
let ModuleArgs = {
'print': function(text) { stdoutArr.push(text); },
};
create_module(ModuleArgs).then((module) => {
module.FS.writeFile("code.c", c_code);
var compile = module.cwrap("compile", "void", ["string"]);
try {
compile("code.c");
} catch (e) {
if (e instanceof module.ExitStatus && e.status == 0) {
console.log("Compilation successful");
} else {
console.log("Failed to compile");
}
}
outputEditor.setValue(stdoutArr.join("\n"));
});
}
// Add event listener to the Compile button
document.getElementById('compileButton').addEventListener('click', () => {
const code = codeEditor.getValue();
c_to_shell(code);
});
// Add event listener to the Copy button
document.getElementById('copyButton').addEventListener('click', () => {
const code = codeEditor.getValue();
navigator.clipboard.writeText(code);
});
// Add event listener to the Copy button for the compiler output
document.getElementById('copyButton2').addEventListener('click', () => {
const output = outputEditor.getValue();
navigator.clipboard.writeText(output);
});
});