-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeEditor.js
100 lines (83 loc) · 3.07 KB
/
codeEditor.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
class CodeEditor {
constructor(elementId, options = {}) {
this.editor = CodeMirror.fromTextArea(document.getElementById(elementId), {
lineNumbers: true,
mode: "javascript",
theme: "monokai",
autoCloseBrackets: true,
viewportMargin: Infinity, // This allows the editor to expand
...options
});
this.outputDiv = document.getElementById(options.outputId || "output");
this.runButton = document.getElementById(options.runButtonId || "run-button");
this.runButton.addEventListener("click", () => this.runCode());
}
setValue(code) {
this.editor.setValue(code);
}
runCode() {
this.outputDiv.innerHTML = "";
const code = this.editor.getValue();
const originalConsoleLog = console.log;
console.log = (...args) => {
this.outputDiv.innerHTML += "<p>" + args.join(" ") + "</p>";
originalConsoleLog.apply(console, args);
};
try {
eval(code);
} catch (error) {
console.log("Error: " + error.message);
}
console.log = originalConsoleLog;
}
}
const menuItems = [
{ href: "intro.html", text: "Intro" },
{ href: "variablar.html", text: "Variablar" },
{ href: "funktioner.html", text: "Funktioner" },
{ href: "integer.html", text: "Integer" },
{ href: "string.html", text: "String" },
{ href: "array.html", text: "Array" },
{ href: "if-else.html", text: "If Else" },
{ href: "loops.html", text: "Loops" },
{ href: "html.html", text: "HTML" },
{ href: "events.html", text: "Events" }
];
function generateNavMenu() {
const navMenu = document.createElement('nav');
navMenu.id = 'navMenu';
const currentIndex = getCurrentIndex();
menuItems.forEach((item, index) => {
const link = document.createElement('a');
link.href = item.href;
link.textContent = item.text;
if (index === currentIndex) {
link.classList.add('current');
}
navMenu.appendChild(link);
});
if (currentIndex > 0) {
const prevButton = document.createElement('button');
prevButton.innerHTML = '«';
prevButton.className = 'nav-button';
prevButton.onclick = () => navigateTo(menuItems[currentIndex - 1].href);
navMenu.prepend(prevButton);
}
if (currentIndex < menuItems.length - 1) {
const nextButton = document.createElement('button');
nextButton.innerHTML = '»';
nextButton.className = 'nav-button';
nextButton.onclick = () => navigateTo(menuItems[currentIndex + 1].href);
navMenu.appendChild(nextButton);
}
document.getElementById('navContainer').appendChild(navMenu);
}
function getCurrentIndex() {
const currentPath = window.location.pathname.split('/').pop();
return menuItems.findIndex(item => item.href === currentPath);
}
function navigateTo(href) {
window.location.href = href;
}
// Call the function to generate the menu
generateNavMenu();