-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
115 lines (109 loc) · 3.08 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Simple Whiteboard</title>
<link rel="stylesheet" href="./src/index.css" />
<script type="module" src="/src/simple-whiteboard.ts"></script>
<script type="module" src="/src/tools/defaults.ts"></script>
</head>
<body>
<main>
<simple-whiteboard id="app">
<simple-whiteboard--tool-defaults
slot="tools"
></simple-whiteboard--tool-defaults>
</simple-whiteboard>
</main>
<footer>
<a href="https://github.com/ludovicm67/simple-whiteboard" target="_blank"
>GitHub</a
>
-
<a href="/" target="_blank">New Tab</a>
-
<a href="https://ludovic-muller.fr/" target="_blank"
>Created by Ludovic Muller</a
>
</footer>
<script>
const id = Math.random().toString(36).substr(2, 9);
const app = document.getElementById("app");
(async () => {
await new Promise((resolve) => {
window.customElements.whenDefined("simple-whiteboard").then(() => {
resolve();
});
});
// Listen for items-updated event
app.addEventListener("items-updated", (e) => {
if (!e.detail.type) {
return;
}
console.log("Items updated event:", e.detail);
switch (e.detail.type) {
case "add":
bc.postMessage({
type: "item-add",
item: e.detail.item,
});
break;
case "update":
bc.postMessage({
type: "item-update",
itemId: e.detail.itemId,
item: e.detail.item,
});
break;
case "clear":
bc.postMessage({
type: "clear",
});
break;
case "remove":
bc.postMessage({
type: "item-remove",
itemId: e.detail.itemId,
});
break;
}
});
const bc = new BroadcastChannel("whiteboard-channel");
bc.postMessage({
type: "join",
id,
});
console.log("Client ID:", id);
bc.onmessage = (e) => {
if (!e.data.type) {
return;
}
switch (e.data.type) {
case "item-add":
app.addItem(e.data.item);
break;
case "item-update":
app.updateItem(e.data.itemId, e.data.item);
break;
case "item-remove":
app.removeItemById(e.data.itemId);
break;
case "join":
bc.postMessage({
type: "sync",
items: app.getItems(),
});
break;
case "sync":
app.setItems(e.data.items);
break;
case "clear":
app.clear();
break;
}
};
})();
</script>
</body>
</html>