@@ -35,7 +35,7 @@ let parsedCSS = [];
35
35
let tempStyleList = [ ] ;
36
36
let concatCSS = [ ] ;
37
37
let styleElSheet ;
38
- let styleIndex = 0 ;
38
+
39
39
let newCSS = [ ] ;
40
40
// event system
41
41
let eventCallback = { } ;
@@ -48,15 +48,7 @@ function on(event, callback) {
48
48
eventCallback [ event ] = callback
49
49
}
50
50
51
- const onlyUnique = ( value , index , self ) => {
52
- return self . indexOf ( value ) === index ;
53
- }
54
-
55
- const diff = ( a , b ) => {
56
- return a . filter ( item => b . indexOf ( item ) === - 1 ) ;
57
- }
58
-
59
- async function init ( ) {
51
+ const observerInit = ( ) => {
60
52
styleEl . setAttribute ( 'component' , 'CoCreateCss' )
61
53
document . head . appendChild ( styleEl ) ;
62
54
styleElSheet = styleEl . sheet ;
@@ -89,10 +81,9 @@ async function init() {
89
81
90
82
} ,
91
83
} ) ;
84
+ }
92
85
93
- if ( document . querySelector ( 'link[parse="false"]' ) || document . querySelector ( 'link[onload="false"]' ) )
94
- return ;
95
-
86
+ const getParsedCss = ( ) => {
96
87
let hasChange = false ;
97
88
let elements = document . querySelectorAll ( "[class]" ) ;
98
89
@@ -102,30 +93,52 @@ async function init() {
102
93
103
94
parsedCSS = tempStyleList ;
104
95
tempStyleList = [ ] ;
96
+ return hasChange ;
97
+ }
105
98
106
- // let isSuccess;
99
+ const getWholeCss = ( ) => {
107
100
let stylesheetCSS = [ ] ;
101
+ let hasChange = true ;
102
+ let styleIndex = 0 ;
103
+ let getValidLinkTag = false ;
104
+
108
105
try {
109
106
let stylesheets = document . querySelectorAll ( "link[type='text/css']" ) ;
110
107
111
108
for ( let stylesheet of stylesheets ) {
112
- if ( stylesheet . hasAttribute ( 'data-save' ) ) break ;
109
+ if ( stylesheet . hasAttribute ( 'data-save' ) ) {
110
+ getValidLinkTag = true ;
111
+ break ;
112
+ }
113
113
styleIndex ++ ;
114
114
}
115
- let myRules = document . styleSheets [ styleIndex ] . cssRules ; // Returns a CSSRuleList
116
115
117
- for ( let rule of myRules ) {
118
- stylesheetCSS . push ( rule . cssText ) ;
119
- }
116
+ if ( getValidLinkTag ) {
117
+ let myRules = document . styleSheets [ styleIndex ] . cssRules ; // Returns a CSSRuleList
120
118
119
+ for ( let rule of myRules ) {
120
+ stylesheetCSS . push ( rule . cssText ) ;
121
+ }
122
+ }
123
+ else
124
+ hasChange = false ;
121
125
}
122
126
catch ( err ) {
127
+ hasChange = false ;
123
128
console . error ( err )
124
129
}
125
130
finally {
126
131
console . log ( 'stylesheetCSS' , stylesheetCSS ) ;
127
132
console . log ( 'parsedCss' , parsedCSS )
128
133
134
+ const onlyUnique = ( value , index , self ) => {
135
+ return self . indexOf ( value ) === index ;
136
+ }
137
+
138
+ const diff = ( a , b ) => {
139
+ return a . filter ( item => b . indexOf ( item ) === - 1 ) ;
140
+ }
141
+
129
142
concatCSS = parsedCSS . concat ( stylesheetCSS ) . filter ( onlyUnique ) ;
130
143
newCSS = [ ...diff ( parsedCSS , stylesheetCSS ) , ...diff ( stylesheetCSS , parsedCSS ) ] ;
131
144
@@ -150,14 +163,12 @@ async function init() {
150
163
}
151
164
concatCSS . sort ( ) ;
152
165
}
166
+ return hasChange ;
167
+ }
153
168
154
- // if (!isSuccess)
155
- // concatCSS.forEach(l => styleElSheet.insertRule(l))
156
-
169
+ const saveCss = ( hasChange ) => {
157
170
console . log ( "hasChange" , hasChange )
158
-
159
171
if ( hasChange ) {
160
- // console.log('parsedCSS', concatCSS)
161
172
console . log ( 'cssString' , concatCSS . join ( '\r\n' ) )
162
173
window . dispatchEvent ( new CustomEvent ( "newCoCreateCssStyles" , {
163
174
detail : {
@@ -169,6 +180,21 @@ async function init() {
169
180
else {
170
181
console . log ( 'cssString after Concat' , concatCSS . join ( '\r\n' ) )
171
182
}
183
+ }
184
+
185
+ async function init ( ) {
186
+
187
+ observerInit ( ) ;
188
+
189
+ if ( document . querySelector ( 'link[parse="false"]' ) || document . querySelector ( 'link[onload="false"]' ) )
190
+ return ;
191
+
192
+ let hasChange = false ;
193
+
194
+ hasChange = getParsedCss ( ) ;
195
+ hasChange = getWholeCss ( ) ;
196
+
197
+ saveCss ( hasChange ) ;
172
198
173
199
// 3 lines events system
174
200
if ( eventCallback . parse )
0 commit comments