|
8 | 8 |
|
9 | 9 | <script lightwindsrc="./lightTheme.json">
|
10 | 10 | {
|
11 |
| - "use strict"; |
12 |
| - |
13 | 11 | // Init style tag where data = lightwindcss
|
14 | 12 | let styleLightWind = document.createElement('style');
|
15 | 13 | styleLightWind.setAttribute('data', 'lightwindcss');
|
16 | 14 | styleLightWind.textContent = '.hidden{display:none;}.invisible{visibility:hidden;}'
|
17 | 15 | document.head.appendChild(styleLightWind);
|
18 | 16 |
|
| 17 | + let res; |
| 18 | + |
| 19 | + (async (res) => { |
| 20 | + res = await fetch(document.querySelector('[lightwindsrc]').getAttribute('lightwindsrc')) |
| 21 | + res = await res.json() |
| 22 | + })(res) |
| 23 | + |
19 | 24 | let allClasses = [];
|
20 | 25 |
|
21 |
| - async function resolveClass(className) { |
22 |
| - let date = new Date(); |
23 |
| - let lasttime = date.getTime() |
24 |
| - let classSubParams = className.split(':'), styleStr = '', screenBreakPointOpen = false |
| 26 | + async function resolveClass(className, res) { |
| 27 | + let classSubParams = className.split(':'), screenBreakPointOpen = false; |
| 28 | + |
25 | 29 | for (i in classSubParams) {
|
26 | 30 | if (i == classSubParams.length - 1) {
|
27 |
| - // value + name getting |
28 |
| - let value = classSubParams[i].split('>')[1], name = classSubParams[i].split('>')[0], resp = ''; |
29 |
| - let res = await fetch(document.querySelector('[lightwindsrc]').getAttribute('lightwindsrc')), strRet = '' |
30 |
| - res = await res.json() |
31 |
| - if (resp == '') { |
32 |
| - resp = `{${name}:${value};}` |
| 31 | + // value |
| 32 | + let name = classSubParams[i].split('>')[0], value = classSubParams[i].split('>')[1] |
| 33 | + try { |
| 34 | + if (typeof(value) == 'undefined') { |
| 35 | + // value only |
| 36 | + styleLightWind.textContent += `.${className.replace(/\:/g, '\\:').replace(/\[/g, '\\[').replace(/\]/g, '\\]').replace(/\>/g, '\\>').replace(/\//g, '\\/').replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\%/g, '\\%').replace(/\-/g, '\\-').replace(/\+/g, '\\+').replace(/\*/g, '\\*').replace(/\,/g, '\\,').replace(/\|/g, '\\|')}{${res.proprieties.valueOnly[name].css}}` |
| 37 | + } |
| 38 | + else { |
| 39 | + styleLightWind.textContent += `.${className.replace(/\:/g, '\\:').replace(/\[/g, '\\[').replace(/\]/g, '\\]').replace(/\>/g, '\\>').replace(/\//g, '\\/').replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\%/g, '\\%').replace(/\-/g, '\\-').replace(/\+/g, '\\+').replace(/\*/g, '\\*').replace(/\,/g, '\\,').replace(/\|/g, '\\|')}{${res.proprieties.valueKey[name].propriety}:${value};}` |
| 40 | + } |
| 41 | + } catch { |
| 42 | + styleLightWind.textContent += `.${className.replace(/\:/g, '\\:').replace(/\[/g, '\\[').replace(/\]/g, '\\]').replace(/\>/g, '\\>').replace(/\//g, '\\/').replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\%/g, '\\%').replace(/\-/g, '\\-').replace(/\+/g, '\\+').replace(/\*/g, '\\*').replace(/\,/g, '\\,').replace(/\|/g, '\\|')}{${name}:${value};}` |
33 | 43 | }
|
34 |
| - styleStr = styleStr + `.${className.replace(/\:/g, '\\:').replace(/\[/g, '\\[').replace(/\]/g, '\\]').replace(/\>/g, '\\>').replace(/\//g, '\\/').replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\%/g, '\\%').replace(/\-/g, '\\-').replace(/\+/g, '\\+').replace(/\*/g, '\\*').replace(/\,/g, '\\,').replace(/\|/g, '\\|')}` + resp |
| 44 | + |
35 | 45 | if (screenBreakPointOpen)
|
36 |
| - styleStr += '}' |
| 46 | + styleLightWind.textContent += '}' |
37 | 47 | }
|
38 | 48 | else if (i == 0) {
|
39 |
| - // screen async search |
40 |
| - |
41 |
| - let resp = async function(classSubParams, screenBreakPointOpen, i, className) { |
42 |
| - let res = await fetch(document.querySelector('[lightwindsrc]').getAttribute('lightwindsrc')), strRet = '' |
43 |
| - res = await res.json() |
44 |
| - res.default.breakpoints.screens.forEach(async (screen) => { |
45 |
| - if (screen.name == classSubParams[i]) { |
46 |
| - screenBreakPointOpen = true; |
47 |
| - if (screen.min != null && screen.max != null) { |
48 |
| - strRet = `@media screen and (min-width: ${screen.min}) and (max-width: ${screen.max}){` |
49 |
| - } |
50 |
| - else if (screen.min != null) { |
51 |
| - strRet = `@media screen and (min-width: ${screen.min}){` |
52 |
| - } |
53 |
| - else if (screen.max != null) { |
54 |
| - strRet = `@media screen and (max-width: ${screen.max}){` |
55 |
| - } |
56 |
| - } |
57 |
| - }) |
58 |
| - return strRet |
59 |
| - }(classSubParams, screenBreakPointOpen, i, className); |
60 |
| - styleStr += await resp |
61 |
| - |
62 |
| - // selector async search (name + alias on async search) |
63 |
| - resp = async function(classSubParams, i) { |
64 |
| - let res = await fetch(document.querySelector('[lightwindsrc]').getAttribute('lightwindsrc')), strRet = '' |
65 |
| - res = await res.json() |
66 |
| - res.default.selectors[':'].forEach(async (sel) => { |
67 |
| - if (sel.name == classSubParams[i] || sel.alias.indexOf(classSubParams[i]) != -1) |
68 |
| - strRet = `.${className.replace(/\:/g, '\\:').replace(/\[/g, '\\[').replace(/\]/g, '\\]').replace(/\>/g, '\\>').replace(/\//g, '\\/').replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\%/g, '\\%').replace(/\-/g, '\\-').replace(/\+/g, '\\+').replace(/\*/g, '\\*').replace(/\,/g, '\\,').replace(/\|/g, '\\|')}:${sel.selector}` |
69 |
| - }) |
70 |
| - res.default.selectors['::'].forEach(async (sel) => { |
71 |
| - if (sel.name == classSubParams[i] || sel.alias.indexOf(classSubParams[i]) != -1) |
72 |
| - strRet = `.${className.replace(/\:/g, '\\:').replace(/\[/g, '\\[').replace(/\]/g, '\\]').replace(/\>/g, '\\>').replace(/\//g, '\\/').replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\%/g, '\\%').replace(/\-/g, '\\-').replace(/\+/g, '\\+').replace(/\*/g, '\\*').replace(/\,/g, '\\,').replace(/\|/g, '\\|')}::${sel.selector}` |
73 |
| - }) |
74 |
| - return strRet |
75 |
| - }(classSubParams, i); |
76 |
| - styleStr += await resp |
| 49 | + // screen + selector |
77 | 50 | }
|
78 | 51 | else {
|
79 |
| - // selector (name + alias on async search) |
80 |
| - let resp = async function(classSubParams, i) { |
81 |
| - let res = await fetch(document.querySelector('[lightwindsrc]').getAttribute('lightwindsrc')), strRet = '' |
82 |
| - res = await res.json() |
83 |
| - res.default.selectors[':'].forEach(async (sel) => { |
84 |
| - if (sel.name == classSubParams[i] || sel.alias.indexOf(classSubParams[i]) != -1) |
85 |
| - strRet = `:${sel.selector}` |
86 |
| - }) |
87 |
| - res.default.selectors['::'].forEach(async (sel) => { |
88 |
| - if (sel.name == classSubParams[i] || sel.alias.indexOf(classSubParams[i]) != -1) |
89 |
| - strRet = `::${sel.selector}` |
90 |
| - }) |
91 |
| - return strRet |
92 |
| - }(classSubParams, i); |
93 |
| - styleStr += await resp |
| 52 | + // selector |
94 | 53 | }
|
95 | 54 | }
|
96 |
| - styleLightWind.textContent += styleStr |
97 |
| - console.log(date.getTime() - lasttime) |
98 | 55 | }
|
99 | 56 |
|
100 | 57 | // mutations
|
|
106 | 63 | if (typeof(node.classList) != 'undefined') {
|
107 | 64 | node.classList.forEach(elClass => {
|
108 | 65 | if (allClasses.indexOf(elClass) == -1) {
|
109 |
| - resolveClass(elClass) |
| 66 | + resolveClass(elClass,res) |
110 | 67 | allClasses.push(elClass)
|
111 | 68 | }
|
112 | 69 | })
|
|
121 | 78 | mutations.forEach(mutation => {
|
122 | 79 | mutation.target.classList.forEach(elClass => {
|
123 | 80 | if (allClasses.indexOf(elClass) == -1) {
|
124 |
| - resolveClass(elClass) |
| 81 | + resolveClass(elClass,res) |
125 | 82 | allClasses.push(elClass)
|
126 | 83 | }
|
127 | 84 | })
|
|
133 | 90 | </script>
|
134 | 91 | </head>
|
135 | 92 | <body>
|
136 |
| - <div class="md:color>red top>100px position>absolute"> |
| 93 | + <div class="color>red"> |
137 | 94 | jj
|
138 | 95 | </div>
|
139 | 96 | </body>
|
|
0 commit comments