2022-04-19 21:21:55 +03:00
|
|
|
<script>
|
2022-04-20 17:49:31 +03:00
|
|
|
import { afterUpdate, beforeUpdate } from "svelte";
|
2022-04-19 21:21:55 +03:00
|
|
|
import { messagesStoreProvider } from "../../../stores.js";
|
|
|
|
|
|
|
|
export let channelId;
|
2022-04-20 17:49:31 +03:00
|
|
|
let scrollTarget;
|
|
|
|
let shouldAutoscroll = false;
|
2022-04-22 16:43:26 +03:00
|
|
|
let lastScrollHeight = null;
|
2022-04-19 21:21:55 +03:00
|
|
|
|
2022-04-20 03:14:28 +03:00
|
|
|
$: messages = messagesStoreProvider.getStore(channelId);
|
2022-04-19 21:21:55 +03:00
|
|
|
|
2022-04-20 17:49:31 +03:00
|
|
|
beforeUpdate(() => {
|
|
|
|
shouldAutoscroll = scrollTarget && (scrollTarget.offsetHeight + scrollTarget.scrollTop) > (scrollTarget.scrollHeight - 20);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterUpdate(() => {
|
2022-04-22 16:43:26 +03:00
|
|
|
// hacky way to preserve scroll position when messages are pushed back
|
|
|
|
if (lastScrollHeight) {
|
|
|
|
scrollTarget.scrollTop = scrollTarget.scrollHeight - lastScrollHeight;
|
|
|
|
lastScrollHeight = null;
|
|
|
|
return;
|
|
|
|
}
|
2022-04-20 17:49:31 +03:00
|
|
|
if (shouldAutoscroll && scrollTarget) {
|
2022-04-20 17:53:20 +03:00
|
|
|
scrollTarget.scrollTo(0, scrollTarget.scrollHeight);
|
2022-04-20 17:49:31 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
const onScroll = (e) => {
|
|
|
|
const { scrollTop, offsetHeight, scrollHeight } = e.target;
|
2022-04-22 16:43:26 +03:00
|
|
|
if ((scrollTop + offsetHeight) >= scrollHeight) { // user scrolled to bottom
|
2022-04-20 03:14:28 +03:00
|
|
|
messages.setIsCollectingOldMessages(true);
|
2022-04-19 21:21:55 +03:00
|
|
|
} else {
|
2022-04-21 02:11:44 +03:00
|
|
|
if (scrollTop === 0) {
|
2022-04-22 16:43:26 +03:00
|
|
|
// load older messages if the user scrolls to the top.
|
|
|
|
// save the current scroll height if the server returned any messages,
|
|
|
|
// before commiting them to the store. this is to provide the jank scroll height
|
|
|
|
// preservation
|
|
|
|
messages.loadOlderMessages(() => {
|
|
|
|
if (scrollTarget)
|
|
|
|
lastScrollHeight = scrollTarget.scrollHeight;
|
|
|
|
});
|
2022-04-21 02:11:44 +03:00
|
|
|
}
|
2022-04-20 03:14:28 +03:00
|
|
|
messages.setIsCollectingOldMessages(false);
|
2022-04-19 21:21:55 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.messages-container {
|
2022-04-22 23:10:00 +03:00
|
|
|
padding: var(--space-normplus);
|
2022-04-22 23:01:44 +03:00
|
|
|
padding-bottom: 0;
|
2022-04-19 21:21:55 +03:00
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
flex-grow: 0;
|
|
|
|
overflow-y: auto;
|
2022-04-22 23:01:44 +03:00
|
|
|
overflow-x: hidden;
|
2022-04-19 21:21:55 +03:00
|
|
|
background-color: var(--background-color-1);
|
|
|
|
}
|
2022-04-21 01:55:37 +03:00
|
|
|
|
2022-04-22 23:01:44 +03:00
|
|
|
.message {
|
|
|
|
overflow-x: none;
|
|
|
|
word-break: break-all;
|
|
|
|
}
|
|
|
|
|
|
|
|
.message-content {
|
2022-04-21 01:55:37 +03:00
|
|
|
color: var(--foreground-color-2);
|
|
|
|
}
|
2022-04-22 23:01:44 +03:00
|
|
|
|
|
|
|
.pending {
|
|
|
|
color: var(--foreground-color-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
.author {
|
|
|
|
font-weight: bold;
|
|
|
|
margin-right: var(--space-xxs);
|
|
|
|
}
|
2022-04-19 21:21:55 +03:00
|
|
|
</style>
|
|
|
|
|
2022-04-20 17:53:20 +03:00
|
|
|
<div class="messages-container" on:scroll={ onScroll } bind:this={ scrollTarget }>
|
2022-04-19 21:21:55 +03:00
|
|
|
{#each $messages as message (message.id)}
|
2022-04-22 23:01:44 +03:00
|
|
|
<div class="message">
|
|
|
|
<span class="author">{ message.author_username }</span>
|
|
|
|
<span class="message-content" class:pending={ message._isPending }>{ message.content }</span>
|
2022-04-19 21:21:55 +03:00
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|