@pronto-tools-and-more/files 3.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ const webSocket = new WebSocket("ws://localhost:3000");
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 = (css) => {
55
+ removeError();
56
+ error = document.createElement("div");
57
+ error.className = "pronto-css-error";
58
+ error.style.position = "fixed";
59
+ error.style.top = "0";
60
+ error.style.right = "0";
61
+ error.style.width = "20px";
62
+ error.style.height = "20px";
63
+ error.style.background = "radial-gradient(red, transparent)";
64
+ error.style.zIndex = "11111111";
65
+ error.style.display = "flex";
66
+ document.body.append(error);
67
+ };
68
+
69
+ const commandMap = {
70
+ reload,
71
+ updateCss,
72
+ updateCssError,
73
+ };
74
+
75
+ const getCommand = (method) => {
76
+ return commandMap[method];
77
+ };
78
+
79
+ const executeCommand = (method, ...params) => {
80
+ const fn = getCommand(method);
81
+ if (!fn) {
82
+ throw new Error(`command not found ${method}`);
83
+ }
84
+ // @ts-ignore
85
+ return fn(...params);
86
+ };
87
+
88
+ const handleMessage = (event) => {
89
+ const { data } = event;
90
+ const parsed = JSON.parse(data);
91
+ const { method, params } = parsed;
92
+ executeCommand(method, ...params);
93
+ };
94
+
95
+ webSocket.onmessage = handleMessage;