Compare commits
2 commits
0d95b02401
...
addfddd3ac
Author | SHA1 | Date | |
---|---|---|---|
|
addfddd3ac | ||
|
e267ca4fc4 |
2 changed files with 17 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
||||||
<script>
|
<script>
|
||||||
import { HashIcon } from "svelte-feather-icons";
|
import { HashIcon } from "svelte-feather-icons";
|
||||||
import request from "../../../request";
|
import request from "../../../request";
|
||||||
import { apiRoute } from "../../../storage";
|
import { apiRoute } from "../../../storage";
|
||||||
import { messagesStoreProvider, userInfoStore } from "../../../stores";
|
import { messagesStoreProvider, userInfoStore } from "../../../stores";
|
||||||
import Messages from "./Messages.svelte";
|
import Messages from "./Messages.svelte";
|
||||||
|
|
||||||
|
@ -28,9 +28,10 @@ import { apiRoute } from "../../../storage";
|
||||||
createdAt: Date.now().toString()
|
createdAt: Date.now().toString()
|
||||||
};
|
};
|
||||||
messages.addMessage(optimisticMessage);
|
messages.addMessage(optimisticMessage);
|
||||||
|
messageInput = "";
|
||||||
|
|
||||||
const res = await request("POST", apiRoute(`channels/${channel.id}/messages`), true, {
|
const res = await request("POST", apiRoute(`channels/${channel.id}/messages`), true, {
|
||||||
content: messageInput
|
content: optimisticMessage.content
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.success && res.ok) {
|
if (res.success && res.ok) {
|
||||||
|
|
|
@ -1,10 +1,23 @@
|
||||||
<script>
|
<script>
|
||||||
|
import { afterUpdate, beforeUpdate } from "svelte";
|
||||||
import { messagesStoreProvider } from "../../../stores.js";
|
import { messagesStoreProvider } from "../../../stores.js";
|
||||||
|
|
||||||
export let channelId;
|
export let channelId;
|
||||||
|
let scrollTarget;
|
||||||
|
let shouldAutoscroll = false;
|
||||||
|
|
||||||
$: messages = messagesStoreProvider.getStore(channelId);
|
$: messages = messagesStoreProvider.getStore(channelId);
|
||||||
|
|
||||||
|
beforeUpdate(() => {
|
||||||
|
shouldAutoscroll = scrollTarget && (scrollTarget.offsetHeight + scrollTarget.scrollTop) > (scrollTarget.scrollHeight - 20);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterUpdate(() => {
|
||||||
|
if (shouldAutoscroll && scrollTarget) {
|
||||||
|
scrollTarget.scrollTo(0, scrollTarget.scrollHeight);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const onScroll = (e) => {
|
const onScroll = (e) => {
|
||||||
const { scrollTop, offsetHeight, scrollHeight } = e.target;
|
const { scrollTop, offsetHeight, scrollHeight } = e.target;
|
||||||
if (scrollTop === 0) {
|
if (scrollTop === 0) {
|
||||||
|
@ -30,7 +43,7 @@
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<div class="messages-container" on:scroll={ onScroll }>
|
<div class="messages-container" on:scroll={ onScroll } bind:this={ scrollTarget }>
|
||||||
{#each $messages as message (message.id)}
|
{#each $messages as message (message.id)}
|
||||||
<div>
|
<div>
|
||||||
<b>{ message.author_username }</b>
|
<b>{ message.author_username }</b>
|
||||||
|
|
Loading…
Reference in a new issue