2022-04-19 21:21:55 +03:00
|
|
|
<script>
|
2022-04-24 20:14:04 +03:00
|
|
|
import { afterUpdate, beforeUpdate, onMount } from "svelte";
|
2022-04-27 15:44:50 +03:00
|
|
|
import { messagesStoreProvider, showSidebar } from "../stores.js";
|
2022-04-24 20:14:04 +03:00
|
|
|
import Message from "./Message.svelte";
|
2022-04-19 21:21:55 +03:00
|
|
|
|
|
|
|
export let channelId;
|
2022-04-20 17:49:31 +03:00
|
|
|
let scrollTarget;
|
2022-04-24 20:14:04 +03:00
|
|
|
let scrollAnchor;
|
|
|
|
let shouldAutoscroll = true;
|
2022-04-22 16:43:26 +03:00
|
|
|
let lastScrollHeight = null;
|
2022-04-27 15:44:50 +03:00
|
|
|
let isScrolledToBottom = true;
|
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
|
|
|
afterUpdate(() => {
|
2022-04-22 16:43:26 +03:00
|
|
|
// hacky way to preserve scroll position when messages are pushed back
|
|
|
|
if (lastScrollHeight) {
|
2022-04-24 20:14:04 +03:00
|
|
|
scrollTarget.scrollTop = scrollTarget.scrollHeight - lastScrollHeight;
|
2022-04-22 16:43:26 +03:00
|
|
|
lastScrollHeight = null;
|
|
|
|
return;
|
|
|
|
}
|
2022-04-24 20:14:04 +03:00
|
|
|
if (shouldAutoscroll && scrollAnchor) {
|
|
|
|
scrollAnchor.scrollIntoView(false);
|
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-24 20:14:04 +03:00
|
|
|
shouldAutoscroll = true;
|
2022-04-27 05:03:47 +03:00
|
|
|
isScrolledToBottom = true;
|
2022-04-19 21:21:55 +03:00
|
|
|
} else {
|
2022-04-24 20:14:04 +03:00
|
|
|
shouldAutoscroll = false;
|
2022-04-27 05:03:47 +03:00
|
|
|
isScrolledToBottom = false;
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-27 15:44:50 +03:00
|
|
|
const windowDidResize = () => {
|
|
|
|
// TODO: hack
|
|
|
|
// showSidebar is false on small viewports
|
|
|
|
// scrolling to bottom when the virtual keyboard pops up does not work on chromium purely based on isScrolledToBottom for some reason
|
|
|
|
const isSmallViewport = !$showSidebar;
|
|
|
|
if (isScrolledToBottom || isSmallViewport) {
|
|
|
|
scrollAnchor.scrollIntoView(false);
|
|
|
|
}
|
|
|
|
};
|
2022-04-19 21:21:55 +03:00
|
|
|
</script>
|
|
|
|
|
2022-04-27 15:44:50 +03:00
|
|
|
<svelte:window on:resize={ windowDidResize } />
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
<style>
|
|
|
|
.messages-container {
|
|
|
|
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-27 20:19:40 +03:00
|
|
|
padding-top: var(--space-sm);
|
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-24 20:14:04 +03:00
|
|
|
<Message message={message} />
|
2022-04-19 21:21:55 +03:00
|
|
|
{/each}
|
2022-04-24 20:14:04 +03:00
|
|
|
<div bind:this={ scrollAnchor } />
|
2022-04-19 21:21:55 +03:00
|
|
|
</div>
|