Uploads: frame a photo before it goes (crop, zoom, position)
A photo field with a shape (crop: square for people, 16:9 for event photos, covers and hero photos, the photo's own for block images and schedule graphics) opens Frame the photo when a JPEG, PNG or WebP is uploaded: drag or arrow keys to place it, a zoom slider, and a shape menu. The framed part is drawn on a canvas and uploaded in place of the original; Upload as is skips it. Logos, SVGs and GIFs go as they are. No new dependency and nothing stored beside the picture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
034ad95b8a
commit
07be48d770
9 changed files with 298 additions and 10 deletions
|
|
@ -47,6 +47,7 @@ import { useAuth } from "../../lib/auth.tsx";
|
|||
import { clock, parseClock } from "../../lib/clock.ts";
|
||||
import { canWrite as roleCanWrite } from "../../lib/roles.ts";
|
||||
import { useSmallImage } from "../../lib/useSmallImage.ts";
|
||||
import CropDialog, { type Crop } from "./CropDialog.tsx";
|
||||
|
||||
export type MediaItem = {
|
||||
id: number;
|
||||
|
|
@ -187,29 +188,41 @@ export function UploadButton({
|
|||
multiple = false,
|
||||
label = "Upload",
|
||||
folder,
|
||||
crop,
|
||||
}: {
|
||||
onUploaded: (item: MediaItem) => void;
|
||||
multiple?: boolean;
|
||||
label?: string;
|
||||
/** Where it goes; blank: the server's default, images. */
|
||||
folder?: string;
|
||||
/** The field's shape: a photo is framed (CropDialog) before it goes. */
|
||||
crop?: Crop;
|
||||
}) {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const keepId = useId();
|
||||
const [keepOriginal, setKeepOriginal] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [framing, setFraming] = useState<File | null>(null);
|
||||
// Captions are kept as they are, so there's no original to keep.
|
||||
const captions = folder === "captions";
|
||||
|
||||
async function send(files: FileList | null) {
|
||||
if (!files || files.length === 0) return;
|
||||
// A single photo for a field with a shape is framed first; SVGs and
|
||||
// GIFs (which would lose their animation) go as they are.
|
||||
function choose(files: FileList | null) {
|
||||
const one = files?.length === 1 ? files[0] : null;
|
||||
if (crop && one && /^image\/(jpeg|png|webp)$/.test(one.type)) setFraming(one);
|
||||
else send(files ? Array.from(files) : []);
|
||||
}
|
||||
|
||||
async function send(files: File[]) {
|
||||
if (files.length === 0) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
// One at a time: each is up to 25 MB, and a failure should
|
||||
// name the file it happened to.
|
||||
for (const file of Array.from(files)) {
|
||||
for (const file of files) {
|
||||
try {
|
||||
onUploaded(await uploadImage(file, keepOriginal, folder));
|
||||
} catch (err) {
|
||||
|
|
@ -241,7 +254,7 @@ export function UploadButton({
|
|||
accept={captions ? ".vtt,text/vtt" : "image/jpeg,image/png,image/gif,image/webp,image/svg+xml"}
|
||||
multiple={multiple}
|
||||
className="hidden"
|
||||
onChange={(e) => send(e.target.files)}
|
||||
onChange={(e) => choose(e.target.files)}
|
||||
/>
|
||||
{!captions && (
|
||||
<label htmlFor={keepId} className="flex items-center gap-1.5 text-xs text-muted">
|
||||
|
|
@ -257,6 +270,17 @@ export function UploadButton({
|
|||
)}
|
||||
</div>
|
||||
{error && <p className="mt-1 text-xs text-danger">{error}</p>}
|
||||
{framing && crop && (
|
||||
<CropDialog
|
||||
file={framing}
|
||||
crop={crop}
|
||||
onDone={(file) => {
|
||||
setFraming(null);
|
||||
if (file) send([file]);
|
||||
else if (inputRef.current) inputRef.current.value = "";
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -535,6 +559,7 @@ export function ImageInput({
|
|||
resolve,
|
||||
folder,
|
||||
videoUrl,
|
||||
crop,
|
||||
locked,
|
||||
className,
|
||||
onChange,
|
||||
|
|
@ -546,6 +571,8 @@ export function ImageInput({
|
|||
folder?: string;
|
||||
/** A video link to take a frame from (the field's `frameFrom`). */
|
||||
videoUrl?: string | null;
|
||||
/** The field's `crop`: uploads are framed to it first. */
|
||||
crop?: Crop;
|
||||
locked: boolean;
|
||||
className: string;
|
||||
onChange: (value: string) => void;
|
||||
|
|
@ -587,7 +614,7 @@ export function ImageInput({
|
|||
/>
|
||||
{editable && (
|
||||
<div className="flex flex-wrap items-start gap-2">
|
||||
<UploadButton folder={folder} onUploaded={(item) => onChange(item.url)} />
|
||||
<UploadButton folder={folder} crop={crop} onUploaded={(item) => onChange(item.url)} />
|
||||
<button type="button" className={button} onClick={() => setPicking(true)}>
|
||||
Choose from library
|
||||
</button>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue