2018-07-08 17:22:01 +00:00
|
|
|
<template>
|
|
|
|
<div
|
2018-07-13 10:43:11 +00:00
|
|
|
ref="chat"
|
|
|
|
class="chat"
|
2018-07-08 17:22:01 +00:00
|
|
|
>
|
2018-07-13 10:43:11 +00:00
|
|
|
<div :class="['show-more', { show: channel.moreHistoryAvailable }]">
|
|
|
|
<button
|
|
|
|
ref="loadMoreButton"
|
|
|
|
:disabled="channel.historyLoading || !$root.connected"
|
|
|
|
class="btn"
|
|
|
|
@click="onShowMoreClick"
|
2018-07-08 17:22:01 +00:00
|
|
|
>
|
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"
|
|
|
|
@copy="onCopy"
|
|
|
|
>
|
|
|
|
<template v-for="(message, id) in condensedMessages">
|
|
|
|
<div
|
|
|
|
v-if="shouldDisplayDateMarker(message, id)"
|
|
|
|
:key="message.id + '-date'"
|
|
|
|
:data-time="message.time"
|
|
|
|
:aria-label="message.time | localedate"
|
|
|
|
class="date-marker-container tooltipped tooltipped-s"
|
|
|
|
>
|
|
|
|
<div class="date-marker">
|
|
|
|
<span
|
|
|
|
:data-label="message.time | friendlydate"
|
|
|
|
class="date-marker-text"/>
|
|
|
|
</div>
|
2018-07-08 17:22:01 +00:00
|
|
|
</div>
|
2018-07-13 10:43:11 +00:00
|
|
|
<div
|
|
|
|
v-if="shouldDisplayUnreadMarker(id)"
|
|
|
|
:key="message.id + '-unread'"
|
|
|
|
class="unread-marker"
|
|
|
|
>
|
|
|
|
<span class="unread-marker-text"/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<MessageCondensed
|
|
|
|
v-if="message.type === 'condensed'"
|
|
|
|
:key="message.id"
|
2018-07-19 17:44:24 +00:00
|
|
|
:network="network"
|
2018-07-13 10:43:11 +00:00
|
|
|
:messages="message.messages"/>
|
|
|
|
<Message
|
|
|
|
v-else
|
2018-07-19 17:44:24 +00:00
|
|
|
:network="network"
|
2018-07-13 10:43:11 +00:00
|
|
|
:message="message"
|
|
|
|
:key="message.id"
|
|
|
|
:keep-scroll-position="keepScrollPosition"
|
|
|
|
@linkPreviewToggle="onLinkPreviewToggle"/>
|
|
|
|
</template>
|
|
|
|
</div>
|
2018-07-18 18:31:51 +00:00
|
|
|
|
|
|
|
<transition name="fade">
|
|
|
|
<div
|
|
|
|
v-if="!channel.scrolledToBottom"
|
|
|
|
class="scroll-down"
|
|
|
|
@click="jumpToBottom()">
|
|
|
|
<div class="scroll-down-arrow"/>
|
|
|
|
<div
|
2018-07-18 19:38:39 +00:00
|
|
|
v-if="channel.unread > 0"
|
|
|
|
class="scroll-down-number">{{ channel.unread }}</div>
|
2018-07-18 18:31:51 +00:00
|
|
|
</div>
|
|
|
|
</transition>
|
2018-07-08 17:22:01 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2018-07-13 10:43:11 +00:00
|
|
|
require("intersection-observer");
|
2018-07-14 07:27:45 +00:00
|
|
|
|
2018-07-16 16:43:25 +00:00
|
|
|
import {throttle} from "lodash";
|
2018-07-13 10:43:11 +00:00
|
|
|
|
2018-07-09 12:14:44 +00:00
|
|
|
const constants = require("../js/constants");
|
2018-07-09 18:37:52 +00:00
|
|
|
const clipboard = require("../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-07-08 17:22:01 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "MessageList",
|
|
|
|
components: {
|
|
|
|
Message,
|
2018-07-09 12:14:44 +00:00
|
|
|
MessageCondensed,
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
|
|
|
props: {
|
2018-07-18 11:16:21 +00:00
|
|
|
settings: Object,
|
2018-07-19 17:44:24 +00:00
|
|
|
network: Object,
|
2018-07-08 17:22:01 +00:00
|
|
|
channel: Object,
|
|
|
|
},
|
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
|
|
|
|
if (this.settings.statusMessages === "hidden") {
|
|
|
|
return this.channel.messages.filter((message) => !constants.condensedTypes.includes(message.type));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If actions are not condensed, just return raw message list
|
|
|
|
if (this.settings.statusMessages !== "condensed") {
|
|
|
|
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
|
|
|
|
if (message.self || message.highlight || !constants.condensedTypes.includes(message.type)) {
|
|
|
|
lastCondensedContainer = null;
|
|
|
|
|
|
|
|
condensed.push(message);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastCondensedContainer === null) {
|
|
|
|
lastCondensedContainer = {
|
2018-07-09 13:21:14 +00:00
|
|
|
id: message.id, // Use id of first message in the condensed container
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-13 20:40:20 +00:00
|
|
|
this.channel.scrolledToBottom = true;
|
2018-07-18 18:31:51 +00:00
|
|
|
this.jumpToBottom();
|
2018-07-13 10:43:11 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
mounted() {
|
2018-07-16 16:45:27 +00:00
|
|
|
this.debouncedResize = throttle(this.handleResize, 100);
|
|
|
|
this.debouncedScroll = throttle(this.handleScroll, 100);
|
2018-07-13 19:53:30 +00:00
|
|
|
|
|
|
|
window.addEventListener("resize", this.debouncedResize, {passive: true});
|
|
|
|
this.$refs.chat.addEventListener("scroll", this.debouncedScroll, {passive: true});
|
|
|
|
|
2018-07-13 10:43:11 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.historyObserver) {
|
|
|
|
this.historyObserver.observe(this.$refs.loadMoreButton);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2018-07-13 17:39:35 +00:00
|
|
|
beforeDestroy() {
|
2018-07-13 19:53:30 +00:00
|
|
|
window.removeEventListener("resize", this.debouncedResize);
|
|
|
|
this.$refs.chat.removeEventListener("scroll", this.debouncedScroll);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-11 09:27:44 +00:00
|
|
|
return (new Date(previousMessage.time)).getDay() !== (new Date(message.time)).getDay();
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
2018-07-11 09:27:44 +00:00
|
|
|
shouldDisplayUnreadMarker(id) {
|
|
|
|
const previousMessage = this.condensedMessages[id - 1];
|
|
|
|
|
|
|
|
if (!previousMessage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.channel.firstUnread === previousMessage.id;
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
2018-07-09 18:37:52 +00:00
|
|
|
onCopy() {
|
|
|
|
clipboard(this.$el);
|
|
|
|
},
|
2018-07-11 20:45:01 +00:00
|
|
|
onLinkPreviewToggle(preview, message) {
|
|
|
|
// Tell the server we're toggling so it remembers at page reload
|
|
|
|
// TODO Avoid sending many single events when using `/collapse` or `/expand`
|
|
|
|
// See https://github.com/thelounge/thelounge/issues/1377
|
|
|
|
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() {
|
|
|
|
let lastMessage = this.channel.messages[0];
|
|
|
|
lastMessage = lastMessage ? lastMessage.id : -1;
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onLoadButtonObserved(entries) {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
if (!entry.isIntersecting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.target.click();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
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() {
|
|
|
|
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() {
|
|
|
|
const el = this.$refs.chat;
|
|
|
|
el.scrollTop = el.scrollHeight;
|
|
|
|
},
|
2018-07-08 17:22:01 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|