From 2a3e225e1dbb40374e841af8977ff19cd4711f0c Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 27 Jan 2025 02:58:28 +0100 Subject: [PATCH] Update. --- src/snek/static/chat-input.js | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/snek/static/chat-input.js diff --git a/src/snek/static/chat-input.js b/src/snek/static/chat-input.js new file mode 100644 index 0000000..fcf925b --- /dev/null +++ b/src/snek/static/chat-input.js @@ -0,0 +1,40 @@ + + +class ChatInputElement extends HTMLElement { + constructor() { + super(); + this.attachShadow({ mode: 'open' }); + this.component = document.createElement('div'); + this.shadowRoot.appendChild(this.component); + } + connectedCallback() { + const link = document.createElement("link") + link.rel = 'stylesheet' + link.href = '/base.css' + this.component.appendChild(link) + this.container = document.createElement('div') + this.container.classList.add("chat-input") + this.container.innerHTML = ` + + + `; + this.container.querySelector('textarea').addEventListener('input', (e) => { + this.dispatchEvent(new CustomEvent("input", {detail:e.target.value,bubbles:true})) + const message = e.target.value; + const button = this.container.querySelector('button'); + button.disabled = !message; + }) + this.container.querySelector('textarea').addEventListener('change',(e)=>{ + this.dispatchEvent(new CustomEvent("change", {detail:e.target.value,bubbles:true})) + console.error(e.target.value) + }) + this.container.querySelector('textarea').addEventListener('keyup', (e) => { + if(e.key == 'Enter' && !e.shiftKey){ + this.dispatchEvent(new CustomEvent("submit", {detail:e.target.value,bubbles:true})) + e.target.value = '' + } + }) + this.component.appendChild(this.container) + } +} +customElements.define('chat-input', ChatInputElement); \ No newline at end of file