@pronto-tools-and-more/files 4.2.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
|
|
1
|
+
const webSocket = new WebSocket("ws://localhost:3000/ws/readmode");
|
2
|
+
|
3
|
+
const reload = () => {
|
4
|
+
window.location.reload();
|
5
|
+
};
|
6
|
+
|
7
|
+
let sheet;
|
8
|
+
|
9
|
+
let error;
|
10
|
+
|
11
|
+
const sheetMap = Object.create(null);
|
12
|
+
|
13
|
+
const getOrCreateSheet = (fileName) => {
|
14
|
+
if (!sheetMap[fileName]) {
|
15
|
+
sheetMap[fileName] = new CSSStyleSheet();
|
16
|
+
document.adoptedStyleSheets.push(sheetMap[fileName]);
|
17
|
+
}
|
18
|
+
const existing = sheetMap[fileName];
|
19
|
+
return existing;
|
20
|
+
};
|
21
|
+
|
22
|
+
let customCssRemoved = false;
|
23
|
+
|
24
|
+
const removeCustomCss = () => {
|
25
|
+
if (customCssRemoved) {
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
const customCss = document.querySelector('[href$="custom.css"]');
|
29
|
+
if (!customCss) {
|
30
|
+
return;
|
31
|
+
}
|
32
|
+
customCss.remove();
|
33
|
+
customCssRemoved = true;
|
34
|
+
};
|
35
|
+
|
36
|
+
const removeError = () => {
|
37
|
+
if (!error) {
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
error.remove();
|
41
|
+
error = undefined;
|
42
|
+
};
|
43
|
+
|
44
|
+
const updateCss = (changes) => {
|
45
|
+
for (const change of changes) {
|
46
|
+
const sheet = getOrCreateSheet(change.fileName);
|
47
|
+
sheet.replaceSync(change.css);
|
48
|
+
}
|
49
|
+
|
50
|
+
removeError();
|
51
|
+
removeCustomCss();
|
52
|
+
};
|
53
|
+
|
54
|
+
const updateCssError = (errorColor, css) => {
|
55
|
+
console.log({ errorColor });
|
56
|
+
removeError();
|
57
|
+
error = document.createElement("div");
|
58
|
+
error.className = "pronto-css-error";
|
59
|
+
error.style.position = "fixed";
|
60
|
+
error.style.top = "0";
|
61
|
+
error.style.right = "0";
|
62
|
+
error.style.width = "20px";
|
63
|
+
error.style.height = "20px";
|
64
|
+
error.style.background = `radial-gradient(${errorColor}, transparent)`;
|
65
|
+
error.style.zIndex = "11111111";
|
66
|
+
error.style.display = "flex";
|
67
|
+
document.body.append(error);
|
68
|
+
};
|
69
|
+
|
70
|
+
const commandMap = {
|
71
|
+
reload,
|
72
|
+
updateCss,
|
73
|
+
updateCssError,
|
74
|
+
};
|
75
|
+
|
76
|
+
const getCommand = (method) => {
|
77
|
+
return commandMap[method];
|
78
|
+
};
|
79
|
+
|
80
|
+
const executeCommand = (method, ...params) => {
|
81
|
+
const fn = getCommand(method);
|
82
|
+
if (!fn) {
|
83
|
+
throw new Error(`command not found ${method}`);
|
84
|
+
}
|
85
|
+
// @ts-ignore
|
86
|
+
return fn(...params);
|
87
|
+
};
|
88
|
+
|
89
|
+
const handleMessage = (event) => {
|
90
|
+
const { data } = event;
|
91
|
+
const parsed = JSON.parse(data);
|
92
|
+
const { method, params } = parsed;
|
93
|
+
executeCommand(method, ...params);
|
94
|
+
};
|
95
|
+
|
96
|
+
webSocket.onmessage = handleMessage;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
const webSocket = new WebSocket("ws://localhost:3000");
|
1
|
+
const webSocket = new WebSocket("ws://localhost:3000/ws/page");
|
2
2
|
|
3
3
|
const reload = () => {
|
4
4
|
window.location.reload();
|
@@ -41,12 +41,21 @@ const removeError = () => {
|
|
41
41
|
error = undefined;
|
42
42
|
};
|
43
43
|
|
44
|
+
const isReadModeUpdate = (changes) => {
|
45
|
+
return (
|
46
|
+
changes.length === 1 &&
|
47
|
+
changes[0].fileName === "default/readmode/custom.css"
|
48
|
+
);
|
49
|
+
};
|
50
|
+
|
44
51
|
const updateCss = (changes) => {
|
52
|
+
if (isReadModeUpdate(changes)) {
|
53
|
+
return;
|
54
|
+
}
|
45
55
|
for (const change of changes) {
|
46
56
|
const sheet = getOrCreateSheet(change.fileName);
|
47
57
|
sheet.replaceSync(change.css);
|
48
58
|
}
|
49
|
-
|
50
59
|
removeError();
|
51
60
|
removeCustomCss();
|
52
61
|
};
|