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>
|
||||
+1
-1
@@ -11,7 +11,7 @@ const nl: Record<string, string> = {
|
||||
'Put online': 'Online zetten', 'Take offline': 'Offline halen', 'Draft': 'Concept', 'Scheduled': 'Ingepland', 'Published': 'Gepubliceerd',
|
||||
'Are you sure?': 'Weet je het zeker?', 'No posts yet.': 'Nog geen berichten.', 'Address (slug)': 'Adres (slug)', 'Summary': 'Samenvatting',
|
||||
'Text (Markdown)': 'Tekst (Markdown)', 'Cover image': 'Omslagafbeelding', 'Tags (comma separated)': 'Tags (met komma\'s)',
|
||||
'Featured': 'Uitgelicht', 'Publish': 'Publiceren', 'Publish at': 'Publiceren op', 'Menu': 'Menu', 'Show everything': 'Alles tonen', 'Simple view': 'Simpele weergave', 'Loading…': 'Laden…',
|
||||
'Featured': 'Uitgelicht', 'Publish': 'Publiceren', 'Publish at': 'Publiceren op', 'Menu': 'Menu', 'New page': 'Nieuwe pagina', 'Description for search engines': 'Omschrijving voor zoekmachines', 'One per line.': 'Eén per regel.', 'Show everything': 'Alles tonen', 'Simple view': 'Simpele weergave', 'Loading…': 'Laden…',
|
||||
'You may not open this page.': 'Je mag deze pagina niet openen.', 'Something went wrong. Try again.': 'Er ging iets mis. Probeer het opnieuw.',
|
||||
'This page has no screen in the manager yet.': 'Deze pagina heeft nog geen scherm in de manager.', 'Go to the dashboard': 'Naar het dashboard',
|
||||
'The page expired. Reload it and try again.': 'De pagina is verlopen. Laad hem opnieuw en probeer het nog eens.', 'Back': 'Terug',
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<script setup lang="ts">
|
||||
import { api, boot, useForm } from '../api';
|
||||
import FormFields, { type Field } from '../components/FormFields.vue';
|
||||
import Link from '../components/Link.vue';
|
||||
import { t } from '../i18n';
|
||||
import { flash, navigate } from '../state';
|
||||
|
||||
const props = defineProps<{
|
||||
page: {
|
||||
slug: string; title: string; meta_description: string; defined: boolean; fields: Field[];
|
||||
content: Record<string, any> | null; body: string | null; is_published: boolean | null;
|
||||
};
|
||||
}>();
|
||||
|
||||
const page = props.page;
|
||||
const form = useForm({
|
||||
title: page.title,
|
||||
meta_description: page.meta_description ?? '',
|
||||
...(page.defined ? { content: { ...page.content } } : { body: page.body ?? '', is_published: page.is_published ?? false }),
|
||||
} as Record<string, any>);
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
if (await form.send('put', `${boot.base}/pages/${page.slug}`)) {
|
||||
flash(t('Saved.'));
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(): Promise<void> {
|
||||
if (!window.confirm(t('Are you sure?'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
await api.delete(`${boot.base}/pages/${page.slug}`);
|
||||
void navigate(`${boot.base}/pages`);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ page.title }}</h1>
|
||||
<Link :href="boot.base + '/pages'" class="m-btn m-btn-secondary">{{ t('Back') }}</Link>
|
||||
</div>
|
||||
<form class="m-card max-w-3xl space-y-5 p-5" @submit.prevent="submit">
|
||||
<div>
|
||||
<label for="title" class="m-label">{{ t('Title') }}</label>
|
||||
<input id="title" v-model="form.values.title" required maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.title" class="m-error">{{ form.errors.title }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label for="meta" class="m-label">{{ t('Description for search engines') }}</label>
|
||||
<input id="meta" v-model="form.values.meta_description" maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.meta_description" class="m-error">{{ form.errors.meta_description }}</p>
|
||||
</div>
|
||||
<FormFields v-if="page.defined" :fields="page.fields" :values="form.values.content" :errors="form.errors" prefix="content." />
|
||||
<template v-else>
|
||||
<div>
|
||||
<label for="body" class="m-label">{{ t('Text (Markdown)') }}</label>
|
||||
<textarea id="body" v-model="form.values.body" rows="14" class="m-input font-mono" />
|
||||
<p v-if="form.errors.body" class="m-error">{{ form.errors.body }}</p>
|
||||
</div>
|
||||
<label class="flex items-center gap-2 text-sm"><input v-model="form.values.is_published" type="checkbox" /> {{ t('Publish') }}</label>
|
||||
</template>
|
||||
<div class="flex gap-3">
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ form.busy ? t('Saving…') : t('Save') }}</button>
|
||||
<button v-if="!page.defined" type="button" class="m-btn m-btn-danger" @click="remove">{{ t('Delete') }}</button>
|
||||
</div>
|
||||
</form>
|
||||
</template>
|
||||
@@ -0,0 +1,46 @@
|
||||
<script setup lang="ts">
|
||||
import { boot, useForm } from '../api';
|
||||
import Link from '../components/Link.vue';
|
||||
import { t } from '../i18n';
|
||||
import { navigate } from '../state';
|
||||
|
||||
defineProps<{ pages: { slug: string; title: string; defined: boolean; saved: boolean; is_published?: boolean; updated_at: string | null }[] }>();
|
||||
|
||||
const form = useForm({ slug: '', title: '' });
|
||||
|
||||
async function create(): Promise<void> {
|
||||
if (await form.send('post', `${boot.base}/pages`)) {
|
||||
void navigate(`${boot.base}/pages/${form.values.slug}`);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1 class="font-display mb-6 text-2xl font-bold">{{ t('Pages') }}</h1>
|
||||
<ul class="m-card divide-y divide-line">
|
||||
<li v-for="page in pages" :key="page.slug" class="flex items-center justify-between gap-3 p-4">
|
||||
<div class="min-w-0">
|
||||
<p class="truncate font-semibold">{{ page.title }}</p>
|
||||
<p class="truncate text-xs text-muted">/{{ page.slug }}</p>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<span v-if="!page.defined" class="m-badge">{{ page.is_published ? t('Published') : t('Draft') }}</span>
|
||||
<Link :href="`${boot.base}/pages/${page.slug}`" class="m-btn m-btn-secondary">{{ t('Edit') }}</Link>
|
||||
</div>
|
||||
</li>
|
||||
<li v-if="!pages.length" class="p-4 text-sm text-muted">{{ t('Nothing here yet.') }}</li>
|
||||
</ul>
|
||||
<form class="m-card mt-6 grid max-w-3xl gap-4 p-5 sm:grid-cols-[1fr_1fr_auto] sm:items-end" @submit.prevent="create">
|
||||
<div>
|
||||
<label for="new-title" class="m-label">{{ t('Title') }}</label>
|
||||
<input id="new-title" v-model="form.values.title" required maxlength="255" class="m-input" />
|
||||
<p v-if="form.errors.title" class="m-error">{{ form.errors.title }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label for="new-slug" class="m-label">{{ t('Address (slug)') }}</label>
|
||||
<input id="new-slug" v-model="form.values.slug" required maxlength="120" class="m-input" />
|
||||
<p v-if="form.errors.slug" class="m-error">{{ form.errors.slug }}</p>
|
||||
</div>
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ t('New page') }}</button>
|
||||
</form>
|
||||
</template>
|
||||
+5
-1
@@ -14,6 +14,8 @@ import MediaLibrary from './pages/MediaLibrary.vue';
|
||||
import AuditLog from './pages/AuditLog.vue';
|
||||
import StatisticsReport from './pages/StatisticsReport.vue';
|
||||
import ListPage from './pages/ListPage.vue';
|
||||
import PageForm from './pages/PageForm.vue';
|
||||
import PagesIndex from './pages/PagesIndex.vue';
|
||||
|
||||
/** The screens that exist. Every other manager address shows `Fallback` until its module gets a screen. */
|
||||
const routes: { pattern: RegExp; component: any; guest?: boolean; data?: boolean; source?: string }[] = [
|
||||
@@ -29,7 +31,9 @@ const routes: { pattern: RegExp; component: any; guest?: boolean; data?: boolean
|
||||
{ pattern: /^\/accounts$/, component: AccountsIndex, data: true },
|
||||
{ pattern: /^\/groups$/, component: GroupsIndex, data: true },
|
||||
{ pattern: /^\/labels$/, component: LabelsIndex, data: true },
|
||||
{ pattern: /^\/(pages|reviews)$/, component: ListPage, data: true },
|
||||
{ pattern: /^\/pages$/, component: PagesIndex, data: true },
|
||||
{ pattern: /^\/pages\/[a-z0-9]+(?:-[a-z0-9]+)*$/, component: PageForm, data: true },
|
||||
{ pattern: /^\/reviews$/, component: ListPage, data: true },
|
||||
];
|
||||
|
||||
export const state = reactive({
|
||||
|
||||
Reference in new issue
Block a user