Make /stp an admin-editable canvas of boxes
/stp is now views/home3/Home3.vue: boxes an admin creates, drags, resizes and edits in the browser, saved to the database so every visitor sees the same layout. - backend: a Box GraphQL type (kind widget|html, x/y/w/h/z in px on a 1400px design canvas, framed, hideOnMobile). Public `boxes` query; create/update/delete are @admin. Geometry is clamped rather than rejected. Framed has no gorm default, since GORM would turn an explicit false into true on insert. - vue: widget boxes render the existing widgets from a registry; html boxes render in an iframe sandboxed with allow-scripts and no allow-same-origin, so admin script can't read cookies or call /api. The site tokens and fonts (inlined as data: URLs) are injected into each frame. The canvas scales to fit the window; phones get a single column in (y, x) order. Edit mode saves geometry on release and has a CodeMirror side panel (lazy-loaded) with a live HTML preview. Templates cover Clock, Weather (Open-Meteo) and Image/GIF. "Import current layout" recreates the old Home.vue grid on an empty canvas. - Home.vue is kept with a STATUS: legacy header. Docs updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Side panel for the selected box in edit mode.
|
||||
*
|
||||
* Two save rhythms, matching how each kind of field is used:
|
||||
* - Settings (title, geometry, frame, mobile, widget) save as soon as they
|
||||
* change — on blur/Enter for numbers, on change for the rest — the same
|
||||
* "instant" model as dragging.
|
||||
* - HTML is a draft. Typing updates the box on the canvas live (through the
|
||||
* `preview` event, so the parent can render the draft without touching the
|
||||
* store) and is saved only by Save or Ctrl/Cmd+S. Saving every keystroke
|
||||
* would push half-written markup to every visitor.
|
||||
*/
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { useBoxesStore } from "@/stores/boxes";
|
||||
import CodeEditor from "./CodeEditor.vue";
|
||||
import { WIDGETS } from "./widgetRegistry";
|
||||
|
||||
const props = defineProps({
|
||||
box: { type: Object, required: true },
|
||||
});
|
||||
const emit = defineEmits(["preview", "close", "error"]);
|
||||
|
||||
const store = useBoxesStore();
|
||||
|
||||
const draftHtml = ref(props.box.html ?? "");
|
||||
const saving = ref(false);
|
||||
const dirty = computed(() => props.box.kind === "html" && draftHtml.value !== (props.box.html ?? ""));
|
||||
|
||||
// Switching to another box: drop this one's preview, start from the new
|
||||
// box's saved html.
|
||||
watch(
|
||||
() => props.box.id,
|
||||
() => {
|
||||
emit("preview", null);
|
||||
draftHtml.value = props.box.html ?? "";
|
||||
},
|
||||
);
|
||||
|
||||
let previewTimer = null;
|
||||
watch(draftHtml, (html) => {
|
||||
clearTimeout(previewTimer);
|
||||
previewTimer = setTimeout(() => emit("preview", dirty.value ? html : null), 300);
|
||||
});
|
||||
|
||||
async function save(patch) {
|
||||
try {
|
||||
await store.update(props.box.id, patch);
|
||||
} catch (err) {
|
||||
emit("error", err);
|
||||
}
|
||||
}
|
||||
|
||||
async function saveHtml() {
|
||||
if (!dirty.value || saving.value) return;
|
||||
saving.value = true;
|
||||
try {
|
||||
await store.update(props.box.id, { html: draftHtml.value });
|
||||
emit("preview", null);
|
||||
} catch (err) {
|
||||
emit("error", err);
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function saveNumber(field, event) {
|
||||
const value = Math.round(Number(event.target.value));
|
||||
if (!Number.isFinite(value) || value === props.box[field]) {
|
||||
event.target.value = props.box[field];
|
||||
return;
|
||||
}
|
||||
save({ [field]: value });
|
||||
}
|
||||
|
||||
function close() {
|
||||
if (dirty.value && !confirm("Discard unsaved HTML changes?")) return;
|
||||
emit("preview", null);
|
||||
emit("close");
|
||||
}
|
||||
|
||||
defineExpose({ close });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<aside class="box-editor" @keydown.esc="close">
|
||||
<header class="flex items-center justify-between gap-2">
|
||||
<h3 class="m-0">
|
||||
{{ box.kind === "widget" ? `Widget · ${box.widget}` : "HTML box" }}
|
||||
</h3>
|
||||
<button type="button" class="close" title="Close" @click="close">✕</button>
|
||||
</header>
|
||||
|
||||
<label class="field">
|
||||
<span>Title</span>
|
||||
<input
|
||||
type="text"
|
||||
:value="box.title ?? ''"
|
||||
placeholder="(label in edit mode)"
|
||||
@change="save({ title: $event.target.value })"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label v-if="box.kind === 'widget'" class="field">
|
||||
<span>Widget</span>
|
||||
<select :value="box.widget" @change="save({ widget: $event.target.value })">
|
||||
<option v-for="name in Object.keys(WIDGETS)" :key="name" :value="name">{{ name }}</option>
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<div class="geometry">
|
||||
<label v-for="field in ['x', 'y', 'w', 'h', 'z']" :key="field" class="field">
|
||||
<span>{{ field }}</span>
|
||||
<input
|
||||
type="number"
|
||||
:value="box[field]"
|
||||
@change="saveNumber(field, $event)"
|
||||
@keydown.enter="$event.target.blur()"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-4">
|
||||
<label class="check">
|
||||
<input type="checkbox" :checked="box.framed" @change="save({ framed: $event.target.checked })" />
|
||||
Frame
|
||||
</label>
|
||||
<label class="check">
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="box.hideOnMobile"
|
||||
@change="save({ hideOnMobile: $event.target.checked })"
|
||||
/>
|
||||
Hide on mobile
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<template v-if="box.kind === 'html'">
|
||||
<CodeEditor v-model="draftHtml" class="flex-1" @save="saveHtml" />
|
||||
<footer class="flex items-center justify-between gap-2">
|
||||
<small class="text-muted">
|
||||
Runs sandboxed: scripts work, cookies and /api don't. fetch() only reaches
|
||||
keyless CORS-open APIs. Ctrl+S saves.
|
||||
</small>
|
||||
<button type="button" class="save" :disabled="!dirty || saving" @click="saveHtml">
|
||||
{{ saving ? "Saving…" : dirty ? "Save" : "Saved" }}
|
||||
</button>
|
||||
</footer>
|
||||
</template>
|
||||
</aside>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.box-editor {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
z-index: 60;
|
||||
width: min(460px, 100vw);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.6rem;
|
||||
padding: 0.75rem;
|
||||
box-sizing: border-box;
|
||||
background: var(--color-surface);
|
||||
border-left: 2px solid var(--color-primary);
|
||||
color: var(--color-secondary);
|
||||
}
|
||||
|
||||
.close {
|
||||
color: var(--color-primary);
|
||||
background: var(--color-link-bg);
|
||||
border: 1px solid var(--color-primary);
|
||||
padding: 0 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
font-size: 0.85rem;
|
||||
color: var(--color-muted);
|
||||
}
|
||||
.field input,
|
||||
.field select {
|
||||
padding: 3px 5px;
|
||||
color: var(--color-secondary);
|
||||
background: var(--color-surface-deep);
|
||||
border: 1px solid var(--color-quaternary);
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.geometry {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(5, 1fr);
|
||||
gap: 0.4rem;
|
||||
}
|
||||
|
||||
.check {
|
||||
white-space: nowrap;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.35rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.save {
|
||||
flex: none;
|
||||
padding: 0.3rem 1rem;
|
||||
color: var(--color-primary);
|
||||
background: var(--color-link-bg);
|
||||
border: 2px solid var(--color-primary);
|
||||
font-family: var(--font-heading);
|
||||
font-size: 1.1rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
.save:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user