Let the canvas panels choose from files already uploaded
Test and deploy / deploy (push) Has been cancelled
Test and deploy / test (push) Successful in 2m4s

The Background panel, the Box editor and the canvas admin bar could only
take a new upload, so reusing a picture meant uploading it again or
pasting its URL. Each now has a picker (UploadPicker.vue): a grid of the
public uploads, newest first, filterable by name.

It lists through a new admin-only GET /api/uploads (Uploads.List), the
union of the Garage bucket and the disk fallback, since nginx serves
from both; a bucket that can't list falls back to the disk's files.
Admin-only because upload URLs are public but unguessable, and a public
listing would give every one away. Private files and anything off the
UploadTypes allow-list are never listed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 19:59:03 +01:00
co-authored by Claude Opus 5.5
parent 1ddaff0690
commit 3fa456a063
12 changed files with 498 additions and 40 deletions
+9 -4
View File
@@ -1,8 +1,9 @@
<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.
* "Background" button: a colour, an image (uploaded, dropped on the panel,
* chosen from what is already uploaded, 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
@@ -13,6 +14,7 @@ import { FITS, useBackgroundStore } from "@/stores/background";
import { ACCEPT, uploadFile, isImage } from "@/uploads";
import OverlayShell from "@/components/util/OverlayShell.vue";
import ConfirmDialog from "@/components/util/ConfirmDialog.vue";
import UploadPicker from "@/components/util/UploadPicker.vue";
const props = defineProps({
page: { type: String, required: true },
@@ -41,6 +43,7 @@ const busy = ref(false);
const error = ref("");
const dragOver = ref(false);
const confirmation = ref(false);
const picking = ref(false);
async function upload(file) {
if (!file) return;
@@ -130,13 +133,14 @@ async function confirmReset() {
<button v-if="draft.image" type="button" class="bar-btn small" @click="draft.image = ''">✕</button>
</span>
</label>
<span class="row">
<span class="row flex-wrap">
<label class="bar-btn upload">
{{ busy ? "Working…" : "Upload image…" }}
<input type="file" :accept="ACCEPT.images" hidden :disabled="busy" @change="upload($event.target.files[0])" />
</label>
<span class="hint">or drop one on this panel</span>
<button type="button" class="bar-btn" :disabled="busy" @click="picking = true">Choose from uploads…</button>
</span>
<span class="hint">or drop one on this panel</span>
<img v-if="draft.image && isImage(draft.image)" :src="draft.image" alt="" class="thumb" />
<label v-if="draft.image">
@@ -164,6 +168,7 @@ async function confirmReset() {
confirm-label="Reset"
@confirm="confirmReset"
/>
<UploadPicker v-if="picking" v-model="picking" @pick="draft.image = $event" />
</template>
<style scoped>
+39 -20
View File
@@ -20,7 +20,8 @@
* changes save on release, and the selected box opens in BoxEditor. Boxes
* 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.
* edit mode are uploaded and each becomes a box where it was dropped; "+ From
* uploads" does the same with a file that is already uploaded.
*
* The canvas also owns its page's background (stores/background.js), which
* DefaultLayout paints behind the whole window while the canvas is mounted;
@@ -33,13 +34,14 @@ import { useBoxesStore } from "@/stores/boxes";
import { useBackgroundStore } from "@/stores/background";
import BackgroundEditor from "./BackgroundEditor.vue";
import ConfirmDialog from "@/components/util/ConfirmDialog.vue";
import UploadPicker from "@/components/util/UploadPicker.vue";
import BoxFrame from "./BoxFrame.vue";
import BoxEditor from "./BoxEditor.vue";
import HtmlBox from "./HtmlBox.vue";
import WidgetBox from "./WidgetBox.vue";
import { WIDGETS } from "./widgetRegistry";
import { TEMPLATES, imageHtml, videoHtml } from "./templates";
import { mediaSize, uploadFile } from "@/uploads";
import { isVideo, mediaSize, uploadFile } from "@/uploads";
const props = defineProps({
page: { type: String, required: true },
@@ -256,8 +258,26 @@ function isMedia(file) {
return file.type.startsWith("image/") || file.type.startsWith("video/");
}
// Each file is uploaded and placed at the drop point, sized to its own
// aspect ratio so nothing is cropped, then cascaded like visibleOrigin().
// A box showing one stored image or video at (x, y), sized to the file's own
// aspect ratio so nothing is cropped. Selected once made.
async function placeMedia(url, size, title, x, y) {
const fit = size ? Math.min(1, DROP_MAX / Math.max(size.w, size.h)) : 1;
const created = await store.create({
kind: "html",
title,
html: isVideo(url) ? videoHtml(url) : imageHtml(url),
framed: false,
x,
y,
w: size ? Math.max(40, Math.round(size.w * fit)) : 300,
h: size ? Math.max(40, Math.round(size.h * fit)) : 300,
z: store.topZ(),
});
selectedId.value = created.id;
}
// Each file is uploaded and placed at the drop point, then cascaded like
// visibleOrigin().
async function onDrop(event) {
dragOver.value = false;
const files = [...(event.dataTransfer?.files ?? [])].filter(isMedia);
@@ -270,22 +290,7 @@ async function onDrop(event) {
for (const file of files) {
try {
const [url, size] = await Promise.all([uploadFile(file), mediaSize(file)]);
const fit = size ? Math.min(1, DROP_MAX / Math.max(size.w, size.h)) : 1;
const w = size ? Math.max(40, Math.round(size.w * fit)) : 300;
const h = size ? Math.max(40, Math.round(size.h * fit)) : 300;
const video = file.type.startsWith("video/");
const created = await store.create({
kind: "html",
title: file.name,
html: video ? videoHtml(url) : imageHtml(url),
framed: false,
x,
y,
w,
h,
z: store.topZ(),
});
selectedId.value = created.id;
await placeMedia(url, size, file.name, x, y);
x += 20;
y += 20;
} catch (err) {
@@ -296,6 +301,17 @@ async function onDrop(event) {
}
}
// A file picked from what is already uploaded goes where a new box would.
const picking = ref(false);
async function placePicked(url) {
const { x, y } = visibleOrigin();
try {
await placeMedia(url, await mediaSize(url), url.split("/").pop(), x, y);
} catch (err) {
report(err);
}
}
// ---- Selection -----------------------------------------------------------
// Leaving a box saves its unsaved HTML; BoxEditor does that itself as the
// selection changes or it unmounts.
@@ -346,6 +362,7 @@ function toggleEdit() {
>
{{ importing ? "Importing…" : starterLabel }}
</button>
<button type="button" class="bar-btn" @click="picking = true">+ From uploads…</button>
</template>
<span v-if="inEditMode" class="hint">
{{ uploading ? `Uploading ${uploading}…` : "Drop images or videos onto the canvas to add them" }}
@@ -422,6 +439,8 @@ function toggleEdit() {
@error="report"
/>
<UploadPicker v-if="picking" v-model="picking" :kinds="['image', 'video']" @pick="placePicked" />
<ConfirmDialog
v-if="confirmation"
v-model="confirmation.open"
+24 -3
View File
@@ -15,6 +15,7 @@
*
* "Insert upload" uploads a file (@/uploads) and puts a tag for it at the
* cursor — the way to add pictures to an existing box, e.g. a gallery.
* "Choose upload" does the same with a file that is already uploaded.
*
* The panel is teleported to <body>, so it is fixed to the window whatever
* transform or stacking context the page puts around the canvas.
@@ -25,6 +26,7 @@ import { ACCEPT, tagFor, uploadFile } from "@/uploads";
import CodeEditor from "./CodeEditor.vue";
import { WIDGETS } from "./widgetRegistry";
import OverlayShell from "@/components/util/OverlayShell.vue";
import UploadPicker from "@/components/util/UploadPicker.vue";
const props = defineProps({
box: { type: Object, required: true },
@@ -36,6 +38,7 @@ const store = useBoxesStore(props.box.page);
const codeEditor = ref(null);
const fileInput = ref(null);
const uploading = ref(false);
const picking = ref(false);
const open = ref(true);
const draftHtml = ref(props.box.html ?? "");
@@ -105,14 +108,18 @@ function saveNumber(field, event) {
save({ [field]: value });
}
function insertFile(url) {
const tag = tagFor(url);
if (!codeEditor.value?.insert(tag)) draftHtml.value += `\n${tag}`;
}
async function upload(event) {
const file = event.target.files?.[0];
event.target.value = "";
if (!file) return;
uploading.value = true;
try {
const tag = tagFor(await uploadFile(file));
if (!codeEditor.value?.insert(tag)) draftHtml.value += `\n${tag}`;
insertFile(await uploadFile(file));
} catch (err) {
emit("error", err);
} finally {
@@ -186,7 +193,7 @@ function close() {
<template v-if="box.kind === 'html'">
<CodeEditor ref="codeEditor" v-model="draftHtml" class="flex-1" @save="saveHtml" />
<footer class="flex items-center justify-between gap-2">
<footer class="flex flex-wrap 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; so does leaving the box.
@@ -207,6 +214,14 @@ function close() {
>
{{ uploading ? "Uploading…" : "Insert upload" }}
</button>
<button
type="button"
class="save"
title="Insert a file that is already uploaded"
@click="picking = true"
>
Choose upload
</button>
<button type="button" class="save" :disabled="!dirty || saving" @click="saveHtml">
{{ saving ? "Saving…" : dirty ? "Save" : "Saved" }}
</button>
@@ -214,6 +229,12 @@ function close() {
</template>
</aside>
</OverlayShell>
<UploadPicker
v-if="picking"
v-model="picking"
:kinds="['image', 'video', 'audio']"
@pick="insertFile"
/>
</template>
<style scoped>