Newer
Older
const Y = require("yjs")
require("y-memory")(Y)
Moritz Langenstein
committed
require("y-map")(Y)
require("y-array")(Y)
require("./y-webrtc")(Y)
Moritz Langenstein
committed
const uuidv4 = require("uuid/v4")
Moritz Langenstein
committed
name: "memory",
},
connector: {
name: "webrtc",
host: "localhost",
port: 3000,
Moritz Langenstein
committed
path: "/api",
Moritz Langenstein
committed
drawing: "Map",
},
}).then((y) => {
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
const userIDElem = document.getElementById("user-id")
const peerIDElem = document.getElementById("peer-id")
const peerButton = document.getElementById("peer-connect")
const connectedP = document.getElementById("connected-peers")
userIDElem.value = y.db.userId
y.connector.onUserEvent(function(event) {
switch (event.action) {
case "userID":
userIDElem.value = event.id
break
case "userJoined":
var peerElem = document.createElement("li")
peerElem.innerHTML = event.user
connectedP.appendChild(peerElem)
break
case "userLeft":
for (var peer of connectedP.children) {
if (peer.innerHTML == event.user) {
connectedP.removeChild(peer)
}
}
break
}
})
peerButton.onclick = function() {
const peerID = peerIDElem.value
if (peerID == "") {
return
}
y.connector.connectToPeer(peerID)
peerIDElem.value = ""
Moritz Langenstein
committed
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const whiteboard = document.getElementById("whiteboard")
var painting = false
var paths = new Map()
var pathID
function createOrUpdatePath(uid, points) {
var path = paths.get(uid)
if (path === undefined) {
path = document.createElementNS("http://www.w3.org/2000/svg", "path")
path.setAttribute("stroke", "blue")
path.setAttribute("stroke-width", 3)
path.setAttribute("fill", "none")
path.setAttribute("pointer-events", "none")
whiteboard.appendChild(path)
paths.set(uid, path)
}
points = points.toArray().filter((point) => point !== undefined)
if (points.length <= 0) {
path.removeAttribute("d")
return path
}
var pathString = "M" + points[0][0] + " " + points[0][1]
for (var i = 1; i < points.length; i++) {
pathString += " L" + points[i][0] + " " + points[i][1]
}
path.setAttribute("d", pathString)
return path
}
whiteboard.onmousedown = function(e) {
painting = true
const mouse = {
x: e.offsetX,
y: e.offsetY,
}
pathID = uuidv4()
const sharedPath = y.share.drawing.set(pathID, Y.Array)
sharedPath.push([[mouse.x, mouse.y]])
}
whiteboard.onmouseup = function() {
painting = false
}
whiteboard.onmousemove = function(e) {
const mouse = {
x: e.offsetX,
y: e.offsetY,
}
if (painting) {
const sharedPath = y.share.drawing.get(pathID)
sharedPath.push([[mouse.x, mouse.y]])
}
}
y.share.drawing.observe(function(lineEvent) {
const lineID = lineEvent.name
switch (lineEvent.type) {
case "add":
createOrUpdatePath(lineID, lineEvent.value)
lineEvent.value.observe(function(pointEvent) {
switch (pointEvent.type) {
case "insert":
console.log(pointEvent)
createOrUpdatePath(lineID, pointEvent.object)
break
}
})
break
}
})