199 lines
8.6 KiB
Vue
199 lines
8.6 KiB
Vue
<script setup>
|
|
import { computed, nextTick, ref, watch } from 'vue';
|
|
import { File as FileGlyph, Image as ImageGlyph, LoaderCircle, Paperclip, Pencil, SendHorizontal, Smile, X } from '@lucide/vue';
|
|
import { toast } from '@inhaus/ui';
|
|
import EmojiPicker from './EmojiPicker.vue';
|
|
import GifPicker from './GifPicker.vue';
|
|
import Popup from './Popup.vue';
|
|
import { fileSize } from '../lib/format';
|
|
|
|
const props = defineProps({
|
|
replyTo: { type: Object, default: null },
|
|
editing: { type: Object, default: null },
|
|
maxUploadMb: { type: Number, default: 25 },
|
|
sending: { type: Boolean, default: false },
|
|
});
|
|
const emit = defineEmits(['send', 'save-edit', 'cancel', 'typing']);
|
|
|
|
const text = ref('');
|
|
const files = ref([]);
|
|
const input = ref(null);
|
|
const fileInput = ref(null);
|
|
const popup = ref(null); // 'emoji' | 'gif'
|
|
const canSend = computed(() => !props.sending && (text.value.trim() || files.value.length));
|
|
|
|
// Beim Bearbeiten den Text übernehmen
|
|
watch(() => props.editing, (message) => {
|
|
text.value = message?.body ?? '';
|
|
nextTick(focus);
|
|
});
|
|
watch(() => props.replyTo, (message) => message && nextTick(focus));
|
|
|
|
function focus() {
|
|
input.value?.focus();
|
|
resize();
|
|
}
|
|
|
|
function resize() {
|
|
const el = input.value;
|
|
|
|
if (!el) return;
|
|
|
|
el.style.height = 'auto';
|
|
el.style.height = `${Math.min(el.scrollHeight, 180)}px`;
|
|
}
|
|
|
|
let lastTyping = 0;
|
|
function onInput() {
|
|
resize();
|
|
|
|
if (Date.now() - lastTyping > 3000 && text.value.trim()) {
|
|
lastTyping = Date.now();
|
|
emit('typing');
|
|
}
|
|
}
|
|
|
|
function addFiles(list) {
|
|
for (const file of list) {
|
|
if (file.size > props.maxUploadMb * 1024 * 1024) {
|
|
toast.error(`„${file.name}“ ist größer als ${props.maxUploadMb} MB.`);
|
|
continue;
|
|
}
|
|
|
|
if (files.value.length >= 10) {
|
|
toast.error('Höchstens 10 Dateien je Nachricht.');
|
|
break;
|
|
}
|
|
|
|
files.value.push({ file, url: file.type.startsWith('image/') ? URL.createObjectURL(file) : null });
|
|
}
|
|
|
|
focus();
|
|
}
|
|
|
|
function onPaste(event) {
|
|
const pasted = [...(event.clipboardData?.files ?? [])];
|
|
|
|
if (pasted.length) {
|
|
event.preventDefault();
|
|
addFiles(pasted);
|
|
}
|
|
}
|
|
|
|
function removeFile(index) {
|
|
const [removed] = files.value.splice(index, 1);
|
|
removed?.url && URL.revokeObjectURL(removed.url);
|
|
}
|
|
|
|
function submit() {
|
|
if (props.editing) {
|
|
text.value.trim() && emit('save-edit', text.value.trim());
|
|
|
|
return;
|
|
}
|
|
|
|
if (!canSend.value) return;
|
|
|
|
emit('send', { body: text.value, files: files.value.map((f) => f.file) });
|
|
files.value.forEach((f) => f.url && URL.revokeObjectURL(f.url));
|
|
files.value = [];
|
|
text.value = '';
|
|
nextTick(resize);
|
|
}
|
|
|
|
function onKeydown(event) {
|
|
// Enter sendet, Umschalt+Enter macht eine neue Zeile
|
|
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing) {
|
|
event.preventDefault();
|
|
submit();
|
|
}
|
|
|
|
if (event.key === 'Escape' && (props.editing || props.replyTo)) emit('cancel');
|
|
}
|
|
|
|
function insertEmoji(emoji) {
|
|
const el = input.value;
|
|
const start = el?.selectionStart ?? text.value.length;
|
|
const end = el?.selectionEnd ?? text.value.length;
|
|
text.value = text.value.slice(0, start) + emoji + text.value.slice(end);
|
|
nextTick(() => {
|
|
el?.focus();
|
|
el?.setSelectionRange(start + emoji.length, start + emoji.length);
|
|
});
|
|
}
|
|
|
|
function pickGif(gif) {
|
|
popup.value = null;
|
|
emit('send', { gifUrl: gif.url });
|
|
}
|
|
|
|
const toggle = (name) => (popup.value = popup.value === name ? null : name);
|
|
|
|
defineExpose({ addFiles, focus });
|
|
</script>
|
|
|
|
<template>
|
|
<div class="ih:border-t ih:bg-card ih:p-3">
|
|
<!-- Antworten / Bearbeiten -->
|
|
<div v-if="replyTo || editing" class="ih:mb-2 ih:flex ih:items-start ih:gap-2 ih:rounded-md ih:border-l-3 ih:border-brand ih:bg-muted ih:px-3 ih:py-1.5 ih:text-[13px]">
|
|
<Pencil v-if="editing" class="ih:mt-0.5 ih:size-3.5 ih:shrink-0 ih:text-brand-text" />
|
|
<span class="ih:min-w-0 ih:flex-1">
|
|
<span class="ih:block ih:font-semibold">{{ editing ? 'Nachricht bearbeiten' : `Antwort an ${replyTo.user?.name ?? ''}` }}</span>
|
|
<span v-if="replyTo" class="ih:line-clamp-1 ih:text-muted-foreground">{{ replyTo.body || (replyTo.gif_url ? 'GIF' : 'Datei') }}</span>
|
|
</span>
|
|
<button type="button" class="ih:rounded ih:p-0.5 ih:text-muted-foreground ih:hover:text-foreground" aria-label="Abbrechen" @click="emit('cancel')"><X class="ih:size-4" /></button>
|
|
</div>
|
|
|
|
<!-- Ausgewählte Dateien -->
|
|
<ul v-if="files.length" class="ih:mb-2 ih:flex ih:gap-2 ih:overflow-x-auto ih:pb-1">
|
|
<li v-for="(f, i) in files" :key="i" class="ih:relative ih:shrink-0">
|
|
<img v-if="f.url" :src="f.url" :alt="f.file.name" class="ih:size-16 ih:rounded-md ih:border ih:object-cover" />
|
|
<span v-else class="ih:flex ih:h-16 ih:w-36 ih:items-center ih:gap-2 ih:rounded-md ih:border ih:bg-muted ih:px-2 ih:text-xs">
|
|
<FileGlyph class="ih:size-5 ih:shrink-0 ih:text-muted-foreground" />
|
|
<span class="ih:min-w-0"><span class="ih:block ih:truncate ih:font-medium">{{ f.file.name }}</span>{{ fileSize(f.file.size) }}</span>
|
|
</span>
|
|
<button type="button" class="ih:absolute ih:-top-1.5 ih:-right-1.5 ih:grid ih:size-5 ih:place-items-center ih:rounded-full ih:bg-foreground ih:text-background" :aria-label="`${f.file.name} entfernen`" @click="removeFile(i)"><X class="ih:size-3" /></button>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="ih:relative ih:flex ih:items-end ih:gap-1">
|
|
<Popup v-if="popup === 'emoji'" @close="popup = null"><EmojiPicker @pick="insertEmoji" /></Popup>
|
|
<Popup v-if="popup === 'gif'" @close="popup = null"><GifPicker @pick="pickGif" /></Popup>
|
|
|
|
<template v-if="!editing">
|
|
<button type="button" data-popup-toggle class="ih:grid ih:size-10 ih:shrink-0 ih:place-items-center ih:rounded-full ih:text-muted-foreground ih:hover:bg-muted ih:hover:text-foreground" :class="{ 'ih:bg-muted ih:text-foreground': popup === 'emoji' }" aria-label="Emoji einfügen" :aria-expanded="popup === 'emoji'" @click="toggle('emoji')"><Smile class="ih:size-5" /></button>
|
|
<button type="button" data-popup-toggle class="ih:grid ih:h-10 ih:shrink-0 ih:place-items-center ih:rounded-full ih:px-2 ih:text-xs ih:font-bold ih:text-muted-foreground ih:hover:bg-muted ih:hover:text-foreground" :class="{ 'ih:bg-muted ih:text-foreground': popup === 'gif' }" aria-label="GIF senden" :aria-expanded="popup === 'gif'" @click="toggle('gif')">GIF</button>
|
|
<button type="button" class="ih:grid ih:size-10 ih:shrink-0 ih:place-items-center ih:rounded-full ih:text-muted-foreground ih:hover:bg-muted ih:hover:text-foreground" aria-label="Datei anhängen" @click="fileInput?.click()"><Paperclip class="ih:size-5" /></button>
|
|
<input ref="fileInput" type="file" multiple class="ih:hidden" @change="addFiles([...$event.target.files]); $event.target.value = ''" />
|
|
</template>
|
|
|
|
<textarea
|
|
ref="input"
|
|
v-model="text"
|
|
rows="1"
|
|
maxlength="5000"
|
|
:placeholder="editing ? 'Nachricht bearbeiten' : 'Nachricht schreiben'"
|
|
aria-label="Nachricht"
|
|
class="ih:max-h-[180px] ih:min-h-10 ih:flex-1 ih:resize-none ih:rounded-2xl ih:border ih:border-input ih:bg-background ih:px-4 ih:py-2 ih:text-[15px] ih:leading-snug ih:focus-visible:ring-3 ih:focus-visible:ring-ring/30 ih:focus-visible:outline-none"
|
|
@input="onInput"
|
|
@keydown="onKeydown"
|
|
@paste="onPaste"
|
|
/>
|
|
|
|
<button
|
|
type="button"
|
|
class="ih:grid ih:size-10 ih:shrink-0 ih:place-items-center ih:rounded-full ih:bg-brand ih:text-brand-foreground ih:transition-opacity ih:disabled:opacity-40"
|
|
:disabled="editing ? !text.trim() : !canSend"
|
|
:aria-label="editing ? 'Speichern' : 'Senden'"
|
|
@click="submit"
|
|
>
|
|
<LoaderCircle v-if="sending" class="ih:size-5 ih:animate-spin" />
|
|
<SendHorizontal v-else class="ih:size-5" />
|
|
</button>
|
|
</div>
|
|
<p class="ih:mt-1.5 ih:hidden ih:text-[11px] ih:text-muted-foreground ih:sm:block">
|
|
<kbd class="ih:font-sans">Enter</kbd> senden · <kbd class="ih:font-sans">Umschalt + Enter</kbd> neue Zeile · Bilder lassen sich auch einfügen oder hierher ziehen
|
|
<ImageGlyph class="ih:inline ih:size-3" />
|
|
</p>
|
|
</div>
|
|
</template>
|