Kommentare mit Antworten unter Neuigkeiten
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m0s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 8m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m10s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 20:13:41 +00:00
parent f3f2d23961
commit 9320d602a8
117 changed files with 1015 additions and 423 deletions

View file

@ -0,0 +1,215 @@
<script setup>
import { nextTick, onMounted, ref } from 'vue';
import { router, usePage } from '@inertiajs/vue3';
import { CornerDownRight, LoaderCircle, MessageCircle, Pencil, Reply, Send, Trash2 } from '@lucide/vue';
import { Button } from '@/components/ui/button';
import PersonAvatar from '@/components/PersonAvatar.vue';
/**
* Kommentare unter einem Beitrag – mit Antworten (eine Ebene, „Antwort an …“).
*/
const props = defineProps({
postSlug: { type: String, required: true },
comments: { type: Object, required: true },
});
const page = usePage();
const me = page.props.auth?.user;
const body = ref('');
const sending = ref(false);
const errors = ref({});
const replyTo = ref(null); // Kommentar, auf den geantwortet wird
const replyBody = ref('');
const editing = ref(null);
const editBody = ref('');
const relative = new Intl.RelativeTimeFormat('de-DE', { numeric: 'auto' });
const dateFormat = new Intl.DateTimeFormat('de-DE', { dateStyle: 'medium', timeStyle: 'short' });
function ago(iso) {
const seconds = (new Date(iso) - new Date()) / 1000;
const steps = [[60, 'second'], [60, 'minute'], [24, 'hour'], [7, 'day']];
let value = seconds;
for (const [size, unit] of steps) {
if (Math.abs(value) < size) return relative.format(Math.round(value), unit);
value /= size;
}
return dateFormat.format(new Date(iso));
}
function send(data, done) {
sending.value = true;
router.post(`/neuigkeiten/${props.postSlug}/kommentare`, data, {
preserveScroll: true,
only: ['comments', 'flash'],
onError: (e) => (errors.value = e),
onSuccess: () => {
errors.value = {};
done();
const id = page.props.flash?.comment;
if (id) nextTick(() => highlight(id));
},
onFinish: () => (sending.value = false),
});
}
function submit() {
if (!body.value.trim()) return;
send({ body: body.value }, () => (body.value = ''));
}
function startReply(comment) {
editing.value = null;
replyTo.value = comment;
replyBody.value = '';
nextTick(() => document.getElementById(`antwort-${comment.id}`)?.focus());
}
function submitReply() {
if (!replyBody.value.trim()) return;
send({ body: replyBody.value, parent_id: replyTo.value.id }, () => {
replyTo.value = null;
replyBody.value = '';
});
}
function startEdit(comment) {
replyTo.value = null;
editing.value = comment.id;
editBody.value = comment.body;
}
function saveEdit(comment) {
router.put(`/kommentare/${comment.id}`, { body: editBody.value }, { preserveScroll: true, only: ['comments', 'flash'], onSuccess: () => (editing.value = null) });
}
function remove(comment) {
if (window.confirm('Kommentar löschen?')) {
router.delete(`/kommentare/${comment.id}`, { preserveScroll: true, only: ['comments', 'flash'] });
}
}
function highlight(id) {
const el = document.getElementById(`kommentar-${id}`);
if (!el) return;
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
el.classList.add('ring-2', 'ring-brand/40');
setTimeout(() => el.classList.remove('ring-2', 'ring-brand/40'), 2000);
}
onMounted(() => {
const match = window.location.hash.match(/^#kommentar-(\d+)$/);
if (match) setTimeout(() => highlight(match[1]), 300);
});
const grow = (event) => {
event.target.style.height = 'auto';
event.target.style.height = `${event.target.scrollHeight}px`;
};
</script>
<template>
<section id="kommentare" class="rounded-xl border bg-card p-6 shadow-xs sm:p-8" aria-labelledby="kommentare-titel">
<h2 id="kommentare-titel" class="flex items-center gap-2 text-lg font-semibold">
<MessageCircle class="size-5 text-brand-text" /> Kommentare
<span v-if="comments.count" class="rounded-full bg-muted px-2 py-0.5 text-xs font-medium text-muted-foreground tabular-nums">{{ comments.count }}</span>
</h2>
<!-- Neuer Kommentar -->
<form v-if="comments.enabled" class="mt-5 flex gap-3" @submit.prevent="submit">
<PersonAvatar v-if="me" :person="me" class="size-9 shrink-0" />
<div class="min-w-0 flex-1">
<textarea
v-model="body"
rows="2"
:maxlength="comments.max"
placeholder="Was meinen Sie dazu?"
aria-label="Kommentar schreiben"
class="w-full resize-none rounded-lg border border-input bg-background px-3 py-2 text-sm leading-relaxed focus-visible:border-brand-400 focus-visible:ring-3 focus-visible:ring-ring/30 focus-visible:outline-none"
@input="grow"
@keydown.ctrl.enter="submit"
@keydown.meta.enter="submit"
/>
<p v-if="errors.body && !replyTo" class="mt-1 text-[13px] text-destructive">{{ errors.body }}</p>
<div v-if="body.trim()" class="mt-2 flex items-center justify-end gap-3">
<span class="text-xs text-muted-foreground">Strg + Enter zum Senden</span>
<Button type="submit" size="sm" :disabled="sending"><LoaderCircle v-if="sending" class="animate-spin" /><Send v-else /> Kommentieren</Button>
</div>
</div>
</form>
<p v-else class="mt-4 rounded-md bg-muted px-3 py-2 text-[13px] text-muted-foreground">Die Kommentare zu diesem Beitrag sind geschlossen.</p>
<p v-if="!comments.threads.length && comments.enabled" class="mt-6 text-sm text-muted-foreground">Noch keine Kommentare – schreiben Sie den ersten.</p>
<!-- Stränge -->
<ol class="mt-6 grid gap-6">
<li v-for="thread in comments.threads" :key="thread.id">
<template v-for="(comment, index) in [thread, ...thread.replies]" :key="comment.id">
<article :id="`kommentar-${comment.id}`" class="flex gap-3 rounded-lg transition-shadow" :class="index > 0 ? 'mt-4 ml-6 sm:ml-12' : ''">
<PersonAvatar v-if="comment.author" :person="comment.author" :class="index > 0 ? 'size-8' : 'size-9'" class="shrink-0" />
<span v-else class="grid shrink-0 place-items-center rounded-full bg-muted text-muted-foreground" :class="index > 0 ? 'size-8' : 'size-9'"><MessageCircle class="size-4" /></span>
<div class="min-w-0 flex-1">
<p v-if="comment.deleted" class="py-1 text-sm text-muted-foreground italic">Dieser Kommentar wurde gelöscht.</p>
<template v-else>
<div class="rounded-lg bg-muted/60 px-3.5 py-2.5">
<p class="flex flex-wrap items-baseline gap-x-2 text-[13px]">
<span class="font-semibold">{{ comment.author?.name ?? 'Ehemalige Person' }}</span>
<span v-if="comment.author?.department" class="text-muted-foreground">{{ comment.author.department }}</span>
</p>
<p v-if="comment.reply_to" class="mt-0.5 flex items-center gap-1 text-xs text-muted-foreground"><CornerDownRight class="size-3" /> Antwort an {{ comment.reply_to }}</p>
<form v-if="editing === comment.id" class="mt-2" @submit.prevent="saveEdit(comment)">
<textarea v-model="editBody" rows="3" :maxlength="comments.max" class="w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-3 focus-visible:ring-ring/30" aria-label="Kommentar bearbeiten" />
<div class="mt-2 flex justify-end gap-2">
<Button type="button" size="sm" variant="ghost" @click="editing = null">Abbrechen</Button>
<Button type="submit" size="sm" :disabled="!editBody.trim()">Speichern</Button>
</div>
</form>
<p v-else class="mt-1 text-sm leading-relaxed break-words whitespace-pre-line">{{ comment.body }}</p>
</div>
<div class="mt-1 flex flex-wrap items-center gap-x-3 px-1 text-xs text-muted-foreground">
<time :datetime="comment.created_at" :title="dateFormat.format(new Date(comment.created_at))">{{ ago(comment.created_at) }}</time>
<span v-if="comment.edited">bearbeitet</span>
<button v-if="comments.enabled" type="button" class="inline-flex items-center gap-1 font-medium hover:text-foreground" @click="startReply(comment)"><Reply class="size-3.5" /> Antworten</button>
<button v-if="comment.can.edit" type="button" class="inline-flex items-center gap-1 hover:text-foreground" @click="startEdit(comment)"><Pencil class="size-3" /> Bearbeiten</button>
<button v-if="comment.can.delete" type="button" class="inline-flex items-center gap-1 hover:text-destructive" @click="remove(comment)"><Trash2 class="size-3" /> Löschen</button>
</div>
</template>
</div>
</article>
</template>
<!-- Antwort schreiben (unter dem Strang) -->
<form v-if="replyTo && [thread.id, ...thread.replies.map((r) => r.id)].includes(replyTo.id)" class="mt-3 ml-6 flex gap-3 sm:ml-12" @submit.prevent="submitReply">
<PersonAvatar v-if="me" :person="me" class="size-8 shrink-0" />
<div class="min-w-0 flex-1">
<p class="mb-1 text-xs text-muted-foreground">Antwort an {{ replyTo.author?.name }}</p>
<textarea
:id="`antwort-${replyTo.id}`"
v-model="replyBody"
rows="2"
:maxlength="comments.max"
class="w-full resize-none rounded-lg border border-input bg-background px-3 py-2 text-sm focus-visible:border-brand-400 focus-visible:ring-3 focus-visible:ring-ring/30 focus-visible:outline-none"
aria-label="Antwort schreiben"
@input="grow"
@keydown.ctrl.enter="submitReply"
@keydown.meta.enter="submitReply"
@keydown.esc="replyTo = null"
/>
<p v-if="errors.body" class="mt-1 text-[13px] text-destructive">{{ errors.body }}</p>
<div class="mt-2 flex justify-end gap-2">
<Button type="button" size="sm" variant="ghost" @click="replyTo = null">Abbrechen</Button>
<Button type="submit" size="sm" :disabled="sending || !replyBody.trim()"><Send /> Antworten</Button>
</div>
</div>
</form>
</li>
</ol>
</section>
</template>