2018-02-13 00:56:11 -06:00
|
|
|
<template>
|
2021-04-09 22:40:50 -05:00
|
|
|
<div class="pemppnzi _block"
|
2018-02-26 15:25:17 -06:00
|
|
|
@dragover.stop="onDragover"
|
|
|
|
@drop.stop="onDrop"
|
2018-02-26 13:36:16 -06:00
|
|
|
>
|
2018-02-24 22:05:55 -06:00
|
|
|
<textarea
|
|
|
|
v-model="text"
|
2020-02-06 04:22:15 -06:00
|
|
|
ref="text"
|
2018-02-24 22:05:55 -06:00
|
|
|
@keypress="onKeypress"
|
2021-02-20 21:26:49 -06:00
|
|
|
@compositionupdate="onCompositionUpdate"
|
2018-02-24 22:05:55 -06:00
|
|
|
@paste="onPaste"
|
2020-12-25 19:47:36 -06:00
|
|
|
:placeholder="$ts.inputMessageHere"
|
2018-02-24 22:05:55 -06:00
|
|
|
></textarea>
|
2018-02-26 11:10:52 -06:00
|
|
|
<div class="file" @click="file = null" v-if="file">{{ file.name }}</div>
|
2020-12-25 19:47:36 -06:00
|
|
|
<button class="send _button" @click="send" :disabled="!canSend || sending" :title="$ts.send">
|
2020-10-17 06:12:00 -05:00
|
|
|
<template v-if="!sending"><Fa :icon="faPaperPlane"/></template><template v-if="sending"><Fa icon="spinner .spin"/></template>
|
2018-02-13 00:56:11 -06:00
|
|
|
</button>
|
2020-10-17 06:12:00 -05:00
|
|
|
<button class="_button" @click="chooseFile"><Fa :icon="faPhotoVideo"/></button>
|
|
|
|
<button class="_button" @click="insertEmoji"><Fa :icon="faLaughSquint"/></button>
|
2018-02-26 11:10:52 -06:00
|
|
|
<input ref="file" type="file" @change="onChangeFile"/>
|
2018-02-13 00:56:11 -06:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 06:12:00 -05:00
|
|
|
import { defineComponent, defineAsyncComponent } from 'vue';
|
2020-02-06 04:22:15 -06:00
|
|
|
import { faPaperPlane, faPhotoVideo, faLaughSquint } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import insertTextAtCursor from 'insert-text-at-cursor';
|
2018-02-24 11:57:19 -06:00
|
|
|
import * as autosize from 'autosize';
|
2021-03-23 03:43:07 -05:00
|
|
|
import { formatTimeString } from '@/misc/format-time-string';
|
2021-03-23 03:30:14 -05:00
|
|
|
import { selectFile } from '@client/scripts/select-file';
|
|
|
|
import * as os from '@client/os';
|
|
|
|
import { Autocomplete } from '@client/scripts/autocomplete';
|
2021-02-20 21:26:49 -06:00
|
|
|
import { throttle } from 'throttle-debounce';
|
2018-02-24 11:57:19 -06:00
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
export default defineComponent({
|
2019-05-18 06:36:33 -05:00
|
|
|
props: {
|
|
|
|
user: {
|
|
|
|
type: Object,
|
|
|
|
requird: false,
|
|
|
|
},
|
|
|
|
group: {
|
|
|
|
type: Object,
|
|
|
|
requird: false,
|
|
|
|
},
|
|
|
|
},
|
2018-02-13 00:56:11 -06:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
text: null,
|
2018-02-18 08:51:41 -06:00
|
|
|
file: null,
|
2020-01-29 13:37:25 -06:00
|
|
|
sending: false,
|
2021-02-20 21:26:49 -06:00
|
|
|
typing: throttle(3000, () => {
|
|
|
|
os.stream.send('typingOnMessaging', this.user ? { partner: this.user.id } : { group: this.group.id });
|
|
|
|
}),
|
2020-02-06 04:22:15 -06:00
|
|
|
faPaperPlane, faPhotoVideo, faLaughSquint
|
2018-02-13 00:56:11 -06:00
|
|
|
};
|
|
|
|
},
|
2018-02-25 07:56:23 -06:00
|
|
|
computed: {
|
2020-08-18 08:44:21 -05:00
|
|
|
draftKey(): string {
|
2019-05-18 06:36:33 -05:00
|
|
|
return this.user ? 'user:' + this.user.id : 'group:' + this.group.id;
|
2018-02-26 11:10:52 -06:00
|
|
|
},
|
|
|
|
canSend(): boolean {
|
|
|
|
return (this.text != null && this.text != '') || this.file != null;
|
2018-02-26 13:36:16 -06:00
|
|
|
},
|
|
|
|
room(): any {
|
|
|
|
return this.$parent;
|
2018-02-25 07:56:23 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
text() {
|
|
|
|
this.saveDraft();
|
|
|
|
},
|
|
|
|
file() {
|
|
|
|
this.saveDraft();
|
|
|
|
}
|
|
|
|
},
|
2018-02-24 11:57:19 -06:00
|
|
|
mounted() {
|
2020-02-06 04:22:15 -06:00
|
|
|
autosize(this.$refs.text);
|
2018-02-25 07:56:23 -06:00
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
// TODO: detach when unmount
|
|
|
|
new Autocomplete(this.$refs.text, this, { model: 'text' });
|
|
|
|
|
2018-02-25 07:56:23 -06:00
|
|
|
// 書きかけの投稿を復元
|
2020-08-18 08:44:21 -05:00
|
|
|
const draft = JSON.parse(localStorage.getItem('message_drafts') || '{}')[this.draftKey];
|
2018-02-25 07:56:23 -06:00
|
|
|
if (draft) {
|
|
|
|
this.text = draft.data.text;
|
|
|
|
this.file = draft.data.file;
|
|
|
|
}
|
2018-02-24 11:57:19 -06:00
|
|
|
},
|
2018-02-13 00:56:11 -06:00
|
|
|
methods: {
|
2019-07-07 23:46:31 -05:00
|
|
|
async onPaste(e: ClipboardEvent) {
|
2018-02-13 00:56:11 -06:00
|
|
|
const data = e.clipboardData;
|
|
|
|
const items = data.items;
|
2018-02-26 13:36:16 -06:00
|
|
|
|
|
|
|
if (items.length == 1) {
|
|
|
|
if (items[0].kind == 'file') {
|
2019-07-07 23:46:31 -05:00
|
|
|
const file = items[0].getAsFile();
|
|
|
|
const lio = file.name.lastIndexOf('.');
|
|
|
|
const ext = lio >= 0 ? file.name.slice(lio) : '';
|
2020-12-18 19:55:52 -06:00
|
|
|
const formatted = `${formatTimeString(new Date(file.lastModified), this.$store.state.pastedFileName).replace(/{{number}}/g, '1')}${ext}`;
|
|
|
|
const name = this.$store.state.pasteDialog
|
2020-10-17 06:12:00 -05:00
|
|
|
? await os.dialog({
|
2020-12-25 19:47:36 -06:00
|
|
|
title: this.$ts.enterFileName,
|
2019-07-07 23:46:31 -05:00
|
|
|
input: {
|
|
|
|
default: formatted
|
|
|
|
},
|
|
|
|
allowEmpty: false
|
|
|
|
}).then(({ canceled, result }) => canceled ? false : result)
|
|
|
|
: formatted;
|
|
|
|
if (name) this.upload(file, name);
|
2018-02-26 13:36:16 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (items[0].kind == 'file') {
|
2020-10-17 06:12:00 -05:00
|
|
|
os.dialog({
|
2019-04-15 23:05:10 -05:00
|
|
|
type: 'error',
|
2020-12-25 19:47:36 -06:00
|
|
|
text: this.$ts.onlyOneFileCanBeAttached
|
2019-04-15 23:05:10 -05:00
|
|
|
});
|
2018-02-26 13:36:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onDragover(e) {
|
2018-02-26 15:25:17 -06:00
|
|
|
const isFile = e.dataTransfer.items[0].kind == 'file';
|
2020-10-17 06:12:00 -05:00
|
|
|
const isDriveFile = e.dataTransfer.types[0] == _DATA_TRANSFER_DRIVE_FILE_;
|
2018-02-26 15:25:17 -06:00
|
|
|
if (isFile || isDriveFile) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
|
|
|
|
}
|
2018-02-26 13:36:16 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
onDrop(e): void {
|
|
|
|
// ファイルだったら
|
|
|
|
if (e.dataTransfer.files.length == 1) {
|
2018-02-26 15:25:17 -06:00
|
|
|
e.preventDefault();
|
2018-02-26 13:36:16 -06:00
|
|
|
this.upload(e.dataTransfer.files[0]);
|
|
|
|
return;
|
|
|
|
} else if (e.dataTransfer.files.length > 1) {
|
2018-02-26 15:25:17 -06:00
|
|
|
e.preventDefault();
|
2020-10-17 06:12:00 -05:00
|
|
|
os.dialog({
|
2019-04-15 23:05:10 -05:00
|
|
|
type: 'error',
|
2020-12-25 19:47:36 -06:00
|
|
|
text: this.$ts.onlyOneFileCanBeAttached
|
2019-04-15 23:05:10 -05:00
|
|
|
});
|
2018-02-26 13:36:16 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-26 15:25:17 -06:00
|
|
|
//#region ドライブのファイル
|
2020-10-17 06:12:00 -05:00
|
|
|
const driveFile = e.dataTransfer.getData(_DATA_TRANSFER_DRIVE_FILE_);
|
2018-02-26 15:25:17 -06:00
|
|
|
if (driveFile != null && driveFile != '') {
|
|
|
|
this.file = JSON.parse(driveFile);
|
|
|
|
e.preventDefault();
|
2018-02-13 00:56:11 -06:00
|
|
|
}
|
2018-02-26 15:25:17 -06:00
|
|
|
//#endregion
|
2018-02-13 00:56:11 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
onKeypress(e) {
|
2021-02-20 21:26:49 -06:00
|
|
|
this.typing();
|
2020-08-01 07:50:21 -05:00
|
|
|
if ((e.which == 10 || e.which == 13) && (e.ctrlKey || e.metaKey) && this.canSend) {
|
2018-02-13 00:56:11 -06:00
|
|
|
this.send();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-02-20 21:26:49 -06:00
|
|
|
onCompositionUpdate() {
|
|
|
|
this.typing();
|
|
|
|
},
|
|
|
|
|
2020-02-06 04:22:15 -06:00
|
|
|
chooseFile(e) {
|
2020-12-25 19:47:36 -06:00
|
|
|
selectFile(e.currentTarget || e.target, this.$ts.selectFile, false).then(file => {
|
2018-02-18 08:51:41 -06:00
|
|
|
this.file = file;
|
2018-02-13 00:56:11 -06:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-02-26 11:10:52 -06:00
|
|
|
onChangeFile() {
|
|
|
|
this.upload((this.$refs.file as any).files[0]);
|
|
|
|
},
|
|
|
|
|
2019-07-07 23:46:31 -05:00
|
|
|
upload(file: File, name?: string) {
|
2020-12-18 19:55:52 -06:00
|
|
|
os.upload(file, this.$store.state.uploadFolder, name).then(res => {
|
2020-10-17 06:12:00 -05:00
|
|
|
this.file = res;
|
|
|
|
});
|
2018-02-18 08:56:25 -06:00
|
|
|
},
|
2018-02-18 08:51:41 -06:00
|
|
|
|
2018-02-13 00:56:11 -06:00
|
|
|
send() {
|
|
|
|
this.sending = true;
|
2020-10-17 06:12:00 -05:00
|
|
|
os.api('messaging/messages/create', {
|
2019-05-18 06:36:33 -05:00
|
|
|
userId: this.user ? this.user.id : undefined,
|
|
|
|
groupId: this.group ? this.group.id : undefined,
|
2018-02-26 11:10:52 -06:00
|
|
|
text: this.text ? this.text : undefined,
|
2018-03-29 00:48:47 -05:00
|
|
|
fileId: this.file ? this.file.id : undefined
|
2018-02-13 00:56:11 -06:00
|
|
|
}).then(message => {
|
|
|
|
this.clear();
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
}).then(() => {
|
|
|
|
this.sending = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.text = '';
|
2018-02-18 08:56:25 -06:00
|
|
|
this.file = null;
|
2018-02-25 07:56:23 -06:00
|
|
|
this.deleteDraft();
|
|
|
|
},
|
|
|
|
|
|
|
|
saveDraft() {
|
|
|
|
const data = JSON.parse(localStorage.getItem('message_drafts') || '{}');
|
|
|
|
|
2020-08-18 08:44:21 -05:00
|
|
|
data[this.draftKey] = {
|
2018-03-29 00:48:47 -05:00
|
|
|
updatedAt: new Date(),
|
2018-02-25 07:56:23 -06:00
|
|
|
data: {
|
|
|
|
text: this.text,
|
|
|
|
file: this.file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem('message_drafts', JSON.stringify(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteDraft() {
|
|
|
|
const data = JSON.parse(localStorage.getItem('message_drafts') || '{}');
|
|
|
|
|
2020-08-18 08:44:21 -05:00
|
|
|
delete data[this.draftKey];
|
2018-02-25 07:56:23 -06:00
|
|
|
|
|
|
|
localStorage.setItem('message_drafts', JSON.stringify(data));
|
|
|
|
},
|
2020-02-06 04:22:15 -06:00
|
|
|
|
|
|
|
async insertEmoji(ev) {
|
2021-02-26 22:08:34 -06:00
|
|
|
os.openEmojiPicker(ev.currentTarget || ev.target, {}, this.$refs.text);
|
2020-02-06 04:22:15 -06:00
|
|
|
}
|
2018-02-13 00:56:11 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2020-01-29 13:37:25 -06:00
|
|
|
<style lang="scss" scoped>
|
2021-04-09 22:40:50 -05:00
|
|
|
.pemppnzi {
|
2020-01-29 13:37:25 -06:00
|
|
|
position: relative;
|
|
|
|
|
|
|
|
> textarea {
|
|
|
|
cursor: auto;
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
min-width: 100%;
|
|
|
|
max-width: 100%;
|
|
|
|
height: 80px;
|
|
|
|
margin: 0;
|
|
|
|
padding: 16px 16px 0 16px;
|
|
|
|
resize: none;
|
|
|
|
font-size: 1em;
|
2020-02-09 04:59:28 -06:00
|
|
|
font-family: inherit;
|
2020-01-29 13:37:25 -06:00
|
|
|
outline: none;
|
|
|
|
border: none;
|
|
|
|
border-radius: 0;
|
|
|
|
box-shadow: none;
|
|
|
|
background: transparent;
|
|
|
|
box-sizing: border-box;
|
|
|
|
color: var(--fg);
|
|
|
|
}
|
|
|
|
|
|
|
|
> .file {
|
|
|
|
padding: 8px;
|
|
|
|
color: #444;
|
|
|
|
background: #eee;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
2018-02-13 00:56:11 -06:00
|
|
|
|
2020-01-29 13:37:25 -06:00
|
|
|
> .send {
|
|
|
|
position: absolute;
|
|
|
|
bottom: 0;
|
|
|
|
right: 0;
|
|
|
|
margin: 0;
|
|
|
|
padding: 16px;
|
|
|
|
font-size: 1em;
|
|
|
|
transition: color 0.1s ease;
|
2020-02-10 06:30:59 -06:00
|
|
|
color: var(--accent);
|
2020-01-29 13:37:25 -06:00
|
|
|
|
|
|
|
&:active {
|
|
|
|
color: var(--accentDarken);
|
|
|
|
transition: color 0s ease;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.files {
|
|
|
|
display: block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0 8px;
|
|
|
|
list-style: none;
|
|
|
|
|
|
|
|
&:after {
|
|
|
|
content: '';
|
|
|
|
display: block;
|
|
|
|
clear: both;
|
|
|
|
}
|
|
|
|
|
|
|
|
> li {
|
|
|
|
display: block;
|
|
|
|
float: left;
|
|
|
|
margin: 4px;
|
|
|
|
padding: 0;
|
|
|
|
width: 64px;
|
|
|
|
height: 64px;
|
|
|
|
background-color: #eee;
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
background-position: center center;
|
|
|
|
background-size: cover;
|
|
|
|
cursor: move;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
> .remove {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .remove {
|
|
|
|
display: none;
|
|
|
|
position: absolute;
|
|
|
|
right: -6px;
|
|
|
|
top: -6px;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
background: transparent;
|
|
|
|
outline: none;
|
|
|
|
border: none;
|
|
|
|
border-radius: 0;
|
|
|
|
box-shadow: none;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 04:22:15 -06:00
|
|
|
._button {
|
2020-01-29 13:37:25 -06:00
|
|
|
margin: 0;
|
|
|
|
padding: 16px;
|
|
|
|
font-size: 1em;
|
|
|
|
font-weight: normal;
|
|
|
|
text-decoration: none;
|
|
|
|
transition: color 0.1s ease;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
color: var(--accent);
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
|
|
|
color: var(--accentDarken);
|
|
|
|
transition: color 0s ease;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input[type=file] {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 00:56:11 -06:00
|
|
|
</style>
|