Add saved components: reusable HTML boxes, placed as copies or linked
A component (models.Component, component.graphql) is a named HTML box. "+ New box" now lists components and places a copy, as it did with the hard-coded starters; the canvas's new Components button (ComponentPanel) can also place one linked. A linked box has Box.componentId, its row's html is nil, and the resolvers fill in the component's html before answering (fillLinked), so saving the html in the box editor saves the component and every linked box on every page changes. Only the html is shared: title, geometry, frame and visibility stay each box's own. The box editor gains "Save as component" (makes one from an html box and links it) and, on a linked box, a banner with Detach (gives the box its own copy). updateBox refuses html on a linked box. Deleting a component detaches its boxes first, so no page changes. Layouts store the link and the html; loading relinks only if the component still exists. The built-in starters in templates.js are no longer listed directly: "Import built-ins" in the panel creates them as components (skipping names already taken), so they can be edited and deleted like the rest. The html helpers there are unchanged. Everything is @admin. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+28
-2
@@ -14,6 +14,11 @@
|
||||
* The list query returns boxes ordered by z, so a new box (given the top z)
|
||||
* belongs at the end. adminOnly boxes only come back for an admin — the
|
||||
* server filters them — so the canvas refetches when the session changes.
|
||||
*
|
||||
* A box with a `componentId` is linked to that component (stores/components.js):
|
||||
* the server answers with the component's html in `html`, and editing it goes
|
||||
* to the component. patchLinked() keeps every page already loaded in step when
|
||||
* a component changes, without refetching them.
|
||||
*/
|
||||
|
||||
import { createCrudStore } from "@/stores/createCrudStore";
|
||||
@@ -33,11 +38,15 @@ const BOX_FIELDS = `
|
||||
framed
|
||||
hideOnMobile
|
||||
adminOnly
|
||||
componentId
|
||||
`;
|
||||
|
||||
// The fields a duplicate copies. kind and widget/html are what make it the
|
||||
// same box; geometry is offset so the copy is visibly separate.
|
||||
const COPIED = ["page", "kind", "widget", "html", "title", "w", "h", "framed", "hideOnMobile", "adminOnly"];
|
||||
// same box — or componentId, which keeps a copy of a linked box linked;
|
||||
// geometry is offset so the copy is visibly separate.
|
||||
const COPIED = [
|
||||
"page", "kind", "widget", "html", "title", "w", "h", "framed", "hideOnMobile", "adminOnly", "componentId",
|
||||
];
|
||||
|
||||
const stores = new Map();
|
||||
|
||||
@@ -64,6 +73,11 @@ function defineFor(page) {
|
||||
const input = Object.fromEntries(COPIED.map((k) => [k, box[k]]));
|
||||
return create({ ...input, x: box.x + 20, y: box.y + 20, z: topZ() });
|
||||
},
|
||||
/** Swap in a box the server answered with from outside this store. */
|
||||
replaceLocal: (box) => {
|
||||
const idx = items.value.findIndex((b) => b.id === box.id);
|
||||
if (idx >= 0) items.value[idx] = box;
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -74,3 +88,15 @@ export function useBoxesStore(page) {
|
||||
if (!stores.has(page)) stores.set(page, defineFor(page));
|
||||
return stores.get(page)();
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply `patch` to every loaded box linked to component `componentId`, on
|
||||
* every page visited so far.
|
||||
*/
|
||||
export function patchLinked(componentId, patch) {
|
||||
for (const use of stores.values()) {
|
||||
for (const box of use().boxes) {
|
||||
if (box.componentId === componentId) Object.assign(box, patch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* Saved components (the `Component` GraphQL type): named HTML boxes that can
|
||||
* be placed on any canvas page as a copy or linked — full CRUD, built on
|
||||
* createCrudStore(), plus the box-side actions.
|
||||
*
|
||||
* A linked box shows its component's html, so `update` is wrapped to push a
|
||||
* new html into every loaded page's boxes (stores/boxes.js patchLinked), and
|
||||
* `remove` unlinks them locally the way the server just did (each keeps the
|
||||
* html it showed). `uses` is kept roughly right locally (countUse) as this
|
||||
* tab places, detaches and deletes linked boxes, and the panel refetches the
|
||||
* list when it opens for the true figure.
|
||||
*
|
||||
* Admin only, so nothing fetches it until an admin opens the canvas's
|
||||
* Components panel or "+ New box" menu (views/home3/ComponentPanel.vue,
|
||||
* BoxCanvas.vue).
|
||||
*/
|
||||
|
||||
import { createCrudStore } from "@/stores/createCrudStore";
|
||||
import { gql } from "@/graphql";
|
||||
import { patchLinked, useBoxesStore } from "@/stores/boxes";
|
||||
import { TEMPLATES } from "@/views/home3/templates";
|
||||
|
||||
const COMPONENT_FIELDS = `
|
||||
id
|
||||
name
|
||||
title
|
||||
html
|
||||
w
|
||||
h
|
||||
framed
|
||||
adminOnly
|
||||
uses
|
||||
`;
|
||||
|
||||
// Matches BOX_FIELDS in stores/boxes.js, for the mutations that answer with
|
||||
// a box.
|
||||
const BOX_FIELDS = `
|
||||
id page kind widget html title x y w h z framed hideOnMobile adminOnly componentId
|
||||
`;
|
||||
|
||||
export const useComponentsStore = createCrudStore("components", {
|
||||
type: "Component",
|
||||
list: "components",
|
||||
fields: COMPONENT_FIELDS,
|
||||
// Created ones are put back in name order by sort() below.
|
||||
insert: "append",
|
||||
deleteReturns: "object",
|
||||
extend: ({ items, fetch, create, update, remove }) => {
|
||||
const sort = () => items.value.sort((a, b) => a.name.localeCompare(b.name));
|
||||
const countUse = (id, delta) => {
|
||||
const c = items.value.find((c) => c.id === id);
|
||||
if (c) c.uses = Math.max(0, c.uses + delta);
|
||||
};
|
||||
|
||||
return {
|
||||
countUse,
|
||||
|
||||
async create(input) {
|
||||
const created = await create(input);
|
||||
sort();
|
||||
return created;
|
||||
},
|
||||
|
||||
async update(id, input) {
|
||||
const updated = await update(id, input);
|
||||
if ("html" in input) patchLinked(id, { html: updated.html });
|
||||
sort();
|
||||
return updated;
|
||||
},
|
||||
|
||||
async remove(id) {
|
||||
await remove(id);
|
||||
patchLinked(id, { componentId: null });
|
||||
},
|
||||
|
||||
/**
|
||||
* Make html box `box` into a component named `name` and link it.
|
||||
* Throws on failure.
|
||||
*/
|
||||
async saveFromBox(box, name) {
|
||||
const data = await gql(
|
||||
`mutation SaveBoxAsComponent($id: ID!, $name: String!) {
|
||||
saveBoxAsComponent(id: $id, name: $name) { ${BOX_FIELDS} }
|
||||
}`,
|
||||
{ id: box.id, name },
|
||||
);
|
||||
useBoxesStore(box.page).replaceLocal(data.saveBoxAsComponent);
|
||||
await fetch();
|
||||
},
|
||||
|
||||
/** Give linked box `box` its own copy of the html. Throws on failure. */
|
||||
async detach(box) {
|
||||
const data = await gql(
|
||||
`mutation DetachBox($id: ID!) { detachBox(id: $id) { ${BOX_FIELDS} } }`,
|
||||
{ id: box.id },
|
||||
);
|
||||
useBoxesStore(box.page).replaceLocal(data.detachBox);
|
||||
countUse(box.componentId, -1);
|
||||
},
|
||||
|
||||
/**
|
||||
* Put `component` on page key `page` at `at` ({ x, y, z }): linked, or
|
||||
* as an ordinary box with a copy of its html. Answers with the new box.
|
||||
* Throws on failure.
|
||||
*/
|
||||
async place(component, page, at, linked) {
|
||||
const input = {
|
||||
kind: "html",
|
||||
title: component.title,
|
||||
w: component.w,
|
||||
h: component.h,
|
||||
framed: component.framed,
|
||||
adminOnly: component.adminOnly,
|
||||
...at,
|
||||
};
|
||||
if (linked) input.componentId = component.id;
|
||||
else input.html = component.html;
|
||||
const created = await useBoxesStore(page).create(input);
|
||||
if (linked) countUse(component.id, 1);
|
||||
return created;
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a component from each built-in starter in templates.js whose
|
||||
* name is free, and answer with how many were added. Deleted ones come
|
||||
* back; ones still there are left as they are. Throws on failure.
|
||||
*/
|
||||
async importBuiltins() {
|
||||
const taken = new Set(items.value.map((c) => c.name));
|
||||
let added = 0;
|
||||
for (const t of TEMPLATES) {
|
||||
if (taken.has(t.label)) continue;
|
||||
await create({
|
||||
name: t.label,
|
||||
title: t.title,
|
||||
html: t.html,
|
||||
w: t.size[0],
|
||||
h: t.size[1],
|
||||
framed: t.framed ?? true,
|
||||
adminOnly: t.adminOnly ?? false,
|
||||
});
|
||||
added++;
|
||||
}
|
||||
sort();
|
||||
return added;
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user