-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (149 loc) · 4.22 KB
/
index.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
164
165
const html = document.querySelector('.html');
const css = document.querySelector('.css');
const js = document.querySelector('.js');
const previewFrame = document.querySelector('.preview');
const editor = document.querySelector('.editor-con');
const init = () => {
html.innerHTML = `<!-- Html here -->
<div class="container">
<img
src="https://somaliprogrammers.com/wp-content/uploads/2021/04/94-1024x768.jpg"
alt="image"
/>
<div class="group-info">
<h1>Welcome Somali Programmers</h1>
<h2>Join Now</h2>
<div class="datials">
<button class="btn facebook">
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Facebook_f_logo_%282019%29.svg/900px-Facebook_f_logo_%282019%29.svg.png"
alt="facebook"
/>
<span>Facebook</span>
</button>
<button class="btn discord">
<img
src="https://assets.mofoprod.net/network/images/discord.width-250.jpg"
alt="facebook"
/>
<span>Discord</span>
</button>
<button class="btn website">
<img
src="https://scontent.fhga3-1.fna.fbcdn.net/v/t39.30808-6/327212072_568031031846328_7501166176074851834_n.jpg?_nc_cat=111&ccb=1-7&_nc_sid=5f2048&_nc_ohc=_6HpZenCqPUAX-7giwW&_nc_ht=scontent.fhga3-1.fna&oh=00_AfBJum4h2uyvQPgbvwHVTEgjblKZ6ipIS65dT9fNZMBqqQ&oe=6607C422"
alt="facebook"
/>
<span>Website</span>
</button>
</div>
</div>
</div>`;
css.innerHTML = `
/* Css here */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.container {
width: 400px;
background: #fff;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}
.container img {
width: 100%;
}
.group-info {
padding: 20px;
}
h2 {
margin: 10 0px;
color: gray;
}
.datials {
display: flex;
gap: 4px;
}
.btn {
padding: 7px 18px;
display: flex;
align-items: center;
gap: 5px;
border: none;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
cursor: pointer;
}
.btn img {
width: 25px;
}
`;
js.innerHTML = `/* Js here */
const facebook =document.querySelector(".facebook")
const discord = document.querySelector(".discord")
const website = document.querySelector(".website")
facebook.addEventListener("click",() => {
window.open('https://www.facebook.com/groups/somaliprogrammers/', '_blank');
})
discord.addEventListener("click",() => {
window.open('https://discord.gg/6ssafYS', '_blank');
})
website.addEventListener("click",() => {
window.open('https://somaliprogrammers.com/', '_blank');
}) `;
};
const saveCodeToLocalStorage = () => {
localStorage.setItem('savedHtml', html.value);
localStorage.setItem('savedCss', css.value);
localStorage.setItem('savedJs', js.value);
};
if (
!localStorage.getItem('savedHtml') ||
!localStorage.getItem('savedCss') ||
!localStorage.getItem('savedJs')
) {
init();
saveCodeToLocalStorage();
location.reload();
run();
}
const removeCodeToLocalStorage = () => {
localStorage.removeItem('savedHtml');
localStorage.removeItem('savedCss');
localStorage.removeItem('savedJs');
};
// Load code snippets from local storage if available
html.innerHTML = localStorage.getItem('savedHtml') || '';
css.innerHTML = localStorage.getItem('savedCss') || '';
js.innerHTML = localStorage.getItem('savedJs') || '';
const run = () => {
const codeContent = `${html.value} <style>${css.value}</style>`;
const doc = previewFrame.contentWindow.document;
doc.open();
doc.write(codeContent);
previewFrame.contentWindow.eval(js.value);
doc.close();
};
[html, css, js].forEach((el) => el.addEventListener('keyup', run));
// save the code
document.querySelector('.save').addEventListener('click', () => {
saveCodeToLocalStorage();
setTimeout(() => {
alert('save successfull');
}, 1000);
});
document.querySelector('.clear').addEventListener('click', () => {
removeCodeToLocalStorage();
setTimeout(() => {
alert('Clear successful');
location.reload(); // Reload the page
}, 1000);
});
// Run the code on page load
document.addEventListener('DOMContentLoaded', () => {
run();
});