2018-02-13 07:56:11 +01:00
|
|
|
<template>
|
2022-06-20 06:20:28 +02:00
|
|
|
<div
|
|
|
|
class="pemppnzi _block"
|
2018-02-26 22:25:17 +01:00
|
|
|
@dragover.stop="onDragover"
|
|
|
|
@drop.stop="onDrop"
|
2018-02-26 20:36:16 +01:00
|
|
|
>
|
2018-02-25 05:05:55 +01:00
|
|
|
<textarea
|
2022-06-20 06:20:28 +02:00
|
|
|
ref="textEl"
|
2021-11-19 11:36:12 +01:00
|
|
|
v-model="text"
|
2022-06-20 06:20:28 +02:00
|
|
|
:placeholder="i18n.ts.inputMessageHere"
|
2022-01-25 10:01:29 +01:00
|
|
|
@keydown="onKeydown"
|
2021-02-21 04:26:49 +01:00
|
|
|
@compositionupdate="onCompositionUpdate"
|
2018-02-25 05:05:55 +01:00
|
|
|
@paste="onPaste"
|
|
|
|
></textarea>
|
2022-06-20 06:20:28 +02:00
|
|
|
<footer>
|
|
|
|
<div v-if="file" class="file" @click="file = null">{{ file.name }}</div>
|
|
|
|
<div class="buttons">
|
|
|
|
<button class="_button" @click="chooseFile"><i class="fas fa-photo-video"></i></button>
|
|
|
|
<button class="_button" @click="insertEmoji"><i class="fas fa-laugh-squint"></i></button>
|
|
|
|
<button class="send _button" :disabled="!canSend || sending" :title="i18n.ts.send" @click="send">
|
|
|
|
<template v-if="!sending"><i class="fas fa-paper-plane"></i></template><template v-if="sending"><i class="fas fa-spinner fa-pulse fa-fw"></i></template>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</footer>
|
|
|
|
<input ref="fileEl" type="file" @change="onChangeFile"/>
|
2018-02-13 07:56:11 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, watch } from 'vue';
|
|
|
|
import * as Misskey from 'misskey-js';
|
2022-01-27 09:55:11 +01:00
|
|
|
import autosize from 'autosize';
|
2022-06-20 06:20:28 +02:00
|
|
|
//import insertTextAtCursor from 'insert-text-at-cursor';
|
|
|
|
import { throttle } from 'throttle-debounce';
|
2022-07-07 03:09:26 +02:00
|
|
|
import { Autocomplete } from '@/scripts/autocomplete';
|
2021-11-11 18:02:25 +01:00
|
|
|
import { formatTimeString } from '@/scripts/format-time-string';
|
|
|
|
import { selectFile } from '@/scripts/select-file';
|
|
|
|
import * as os from '@/os';
|
2021-12-29 14:13:09 +01:00
|
|
|
import { stream } from '@/stream';
|
2022-06-20 06:20:28 +02:00
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { i18n } from '@/i18n';
|
2022-04-28 04:14:03 +02:00
|
|
|
import { uploadFile } from '@/scripts/upload';
|
2018-02-24 18:57:19 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
const props = defineProps<{
|
|
|
|
user?: Misskey.entities.UserDetailed | null;
|
|
|
|
group?: Misskey.entities.UserGroup | null;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
let textEl = $ref<HTMLTextAreaElement>();
|
|
|
|
let fileEl = $ref<HTMLInputElement>();
|
|
|
|
|
|
|
|
let text = $ref<string>('');
|
|
|
|
let file = $ref<Misskey.entities.DriveFile | null>(null);
|
|
|
|
let sending = $ref(false);
|
|
|
|
const typing = throttle(3000, () => {
|
|
|
|
stream.send('typingOnMessaging', props.user ? { partner: props.user.id } : { group: props.group?.id });
|
|
|
|
});
|
|
|
|
|
|
|
|
let draftKey = $computed(() => props.user ? 'user:' + props.user.id : 'group:' + props.group?.id);
|
|
|
|
let canSend = $computed(() => (text != null && text !== '') || file != null);
|
|
|
|
|
|
|
|
watch([$$(text), $$(file)], saveDraft);
|
|
|
|
|
|
|
|
async function onPaste(ev: ClipboardEvent) {
|
|
|
|
if (!ev.clipboardData) return;
|
|
|
|
|
|
|
|
const clipboardData = ev.clipboardData;
|
|
|
|
const items = clipboardData.items;
|
|
|
|
|
|
|
|
if (items.length === 1) {
|
|
|
|
if (items[0].kind === 'file') {
|
|
|
|
const pastedFile = items[0].getAsFile();
|
|
|
|
if (!pastedFile) return;
|
|
|
|
const lio = pastedFile.name.lastIndexOf('.');
|
|
|
|
const ext = lio >= 0 ? pastedFile.name.slice(lio) : '';
|
|
|
|
const formatted = formatTimeString(new Date(pastedFile.lastModified), defaultStore.state.pastedFileName).replace(/{{number}}/g, '1') + ext;
|
|
|
|
if (formatted) upload(pastedFile, formatted);
|
2018-02-25 14:56:23 +01:00
|
|
|
}
|
2022-06-20 06:20:28 +02:00
|
|
|
} else {
|
|
|
|
if (items[0].kind === 'file') {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: i18n.ts.onlyOneFileCanBeAttached,
|
|
|
|
});
|
2018-02-25 14:56:23 +01:00
|
|
|
}
|
2022-06-20 06:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-26 20:36:16 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function onDragover(ev: DragEvent) {
|
|
|
|
if (!ev.dataTransfer) return;
|
2018-02-26 20:36:16 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
const isFile = ev.dataTransfer.items[0].kind === 'file';
|
|
|
|
const isDriveFile = ev.dataTransfer.types[0] === _DATA_TRANSFER_DRIVE_FILE_;
|
|
|
|
if (isFile || isDriveFile) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.dataTransfer.dropEffect = ev.dataTransfer.effectAllowed === 'all' ? 'copy' : 'move';
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 20:36:16 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function onDrop(ev: DragEvent): void {
|
|
|
|
if (!ev.dataTransfer) return;
|
|
|
|
|
|
|
|
// ファイルだったら
|
|
|
|
if (ev.dataTransfer.files.length === 1) {
|
|
|
|
ev.preventDefault();
|
|
|
|
upload(ev.dataTransfer.files[0]);
|
|
|
|
return;
|
|
|
|
} else if (ev.dataTransfer.files.length > 1) {
|
|
|
|
ev.preventDefault();
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: i18n.ts.onlyOneFileCanBeAttached,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2018-02-13 07:56:11 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
//#region ドライブのファイル
|
|
|
|
const driveFile = ev.dataTransfer.getData(_DATA_TRANSFER_DRIVE_FILE_);
|
|
|
|
if (driveFile != null && driveFile !== '') {
|
|
|
|
file = JSON.parse(driveFile);
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
}
|
2018-02-13 07:56:11 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function onKeydown(ev: KeyboardEvent) {
|
|
|
|
typing();
|
2022-07-09 00:45:19 +02:00
|
|
|
let sendOnEnter = localStorage.getItem('enterSendsMessage') === 'true' || defaultStore.state.enterSendsMessage;
|
|
|
|
if (sendOnEnter) {
|
|
|
|
if ((ev.key === 'Enter') && (ev.ctrlKey || ev.metaKey)) {
|
|
|
|
textEl.value += '\n';
|
|
|
|
}
|
|
|
|
else if (ev.key === 'Enter' && !ev.shiftKey && !('ontouchstart' in document.documentElement) && canSend) {
|
|
|
|
ev.preventDefault();
|
|
|
|
send();
|
|
|
|
}
|
2022-07-07 02:54:04 +02:00
|
|
|
}
|
2022-07-09 00:45:19 +02:00
|
|
|
else {
|
|
|
|
if ((ev.key === 'Enter') && (ev.ctrlKey || ev.metaKey) && canSend) {
|
|
|
|
ev.preventDefault();
|
|
|
|
send();
|
|
|
|
}
|
2022-06-20 06:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-21 04:26:49 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function onCompositionUpdate() {
|
|
|
|
typing();
|
|
|
|
}
|
2018-02-13 07:56:11 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function chooseFile(ev: MouseEvent) {
|
|
|
|
selectFile(ev.currentTarget ?? ev.target, i18n.ts.selectFile).then(selectedFile => {
|
|
|
|
file = selectedFile;
|
|
|
|
});
|
|
|
|
}
|
2018-02-26 18:10:52 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function onChangeFile() {
|
|
|
|
if (fileEl.files![0]) upload(fileEl.files[0]);
|
|
|
|
}
|
2018-02-18 15:51:41 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function upload(fileToUpload: File, name?: string) {
|
|
|
|
uploadFile(fileToUpload, defaultStore.state.uploadFolder, name).then(res => {
|
|
|
|
file = res;
|
|
|
|
});
|
|
|
|
}
|
2018-02-13 07:56:11 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function send() {
|
|
|
|
sending = true;
|
|
|
|
os.api('messaging/messages/create', {
|
|
|
|
userId: props.user ? props.user.id : undefined,
|
|
|
|
groupId: props.group ? props.group.id : undefined,
|
|
|
|
text: text ? text : undefined,
|
|
|
|
fileId: file ? file.id : undefined,
|
|
|
|
}).then(message => {
|
|
|
|
clear();
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
}).then(() => {
|
|
|
|
sending = false;
|
|
|
|
});
|
|
|
|
}
|
2018-02-25 14:56:23 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function clear() {
|
|
|
|
text = '';
|
|
|
|
file = null;
|
|
|
|
deleteDraft();
|
|
|
|
}
|
2018-02-25 14:56:23 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function saveDraft() {
|
|
|
|
const drafts = JSON.parse(localStorage.getItem('message_drafts') || '{}');
|
2018-02-25 14:56:23 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
drafts[draftKey] = {
|
|
|
|
updatedAt: new Date(),
|
|
|
|
// eslint-disable-next-line id-denylist
|
|
|
|
data: {
|
|
|
|
text: text,
|
|
|
|
file: file,
|
2018-02-25 14:56:23 +01:00
|
|
|
},
|
2022-06-20 06:20:28 +02:00
|
|
|
};
|
2018-02-25 14:56:23 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
localStorage.setItem('message_drafts', JSON.stringify(drafts));
|
|
|
|
}
|
2018-02-25 14:56:23 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
function deleteDraft() {
|
|
|
|
const drafts = JSON.parse(localStorage.getItem('message_drafts') || '{}');
|
2018-02-25 14:56:23 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
delete drafts[draftKey];
|
2020-02-06 11:22:15 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
localStorage.setItem('message_drafts', JSON.stringify(drafts));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function insertEmoji(ev: MouseEvent) {
|
|
|
|
os.openEmojiPicker(ev.currentTarget ?? ev.target, {}, textEl);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
autosize(textEl);
|
|
|
|
|
|
|
|
// TODO: detach when unmount
|
2022-07-07 03:09:26 +02:00
|
|
|
new Autocomplete(textEl, $$(text));
|
2022-06-20 06:20:28 +02:00
|
|
|
|
|
|
|
// 書きかけの投稿を復元
|
|
|
|
const draft = JSON.parse(localStorage.getItem('message_drafts') || '{}')[draftKey];
|
|
|
|
if (draft) {
|
|
|
|
text = draft.data.text;
|
|
|
|
file = draft.data.file;
|
2018-02-13 07:56:11 +01:00
|
|
|
}
|
|
|
|
});
|
2022-06-20 06:20:28 +02:00
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
file,
|
|
|
|
upload,
|
|
|
|
});
|
2018-02-13 07:56:11 +01:00
|
|
|
</script>
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
<style lang="scss" scoped>
|
2021-04-10 05:40:50 +02:00
|
|
|
.pemppnzi {
|
2022-09-14 05:07:19 +02:00
|
|
|
position: relative;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
> textarea {
|
|
|
|
cursor: auto;
|
|
|
|
display: block;
|
2022-09-14 05:07:19 +02:00
|
|
|
width: 100%;
|
|
|
|
min-width: 100%;
|
|
|
|
max-width: 100%;
|
|
|
|
min-height: 80px;
|
2020-01-29 20:37:25 +01:00
|
|
|
margin: 0;
|
|
|
|
padding: 16px 16px 0 16px;
|
|
|
|
resize: none;
|
|
|
|
font-size: 1em;
|
2020-02-09 11:59:28 +01:00
|
|
|
font-family: inherit;
|
2020-01-29 20:37:25 +01:00
|
|
|
outline: none;
|
|
|
|
border: none;
|
|
|
|
border-radius: 0;
|
|
|
|
box-shadow: none;
|
|
|
|
background: transparent;
|
|
|
|
box-sizing: border-box;
|
|
|
|
color: var(--fg);
|
|
|
|
}
|
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
footer {
|
|
|
|
position: sticky;
|
2020-01-29 20:37:25 +01:00
|
|
|
bottom: 0;
|
2022-06-20 06:20:28 +02:00
|
|
|
background: var(--panel);
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
> .file {
|
|
|
|
padding: 8px;
|
|
|
|
color: var(--fg);
|
|
|
|
background: transparent;
|
|
|
|
cursor: pointer;
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
.buttons {
|
|
|
|
display: flex;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
._button {
|
|
|
|
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;
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
> .send {
|
|
|
|
margin-left: auto;
|
|
|
|
color: var(--accent);
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
color: var(--accentLighten);
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
|
|
|
color: var(--accentDarken);
|
|
|
|
transition: color 0s ease;
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input[type=file] {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 07:56:11 +01:00
|
|
|
</style>
|