import { useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from "react"; import { useStore } from "zustand"; import { onEditorEvent } from "@/editor/api"; import { isSvgPath } from "@/features/viewers/fileKinds"; import { useForwardFocus } from "@/features/viewers/forwardFocus"; import { useT, useTx } from "@/platform/i18n"; import { Button, IconButton, Select, T } from "@/ui"; import { useEditorDocuments } from "@/workbench/contributions"; import { emptyStagedStatus, stagedDiffPath, stagedDocumentAtPath } from "./stagedDocument"; import { sideFile, sideLabel, type DiffData } from "./diffInput"; import { loadImageFrames, type ImageFrame } from "./imageDiffFrames"; import { canComparePixels, imageBounds, paintPixelDifference, type ImageSize } from "./imagePixels"; import { useVcs } from "./store"; import styles from "./ImageDiff.module.css"; function frameSize(frame: ImageFrame) { return frame.kind === "ready" ? [frame.size] : []; } function Pane({ label, detail, children }: { label: string; detail?: ReactNode; children: ReactNode }) { return (
{label} {detail && {detail}}
{children}
); } function imageStyle(size: ImageSize, bounds: ImageSize): CSSProperties { return { width: (100 * size.width) / bounds.width + "%", height: (100 * size.height) / bounds.height + "%" }; } function PixelDifference({ before, after, bounds }: { before: ImageFrame; after: ImageFrame; bounds: ImageSize }) { const tx = useTx(); const t = useT(); const canvas = useRef(null); const [result, setResult] = useState(null); const extent = imageBounds([...frameSize(before), ...frameSize(after)]); const empty = before.kind === "absent" && after.kind === "absent"; const comparable = before.kind !== "error" && after.kind !== "error" && canComparePixels(extent); useEffect(() => { const mask = canvas.current; if (!mask || !comparable) return; try { mask.width = extent.width; mask.height = extent.height; const ctx = mask.getContext("2d", { willReadFrequently: true }); if (!ctx) throw new Error("Canvas unavailable"); const pixels = (frame: ImageFrame) => { if (frame.kind !== "ready") return null; const { width, height } = frame.size; ctx.clearRect(0, 0, width, height); ctx.drawImage(frame.image, 0, 0); return ctx.getImageData(0, 0, width, height); }; const left = pixels(before); const right = pixels(after); const out = ctx.createImageData(extent.width, extent.height); const count = paintPixelDifference(left, right, out); ctx.putImageData(out, 0, 0); setResult(count); } catch { setResult("error"); } return () => { mask.width = mask.height = 0; }; }, [before, after, comparable, extent.width, extent.height]); const message = before.kind === "error" || after.kind === "error" || result === "error" ? tx("Pixel differences unavailable") : empty ? tx("No images to compare") : !comparable ? tx("Image dimensions exceed the pixel comparison limit") : null; const count = empty ? 0 : result; return ( {typeof count === "number" && t("vcs.imageDiff.changedPixels", { count })}} > {message ? ( {message} ) : (
)}
); } export function ImageDiff({ data, onSource, toolbarActions, forwardFocus = false, }: { data: DiffData; onSource?: () => void; toolbarActions?: ReactNode; forwardFocus?: boolean; }) { const root = useRef(null); useForwardFocus(root); const tx = useTx(); const { left, middle, right, path } = data; const sides = useMemo(() => (middle ? [left, middle, right] : [left, right]), [left, middle, right]); const immutable = sides.every( (side) => side.type === "text" || (side.type === "rev" && /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(side.rev)), ); const version = useVcs((s) => (immutable ? 0 : s.version)); const stagedPath = stagedDiffPath(data); const indexDoc = useEditorDocuments(() => stagedPath && isSvgPath(stagedPath) ? stagedDocumentAtPath(stagedPath) : undefined, ); const stagedDraft = useStore(indexDoc?.status ?? emptyStagedStatus, (s) => s.base && s.text !== s.base.text ? s.text : null, ); const [documentVersion, setDocumentVersion] = useState(0); useEffect(() => { const files = sides.map((side) => sideFile(side, path)); if (!files.some(Boolean)) return; return onEditorEvent((e) => { if ((e.type === "docSaved" || (e.type === "docChanged" && isSvgPath(e.path))) && files.includes(e.path)) setDocumentVersion((n) => n + 1); }); }, [path, sides]); const request = useMemo( () => ({ path, sides, version, documentVersion, stagedDraft }), [path, sides, version, documentVersion, stagedDraft], ); const [loaded, setLoaded] = useState<{ request: typeof request; frames: ImageFrame[] }>(); useEffect(() => { let cancelled = false; void loadImageFrames(request.path, request.sides, () => cancelled).then((frames) => { if (!cancelled) setLoaded({ request, frames }); }); return () => { cancelled = true; }; }, [request]); const frames = loaded?.request === request ? loaded.frames : undefined; const labels = sides.map((side) => tx(sideLabel(side))); const [highlight, setHighlight] = useState(false); const [fit, setFit] = useState(true); const [pair, setPair] = useState("0-1"); const pairs: readonly (readonly [number, number])[] = middle ? [ [0, 1], [1, 2], [0, 2], ] : [[0, 1]]; const selected = pairs.find((p) => p.join("-") === pair) ?? pairs[0]!; const visible = highlight ? selected : sides.map((_, i) => i); const bounds = imageBounds(visible.flatMap((i) => (frames?.[i] ? frameSize(frames[i]!) : []))); const hasImage = frames?.some((frame) => frame.kind === "ready"); return (
{highlight && middle && (