Let the canvas panels choose from files already uploaded
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:
+9
-3
@@ -160,7 +160,10 @@ the allowed types, the 10MB limit and the file name, and stores the file in
|
||||
Garage. A refused file rejects with an `ApiError` whose message can be shown
|
||||
as-is. `kindOf(url)` says image/video/audio/file, `tagFor(url)` gives the
|
||||
markup for a box, `isUploadUrl(url)` checks a URL from someone else before
|
||||
rendering it, and `mediaSize(file)` reads an image or video's dimensions.
|
||||
rendering it, and `mediaSize(file)` reads an image or video's dimensions
|
||||
(from a `File` or a stored URL). `listUploads()` (admin only) lists every
|
||||
public upload, newest first; `components/util/UploadPicker.vue` is the modal
|
||||
that shows them as a grid to choose from.
|
||||
`{ private: true }` (admin only) is for private chat attachments; never use
|
||||
it for anything shown in a canvas box, whose sandboxed frame has no cookie.
|
||||
|
||||
@@ -239,7 +242,8 @@ renders any page's boxes.
|
||||
GraphQL, `stores/background.js`): a colour, an image, and whether the image
|
||||
fills the window, fits inside it or tiles. Admins set it with the
|
||||
"Background" button next to "Edit layout" (`BackgroundEditor.vue`), which
|
||||
previews live and takes an uploaded or dropped image. `DefaultLayout` paints
|
||||
previews live and takes an uploaded, dropped or already-uploaded image
|
||||
(`UploadPicker`). `DefaultLayout` paints
|
||||
it behind the whole window, navbar included, while that canvas is mounted,
|
||||
and falls back to the halftone. Renaming or deleting a page carries its
|
||||
background along; an admin-only page's background is hidden with it.
|
||||
@@ -275,7 +279,9 @@ renders any page's boxes.
|
||||
Image and video files dropped onto the canvas in edit mode are uploaded and
|
||||
each becomes an unframed box at the drop point. "Insert upload" stores a
|
||||
file through `@/uploads` (see "Uploading files") and puts an `<img>`/`<video>`
|
||||
tag for it at the cursor. Starter HTML is in `templates.js`; links in a box
|
||||
tag for it at the cursor; "Choose upload" does the same with a file already
|
||||
stored, and "+ From uploads…" in the admin bar makes a box from one, as a
|
||||
drop would (`UploadPicker`). Starter HTML is in `templates.js`; links in a box
|
||||
need `target="_top"` or `_blank` (the Link templates set it), or they open
|
||||
inside the frame. A canvas given a `starter` has an import button in edit
|
||||
mode (it confirms first if the page isn't empty): on `/stp`, "Import current
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Choose a file that is already uploaded, instead of uploading it again: a
|
||||
* modal grid of every public upload (listUploads in @/uploads), newest first.
|
||||
* Admin only, because the listing is. Used by the canvas's Background panel,
|
||||
* Box editor and "+ From uploads" button.
|
||||
*
|
||||
* <UploadPicker v-if="picking" v-model="picking" :kinds="['image']" @pick="use" />
|
||||
*
|
||||
* `kinds` filters by kindOf(url). The bucket holds hundreds of megabytes, so
|
||||
* thumbnails load lazily and videos fetch only their metadata.
|
||||
*/
|
||||
import { computed, ref } from "vue";
|
||||
import { kindOf, listUploads } from "@/uploads";
|
||||
import OverlayShell from "./OverlayShell.vue";
|
||||
|
||||
const open = defineModel({ type: Boolean, default: false });
|
||||
|
||||
const props = defineProps({
|
||||
kinds: { type: Array, default: () => ["image"] },
|
||||
title: { type: String, default: "Choose an upload" },
|
||||
});
|
||||
|
||||
const emit = defineEmits(["pick", "close"]);
|
||||
|
||||
const files = ref([]);
|
||||
const loading = ref(true);
|
||||
const error = ref("");
|
||||
const filter = ref("");
|
||||
|
||||
listUploads()
|
||||
.then((all) => (files.value = all.filter((f) => props.kinds.includes(kindOf(f.url)))))
|
||||
.catch((err) => (error.value = err?.message || "Couldn't list uploads"))
|
||||
.finally(() => (loading.value = false));
|
||||
|
||||
const shown = computed(() => {
|
||||
const q = filter.value.trim().toLowerCase();
|
||||
return q ? files.value.filter((f) => f.name.toLowerCase().includes(q)) : files.value;
|
||||
});
|
||||
|
||||
function describe(f) {
|
||||
const kb = f.size / 1024;
|
||||
const size = kb < 1024 ? `${Math.round(kb)} KB` : `${(kb / 1024).toFixed(1)} MB`;
|
||||
return `${f.name} · ${size} · ${new Date(f.modified).toLocaleString()}`;
|
||||
}
|
||||
|
||||
function close() {
|
||||
open.value = false;
|
||||
emit("close");
|
||||
}
|
||||
|
||||
function pick(f) {
|
||||
emit("pick", f.url);
|
||||
close();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<OverlayShell v-model="open" :title="title" backdrop close-on-backdrop @close="close">
|
||||
<div class="picker bdr-2 bg-surface">
|
||||
<header class="flex items-center gap-2">
|
||||
<h3 class="m-0 flex-1">{{ title }}</h3>
|
||||
<input v-model="filter" type="search" placeholder="Filter by name" class="filter" />
|
||||
<button type="button" class="close" title="Close" @click="close">✕</button>
|
||||
</header>
|
||||
|
||||
<p v-if="loading" class="state">Loading…</p>
|
||||
<p v-else-if="error" class="state text-tertiary">{{ error }}</p>
|
||||
<p v-else-if="!shown.length" class="state">
|
||||
{{ files.length ? "Nothing matches." : "No uploads yet." }}
|
||||
</p>
|
||||
<div v-else class="grid">
|
||||
<button v-for="f in shown" :key="f.name" type="button" class="tile" :title="describe(f)" @click="pick(f)">
|
||||
<img v-if="kindOf(f.url) === 'image'" :src="f.url" alt="" loading="lazy" />
|
||||
<video v-else-if="kindOf(f.url) === 'video'" :src="f.url" preload="metadata" muted></video>
|
||||
<span v-else class="name">{{ f.name }}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</OverlayShell>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.picker {
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.6rem;
|
||||
width: min(760px, calc(100vw - 24px));
|
||||
height: min(600px, calc(100vh - 24px));
|
||||
padding: 0.75rem;
|
||||
color: var(--color-secondary);
|
||||
}
|
||||
.filter {
|
||||
min-width: 0;
|
||||
width: 12rem;
|
||||
padding: 3px 5px;
|
||||
color: var(--color-secondary);
|
||||
background: var(--color-surface-deep);
|
||||
border: 1px solid var(--color-quaternary);
|
||||
}
|
||||
.close {
|
||||
color: var(--color-primary);
|
||||
background: var(--color-link-bg);
|
||||
border: 1px solid var(--color-primary);
|
||||
padding: 0 0.5rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
.state {
|
||||
color: var(--color-muted);
|
||||
}
|
||||
.grid {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
|
||||
grid-auto-rows: 120px;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
.tile {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
background: var(--color-surface-deep);
|
||||
border: 2px solid var(--color-quaternary);
|
||||
cursor: pointer;
|
||||
}
|
||||
.tile:hover,
|
||||
.tile:focus-visible {
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
.tile img,
|
||||
.tile video {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
pointer-events: none;
|
||||
}
|
||||
.name {
|
||||
padding: 0.25rem;
|
||||
font-size: 0.75rem;
|
||||
word-break: break-all;
|
||||
color: var(--color-muted);
|
||||
}
|
||||
</style>
|
||||
+22
-4
@@ -18,6 +18,10 @@
|
||||
*
|
||||
* A refused file (wrong type, too large) rejects with an ApiError whose
|
||||
* `message` is the backend's sentence, ready to show the user.
|
||||
*
|
||||
* listUploads() lists what is already stored, for reusing a file instead of
|
||||
* uploading it again (components/util/UploadPicker.vue). It is admin-only:
|
||||
* the listing is what would turn every unguessable URL into a known one.
|
||||
*/
|
||||
import { api } from "@/api";
|
||||
|
||||
@@ -50,6 +54,16 @@ export async function uploadFile(file, { private: isPrivate = false } = {}) {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every public upload, newest first. Admin only.
|
||||
*
|
||||
* @returns {Promise<Array<{name: string, url: string, size: number, modified: string}>>}
|
||||
*/
|
||||
export async function listUploads() {
|
||||
const { files } = await api.get("/uploads", { fallback: "Couldn't list uploads" });
|
||||
return files;
|
||||
}
|
||||
|
||||
/** What a stored file is, from its URL: "image", "video", "audio" or "file". */
|
||||
export function kindOf(url) {
|
||||
if (/\.(jpe?g|png|gif|webp)$/i.test(url)) return "image";
|
||||
@@ -86,15 +100,19 @@ export function tagFor(url) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Natural size of an image or video File, or null if it can't be read. */
|
||||
/**
|
||||
* Natural size of an image or video — a File, or the URL of one already
|
||||
* stored — or null if it can't be read.
|
||||
*/
|
||||
export function mediaSize(file) {
|
||||
return new Promise((resolve) => {
|
||||
const src = URL.createObjectURL(file);
|
||||
const isFile = typeof file !== "string";
|
||||
const src = isFile ? URL.createObjectURL(file) : file;
|
||||
const done = (size) => {
|
||||
URL.revokeObjectURL(src);
|
||||
if (isFile) URL.revokeObjectURL(src);
|
||||
resolve(size);
|
||||
};
|
||||
if (file.type.startsWith("video/")) {
|
||||
if (isFile ? file.type.startsWith("video/") : isVideo(file)) {
|
||||
const v = document.createElement("video");
|
||||
v.onloadedmetadata = () => done({ w: v.videoWidth, h: v.videoHeight });
|
||||
v.onerror = () => done(null);
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user