Fix the landing notes editor, drop-to-upload pictures, retire Demoman
The box editor rendered inside the landing page's overlay canvas, which is pointer-events: none so the page underneath stays clickable; the panel inherited it and ignored every click and keystroke. It is now teleported to <body>. Image and video files dropped onto a canvas in edit mode are uploaded and each becomes an unframed box at the drop point, sized to its aspect ratio. The upload code moves to home3/upload.js, shared with the editor's "Insert upload". The hand-written Demoman shrine is unrouted (and its slug unreserved) so it can be remade as a canvas page. /shrines lists only canvas pages and keeps the admin-only create form behind a "+ New shrine" button. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -65,14 +65,9 @@ const router = createRouter({
|
||||
name: "shrine links",
|
||||
component: () => import("@/views/home/shrines/Shrines.vue"),
|
||||
},
|
||||
// The GTO, Evangelion and Skipskipbenben shrines were <Wip />
|
||||
// placeholders; they are canvas pages now, made from /shrines
|
||||
// and served by the catch-all below.
|
||||
{
|
||||
path: "shrines/demoman",
|
||||
name: "demoman shrine",
|
||||
component: () => import("@/views/home/shrines/Demoman.vue"),
|
||||
},
|
||||
// Individual shrines are canvas pages, made from /shrines and
|
||||
// served by the catch-all below. The old hand-written ones
|
||||
// (views/home/shrines/) are unrouted.
|
||||
// Anything else is an admin-created canvas page if one exists
|
||||
// at that address (the Page GraphQL type), else the 404.
|
||||
{
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Route `/shrines/demoman` — a wall of TF2 Demoman clips via <VideoTable>.
|
||||
* STATUS: no route points here — removed so /shrines/demoman can be made
|
||||
* again as a canvas page (views/canvas/CanvasPage.vue). The clips are still
|
||||
* in public/img/demoman/.
|
||||
*
|
||||
* Was route `/shrines/demoman` — a wall of TF2 Demoman clips via <VideoTable>.
|
||||
*/
|
||||
import VideoTable from "@/components/util/VideoTable.vue";
|
||||
import Link from "@/components/text/Link.vue";
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Route `/shrines` — index of the shrine pages. Demoman is a hand-written
|
||||
* view; every other shrine is a canvas page (views/canvas/) whose address
|
||||
* starts with "shrines/", listed from the pages query. Admins get a form to
|
||||
* start a new one from the Shrine layout.
|
||||
* Route `/shrines` — index of the shrine pages: every canvas page
|
||||
* (views/canvas/) whose address starts with "shrines/", from the pages query.
|
||||
* Admins — and only admins; the server refuses createPage to anyone else
|
||||
* anyway — get a button that opens a form to start one from the Shrine
|
||||
* layout.
|
||||
*/
|
||||
import { computed } from "vue";
|
||||
import { computed, ref } from "vue";
|
||||
import RouterTable from "@/components/util/RouterTable.vue";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { usePagesStore } from "@/stores/pages";
|
||||
@@ -17,14 +18,13 @@ auth.ensureLoaded();
|
||||
pages.fetch();
|
||||
|
||||
const isAdmin = computed(() => auth.loggedIn && !!auth.user.admin);
|
||||
const creating = ref(false);
|
||||
|
||||
const shrine_links = computed(() =>
|
||||
[
|
||||
{ name: "Demoman", link: "/shrines/demoman" },
|
||||
...pages.pages
|
||||
.filter((p) => p.slug.startsWith("shrines/"))
|
||||
.map((p) => ({ name: p.adminOnly ? `${p.title} 🔒` : p.title, link: `/${p.slug}` })),
|
||||
].sort((a, b) => a.name.localeCompare(b.name)),
|
||||
pages.pages
|
||||
.filter((p) => p.slug.startsWith("shrines/"))
|
||||
.map((p) => ({ name: p.adminOnly ? `${p.title} 🔒` : p.title, link: `/${p.slug}` }))
|
||||
.sort((a, b) => a.name.localeCompare(b.name)),
|
||||
);
|
||||
</script>
|
||||
|
||||
@@ -34,10 +34,15 @@ const shrine_links = computed(() =>
|
||||
class="page-a4 bdr-1 flex flex-col relative overflow-scroll gap-1"
|
||||
>
|
||||
<RouterTable :linkArr="shrine_links" />
|
||||
<section v-if="isAdmin" class="mt-4">
|
||||
<h3>New shrine</h3>
|
||||
<PageForm prefix="shrines/" starter="shrine" />
|
||||
</section>
|
||||
<p v-if="pages.loaded && !shrine_links.length" class="text-muted text-center">
|
||||
No shrines yet.
|
||||
</p>
|
||||
<template v-if="isAdmin">
|
||||
<button type="button" class="bdr-2 bg-surface mt-4 cursor-pointer" @click="creating = !creating">
|
||||
{{ creating ? "Cancel" : "+ New shrine" }}
|
||||
</button>
|
||||
<PageForm v-if="creating" prefix="shrines/" starter="shrine" />
|
||||
</template>
|
||||
</div>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
* Admins get an Edit toggle. In edit mode boxes drag and resize (BoxFrame),
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* `overlay` lays the canvas over whatever the parent already renders instead
|
||||
* of taking space of its own — the landing page uses it to pin notes over its
|
||||
@@ -38,7 +39,8 @@ import BoxEditor from "./BoxEditor.vue";
|
||||
import HtmlBox from "./HtmlBox.vue";
|
||||
import WidgetBox from "./WidgetBox.vue";
|
||||
import { WIDGETS } from "./widgetRegistry";
|
||||
import { TEMPLATES } from "./templates";
|
||||
import { TEMPLATES, imageHtml, videoHtml } from "./templates";
|
||||
import { mediaSize, uploadFile } from "./upload";
|
||||
|
||||
const props = defineProps({
|
||||
page: { type: String, required: true },
|
||||
@@ -218,6 +220,55 @@ async function importStarter() {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Dropping files -----------------------------------------------------
|
||||
const DROP_MAX = 400; // longest side of a dropped picture's box, canvas px
|
||||
const dragOver = ref(false);
|
||||
const uploading = ref(0);
|
||||
|
||||
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().
|
||||
async function onDrop(event) {
|
||||
dragOver.value = false;
|
||||
const files = [...(event.dataTransfer?.files ?? [])].filter(isMedia);
|
||||
if (!files.length) return;
|
||||
const rect = canvasEl.value.getBoundingClientRect();
|
||||
const snap = (n) => Math.max(0, Math.round(n / 5) * 5);
|
||||
let x = snap((event.clientX - rect.left) / scale.value);
|
||||
let y = snap((event.clientY - rect.top) / scale.value);
|
||||
uploading.value += files.length;
|
||||
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;
|
||||
x += 20;
|
||||
y += 20;
|
||||
} catch (err) {
|
||||
report(err);
|
||||
} finally {
|
||||
uploading.value--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Selection -----------------------------------------------------------
|
||||
function select(box) {
|
||||
if (selectedId.value === box.id) return;
|
||||
@@ -271,6 +322,9 @@ function toggleEdit() {
|
||||
{{ importing ? "Importing…" : starterLabel }}
|
||||
</button>
|
||||
</template>
|
||||
<span v-if="inEditMode" class="hint">
|
||||
{{ uploading ? `Uploading ${uploading}…` : "Drop images or videos onto the canvas to add them" }}
|
||||
</span>
|
||||
<slot name="admin-bar" :editing="inEditMode" />
|
||||
<span v-if="errorMsg" class="error">{{ errorMsg }}</span>
|
||||
</div>
|
||||
@@ -303,9 +357,12 @@ function toggleEdit() {
|
||||
<div
|
||||
ref="canvasEl"
|
||||
class="canvas"
|
||||
:class="{ editing: inEditMode }"
|
||||
:class="{ editing: inEditMode, 'drag-over': dragOver }"
|
||||
:style="{ width: `${CANVAS_W}px`, height: `${canvasH}px`, transform: `scale(${scale})` }"
|
||||
@pointerdown.self="selectedId = null"
|
||||
@dragover.prevent="dragOver = inEditMode"
|
||||
@dragleave.self="dragOver = false"
|
||||
@drop.prevent="inEditMode && onDrop($event)"
|
||||
>
|
||||
<BoxFrame
|
||||
v-for="box in store.boxes"
|
||||
@@ -405,6 +462,10 @@ function toggleEdit() {
|
||||
.error {
|
||||
color: var(--color-tertiary);
|
||||
}
|
||||
.hint {
|
||||
color: var(--color-muted);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
.state {
|
||||
text-align: center;
|
||||
color: var(--color-muted);
|
||||
@@ -429,6 +490,9 @@ function toggleEdit() {
|
||||
background-image: radial-gradient(var(--color-quaternary) 1px, transparent 1px);
|
||||
background-size: 20px 20px;
|
||||
}
|
||||
.canvas.drag-over {
|
||||
outline: 3px dashed var(--color-primary);
|
||||
}
|
||||
|
||||
.stack {
|
||||
display: flex;
|
||||
|
||||
@@ -11,15 +11,16 @@
|
||||
* store) and is saved only by Save or Ctrl/Cmd+S. Saving every keystroke
|
||||
* would push half-written markup to every visitor.
|
||||
*
|
||||
* "Insert upload" stores a file through the chat's upload endpoint (public,
|
||||
* unguessable URLs under /uploads/) and puts a tag for it at the cursor —
|
||||
* the quick way to fill a shrine with images. It is never the private upload
|
||||
* directory: that one is behind a cookie check, and the sandboxed frame
|
||||
* loads as an opaque origin, so its requests may not carry the cookie.
|
||||
* "Insert upload" uploads a file (upload.js) and puts a tag for it at the
|
||||
* cursor — the way to add pictures to an existing box, e.g. a gallery.
|
||||
*
|
||||
* The panel is teleported to <body>. On the landing page the canvas is an
|
||||
* overlay with pointer-events: none, which a panel rendered inside it would
|
||||
* inherit — it would show but ignore every click and keystroke.
|
||||
*/
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { useBoxesStore } from "@/stores/boxes";
|
||||
import { api } from "@/api";
|
||||
import { tagFor, uploadFile } from "./upload";
|
||||
import CodeEditor from "./CodeEditor.vue";
|
||||
import { WIDGETS } from "./widgetRegistry";
|
||||
|
||||
@@ -84,25 +85,13 @@ function saveNumber(field, event) {
|
||||
save({ [field]: value });
|
||||
}
|
||||
|
||||
// The markup an upload is inserted as, by file type. Anything else (a PDF,
|
||||
// a text file) becomes a link.
|
||||
function tagFor(url) {
|
||||
if (/\.(jpe?g|png|gif|webp)$/i.test(url)) return `<img src="${url}" alt="">`;
|
||||
if (/\.(mp4|webm)$/i.test(url)) return `<video src="${url}" controls loop muted></video>`;
|
||||
if (/\.(mp3|ogg)$/i.test(url)) return `<audio src="${url}" controls></audio>`;
|
||||
return `<a href="${url}" target="_blank">${url.split("/").pop()}</a>`;
|
||||
}
|
||||
|
||||
async function upload(event) {
|
||||
const file = event.target.files?.[0];
|
||||
event.target.value = "";
|
||||
if (!file) return;
|
||||
uploading.value = true;
|
||||
try {
|
||||
const body = new FormData();
|
||||
body.append("file", file);
|
||||
const { url } = await api.post("/messages/upload", body, { fallback: "Upload failed" });
|
||||
const tag = tagFor(url);
|
||||
const tag = tagFor(await uploadFile(file));
|
||||
if (!codeEditor.value?.insert(tag)) draftHtml.value += `\n${tag}`;
|
||||
} catch (err) {
|
||||
emit("error", err);
|
||||
@@ -121,91 +110,93 @@ 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>
|
||||
<Teleport to="body">
|
||||
<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>
|
||||
<label class="field">
|
||||
<span>Title</span>
|
||||
<input
|
||||
type="number"
|
||||
:value="box[field]"
|
||||
@change="saveNumber(field, $event)"
|
||||
@keydown.enter="$event.target.blur()"
|
||||
type="text"
|
||||
:value="box.title ?? ''"
|
||||
placeholder="(label in edit mode)"
|
||||
@change="save({ title: $event.target.value })"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-wrap gap-x-4 gap-y-1">
|
||||
<label class="check">
|
||||
<input type="checkbox" :checked="box.framed" @change="save({ framed: $event.target.checked })" />
|
||||
Frame
|
||||
<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>
|
||||
<label class="check">
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="box.hideOnMobile"
|
||||
@change="save({ hideOnMobile: $event.target.checked })"
|
||||
/>
|
||||
Hide on mobile
|
||||
</label>
|
||||
<label class="check" title="Only signed-in admins are sent this box">
|
||||
<input type="checkbox" :checked="box.adminOnly" @change="save({ adminOnly: $event.target.checked })" />
|
||||
Admin only
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<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">
|
||||
<small class="text-muted">
|
||||
Runs sandboxed: scripts work, cookies and /api don't. fetch() only reaches
|
||||
keyless CORS-open APIs. Ctrl+S saves.
|
||||
</small>
|
||||
<input
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
accept="image/*,video/mp4,video/webm,audio/mpeg,audio/ogg"
|
||||
hidden
|
||||
@change="upload"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
class="save"
|
||||
:disabled="uploading"
|
||||
title="Upload a file and insert it at the cursor"
|
||||
@click="fileInput.click()"
|
||||
>
|
||||
{{ uploading ? "Uploading…" : "Insert upload" }}
|
||||
</button>
|
||||
<button type="button" class="save" :disabled="!dirty || saving" @click="saveHtml">
|
||||
{{ saving ? "Saving…" : dirty ? "Save" : "Saved" }}
|
||||
</button>
|
||||
</footer>
|
||||
</template>
|
||||
</aside>
|
||||
<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 flex-wrap gap-x-4 gap-y-1">
|
||||
<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>
|
||||
<label class="check" title="Only signed-in admins are sent this box">
|
||||
<input type="checkbox" :checked="box.adminOnly" @change="save({ adminOnly: $event.target.checked })" />
|
||||
Admin only
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<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">
|
||||
<small class="text-muted">
|
||||
Runs sandboxed: scripts work, cookies and /api don't. fetch() only reaches
|
||||
keyless CORS-open APIs. Ctrl+S saves.
|
||||
</small>
|
||||
<input
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
accept="image/*,video/mp4,video/webm,audio/mpeg,audio/ogg"
|
||||
hidden
|
||||
@change="upload"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
class="save"
|
||||
:disabled="uploading"
|
||||
title="Upload a file and insert it at the cursor"
|
||||
@click="fileInput.click()"
|
||||
>
|
||||
{{ uploading ? "Uploading…" : "Insert upload" }}
|
||||
</button>
|
||||
<button type="button" class="save" :disabled="!dirty || saving" @click="saveHtml">
|
||||
{{ saving ? "Saving…" : dirty ? "Save" : "Saved" }}
|
||||
</button>
|
||||
</footer>
|
||||
</template>
|
||||
</aside>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -131,11 +131,11 @@ const galleryHtml = (srcs) => `<style>
|
||||
${srcs.map((src) => ` <img src="${src}" alt="">`).join("\n")}
|
||||
</div>`;
|
||||
|
||||
const VIDEO = `<style>
|
||||
const videoHtml = (src) => `<style>
|
||||
body { padding: 0; overflow: hidden; background: #000; }
|
||||
video { display: block; width: 100%; height: 100%; object-fit: contain; }
|
||||
</style>
|
||||
<video src="/img/demoman/1760582395316219.webm" controls loop muted></video>`;
|
||||
<video src="${src}" controls loop muted></video>`;
|
||||
|
||||
/**
|
||||
* Menu entries; `size` is [w, h] in canvas px. `adminOnly` sets the new
|
||||
@@ -147,10 +147,10 @@ export const TEMPLATES = [
|
||||
{ label: "Text", title: "Text", html: TEXT, size: [400, 260] },
|
||||
{ label: "Image / GIF", title: "Image", html: imageHtml("/img/memes/fire-woman.gif"), size: [240, 240], framed: false },
|
||||
{ label: "Image gallery", title: "Gallery", html: galleryHtml(["/img/memes/fire-woman.gif", "/img/memes/epic.jpeg"]), size: [460, 320] },
|
||||
{ label: "Video", title: "Video", html: VIDEO, size: [400, 260], framed: false },
|
||||
{ label: "Video", title: "Video", html: videoHtml("/img/demoman/1760582395316219.webm"), size: [400, 260], framed: false },
|
||||
{ label: "Clock", title: "Clock", html: CLOCK, size: [240, 120] },
|
||||
{ label: "Weather", title: "Weather", html: WEATHER, size: [240, 170] },
|
||||
{ label: "Empty HTML", title: "HTML", html: EMPTY, size: [300, 200] },
|
||||
];
|
||||
|
||||
export { imageHtml, galleryHtml };
|
||||
export { imageHtml, galleryHtml, videoHtml };
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Uploading a file for use in a box: stores it through the chat's upload
|
||||
* endpoint (public, unguessable URLs under /uploads/) and says what markup
|
||||
* shows it. Used by BoxEditor's "Insert upload" and by dropping files onto
|
||||
* the canvas in edit mode.
|
||||
*
|
||||
* Never the private upload directory: that one is behind a cookie check, and
|
||||
* a box's sandboxed frame loads as an opaque origin, so its requests may not
|
||||
* carry the cookie.
|
||||
*/
|
||||
import { api } from "@/api";
|
||||
|
||||
/** Upload one file; resolves to its URL. Throws an ApiError on failure. */
|
||||
export async function uploadFile(file) {
|
||||
const body = new FormData();
|
||||
body.append("file", file);
|
||||
const { url } = await api.post("/messages/upload", body, { fallback: `Upload of ${file.name} failed` });
|
||||
return url;
|
||||
}
|
||||
|
||||
export const isImage = (url) => /\.(jpe?g|png|gif|webp)$/i.test(url);
|
||||
export const isVideo = (url) => /\.(mp4|webm)$/i.test(url);
|
||||
|
||||
/** The markup an uploaded file is shown with, by type. Anything else (a PDF,
|
||||
* a text file) becomes a link. */
|
||||
export function tagFor(url) {
|
||||
if (isImage(url)) return `<img src="${url}" alt="">`;
|
||||
if (isVideo(url)) return `<video src="${url}" controls loop muted></video>`;
|
||||
if (/\.(mp3|ogg)$/i.test(url)) return `<audio src="${url}" controls></audio>`;
|
||||
return `<a href="${url}" target="_blank">${url.split("/").pop()}</a>`;
|
||||
}
|
||||
|
||||
/** Natural size of an image or video file, or null if it can't be read. */
|
||||
export function mediaSize(file) {
|
||||
return new Promise((resolve) => {
|
||||
const src = URL.createObjectURL(file);
|
||||
const done = (size) => {
|
||||
URL.revokeObjectURL(src);
|
||||
resolve(size);
|
||||
};
|
||||
if (file.type.startsWith("video/")) {
|
||||
const v = document.createElement("video");
|
||||
v.onloadedmetadata = () => done({ w: v.videoWidth, h: v.videoHeight });
|
||||
v.onerror = () => done(null);
|
||||
v.src = src;
|
||||
} else {
|
||||
const img = new Image();
|
||||
img.onload = () => done({ w: img.naturalWidth, h: img.naturalHeight });
|
||||
img.onerror = () => done(null);
|
||||
img.src = src;
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user