Add resources defined in config and a bundle of the site's own screens
This commit is contained in:
1 parent
37a29d9195
commit
3345797141
35 files changed
+905
-19
No files matched your search
@@ -29,6 +29,7 @@ export interface Boot {
|
||||
permissions: string[];
|
||||
navigation: NavItem[];
|
||||
view: 'full' | 'simple';
|
||||
extensions: string[];
|
||||
can_switch: boolean;
|
||||
theme: string;
|
||||
can_login: boolean;
|
||||
|
||||
@@ -57,7 +57,8 @@ watch(() => state.path, () => (open.value = false));
|
||||
<span class="font-display text-lg font-bold">{{ boot.name }}</span>
|
||||
</div>
|
||||
<nav class="flex-1 space-y-0.5 overflow-y-auto px-3 py-3" aria-label="Manager">
|
||||
<template v-for="group in groups" :key="group.title">
|
||||
<template v-for="(group, index) in groups" :key="group.title + index">
|
||||
<hr v-if="!group.title && index > 0" class="my-3 border-line" />
|
||||
<p v-if="group.title" class="px-3 pb-1 pt-4 text-xs font-semibold uppercase tracking-wide text-muted">{{ t(group.title) }}</p>
|
||||
<template v-for="item in group.items" :key="item.href">
|
||||
<a
|
||||
|
||||
+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', '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', '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',
|
||||
|
||||
+27
-6
@@ -1,10 +1,31 @@
|
||||
import { createApp } from 'vue';
|
||||
import * as Vue from 'vue';
|
||||
import App from './App.vue';
|
||||
import { boot } from './api';
|
||||
import { setLocale } from './i18n';
|
||||
import { start } from './state';
|
||||
import { api, boot } from './api';
|
||||
import FormFields from './components/FormFields.vue';
|
||||
import Icon from './components/Icon.vue';
|
||||
import Link from './components/Link.vue';
|
||||
import MediaPicker from './components/MediaPicker.vue';
|
||||
import { setLocale, t } from './i18n';
|
||||
import { flash, navigate, register, start } from './state';
|
||||
import './style.css';
|
||||
|
||||
setLocale(boot.locale);
|
||||
createApp(App).mount('#manager');
|
||||
start();
|
||||
|
||||
// What a site's own bundle (config `manager.extensions`) may use. Its `import 'vue'` is mapped to this very Vue (see
|
||||
// resources/dist/vue.js), so its components share the reactivity of the screens.
|
||||
(window as any).IriksManager = { Vue, boot, api, t, flash, navigate, register, components: { Link, FormFields, Icon, MediaPicker } };
|
||||
|
||||
async function run(): Promise<void> {
|
||||
for (const url of boot.extensions ?? []) {
|
||||
try {
|
||||
await import(/* @vite-ignore */ url);
|
||||
} catch (error) {
|
||||
console.error(`Manager extension ${url} did not load`, error);
|
||||
}
|
||||
}
|
||||
|
||||
Vue.createApp(App).mount('#manager');
|
||||
start();
|
||||
}
|
||||
|
||||
void run();
|
||||
@@ -0,0 +1,35 @@
|
||||
<script setup lang="ts">
|
||||
import { 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<{
|
||||
resource: { key: string; label: string; singular: string; fields: Field[] };
|
||||
record: { id: number | null; values: Record<string, any> };
|
||||
}>();
|
||||
|
||||
const base = `${boot.base}/records/${props.resource.key}`;
|
||||
const form = useForm({ ...props.record.values });
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
const saved = props.record.id === null ? await form.send('post', base) : await form.send('put', `${base}/${props.record.id}`);
|
||||
|
||||
if (saved) {
|
||||
flash(t('Saved.'));
|
||||
void navigate(base);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ record.id === null ? t('New') + ': ' + t(resource.singular) : t(resource.singular) }}</h1>
|
||||
<Link :href="base" 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">
|
||||
<FormFields :fields="resource.fields" :values="form.values" :errors="form.errors" />
|
||||
<button type="submit" class="m-btn m-btn-primary" :disabled="form.busy">{{ form.busy ? t('Saving…') : t('Save') }}</button>
|
||||
</form>
|
||||
</template>
|
||||
@@ -0,0 +1,68 @@
|
||||
<script setup lang="ts">
|
||||
import { api, boot } from '../api';
|
||||
import type { Field } from '../components/FormFields.vue';
|
||||
import Link from '../components/Link.vue';
|
||||
import { t } from '../i18n';
|
||||
import { reload } from '../state';
|
||||
|
||||
const props = defineProps<{
|
||||
resource: { key: string; label: string; singular: string; columns: string[]; fields: Field[]; can_manage: boolean };
|
||||
rows: { id: number; values: Record<string, any> }[];
|
||||
current_page: number;
|
||||
last_page: number;
|
||||
}>();
|
||||
|
||||
const base = `${boot.base}/records/${props.resource.key}`;
|
||||
const labelOf = (key: string) => props.resource.fields.find((field) => field.key === key)?.label ?? key;
|
||||
|
||||
function shown(row: { values: Record<string, any> }, key: string): string {
|
||||
const value = row.values[key];
|
||||
const field = props.resource.fields.find((f) => f.key === key);
|
||||
|
||||
if (field?.type === 'toggle') {
|
||||
return value ? '✓' : '';
|
||||
}
|
||||
|
||||
return field?.type === 'select' ? (field.options[value] ?? value) : String(value ?? '');
|
||||
}
|
||||
|
||||
async function remove(row: { id: number }): Promise<void> {
|
||||
if (!window.confirm(t('Are you sure?'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
await api.delete(`${base}/${row.id}`);
|
||||
await reload();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="mb-6 flex items-center justify-between gap-3">
|
||||
<h1 class="font-display text-2xl font-bold">{{ t(resource.label) }}</h1>
|
||||
<Link v-if="resource.can_manage" :href="base + '/create'" class="m-btn m-btn-primary">{{ t('Add') }}</Link>
|
||||
</div>
|
||||
<p v-if="!rows.length" class="m-card p-5 text-sm text-muted">{{ t('Nothing here yet.') }}</p>
|
||||
<div v-else class="m-card overflow-x-auto">
|
||||
<table class="w-full text-left text-sm">
|
||||
<thead class="border-b border-line text-xs uppercase tracking-wide text-muted">
|
||||
<tr>
|
||||
<th v-for="column in resource.columns" :key="column" class="px-4 py-3 font-semibold">{{ t(labelOf(column)) }}</th>
|
||||
<th v-if="resource.can_manage" class="px-4 py-3" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-line">
|
||||
<tr v-for="row in rows" :key="row.id">
|
||||
<td v-for="column in resource.columns" :key="column" class="max-w-xs truncate px-4 py-3">{{ shown(row, column) }}</td>
|
||||
<td v-if="resource.can_manage" class="flex justify-end gap-2 px-4 py-2">
|
||||
<Link :href="`${base}/${row.id}/edit`" class="m-btn m-btn-secondary">{{ t('Edit') }}</Link>
|
||||
<button type="button" class="m-btn m-btn-danger" @click="remove(row)">{{ t('Delete') }}</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<nav v-if="last_page > 1" class="mt-4 flex gap-2">
|
||||
<Link v-if="current_page > 1" :href="`${base}?page=${current_page - 1}`" class="m-btn m-btn-secondary">{{ t('Previous') }}</Link>
|
||||
<Link v-if="current_page < last_page" :href="`${base}?page=${current_page + 1}`" class="m-btn m-btn-secondary">{{ t('Next') }}</Link>
|
||||
</nav>
|
||||
</template>
|
||||
@@ -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 RecordForm from './pages/RecordForm.vue';
|
||||
import RecordsIndex from './pages/RecordsIndex.vue';
|
||||
import PageForm from './pages/PageForm.vue';
|
||||
import PagesIndex from './pages/PagesIndex.vue';
|
||||
|
||||
@@ -33,9 +35,19 @@ const routes: { pattern: RegExp; component: any; guest?: boolean; data?: boolean
|
||||
{ pattern: /^\/labels$/, component: LabelsIndex, data: true },
|
||||
{ pattern: /^\/pages$/, component: PagesIndex, data: true },
|
||||
{ pattern: /^\/pages\/[a-z0-9]+(?:-[a-z0-9]+)*$/, component: PageForm, data: true },
|
||||
{ pattern: /^\/records\/[a-z][a-z0-9_]*$/, component: RecordsIndex, data: true },
|
||||
{ pattern: /^\/records\/[a-z][a-z0-9_]*\/(create|\d+\/edit)$/, component: RecordForm, data: true },
|
||||
{ pattern: /^\/reviews$/, component: ListPage, data: true },
|
||||
];
|
||||
|
||||
/**
|
||||
* A screen of a site's own bundle (config `manager.extensions`): `pattern` is a regular expression for the path inside the
|
||||
* manager, `data` fetches the same address (or `source`) as JSON and passes it as props. Its screens win over the built in ones.
|
||||
*/
|
||||
export function register(pattern: string, component: any, options: { data?: boolean; source?: string } = {}): void {
|
||||
routes.unshift({ pattern: new RegExp(pattern), component, data: options.data, source: options.source });
|
||||
}
|
||||
|
||||
export const state = reactive({
|
||||
path: '',
|
||||
component: null as any,
|
||||
|
||||
+17
-2
@@ -1,11 +1,26 @@
|
||||
import tailwindcss from '@tailwindcss/vite';
|
||||
import vue from '@vitejs/plugin-vue';
|
||||
import { defineConfig } from 'vite';
|
||||
import { defineConfig, type Plugin } from 'vite';
|
||||
|
||||
// One script and one stylesheet with fixed names: the package serves them itself (resources/dist), so a site needs
|
||||
// no Node. `npm run build` here, and commit the result.
|
||||
// A site's own bundle imports `vue` like any component library would. `vue.js` makes that the Vue of the screens (an import map
|
||||
// in the shell points `vue` at it), by re-exporting what the screens put on `window.IriksManager.Vue`.
|
||||
function vueForExtensions(): Plugin {
|
||||
return {
|
||||
name: 'manager-vue-for-extensions',
|
||||
async generateBundle() {
|
||||
const loaded: Record<string, unknown> = await import('vue');
|
||||
const names = [...new Set([...Object.keys(loaded), ...Object.keys((loaded.default as object) ?? {})])].filter((name) => /^[A-Za-z_$][\w$]*$/.test(name) && name !== 'default');
|
||||
const source = `const Vue = window.IriksManager.Vue;\n${names.map((name) => `export const ${name} = Vue.${name};`).join('\n')}\nexport default Vue;\n`;
|
||||
|
||||
this.emitFile({ type: 'asset', fileName: 'vue.js', source });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [vue(), tailwindcss()],
|
||||
plugins: [vue(), tailwindcss(), vueForExtensions()],
|
||||
build: {
|
||||
outDir: '../resources/dist',
|
||||
emptyOutDir: true,
|
||||
|
||||
Reference in new issue
Block a user