-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
576 lines (490 loc) · 17.7 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini Chat</title>
<style>
/* Simulating terminal style */
body {
background-color: black;
color: white;
font-family: monospace;
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
padding-left: 20px; /* Increase left margin */
}
/* Chat container styles */
#chat-container {
display: flex;
flex: 1;
overflow: hidden;
}
/* User list styles */
#user-list {
flex: 0 0 auto;
width: 20%;
min-width: 100px;
max-width: 50%;
padding: 10px;
box-sizing: border-box;
overflow-y: auto;
}
@media screen and (max-width: 768px) {
#user-list {
width: 35%;
}
}
/* Chat content container styles */
#chat-content-container {
flex: 1;
min-width: 200px;
display: flex;
flex-direction: column;
}
/* Chat area styles */
#chat {
flex: 1;
padding: 10px;
overflow-y: auto;
box-sizing: border-box;
min-height: 100px; /* Set minimum height */
overflow-y: auto;
word-wrap: break-word;
}
/* Horizontal resizer styles */
#resizer {
height: 5px;
background-color: #555;
cursor: ns-resize;
}
/* Input container styles */
#input-container {
padding: 10px;
box-sizing: border-box;
border-top: 1px solid gray;
height: 30%;
min-height: 100px; /* Increase minimum height */
position: relative;
}
/* Textarea styles */
textarea {
width: 100%;
height: calc(100% - 20px); /* Subtract padding */
background-color: black;
color: white;
border: none; /* Remove border */
font-family: monospace;
resize: none;
outline: none; /* Remove outline on focus */
line-height: 1.5; /* Increase line height */
padding: 10px; /* Increase padding */
padding-right: 80px; /* Make room for the send button */
}
/* Message container styles */
.message-container {
margin-bottom: 30px; /* Increased from 20px to 30px */
}
/* Message styles */
.message {
border: 1px solid gray;
background-color: #333;
padding: 10px;
border-radius: 5px;
position: relative;
max-width: 100%;
overflow-wrap: break-word;
word-break: break-word;
margin-bottom: 10px;
}
/* Username styles */
.username {
display: flex;
align-items: center;
margin-bottom: 8px; /* Increased from 5px to 8px */
}
/* Copy button styles */
.copy-btn {
position: absolute;
right: 7px;
top: 7px;
background-color: #555;
color: white;
border: none;
padding: 5px;
cursor: pointer;
border-radius: 3px;
font-size: 12px;
}
.copy-btn:hover {
background-color: #777;
}
/* Preserve line breaks */
.message-content {
white-space: pre-wrap;
line-height: 1.5; /* Increase line height */
}
/* Toast styles */
#toast {
visibility: hidden;
min-width: 200px;
background-color: #333;
color: white;
text-align: center;
border-radius: 5px;
padding: 10px;
position: fixed;
z-index: 1;
left: 50%;
bottom: 30px;
transform: translateX(-50%);
}
#toast.show {
visibility: visible;
animation: fadein 0.5s, fadeout 0.5s 1s;
}
@keyframes fadein {
from { bottom: 0; opacity: 0; }
to { bottom: 30px; opacity: 1; }
}
@keyframes fadeout {
from { bottom: 30px; opacity: 1; }
to { bottom: 0; opacity: 0; }
}
/* User list item styles */
#users li {
display: flex;
align-items: center;
margin-bottom: 15px; /* Increased from 10px to 15px */
}
@media screen and (max-width: 768px) {
#users li {
margin-left: -50px;
}
}
@media screen and (min-width: 769px) {
#users li {
margin-left: 0;
}
}
#users li.me {
color: lightgreen;
}
#users li.me::after {
content: ' [me]';
color: #888;
}
/* Current user's username styles in chat */
.username.me::after {
content: ' [me]';
color: #888;
}
/* Avatar styles */
.avatar {
width: 32px;
height: 32px;
border-radius: 6px; /* Rounded square */
margin-right: 8px;
vertical-align: middle;
}
/* Vertical resizer styles */
#vertical-resizer {
width: 5px;
background-color: #555;
cursor: ew-resize;
flex: 0 0 5px;
}
/* Current user's username color */
.username.me {
color: lightgreen;
}
/* Added styles for GitHub link */
.header-container {
display: flex;
align-items: flex-end; /* Align to the bottom */
margin-bottom: 20px;
}
h1 {
margin-right: 15px;
margin-bottom: 0;
}
.github-link {
color: #fff;
text-decoration: none;
font-size: 14px;
display: flex;
align-items: center;
margin-bottom: 5px; /* Add bottom margin for fine-tuning alignment */
}
.github-link img {
width: 20px;
height: 20px;
margin-right: 5px;
}
#send-button {
position: absolute;
right: 10px;
bottom: 10px;
background-color: #555;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
border-radius: 3px;
}
#send-button:hover {
background-color: #777;
}
/* Add styles for code blocks within messages */
.message pre {
white-space: pre-wrap;
word-wrap: break-word;
overflow-x: auto;
background-color: #f8f8f8;
padding: 10px;
border-radius: 3px;
}
.message code {
font-family: monospace;
}
</style>
</head>
<body>
<div class="header-container">
<h1>Mini Chat</h1>
<a href="https://github.com/kugarocks/minichat" target="_blank" class="github-link">
<img src="img/GitHub-Mark.png" alt="GitHub">
GitHub
</a>
</div>
<div id="chat-container">
<div id="user-list">
<ul id="users"></ul>
</div>
<div id="vertical-resizer"></div>
<div id="chat-content-container">
<div id="chat"></div>
<div id="resizer"></div>
<div id="input-container">
<textarea id="message" placeholder="Press Enter to send"></textarea>
<button id="send-button">Send</button>
</div>
</div>
</div>
<script>
let ws;
let username = '';
let pingInterval;
let currentTheme = '';
function connectWebSocket() {
ws = new WebSocket("ws://localhost:8080/ws");
ws.onopen = function() {
console.log("WebSocket connection established");
startPing();
ws.send(JSON.stringify({ type: 'requestTheme' }));
};
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
if (data.type === 'theme') {
currentTheme = data.theme;
console.log("Theme set to:", currentTheme);
} else if (data.type === 'username') {
username = data.username;
console.log("Assigned username:", username);
ws.send(JSON.stringify({ type: 'requestUserList' }));
} else if (data.type === 'userList') {
updateUserList(data.users);
} else if (data.type === 'message') {
addMessageToChat(data.user, data.text);
} else if (data.type === 'pong') {
console.log("Received pong");
}
};
ws.onclose = function() {
console.log('Connection closed');
stopPing();
};
ws.onerror = function(error) {
console.log('WebSocket error:', error);
};
}
function startPing() {
pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: 'ping' }));
console.log("Sent ping");
}
}, 15000); // Send ping
}
function stopPing() {
clearInterval(pingInterval);
}
window.onload = function() {
connectWebSocket();
setTimeout(focusMessageInput, 100);
};
// Function to focus the message input
function focusMessageInput() {
const messageInput = document.getElementById('message');
if (messageInput) {
messageInput.focus();
// Move the cursor to the end of the text area
messageInput.setSelectionRange(messageInput.value.length, messageInput.value.length);
}
}
// Send message
function sendMessage() {
const input = document.getElementById('message');
const message = input.value.trim();
if (message !== '') {
ws.send(JSON.stringify({ type: 'message', text: message }));
input.value = '';
// Use setTimeout to ensure the input is focused after DOM updates
setTimeout(focusMessageInput, 0);
}
}
// Update user list
function updateUserList(onlineUsers) {
const userListElement = document.getElementById('users');
userListElement.innerHTML = '';
// Only proceed if username has been assigned
if (username) {
// Remove current user from the list and create a new sorted array
const otherUsers = onlineUsers.filter(user => user !== username);
const sortedUsers = [username, ...otherUsers];
sortedUsers.forEach(user => {
const li = document.createElement('li');
const avatar = document.createElement('img');
const baseUsername = user.split('-')[0].toLowerCase();
avatar.src = `img/${currentTheme}/${baseUsername}.jpg`;
avatar.alt = user;
avatar.className = 'avatar';
const span = document.createElement('span');
span.textContent = user;
li.appendChild(avatar);
li.appendChild(span);
if (user === username) {
li.className = 'me';
}
userListElement.appendChild(li);
});
}
}
// Add message to chat window
function addMessageToChat(user, messageText) {
const chat = document.getElementById('chat');
const messageContainer = document.createElement('div');
messageContainer.className = 'message-container';
const usernameDiv = document.createElement('div');
usernameDiv.className = 'username';
const avatar = document.createElement('img');
const baseUsername = user.split('-')[0].toLowerCase();
avatar.src = `img/${currentTheme}/${baseUsername}.jpg`;
avatar.alt = user;
avatar.className = 'avatar';
const usernameSpan = document.createElement('span');
usernameSpan.textContent = user;
usernameDiv.appendChild(avatar);
usernameDiv.appendChild(usernameSpan);
if (user === username) {
usernameDiv.classList.add('me');
}
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
const messageContent = document.createElement('div');
messageContent.className = 'message-content';
messageContent.textContent = messageText;
const copyButton = document.createElement('button');
copyButton.className = 'copy-btn';
copyButton.textContent = 'Copy';
copyButton.onclick = function() {
copyToClipboard(messageText, copyButton);
};
messageDiv.appendChild(messageContent);
messageDiv.appendChild(copyButton);
messageContainer.appendChild(usernameDiv);
messageContainer.appendChild(messageDiv);
chat.appendChild(messageContainer);
chat.scrollTop = chat.scrollHeight;
}
// Copy content to clipboard
function copyToClipboard(text, button) {
const tempTextArea = document.createElement('textarea');
tempTextArea.value = text;
document.body.appendChild(tempTextArea);
tempTextArea.select();
document.execCommand('copy');
document.body.removeChild(tempTextArea);
// Change button text and revert after 1 second
button.textContent = 'OK';
setTimeout(() => {
button.textContent = 'Copy';
}, 1000);
}
// Handle key input: Enter to send, Shift+Enter for new line
document.getElementById('message').addEventListener('keydown', function(event) {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
sendMessage();
}
});
// Handle horizontal resizing
const resizer = document.getElementById('resizer');
const chatContainer = document.getElementById('chat-container');
const chat = document.getElementById('chat');
const inputContainer = document.getElementById('input-container');
let isResizing = false;
resizer.addEventListener('mousedown', (e) => {
isResizing = true;
document.addEventListener('mousemove', resize);
document.addEventListener('mouseup', stopResize);
});
function resize(e) {
if (!isResizing) return;
const containerHeight = chatContainer.offsetHeight;
const newChatHeight = e.clientY - chatContainer.offsetTop;
const newInputHeight = containerHeight - newChatHeight - resizer.offsetHeight;
if (newChatHeight > 100 && newInputHeight > 70) {
chat.style.height = newChatHeight + 'px';
inputContainer.style.height = newInputHeight + 'px';
}
}
function stopResize() {
isResizing = false;
document.removeEventListener('mousemove', resize);
document.removeEventListener('mouseup', stopResize);
}
// Handle vertical resizing
const verticalResizer = document.getElementById('vertical-resizer');
const userList = document.getElementById('user-list');
const chatContentContainer = document.getElementById('chat-content-container');
let isVerticalResizing = false;
verticalResizer.addEventListener('mousedown', startVerticalResize);
function startVerticalResize(e) {
e.preventDefault();
isVerticalResizing = true;
document.addEventListener('mousemove', verticalResize);
document.addEventListener('mouseup', stopVerticalResize);
}
function verticalResize(e) {
if (!isVerticalResizing) return;
const containerRect = chatContainer.getBoundingClientRect();
const newUserListWidth = e.clientX - containerRect.left - 20;
const newChatContentWidth = containerRect.width - newUserListWidth - verticalResizer.offsetWidth;
if (newUserListWidth > 100 && newChatContentWidth > 200) {
userList.style.width = `${newUserListWidth}px`;
chatContentContainer.style.width = `${newChatContentWidth}px`;
}
}
function stopVerticalResize() {
isVerticalResizing = false;
document.removeEventListener('mousemove', verticalResize);
document.removeEventListener('mouseup', stopVerticalResize);
}
// Added event listener for send button
document.getElementById('send-button').addEventListener('click', sendMessage);
</script>
</body>
</html>