-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
160 lines (145 loc) · 4.76 KB
/
editor.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
// Editor Settings (Provided by C9)
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.setShowPrintMargin(false);
editor.setFontSize(22);
editor.session.setMode("ace/mode/c_cpp");
editor.setValue(
"#include <stdio.h>\n\n\nint main() {\n // Complete the code.\n return 0;\n}\n"
);
editor.clearSelection();
var lang = document.getElementById("lang");
var langmode = lang.options[lang.selectedIndex].value;
if (langmode == "c" || langmode == "cpp") {
editor.session.setMode("ace/mode/c_cpp");
}
if (langmode == "python3") {
editor.session.setMode("ace/mode/python");
}
if (langmode == "java") {
editor.session.setMode("ace/mode/java");
}
var uth = document.getElementById("mode2");
var uthValue = uth.options[uth.selectedIndex].value;
editor.setTheme("ace/theme/" + uthValue);
var ufo = document.getElementById("mode3");
var ufoValue = ufo.options[ufo.selectedIndex].value;
editor.setFontSize(ufoValue);
// Function to change the mode of the editor as a different language is selected dynamically
function changeMode() {
var x = document.getElementById("lang");
var modeValue = x.options[x.selectedIndex].value;
if (modeValue == "c") {
editor.session.setMode("ace/mode/c_cpp");
editor.setValue(
"#include <stdio.h>\n\n\nint main() {\n // Complete the code.\n return 0;\n}\n"
);
editor.clearSelection();
document.getElementById("langExt").innerHTML = "c";
}
if (modeValue == "cpp") {
editor.session.setMode("ace/mode/c_cpp");
editor.setValue(
"#include <iostream>\nusing namespace std;\n\nint main() {\n // Complete the code.\n return 0;\n}\n"
);
editor.clearSelection();
document.getElementById("langExt").innerHTML = "cpp";
}
if (modeValue == "python3") {
editor.session.setMode("ace/mode/python");
editor.setValue(
"# Enter your code here. Read input from STDIN. Print output to STDOUT\n"
);
editor.clearSelection();
document.getElementById("langExt").innerHTML = "py";
}
if (modeValue == "java") {
editor.session.setMode("ace/mode/java");
editor.setValue(
"import java.io.*;\nimport java.util.*;\n\npublic class Main {\n\n public static void main(String[] args) {\n // Your code goes here\n }\n}\n"
);
editor.clearSelection();
document.getElementById("langExt").innerHTML = "java";
}
}
function change2mode() {
var ut = document.getElementById("mode2");
var mode2Value = ut.options[ut.selectedIndex].value;
if (mode2Value == "monokai") {
editor.setTheme("ace/theme/monokai");
}
if (mode2Value == "chrome") {
editor.setTheme("ace/theme/chrome");
}
if (mode2Value == "solarized_light") {
editor.setTheme("ace/theme/solarized_light");
}
if (mode2Value == "solarized_dark") {
editor.setTheme("ace/theme/solarized_dark");
}
if (mode2Value == "vibrant_ink") {
editor.setTheme("ace/theme/vibrant_ink");
}
}
function change3mode() {
var ft = document.getElementById("mode3");
var mode2Value = ft.options[ft.selectedIndex].value;
if (mode2Value == 16) {
editor.setFontSize(16);
}
if (mode2Value == 18) {
editor.setFontSize(18);
}
if (mode2Value == 22) {
editor.setFontSize(22);
}
if (mode2Value == 24) {
editor.setFontSize(24);
}
if (mode2Value == 26) {
editor.setFontSize(26);
}
}
function saveTextAsFile() {
var codeText = editor.getValue();
// console.log(codeText);
var textFileAsBlob = new Blob([codeText], { type: "text/plain" });
// var fileNameToSaveAs = "ecc.java";
// save file as per language
var sf = document.getElementById("lang");
var modeValue = sf.options[sf.selectedIndex].value;
if (modeValue == "c") {
var fileNameToSaveAs = "code.c";
}
if (modeValue == "cpp") {
var fileNameToSaveAs = "code.cpp";
}
if (modeValue == "python3") {
var fileNameToSaveAs = "code.py";
}
if (modeValue == "java") {
var fileNameToSaveAs = "code.java";
}
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null) {
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
} else {
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
var button = document.getElementById("save");
button.addEventListener("click", saveTextAsFile);
// $(document).ready(function () {
// $(".selectpicker").selectpicker();
// $('[data-toggle="tooltip"]').tooltip();
// });