46 lines
1 KiB
JavaScript
46 lines
1 KiB
JavaScript
import {q} from "./basic.js"
|
|
import {store} from "./store/store.js"
|
|
import * as lsm from "./lsm.js"
|
|
|
|
let sentIndex = 0
|
|
|
|
const chat = q("#c-chat-textarea")
|
|
|
|
chat.addEventListener("keydown", event => {
|
|
if (event.key === "Enter" && !event.shiftKey && !event.ctrlKey) {
|
|
event.preventDefault()
|
|
const body = chat.value
|
|
send(chat.value)
|
|
chat.value = ""
|
|
fixHeight()
|
|
}
|
|
})
|
|
|
|
chat.addEventListener("input", () => {
|
|
fixHeight()
|
|
})
|
|
|
|
function fixHeight() {
|
|
chat.style.height = "0px"
|
|
console.log(chat.clientHeight, chat.scrollHeight)
|
|
chat.style.height = (chat.scrollHeight + 1) + "px"
|
|
}
|
|
|
|
function getTxnId() {
|
|
return Date.now() + (sentIndex++)
|
|
}
|
|
|
|
function send(body) {
|
|
if (!store.activeRoom.exists()) return
|
|
const id = store.activeRoom.value().id
|
|
return fetch(`${lsm.get("domain")}/_matrix/client/r0/rooms/${id}/send/m.room.message/${getTxnId()}?access_token=${lsm.get("access_token")}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify({
|
|
msgtype: "m.text",
|
|
body
|
|
}),
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
}
|
|
})
|
|
}
|