Auto always asked for highlights −66 and shadows +52, a raw-file recipe. On a camera JPEG that flattens faces. The pull now stops at −20 and the lift at +30, and one slider can be copied on its own.
78 lines
3.3 KiB
TypeScript
78 lines
3.3 KiB
TypeScript
import { CONTROLS, MODULES, controlsForModule, type EditCommand, type Module } from "@photoreport/contracts";
|
|
import type { ApiClient } from "@photoreport/sdk";
|
|
import { cache } from "../offline/cache.js";
|
|
import { isNetworkError, offlineQueue } from "../offline/queue.js";
|
|
import { type EngineModule, loadEngine } from "./engine.js";
|
|
import { cropOf, localOutbox } from "./session.js";
|
|
|
|
/** Groups the photographer can copy. Crop is listed and starts off. */
|
|
export const SYNC_MODULES: Module[] = MODULES.filter((m) => m === "crop" || controlsForModule(m).length > 0);
|
|
|
|
/** Only the chosen controls. Other controls on the target photo stay as they are. */
|
|
export function syncDevelopCommand(controlIds: readonly string[], value: (id: string) => number): EditCommand | null {
|
|
const wanted = new Set(controlIds);
|
|
const values: Record<string, number> = {};
|
|
for (const c of CONTROLS) {
|
|
if (c.module === "crop" || !wanted.has(c.id)) continue;
|
|
const v = value(c.id);
|
|
if (Number.isFinite(v)) values[c.id] = v;
|
|
}
|
|
if (!Object.keys(values).length) return null;
|
|
return { command: "develop.set", params: { values } };
|
|
}
|
|
|
|
export function syncCropCommand(settings: string): EditCommand {
|
|
const crop = cropOf(settings);
|
|
return { command: "crop.set", params: { rect: crop.rect, angle: crop.angle, aspect: crop.aspect } };
|
|
}
|
|
|
|
export function commandsFrom(controlIds: readonly string[], crop: boolean, settings: string, value: (id: string) => number): EditCommand[] {
|
|
const out: EditCommand[] = [];
|
|
const develop = syncDevelopCommand(controlIds, value);
|
|
if (develop) out.push(develop);
|
|
if (crop) out.push(syncCropCommand(settings));
|
|
return out;
|
|
}
|
|
|
|
/** Settings of one photo: the cache, then anything newer on the server, then edits still waiting here. */
|
|
export async function settingsOf(api: ApiClient, photoId: string): Promise<string> {
|
|
const engine = await loadEngine();
|
|
let settings = engine.defaultSettings();
|
|
let seq = 0;
|
|
const cached = await cache.develop(photoId);
|
|
if (cached?.settings) {
|
|
settings = cached.settings;
|
|
seq = cached.seq;
|
|
}
|
|
if (!photoId.startsWith("local:")) {
|
|
try {
|
|
const state = await api.developState(photoId, cached ? seq : undefined);
|
|
if (!cached && state.snapshot) {
|
|
settings = JSON.stringify(state.snapshot);
|
|
seq = state.snapshotSeq;
|
|
}
|
|
for (const op of state.ops) {
|
|
if (op.seq > seq) settings = engine.apply(settings, JSON.stringify(op.command));
|
|
}
|
|
} catch (err) {
|
|
if (!cached && !isNetworkError(err)) throw err;
|
|
}
|
|
}
|
|
for (const op of localOutbox.load(photoId)) settings = engine.apply(settings, JSON.stringify(op.command));
|
|
return settings;
|
|
}
|
|
|
|
export async function commandsForPhoto(api: ApiClient, photoId: string, controlIds: readonly string[], crop: boolean): Promise<EditCommand[]> {
|
|
const engine = await loadEngine();
|
|
const settings = await settingsOf(api, photoId);
|
|
return commandsFrom(controlIds, crop, settings, (id) => engine.value(settings, id));
|
|
}
|
|
|
|
/** Queue the commands for the other selected photos. They go out with the socket, and follow a local photo. */
|
|
export function queueSync(orgId: string, photoIds: string[], commands: EditCommand[]): void {
|
|
const queue = offlineQueue(orgId);
|
|
for (const id of photoIds) for (const command of commands) queue.enqueueEdit(id, command);
|
|
}
|
|
|
|
export type { EngineModule };
|