Make the box canvas reusable: canvas pages, admin-only boxes, landing notes
The /stp canvas is now BoxCanvas.vue, keyed by a page. Boxes carry a
page ("stp", "index" for the landing page's notes overlay, or a Page
slug) and an adminOnly flag that the boxes resolver filters server-side.
A new Page type lets the admin create canvas pages in the browser,
served at /<slug> by the router's catch-all (CanvasPage.vue), starting
from a Blank, Shrine or Notes-board layout. Slugs that an SPA or nginx
route would answer first are refused. Renaming a page moves its boxes;
deleting it deletes them.
The GTO, Evangelion and Skipskipbenben placeholder routes are dropped
in favour of canvas pages listed on /shrines, which the landing page
now links to. The box editor gains an admin-only toggle and an
upload-and-insert button, and there are Note, Heading, Text, Gallery
and Video templates.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
<script setup>
|
||||
/**
|
||||
* The router's catch-all: any path no other route claims is looked up as an
|
||||
* admin-created canvas page (the `Page` GraphQL type) and, if there is one,
|
||||
* rendered as a BoxCanvas whose page key is the slug. Otherwise it is the
|
||||
* site's 404 — with, for an admin, a form to create a page right here.
|
||||
*
|
||||
* Static routes always win, which is why the server refuses slugs under the
|
||||
* SPA's and nginx's own paths (validateSlug in backend/graph/page_helpers.go).
|
||||
*
|
||||
* An adminOnly page answers null to anyone else, so to them it is simply a
|
||||
* 404.
|
||||
*/
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { gql } from "@/graphql";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { PAGE_FIELDS, usePagesStore } from "@/stores/pages";
|
||||
import BoxCanvas from "@/views/home3/BoxCanvas.vue";
|
||||
import NotFound from "@/views/404/404.vue";
|
||||
import PageForm from "./PageForm.vue";
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const auth = useAuthStore();
|
||||
const pages = usePagesStore();
|
||||
|
||||
const isAdmin = computed(() => auth.loggedIn && !!auth.user.admin);
|
||||
|
||||
// pathMatch is the path split on "/"; a trailing slash leaves an empty last
|
||||
// part.
|
||||
const slug = computed(() => {
|
||||
const parts = route.params.pathMatch;
|
||||
return (Array.isArray(parts) ? parts : [parts]).filter(Boolean).join("/");
|
||||
});
|
||||
|
||||
const page = ref(null);
|
||||
const state = ref("loading"); // loading | found | missing | error
|
||||
|
||||
async function load() {
|
||||
const wanted = slug.value;
|
||||
state.value = "loading";
|
||||
try {
|
||||
const data = await gql(`query Page($slug: String!) { page(slug: $slug) { ${PAGE_FIELDS} } }`, {
|
||||
slug: wanted,
|
||||
});
|
||||
if (wanted !== slug.value) return; // navigated on while it loaded
|
||||
page.value = data.page;
|
||||
state.value = data.page ? "found" : "missing";
|
||||
} catch {
|
||||
if (wanted === slug.value) state.value = "error";
|
||||
}
|
||||
}
|
||||
// The session matters too: signing in may reveal an adminOnly page.
|
||||
watch([slug, isAdmin], load, { immediate: true });
|
||||
auth.ensureLoaded();
|
||||
|
||||
// ---- Page settings (admin) -----------------------------------------------
|
||||
const settingsOpen = ref(false);
|
||||
const draft = ref({ title: "", slug: "", adminOnly: false });
|
||||
const settingsError = ref("");
|
||||
|
||||
function openSettings() {
|
||||
draft.value = { title: page.value.title, slug: page.value.slug, adminOnly: page.value.adminOnly };
|
||||
settingsError.value = "";
|
||||
settingsOpen.value = !settingsOpen.value;
|
||||
}
|
||||
|
||||
async function saveSettings() {
|
||||
settingsError.value = "";
|
||||
try {
|
||||
const updated = await pages.update(page.value.id, {
|
||||
title: draft.value.title.trim(),
|
||||
slug: draft.value.slug.trim().replace(/^\/+|\/+$/g, ""),
|
||||
adminOnly: draft.value.adminOnly,
|
||||
});
|
||||
settingsOpen.value = false;
|
||||
if (updated.slug !== page.value.slug) router.replace(`/${updated.slug}`);
|
||||
else page.value = updated;
|
||||
} catch (err) {
|
||||
settingsError.value = err?.message || "Failed to save";
|
||||
}
|
||||
}
|
||||
|
||||
async function deletePage() {
|
||||
if (!confirm(`Delete the page “${page.value.title}” and everything on it?`)) return;
|
||||
try {
|
||||
await pages.remove(page.value.id);
|
||||
router.push("/");
|
||||
} catch (err) {
|
||||
settingsError.value = err?.message || "Failed to delete";
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main v-if="state === 'found'" class="canvas-page">
|
||||
<BoxCanvas :key="page.slug" :page="page.slug">
|
||||
<template #admin-bar>
|
||||
<button type="button" class="bar-btn" :class="{ on: settingsOpen }" @click="openSettings">
|
||||
Page settings
|
||||
</button>
|
||||
<span v-if="page.adminOnly" class="text-tertiary" title="Only admins can see this page">🔒 admin only</span>
|
||||
</template>
|
||||
</BoxCanvas>
|
||||
|
||||
<form v-if="settingsOpen" class="settings bdr-2 bg-surface" @submit.prevent="saveSettings">
|
||||
<label>Title <input v-model="draft.title" type="text" required /></label>
|
||||
<label>Address <input v-model="draft.slug" type="text" required /></label>
|
||||
<label class="check"><input v-model="draft.adminOnly" type="checkbox" /> Only admins can see this page</label>
|
||||
<div class="flex gap-2">
|
||||
<button type="submit" class="bar-btn">Save</button>
|
||||
<button type="button" class="bar-btn" @click="settingsOpen = false">Cancel</button>
|
||||
<button type="button" class="bar-btn danger" @click="deletePage">Delete page</button>
|
||||
</div>
|
||||
<p v-if="settingsError" class="text-tertiary">{{ settingsError }}</p>
|
||||
</form>
|
||||
</main>
|
||||
|
||||
<p v-else-if="state === 'error'" class="state">Couldn't load this page.</p>
|
||||
|
||||
<template v-else-if="state === 'missing'">
|
||||
<section v-if="isAdmin" class="create bdr-2 bg-surface">
|
||||
<h3>No page here yet — make one?</h3>
|
||||
<PageForm :slug="slug" @created="load" />
|
||||
</section>
|
||||
<NotFound />
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.canvas-page {
|
||||
width: 100%;
|
||||
min-height: 100%;
|
||||
box-sizing: border-box;
|
||||
padding: 10px 16px;
|
||||
}
|
||||
.state {
|
||||
text-align: center;
|
||||
color: var(--color-muted);
|
||||
}
|
||||
|
||||
.bar-btn {
|
||||
padding: 0.25rem 0.75rem;
|
||||
color: var(--color-primary);
|
||||
background: var(--color-link-bg);
|
||||
border: 2px solid var(--color-quaternary);
|
||||
font-family: var(--font-heading);
|
||||
font-size: 1.1rem;
|
||||
letter-spacing: 0.025em;
|
||||
cursor: pointer;
|
||||
}
|
||||
.bar-btn:hover,
|
||||
.bar-btn.on {
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
.bar-btn.danger:hover {
|
||||
border-color: var(--color-tertiary);
|
||||
color: var(--color-tertiary);
|
||||
}
|
||||
|
||||
.settings,
|
||||
.create {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
max-width: 420px;
|
||||
margin: 0 auto 10px;
|
||||
padding: 0.75rem;
|
||||
}
|
||||
.settings {
|
||||
position: fixed;
|
||||
top: 80px;
|
||||
left: 16px;
|
||||
z-index: 65;
|
||||
}
|
||||
.settings label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
color: var(--color-muted);
|
||||
}
|
||||
.settings label.check {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
gap: 0.4rem;
|
||||
}
|
||||
.settings label.check input {
|
||||
width: auto;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,98 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Create a canvas page: its address, title, a starting layout from
|
||||
* pageStarters.js, and whether only admins may see it. On success it opens
|
||||
* the new page and emits `created`. Used on the admin panel's Pages tab, on /shrines, and on the
|
||||
* 404 an admin sees at an address that has no page yet.
|
||||
*/
|
||||
import { ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import Button from "@/components/input/Button.vue";
|
||||
import { usePagesStore } from "@/stores/pages";
|
||||
import { useBoxesStore } from "@/stores/boxes";
|
||||
import { STARTERS } from "./pageStarters";
|
||||
|
||||
const props = defineProps({
|
||||
// Pre-filled address, e.g. the path of the 404 being looked at.
|
||||
slug: { type: String, default: "" },
|
||||
// Fixed leading part of the address, e.g. "shrines/" on /shrines.
|
||||
prefix: { type: String, default: "" },
|
||||
starter: { type: String, default: "blank" },
|
||||
});
|
||||
|
||||
const emit = defineEmits(["created"]);
|
||||
const router = useRouter();
|
||||
const pages = usePagesStore();
|
||||
|
||||
const slug = ref(props.slug);
|
||||
const title = ref("");
|
||||
const starter = ref(props.starter);
|
||||
const adminOnly = ref(false);
|
||||
const busy = ref(false);
|
||||
const error = ref("");
|
||||
|
||||
async function submit() {
|
||||
const fullSlug = props.prefix + slug.value.trim().replace(/^\/+|\/+$/g, "");
|
||||
if (!title.value.trim()) {
|
||||
error.value = "Title is required";
|
||||
return;
|
||||
}
|
||||
error.value = "";
|
||||
busy.value = true;
|
||||
try {
|
||||
const page = await pages.create({ slug: fullSlug, title: title.value.trim(), adminOnly: adminOnly.value });
|
||||
// Page first, then its boxes: a starter that fails halfway leaves a page
|
||||
// with some boxes, which is easy to finish by hand, rather than boxes
|
||||
// under an address with no page.
|
||||
const boxes = useBoxesStore(page.slug);
|
||||
for (const box of STARTERS.find((s) => s.id === starter.value).boxes(page.title)) {
|
||||
await boxes.create(box);
|
||||
}
|
||||
// On the 404 of the address just created the push is a no-op, since the
|
||||
// route is unchanged; `created` is what tells that view to reload.
|
||||
await router.push(`/${page.slug}`);
|
||||
emit("created", page);
|
||||
} catch (err) {
|
||||
error.value = err?.message || "Failed to create the page";
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form class="page-form flex flex-col gap-1" @submit.prevent="submit">
|
||||
<label class="flex items-center gap-1">
|
||||
<span class="text-muted">/{{ prefix }}</span>
|
||||
<input v-model="slug" type="text" placeholder="address, e.g. evangelion" required class="flex-1" />
|
||||
</label>
|
||||
<input v-model="title" type="text" placeholder="Title (required)" />
|
||||
<select v-model="starter">
|
||||
<option v-for="s in STARTERS" :key="s.id" :value="s.id">Start from: {{ s.label }}</option>
|
||||
</select>
|
||||
<label class="check">
|
||||
<input v-model="adminOnly" type="checkbox" />
|
||||
Only admins can see this page
|
||||
</label>
|
||||
<Button type="submit" :disabled="busy">{{ busy ? "Creating…" : "Create page" }}</Button>
|
||||
<p v-if="error" class="text-tertiary">{{ error }}</p>
|
||||
</form>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.page-form select {
|
||||
padding: 0.5rem;
|
||||
color: var(--color-primary);
|
||||
background: var(--color-surface-deep);
|
||||
border: 1px solid var(--color-primary);
|
||||
}
|
||||
.check {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
color: var(--color-secondary);
|
||||
}
|
||||
.check input {
|
||||
width: auto;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Starting layouts for a new canvas page, offered when one is created
|
||||
* (PageForm.vue). Each `boxes(title)` returns box inputs for
|
||||
* stores/boxes.js — without `page`, which the store adds. Everything is an
|
||||
* ordinary box afterwards: move it, edit it, delete it.
|
||||
*/
|
||||
import { galleryHtml, imageHtml } from "@/views/home3/templates";
|
||||
|
||||
const escape = (s) =>
|
||||
s.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
||||
|
||||
const html = (title, body, geom, extra = {}) => ({ kind: "html", title, html: body, ...geom, ...extra });
|
||||
|
||||
function shrine(title) {
|
||||
const t = escape(title);
|
||||
return [
|
||||
html(
|
||||
"Heading",
|
||||
`<style>
|
||||
body { display: grid; place-items: center; text-align: center; }
|
||||
h1 { margin: 0; font-size: 4.5rem; line-height: 1; }
|
||||
p { margin: .3rem 0 0; color: var(--color-tertiary); }
|
||||
</style>
|
||||
<div>
|
||||
<h1>${t}</h1>
|
||||
<p>a shrine</p>
|
||||
</div>`,
|
||||
{ x: 300, y: 20, w: 800, h: 160 },
|
||||
{ framed: false },
|
||||
),
|
||||
html(
|
||||
"Main image",
|
||||
imageHtml("/img/memes/epic.jpeg"),
|
||||
{ x: 300, y: 200, w: 380, h: 380 },
|
||||
{ framed: false },
|
||||
),
|
||||
html(
|
||||
"About",
|
||||
`<h3>Why ${t}</h3>
|
||||
<p>What it is, and why it gets a shrine.</p>
|
||||
<h3>Favourite moments</h3>
|
||||
<ul>
|
||||
<li>…</li>
|
||||
</ul>`,
|
||||
{ x: 700, y: 200, w: 400, h: 380 },
|
||||
),
|
||||
html(
|
||||
"Gallery",
|
||||
galleryHtml(["/img/memes/fire-woman.gif", "/img/memes/mix.gif", "/img/memes/lol.jpg"]),
|
||||
{ x: 300, y: 600, w: 800, h: 320 },
|
||||
),
|
||||
html(
|
||||
"Quote",
|
||||
`<style>
|
||||
body { display: grid; place-items: center; text-align: center; font-style: italic; }
|
||||
</style>
|
||||
<blockquote>“A line worth remembering.”</blockquote>`,
|
||||
{ x: 300, y: 940, w: 800, h: 120 },
|
||||
),
|
||||
].map((box, i) => ({ z: i + 1, ...box }));
|
||||
}
|
||||
|
||||
function notes() {
|
||||
const note = (text, x, y) =>
|
||||
html(
|
||||
"Note",
|
||||
`<style>
|
||||
body { padding: 0; }
|
||||
.note { box-sizing: border-box; height: 100%; padding: .75rem 1rem; background: #f6e27a;
|
||||
color: #2b2610; box-shadow: 2px 3px 0 rgba(0, 0, 0, .35); overflow: auto; }
|
||||
.note h3 { margin: 0 0 .3rem; color: inherit; }
|
||||
</style>
|
||||
<div class="note"><h3>${text}</h3><ul><li>…</li></ul></div>`,
|
||||
{ x, y, w: 260, h: 220 },
|
||||
{ framed: false, adminOnly: true },
|
||||
);
|
||||
return [note("To do", 40, 40), note("Ideas", 330, 40), note("Bugs", 620, 40)].map((box, i) => ({
|
||||
z: i + 1,
|
||||
...box,
|
||||
}));
|
||||
}
|
||||
|
||||
export const STARTERS = [
|
||||
{ id: "blank", label: "Blank", boxes: () => [] },
|
||||
{ id: "shrine", label: "Shrine", boxes: shrine },
|
||||
{ id: "notes", label: "Notes board (admin-only notes)", boxes: notes },
|
||||
];
|
||||
Reference in New Issue
Block a user