keanu-weblite/src/components/Chat.vue

527 lines
15 KiB
Vue
Raw Normal View History

2020-11-09 10:26:56 +01:00
<template>
<div class="chat-root fill-height d-flex flex-column" ma-0 pa-0>
<div
class="chat-content flex-grow-1 flex-shrink-1"
ref="chatContainer"
style="overflow-x: hidden; overflow-y: auto"
v-on:scroll="onScroll"
2020-11-09 10:26:56 +01:00
>
2020-11-19 17:08:58 +01:00
<div v-for="event in events" :key="event.getId()">
<div v-event:tap.self="(e) => { touchTap(event); } " v-event:longTap="(e) => { touchLongTap(event); } " v-if="!event.isRelation() && !event.isRedacted() && !event.isRedaction()">
<div style="position:relative;user-select:none">
2020-11-25 14:42:50 +01:00
<component
:is="componentForEvent(event)"
:room="room"
:event="event"
2020-11-25 14:42:50 +01:00
:reactions="
timelineWindow._timelineSet.getRelationsForEvent(
event.getId(),
'm.annotation',
'm.reaction'
)
"
v-on:send-quick-reaction="sendQuickReaction"
/>
<message-operations
v-if="selectedEvent == event && showContextMenu"
2020-11-25 14:42:50 +01:00
v-on:addreaction="addReaction"
:event="event"
:incoming="event.getSender() != $matrix.currentUserId"
/>
</div>
</div>
2020-11-09 10:26:56 +01:00
</div>
</div>
<!-- Input area -->
2020-11-17 20:02:42 +01:00
<div v-if="room" class="input-area flex-grow-0 flex-shrink-0">
<!-- CONTACT IS TYPING -->
<div v-show="contactIsTyping" class="typing">Someone is typing...</div>
2020-11-09 10:26:56 +01:00
<v-textarea
ref="messageInput"
full-width
v-model="currentInput"
no-resize
class="input-message"
placeholder="Send message"
hide-details
2020-11-17 20:02:42 +01:00
background-color="white"
>
<template v-slot:prepend>
<label icon flat>
<v-icon>attachment</v-icon>
<input
ref="attachment"
type="file"
name="attachment"
@change="pickAttachment($event)"
accept="image/*|audio/*|video/*|application/pdf"
2020-11-17 20:02:42 +01:00
style="display: none"
/>
</label>
</template>
</v-textarea>
2020-11-09 10:26:56 +01:00
<div align-self="end" class="text-right">
<v-btn
elevation="0"
@click.stop="sendMessage"
:disabled="sendButtonDisabled"
>Send</v-btn
>
</div>
</div>
2020-11-17 20:02:42 +01:00
<div v-if="currentImageInput">
<v-dialog v-model="currentImageInput" class="ma-0 pa-0" width="50%">
<v-card class="ma-0 pa-0">
<v-card-text class="ma-0 pa-0">
<v-img
:aspect-ratio="1"
:src="currentImageInput"
contain
style="max-height: 50vh"
/>
<div v-if="currentSendError">{{ currentSendError }}</div>
<div v-else>{{ currentSendProgress }}</div>
</v-card-text>
<v-divider></v-divider>
<v-card-actions>
<v-spacer></v-spacer>
2020-11-21 14:57:43 +01:00
<v-btn color="primary" text @click="cancelSendAttachment"
2020-11-17 20:02:42 +01:00
>Cancel</v-btn
>
2020-11-25 14:42:50 +01:00
<v-btn
color="primary"
text
@click="sendAttachment"
:disabled="currentSendOperation != null"
>Send</v-btn
>
2020-11-17 20:02:42 +01:00
</v-card-actions>
</v-card>
</v-dialog>
</div>
2020-11-25 14:42:50 +01:00
<div>
<v-dialog v-model="showEmojiPicker" class="ma-0 pa-0" width="50%">
<VEmojiPicker style="width: 100%" @select="emojiSelected" />
</v-dialog>
</div>
2020-11-09 10:26:56 +01:00
</div>
</template>
<script>
import { TimelineWindow, EventTimeline } from "matrix-js-sdk";
2020-11-19 22:48:08 +01:00
import MessageIncomingText from "./messages/MessageIncomingText";
import MessageIncomingImage from "./messages/MessageIncomingImage.vue";
import MessageIncomingAudio from "./messages/MessageIncomingAudio.vue";
2020-11-19 22:48:08 +01:00
import MessageOutgoingText from "./messages/MessageOutgoingText";
import MessageOutgoingImage from "./messages/MessageOutgoingImage.vue";
import MessageOutgoingAudio from "./messages/MessageOutgoingAudio.vue";
2020-11-19 22:48:08 +01:00
import ContactJoin from "./messages/ContactJoin.vue";
import ContactLeave from "./messages/ContactLeave.vue";
import ContactInvited from "./messages/ContactInvited.vue";
import RoomNameChanged from "./messages/RoomNameChanged.vue";
import RoomTopicChanged from "./messages/RoomTopicChanged.vue";
import RoomAvatarChanged from "./messages/RoomAvatarChanged.vue";
import DebugEvent from "./messages/DebugEvent.vue";
2020-11-21 14:57:43 +01:00
import util from "../plugins/utils";
2020-11-25 14:42:50 +01:00
import MessageOperations from "./messages/MessageOperations.vue";
// from https://kirbysayshi.com/2013/08/19/maintaining-scroll-position-knockoutjs-list.html
function ScrollPosition(node) {
this.node = node;
this.previousScrollHeightMinusTop = 0;
2020-11-17 20:02:42 +01:00
this.previousScrollTop = 0;
this.readyFor = "up";
}
ScrollPosition.prototype.restore = function () {
if (this.readyFor === "up") {
this.node.scrollTop =
this.node.scrollHeight - this.previousScrollHeightMinusTop;
2020-11-17 20:02:42 +01:00
} else {
this.node.scrollTop = this.previousScrollTop;
}
};
ScrollPosition.prototype.prepareFor = function (direction) {
this.readyFor = direction || "up";
2020-11-17 20:02:42 +01:00
if (this.readyFor === "up") {
this.previousScrollHeightMinusTop =
this.node.scrollHeight - this.node.scrollTop;
} else {
this.previousScrollTop = this.node.scrollTop;
}
};
2020-11-09 10:26:56 +01:00
export default {
name: "Chat",
components: {
MessageIncomingText,
MessageIncomingImage,
MessageIncomingAudio,
MessageOutgoingText,
MessageOutgoingImage,
MessageOutgoingAudio,
ContactJoin,
ContactLeave,
ContactInvited,
RoomNameChanged,
RoomTopicChanged,
RoomAvatarChanged,
DebugEvent,
2020-11-25 14:42:50 +01:00
MessageOperations,
},
2020-11-19 22:48:08 +01:00
data() {
return {
room: null,
events: [],
currentInput: "",
contactIsTyping: false,
timelineWindow: null,
scrollPosition: null,
currentImageInput: null,
currentImageInputPath: null,
currentSendOperation: null,
currentSendProgress: null,
currentSendError: null,
2020-11-25 14:42:50 +01:00
showEmojiPicker: false,
selectedEvent: null,
showContextMenu: false
2020-11-19 22:48:08 +01:00
};
},
2020-11-09 10:26:56 +01:00
mounted() {
const container = this.$refs.chatContainer;
this.scrollPosition = new ScrollPosition(container);
this.$matrix.on("Room.timeline", this.onEvent);
this.$matrix.on("RoomMember.typing", this.onUserTyping);
},
destroyed() {
this.$matrix.off("Room.timeline", this.onEvent);
this.$matrix.off("RoomMember.typing", this.onUserTyping);
},
2020-11-09 10:26:56 +01:00
computed: {
roomId() {
return this.$matrix.currentRoomId;
2020-11-09 10:26:56 +01:00
},
sendButtonDisabled() {
return this.currentInput.length == 0;
},
},
watch: {
2020-11-25 14:42:50 +01:00
roomId: {
handler(ignoredNewVal, ignoredOldVal) {
console.log("Chat: Current room changed");
2020-11-09 10:26:56 +01:00
2020-11-25 14:42:50 +01:00
// Clear old events
this.events = [];
this.timelineWindow = null;
this.contactIsTyping = false;
2020-11-09 10:26:56 +01:00
2020-11-25 14:42:50 +01:00
if (!this.roomId) {
return; // no room
}
2020-11-25 14:42:50 +01:00
this.room = this.$matrix.getRoom(this.roomId);
if (!this.room) {
return; // Not found
}
2020-11-09 10:26:56 +01:00
2020-11-25 14:42:50 +01:00
this.timelineWindow = new TimelineWindow(
this.$matrix.matrixClient,
this.room.getUnfilteredTimelineSet(),
{}
);
this.timelineWindow.load(null, 20).then(() => {
this.events = this.timelineWindow.getEvents();
this.$nextTick(() => {
this.paginateBackIfNeeded();
});
2020-11-21 14:57:43 +01:00
});
2020-11-25 14:42:50 +01:00
},
immediate: true,
2020-11-09 10:26:56 +01:00
},
},
methods: {
touchTap(ignoredEvent) {
if (this.selectedEvent && this.showContextMenu) {
// If anything is selected, unselect
this.selectedEvent = null;
this.showContextMenu = false;
}
},
touchLongTap(event) {
this.selectedEvent = event;
this.showContextMenu = true;
},
componentForEvent(event) {
switch (event.getType()) {
2020-11-19 22:48:08 +01:00
case "m.room.member":
2020-11-25 14:42:50 +01:00
if (event.getContent().membership == "join") {
return ContactJoin;
} else if (event.getContent().membership == "leave") {
return ContactLeave;
} else if (event.getContent().membership == "invite") {
return ContactInvited;
}
2020-11-25 14:42:50 +01:00
break;
2020-11-19 22:48:08 +01:00
case "m.room.message":
2020-11-25 14:42:50 +01:00
if (event.getSender() != this.$matrix.currentUserId) {
2020-11-19 22:48:08 +01:00
if (event.getContent().msgtype == "m.image") {
return MessageIncomingImage;
} else if (event.getContent().msgtype == "m.audio") {
return MessageIncomingAudio;
}
return MessageIncomingText;
} else {
2020-11-19 22:48:08 +01:00
if (event.getContent().msgtype == "m.image") {
return MessageOutgoingImage;
} else if (event.getContent().msgtype == "m.audio") {
return MessageOutgoingAudio;
}
return MessageOutgoingText;
}
2020-11-19 22:48:08 +01:00
case "m.room.name":
return RoomNameChanged;
2020-11-19 22:48:08 +01:00
case "m.room.topic":
return RoomTopicChanged;
2020-11-19 22:48:08 +01:00
case "m.room.avatar":
return RoomAvatarChanged;
}
return DebugEvent;
},
paginateBackIfNeeded() {
2020-11-17 20:02:42 +01:00
this.$nextTick(() => {
const container = this.$refs.chatContainer;
if (container.scrollHeight <= container.clientHeight) {
this.handleScrolledToTop();
}
});
},
onScroll(ignoredevent) {
const container = this.$refs.chatContainer;
if (container.scrollTop == 0) {
// Scrolled to top
this.handleScrolledToTop();
} else if (
container.scrollHeight - container.scrollTop ==
container.clientHeight
) {
2020-11-17 20:02:42 +01:00
this.handleScrolledToBottom(false);
}
},
onEvent(event) {
if (event.getRoomId() !== this.roomId) {
return; // Not for this room
}
this.paginateBackIfNeeded();
2020-11-17 20:02:42 +01:00
// If we are at bottom, scroll to see new events...
const container = this.$refs.chatContainer;
if (
container.scrollHeight - container.scrollTop ==
container.clientHeight
) {
this.handleScrolledToBottom(true);
}
},
onUserTyping(event) {
if (event.getRoomId() !== this.roomId) {
return; // Not for this room
2020-11-09 10:26:56 +01:00
}
console.log("Typing:", event);
2020-11-09 10:26:56 +01:00
},
sendMessage() {
if (this.currentInput.length > 0) {
2020-11-25 14:42:50 +01:00
util
.sendTextMessage(
this.$matrix.matrixClient,
this.roomId,
this.currentInput
)
.then(() => {
console.log("Sent message");
})
.catch((err) => {
console.log("Failed to send:", err);
});
2020-11-09 10:26:56 +01:00
this.currentInput = "";
}
},
2020-11-17 20:02:42 +01:00
/**
* Show attachment picker to select image
*/
pickAttachment(event) {
if (event.target.files && event.target.files[0]) {
var reader = new FileReader();
reader.onload = (e) => {
this.currentImageInput = e.target.result;
this.currentImageInputPath = event.target.files[0];
};
reader.readAsDataURL(event.target.files[0]);
}
},
onUploadProgress(p) {
if (p.total) {
this.currentSendProgress =
"Uploaded " + (p.loaded || 0) + " of " + p.total;
} else {
this.currentSendProgress = "Uploaded " + (p.loaded || 0);
}
},
sendAttachment() {
if (this.currentImageInputPath) {
2020-11-21 14:57:43 +01:00
this.currentSendProgress = 0;
2020-11-25 14:42:50 +01:00
this.currentSendOperation = util.sendImage(
this.$matrix.matrixClient,
this.roomId,
this.currentImageInputPath,
this.onUploadProgress
);
2020-11-17 20:02:42 +01:00
this.currentSendOperation
2020-11-25 14:42:50 +01:00
.then(() => {
this.currentSendOperation = null;
this.currentImageInput = null;
this.currentSendProgress = 0;
})
.catch((err) => {
this.currentSendError = err.toLocaleString();
this.currentSendOperation = null;
this.currentSendProgress = 0;
});
2020-11-17 20:02:42 +01:00
}
},
2020-11-21 14:57:43 +01:00
cancelSendAttachment() {
if (this.currentSendOperation) {
this.currentSendOperation.reject("Canceled");
}
this.currentSendOperation = null;
2020-11-21 14:57:43 +01:00
this.currentImageInput = null;
this.currentSendProgress = 0;
this.currentSendError = null;
2020-11-09 10:26:56 +01:00
},
handleScrolledToTop() {
console.log("@top");
if (
this.timelineWindow &&
this.timelineWindow.canPaginate(EventTimeline.BACKWARDS)
) {
this.timelineWindow
.paginate(EventTimeline.BACKWARDS, 10, true)
.then((success) => {
if (success) {
this.scrollPosition.prepareFor("up");
this.events = this.timelineWindow.getEvents();
this.$nextTick(() => {
// restore scroll position!
console.log("Restore scroll!");
this.scrollPosition.restore();
});
}
});
}
},
2020-11-17 20:02:42 +01:00
handleScrolledToBottom(scrollToEnd) {
console.log("@bottom");
2020-11-17 20:02:42 +01:00
if (
this.timelineWindow &&
this.timelineWindow.canPaginate(EventTimeline.FORWARDS)
) {
this.timelineWindow
.paginate(EventTimeline.FORWARDS, 10, true)
.then((success) => {
if (success) {
this.scrollPosition.prepareFor("down");
this.events = this.timelineWindow.getEvents();
this.$nextTick(() => {
// restore scroll position!
console.log("Restore scroll!");
this.scrollPosition.restore();
if (scrollToEnd) {
this.smoothScrollToEnd();
}
});
}
});
}
},
smoothScrollToEnd() {
this.$nextTick(function () {
const container = this.$refs.chatContainer;
if (container.children.length > 0) {
const lastChild = container.children[container.children.length - 1];
console.log("Scroll into view", lastChild);
window.requestAnimationFrame(() => {
lastChild.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest",
});
});
}
});
},
2020-11-25 14:42:50 +01:00
addReaction(e) {
const event = e.event;
// Store the event we are reacting to, so that we know where to
// send when the picker closes.
this.selectedEvent = event;
this.showEmojiPicker = true;
},
emojiSelected(e) {
this.showEmojiPicker = false;
if (this.selectedEvent) {
const event = this.selectedEvent;
this.selectedEvent = null;
this.sendQuickReaction({reaction:e.data, event: event});
}
},
sendQuickReaction(e) {
util
.sendQuickReaction(
this.$matrix.matrixClient,
this.roomId,
e.reaction,
e.event
)
.then(() => {
console.log("Quick reaction message");
})
.catch((err) => {
console.log("Failed to send quick reaction:", err);
});
}
2020-11-09 10:26:56 +01:00
},
};
</script>
<style lang="scss">
@import "@/assets/css/chat.scss";
</style>