2018-02-13 00:56:11 -06:00
|
|
|
<template>
|
2022-06-19 23:20:28 -05:00
|
|
|
<div
|
2023-01-15 11:37:45 -06:00
|
|
|
:class="$style['root']"
|
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
|
2023-01-15 11:37:45 -06:00
|
|
|
:class="$style['textarea']"
|
|
|
|
class="_acrylic"
|
2022-06-19 23:20:28 -05:00
|
|
|
ref="textEl"
|
2021-11-19 04:36:12 -06:00
|
|
|
v-model="text"
|
2022-06-19 23:20:28 -05:00
|
|
|
:placeholder="i18n.ts.inputMessageHere"
|
2022-01-25 03:01:29 -06:00
|
|
|
@keydown="onKeydown"
|
2021-02-20 21:26:49 -06:00
|
|
|
@compositionupdate="onCompositionUpdate"
|
2018-02-24 22:05:55 -06:00
|
|
|
@paste="onPaste"
|
|
|
|
></textarea>
|
2023-01-15 11:37:45 -06:00
|
|
|
<footer :class="$style['footer']">
|
|
|
|
<div v-if="file" :class="$style['file']" @click="file = null">{{ file.name }}</div>
|
|
|
|
<div :class="$style['buttons']">
|
|
|
|
<button class="_button" :class="$style['button']" @click="chooseFile"><i class="ti ti-photo-plus"></i></button>
|
|
|
|
<button class="_button" :class="$style['button']" @click="insertEmoji"><i class="ti ti-mood-happy"></i></button>
|
|
|
|
<button class="_button" :class="[$style['button'], $style['send']]" :disabled="!canSend || sending" :title="i18n.ts.send" @click="send">
|
2022-12-19 20:00:05 -06:00
|
|
|
<template v-if="!sending"><i class="ti ti-send"></i></template><template v-if="sending"><MkLoading :em="true"/></template>
|
2022-06-19 23:20:28 -05:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</footer>
|
2023-01-15 11:37:45 -06:00
|
|
|
<input :class="$style['file-input']" ref="fileEl" type="file" @change="onChangeFile"/>
|
2018-02-13 00:56:11 -06:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, watch } from 'vue';
|
|
|
|
import * as Misskey from 'misskey-js';
|
2022-01-27 02:55:11 -06:00
|
|
|
import autosize from 'autosize';
|
2022-06-19 23:20:28 -05:00
|
|
|
//import insertTextAtCursor from 'insert-text-at-cursor';
|
|
|
|
import { throttle } from 'throttle-debounce';
|
2021-11-11 11:02:25 -06:00
|
|
|
import { formatTimeString } from '@/scripts/format-time-string';
|
|
|
|
import { selectFile } from '@/scripts/select-file';
|
|
|
|
import * as os from '@/os';
|
2021-12-29 07:13:09 -06:00
|
|
|
import { stream } from '@/stream';
|
2022-06-19 23:20:28 -05:00
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { i18n } from '@/i18n';
|
|
|
|
//import { Autocomplete } from '@/scripts/autocomplete';
|
2022-04-27 21:14:03 -05:00
|
|
|
import { uploadFile } from '@/scripts/upload';
|
2023-01-06 19:13:02 -06:00
|
|
|
import { miLocalStorage } from '@/local-storage';
|
2018-02-24 11:57:19 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
const props = defineProps<{
|
|
|
|
user?: Misskey.entities.UserDetailed | null;
|
|
|
|
group?: Misskey.entities.UserGroup | null;
|
|
|
|
}>();
|
|
|
|
|
2023-01-02 19:12:37 -06:00
|
|
|
let textEl = $shallowRef<HTMLTextAreaElement>();
|
|
|
|
let fileEl = $shallowRef<HTMLInputElement>();
|
2022-06-19 23:20:28 -05:00
|
|
|
|
|
|
|
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 07:56:23 -06:00
|
|
|
}
|
2022-06-19 23:20:28 -05:00
|
|
|
} else {
|
|
|
|
if (items[0].kind === 'file') {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: i18n.ts.onlyOneFileCanBeAttached,
|
|
|
|
});
|
2018-02-25 07:56:23 -06:00
|
|
|
}
|
2022-06-19 23:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
2018-02-26 13:36:16 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function onDragover(ev: DragEvent) {
|
|
|
|
if (!ev.dataTransfer) return;
|
2018-02-26 13:36:16 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
const isFile = ev.dataTransfer.items[0].kind === 'file';
|
|
|
|
const isDriveFile = ev.dataTransfer.types[0] === _DATA_TRANSFER_DRIVE_FILE_;
|
|
|
|
if (isFile || isDriveFile) {
|
|
|
|
ev.preventDefault();
|
2022-10-12 18:34:23 -05:00
|
|
|
switch (ev.dataTransfer.effectAllowed) {
|
|
|
|
case 'all':
|
|
|
|
case 'uninitialized':
|
|
|
|
case 'copy':
|
|
|
|
case 'copyLink':
|
|
|
|
case 'copyMove':
|
|
|
|
ev.dataTransfer.dropEffect = 'copy';
|
|
|
|
break;
|
|
|
|
case 'linkMove':
|
|
|
|
case 'move':
|
|
|
|
ev.dataTransfer.dropEffect = 'move';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ev.dataTransfer.dropEffect = 'none';
|
|
|
|
break;
|
|
|
|
}
|
2022-06-19 23:20:28 -05:00
|
|
|
}
|
|
|
|
}
|
2018-02-26 13:36:16 -06:00
|
|
|
|
2022-06-19 23:20:28 -05: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 00:56:11 -06:00
|
|
|
|
2022-06-19 23:20:28 -05: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 00:56:11 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function onKeydown(ev: KeyboardEvent) {
|
|
|
|
typing();
|
|
|
|
if ((ev.key === 'Enter') && (ev.ctrlKey || ev.metaKey) && canSend) {
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 21:26:49 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function onCompositionUpdate() {
|
|
|
|
typing();
|
|
|
|
}
|
2018-02-13 00:56:11 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function chooseFile(ev: MouseEvent) {
|
|
|
|
selectFile(ev.currentTarget ?? ev.target, i18n.ts.selectFile).then(selectedFile => {
|
|
|
|
file = selectedFile;
|
|
|
|
});
|
|
|
|
}
|
2018-02-26 11:10:52 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function onChangeFile() {
|
|
|
|
if (fileEl.files![0]) upload(fileEl.files[0]);
|
|
|
|
}
|
2018-02-18 08:51:41 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function upload(fileToUpload: File, name?: string) {
|
|
|
|
uploadFile(fileToUpload, defaultStore.state.uploadFolder, name).then(res => {
|
|
|
|
file = res;
|
|
|
|
});
|
|
|
|
}
|
2018-02-13 00:56:11 -06:00
|
|
|
|
2022-06-19 23:20:28 -05: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 07:56:23 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function clear() {
|
|
|
|
text = '';
|
|
|
|
file = null;
|
|
|
|
deleteDraft();
|
|
|
|
}
|
2018-02-25 07:56:23 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function saveDraft() {
|
2023-01-06 19:13:02 -06:00
|
|
|
const drafts = JSON.parse(miLocalStorage.getItem('message_drafts') || '{}');
|
2018-02-25 07:56:23 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
drafts[draftKey] = {
|
|
|
|
updatedAt: new Date(),
|
|
|
|
// eslint-disable-next-line id-denylist
|
|
|
|
data: {
|
|
|
|
text: text,
|
|
|
|
file: file,
|
2018-02-25 07:56:23 -06:00
|
|
|
},
|
2022-06-19 23:20:28 -05:00
|
|
|
};
|
2018-02-25 07:56:23 -06:00
|
|
|
|
2023-01-06 19:13:02 -06:00
|
|
|
miLocalStorage.setItem('message_drafts', JSON.stringify(drafts));
|
2022-06-19 23:20:28 -05:00
|
|
|
}
|
2018-02-25 07:56:23 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
function deleteDraft() {
|
2023-01-06 19:13:02 -06:00
|
|
|
const drafts = JSON.parse(miLocalStorage.getItem('message_drafts') || '{}');
|
2018-02-25 07:56:23 -06:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
delete drafts[draftKey];
|
2020-02-06 04:22:15 -06:00
|
|
|
|
2023-01-06 19:13:02 -06:00
|
|
|
miLocalStorage.setItem('message_drafts', JSON.stringify(drafts));
|
2022-06-19 23:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function insertEmoji(ev: MouseEvent) {
|
|
|
|
os.openEmojiPicker(ev.currentTarget ?? ev.target, {}, textEl);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
autosize(textEl);
|
|
|
|
|
|
|
|
// TODO: detach when unmount
|
|
|
|
// TODO
|
|
|
|
//new Autocomplete(textEl, this, { model: 'text' });
|
|
|
|
|
|
|
|
// 書きかけの投稿を復元
|
2023-01-06 19:13:02 -06:00
|
|
|
const draft = JSON.parse(miLocalStorage.getItem('message_drafts') || '{}')[draftKey];
|
2022-06-19 23:20:28 -05:00
|
|
|
if (draft) {
|
|
|
|
text = draft.data.text;
|
|
|
|
file = draft.data.file;
|
2018-02-13 00:56:11 -06:00
|
|
|
}
|
|
|
|
});
|
2022-06-19 23:20:28 -05:00
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
file,
|
|
|
|
upload,
|
|
|
|
});
|
2018-02-13 00:56:11 -06:00
|
|
|
</script>
|
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
2020-01-29 13:37:25 -06:00
|
|
|
position: relative;
|
2023-01-15 11:37:45 -06:00
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
.textarea {
|
|
|
|
cursor: auto;
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
min-width: 100%;
|
|
|
|
max-width: 100%;
|
|
|
|
min-height: 80px;
|
|
|
|
margin: 0;
|
|
|
|
padding: 16px 16px 0 16px;
|
|
|
|
resize: none;
|
|
|
|
font-size: 1em;
|
|
|
|
font-family: inherit;
|
|
|
|
outline: none;
|
|
|
|
border: none;
|
|
|
|
border-radius: 0;
|
|
|
|
box-shadow: none;
|
|
|
|
box-sizing: border-box;
|
|
|
|
color: var(--fg);
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
.footer {
|
|
|
|
position: sticky;
|
|
|
|
bottom: 0;
|
|
|
|
background: var(--panel);
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
.file {
|
|
|
|
padding: 8px;
|
|
|
|
color: var(--fg);
|
|
|
|
background: transparent;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
.files {
|
|
|
|
display: block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0 8px;
|
|
|
|
list-style: none;
|
|
|
|
|
|
|
|
&:after {
|
|
|
|
content: '';
|
|
|
|
display: block;
|
|
|
|
clear: both;
|
2020-01-29 13:37:25 -06:00
|
|
|
}
|
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
> li {
|
2020-01-29 13:37:25 -06:00
|
|
|
display: block;
|
2023-01-15 11:37:45 -06:00
|
|
|
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 {
|
2020-01-29 13:37:25 -06:00
|
|
|
> .remove {
|
2023-01-15 11:37:45 -06:00
|
|
|
display: block;
|
2020-01-29 13:37:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 11:37:45 -06:00
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
.file-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-19 23:20:28 -05:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
.buttons {
|
|
|
|
display: flex;
|
|
|
|
}
|
2022-06-19 23:20:28 -05:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
.button {
|
|
|
|
margin: 0;
|
|
|
|
padding: 16px;
|
|
|
|
font-size: 1em;
|
|
|
|
font-weight: normal;
|
|
|
|
text-decoration: none;
|
|
|
|
transition: color 0.1s ease;
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
&:hover {
|
|
|
|
color: var(--accent);
|
|
|
|
}
|
2022-06-19 23:20:28 -05:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
&:active {
|
|
|
|
color: var(--accentDarken);
|
|
|
|
transition: color 0s ease;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.send {
|
|
|
|
margin-left: auto;
|
|
|
|
color: var(--accent);
|
2022-06-19 23:20:28 -05:00
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
&:hover {
|
|
|
|
color: var(--accentLighten);
|
2020-01-29 13:37:25 -06:00
|
|
|
}
|
|
|
|
|
2023-01-15 11:37:45 -06:00
|
|
|
&:active {
|
|
|
|
color: var(--accentDarken);
|
|
|
|
transition: color 0s ease;
|
2020-01-29 13:37:25 -06:00
|
|
|
}
|
|
|
|
}
|
2023-01-15 11:37:45 -06:00
|
|
|
|
|
|
|
.file-input {
|
|
|
|
display: none;
|
|
|
|
}
|
2018-02-13 00:56:11 -06:00
|
|
|
</style>
|