Skip to content

Commit 94c049a

Browse files
committed
feat: if cant find style sheet save is false
1 parent 6111e3d commit 94c049a

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

docs/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<!-- CoCreate CSS -->
1313
<link rel="stylesheet" href="https://server.cocreate.app/CoCreateJS/dist/CoCreate.css" type="text/css" />
14-
<link rel="stylesheet" href="https://ws.cocreate.app/docs/index.css" type="text/css" data-save="true" data-collection="files" data-document_id="60888216117c640e7596303f" name="src" />
14+
<!--<link rel="stylesheet" href="https://ws.cocreate.app/docs/index.css" type="text/css" data-save="true" data-collection="files" data-document_id="60888216117c640e7596303f" name="src" />-->
1515
<!--<link rel="stylesheet" href="../src/index.css" data-collection="files" data-document_id="60888216117c640e7596303f" name="src" type="text/css" data-save="true" />-->
1616
</head>
1717
<style type="text/css" media="screen">

src/index.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let parsedCSS = [];
3535
let tempStyleList = [];
3636
let concatCSS = [];
3737
let styleElSheet;
38-
let styleIndex = 0;
38+
3939
let newCSS = [];
4040
// event system
4141
let eventCallback = {};
@@ -48,15 +48,7 @@ function on(event, callback) {
4848
eventCallback[event] = callback
4949
}
5050

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 = () => {
6052
styleEl.setAttribute('component', 'CoCreateCss')
6153
document.head.appendChild(styleEl);
6254
styleElSheet = styleEl.sheet;
@@ -89,10 +81,9 @@ async function init() {
8981

9082
},
9183
});
84+
}
9285

93-
if (document.querySelector('link[parse="false"]') || document.querySelector('link[onload="false"]'))
94-
return;
95-
86+
const getParsedCss = () => {
9687
let hasChange = false;
9788
let elements = document.querySelectorAll("[class]");
9889

@@ -102,30 +93,52 @@ async function init() {
10293

10394
parsedCSS = tempStyleList;
10495
tempStyleList = [];
96+
return hasChange;
97+
}
10598

106-
// let isSuccess;
99+
const getWholeCss = () => {
107100
let stylesheetCSS = [];
101+
let hasChange = true;
102+
let styleIndex = 0;
103+
let getValidLinkTag = false;
104+
108105
try {
109106
let stylesheets = document.querySelectorAll("link[type='text/css']");
110107

111108
for (let stylesheet of stylesheets) {
112-
if (stylesheet.hasAttribute('data-save')) break;
109+
if (stylesheet.hasAttribute('data-save')) {
110+
getValidLinkTag = true;
111+
break;
112+
}
113113
styleIndex++;
114114
}
115-
let myRules = document.styleSheets[styleIndex].cssRules; // Returns a CSSRuleList
116115

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
120118

119+
for (let rule of myRules) {
120+
stylesheetCSS.push(rule.cssText);
121+
}
122+
}
123+
else
124+
hasChange = false;
121125
}
122126
catch (err) {
127+
hasChange = false;
123128
console.error(err)
124129
}
125130
finally {
126131
console.log('stylesheetCSS', stylesheetCSS);
127132
console.log('parsedCss', parsedCSS)
128133

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+
129142
concatCSS = parsedCSS.concat(stylesheetCSS).filter(onlyUnique);
130143
newCSS = [...diff(parsedCSS, stylesheetCSS), ...diff(stylesheetCSS, parsedCSS)];
131144

@@ -150,14 +163,12 @@ async function init() {
150163
}
151164
concatCSS.sort();
152165
}
166+
return hasChange;
167+
}
153168

154-
// if (!isSuccess)
155-
// concatCSS.forEach(l => styleElSheet.insertRule(l))
156-
169+
const saveCss = (hasChange) => {
157170
console.log("hasChange", hasChange)
158-
159171
if (hasChange) {
160-
// console.log('parsedCSS', concatCSS)
161172
console.log('cssString', concatCSS.join('\r\n'))
162173
window.dispatchEvent(new CustomEvent("newCoCreateCssStyles", {
163174
detail: {
@@ -169,6 +180,21 @@ async function init() {
169180
else {
170181
console.log('cssString after Concat', concatCSS.join('\r\n'))
171182
}
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);
172198

173199
// 3 lines events system
174200
if (eventCallback.parse)

0 commit comments

Comments
 (0)