Add per-page backgrounds to canvas pages
Each canvas page key (the landing page, /stp, shrines and other admin-made pages) can have its own background: a colour, an image and how the image fills the window. Admins set it from a Background button beside Edit layout, with live preview and image upload or drop; DefaultLayout paints it behind the whole window and falls back to the halftone. Backed by a new Background GraphQL type keyed by page key, validated server-side, hidden with admin-only pages, and moved or deleted along with its page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -6,14 +6,21 @@
|
||||
* assets/styles.css). `:key="$route.path"` forces a full remount on every path
|
||||
* change so the transition actually runs even between two routes that share a
|
||||
* component; `mode="out-in"` stops the outgoing and incoming pages overlapping.
|
||||
*
|
||||
* The background is the halftone unless the page on screen has one of its
|
||||
* own (stores/background.js — set per canvas page, landing page and shrines
|
||||
* included). The inline style overrides the halftone's layers.
|
||||
*/
|
||||
import { RouterView } from "vue-router";
|
||||
import { useBackgroundStore } from "@/stores/background";
|
||||
import Navbar from "@/components/Navbar.vue";
|
||||
import Footer from "@/components/Footer.vue";
|
||||
|
||||
const background = useBackgroundStore();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="default-layout halftone">
|
||||
<div class="default-layout halftone" :style="background.style">
|
||||
<Navbar class="no-print sticky top-0 z-50" />
|
||||
<main class="default-content">
|
||||
<RouterView v-slot="{ Component }">
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Page backgrounds (the `Background` GraphQL type), one per canvas page key —
|
||||
* "index" for the landing page, "stp", or a Page slug such as
|
||||
* "shrines/evangelion" — and which one is on screen.
|
||||
*
|
||||
* DefaultLayout paints `style` behind everything, navbar included, so a
|
||||
* background covers the whole window rather than just the canvas. Each
|
||||
* BoxCanvas calls show(page) when it mounts and hide(page) when it goes;
|
||||
* a page with no background (null) keeps the theme's halftone.
|
||||
*
|
||||
* `preview` is BackgroundEditor's unsaved draft: while it is set for the page
|
||||
* on screen, it is what the layout paints, so every change shows at once.
|
||||
*/
|
||||
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, ref } from "vue";
|
||||
import { gql } from "@/graphql";
|
||||
|
||||
const FIELDS = "id page color image fit";
|
||||
|
||||
export const FITS = [
|
||||
{ id: "cover", label: "Fill the window" },
|
||||
{ id: "contain", label: "Fit inside the window" },
|
||||
{ id: "tile", label: "Tile" },
|
||||
];
|
||||
|
||||
/** CSS for a background, or null for the theme default. */
|
||||
export function backgroundStyle(bg) {
|
||||
if (!bg || (!bg.color && !bg.image)) return null;
|
||||
const style = {
|
||||
backgroundColor: bg.color || "var(--color-surface-deep)",
|
||||
// "none" as well as a URL: either way the halftone's own layers go.
|
||||
backgroundImage: bg.image ? `url("${bg.image}")` : "none",
|
||||
};
|
||||
if (bg.fit === "tile") {
|
||||
Object.assign(style, { backgroundSize: "auto", backgroundRepeat: "repeat", backgroundPosition: "0 0" });
|
||||
} else {
|
||||
// Pinned to the window, so a tall page scrolls over one picture rather
|
||||
// than stretching it down the whole page.
|
||||
Object.assign(style, {
|
||||
backgroundSize: bg.fit,
|
||||
backgroundRepeat: "no-repeat",
|
||||
backgroundPosition: "center",
|
||||
backgroundAttachment: "fixed",
|
||||
});
|
||||
}
|
||||
return style;
|
||||
}
|
||||
|
||||
export const useBackgroundStore = defineStore("background", () => {
|
||||
// page key -> Background or null; a key not present hasn't been loaded.
|
||||
const byPage = ref({});
|
||||
const active = ref(null);
|
||||
const preview = ref(null); // { page, color, image, fit }
|
||||
|
||||
const current = computed(() =>
|
||||
preview.value && preview.value.page === active.value ? preview.value : byPage.value[active.value],
|
||||
);
|
||||
const style = computed(() => backgroundStyle(current.value));
|
||||
|
||||
async function load(page) {
|
||||
try {
|
||||
const data = await gql(`query Background($page: String!) { background(page: $page) { ${FIELDS} } }`, {
|
||||
page,
|
||||
});
|
||||
byPage.value[page] = data.background;
|
||||
} catch (err) {
|
||||
// The theme default is a fine fallback; the page itself still loads.
|
||||
console.warn(`Couldn't load the background for ${page}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
function show(page) {
|
||||
active.value = page;
|
||||
if (!(page in byPage.value)) load(page);
|
||||
}
|
||||
|
||||
function hide(page) {
|
||||
if (active.value === page) active.value = null;
|
||||
if (preview.value?.page === page) preview.value = null;
|
||||
}
|
||||
|
||||
async function save(page, { color, image, fit }) {
|
||||
const data = await gql(
|
||||
`mutation SetBackground($page: String!, $input: BackgroundInput!) {
|
||||
setBackground(page: $page, input: $input) { ${FIELDS} }
|
||||
}`,
|
||||
{ page, input: { color, image, fit } },
|
||||
);
|
||||
byPage.value[page] = data.setBackground;
|
||||
return data.setBackground;
|
||||
}
|
||||
|
||||
async function clear(page) {
|
||||
await gql(`mutation ClearBackground($page: String!) { clearBackground(page: $page) }`, { page });
|
||||
byPage.value[page] = null;
|
||||
}
|
||||
|
||||
return { byPage, active, preview, current, style, load, show, hide, save, clear };
|
||||
});
|
||||
@@ -0,0 +1,228 @@
|
||||
<script setup>
|
||||
/**
|
||||
* The admin's background panel for one canvas page, opened from BoxCanvas's
|
||||
* "Background" button: a colour, an image (uploaded, dropped on the panel or
|
||||
* given as a URL) and how the image fills the window.
|
||||
*
|
||||
* Every change previews live through the background store; Cancel (or
|
||||
* closing) puts the saved one back. Saving with neither colour nor image is
|
||||
* the same as "Theme default", which removes the page's background.
|
||||
*/
|
||||
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
||||
import { FITS, useBackgroundStore } from "@/stores/background";
|
||||
import { uploadFile, isImage } from "./upload";
|
||||
|
||||
const props = defineProps({
|
||||
page: { type: String, required: true },
|
||||
});
|
||||
const emit = defineEmits(["close"]);
|
||||
|
||||
const bg = useBackgroundStore();
|
||||
const saved = bg.byPage[props.page];
|
||||
const draft = ref({
|
||||
color: saved?.color ?? "",
|
||||
image: saved?.image ?? "",
|
||||
fit: saved?.fit ?? "cover",
|
||||
});
|
||||
watch(draft, (d) => (bg.preview = { page: props.page, ...d }), { deep: true, immediate: true });
|
||||
onBeforeUnmount(() => {
|
||||
if (bg.preview?.page === props.page) bg.preview = null;
|
||||
});
|
||||
|
||||
// <input type="color"> only takes #rrggbb; a colour typed as a name or rgb()
|
||||
// still works, the swatch just can't show it.
|
||||
const HEX = /^#[0-9a-f]{6}$/i;
|
||||
const swatch = computed(() => (HEX.test(draft.value.color) ? draft.value.color : "#04080f"));
|
||||
|
||||
const busy = ref(false);
|
||||
const error = ref("");
|
||||
const dragOver = ref(false);
|
||||
|
||||
async function upload(file) {
|
||||
if (!file) return;
|
||||
if (!file.type.startsWith("image/")) {
|
||||
error.value = "That isn't an image";
|
||||
return;
|
||||
}
|
||||
error.value = "";
|
||||
busy.value = true;
|
||||
try {
|
||||
draft.value.image = await uploadFile(file);
|
||||
} catch (err) {
|
||||
error.value = err?.message || "Upload failed";
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onDrop(event) {
|
||||
dragOver.value = false;
|
||||
upload(event.dataTransfer?.files?.[0]);
|
||||
}
|
||||
|
||||
async function save() {
|
||||
error.value = "";
|
||||
busy.value = true;
|
||||
try {
|
||||
const { color, image, fit } = draft.value;
|
||||
if (!color.trim() && !image.trim()) await bg.clear(props.page);
|
||||
else await bg.save(props.page, { color, image, fit });
|
||||
emit("close");
|
||||
} catch (err) {
|
||||
error.value = err?.message || "Failed to save";
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function resetToDefault() {
|
||||
if (!confirm("Go back to the site's default background on this page?")) return;
|
||||
error.value = "";
|
||||
busy.value = true;
|
||||
try {
|
||||
await bg.clear(props.page);
|
||||
emit("close");
|
||||
} catch (err) {
|
||||
error.value = err?.message || "Failed to reset";
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form
|
||||
class="bg-editor bdr-2 bg-surface"
|
||||
:class="{ 'drag-over': dragOver }"
|
||||
@submit.prevent="save"
|
||||
@dragover.prevent="dragOver = true"
|
||||
@dragleave.self="dragOver = false"
|
||||
@drop.prevent="onDrop"
|
||||
>
|
||||
<h3>Page background</h3>
|
||||
|
||||
<label>
|
||||
Colour
|
||||
<span class="row">
|
||||
<input type="color" :value="swatch" @input="draft.color = $event.target.value" />
|
||||
<input v-model="draft.color" type="text" placeholder="theme colour" class="flex-1" />
|
||||
<button v-if="draft.color" type="button" class="bar-btn small" @click="draft.color = ''">✕</button>
|
||||
</span>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Image
|
||||
<span class="row">
|
||||
<input v-model="draft.image" type="text" placeholder="/uploads/… or https://…" class="flex-1" />
|
||||
<button v-if="draft.image" type="button" class="bar-btn small" @click="draft.image = ''">✕</button>
|
||||
</span>
|
||||
</label>
|
||||
<span class="row">
|
||||
<label class="bar-btn upload">
|
||||
{{ busy ? "Working…" : "Upload image…" }}
|
||||
<input type="file" accept="image/*" hidden :disabled="busy" @change="upload($event.target.files[0])" />
|
||||
</label>
|
||||
<span class="hint">or drop one on this panel</span>
|
||||
</span>
|
||||
<img v-if="draft.image && isImage(draft.image)" :src="draft.image" alt="" class="thumb" />
|
||||
|
||||
<label v-if="draft.image">
|
||||
Image fit
|
||||
<select v-model="draft.fit">
|
||||
<option v-for="f in FITS" :key="f.id" :value="f.id">{{ f.label }}</option>
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<div class="row">
|
||||
<button type="submit" class="bar-btn" :disabled="busy">Save</button>
|
||||
<button type="button" class="bar-btn" @click="emit('close')">Cancel</button>
|
||||
<button v-if="saved" type="button" class="bar-btn" :disabled="busy" @click="resetToDefault">
|
||||
Theme default
|
||||
</button>
|
||||
</div>
|
||||
<p v-if="error" class="text-tertiary">{{ error }}</p>
|
||||
</form>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.bg-editor {
|
||||
position: fixed;
|
||||
top: 80px;
|
||||
left: 16px;
|
||||
z-index: 65;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
width: min(380px, calc(100vw - 32px));
|
||||
padding: 0.75rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.bg-editor.drag-over {
|
||||
outline: 3px dashed var(--color-primary);
|
||||
}
|
||||
h3 {
|
||||
margin: 0;
|
||||
}
|
||||
label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
color: var(--color-muted);
|
||||
}
|
||||
.row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.4rem;
|
||||
}
|
||||
input[type="color"] {
|
||||
width: 2.5rem;
|
||||
height: 2.2rem;
|
||||
padding: 0;
|
||||
border: 1px solid var(--color-quaternary);
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
select {
|
||||
padding: 0.4rem;
|
||||
color: var(--color-primary);
|
||||
background: var(--color-surface-deep);
|
||||
border: 1px solid var(--color-primary);
|
||||
}
|
||||
.thumb {
|
||||
max-height: 100px;
|
||||
max-width: 100%;
|
||||
align-self: flex-start;
|
||||
border: 1px solid var(--color-quaternary);
|
||||
}
|
||||
.hint {
|
||||
color: var(--color-muted);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.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 {
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
.bar-btn:disabled {
|
||||
opacity: 0.5;
|
||||
}
|
||||
.bar-btn.small {
|
||||
padding: 0.1rem 0.5rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
/* The label is the button; the file input inside it is hidden. */
|
||||
.bar-btn.upload {
|
||||
display: inline-block;
|
||||
flex-direction: row;
|
||||
color: var(--color-primary);
|
||||
}
|
||||
</style>
|
||||
@@ -21,11 +21,17 @@
|
||||
* marked adminOnly are never sent to anyone else (the server filters them);
|
||||
* admins see them outlined. Image and video files dropped onto the canvas in
|
||||
* edit mode are uploaded and each becomes a box where it was dropped.
|
||||
*
|
||||
* The canvas also owns its page's background (stores/background.js), which
|
||||
* DefaultLayout paints behind the whole window while the canvas is mounted;
|
||||
* admins change it with the Background button.
|
||||
*/
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { useMediaQuery } from "@vueuse/core";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { useBoxesStore } from "@/stores/boxes";
|
||||
import { useBackgroundStore } from "@/stores/background";
|
||||
import BackgroundEditor from "./BackgroundEditor.vue";
|
||||
import BoxFrame from "./BoxFrame.vue";
|
||||
import BoxEditor from "./BoxEditor.vue";
|
||||
import HtmlBox from "./HtmlBox.vue";
|
||||
@@ -54,10 +60,19 @@ auth.ensureLoaded();
|
||||
|
||||
store.fetch();
|
||||
|
||||
const background = useBackgroundStore();
|
||||
background.show(props.page);
|
||||
onBeforeUnmount(() => background.hide(props.page));
|
||||
const backgroundOpen = ref(false);
|
||||
|
||||
const isMobile = useMediaQuery(MOBILE_QUERY);
|
||||
const isAdmin = computed(() => auth.loggedIn && !!auth.user.admin);
|
||||
// adminOnly boxes come and go with the session.
|
||||
watch(isAdmin, () => store.fetch());
|
||||
// adminOnly boxes come and go with the session, and so does an adminOnly
|
||||
// page's background.
|
||||
watch(isAdmin, () => {
|
||||
store.fetch();
|
||||
background.load(props.page);
|
||||
});
|
||||
|
||||
const editing = ref(false);
|
||||
const canEdit = computed(() => isAdmin.value && !isMobile.value);
|
||||
@@ -284,6 +299,9 @@ function toggleEdit() {
|
||||
<button type="button" class="bar-btn" :class="{ on: editing }" @click="toggleEdit">
|
||||
{{ editing ? "Done" : "Edit layout" }}
|
||||
</button>
|
||||
<button type="button" class="bar-btn" :class="{ on: backgroundOpen }" @click="backgroundOpen = !backgroundOpen">
|
||||
Background
|
||||
</button>
|
||||
<template v-if="inEditMode">
|
||||
<select v-model="newChoice" class="bar-btn" @change="addBox">
|
||||
<option value="" disabled>+ New box…</option>
|
||||
@@ -369,6 +387,8 @@ function toggleEdit() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<BackgroundEditor v-if="backgroundOpen && canEdit" :page="page" @close="backgroundOpen = false" />
|
||||
|
||||
<BoxEditor
|
||||
v-if="inEditMode && selected"
|
||||
ref="editor"
|
||||
|
||||
Reference in New Issue
Block a user