2026-03-25 09:27:12 +08:00
|
|
|
import { FlipHorizontal, FlipVertical, RotateCcw, RotateCw } from "lucide-react";
|
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
2026-03-23 01:45:58 +08:00
|
|
|
import { ProgressCard } from "@/components/common/progress-card";
|
2026-03-25 09:27:12 +08:00
|
|
|
import { useToolProcessor } from "@/hooks/use-tool-processor";
|
|
|
|
|
import { useFileStore } from "@/stores/file-store";
|
2026-03-22 03:56:44 +08:00
|
|
|
|
2026-03-23 12:51:29 +08:00
|
|
|
export interface PreviewTransform {
|
|
|
|
|
rotate: number;
|
|
|
|
|
flipH: boolean;
|
|
|
|
|
flipV: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface RotateSettingsProps {
|
|
|
|
|
onPreviewTransform?: (transform: PreviewTransform) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function RotateSettings({ onPreviewTransform }: RotateSettingsProps) {
|
2026-03-22 03:56:44 +08:00
|
|
|
const { files } = useFileStore();
|
2026-03-25 09:27:12 +08:00
|
|
|
const { processFiles, processAllFiles, processing, error, progress } = useToolProcessor("rotate");
|
2026-03-22 03:56:44 +08:00
|
|
|
|
2026-03-24 00:41:54 +08:00
|
|
|
// Quick rotation in 90° steps: 0, 90, 180, 270
|
|
|
|
|
const [rotation, setRotation] = useState(0);
|
|
|
|
|
// Fine straighten adjustment: -45 to +45
|
|
|
|
|
const [straighten, setStraighten] = useState(0);
|
2026-03-22 03:56:44 +08:00
|
|
|
const [flipH, setFlipH] = useState(false);
|
|
|
|
|
const [flipV, setFlipV] = useState(false);
|
|
|
|
|
|
2026-03-24 00:41:54 +08:00
|
|
|
const totalAngle = rotation + straighten;
|
|
|
|
|
|
2026-03-23 12:51:29 +08:00
|
|
|
// Emit preview transform on every change
|
|
|
|
|
useEffect(() => {
|
2026-03-24 00:41:54 +08:00
|
|
|
onPreviewTransform?.({ rotate: totalAngle, flipH, flipV });
|
|
|
|
|
}, [totalAngle, flipH, flipV, onPreviewTransform]);
|
2026-03-23 12:51:29 +08:00
|
|
|
|
2026-03-24 00:41:54 +08:00
|
|
|
// Reset controls after successful processing
|
|
|
|
|
const prevProcessing = useRef(processing);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (prevProcessing.current && !processing && !error) {
|
|
|
|
|
setRotation(0);
|
|
|
|
|
setStraighten(0);
|
|
|
|
|
setFlipH(false);
|
|
|
|
|
setFlipV(false);
|
|
|
|
|
}
|
|
|
|
|
prevProcessing.current = processing;
|
|
|
|
|
}, [processing, error]);
|
|
|
|
|
|
|
|
|
|
const rotateLeft = () => setRotation((r) => r - 90);
|
|
|
|
|
const rotateRight = () => setRotation((r) => r + 90);
|
2026-03-22 03:56:44 +08:00
|
|
|
|
|
|
|
|
const handleProcess = () => {
|
2026-03-24 00:41:54 +08:00
|
|
|
const backendAngle = ((totalAngle % 360) + 360) % 360;
|
2026-03-23 14:11:08 +08:00
|
|
|
const settings = {
|
2026-03-24 00:41:54 +08:00
|
|
|
angle: backendAngle,
|
2026-03-22 03:56:44 +08:00
|
|
|
horizontal: flipH,
|
|
|
|
|
vertical: flipV,
|
2026-03-23 14:11:08 +08:00
|
|
|
};
|
|
|
|
|
if (files.length > 1) {
|
|
|
|
|
processAllFiles(files, settings);
|
|
|
|
|
} else {
|
|
|
|
|
processFiles(files, settings);
|
|
|
|
|
}
|
2026-03-22 03:56:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hasFile = files.length > 0;
|
2026-03-24 00:41:54 +08:00
|
|
|
const hasChanges = totalAngle !== 0 || flipH || flipV;
|
2026-03-22 03:56:44 +08:00
|
|
|
|
2026-03-22 11:04:20 +08:00
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (hasFile && hasChanges && !processing) handleProcess();
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-24 00:41:54 +08:00
|
|
|
const handleReset = () => {
|
|
|
|
|
setRotation(0);
|
|
|
|
|
setStraighten(0);
|
|
|
|
|
setFlipH(false);
|
|
|
|
|
setFlipV(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Display angle normalized to 0-359
|
|
|
|
|
const displayAngle = ((totalAngle % 360) + 360) % 360;
|
|
|
|
|
|
2026-03-22 03:56:44 +08:00
|
|
|
return (
|
2026-03-22 11:04:20 +08:00
|
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
2026-03-24 00:41:54 +08:00
|
|
|
{/* Quick rotate */}
|
2026-03-22 03:56:44 +08:00
|
|
|
<div>
|
2026-03-24 00:41:54 +08:00
|
|
|
<label className="text-xs text-muted-foreground">Rotate</label>
|
|
|
|
|
<div className="flex items-center gap-2 mt-1">
|
2026-03-22 03:56:44 +08:00
|
|
|
<button
|
2026-03-22 11:04:20 +08:00
|
|
|
type="button"
|
2026-03-22 03:56:44 +08:00
|
|
|
onClick={rotateLeft}
|
2026-03-24 00:41:54 +08:00
|
|
|
className="flex-1 flex items-center justify-center gap-1.5 py-2.5 rounded-lg bg-muted text-muted-foreground hover:bg-primary hover:text-primary-foreground transition-colors text-sm font-medium"
|
|
|
|
|
title="Rotate 90° counter-clockwise"
|
2026-03-22 03:56:44 +08:00
|
|
|
>
|
|
|
|
|
<RotateCcw className="h-4 w-4" />
|
2026-03-24 00:41:54 +08:00
|
|
|
Left
|
2026-03-22 03:56:44 +08:00
|
|
|
</button>
|
2026-03-24 00:41:54 +08:00
|
|
|
<div className="px-3 py-1.5 rounded-md bg-background border border-border text-center min-w-[4rem]">
|
2026-03-25 09:27:12 +08:00
|
|
|
<span className="text-sm font-mono font-medium tabular-nums">{displayAngle}°</span>
|
2026-03-24 00:41:54 +08:00
|
|
|
</div>
|
2026-03-22 03:56:44 +08:00
|
|
|
<button
|
2026-03-22 11:04:20 +08:00
|
|
|
type="button"
|
2026-03-22 03:56:44 +08:00
|
|
|
onClick={rotateRight}
|
2026-03-24 00:41:54 +08:00
|
|
|
className="flex-1 flex items-center justify-center gap-1.5 py-2.5 rounded-lg bg-muted text-muted-foreground hover:bg-primary hover:text-primary-foreground transition-colors text-sm font-medium"
|
|
|
|
|
title="Rotate 90° clockwise"
|
2026-03-22 03:56:44 +08:00
|
|
|
>
|
2026-03-24 00:41:54 +08:00
|
|
|
Right
|
2026-03-22 03:56:44 +08:00
|
|
|
<RotateCw className="h-4 w-4" />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-24 00:41:54 +08:00
|
|
|
{/* Straighten */}
|
2026-03-22 03:56:44 +08:00
|
|
|
<div>
|
|
|
|
|
<div className="flex justify-between items-center">
|
2026-03-24 00:41:54 +08:00
|
|
|
<label className="text-xs text-muted-foreground">Straighten</label>
|
|
|
|
|
<span className="text-xs font-mono tabular-nums text-muted-foreground">
|
|
|
|
|
{straighten > 0 ? "+" : ""}
|
|
|
|
|
{straighten}°
|
|
|
|
|
</span>
|
2026-03-22 03:56:44 +08:00
|
|
|
</div>
|
|
|
|
|
<input
|
|
|
|
|
type="range"
|
2026-03-24 00:41:54 +08:00
|
|
|
min={-45}
|
|
|
|
|
max={45}
|
|
|
|
|
step={0.5}
|
|
|
|
|
value={straighten}
|
|
|
|
|
onChange={(e) => setStraighten(Number(e.target.value))}
|
2026-03-22 03:56:44 +08:00
|
|
|
className="w-full mt-1"
|
|
|
|
|
/>
|
2026-03-24 00:41:54 +08:00
|
|
|
<div className="flex justify-between text-[10px] text-muted-foreground mt-0.5">
|
|
|
|
|
<span>-45°</span>
|
|
|
|
|
<span>0°</span>
|
|
|
|
|
<span>+45°</span>
|
|
|
|
|
</div>
|
2026-03-22 03:56:44 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Flip buttons */}
|
|
|
|
|
<div>
|
|
|
|
|
<label className="text-xs text-muted-foreground">Flip</label>
|
|
|
|
|
<div className="flex gap-2 mt-1">
|
|
|
|
|
<button
|
2026-03-22 11:04:20 +08:00
|
|
|
type="button"
|
2026-03-22 03:56:44 +08:00
|
|
|
onClick={() => setFlipH(!flipH)}
|
2026-03-24 00:41:54 +08:00
|
|
|
className={`flex-1 flex items-center justify-center gap-1.5 py-2.5 rounded-lg text-sm font-medium transition-colors ${
|
2026-03-22 03:56:44 +08:00
|
|
|
flipH
|
|
|
|
|
? "bg-primary text-primary-foreground"
|
|
|
|
|
: "bg-muted text-muted-foreground hover:bg-primary/10"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<FlipHorizontal className="h-4 w-4" />
|
|
|
|
|
Horizontal
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
2026-03-22 11:04:20 +08:00
|
|
|
type="button"
|
2026-03-22 03:56:44 +08:00
|
|
|
onClick={() => setFlipV(!flipV)}
|
2026-03-24 00:41:54 +08:00
|
|
|
className={`flex-1 flex items-center justify-center gap-1.5 py-2.5 rounded-lg text-sm font-medium transition-colors ${
|
2026-03-22 03:56:44 +08:00
|
|
|
flipV
|
|
|
|
|
? "bg-primary text-primary-foreground"
|
|
|
|
|
: "bg-muted text-muted-foreground hover:bg-primary/10"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<FlipVertical className="h-4 w-4" />
|
|
|
|
|
Vertical
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-24 00:41:54 +08:00
|
|
|
{/* Reset all */}
|
|
|
|
|
{hasChanges && (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleReset}
|
|
|
|
|
className="w-full text-xs text-muted-foreground hover:text-foreground py-1"
|
|
|
|
|
>
|
|
|
|
|
Reset all changes
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-03-22 03:56:44 +08:00
|
|
|
{/* Error */}
|
|
|
|
|
{error && <p className="text-xs text-red-500">{error}</p>}
|
|
|
|
|
|
|
|
|
|
{/* Process */}
|
2026-03-23 01:45:58 +08:00
|
|
|
{processing ? (
|
|
|
|
|
<ProgressCard
|
|
|
|
|
active={processing}
|
|
|
|
|
phase={progress.phase === "idle" ? "uploading" : progress.phase}
|
2026-03-23 12:51:29 +08:00
|
|
|
label="Applying"
|
2026-03-23 01:45:58 +08:00
|
|
|
stage={progress.stage}
|
|
|
|
|
percent={progress.percent}
|
|
|
|
|
elapsed={progress.elapsed}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={!hasFile || !hasChanges || processing}
|
|
|
|
|
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
|
|
|
|
|
>
|
2026-03-23 14:11:08 +08:00
|
|
|
{files.length > 1 ? `Apply (${files.length} files)` : "Apply"}
|
2026-03-23 01:45:58 +08:00
|
|
|
</button>
|
|
|
|
|
)}
|
2026-03-22 11:04:20 +08:00
|
|
|
</form>
|
2026-03-22 03:56:44 +08:00
|
|
|
);
|
|
|
|
|
}
|