diff --git a/src/renderer/src/components/editor/combined-diff/CombinedDiffViewer.tsx b/src/renderer/src/components/editor/combined-diff/CombinedDiffViewer.tsx index c1ecbb9db3c..3c9c1b2d80b 100644 --- a/src/renderer/src/components/editor/combined-diff/CombinedDiffViewer.tsx +++ b/src/renderer/src/components/editor/combined-diff/CombinedDiffViewer.tsx @@ -167,7 +167,13 @@ export default function CombinedDiffViewer({ const toggleSection = useCallback( (index: number) => { - const shouldLoadAfterExpand = registry.sectionsRef.current[index]?.collapsed ?? false + const section = registry.sectionsRef.current[index] + const shouldLoadAfterExpand = section?.collapsed ?? false + const anchor = restore.scrollAnchorRef.current + if (section && !section.collapsed && anchor?.key === section.key) { + // The offset points into the body being removed; restore would land on later sections. + anchor.offset = 0 + } setSections((prev) => prev.map((s, i) => (i === index ? { ...s, collapsed: !s.collapsed } : s)) ) @@ -175,7 +181,7 @@ export default function CombinedDiffViewer({ registry.loadSchedulerRef.current.request(index) } }, - [registry.loadSchedulerRef, registry.sectionsRef] + [registry.loadSchedulerRef, registry.sectionsRef, restore.scrollAnchorRef] ) const treeNavigation = useCombinedDiffTreeNavigation({