Files
stpweb/vue/src/stores/layouts.js
T
adamfandClaude Opus 5.5 2eacbc2e33
Test and deploy / test (push) Successful in 1m51s
Test and deploy / deploy (push) Successful in 1m10s
Add saved layouts, and make /stp an ordinary canvas page
A layout is a named copy of one canvas page's boxes and background
(models.Layout, layout.graphql), saved from the canvas's new Layouts
button and loaded onto any page from there or from the admin Pages
tab. Loading onto a page with boxes asks whether to replace them or
add on top; a replace first saves the page as a "<page> before
<layout>" layout so it can be undone. Copies, not links: editing a
page never changes a layout or another page. The copying is
server-side in one transaction, and everything is @admin since a
layout keeps admin-only boxes.

/stp loses its static route and becomes a Page with slug "stp",
created once at start-up (ensureStpPage), so it can be renamed,
hidden or deleted like a shrine. Its boxes were already keyed "stp".
Home3.vue is kept as legacy; its starter is now "Old /stp grid" in
pageStarters.js. ConfirmDialog gains an optional second button.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-23 20:33:07 +01:00

58 lines
1.9 KiB
JavaScript

/**
* Saved layouts (the `Layout` GraphQL type): named copies of a canvas page's
* boxes and background — full CRUD, built on createCrudStore(), plus apply().
*
* `create({ name, page })` saves that page as it is now; `update` only
* renames. What a layout holds never reaches the browser, only its size: the
* server does both the copying in and the copying out.
*
* Admin only, so nothing fetches it until an admin opens a layouts list
* (views/home3/LayoutPanel.vue, the admin panel's Pages tab).
*/
import { createCrudStore } from "@/stores/createCrudStore";
import { gql } from "@/graphql";
import { useBoxesStore } from "@/stores/boxes";
import { useBackgroundStore } from "@/stores/background";
const LAYOUT_FIELDS = `
id
name
sourcePage
boxCount
hasBackground
createdAt
`;
export const useLayoutsStore = createCrudStore("layouts", {
type: "Layout",
list: "layouts",
fields: LAYOUT_FIELDS,
// The list query is newest first.
insert: "prepend",
deleteReturns: "object",
extend: ({ fetch }) => ({
/**
* Copy layout `id` onto page key `page`, replacing what is there or
* adding on top. Throws on failure.
*/
async apply(id, page, replace) {
await gql(
`mutation ApplyLayout($id: ID!, $page: String!, $replace: Boolean!) {
applyLayout(id: $id, page: $page, replace: $replace) { id }
}`,
{ id, page, replace },
);
// The page's own stores are refetched rather than patched from the
// answer, so they stay in the shape their list query gives. A replace
// also saved the old page as a new layout, hence the list too.
await Promise.all([useBoxesStore(page).fetch(), useBackgroundStore().load(page), replace && fetch()]);
},
}),
});
/** How a page key reads to a person: "/" for the landing page. */
export function pagePath(page) {
return page === "index" ? "/" : `/${page}`;
}