-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.js
85 lines (74 loc) · 2.08 KB
/
debug.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
export class debugSassMqRatio {
constructor() {
this.init();
}
init() {
this.cssVarNames = this.getCssVarNames();
if (this.isDebug()) {
//console.log(this.cssVarNames);
this.addHtmlToBody(this.getDebugHtml());
if (module.hot) {
module.hot.accept();
module.hot.dispose(function () {
const mqElement = document.querySelector(".mq-container");
if (mqElement) {
mqElement.remove();
}
// le module est sur le point d'être remplacé
});
}
}
}
isDebug() {
console.warn(
"debug helper is active, you can disable it with $mq-debug: false;"
);
return this.cssVarNames.includes("--mq-debug");
}
getDebugHtml() {
let html = ``;
html += `<div class="mq-container">
<div class="mq-ratio-models">
<div class="mq-portrait-ratio"></div>
<div class="mq-square-ratio"></div>
<div class="mq-landscape-ratio"></div>
</div>
<div class="mq-rules">${this.cssVarNames
.map((name) => `<div class="${name}"></div>`)
.join("")}</div>
</div>`;
return html;
}
addHtmlToBody(html) {
document.querySelector("body").innerHTML += html;
}
getCssVarNames() {
const rules = Array.from(document.styleSheets)
.filter(
(sheet) =>
sheet.href === null || sheet.href.startsWith(window.location.origin)
)
.reduce(
(acc, sheet) =>
(acc = [
...acc,
...Array.from(sheet.cssRules).reduce(
(def, rule) =>
(def =
rule.selectorText === ":root"
? [
...def,
...Array.from(rule.style).filter((name) =>
name.startsWith("--")
),
]
: def),
[]
),
]),
[]
);
return rules;
}
}
export default new debugSassMqRatio();