Show edited renders in thumbnails and paste to a filmstrip selection
The grid and filmstrip only ever showed the import thumbnail, so edits and virtual copies looked untouched. Edited tiles now render client-side with the same shader as the canvas, once each, from the small cached thumbnail. The filmstrip also gains Cmd/Shift multi-select so Paste, Copy and Reset can act on several photos from the toolbar or the right-click menu.
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import type { Edits, PhotoRecord } from '../types';
|
||||
import { isEdited } from './edits';
|
||||
import { Renderer, rotatedBounds } from '../gl/renderer';
|
||||
|
||||
/**
|
||||
* Thumbnails of edited photos.
|
||||
*
|
||||
* The server thumbnail is always the *undeveloped* frame, so the grid and the
|
||||
* filmstrip would otherwise show the import look no matter how far a photo was
|
||||
* edited. These are rendered on demand with the same shader the canvas uses,
|
||||
* from the already-cached small thumbnail (not the multi-megapixel proxy), and
|
||||
* kept as data URLs. One renderer does every tile: a WebGL context per picture
|
||||
* would exhaust the browser's limit long before a shoot.
|
||||
*/
|
||||
|
||||
/** Long edge of a rendered thumb; twice the largest card, so it stays sharp. */
|
||||
const RENDER_EDGE = 384;
|
||||
const CACHE_LIMIT = 700;
|
||||
|
||||
const cache = new Map<string, string>();
|
||||
|
||||
/**
|
||||
* Edits are immutable: every change stores a new object (undo re-uses an old
|
||||
* one). A per-object version number is therefore a cheap, exact cache key — far
|
||||
* cheaper than serializing the whole edit for every visible card on every render.
|
||||
*/
|
||||
const versions = new WeakMap<Edits, number>();
|
||||
let versionSeq = 0;
|
||||
|
||||
function versionOf(edits: Edits): number {
|
||||
let version = versions.get(edits);
|
||||
if (version === undefined) {
|
||||
version = ++versionSeq;
|
||||
versions.set(edits, version);
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
function keyOf(photo: PhotoRecord): string {
|
||||
return `${photo.id}|${versionOf(photo.edits)}`;
|
||||
}
|
||||
|
||||
let renderer: Renderer | null = null;
|
||||
let rendererFailed = false;
|
||||
|
||||
function getRenderer(): Renderer | null {
|
||||
if (renderer) return renderer;
|
||||
if (rendererFailed) return null;
|
||||
try {
|
||||
renderer = new Renderer(document.createElement('canvas'));
|
||||
} catch {
|
||||
// No WebGL2: tiles quietly keep their plain thumbnails.
|
||||
rendererFailed = true;
|
||||
}
|
||||
return renderer;
|
||||
}
|
||||
|
||||
/** The small picture the browser already has, decoded for the shader. */
|
||||
async function loadThumb(photo: PhotoRecord): Promise<ImageBitmap | null> {
|
||||
const src = photo.thumbUrl ?? photo.thumb;
|
||||
if (!src) return null;
|
||||
try {
|
||||
const res = await fetch(src, { credentials: 'same-origin' });
|
||||
if (!res.ok) return null;
|
||||
return await createImageBitmap(await res.blob(), { imageOrientation: 'from-image' });
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function renderThumb(photo: PhotoRecord): Promise<string | null> {
|
||||
const r = getRenderer();
|
||||
if (!r) return null;
|
||||
const bitmap = await loadThumb(photo);
|
||||
if (!bitmap) return null;
|
||||
try {
|
||||
const crop = photo.edits.crop;
|
||||
const frame = rotatedBounds(bitmap.width, bitmap.height, crop.angle, crop.quarter);
|
||||
const cropW = Math.max(1, frame.w * crop.w);
|
||||
const cropH = Math.max(1, frame.h * crop.h);
|
||||
const scale = Math.min(2, RENDER_EDGE / Math.max(cropW, cropH));
|
||||
const width = Math.max(16, Math.round(cropW * scale));
|
||||
const height = Math.max(16, Math.round(cropH * scale));
|
||||
r.setImage(bitmap);
|
||||
// `lens: null` clears the previous photo's calibration maps: this renderer
|
||||
// is shared, and a stale map would warp the next thumbnail.
|
||||
r.render(photo.edits, { width, height, lens: null, upscale: scale > 1.02 });
|
||||
return r.canvas.toDataURL('image/jpeg', 0.74);
|
||||
} finally {
|
||||
bitmap.close();
|
||||
}
|
||||
}
|
||||
|
||||
interface Job {
|
||||
photo: PhotoRecord;
|
||||
key: string;
|
||||
waiters: ((url: string | null) => void)[];
|
||||
}
|
||||
|
||||
const jobs: Job[] = [];
|
||||
let pumping = false;
|
||||
|
||||
function enqueue(photo: PhotoRecord, key: string): Promise<string | null> {
|
||||
return new Promise((resolve) => {
|
||||
const existing = jobs.find((job) => job.key === key);
|
||||
if (existing) {
|
||||
existing.waiters.push(resolve);
|
||||
return;
|
||||
}
|
||||
jobs.push({ photo, key, waiters: [resolve] });
|
||||
void pump();
|
||||
});
|
||||
}
|
||||
|
||||
/** One render at a time, yielding between tiles so the UI stays responsive. */
|
||||
async function pump() {
|
||||
if (pumping) return;
|
||||
pumping = true;
|
||||
while (jobs.length) {
|
||||
const job = jobs.shift()!;
|
||||
let url: string | null = null;
|
||||
try {
|
||||
url = await renderThumb(job.photo);
|
||||
} catch {
|
||||
url = null;
|
||||
}
|
||||
if (url) {
|
||||
cache.set(job.key, url);
|
||||
while (cache.size > CACHE_LIMIT) {
|
||||
const first = cache.keys().next();
|
||||
if (first.done) break;
|
||||
cache.delete(first.value);
|
||||
}
|
||||
}
|
||||
for (const waiter of job.waiters) waiter(url);
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
pumping = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The edited thumbnail for a photo, or null while there is none to show.
|
||||
*
|
||||
* Only edited photos are rendered, and only once the tile is near the viewport
|
||||
* (`enabled`), so a long shoot does not pay for pictures nobody can see. Until
|
||||
* a render lands the caller keeps showing the plain thumbnail.
|
||||
*/
|
||||
export function useEditedThumb(photo: PhotoRecord, enabled = true): string | null {
|
||||
const key = isEdited(photo.edits) ? keyOf(photo) : '';
|
||||
const [src, setSrc] = useState<string | null>(() => (key ? cache.get(key) ?? null : null));
|
||||
|
||||
useEffect(() => {
|
||||
if (!key) {
|
||||
setSrc(null);
|
||||
return;
|
||||
}
|
||||
const hit = cache.get(key);
|
||||
if (hit) {
|
||||
setSrc(hit);
|
||||
return;
|
||||
}
|
||||
if (!enabled) return;
|
||||
let alive = true;
|
||||
// Coalesce slider drags: only the setting you stopped on gets rendered.
|
||||
const timer = window.setTimeout(() => {
|
||||
void enqueue(photo, key).then((url) => {
|
||||
if (alive && url) setSrc(url);
|
||||
});
|
||||
}, 120);
|
||||
return () => {
|
||||
alive = false;
|
||||
window.clearTimeout(timer);
|
||||
};
|
||||
}, [key, photo, enabled]);
|
||||
|
||||
return src;
|
||||
}
|
||||
Reference in New Issue
Block a user