2018-07-08 17:22:01 +00:00
|
|
|
<template>
|
2019-07-17 09:33:59 +00:00
|
|
|
<div ref="chat" class="chat" tabindex="-1">
|
2019-12-12 13:05:02 +00:00
|
|
|
<div v-show="channel.moreHistoryAvailable" class="show-more">
|
2018-07-13 10:43:11 +00:00
|
|
|
<button
|
|
|
|
ref="loadMoreButton"
|
2022-06-19 00:25:21 +00:00
|
|
|
:disabled="channel.historyLoading || !store.state.isConnected"
|
2018-07-13 10:43:11 +00:00
|
|
|
class="btn"
|
2019-02-25 05:38:13 +00:00
|
|
|
@click="onShowMoreClick"
|
|
|
|
>
|
2018-07-13 10:43:11 +00:00
|
|
|
<span v-if="channel.historyLoading">Loading…</span>
|
|
|
|
<span v-else>Show older messages</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
class="messages"
|
|
|
|
role="log"
|
|
|
|
aria-live="polite"
|
|
|
|
aria-relevant="additions"
|
2019-02-25 05:38:13 +00:00
|
|
|
@copy="onCopy"
|
|
|
|
>
|
2018-07-13 10:43:11 +00:00
|
|
|
<template v-for="(message, id) in condensedMessages">
|
2018-09-12 17:14:45 +00:00
|
|
|
<DateMarker
|
2018-07-13 10:43:11 +00:00
|
|
|
v-if="shouldDisplayDateMarker(message, id)"
|
|
|
|
:key="message.id + '-date'"
|
2022-06-19 00:25:21 +00:00
|
|
|
:message="message as any"
|
|
|
|
:focused="message.id === focused"
|
2019-02-25 05:38:13 +00:00
|
|
|
/>
|
2018-07-13 10:43:11 +00:00
|
|
|
<div
|
2022-06-19 00:25:21 +00:00
|
|
|
v-if="shouldDisplayUnreadMarker(Number(message.id))"
|
2018-07-13 10:43:11 +00:00
|
|
|
:key="message.id + '-unread'"
|
2019-02-25 05:38:13 +00:00
|
|
|
class="unread-marker"
|
|
|
|
>
|
2018-07-29 17:57:14 +00:00
|
|
|
<span class="unread-marker-text" />
|
2018-07-13 10:43:11 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<MessageCondensed
|
|
|
|
v-if="message.type === 'condensed'"
|
2019-10-03 20:44:11 +00:00
|
|
|
:key="message.messages[0].id"
|
2018-07-19 17:44:24 +00:00
|
|
|
:network="network"
|
2018-07-29 17:57:14 +00:00
|
|
|
:keep-scroll-position="keepScrollPosition"
|
2019-02-25 05:38:13 +00:00
|
|
|
:messages="message.messages"
|
2022-06-19 00:25:21 +00:00
|
|
|
:focused="message.id === focused"
|
2019-02-25 05:38:13 +00:00
|
|
|
/>
|
2018-07-13 10:43:11 +00:00
|
|
|
<Message
|
|
|
|
v-else
|
2018-07-29 17:57:14 +00:00
|
|
|
:key="message.id"
|
2019-11-09 22:21:34 +00:00
|
|
|
:channel="channel"
|
2018-07-19 17:44:24 +00:00
|
|
|
:network="network"
|
2018-07-13 10:43:11 +00:00
|
|
|
:message="message"
|
|
|
|
:keep-scroll-position="keepScrollPosition"
|
2019-11-27 00:56:57 +00:00
|
|
|
:is-previous-source="isPreviousSource(message, id)"
|
2022-06-19 00:25:21 +00:00
|
|
|
:focused="message.id === focused"
|
2020-09-30 14:44:07 +00:00
|
|
|
@toggle-link-preview="onLinkPreviewToggle"
|
2019-02-25 05:38:13 +00:00
|
|
|
/>
|
2018-07-13 10:43:11 +00:00
|
|
|
</template>
|
|
|
|
</div>
|
2018-07-08 17:22:01 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
<script lang="ts">
|
|
|
|
import constants from "../js/constants";
|
2020-03-16 17:58:40 +00:00
|
|
|
import eventbus from "../js/eventbus";
|
2019-11-16 17:24:03 +00:00
|
|
|
import clipboard from "../js/clipboard";
|
2018-07-11 20:45:01 +00:00
|
|
|
import socket from "../js/socket";
|
2018-07-08 17:22:01 +00:00
|
|
|
import Message from "./Message.vue";
|
2018-07-09 12:14:44 +00:00
|
|
|
import MessageCondensed from "./MessageCondensed.vue";
|
2018-09-12 17:14:45 +00:00
|
|
|
import DateMarker from "./DateMarker.vue";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {
|
|
|
|
computed,
|
|
|
|
defineComponent,
|
|
|
|
nextTick,
|
|
|
|
onBeforeUnmount,
|
|
|
|
onBeforeUpdate,
|
|
|
|
onMounted,
|
|
|
|
onUnmounted,
|
|
|
|
PropType,
|
|
|
|
ref,
|
|
|
|
watch,
|
|
|
|
} from "vue";
|
|
|
|
import {useStore} from "../js/store";
|
|
|
|
import {ClientChan, ClientMessage, ClientNetwork, ClientLinkPreview} from "../js/types";
|
|
|
|
import Msg from "../../server/models/msg";
|
|
|
|
|
|
|
|
type CondensedMessageContainer = {
|
|
|
|
type: "condensed";
|
|
|
|
time: Date;
|
|
|
|
messages: ClientMessage[];
|
|
|
|
id?: number;
|
|
|
|
};
|
2018-07-08 17:22:01 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
// TODO; move into component
|
2020-04-28 14:21:49 +00:00
|
|
|
let unreadMarkerShown = false;
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default defineComponent({
|
2018-07-08 17:22:01 +00:00
|
|
|
name: "MessageList",
|
|
|
|
components: {
|
|
|
|
Message,
|
2018-07-09 12:14:44 +00:00
|
|
|
MessageCondensed,
|
2018-09-12 17:14:45 +00:00
|
|
|
DateMarker,
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
|
|
|
props: {
|
2022-06-19 00:25:21 +00:00
|
|
|
network: {type: Object as PropType<ClientNetwork>, required: true},
|
|
|
|
channel: {type: Object as PropType<ClientChan>, required: true},
|
|
|
|
focused: Number,
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
setup(props, {emit}) {
|
|
|
|
const store = useStore();
|
|
|
|
|
|
|
|
const chat = ref<HTMLDivElement | null>(null);
|
|
|
|
const loadMoreButton = ref<HTMLButtonElement | null>(null);
|
|
|
|
const historyObserver = ref<IntersectionObserver | null>(null);
|
|
|
|
const skipNextScrollEvent = ref(false);
|
|
|
|
|
|
|
|
const isWaitingForNextTick = ref(false);
|
|
|
|
|
|
|
|
const jumpToBottom = () => {
|
|
|
|
skipNextScrollEvent.value = true;
|
|
|
|
props.channel.scrolledToBottom = true;
|
|
|
|
|
|
|
|
const el = chat.value;
|
|
|
|
|
|
|
|
if (el) {
|
|
|
|
el.scrollTop = el.scrollHeight;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onShowMoreClick = () => {
|
|
|
|
if (!store.state.isConnected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lastMessage = -1;
|
|
|
|
|
|
|
|
// Find the id of first message that isn't showInActive
|
|
|
|
// If showInActive is set, this message is actually in another channel
|
|
|
|
for (const message of props.channel.messages) {
|
|
|
|
if (!message.showInActive) {
|
|
|
|
lastMessage = message.id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
props.channel.historyLoading = true;
|
|
|
|
|
|
|
|
socket.emit("more", {
|
|
|
|
target: props.channel.id,
|
|
|
|
lastId: lastMessage,
|
|
|
|
condensed: store.state.settings.statusMessages !== "shown",
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onLoadButtonObserved = (entries: IntersectionObserverEntry[]) => {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
if (!entry.isIntersecting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onShowMoreClick();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
if (!chat.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.IntersectionObserver) {
|
|
|
|
historyObserver.value = new window.IntersectionObserver(onLoadButtonObserved, {
|
|
|
|
root: chat.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
jumpToBottom();
|
|
|
|
}).catch((e) => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error("Error in new IntersectionObserver", e);
|
|
|
|
});
|
|
|
|
|
|
|
|
const condensedMessages = computed(() => {
|
|
|
|
if (props.channel.type !== "channel") {
|
|
|
|
return props.channel.messages;
|
2018-07-09 12:14:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 11:16:21 +00:00
|
|
|
// If actions are hidden, just return a message list with them excluded
|
2022-06-19 00:25:21 +00:00
|
|
|
if (store.state.settings.statusMessages === "hidden") {
|
|
|
|
return props.channel.messages.filter(
|
2019-12-17 10:48:12 +00:00
|
|
|
(message) => !constants.condensedTypes.has(message.type)
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2018-07-18 11:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If actions are not condensed, just return raw message list
|
2022-06-19 00:25:21 +00:00
|
|
|
if (store.state.settings.statusMessages !== "condensed") {
|
|
|
|
return props.channel.messages;
|
2018-07-18 11:16:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
let lastCondensedContainer: CondensedMessageContainer | null = null;
|
2018-07-09 12:14:44 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const condensed: (ClientMessage | CondensedMessageContainer)[] = [];
|
|
|
|
|
|
|
|
for (const message of props.channel.messages) {
|
2018-07-09 12:14:44 +00:00
|
|
|
// If this message is not condensable, or its an action affecting our user,
|
|
|
|
// then just append the message to container and be done with it
|
2019-07-17 09:33:59 +00:00
|
|
|
if (
|
|
|
|
message.self ||
|
|
|
|
message.highlight ||
|
2019-12-17 10:48:12 +00:00
|
|
|
!constants.condensedTypes.has(message.type)
|
2019-07-17 09:33:59 +00:00
|
|
|
) {
|
2018-07-09 12:14:44 +00:00
|
|
|
lastCondensedContainer = null;
|
|
|
|
|
|
|
|
condensed.push(message);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!lastCondensedContainer) {
|
2018-07-09 12:14:44 +00:00
|
|
|
lastCondensedContainer = {
|
2018-07-11 09:27:44 +00:00
|
|
|
time: message.time,
|
2018-07-09 12:14:44 +00:00
|
|
|
type: "condensed",
|
|
|
|
messages: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
condensed.push(lastCondensedContainer);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
lastCondensedContainer!.messages.push(message);
|
2018-07-23 06:58:35 +00:00
|
|
|
|
|
|
|
// Set id of the condensed container to last message id,
|
|
|
|
// which is required for the unread marker to work correctly
|
2022-06-19 00:25:21 +00:00
|
|
|
lastCondensedContainer!.id = message.id;
|
2018-07-23 09:27:52 +00:00
|
|
|
|
|
|
|
// If this message is the unread boundary, create a split condensed container
|
2022-06-19 00:25:21 +00:00
|
|
|
if (message.id === props.channel.firstUnread) {
|
2018-07-23 09:27:52 +00:00
|
|
|
lastCondensedContainer = null;
|
|
|
|
}
|
2018-07-09 12:14:44 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 19:33:40 +00:00
|
|
|
return condensed.map((message) => {
|
|
|
|
// Skip condensing single messages, it doesn't save any
|
|
|
|
// space but makes useful information harder to see
|
|
|
|
if (message.type === "condensed" && message.messages.length === 1) {
|
|
|
|
return message.messages[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
});
|
2018-07-13 10:43:11 +00:00
|
|
|
});
|
2018-07-13 19:53:30 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const shouldDisplayDateMarker = (
|
|
|
|
message: Msg | ClientMessage | CondensedMessageContainer,
|
|
|
|
id: number
|
|
|
|
) => {
|
|
|
|
const previousMessage = condensedMessages.value[id - 1];
|
2018-07-08 17:22:01 +00:00
|
|
|
|
2018-07-11 09:27:44 +00:00
|
|
|
if (!previousMessage) {
|
2018-07-08 17:22:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:43:43 +00:00
|
|
|
const oldDate = new Date(previousMessage.time);
|
|
|
|
const newDate = new Date(message.time);
|
|
|
|
|
|
|
|
return (
|
|
|
|
oldDate.getDate() !== newDate.getDate() ||
|
|
|
|
oldDate.getMonth() !== newDate.getMonth() ||
|
|
|
|
oldDate.getFullYear() !== newDate.getFullYear()
|
|
|
|
);
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const shouldDisplayUnreadMarker = (id: number) => {
|
|
|
|
if (!unreadMarkerShown && id > props.channel.firstUnread) {
|
2020-04-28 14:21:49 +00:00
|
|
|
unreadMarkerShown = true;
|
2018-09-14 13:27:41 +00:00
|
|
|
return true;
|
2018-07-11 09:27:44 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 13:27:41 +00:00
|
|
|
return false;
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const isPreviousSource = (currentMessage: ClientMessage | Msg, id: number) => {
|
|
|
|
const previousMessage = condensedMessages[id - 1];
|
|
|
|
return !!(
|
2019-11-27 00:56:57 +00:00
|
|
|
previousMessage &&
|
|
|
|
currentMessage.type === "message" &&
|
|
|
|
previousMessage.type === "message" &&
|
|
|
|
previousMessage.from &&
|
|
|
|
currentMessage.from.nick === previousMessage.from.nick
|
|
|
|
);
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
2018-07-25 11:04:10 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const onCopy = () => {
|
|
|
|
if (chat.value) {
|
|
|
|
clipboard(chat.value);
|
2019-10-17 09:38:46 +00:00
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
2019-10-17 09:38:46 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const keepScrollPosition = async () => {
|
2018-07-13 10:43:11 +00:00
|
|
|
// If we are already waiting for the next tick to force scroll position,
|
|
|
|
// we have no reason to perform more checks and set it again in the next tick
|
2022-06-19 00:25:21 +00:00
|
|
|
if (isWaitingForNextTick.value) {
|
2018-07-13 10:43:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const el = chat.value;
|
2018-07-13 10:43:11 +00:00
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!props.channel.scrolledToBottom) {
|
|
|
|
if (props.channel.historyLoading) {
|
2018-07-13 10:43:11 +00:00
|
|
|
const heightOld = el.scrollHeight - el.scrollTop;
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
isWaitingForNextTick.value = true;
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
isWaitingForNextTick.value = false;
|
|
|
|
skipNextScrollEvent.value = true;
|
|
|
|
|
|
|
|
el.scrollTop = el.scrollHeight - heightOld;
|
2018-07-13 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
isWaitingForNextTick.value = true;
|
|
|
|
await nextTick();
|
|
|
|
isWaitingForNextTick.value = false;
|
|
|
|
|
|
|
|
jumpToBottom();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onLinkPreviewToggle = async (preview: ClientLinkPreview, message: ClientMessage) => {
|
|
|
|
await keepScrollPosition();
|
|
|
|
|
|
|
|
// Tell the server we're toggling so it remembers at page reload
|
|
|
|
socket.emit("msg:preview:toggle", {
|
|
|
|
target: props.channel.id,
|
|
|
|
msgId: message.id,
|
|
|
|
link: preview.link,
|
|
|
|
shown: preview.shown,
|
2018-07-13 10:43:11 +00:00
|
|
|
});
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleScroll = () => {
|
2018-09-21 15:36:21 +00:00
|
|
|
// Setting scrollTop also triggers scroll event
|
|
|
|
// We don't want to perform calculations for that
|
2022-06-19 00:25:21 +00:00
|
|
|
if (skipNextScrollEvent.value) {
|
|
|
|
skipNextScrollEvent.value = false;
|
2018-09-21 15:36:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const el = chat.value;
|
2018-07-13 17:39:35 +00:00
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
props.channel.scrolledToBottom = el.scrollHeight - el.scrollTop - el.offsetHeight <= 30;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleResize = () => {
|
2018-07-13 17:39:35 +00:00
|
|
|
// Keep message list scrolled to bottom on resize
|
2022-06-19 00:25:21 +00:00
|
|
|
if (props.channel.scrolledToBottom) {
|
|
|
|
jumpToBottom();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
chat.value?.addEventListener("scroll", handleScroll, {passive: true});
|
|
|
|
|
|
|
|
eventbus.on("resize", handleResize);
|
|
|
|
|
|
|
|
void nextTick(() => {
|
|
|
|
if (historyObserver.value && loadMoreButton.value) {
|
|
|
|
historyObserver.value.observe(loadMoreButton.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.channel.id,
|
|
|
|
() => {
|
|
|
|
props.channel.scrolledToBottom = true;
|
|
|
|
|
|
|
|
// Re-add the intersection observer to trigger the check again on channel switch
|
|
|
|
// Otherwise if last channel had the button visible, switching to a new channel won't trigger the history
|
|
|
|
if (historyObserver.value && loadMoreButton.value) {
|
|
|
|
historyObserver.value.unobserve(loadMoreButton.value);
|
|
|
|
historyObserver.value.observe(loadMoreButton.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.channel.messages,
|
|
|
|
async () => {
|
|
|
|
await keepScrollPosition();
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deep: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.channel.pendingMessage,
|
|
|
|
async () => {
|
|
|
|
// Keep the scroll stuck when input gets resized while typing
|
|
|
|
await keepScrollPosition();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
onBeforeUpdate(() => {
|
|
|
|
unreadMarkerShown = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
eventbus.off("resize", handleResize);
|
|
|
|
chat.value?.removeEventListener("scroll", handleScroll);
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
if (historyObserver.value) {
|
|
|
|
historyObserver.value.disconnect();
|
2018-07-13 17:39:35 +00:00
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
chat,
|
|
|
|
store,
|
|
|
|
onShowMoreClick,
|
|
|
|
loadMoreButton,
|
|
|
|
onCopy,
|
|
|
|
condensedMessages,
|
|
|
|
shouldDisplayDateMarker,
|
|
|
|
shouldDisplayUnreadMarker,
|
|
|
|
keepScrollPosition,
|
|
|
|
isPreviousSource,
|
|
|
|
jumpToBottom,
|
|
|
|
onLinkPreviewToggle,
|
|
|
|
};
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
});
|
2018-07-08 17:22:01 +00:00
|
|
|
</script>
|