-
Notifications
You must be signed in to change notification settings - Fork 0
/
render-computed-styles.js
121 lines (110 loc) · 3.97 KB
/
render-computed-styles.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
let resizeTimeout;
renderCompStyles = function(update = false) {
// If not resizing
if( ! update ) {
// Remove all previous localStorage instances of csElements
let styleStorage = [];
for( let key in localStorage ) {
if( key.indexOf('csElement') > -1 ) {
styleStorage.push(key);
}
}
(styleStorage.length ? window.localStorage.removeItem(styleStorage) : null)
// Generate stylesheet
var stylesheet = document.createElement('style');
stylesheet.type = 'text/css';
var css = `
.computedStyles {
position: relative;
z-index: 99;
background: #263238;
color:white;
font-family: monospace;
font-size: 16px;
padding: 16px;
border-radius: 5px;
display: inline-flex;
flex-flow: column nowrap;
}
.is-updating {
display: inline-block;
animation: updatePulse 1s forwards;
color: #00E676;
}
@keyframes updatePulse {
0% {
transform: scale(0);
}
50% {
transform: scale(1.25);
}
100% {
transform: scale(1);
}
}
`;
stylesheet.innerHTML = css;
document.getElementsByTagName('head')[0].appendChild(stylesheet);
}
// Function to generate styleIds
function generateStyleId(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
// Get all elements with the [data-render-styles] attribute
let elements = document.querySelectorAll('[data-render-styles]');
elements.forEach(element => {
let styleAttributes = element.getAttribute('data-render-styles').replace(/\s/g, "").split(',');
let computedStylesData = JSON.stringify(window.getComputedStyle(element));
let elementStyleId = 'csElement-'+ generateStyleId(6);
let stylesBlock = element.nextElementSibling;
renderStyleData = function(elementStyleId) {
let elementStyleData = JSON.parse(window.localStorage.getItem(elementStyleId));
if( ! update ) {
element.insertAdjacentHTML('afterend', `<div class="computedStyles" />`);
}
// Loop throught style attributes
styleAttributes.forEach(attribute => {
if( ! element.nextElementSibling.querySelector(`[data-attribute=${attribute}]`) ) {
element.nextElementSibling.insertAdjacentHTML('beforeend',
`<div class="computedStyles__item" data-attribute=${attribute}>
<strong>${attribute}</strong>: <span class="computedStyles__value"></span>;
</div>`
)
}
// Check if attribute exists in elementStyleData (localStorage)
if( attribute in elementStyleData ) {
let csValue = element.nextElementSibling.querySelector(`[data-attribute=${attribute}] .computedStyles__value`);
if( csValue.innerHTML !== elementStyleData[attribute] ) {
csValue.innerHTML = elementStyleData[attribute];
csValue.classList.add('is-updating');
csValue.addEventListener('animationend', function() {
csValue.classList.remove('is-updating');
});
}
}
})
}
if( ! update ) {
element.setAttribute('data-style-id', elementStyleId)
window.localStorage.setItem(elementStyleId, computedStylesData);
renderStyleData(elementStyleId);
} else {
let elementStyleId = element.getAttribute('data-style-id');
window.localStorage.setItem(elementStyleId, computedStylesData);
renderStyleData(elementStyleId);
}
});
// Resize functionality
window.addEventListener('resize', function() {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(function() {
return renderCompStyles(true);
}, 100);
});
}