2021-07-07 18:07:58 +02:00
|
|
|
<script lang="ts">
|
2021-12-06 16:12:37 +01:00
|
|
|
import { fly } from "svelte/transition";
|
2021-07-12 15:00:28 +02:00
|
|
|
import { chatMessagesStore, chatVisibilityStore } from "../../Stores/ChatStore";
|
2021-12-06 16:12:37 +01:00
|
|
|
import ChatMessageForm from "./ChatMessageForm.svelte";
|
|
|
|
import ChatElement from "./ChatElement.svelte";
|
|
|
|
import { afterUpdate, beforeUpdate, onMount } from "svelte";
|
|
|
|
import { HtmlUtils } from "../../WebRtc/HtmlUtils";
|
|
|
|
|
2021-07-07 18:07:58 +02:00
|
|
|
let listDom: HTMLElement;
|
2021-07-27 14:28:35 +02:00
|
|
|
let chatWindowElement: HTMLElement;
|
2021-12-06 16:12:37 +01:00
|
|
|
let handleFormBlur: { blur(): void };
|
2021-07-12 15:00:28 +02:00
|
|
|
let autoscroll: boolean;
|
|
|
|
|
|
|
|
beforeUpdate(() => {
|
2021-12-06 16:12:37 +01:00
|
|
|
autoscroll = listDom && listDom.offsetHeight + listDom.scrollTop > listDom.scrollHeight - 20;
|
2021-07-12 15:00:28 +02:00
|
|
|
});
|
|
|
|
|
2021-09-14 09:36:49 +02:00
|
|
|
onMount(() => {
|
|
|
|
listDom.scrollTo(0, listDom.scrollHeight);
|
2021-12-06 16:12:37 +01:00
|
|
|
});
|
2021-09-14 09:36:49 +02:00
|
|
|
|
2021-07-12 15:00:28 +02:00
|
|
|
afterUpdate(() => {
|
|
|
|
if (autoscroll) listDom.scrollTo(0, listDom.scrollHeight);
|
|
|
|
});
|
2021-07-07 18:07:58 +02:00
|
|
|
|
2021-07-27 14:28:35 +02:00
|
|
|
function onClick(event: MouseEvent) {
|
|
|
|
if (HtmlUtils.isClickedOutside(event, chatWindowElement)) {
|
|
|
|
handleFormBlur.blur();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:07:58 +02:00
|
|
|
function closeChat() {
|
|
|
|
chatVisibilityStore.set(false);
|
|
|
|
}
|
2021-12-06 16:12:37 +01:00
|
|
|
function onKeyDown(e: KeyboardEvent) {
|
|
|
|
if (e.key === "Escape") {
|
2021-07-07 18:07:58 +02:00
|
|
|
closeChat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-12-06 16:12:37 +01:00
|
|
|
<svelte:window on:keydown={onKeyDown} on:click={onClick} />
|
2021-07-07 18:07:58 +02:00
|
|
|
|
2021-12-06 16:12:37 +01:00
|
|
|
<aside class="chatWindow" transition:fly={{ x: -1000, duration: 500 }} bind:this={chatWindowElement}>
|
2021-07-20 10:28:40 +02:00
|
|
|
<p class="close-icon" on:click={closeChat}>×</p>
|
2021-07-12 15:00:28 +02:00
|
|
|
<section class="messagesList" bind:this={listDom}>
|
|
|
|
<ul>
|
2021-12-06 16:12:37 +01:00
|
|
|
<li><p class="system-text">Here is your chat history:</p></li>
|
|
|
|
{#each $chatMessagesStore as message, i}
|
|
|
|
<li><ChatElement {message} line={i} /></li>
|
|
|
|
{/each}
|
2021-07-07 18:07:58 +02:00
|
|
|
</ul>
|
|
|
|
</section>
|
|
|
|
<section class="messageForm">
|
2021-12-06 16:12:37 +01:00
|
|
|
<ChatMessageForm bind:handleForm={handleFormBlur} />
|
2021-07-07 18:07:58 +02:00
|
|
|
</section>
|
|
|
|
</aside>
|
|
|
|
|
|
|
|
<style lang="scss">
|
2021-07-20 10:28:40 +02:00
|
|
|
p.close-icon {
|
2021-12-06 16:12:37 +01:00
|
|
|
position: absolute;
|
|
|
|
padding: 4px;
|
|
|
|
right: 12px;
|
|
|
|
font-size: 30px;
|
|
|
|
line-height: 25px;
|
|
|
|
cursor: pointer;
|
2021-07-15 19:12:19 +02:00
|
|
|
}
|
2021-12-06 16:12:37 +01:00
|
|
|
|
2021-07-15 19:12:19 +02:00
|
|
|
p.system-text {
|
2021-12-06 16:12:37 +01:00
|
|
|
border-radius: 8px;
|
|
|
|
margin-bottom: 10px;
|
|
|
|
padding: 6px;
|
|
|
|
overflow-wrap: break-word;
|
|
|
|
max-width: 100%;
|
|
|
|
background: gray;
|
|
|
|
display: inline-block;
|
2021-07-07 18:07:58 +02:00
|
|
|
}
|
2021-07-12 11:59:05 +02:00
|
|
|
|
2021-07-07 18:07:58 +02:00
|
|
|
aside.chatWindow {
|
2021-12-06 16:12:37 +01:00
|
|
|
z-index: 100;
|
|
|
|
pointer-events: auto;
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
height: 100vh;
|
|
|
|
width: 30vw;
|
|
|
|
min-width: 350px;
|
|
|
|
background: rgb(5, 31, 51, 0.9);
|
|
|
|
color: whitesmoke;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-07-07 18:07:58 +02:00
|
|
|
|
2021-12-06 16:12:37 +01:00
|
|
|
padding: 10px;
|
2021-07-07 18:07:58 +02:00
|
|
|
|
2021-12-06 16:12:37 +01:00
|
|
|
border-bottom-right-radius: 16px;
|
|
|
|
border-top-right-radius: 16px;
|
|
|
|
|
|
|
|
.messagesList {
|
|
|
|
margin-top: 35px;
|
|
|
|
overflow-y: auto;
|
|
|
|
flex: auto;
|
|
|
|
|
|
|
|
ul {
|
|
|
|
list-style-type: none;
|
|
|
|
padding-left: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.messageForm {
|
|
|
|
flex: 0 70px;
|
|
|
|
padding-top: 15px;
|
2021-07-07 18:07:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-06 16:12:37 +01:00
|
|
|
</style>
|