Add pages with fields, a field schema and the screens to edit them
This commit is contained in:
1 parent
ce1a8a1370
commit
517039b625
19 files changed
+660
-13
No files matched your search
@@ -0,0 +1,69 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { boot } from '../api';
|
||||
import { t } from '../i18n';
|
||||
import MediaPicker from './MediaPicker.vue';
|
||||
|
||||
/** One field as the server describes it (src/Support/Fields.php). */
|
||||
export interface Field {
|
||||
key: string;
|
||||
label: string;
|
||||
type: string;
|
||||
help: string | null;
|
||||
required: boolean;
|
||||
options: Record<string, string>;
|
||||
max: number | null;
|
||||
}
|
||||
|
||||
// Draws a form from field definitions: pages use it now, and any record a site defines without code of its own.
|
||||
// `values` is changed in place; `errors` is keyed like the server does (`<prefix><key>`).
|
||||
defineProps<{ fields: Field[]; values: Record<string, any>; errors?: Record<string, string>; prefix?: string }>();
|
||||
|
||||
const picking = ref<string | null>(null);
|
||||
const canPick = boot.permissions.includes('media.view');
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="space-y-5">
|
||||
<div v-for="field in fields" :key="field.key">
|
||||
<label v-if="field.type === 'toggle'" class="flex items-center gap-2 text-sm">
|
||||
<input v-model="values[field.key]" type="checkbox" /> {{ t(field.label) }}
|
||||
</label>
|
||||
<template v-else>
|
||||
<label :for="'f-' + field.key" class="m-label">{{ t(field.label) }}<span v-if="field.required" class="text-muted"> *</span></label>
|
||||
<select v-if="field.type === 'select'" :id="'f-' + field.key" v-model="values[field.key]" class="m-input">
|
||||
<option v-if="!field.required" value="" />
|
||||
<option v-for="(label, value) in field.options" :key="value" :value="value">{{ label }}</option>
|
||||
</select>
|
||||
<textarea
|
||||
v-else-if="field.type === 'textarea' || field.type === 'lines' || field.type === 'markdown'"
|
||||
:id="'f-' + field.key"
|
||||
v-model="values[field.key]"
|
||||
:rows="field.type === 'markdown' ? 12 : field.type === 'lines' ? 5 : 4"
|
||||
:maxlength="field.max ?? undefined"
|
||||
:required="field.required"
|
||||
class="m-input"
|
||||
:class="{ 'font-mono': field.type === 'markdown' }"
|
||||
/>
|
||||
<div v-else-if="field.type === 'media'" class="flex gap-2">
|
||||
<input :id="'f-' + field.key" v-model="values[field.key]" maxlength="2048" class="m-input" />
|
||||
<button v-if="canPick" type="button" class="m-btn m-btn-secondary shrink-0" @click="picking = field.key">{{ t('Choose') }}</button>
|
||||
</div>
|
||||
<input
|
||||
v-else
|
||||
:id="'f-' + field.key"
|
||||
v-model="values[field.key]"
|
||||
:type="field.type === 'number' ? 'number' : field.type === 'date' ? 'date' : 'text'"
|
||||
:maxlength="field.max ?? undefined"
|
||||
:required="field.required"
|
||||
class="m-input"
|
||||
/>
|
||||
<img v-if="field.type === 'media' && values[field.key]" :src="values[field.key]" alt="" class="mt-2 h-24 w-auto rounded-lg border border-line object-cover" />
|
||||
</template>
|
||||
<p v-if="field.type === 'lines'" class="mt-1 text-xs text-muted">{{ t('One per line.') }}</p>
|
||||
<p v-if="field.help" class="mt-1 text-xs text-muted">{{ t(field.help) }}</p>
|
||||
<p v-if="errors?.[(prefix ?? '') + field.key]" class="m-error">{{ errors[(prefix ?? '') + field.key] }}</p>
|
||||
</div>
|
||||
<MediaPicker v-if="picking" @pick="(url) => { values[picking!] = url; picking = null; }" @close="picking = null" />
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user