Loheide-eu-mitarbeiterplatt.../resources/js/pagebuilder/editor/FieldInput.vue
Friederich Loheide 23c36d28b5
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m33s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m41s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m41s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m41s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m18s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m9s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m0s
Menü: Symbole je Menüpunkt, gemeinsame Symbolauswahl
- Optionales Symbol für jeden Menü- und Unterpunkt (Kopfzeile,
  Aufklappmenüs, „Mehr“, Handy-Menü)
- Gemeinsame Liste resources/icons/icons.json (66 Lucide-Symbole) für
  Menü, Schnellzugriffe und Pagebuilder; `npm run icons` erzeugt
  Frontend-Zuordnung und SVGs für die Vorschau im Admin
- Admin-Auswahl mit Symbolvorschau und Suche

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

314 lines
16 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 { computed, inject, ref, watch } from 'vue';
import { ArrowDown, ArrowUp, FileDown, ImagePlus, LoaderCircle, Plus, Search, Trash2, Upload, X } from '@lucide/vue';
import { toast } from 'vue-sonner';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Switch } from '@/components/ui/switch';
import NativeSelect from '@/components/NativeSelect.vue';
import PersonAvatar from '@/components/PersonAvatar.vue';
import RichTextEditor from '@/components/RichTextEditor.vue';
import { icons } from '@/lib/icons';
import { http } from '@/lib/http';
/**
* Eingabefeld für ein Blockfeld – erzeugt aus der Feldbeschreibung des Servers
* (App\Pagebuilder\Field). Plugin-Blöcke bekommen so ihr Formular geschenkt.
*/
const props = defineProps({
field: { type: Object, required: true },
id: { type: String, required: true },
});
const model = defineModel({ default: null });
const editor = inject('pagebuilder:editor');
const emit = defineEmits(['image-uploaded']);
// Bild
const uploading = ref(false);
const imageInput = ref(null);
async function uploadImage(event) {
const file = event.target.files?.[0];
event.target.value = '';
if (!file) return;
uploading.value = true;
try {
const result = await editor.uploadImage(file);
model.value = result.path;
emit('image-uploaded', result);
} catch (error) {
toast.error(error.fieldErrors?.image ?? error.message);
} finally {
uploading.value = false;
}
}
// Personen
const peopleQuery = ref('');
const peopleResults = ref([]);
const peopleCache = editor.peopleCache;
let searchTimer;
watch(peopleQuery, (query) => {
clearTimeout(searchTimer);
if (query.trim().length < 2) {
peopleResults.value = [];
return;
}
searchTimer = setTimeout(async () => {
const data = await http.get(`/personen/suche?q=${encodeURIComponent(query.trim())}`);
data.people.forEach((person) => (peopleCache[person.id] = person));
peopleResults.value = data.people.filter((person) => !(model.value ?? []).includes(person.id));
}, 250);
});
if (props.field.type === 'people' && (model.value ?? []).some((id) => !peopleCache[id])) {
const missing = model.value.filter((id) => !peopleCache[id]);
http.get(`/personen/suche?${missing.map((id) => `ids[]=${id}`).join('&')}`).then((data) => data.people.forEach((person) => (peopleCache[person.id] = person)));
}
function addPerson(person) {
model.value = [...(model.value ?? []), person.id];
peopleQuery.value = '';
peopleResults.value = [];
}
// Listen (Wiederholer, Personen, Dateien)
function moveItem(list, index, direction) {
const target = index + direction;
if (target < 0 || target >= list.length) return;
const copy = [...list];
[copy[index], copy[target]] = [copy[target], copy[index]];
model.value = copy;
}
function removeItem(index) {
model.value = model.value.filter((_, i) => i !== index);
}
function addRepeaterItem() {
const item = Object.fromEntries(props.field.fields.map((sub) => [sub.name, structuredClone(sub.default ?? (sub.type === 'toggle' ? false : sub.type === 'repeater' ? [] : ''))]));
model.value = [...(model.value ?? []), item];
}
// Tabelle
const table = computed(() => model.value ?? [['']]);
function updateCell(row, col, value) {
const copy = table.value.map((r) => [...r]);
copy[row][col] = value;
model.value = copy;
}
function addRow() {
model.value = [...table.value, Array(table.value[0]?.length ?? 1).fill('')];
}
function addColumn() {
model.value = table.value.map((row) => [...row, '']);
}
function removeRow(index) {
if (table.value.length > 1) model.value = table.value.filter((_, i) => i !== index);
}
function removeColumn(index) {
if ((table.value[0]?.length ?? 1) > 1) model.value = table.value.map((row) => row.filter((_, i) => i !== index));
}
// Dateien (Downloads)
const fileInput = ref(null);
const files = computed(() => Object.fromEntries(editor.files.value.map((file) => [file.id, file])));
async function uploadFiles(event) {
const chosen = [...(event.target.files ?? [])];
event.target.value = '';
uploading.value = true;
try {
for (const file of chosen) {
const result = await editor.uploadFile(file);
model.value = [...(model.value ?? []), result.id];
}
} catch (error) {
toast.error(error.fieldErrors?.file ?? error.message);
} finally {
uploading.value = false;
}
}
const availableFiles = computed(() => editor.files.value.filter((file) => !(model.value ?? []).includes(file.id)));
const formatSize = (bytes) => (bytes > 1024 * 1024 ? `${(bytes / 1024 / 1024).toFixed(1).replace('.', ',')} MB` : `${Math.max(1, Math.round(bytes / 1024))} KB`);
const iconOptions = computed(() => (props.field.choices ?? []).map((choice) => ({ ...choice, icon: icons[choice.value] })));
</script>
<template>
<div class="grid gap-1.5">
<label v-if="field.type !== 'toggle'" :for="id" class="text-[13px] font-medium">{{ field.label }}</label>
<Input v-if="field.type === 'text'" :id="id" v-model="model" :maxlength="field.max" :placeholder="field.placeholder ?? ''" />
<Input v-else-if="field.type === 'url'" :id="id" v-model="model" type="text" inputmode="url" placeholder="https://… oder /seiten/…" />
<Textarea v-else-if="field.type === 'textarea'" :id="id" v-model="model" :maxlength="field.max" rows="3" />
<RichTextEditor v-else-if="field.type === 'html'" v-model="model" bordered min-height="[&_.tiptap]:min-h-24" />
<Input v-else-if="field.type === 'number'" :id="id" v-model.number="model" type="number" :min="field.min" :max="field.max" />
<NativeSelect v-else-if="field.type === 'select'" :id="id" v-model="model" :options="field.choices" />
<label v-else-if="field.type === 'toggle'" :for="id" class="flex items-center justify-between gap-3 py-0.5 text-[13px] font-medium">
{{ field.label }}
<Switch :id="id" v-model="model" />
</label>
<div v-else-if="field.type === 'icon'" class="grid grid-cols-6 gap-1.5">
<button
v-for="option in iconOptions"
:key="option.value"
type="button"
class="grid aspect-square place-items-center rounded-md border transition-colors"
:class="model === option.value ? 'border-brand-400 bg-brand-soft text-brand-soft-foreground' : 'text-muted-foreground hover:bg-accent'"
:title="option.label"
:aria-pressed="model === option.value"
@click="model = option.value"
>
<component :is="option.icon" class="size-4" />
</button>
</div>
<div v-else-if="field.type === 'image'" class="grid gap-2">
<div v-if="model" class="relative overflow-hidden rounded-md border bg-muted">
<img :src="editor.imageUrl(model)" alt="" class="aspect-video w-full object-cover" />
<button type="button" class="absolute top-1.5 right-1.5 grid size-7 place-items-center rounded-md bg-card/90 shadow-sm hover:bg-card" title="Bild entfernen" @click="model = null">
<X class="size-4" />
</button>
</div>
<button
type="button"
class="flex h-9 items-center justify-center gap-2 rounded-md border border-dashed text-[13px] font-medium text-muted-foreground hover:border-brand-300 hover:text-foreground"
:disabled="uploading"
@click="imageInput?.click()"
>
<LoaderCircle v-if="uploading" class="size-4 animate-spin" />
<ImagePlus v-else class="size-4" />
{{ model ? 'Anderes Bild wählen' : 'Bild hochladen' }}
</button>
<input ref="imageInput" type="file" accept="image/jpeg,image/png,image/webp,image/gif" class="hidden" @change="uploadImage" />
</div>
<div v-else-if="field.type === 'people'" class="grid gap-2">
<ul v-if="(model ?? []).length" class="grid gap-1">
<li v-for="(personId, index) in model" :key="personId" class="flex items-center gap-2 rounded-md border bg-card px-2 py-1.5">
<PersonAvatar v-if="peopleCache[personId]" :person="peopleCache[personId]" class="size-7 text-[10px]" />
<span class="min-w-0 flex-1 truncate text-[13px]">{{ peopleCache[personId]?.name ?? '…' }}</span>
<button type="button" class="grid size-6 place-items-center rounded hover:bg-accent" title="Nach oben" @click="moveItem(model, index, -1)"><ArrowUp class="size-3.5" /></button>
<button type="button" class="grid size-6 place-items-center rounded hover:bg-accent" title="Entfernen" @click="removeItem(index)"><X class="size-3.5" /></button>
</li>
</ul>
<div class="relative">
<Search class="pointer-events-none absolute top-1/2 left-2.5 size-4 -translate-y-1/2 text-muted-foreground" />
<Input :id="id" v-model="peopleQuery" class="pl-8" placeholder="Person suchen …" autocomplete="off" />
<ul v-if="peopleResults.length" class="absolute inset-x-0 top-full z-30 mt-1 max-h-60 overflow-y-auto rounded-md border bg-popover p-1 shadow-md">
<li v-for="person in peopleResults" :key="person.id">
<button type="button" class="flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-[13px] hover:bg-accent" @click="addPerson(person)">
<PersonAvatar :person="person" class="size-7 text-[10px]" />
<span class="min-w-0"><span class="block truncate font-medium">{{ person.name }}</span><span class="block truncate text-xs text-muted-foreground">{{ person.job_title }}</span></span>
</button>
</li>
</ul>
</div>
</div>
<div v-else-if="field.type === 'table'" class="grid gap-2">
<div class="overflow-x-auto rounded-md border">
<table class="w-full text-[13px]">
<tbody>
<tr v-for="(row, rowIndex) in table" :key="rowIndex" class="border-b last:border-0">
<td v-for="(cell, colIndex) in row" :key="colIndex" class="border-r p-0 last:border-r-0">
<input
:value="cell"
class="h-8 w-full min-w-20 bg-transparent px-2 focus:bg-brand-soft/50 focus:outline-none"
:aria-label="`Zeile ${rowIndex + 1}, Spalte ${colIndex + 1}`"
@input="updateCell(rowIndex, colIndex, $event.target.value)"
/>
</td>
<td class="w-7 p-0 text-center"><button type="button" class="grid size-7 place-items-center text-muted-foreground hover:text-destructive" title="Zeile entfernen" @click="removeRow(rowIndex)"><X class="size-3.5" /></button></td>
</tr>
<tr>
<td v-for="(_, colIndex) in table[0]" :key="colIndex" class="p-0 text-center">
<button type="button" class="mx-auto grid size-7 place-items-center text-muted-foreground hover:text-destructive" title="Spalte entfernen" @click="removeColumn(colIndex)"><X class="size-3.5" /></button>
</td>
<td />
</tr>
</tbody>
</table>
</div>
<div class="flex gap-2">
<button type="button" class="inline-flex items-center gap-1 text-[13px] font-medium text-brand-text hover:underline" @click="addRow"><Plus class="size-3.5" /> Zeile</button>
<button type="button" class="inline-flex items-center gap-1 text-[13px] font-medium text-brand-text hover:underline" @click="addColumn"><Plus class="size-3.5" /> Spalte</button>
</div>
</div>
<div v-else-if="field.type === 'repeater'" class="grid gap-2">
<details v-for="(entry, index) in model ?? []" :key="index" class="group rounded-md border bg-card" :open="index === (model ?? []).length - 1">
<summary class="flex cursor-pointer list-none items-center gap-2 px-3 py-2 text-[13px] font-medium">
<span class="min-w-0 flex-1 truncate">{{ entry.label || entry.title || entry.alt || `${field.itemLabel} ${index + 1}` }}</span>
<button type="button" class="grid size-6 place-items-center rounded hover:bg-accent" title="Nach oben" @click.prevent="moveItem(model, index, -1)"><ArrowUp class="size-3.5" /></button>
<button type="button" class="grid size-6 place-items-center rounded hover:bg-accent" title="Nach unten" @click.prevent="moveItem(model, index, 1)"><ArrowDown class="size-3.5" /></button>
<button type="button" class="grid size-6 place-items-center rounded text-destructive hover:bg-destructive/10" title="Entfernen" @click.prevent="removeItem(index)"><Trash2 class="size-3.5" /></button>
</summary>
<div class="grid gap-3 border-t px-3 py-3">
<FieldInput v-for="sub in field.fields" :id="`${id}-${index}-${sub.name}`" :key="sub.name" v-model="entry[sub.name]" :field="sub" />
</div>
</details>
<button
v-if="(model ?? []).length < field.max"
type="button"
class="flex h-8 items-center justify-center gap-1.5 rounded-md border border-dashed text-[13px] font-medium text-muted-foreground hover:border-brand-300 hover:text-foreground"
@click="addRepeaterItem"
>
<Plus class="size-3.5" /> {{ field.itemLabel }} hinzufügen
</button>
</div>
<div v-else-if="field.type === 'ids' && field.kind === 'page-file'" class="grid gap-2">
<ul v-if="(model ?? []).length" class="grid gap-1">
<li v-for="(fileId, index) in model" :key="fileId" class="flex items-center gap-2 rounded-md border bg-card px-2 py-1.5 text-[13px]">
<FileDown class="size-4 shrink-0 text-muted-foreground" />
<span class="min-w-0 flex-1 truncate">{{ files[fileId]?.name ?? 'Datei nicht mehr vorhanden' }}</span>
<span v-if="files[fileId]" class="shrink-0 text-xs text-muted-foreground">{{ formatSize(files[fileId].size) }}</span>
<button type="button" class="grid size-6 place-items-center rounded hover:bg-accent" title="Nach oben" @click="moveItem(model, index, -1)"><ArrowUp class="size-3.5" /></button>
<button type="button" class="grid size-6 place-items-center rounded hover:bg-accent" title="Entfernen" @click="removeItem(index)"><X class="size-3.5" /></button>
</li>
</ul>
<button
type="button"
class="flex h-9 items-center justify-center gap-2 rounded-md border border-dashed text-[13px] font-medium text-muted-foreground hover:border-brand-300 hover:text-foreground"
:disabled="uploading"
@click="fileInput?.click()"
>
<LoaderCircle v-if="uploading" class="size-4 animate-spin" />
<Upload v-else class="size-4" />
Dateien hochladen
</button>
<input ref="fileInput" type="file" multiple class="hidden" @change="uploadFiles" />
<NativeSelect
v-if="availableFiles.length"
:model-value="null"
:options="availableFiles.map((file) => ({ value: file.id, label: file.name }))"
placeholder="Bereits hochgeladene Datei wählen …"
@update:model-value="(value) => value && (model = [...(model ?? []), value])"
/>
</div>
<p v-if="field.help" class="text-xs text-muted-foreground">{{ field.help }}</p>
</div>
</template>