-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (129 loc) · 4.16 KB
/
server.js
File metadata and controls
147 lines (129 loc) · 4.16 KB
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
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
// server.js
import express from 'express';
import { createServer } from 'http';
import { Server } from 'socket.io';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "http://localhost:5173",
methods: ["GET", "POST"]
}
});
const PORT = process.env.PORT || 3000;
// Serve static files from the 'dist' directory
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
import { Diagram, Shape } from '@mindfusion/diagramming';
import { Rect } from '@mindfusion/drawing';
// Server-side diagram instance
const serverDiagram = new Diagram();
// shape created when users draw directly on the canvas (instead of drag and drop from palette)
serverDiagram.defaultShape = Shape.fromId("Ellipse");
// Create a default diagram
const node1 = serverDiagram.factory.createShapeNode(10, 10, 30, 30);
node1.text = "Hello";
node1.id = "node1";
const node2 = serverDiagram.factory.createShapeNode(60, 25, 30, 30);
node2.text = "World";
node2.id = "node2";
const link = serverDiagram.factory.createDiagramLink(node1, node2);
link.id = "link1";
// Helper functions
function findNode(id) {
return serverDiagram.nodes.find(n => n.id === id);
}
function findLink(id) {
return serverDiagram.links.find(l => l.id === id);
}
io.on('connection', (socket) => {
console.log('A user connected:', socket.id);
// Send the current diagram to the new client
socket.emit('load', serverDiagram.toJson());
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
});
socket.on('nodeCreated', (data) => {
const node = serverDiagram.factory.createShapeNode(
data.x, data.y, data.width, data.height);
node.id = data.id;
node.text = data.text;
node.shape = Shape.fromId(data.shape);
socket.broadcast.emit('nodeCreated', data);
});
socket.on('nodeModified', (data) => {
const node = findNode(data.id);
if (node) {
var newBounds = new Rect(data.x, data.y, data.width, data.height);
node.setBounds(newBounds, true); // the true argument also updates link end points
}
socket.broadcast.emit('nodeModified', data);
});
socket.on('nodeTextEdited', (data) => {
const node = findNode(data.id);
if (node) {
node.text = data.text;
}
socket.broadcast.emit('nodeTextEdited', data);
});
socket.on('nodeDeleted', (data) => {
const node = findNode(data.id);
if (node) {
serverDiagram.removeItem(node);
}
socket.broadcast.emit('nodeDeleted', data);
});
socket.on('linkCreated', (data) => {
const origin = findNode(data.originId);
const destination = findNode(data.destinationId);
if (origin && destination) {
const link = serverDiagram.factory.createDiagramLink(origin, destination);
link.id = data.id;
link.text = data.text;
}
socket.broadcast.emit('linkCreated', data);
});
socket.on('linkModified', (data) => {
const link = findLink(data.id);
if (link) {
const origin = findNode(data.originId);
const destination = findNode(data.destinationId);
if (origin && destination) {
link.origin = origin;
link.destination = destination;
}
}
socket.broadcast.emit('linkModified', data);
});
socket.on('linkTextEdited', (data) => {
const link = findLink(data.id);
if (link) {
link.text = data.text;
}
socket.broadcast.emit('linkTextEdited', data);
});
socket.on('linkDeleted', (data) => {
const link = findLink(data.id);
if (link) {
serverDiagram.removeItem(link);
}
socket.broadcast.emit('linkDeleted', data);
});
socket.on('clear', () => {
serverDiagram.clearAll();
socket.broadcast.emit('clear');
});
socket.on('load', (data) => {
serverDiagram.fromJson(data);
socket.broadcast.emit('load', data);
});
});
httpServer.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});