diff --git a/server/static/ui_chat.js b/server/static/ui_chat.js new file mode 100644 index 0000000..aa2d9c4 --- /dev/null +++ b/server/static/ui_chat.js @@ -0,0 +1,36 @@ +export function setupChat(root, socket) { + root.innerHTML = ` +
+ `; + const panel = root.firstElementChild; + const log = panel.querySelector('#chat-log'); + const input = panel.querySelector('#chat-input'); + + function addLine(t) { + const div = document.createElement('div'); + div.textContent = t; + log.appendChild(div); + log.scrollTop = log.scrollHeight; + } + + panel.querySelector('#chat-send').onclick = () => { + const t = input.value.trim(); + if (!t) return; + socket.emit('chat:send', { t }); + input.value = ''; + }; + + input.addEventListener('keydown', e => { + if (e.key === 'Enter') panel.querySelector('#chat-send').click(); + }); + + socket.on('chat:push', m => addLine(m)); + + window.addChatLine = addLine; +}