Loheide-eu-mitarbeiterplatt.../resources/js/components/RichTextEditor.vue
Friederich Loheide 1a54e0c215
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m6s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m23s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m9s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m59s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m16s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m27s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m9s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m15s
Leere Textblöcke anklickbar, Startseiten-Slider nach Durchklicken repariert
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:17:07 +00:00

177 lines
8.2 KiB
Vue
Raw Permalink 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,
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.
*/
const props = defineProps({
modelValue: { type: String, default: '' },
placeholder: { type: String, default: '' },
headings: { type: Array, default: () => [2, 3] },
uploadImage: { 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;
}
}
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>
<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>