fix scroll

This commit is contained in:
Philipinho
2026-02-01 00:39:29 +00:00
parent 040ad04a27
commit 20a7acfccc
3 changed files with 62 additions and 79 deletions
@@ -1,6 +1,6 @@
import "@/features/editor/styles/index.css";
import "./history-diff.module.css";
import { useEffect, useImperativeHandle, forwardRef, useRef } from "react";
import { useEffect } from "react";
import { EditorContent, useEditor } from "@tiptap/react";
import { mainExtensions } from "@/features/editor/extensions/extensions";
import { Title } from "@mantine/core";
@@ -12,10 +12,6 @@ import { ChangeSet, simplifyChanges } from "prosemirror-changeset";
export type DiffCounts = { added: number; deleted: number; total: number };
export type HistoryEditorHandle = {
scrollToChange: (index: number) => void;
};
export interface HistoryEditorProps {
title: string;
content: any;
@@ -24,31 +20,19 @@ export interface HistoryEditorProps {
onDiffCalculated?: (counts: DiffCounts) => void;
}
export const HistoryEditor = forwardRef<HistoryEditorHandle, HistoryEditorProps>(
function HistoryEditor(
{ title, content, previousContent, highlightChanges = true, onDiffCalculated },
ref,
) {
const editor = useEditor({
extensions: mainExtensions,
editable: false,
});
const containerRef = useRef<HTMLDivElement>(null);
const changeIndexRef = useRef<number[]>([]);
export function HistoryEditor({
title,
content,
previousContent,
highlightChanges = true,
onDiffCalculated,
}: HistoryEditorProps) {
const editor = useEditor({
extensions: mainExtensions,
editable: false,
});
useImperativeHandle(ref, () => ({
scrollToChange: (index: number) => {
if (!containerRef.current || index < 1) return;
const element = containerRef.current.querySelector(
`[data-diff-index="${index}"]`,
);
if (element) {
element.scrollIntoView({ behavior: "smooth", block: "center" });
}
},
}));
useEffect(() => {
useEffect(() => {
if (!editor || !content) return;
let decorationSet = DecorationSet.empty;
@@ -175,11 +159,6 @@ export const HistoryEditor = forwardRef<HistoryEditorHandle, HistoryEditorProps>
}
}
changeIndexRef.current = Array.from(
{ length: changeIndex },
(_, i) => i + 1,
);
decorationSet = DecorationSet.create(docNew, decorations);
} catch (e) {
console.error("History diff failed:", e);
@@ -201,16 +180,15 @@ export const HistoryEditor = forwardRef<HistoryEditorHandle, HistoryEditorProps>
});
}, [title, content, editor, previousContent, highlightChanges]);
return (
<div ref={containerRef}>
<Title order={1}>{title}</Title>
{editor && (
<EditorContent
editor={editor}
className={historyClasses.historyEditor}
/>
)}
</div>
);
},
);
return (
<div>
<Title order={1}>{title}</Title>
{editor && (
<EditorContent
editor={editor}
className={historyClasses.historyEditor}
/>
)}
</div>
);
}
@@ -7,10 +7,7 @@ import {
Switch,
Text,
} from "@mantine/core";
import {
DiffCounts,
HistoryEditorHandle,
} from "@/features/page-history/components/history-editor";
import { DiffCounts } from "@/features/page-history/components/history-editor";
import HistoryList from "@/features/page-history/components/history-list";
import classes from "./history.module.css";
import { useAtom } from "jotai";
@@ -34,7 +31,7 @@ export default function HistoryModalBody({ pageId }: Props) {
const [highlightChanges, setHighlightChanges] = useState(true);
const [diffCounts, setDiffCounts] = useState<DiffCounts | null>(null);
const [currentChangeIndex, setCurrentChangeIndex] = useState(0);
const historyEditorRef = useRef<HistoryEditorHandle>(null);
const scrollViewportRef = useRef<HTMLDivElement>(null);
useEffect(() => {
setActiveHistoryId("");
@@ -45,12 +42,24 @@ export default function HistoryModalBody({ pageId }: Props) {
setCurrentChangeIndex(0);
}, [activeHistoryId]);
const scrollToChangeIndex = (index: number) => {
const viewport = scrollViewportRef.current;
if (!viewport || index < 1) return;
const element = viewport.querySelector(`[data-diff-index="${index}"]`);
if (element instanceof HTMLElement) {
const elementTop = element.offsetTop;
const viewportHeight = viewport.clientHeight;
const scrollTarget = elementTop - viewportHeight / 2 + element.offsetHeight / 2;
viewport.scrollTo({ top: scrollTarget, behavior: "smooth" });
}
};
const handlePrevChange = () => {
if (!diffCounts || diffCounts.total === 0) return;
const newIndex =
currentChangeIndex <= 1 ? diffCounts.total : currentChangeIndex - 1;
setCurrentChangeIndex(newIndex);
historyEditorRef.current?.scrollToChange(newIndex);
scrollToChangeIndex(newIndex);
};
const handleNextChange = () => {
@@ -58,7 +67,7 @@ export default function HistoryModalBody({ pageId }: Props) {
const newIndex =
currentChangeIndex >= diffCounts.total ? 1 : currentChangeIndex + 1;
setCurrentChangeIndex(newIndex);
historyEditorRef.current?.scrollToChange(newIndex);
scrollToChangeIndex(newIndex);
};
return (
@@ -70,11 +79,10 @@ export default function HistoryModalBody({ pageId }: Props) {
</nav>
<div style={{ position: "relative", flex: 1 }}>
<ScrollArea h={650} w="100%" scrollbarSize={5}>
<ScrollArea h={650} w="100%" scrollbarSize={5} viewportRef={scrollViewportRef}>
<div className={classes.sidebarRightSection}>
{activeHistoryId && (
<HistoryView
ref={historyEditorRef}
historyId={activeHistoryId}
prevHistoryId={activeHistoryPrevId}
highlightChanges={highlightChanges}
@@ -2,10 +2,8 @@ import { usePageHistoryQuery } from "@/features/page-history/queries/page-histor
import {
DiffCounts,
HistoryEditor,
HistoryEditorHandle,
} from "@/features/page-history/components/history-editor";
import { useTranslation } from "react-i18next";
import { forwardRef } from "react";
interface HistoryProps {
historyId: string;
@@ -14,11 +12,12 @@ interface HistoryProps {
onDiffCalculated?: (counts: DiffCounts) => void;
}
const HistoryView = forwardRef<HistoryEditorHandle, HistoryProps>(
function HistoryView(
{ historyId, prevHistoryId, highlightChanges, onDiffCalculated },
ref,
) {
function HistoryView({
historyId,
prevHistoryId,
highlightChanges,
onDiffCalculated,
}: HistoryProps) {
const { t } = useTranslation();
const {
data,
@@ -39,21 +38,19 @@ const HistoryView = forwardRef<HistoryEditorHandle, HistoryProps>(
return <div>{t("Error fetching page data.")}</div>;
}
return (
data && (
<div>
<HistoryEditor
ref={ref}
content={data.content}
title={data.title}
previousContent={!isErrorPrev ? prevData?.content : undefined}
highlightChanges={highlightChanges}
onDiffCalculated={onDiffCalculated}
/>
</div>
)
);
},
);
return (
data && (
<div>
<HistoryEditor
content={data.content}
title={data.title}
previousContent={!isErrorPrev ? prevData?.content : undefined}
highlightChanges={highlightChanges}
onDiffCalculated={onDiffCalculated}
/>
</div>
)
);
}
export default HistoryView;