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"
|
2019-02-26 20:23:41 +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'"
|
2019-02-25 05:38:13 +00:00
|
|
|
:message="message"
|
2021-01-26 22:51:22 +00:00
|
|
|
:focused="message.id == focused"
|
2019-02-25 05:38:13 +00:00
|
|
|
/>
|
2018-07-13 10:43:11 +00:00
|
|
|
<div
|
2018-09-14 13:27:41 +00:00
|
|
|
v-if="shouldDisplayUnreadMarker(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"
|
2021-01-26 22:51:22 +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)"
|
2021-01-26 22:51:22 +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>
|
|
|
|
|
|
|
|
<script>
|
2019-12-17 22:10:50 +00:00
|
|
|
const constants = require("../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";
|
2018-07-08 17:22:01 +00:00
|
|
|
|
2020-04-28 14:21:49 +00:00
|
|
|
let unreadMarkerShown = false;
|
|
|
|
|
2018-07-08 17:22:01 +00:00
|
|
|
export default {
|
|
|
|
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: {
|
2018-07-19 17:44:24 +00:00
|
|
|
network: Object,
|
2018-07-08 17:22:01 +00:00
|
|
|
channel: Object,
|
2021-01-26 22:51:22 +00:00
|
|
|
focused: String,
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
2018-07-09 12:14:44 +00:00
|
|
|
computed: {
|
2018-07-11 09:27:44 +00:00
|
|
|
condensedMessages() {
|
2018-07-09 12:14:44 +00:00
|
|
|
if (this.channel.type !== "channel") {
|
|
|
|
return this.channel.messages;
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:16:21 +00:00
|
|
|
// If actions are hidden, just return a message list with them excluded
|
2019-11-03 18:05:19 +00:00
|
|
|
if (this.$store.state.settings.statusMessages === "hidden") {
|
2019-07-17 09:33:59 +00:00
|
|
|
return this.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
|
2019-11-03 18:05:19 +00:00
|
|
|
if (this.$store.state.settings.statusMessages !== "condensed") {
|
2018-07-18 11:16:21 +00:00
|
|
|
return this.channel.messages;
|
|
|
|
}
|
|
|
|
|
2018-07-09 12:14:44 +00:00
|
|
|
const condensed = [];
|
|
|
|
let lastCondensedContainer = null;
|
|
|
|
|
|
|
|
for (const message of this.channel.messages) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastCondensedContainer === null) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
lastCondensedContainer.id = message.id;
|
2018-07-23 09:27:52 +00:00
|
|
|
|
|
|
|
// If this message is the unread boundary, create a split condensed container
|
|
|
|
if (message.id === this.channel.firstUnread) {
|
|
|
|
lastCondensedContainer = null;
|
|
|
|
}
|
2018-07-09 12:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return condensed;
|
|
|
|
},
|
|
|
|
},
|
2018-07-13 10:43:11 +00:00
|
|
|
watch: {
|
2018-07-13 20:07:16 +00:00
|
|
|
"channel.id"() {
|
2018-07-18 18:31:51 +00:00
|
|
|
this.channel.scrolledToBottom = true;
|
2018-07-25 11:12:28 +00:00
|
|
|
|
|
|
|
// 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 (this.historyObserver) {
|
|
|
|
this.historyObserver.unobserve(this.$refs.loadMoreButton);
|
|
|
|
this.historyObserver.observe(this.$refs.loadMoreButton);
|
|
|
|
}
|
2018-07-13 20:07:16 +00:00
|
|
|
},
|
|
|
|
"channel.messages"() {
|
2018-07-13 10:43:11 +00:00
|
|
|
this.keepScrollPosition();
|
|
|
|
},
|
2018-07-16 16:43:25 +00:00
|
|
|
"channel.pendingMessage"() {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
// Keep the scroll stuck when input gets resized while typing
|
|
|
|
this.keepScrollPosition();
|
|
|
|
});
|
|
|
|
},
|
2018-07-13 10:43:11 +00:00
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (!this.$refs.chat) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.IntersectionObserver) {
|
|
|
|
this.historyObserver = new window.IntersectionObserver(this.onLoadButtonObserved, {
|
|
|
|
root: this.$refs.chat,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-18 18:31:51 +00:00
|
|
|
this.jumpToBottom();
|
2018-07-13 10:43:11 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
mounted() {
|
2018-09-21 15:36:21 +00:00
|
|
|
this.$refs.chat.addEventListener("scroll", this.handleScroll, {passive: true});
|
2018-07-13 19:53:30 +00:00
|
|
|
|
2020-03-16 17:58:40 +00:00
|
|
|
eventbus.on("resize", this.handleResize);
|
2018-08-31 18:15:46 +00:00
|
|
|
|
2018-07-13 10:43:11 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.historyObserver) {
|
|
|
|
this.historyObserver.observe(this.$refs.loadMoreButton);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2018-09-14 13:27:41 +00:00
|
|
|
beforeUpdate() {
|
2020-04-28 14:21:49 +00:00
|
|
|
unreadMarkerShown = false;
|
2018-09-14 13:27:41 +00:00
|
|
|
},
|
2018-07-13 17:39:35 +00:00
|
|
|
beforeDestroy() {
|
2020-03-16 17:58:40 +00:00
|
|
|
eventbus.off("resize", this.handleResize);
|
2018-09-21 15:36:21 +00:00
|
|
|
this.$refs.chat.removeEventListener("scroll", this.handleScroll);
|
2018-07-13 17:39:35 +00:00
|
|
|
},
|
2018-07-13 10:43:11 +00:00
|
|
|
destroyed() {
|
|
|
|
if (this.historyObserver) {
|
|
|
|
this.historyObserver.disconnect();
|
|
|
|
}
|
|
|
|
},
|
2018-07-08 17:22:01 +00:00
|
|
|
methods: {
|
2018-07-11 09:27:44 +00:00
|
|
|
shouldDisplayDateMarker(message, id) {
|
|
|
|
const previousMessage = this.condensedMessages[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()
|
|
|
|
);
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
2018-07-11 09:27:44 +00:00
|
|
|
shouldDisplayUnreadMarker(id) {
|
2020-04-28 14:21:49 +00:00
|
|
|
if (!unreadMarkerShown && id > this.channel.firstUnread) {
|
|
|
|
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;
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
2019-11-27 00:56:57 +00:00
|
|
|
isPreviousSource(currentMessage, id) {
|
|
|
|
const previousMessage = this.condensedMessages[id - 1];
|
|
|
|
return (
|
|
|
|
previousMessage &&
|
|
|
|
currentMessage.type === "message" &&
|
|
|
|
previousMessage.type === "message" &&
|
|
|
|
previousMessage.from &&
|
|
|
|
currentMessage.from.nick === previousMessage.from.nick
|
|
|
|
);
|
|
|
|
},
|
2018-07-09 18:37:52 +00:00
|
|
|
onCopy() {
|
|
|
|
clipboard(this.$el);
|
|
|
|
},
|
2018-07-11 20:45:01 +00:00
|
|
|
onLinkPreviewToggle(preview, message) {
|
2018-07-25 11:04:10 +00:00
|
|
|
this.keepScrollPosition();
|
|
|
|
|
2018-07-11 20:45:01 +00:00
|
|
|
// Tell the server we're toggling so it remembers at page reload
|
|
|
|
socket.emit("msg:preview:toggle", {
|
|
|
|
target: this.channel.id,
|
|
|
|
msgId: message.id,
|
|
|
|
link: preview.link,
|
|
|
|
shown: preview.shown,
|
|
|
|
});
|
|
|
|
},
|
2018-07-13 10:43:11 +00:00
|
|
|
onShowMoreClick() {
|
2019-02-26 20:23:41 +00:00
|
|
|
if (!this.$store.state.isConnected) {
|
2019-10-17 09:38:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-22 17:22:29 +00:00
|
|
|
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 this.channel.messages) {
|
|
|
|
if (!message.showInActive) {
|
|
|
|
lastMessage = message.id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 10:43:11 +00:00
|
|
|
|
2018-07-18 18:40:09 +00:00
|
|
|
this.channel.historyLoading = true;
|
2018-07-13 10:43:11 +00:00
|
|
|
|
|
|
|
socket.emit("more", {
|
|
|
|
target: this.channel.id,
|
|
|
|
lastId: lastMessage,
|
2019-12-17 22:11:11 +00:00
|
|
|
condensed: this.$store.state.settings.statusMessages !== "shown",
|
2018-07-13 10:43:11 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
onLoadButtonObserved(entries) {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
if (!entry.isIntersecting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-25 11:12:28 +00:00
|
|
|
this.onShowMoreClick();
|
2018-07-13 10:43:11 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
keepScrollPosition() {
|
|
|
|
// 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
|
|
|
|
if (this.isWaitingForNextTick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const el = this.$refs.chat;
|
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-13 20:40:20 +00:00
|
|
|
if (!this.channel.scrolledToBottom) {
|
2018-07-13 10:43:11 +00:00
|
|
|
if (this.channel.historyLoading) {
|
|
|
|
const heightOld = el.scrollHeight - el.scrollTop;
|
|
|
|
|
|
|
|
this.isWaitingForNextTick = true;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.isWaitingForNextTick = false;
|
2018-09-21 15:36:21 +00:00
|
|
|
this.skipNextScrollEvent = true;
|
2018-07-13 10:43:11 +00:00
|
|
|
el.scrollTop = el.scrollHeight - heightOld;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isWaitingForNextTick = true;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.isWaitingForNextTick = false;
|
2018-07-18 18:31:51 +00:00
|
|
|
this.jumpToBottom();
|
2018-07-13 10:43:11 +00:00
|
|
|
});
|
|
|
|
},
|
2018-07-13 17:39:35 +00:00
|
|
|
handleScroll() {
|
2018-09-21 15:36:21 +00:00
|
|
|
// Setting scrollTop also triggers scroll event
|
|
|
|
// We don't want to perform calculations for that
|
|
|
|
if (this.skipNextScrollEvent) {
|
|
|
|
this.skipNextScrollEvent = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-13 17:39:35 +00:00
|
|
|
const el = this.$refs.chat;
|
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-13 20:40:20 +00:00
|
|
|
this.channel.scrolledToBottom = el.scrollHeight - el.scrollTop - el.offsetHeight <= 30;
|
2018-07-13 17:39:35 +00:00
|
|
|
},
|
|
|
|
handleResize() {
|
|
|
|
// Keep message list scrolled to bottom on resize
|
2018-07-18 18:31:51 +00:00
|
|
|
if (this.channel.scrolledToBottom) {
|
|
|
|
this.jumpToBottom();
|
2018-07-13 17:39:35 +00:00
|
|
|
}
|
|
|
|
},
|
2018-07-18 18:31:51 +00:00
|
|
|
jumpToBottom() {
|
2018-09-21 15:36:21 +00:00
|
|
|
this.skipNextScrollEvent = true;
|
|
|
|
this.channel.scrolledToBottom = true;
|
|
|
|
|
2018-07-18 18:31:51 +00:00
|
|
|
const el = this.$refs.chat;
|
|
|
|
el.scrollTop = el.scrollHeight;
|
|
|
|
},
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|