Skip to content

Commit d6a8c17

Browse files
committed
feat: saves any changed css to object store for content dilevery and caching. data-save='true' to lazyload save component and to save any changes to the css
1 parent 6345709 commit d6a8c17

File tree

5 files changed

+115
-10
lines changed

5 files changed

+115
-10
lines changed

dist/CoCreateCSS.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
"webpack-log": "^3.0.1"
6565
},
6666
"dependencies": {
67-
"@cocreate/crdt": "^1.0.11",
6867
"@cocreate/docs": "^1.0.3",
68+
"@cocreate/hosting": "^1.0.0",
6969
"@cocreate/observer": "^1.0.1"
7070
}
7171
}

src/index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* Parsing CSS for Utility CSS*/
22
import observer from '@cocreate/observer'
3-
43
import './box-shadow.css'
54
import './CoCreate-avatar.css'
65
import './CoCreate-badge.css'
@@ -31,17 +30,27 @@ const rangesArray = Object.values(ranges);
3130

3231

3332
let styleEl = document.createElement("style");
34-
styleEl.setAttribute('component', 'CoCreateCss')
35-
document.head.appendChild(styleEl);
33+
3634
let selectorList = [];
3735
let styleList = [];
3836
let tempStyleList = [];
39-
let styleElSheet = styleEl.sheet;
37+
let styleElSheet;
4038

4139

4240

4341

4442
window.addEventListener("load", function() {
43+
44+
45+
// let link = document.querySelector('link[data-collection][data-document_id][name]');
46+
// if (link)
47+
// return;
48+
49+
styleEl.setAttribute('component', 'CoCreateCss')
50+
document.head.appendChild(styleEl);
51+
styleElSheet = styleEl.sheet;
52+
53+
4554
let hasChange = false;
4655

4756
observer.init({
@@ -52,8 +61,8 @@ window.addEventListener("load", function() {
5261

5362

5463
// // console.log('ccCSS observer start', performance.now())
55-
56-
hasChange = addParsingClassList(mutation.target.classList);
64+
65+
hasChange = addParsingClassList(mutation.target.classList);
5766
if (mutation.type == "childList")
5867
mutation.target.querySelectorAll("*").forEach((el) => {
5968
hasChange = addParsingClassList(el.classList) || hasChange;
@@ -179,13 +188,16 @@ function parseClass(classname) {
179188
let rule = "";
180189
let suffix = res[1]
181190
.replace(/\./g, "\\.")
182-
.replace(/%/, "\\%")
191+
.replace(/%/g, "\\%")
183192
.replace(/@/g, "\\@")
184193
.replace(/\(/g, "\\(")
185194
.replace(/\)/g, "\\)")
186195
.replace(/#/g, "\\#")
187196
.replace(/,/g, "\\,")
188-
.replace(/!/, "\\!");
197+
.replace(/!/g, "\\!")
198+
.replace(/\//g, "\\/")
199+
.replace(/\"/g, "\\\"")
200+
.replace(/\'/g, "\\'");
189201
res[1] = res[1].split("@")[0];
190202
res[1] = res[1].replace(/_/g, " ");
191203
if (res.length > 2) {

src/save.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// import crud from '@cocreate/crud-client'
2+
// import crdt from '@cocreate/crdt'
3+
4+
5+
// window.addEventListener('load', () => {
6+
// let links = document.querySelectorAll('link[data-save=true]');
7+
// for (let link of links) {
8+
// let collection = link.getAttribute('data-collection');
9+
// let name = link.getAttribute('name');
10+
// let document_id = link.getAttribute('data-document_id');
11+
// if (collection && name && document_id)
12+
// window.addEventListener('newCoCreateCssStyles', function(isFirst, styleList) {
13+
// crdt.replaceText({ collection, name, document_id, name: 'css', value: styleList.join('\r\n') });
14+
// })
15+
16+
// }
17+
18+
// })
19+
20+
21+
22+
23+
if (window.CoCreate.crdt) {
24+
window.addEventListener('load', () => {
25+
let links = document.querySelectorAll('link[data-save=true]');
26+
for (let link of links) {
27+
let collection = link.getAttribute('data-collection');
28+
let name = link.getAttribute('name');
29+
let document_id = link.getAttribute('data-document_id');
30+
if (collection && name && document_id)
31+
window.addEventListener('newCoCreateCssStyles', function(isFirst, styleList) {
32+
33+
window.CoCreate.crdt.init({
34+
collection,
35+
document_id,
36+
name,
37+
})
38+
window.CoCreate.crdt.replaceText({ collection, name, document_id, name: 'css', value: styleList.join('\r\n') });
39+
})
40+
}
41+
})
42+
}
43+
else {
44+
45+
window.addEventListener('load', () => {
46+
let links = document.querySelectorAll('link[data-save=true]');
47+
for (let link of links) {
48+
let collection = link.getAttribute('data-collection');
49+
let name = link.getAttribute('name');
50+
let document_id = link.getAttribute('data-document_id');
51+
if (collection && name && document_id)
52+
window.addEventListener('newCoCreateCssStyles', function(isFirst, styleList) {
53+
window.CoCreate.crud.updateDocument({
54+
collection,
55+
document_id,
56+
upsert: true,
57+
// broadcast_sender,
58+
data: {
59+
[name]: styleList.join('\r\n')
60+
},
61+
});
62+
})
63+
}
64+
})
65+
66+
}
67+

yarn.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,15 @@
859859
"@cocreate/docs" "^1.0.3"
860860
"@cocreate/socket-client" "^1.0.2"
861861

862+
"@cocreate/crud-client@^1.0.7":
863+
version "1.0.7"
864+
resolved "https://registry.yarnpkg.com/@cocreate/crud-client/-/crud-client-1.0.7.tgz#006c803346f750d25d44615dd901d4a87f308516"
865+
integrity sha512-Eq5sW08EIB+HG9MWNm65p+1Uw3GxnmzhfARwzSSdeUljlFxq30w7SyIsH9Cb3m5nKE3hzeN1JHVrw9HvkClXoQ==
866+
dependencies:
867+
"@cocreate/docs" "^1.0.3"
868+
"@cocreate/socket-client" "^1.0.2"
869+
mini-css-extract-plugin "^1.5.0"
870+
862871
"@cocreate/cursors@^1.1.5":
863872
version "1.1.6"
864873
resolved "https://registry.yarnpkg.com/@cocreate/cursors/-/cursors-1.1.6.tgz#0f6a327b91b502e5c15cff972167830b1e5e0d00"
@@ -892,6 +901,15 @@
892901
"@cocreate/observer" "^1.0.0"
893902
"@cocreate/utils" "^1.0.0"
894903

904+
"@cocreate/hosting@^1.0.0":
905+
version "1.0.0"
906+
resolved "https://registry.yarnpkg.com/@cocreate/hosting/-/hosting-1.0.0.tgz#33877f617f5d6a61a809266d7b629988ee592bb4"
907+
integrity sha512-VuH0CsA+qs3AHhrjEHskZlkooHHD/+Hp/gUM3vu7caFETBbsdrcolrx0w9U+9whaoY48QcROgH5Dpp/gUSw6ag==
908+
dependencies:
909+
"@cocreate/crud-client" "^1.0.7"
910+
"@cocreate/docs" "^1.0.3"
911+
"@cocreate/socket-client" "^1.0.9"
912+
895913
"@cocreate/input@^1.0.0":
896914
version "1.0.5"
897915
resolved "https://registry.yarnpkg.com/@cocreate/input/-/input-1.0.5.tgz#b99960ba206aae1b4a2f8b8c56bd38ff6d867630"
@@ -915,6 +933,15 @@
915933
"@cocreate/docs" "^1.0.3"
916934
ws "^7.4.4"
917935

936+
"@cocreate/socket-client@^1.0.9":
937+
version "1.0.9"
938+
resolved "https://registry.yarnpkg.com/@cocreate/socket-client/-/socket-client-1.0.9.tgz#e9604f906ef50bc6563832c61457d9b136591733"
939+
integrity sha512-RWNcqJWkS+Y0nfJvA3dbEy4nV48vX9XGLtzjfXqYwvg9NfnZ+Nfis/+BnV1AK5aU6QC/XrxrSFcuEfrp+lc/mg==
940+
dependencies:
941+
"@cocreate/docs" "^1.0.3"
942+
mini-css-extract-plugin "^1.5.0"
943+
ws "^7.4.4"
944+
918945
"@cocreate/utils@^1.0.0":
919946
version "1.0.2"
920947
resolved "https://registry.yarnpkg.com/@cocreate/utils/-/utils-1.0.2.tgz#91062620ec481ed25d19973d1f9b5dee7e945012"

0 commit comments

Comments
 (0)