Repository navigation
Expand file tree
/
Copy pathCode-Editor.html
More file actions
203 lines (178 loc) · 6 KB
/
Copy pathCode-Editor.html
File metadata and controls
203 lines (178 loc) · 6 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Code-Editor</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: poppins, Verdana, Geneva, Tahoma, sans-serif !important;
}
body {
width: 100%;
height: 100vh;
display: flex;
background-color: #00091b;
background: hsla(220, 100%, 5%, 1);
background: linear-gradient(
135deg,
hsla(220, 100%, 5%, 1) 0%,
hsla(271, 100%, 14%, 1) 100%
);
background: -moz-linear-gradient(
135deg,
hsla(220, 100%, 5%, 1) 0%,
hsla(271, 100%, 14%, 1) 100%
);
background: -webkit-linear-gradient(
135deg,
hsla(220, 100%, 5%, 1) 0%,
hsla(271, 100%, 14%, 1) 100%
);
filter: progid: DXImageTransform.Microsoft.gradient( startColorstr="#00091b", endColorstr="#240046", GradientType=1 );
}
#leftSide {
flex-basis: 50%;
height: 100%;
display: flex;
flex-direction: column;
background-color: #10002b;
background: hsla(271, 100%, 14%, 1);
background: linear-gradient(
360deg,
hsla(271, 100%, 14%, 1) 0%,
hsla(262, 100%, 8%, 1) 100%
);
background: -moz-linear-gradient(
360deg,
hsla(271, 100%, 14%, 1) 0%,
hsla(262, 100%, 8%, 1) 100%
);
background: -webkit-linear-gradient(
360deg,
hsla(271, 100%, 14%, 1) 0%,
hsla(262, 100%, 8%, 1) 100%
);
filter: progid: DXImageTransform.Microsoft.gradient( startColorstr="#240046", endColorstr="#10002B", GradientType=1 );
}
.code {
flex-basis: 33%;
background-color: transparent;
}
#leftSide .code textarea{
width: calc(100% - 20px);
margin-left: 2%;
padding: 10px;
height: calc(85% - 20px);
background-color: transparent;
outline: none;
color: whitesmoke;
border-radius: 2px solid transparent;
}
#leftSide .code label {
text-align: center;
font-size: medium;
display: block;
background-color: transparent;
color: whitesmoke;
padding: 10px 0px;
}
#rightSide {
flex-basis: 50%;
height: 100%;
background-color: #240046;
background: hsla(271, 100%, 11%, 1);
background: linear-gradient(
225deg,
hsla(271, 100%, 11%, 1) 0%,
hsla(270, 92%, 9%, 1) 100%
);
background: -moz-linear-gradient(
225deg,
hsla(271, 100%, 11%, 1) 0%,
hsla(270, 92%, 9%, 1) 100%
);
background: -webkit-linear-gradient(
225deg,
hsla(271, 100%, 11%, 1) 0%,
hsla(270, 92%, 9%, 1) 100%
);
filter: progid: DXImageTransform.Microsoft.gradient( startColorstr="#1D0038", endColorstr="#18022E", GradientType=1 );
}
#rightSide label {
text-align: center;
font-size: medium;
display: block;
background-color: transparent;
color: whitesmoke;
padding: 10px;
margin: 2%;
}
#rightSide iframe {
display: block;
width: 100%;
padding-left: 2%;
border: 2px solid whitesmoke;
box-sizing: border-box;
height: 90%;
border: none;
}
</style>
</head>
<body>
<div id="leftSide">
<div class="code">
<label for="HTML">HTML</label>
<textarea name="HTML" id=""></textarea>
</div>
<div class="code">
<label for="CSS">CSS</label>
<textarea name="CSS" id=""></textarea>
</div>
<div class="code">
<label for="JAVASCRIPT">JAVASCRIPT</label>
<textarea name="JAVASCRIPT" id=""></textarea>
</div>
</div>
<div id="rightSide">
<label for="OUTPUT">OUTPUT</label>
<iframe id="OUTPUT" src="" frameborder="0"></iframe>
</div>
</body>
<script>
let OUTPUT = document.querySelector("#OUTPUT");
let allInput = document.querySelectorAll("#leftSide textarea");
// Variables to store the content of HTML, CSS, and JavaScript from the textarea inputs
let htmlCode, cssCode, jsCode = "";
// Loop through each textarea element and listen for the 'keyup' event
allInput.forEach((el, index) => {
el.addEventListener("keyup", () => {
// Assign the content of the respective textarea to the corresponding variable
if(index == 0){
htmlCode = el.value; // Assign HTML content
}
if(index == 1){
cssCode = el.value; // Assign CSS content
}
if(index == 2){
jsCode = el.value; // Assign JavaScript content
}
// This is where the magic happens - each code is applied to the iframe
// 1. OUTPUT.contentDocument.body.innerHTML = htmlCode;
// - Sets the HTML content of the iframe body. The content of the HTML textarea is assigned to the body of the iframe.
OUTPUT.contentDocument.body.innerHTML = htmlCode;
// 2. OUTPUT.contentDocument.head.innerHTML = `<style>${cssCode}</style>`;
// - Adds the CSS content inside a <style> tag in the head of the iframe document.
// - This dynamically styles the HTML content inside the iframe according to the CSS input.
OUTPUT.contentDocument.head.innerHTML = `<style>${cssCode}</style>`;
// 3. OUTPUT.contentWindow.eval(jsCode);
// - Executes the JavaScript code inside the iframe's window context.
// - This allows dynamic behavior (like functions, interactions, etc.) to be applied to the HTML content.
OUTPUT.contentWindow.eval(jsCode);
});
})
</script>
</html>