-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (75 loc) · 3.13 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
(function() {
const target = document.querySelector('#variable_list');
// Persistence for keeping a theme after reaload
const persistStorageKey = 'theme';
const persistCheckboxKey = 'shouldPersist'
let shouldPersist = JSON.parse(localStorage.getItem(persistCheckboxKey));
function persistChange(key, value) {
if (!shouldPersist) return;
const theme = JSON.parse(localStorage.getItem(persistStorageKey)) || {};
theme[key] = value;
localStorage.setItem(persistStorageKey, JSON.stringify(theme));
}
function getPersisted(key) {
const theme = JSON.parse(localStorage.getItem(persistStorageKey)) || {};
return theme[key];
}
(function setPersisted(){
const theme = JSON.parse(localStorage.getItem(persistStorageKey)) || {};
Object.keys(theme).forEach(key => {
document.documentElement.style.setProperty(key, theme[key]);
})
})();
(function persistElement(){
const div = document.createElement('div')
div.classList.add('persist_box');
const checkbox = document.createElement('input')
checkbox.setAttribute('type', 'checkbox')
checkbox.setAttribute('id', 'persist')
checkbox.checked = shouldPersist;
checkbox.onchange = (e) => {
localStorage.setItem(persistCheckboxKey, JSON.stringify(e.target.checked));
shouldPersist = e.target.checked
};
const label = document.createElement('label')
label.setAttribute('for', 'persist')
label.innerText = "Persist changes"
const clear = document.createElement('button')
clear.innerText = 'clear storage';
clear.onclick = () => {
delete localStorage[persistStorageKey];
delete localStorage[persistCheckboxKey];
};
div.appendChild(checkbox);
div.appendChild(label)
div.appendChild(clear)
target.appendChild(div);
})()
// Get css variables from stylesheets
function getVariables() {
return [].slice.call(document.styleSheets)
.map(styleSheet => [].slice.call(styleSheet.cssRules)).flat()
.filter(cssRule => cssRule.selectorText === ':root')
.map(cssRule => cssRule.cssText.split('{')[1].split('}')[0].trim().split(';'))
.flat().filter(text => text !== "")
.map(text => text.split(':'))
.map(parts => ({ key: parts[0].trim(), value: getPersisted(parts[0].trim()) || parts[1].trim() }) );
;
}
// Create the inputs to change variables
getVariables().forEach(v => {
const div = document.createElement('div');
div.classList.add('var_box');
const text = document.createTextNode(v.key);
const input = document.createElement('input');
input.setAttribute('name', 'in');
input.value = v.value;
input.oninput = (e) => {
document.documentElement.style.setProperty(v.key, e.target.value);
persistChange(v.key, e.target.value);
}
div.appendChild(text);
div.appendChild(input);
target.appendChild(div);
});
})()