send attachments and recordings

dev
adb 4 years ago
parent cce7af2178
commit 64d6273a52

@ -2,10 +2,10 @@
<div v-if="content.msgtype==='m.text'" v-html="parseMessage(content.body)"/> <div v-if="content.msgtype==='m.text'" v-html="parseMessage(content.body)"/>
<div v-else-if="content.msgtype==='m.notice'" class="notice" v-html="parseMessage(content.body)"/> <div v-else-if="content.msgtype==='m.notice'" class="notice" v-html="parseMessage(content.body)"/>
<div v-else-if="content.msgtype==='m.image'" class="image"> <div v-else-if="content.msgtype==='m.image'" class="image">
<img :src="getSource(content.url)" :alt="content.body"/><br> <img :src="getSource(content.url)" :alt="content.body" :class="`${compact?'compact':''}`"/><br>
{{content.body}} {{content.body}}
</div> </div>
<div v-else-if="content.msgtype==='m.file'" class="file"> <div v-else-if="content.msgtype==='m.file'" :class="`file ${compact?'compact':''}`">
<icon <icon
title="file" title="file"
ic="./sym/ic_attach_file_white.svg" ic="./sym/ic_attach_file_white.svg"
@ -16,15 +16,15 @@
</a><br>{{content.body}} </a><br>{{content.body}}
</div> </div>
</div> </div>
<div v-else-if="content.msgtype==='m.audio'" class="audio"> <div v-else-if="content.msgtype==='m.audio'" :class="`audio ${compact?'compact':''}`">
<audio controls> <audio controls :class="`${compact?'compact':''}`">
<source :src="getSource(content.url)" :type="content.mimetype"> <source :src="getSource(content.url)" :type="content.mimetype">
your browser doesn't support audio your browser doesn't support audio
</audio><br> </audio><br>
{{content.body}} {{content.body}}
</div> </div>
<div v-else-if="content.msgtype==='m.video'" class="video"> <div v-else-if="content.msgtype==='m.video'" :class="`video ${compact?'compact':''}`">
<video controls> <video controls :class="`${compact?'compact':''}`">
<source :src="getSource(content.url)" :type="content.mimetype"> <source :src="getSource(content.url)" :type="content.mimetype">
your browser doesn't support video your browser doesn't support video
</video><br> </video><br>
@ -42,7 +42,11 @@ export default {
name: 'eventContent', name: 'eventContent',
components: {Icon}, components: {Icon},
props: { props: {
content: Object content: Object,
compact: {
type: Boolean,
default: false
}
}, },
methods: { methods: {
getSource(url){ getSource(url){
@ -70,6 +74,10 @@ export default {
max-height: 35rem; max-height: 35rem;
border-radius: 0.5rem; border-radius: 0.5rem;
} }
.compact{
max-width: 8rem;
max-height: 8rem;
}
} }
.video{ .video{
width: 100%; width: 100%;
@ -79,11 +87,19 @@ export default {
max-height: 35rem; max-height: 35rem;
border-radius: 0.5rem; border-radius: 0.5rem;
} }
.compact{
max-width: 8rem;
max-height: 8rem;
}
} }
.audio{ .audio{
audio{ audio{
max-width: 100%; max-width: 100%;
} }
.compact{
max-width: 16rem;
max-height: 8rem;
}
} }
.italic{ .italic{
font-style: italic; font-style: italic;

@ -1,43 +1,50 @@
<template> <template>
<div class="newMessageBanner" ref="newMessageBanner"> <div class="newMessageBanner" ref="newMessageBanner">
<reply-event v-if="replyTo" :event="replyTo" @click.native="resetReplyTo()"/> <reply-event v-if="replyTo" :event="replyTo" @click.native="resetReplyTo()"/>
<event-content v-if="attachment" :content="attachment" class="attachment"/> <div v-if="attachment" class="attachment">
<form v-on:submit.prevent="sendMessage()"> <event-content :content="attachment" class="attachmentContent" :compact="true"/>
<textarea <icon
@keyup.enter.exact="sendMessage()" title="remove"
@input="resizeMessageBanner(); sendTyping(2000);" class="remove"
v-model="event.content.body" ic="./sym/ic_close_white.svg"
ref="newMessageInput" class="newMessageInput" @click.native="resetAttachment()"
rows="1" placeholder="type a message ..."
/> />
</div>
<textarea
@keyup.enter.exact="onSubmit(event)"
@input="resizeMessageBanner(); sendTyping(2000);"
v-model="event.content.body"
ref="newMessageInput" class="newMessageInput"
rows="1" placeholder="type a message ..."
/>
<icon
v-if="event.content.body && !isRecording || attachment"
type="submit"
title="press enter to submit"
class="sendMessageBtn"
ic="./sym/ic_send_white.svg"
@click.native="onSubmit(event)"
/>
<div v-else class="recorder">
<icon <icon
v-if="event.content.body && !isRecording" v-if="!isRecording"
type="submit" title="record voice"
title="press enter to submit" class="recordBtn start"
class="sendMessageBtn" ic="./sym/ic_mic_white.svg"
ic="./sym/ic_send_white.svg" @click.native="startRecording()"
ref="startRecord"
/> />
<div v-else class="recorder"> <div v-else class="voiceMeterContainer">
<div class="voiceMeter" ref="voiceMeter"></div>
<icon <icon
v-if="!isRecording"
title="record voice" title="record voice"
class="recordBtn start" class="recordBtn stop"
ic="./sym/ic_mic_white.svg" ic="./sym/ic_mic_white.svg"
@click.native="startRecording()" @click.native="stopRecording()"
ref="startRecord" ref="stopRecord"
/> />
<div v-else class="voiceMeterContainer">
<div class="voiceMeter" ref="voiceMeter"></div>
<icon
title="record voice"
class="recordBtn stop"
ic="./sym/ic_mic_white.svg"
@click.native="stopRecording()"
ref="stopRecord"
/>
</div>
</div> </div>
</form> </div>
<div class="mediaButtons"> <div class="mediaButtons">
<icon <icon
title="toggle emoji" title="toggle emoji"
@ -53,7 +60,7 @@
/> />
<input <input
type="file" id="fileInput" ref="fileInput" type="file" id="fileInput" ref="fileInput"
@change="setAttachment($refs.fileInput.files[0])" @change="setAttachment({file: $refs.fileInput.files[0]})"
> >
</div> </div>
<v-emoji-picker <v-emoji-picker
@ -92,16 +99,27 @@ export default {
resetReplyTo: Function resetReplyTo: Function
}, },
methods: { methods: {
sendMessage(){ onSubmit(event){
let content = this.event.content; console.log(event)
if (!content.body.trim()) return; event.content.msgtype==='m.text'?this.sendEvent(event):this.sendMediaEvent(event);
matrix.sendEvent(this.event, this.roomId, this.replyTo); },
content.body = ''; async sendEvent(event){
if (!event.content.body.trim()) return;
await matrix.sendEvent(this.event, this.roomId, this.replyTo);
//await matrix.sendEvent(new Proxy(this.event, this.eventProxy), this.roomId, this.replyTo);
event.content.body = '';
this.resetAttachment();
this.resetReplyTo(); this.resetReplyTo();
let id = this.$refs.newMessageInput; let id = this.$refs.newMessageInput;
id.style.height = '1.25rem'; id.style.height = '1.25rem';
this.onResize(id.parentElement.clientHeight); this.onResize(id.parentElement.clientHeight);
}, },
sendMediaEvent(event){
matrix.client.uploadContent(this.attachment.blob).then(mxc => {
event.content.url = mxc;
this.sendEvent(event);
});
},
sendTyping(timeout){ sendTyping(timeout){
if (this.waitForSendTyping) return; if (this.waitForSendTyping) return;
matrix.client.sendTyping(this.roomId, true, timeout+100); matrix.client.sendTyping(this.roomId, true, timeout+100);
@ -133,8 +151,9 @@ export default {
stopRecording(){ stopRecording(){
this.recorder.stop() this.recorder.stop()
.then(({blob}) => { .then(({blob}) => {
this.recBlob = blob;
this.isRecording=false; this.isRecording=false;
blob.name = `Recording-${new Date().toISOString()}.${blob.type.split('/')[1]}`;
this.setAttachment({blob});
}); });
}, },
setVoiceMeter(value){ setVoiceMeter(value){
@ -142,18 +161,38 @@ export default {
this.$refs.voiceMeter.style.height = `calc(3rem + ${value/4}px`; this.$refs.voiceMeter.style.height = `calc(3rem + ${value/4}px`;
this.$refs.voiceMeter.style.width = `calc(3rem + ${value/4}px`; this.$refs.voiceMeter.style.width = `calc(3rem + ${value/4}px`;
}, },
setAttachment(file){ async readFile(file){
let reader = new FileReader(); return await new Promise(resolve => {
reader.onerror = console.error; let reader = new FileReader();
reader.onload = event => { reader.onerror = console.error;
this.attachment = { reader.onload = async event => {
msgtype: this.getMsgType(file.type), resolve(await (await fetch(event.target.result)).blob());
mimetype: file.type, }
url: event.target.result reader.readAsDataURL(file);
}; });
this.event.content.body = file.name; },
} async setAttachment({blob, file = blob}){
reader.readAsDataURL(file); this.attachment = {
msgtype: this.getMsgType(file.type),
mimetype: file.type,
url: window.URL.createObjectURL(file),
blob: blob || await this.readFile(file),
file
};
this.event.content = {
body: file.name,
msgtype: this.attachment.msgtype,
mimetype: this.attachment.mimetype
};
},
resetAttachment(){
if (!this.attachment) return;
window.URL.revokeObjectURL(this.attachment.file);
this.event.content = {
body: this.attachment?this.event.content.body.replace(this.attachment.file.name, ''):'',
msgtype: 'm.text'
};
this.attachment = undefined;
}, },
getMsgType(fileType){ getMsgType(fileType){
return { return {
@ -173,9 +212,7 @@ export default {
body: '', body: '',
msgtype: 'm.text', msgtype: 'm.text',
'm.relates_to': { 'm.relates_to': {
'm.in_reply_to': { 'm.in_reply_to': this.replyTo
event_id: undefined
}
} }
} }
}, },
@ -186,7 +223,14 @@ export default {
}), }),
isRecording: false, isRecording: false,
recBlob: undefined, recBlob: undefined,
attachment: undefined attachment: undefined,
eventProxy: {
set: ()=>{},
get: (target, key) => {
if (typeof target[key] === 'object') return new Proxy(target[key], this.eventProxy);
return target[key];
}
}
} }
}, },
updated() { updated() {
@ -195,7 +239,7 @@ export default {
} }
</script> </script>
<style scoped> <style scoped lang="scss">
.newMessageBanner{ .newMessageBanner{
position: absolute; position: absolute;
bottom: 0; bottom: 0;
@ -269,6 +313,8 @@ export default {
width: 2.5rem; width: 2.5rem;
box-shadow: none; box-shadow: none;
} }
.emojiToggle{
}
.recordBtn{ .recordBtn{
position: absolute; position: absolute;
right: 1rem; right: 1rem;
@ -311,7 +357,23 @@ export default {
display: none; display: none;
} }
.attachment{ .attachment{
max-width: 5rem; top: 0.5rem;
left: 0.5rem;
position: relative;
width: fit-content;
height: fit-content;
.attachmentContent{
position: relative;
width: fit-content;
}
.remove{
position: absolute;
top: 0;
right: -3rem;
background-color: unset;
height: 2.5rem;
width: 2.5rem;
}
} }
img{ img{
max-width: 10rem; max-width: 10rem;

@ -1,5 +1,5 @@
import matrix from 'matrix-js-sdk'; import matrix from 'matrix-js-sdk';
import {NotificationHandler} from "@/lib/NotificationHandler"; import {NotificationHandler} from '@/lib/NotificationHandler';
export class MatrixHandler { export class MatrixHandler {
constructor(clientDisplayName = 'matrix-chat') { constructor(clientDisplayName = 'matrix-chat') {
@ -78,16 +78,21 @@ export class MatrixHandler {
}); });
} }
async sendEvent({content, type}, roomId, replyTo = undefined){ async sendEvent({content, type}, roomId, replyTo = undefined){
console.log(replyTo, content)
await this.client.sendEvent(roomId, type, { await this.client.sendEvent(roomId, type, {
body: content.body.trim(), body: content.body.trim(),
msgtype: content.msgtype, msgtype: content.msgtype,
mimetype: content.mimetype,
url: content.url,
'm.relates_to': { 'm.relates_to': {
'm.in_reply_to': { 'm.in_reply_to': {
event_id: replyTo?replyTo.event_id:undefined event_id: replyTo?replyTo.event_id:undefined
} },
} },
}).then(() => console.log('message sent successfully')) }).then(() => console.log('message sent successfully'))
.catch((err) => console.log(`error while sending message => ${err}`) .catch((err) => console.log(`error while sending message => ${err}`));
); /*return await this.client.sendEvent(roomId, type, content)
.then(() => console.log('message sent successfully'))
.catch((err) => console.log(`error while sending message => ${err}`));*/
} }
} }
Loading…
Cancel
Save