fix: preserve manual detail cutlines

This commit is contained in:
Your Name 2026-06-01 15:06:17 +08:00
parent a3c291a1e0
commit 00ae2d4e59

View File

@ -197,6 +197,12 @@ export function AgentDetailReferenceStitchDialog({
const [warningText, setWarningText] = useState<string | null>(null);
const [previewImageLoadFailed, setPreviewImageLoadFailed] = useState(false);
const [passthroughSegmentIndexes, setPassthroughSegmentIndexes] = useState<number[]>([]);
const hasManualCutLineChangesRef = useRef(false);
const onPreparedRef = useRef(onPrepared);
useEffect(() => {
onPreparedRef.current = onPrepared;
}, [onPrepared]);
const validCards = useMemo(
() =>
@ -210,8 +216,8 @@ export function AgentDetailReferenceStitchDialog({
() =>
validCards
.map(
(card) =>
`${card.id}::${String(card.imageUrl || "").trim()}::${String(card.previewUrl || "").trim()}::${card.width || ""}x${card.height || ""}`
(card, index) =>
`${card.id}::${String(card.imageUrl || "").trim()}::${card.sortOrder ?? index}`
)
.join("|"),
[validCards]
@ -265,12 +271,14 @@ export function AgentDetailReferenceStitchDialog({
setWarningText(null);
setPreviewImageLoadFailed(false);
setPassthroughSegmentIndexes([]);
hasManualCutLineChangesRef.current = false;
return;
}
if (setupCards.length === 0) {
setSnapshot(null);
setCutLines([]);
hasManualCutLineChangesRef.current = false;
setIsLoading(false);
setIsPreparing(false);
setErrorText(`当前没有可拼合的${sourceLabel}`);
@ -282,6 +290,7 @@ export function AgentDetailReferenceStitchDialog({
setSnapshot(optimisticSnapshot);
setCutLines(optimisticSnapshot.defaultCutLines);
hasManualCutLineChangesRef.current = false;
setPassthroughSegmentIndexes([]);
setErrorText(null);
setWarningText(null);
@ -341,12 +350,15 @@ export function AgentDetailReferenceStitchDialog({
};
setSnapshot(preciseSnapshot);
setPreviewImageLoadFailed(!previewUrl);
setCutLines((current) =>
areCutLinesEqual(current, optimisticSnapshot.defaultCutLines)
setCutLines((current) => {
if (hasManualCutLineChangesRef.current) {
return scaleCutLines(current, optimisticSnapshot.height, height);
}
return areCutLinesEqual(current, optimisticSnapshot.defaultCutLines)
? defaultCutLines
: scaleCutLines(current, optimisticSnapshot.height, height)
);
if (onPrepared) {
: scaleCutLines(current, optimisticSnapshot.height, height);
});
if (onPreparedRef.current) {
const dimensionByCardId = new Map(
sourceSegments.map((segment) => [
segment.card.id,
@ -357,7 +369,7 @@ export function AgentDetailReferenceStitchDialog({
},
])
);
onPrepared(
onPreparedRef.current(
setupCards.map((card, index) => {
const dimensions = dimensionByCardId.get(card.id);
return {
@ -394,7 +406,7 @@ export function AgentDetailReferenceStitchDialog({
return () => {
cancelled = true;
};
}, [onPrepared, open, setupCards, sourceLabel]);
}, [open, setupCards, sourceLabel]);
useEffect(() => {
const validIndexes = new Set(sliceSegments.map((segment) => segment.index));
@ -532,10 +544,12 @@ export function AgentDetailReferenceStitchDialog({
const handleResetCutLines = useCallback(() => {
if (!snapshot) return;
hasManualCutLineChangesRef.current = false;
setCutLines(snapshot.defaultCutLines);
}, [snapshot]);
const handleClearCutLines = useCallback(() => {
hasManualCutLineChangesRef.current = true;
setCutLines([]);
setDraggingLineIndex(null);
setIsAddingCutLine(false);
@ -543,6 +557,7 @@ export function AgentDetailReferenceStitchDialog({
}, []);
const handleRemoveCutLine = useCallback((index: number) => {
hasManualCutLineChangesRef.current = true;
setCutLines((current) => current.filter((_, currentIndex) => currentIndex !== index));
}, []);
@ -573,6 +588,7 @@ export function AgentDetailReferenceStitchDialog({
toast.error("当前位置无法添加切线,请换一个位置");
return;
}
hasManualCutLineChangesRef.current = true;
setCutLines((current) => [...current, Math.round(nextY)].sort((left, right) => left - right));
setIsAddingCutLine(false);
setPreviewCutLineY(null);
@ -589,6 +605,7 @@ export function AgentDetailReferenceStitchDialog({
} catch {
// Older browsers may not support pointer capture for this target.
}
hasManualCutLineChangesRef.current = true;
setDraggingLineIndex(index);
},
[]