2020-10-12 11:19:10 +00:00
|
|
|
import {q} from "./basic.js"
|
2020-10-15 13:24:15 +00:00
|
|
|
import {store} from "./store/store.js"
|
|
|
|
import * as lsm from "./lsm.js"
|
2020-10-19 07:23:28 +00:00
|
|
|
import {chat} from "./chat.js"
|
2020-10-15 13:24:15 +00:00
|
|
|
|
|
|
|
let sentIndex = 0
|
2020-10-12 11:19:10 +00:00
|
|
|
|
2020-10-19 07:23:28 +00:00
|
|
|
const input = q("#c-chat-textarea")
|
2020-10-12 11:19:10 +00:00
|
|
|
|
2020-10-19 07:23:28 +00:00
|
|
|
store.activeRoom.subscribe("changeSelf", () => {
|
|
|
|
if (store.activeRoom.exists()) {
|
|
|
|
input.focus()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
input.addEventListener("keydown", event => {
|
2020-10-12 11:19:10 +00:00
|
|
|
if (event.key === "Enter" && !event.shiftKey && !event.ctrlKey) {
|
|
|
|
event.preventDefault()
|
2020-10-19 07:23:28 +00:00
|
|
|
const body = input.value
|
|
|
|
send(input.value)
|
|
|
|
input.value = ""
|
2020-10-15 13:24:15 +00:00
|
|
|
fixHeight()
|
2020-10-12 11:19:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-10-19 07:23:28 +00:00
|
|
|
input.addEventListener("input", () => {
|
2020-10-15 13:24:15 +00:00
|
|
|
fixHeight()
|
|
|
|
})
|
|
|
|
|
|
|
|
function fixHeight() {
|
2020-10-19 07:23:28 +00:00
|
|
|
input.style.height = "0px"
|
|
|
|
// console.log(input.clientHeight, input.scrollHeight)
|
|
|
|
input.style.height = (input.scrollHeight + 1) + "px"
|
2020-10-15 13:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTxnId() {
|
|
|
|
return Date.now() + (sentIndex++)
|
|
|
|
}
|
2020-10-12 11:19:10 +00:00
|
|
|
|
2020-10-15 13:24:15 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|