-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.js
More file actions
111 lines (101 loc) · 2.77 KB
/
main.js
File metadata and controls
111 lines (101 loc) · 2.77 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
const el = {
create(tag, attrs) {
const el = Object.assign(document.createElement(tag), attrs);
document.querySelector('head').appendChild(el);
},
remove(selector) {
document.querySelectorAll(selector)?.forEach(el => el.remove());
}
}
let {href} = window.location;
let url = new URL(href);
let file = url.searchParams.get('file');
const checkFile = async(file) => {
try {
const req = await fetch(file);
console.log(req);
} catch (error) {
console.error(error);
}
}
if (file) {
const files = file.split('|');
checkFile(file);
files.forEach(file => {
const link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('href', file);
document.querySelector('head').appendChild(link);
});
}
if (url.searchParams.get('lightTheme') === "true") {
document.querySelector('html').classList.remove('theme-dark');
document.querySelector('html').classList.add('theme-light');
}
window.addEventListener('message', event => {
const data = JSON.parse(event.data);
const actions = {
setPreview() {
document.querySelector('#preview').textContent = data.text;
},
reset() {
const props = document.documentElement.getAttribute('style').split(';').slice(0, 2).map(e => e += ';').join(' ');
document.documentElement.setAttribute('style', props);
},
setProp() {
document.documentElement.style.setProperty(`--${data.variable}`, data.value);
},
removeProp() {
document.documentElement.style.removeProperty(`--${data.variable}`);
},
addFont() {
const tag = document.querySelector(`#font-${data.index}`)
if (!tag) {
el.create('style', {
id: `font-${data.index}`,
className: 'customfont',
innerText: data.text
})
} else {
tag.innerHTML = data.text;
}
},
removeFont() {
el.remove(`#font-${data.index}`)
},
addAddon() {
if (!document.querySelector(`.${data.class}`)) {
el.create('style', {
className: data.class,
textContent: `@import url('${data.text}')`
});
}
},
removeAddon() {
el.remove(`.${data.class}`);
},
toggleModal() {
if (data.visible) {
document.querySelector('#modal').classList.remove('HIDDEN');
document.querySelector('#popout').classList.add('HIDDEN');
} else {
document.querySelector('#modal').classList.add('HIDDEN');
document.querySelector('#popout').classList.remove('HIDDEN');
}
},
toggleTheme() {
if (document.documentElement.classList.contains('theme-dark')) {
document.querySelectorAll('.theme-dark')?.forEach(el => {
el.classList.add('theme-light');
el.classList.remove('theme-dark');
});
} else {
document.querySelectorAll('.theme-light')?.forEach(el => {
el.classList.add('theme-dark');
el.classList.remove('theme-light');
});
}
}
}
actions[data.action]?.();
})