-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex-code-share.html
127 lines (117 loc) · 4.14 KB
/
index-code-share.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
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UT-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Share Clone</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body {
display: flex;
flex-direction: column;
height: 100vh;
}
#header {
background-color: #343a40;
color: white;
padding: 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
#editor {
flex: 1;
height: calc(100vh - 70px);
padding: 10px;
border: 1px solid #ced4da;
border-radius: 5px;
margin: 10px;
font-family: monospace;
font-size: 16px;
overflow-y: auto;
}
#share-url {
margin-left: 10px;
color: #007bff;
}
</style>
</head>
<body>
<div id="header" class="container-fluid">
<h1 class="h4">Code Share (Semicolon Projects)</h1>
<div>
<button id="share-button" class="btn btn-primary">Share</button>
<span id="share-url"></span>
</div>
</div>
<div class="container-fluid">
<textarea id="editor" class="form-control" placeholder="Write your code here..."></textarea>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
let userId = generateUniqueId();
let roomId = new URLSearchParams(window.location.search).get('room_id') || generateUniqueId();
const socket = new WebSocket(`ws://localhost:4000/send?user_id=${userId}&room_id=${roomId}`);
// Assign new user_id and room_id if not already present
window.addEventListener('load', () => {
console.log(`User ID: ${userId}, Room ID: ${roomId}`);
if (roomId) {
fetchHistory(roomId);
}
});
// Fetch history from the server and set it in the editor
async function fetchHistory(roomId) {
try {
let response = await fetch(`http://localhost:4000/history/${roomId}`);
if (response.ok) {
let data = await response.text();
if (data !== `No clients found for room ${roomId}`) {
document.getElementById('editor').value = data;
}
}
} catch (error) {
console.error('Error fetching history:', error);
}
}
// Share button click event
document.getElementById('share-button').addEventListener('click', () => {
const shareUrl = `http://localhost:3000?room_id=${roomId}`;
copyToClipboard(shareUrl).then(_);
});
// Function to generate a unique ID
function generateUniqueId() {
return Math.random().toString(36).substring(2, 15);
}
async function copyToClipboard(text) {
const type = "text/plain";
const blob = new Blob([text], { type });
const data = [new ClipboardItem({ [type]: blob })];
await navigator.clipboard.write(data);
alert('Share URL copied to clipboard!');
}
// WebSocket to send and receive code updates
socket.onopen = () => {
console.log("WebSocket connection opened.");
document.getElementById('editor').addEventListener('input', () => {
const code = document.getElementById('editor').value;
const message = { code: code, user_id: userId };
socket.send(JSON.stringify(message));
});
};
socket.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.user_id !== userId) {
document.getElementById('editor').value = message.code;
}
};
socket.onerror = (error) => {
console.error('WebSocket Error:', error);
};
socket.onclose = (event) => {
console.log('WebSocket connection closed:', event);
};
</script>
</body>
</html>