Add a history of posts and pages with restore

This commit is contained in:
Matthijs Iriks committed 2026-12-04 05:29:34 +01:00
1 parent eb37a7c6fb
commit 6fdb02040b
20 files changed
+482 -8

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { api, boot } from '../api';
import { t } from '../i18n';
import { flash, reload } from '../state';
// The history of a post, a page or another record: what was saved when and by whom, and a way back.
const props = defineProps<{ type: string; id: number }>();
interface Revision { id: number; created_at: string; user: string | null; changed: string[]; current: boolean; values: Record<string, string> }
const revisions = ref<Revision[]>([]);
const canRestore = ref(false);
const open = ref<number | null>(null);
const loading = ref(true);
const base = `${boot.base}/revisions/${props.type}/${props.id}`;
onMounted(async () => {
try {
const data = await api.get(base);
revisions.value = data.revisions;
canRestore.value = data.can_restore;
} finally {
loading.value = false;
}
});
async function restore(revision: Revision): Promise<void> {
if (!window.confirm(t('Put this version back? The text now is kept in the history.'))) {
return;
}
await api.post(`${base}/${revision.id}/restore`);
flash(t('The earlier version is back.'));
await reload();
}
const when = (value: string) => new Date(value).toLocaleString(boot.locale || undefined);
</script>
<template>
<section class="m-card mt-6 max-w-3xl p-5">
<h2 class="font-display text-lg font-bold">{{ t('History') }}</h2>
<p v-if="loading" class="mt-2 text-sm text-muted">{{ t('Loading…') }}</p>
<p v-else-if="!revisions.length" class="mt-2 text-sm text-muted">{{ t('Nothing here yet.') }}</p>
<ul v-else class="mt-3 divide-y divide-line">
<li v-for="revision in revisions" :key="revision.id" class="py-3">
<div class="flex flex-wrap items-center justify-between gap-2">
<button type="button" class="text-left text-sm" :aria-expanded="open === revision.id" @click="open = open === revision.id ? null : revision.id">
<span class="font-semibold">{{ when(revision.created_at) }}</span>
<span v-if="revision.user" class="text-muted"> · {{ revision.user }}</span>
<span v-if="revision.current" class="m-badge ml-2">{{ t('Now') }}</span>
<span class="block text-xs text-muted">{{ revision.changed.join(', ') }}</span>
</button>
<button v-if="canRestore && !revision.current" type="button" class="m-btn m-btn-secondary" @click="restore(revision)">{{ t('Put back') }}</button>
</div>
<dl v-if="open === revision.id" class="mt-2 space-y-2 text-sm">
<div v-for="(value, key) in revision.values" :key="key">
<dt class="text-xs font-semibold uppercase tracking-wide text-muted">{{ key }}</dt>
<dd class="whitespace-pre-wrap break-words">{{ value }}</dd>
</div>
</dl>
</li>
</ul>
</section>
</template>
+1 -1
View File
@@ -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', 'New': 'Nieuw', '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…',
'Featured': 'Uitgelicht', 'Publish': 'Publiceren', 'Publish at': 'Publiceren op', 'Menu': 'Menu', 'History': 'Geschiedenis', 'Now': 'Nu', 'Put back': 'Terugzetten', 'Put this version back? The text now is kept in the history.': 'Deze versie terugzetten? De tekst van nu blijft bewaard in de geschiedenis.', 'The earlier version is back.': 'De eerdere versie is terug.', 'New': 'Nieuw', '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',
+3 -1
View File
@@ -2,12 +2,13 @@
import { api, boot, useForm } from '../api';
import FormFields, { type Field } from '../components/FormFields.vue';
import Link from '../components/Link.vue';
import RevisionsPanel from '../components/RevisionsPanel.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[];
id: number | null; slug: string; title: string; meta_description: string; defined: boolean; fields: Field[];
content: Record<string, any> | null; body: string | null; is_published: boolean | null;
};
}>();
@@ -65,4 +66,5 @@ async function remove(): Promise<void> {
<button v-if="!page.defined" type="button" class="m-btn m-btn-danger" @click="remove">{{ t('Delete') }}</button>
</div>
</form>
<RevisionsPanel v-if="page.id && boot.permissions.includes('revisions.view')" type="pages" :id="page.id" />
</template>
+2
View File
@@ -3,6 +3,7 @@ import { boot, useForm } from '../api';
import { ref } from 'vue';
import Link from '../components/Link.vue';
import MediaPicker from '../components/MediaPicker.vue';
import RevisionsPanel from '../components/RevisionsPanel.vue';
import { t } from '../i18n';
import { flash, navigate } from '../state';
@@ -88,5 +89,6 @@ async function submit(): Promise<void> {
</div>
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ form.busy ? t('Saving…') : t('Save') }}</button>
</form>
<RevisionsPanel v-if="post && boot.permissions.includes('revisions.view')" type="posts" :id="post.id" />
<MediaPicker v-if="picking" @pick="(url) => { form.values.cover_image = url; picking = false; }" @close="picking = false" />
</template>