Loheide-eu-mitarbeiterplatt.../resources/js/components/RichTextEditor.vue
Friederich Loheide 694ccf6ae0 Mediathek: zentrale Bildverwaltung mit Verwendungsnachweis und Bildauswahl
- Tabellen media und media_usages, Modelle Media/MediaUsage
- App\Media\MediaLibrary (store/register/replace/delete/rebuild) und
  Registry App\Media\MediaUsages (Quellen: Seiten, Neuigkeiten inkl.
  künftiger content-Spalte, Termine, Startseiten-Kacheln), ImagePaths-Extraktor
- Alle Uploads landen in der Mediathek (Seiten-Editor direkt, übrige über
  Beobachter beim Speichern); Daten-Migration und media:rebuild übernehmen
  bestehende Bilder
- Recht media.manage, Zugang media.use für alle Redaktionsbereiche
- /redaktion/mediathek (Suche, Filter, Upload mit Fortschritt, Details,
  Eingebunden in, Ersetzen, Löschen mit Warnung, Mehrfachauswahl),
  Abschnitt in der Redaktion und Menüpunkt im Admin-Bereich
- MediaPicker (Hochladen/Mediathek) im Pagebuilder: Bildfelder und
  Galerie (Mehrfachauswahl), editor.pickMedia im provide-API
- Bild-URLs mit ?v=Änderungszeit, null bei fehlender Datei; Modelle löschen
  geteilte Bilder nicht mehr selbst

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:05:50 +00:00

192 lines
8.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup>
import { onBeforeUnmount, ref, watch } from 'vue';
import { EditorContent, useEditor } from '@tiptap/vue-3';
import StarterKit from '@tiptap/starter-kit';
import Image from '@tiptap/extension-image';
import { Placeholder } from '@tiptap/extensions';
import {
Bold,
Check,
Heading2,
Heading3,
ImagePlus,
Images,
Italic,
Link as LinkIcon,
List,
ListOrdered,
LoaderCircle,
Quote,
Redo2,
Underline,
Undo2,
Unlink,
X,
} from '@lucide/vue';
/**
* Formatierter Text (TipTap). Liefert HTML – bereinigt wird auf dem Server
* (App\Support\RichText::sanitize). Bilder nur, wenn uploadImage übergeben wird;
* mit pickImage (z. B. editor.pickMedia der Bildauswahl) zusätzlich „Aus der Mediathek“.
*/
const props = defineProps({
modelValue: { type: String, default: '' },
placeholder: { type: String, default: '' },
headings: { type: Array, default: () => [2, 3] },
uploadImage: { type: Function, default: null },
/** ({ multiple }) => Promise<[{ url, alt }] | null> */
pickImage: { type: Function, default: null },
minHeight: { type: String, default: '' },
bordered: { type: Boolean, default: false },
});
const emit = defineEmits(['update:modelValue']);
const editor = useEditor({
content: props.modelValue,
extensions: [
StarterKit.configure({
heading: { levels: props.headings },
code: false,
codeBlock: false,
link: { openOnClick: false, autolink: true, protocols: ['mailto', 'tel'], HTMLAttributes: { rel: 'noopener noreferrer', target: null } },
}),
Image.configure({ HTMLAttributes: { loading: 'lazy' } }),
Placeholder.configure({ placeholder: props.placeholder }),
],
editorProps: {
attributes: {
class: 'prose prose-neutral max-w-none focus:outline-none prose-headings:tracking-tight prose-img:rounded-lg',
},
},
onUpdate: ({ editor }) => emit('update:modelValue', editor.isEmpty ? '' : editor.getHTML()),
});
watch(() => props.modelValue, (value) => {
if (editor.value && value !== editor.value.getHTML() && !(value === '' && editor.value.isEmpty)) {
editor.value.commands.setContent(value || '', { emitUpdate: false });
}
});
onBeforeUnmount(() => editor.value?.destroy());
// Klick neben den Text (z. B. in einen leeren Block) setzt den Cursor ans Ende
defineExpose({ focus: () => editor.value?.chain().focus('end').run() });
// Link bearbeiten
const linkOpen = ref(false);
const linkUrl = ref('');
function openLink() {
linkUrl.value = editor.value.getAttributes('link').href ?? '';
linkOpen.value = true;
}
function applyLink() {
let url = linkUrl.value.trim();
if (url && !/^(https?:\/\/|mailto:|tel:|\/|#)/i.test(url)) {
url = `https://${url}`;
}
const chain = editor.value.chain().focus().extendMarkRange('link');
(url ? chain.setLink({ href: url }) : chain.unsetLink()).run();
linkOpen.value = false;
}
// Bild einfügen
const fileInput = ref(null);
const uploading = ref(false);
async function insertImage(event) {
const file = event.target.files?.[0];
event.target.value = '';
if (!file || !props.uploadImage) return;
uploading.value = true;
try {
const url = await props.uploadImage(file);
editor.value.chain().focus().setImage({ src: url, alt: '' }).run();
} finally {
uploading.value = false;
}
}
async function pickImages() {
const items = await props.pickImage({ multiple: true });
if (!items?.length) return;
const chain = editor.value.chain().focus();
items.forEach((item) => chain.setImage({ src: item.url, alt: item.alt ?? '' }));
chain.run();
}
const buttonClass = (active) => [
'grid size-8 place-items-center rounded-md transition-colors disabled:opacity-40',
active ? 'bg-brand-soft text-brand-soft-foreground' : 'text-muted-foreground hover:bg-accent hover:text-foreground',
];
</script>
<template>
<div v-if="editor" class="group/rte" :class="bordered ? 'rounded-md border border-input bg-card shadow-xs focus-within:border-brand-400 focus-within:ring-3 focus-within:ring-ring/30' : ''">
<div
class="flex flex-wrap items-center gap-0.5"
:class="bordered ? 'border-b px-1.5 py-1' : 'sticky top-0 z-10 -mx-1 mb-2 hidden rounded-md border bg-card px-1 py-0.5 shadow-sm group-focus-within/rte:flex'"
role="toolbar"
aria-label="Textformatierung"
@mousedown.prevent
>
<button type="button" :class="buttonClass(editor.isActive('bold'))" title="Fett (Strg+B)" @click="editor.chain().focus().toggleBold().run()"><Bold class="size-4" /></button>
<button type="button" :class="buttonClass(editor.isActive('italic'))" title="Kursiv (Strg+I)" @click="editor.chain().focus().toggleItalic().run()"><Italic class="size-4" /></button>
<button type="button" :class="buttonClass(editor.isActive('underline'))" title="Unterstrichen (Strg+U)" @click="editor.chain().focus().toggleUnderline().run()"><Underline class="size-4" /></button>
<span class="mx-1 h-5 w-px bg-border" />
<button v-if="headings.includes(2)" type="button" :class="buttonClass(editor.isActive('heading', { level: 2 }))" title="Zwischenüberschrift" @click="editor.chain().focus().toggleHeading({ level: 2 }).run()"><Heading2 class="size-4" /></button>
<button v-if="headings.includes(3)" type="button" :class="buttonClass(editor.isActive('heading', { level: 3 }))" title="Kleine Überschrift" @click="editor.chain().focus().toggleHeading({ level: 3 }).run()"><Heading3 class="size-4" /></button>
<button type="button" :class="buttonClass(editor.isActive('bulletList'))" title="Aufzählung" @click="editor.chain().focus().toggleBulletList().run()"><List class="size-4" /></button>
<button type="button" :class="buttonClass(editor.isActive('orderedList'))" title="Nummerierte Liste" @click="editor.chain().focus().toggleOrderedList().run()"><ListOrdered class="size-4" /></button>
<button type="button" :class="buttonClass(editor.isActive('blockquote'))" title="Zitat" @click="editor.chain().focus().toggleBlockquote().run()"><Quote class="size-4" /></button>
<span class="mx-1 h-5 w-px bg-border" />
<button type="button" :class="buttonClass(editor.isActive('link'))" title="Link" @click="openLink"><LinkIcon class="size-4" /></button>
<button v-if="editor.isActive('link')" type="button" :class="buttonClass(false)" title="Link entfernen" @click="editor.chain().focus().unsetLink().run()"><Unlink class="size-4" /></button>
<template v-if="uploadImage">
<button type="button" :class="buttonClass(false)" title="Bild einfügen" :disabled="uploading" @click="fileInput?.click()">
<LoaderCircle v-if="uploading" class="size-4 animate-spin" />
<ImagePlus v-else class="size-4" />
</button>
<input ref="fileInput" type="file" accept="image/jpeg,image/png,image/webp,image/gif" class="hidden" @change="insertImage" />
</template>
<button v-if="pickImage" type="button" :class="buttonClass(false)" title="Bild aus der Mediathek" @click="pickImages"><Images class="size-4" /></button>
<span class="mx-1 h-5 w-px bg-border" />
<button type="button" :class="buttonClass(false)" title="Rückgängig" :disabled="!editor.can().undo()" @click="editor.chain().focus().undo().run()"><Undo2 class="size-4" /></button>
<button type="button" :class="buttonClass(false)" title="Wiederholen" :disabled="!editor.can().redo()" @click="editor.chain().focus().redo().run()"><Redo2 class="size-4" /></button>
<form v-if="linkOpen" class="flex w-full items-center gap-1 border-t px-1 pt-1.5 pb-1" @submit.prevent="applyLink" @mousedown.stop>
<input
v-model="linkUrl"
type="text"
placeholder="https://… oder /seiten/…"
class="h-8 min-w-0 flex-1 rounded-md border border-input bg-card px-2 text-sm focus-visible:outline-none focus-visible:ring-3 focus-visible:ring-ring/30"
aria-label="Adresse des Links"
autofocus
/>
<button type="submit" :class="buttonClass(false)" title="Übernehmen"><Check class="size-4" /></button>
<button type="button" :class="buttonClass(false)" title="Abbrechen" @click="linkOpen = false"><X class="size-4" /></button>
</form>
</div>
<EditorContent :editor="editor" :class="[bordered ? 'px-4 py-3' : '', minHeight]" />
</div>
</template>
<style>
.tiptap p.is-editor-empty:first-child::before {
content: attr(data-placeholder);
float: left;
height: 0;
pointer-events: none;
color: var(--muted-foreground);
opacity: 0.7;
}
</style>