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 "@/features/editor/styles/index.css";
import "./history-diff.module.css"; import "./history-diff.module.css";
import { useEffect, useImperativeHandle, forwardRef, useRef } from "react"; import { useEffect } from "react";
import { EditorContent, useEditor } from "@tiptap/react"; import { EditorContent, useEditor } from "@tiptap/react";
import { mainExtensions } from "@/features/editor/extensions/extensions"; import { mainExtensions } from "@/features/editor/extensions/extensions";
import { Title } from "@mantine/core"; 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 DiffCounts = { added: number; deleted: number; total: number };
export type HistoryEditorHandle = {
scrollToChange: (index: number) => void;
};
export interface HistoryEditorProps { export interface HistoryEditorProps {
title: string; title: string;
content: any; content: any;
@@ -24,31 +20,19 @@ export interface HistoryEditorProps {
onDiffCalculated?: (counts: DiffCounts) => void; onDiffCalculated?: (counts: DiffCounts) => void;
} }
export const HistoryEditor = forwardRef<HistoryEditorHandle, HistoryEditorProps>( export function HistoryEditor({
function HistoryEditor( title,
{ title, content, previousContent, highlightChanges = true, onDiffCalculated }, content,
ref, previousContent,
) { highlightChanges = true,
const editor = useEditor({ onDiffCalculated,
extensions: mainExtensions, }: HistoryEditorProps) {
editable: false, const editor = useEditor({
}); extensions: mainExtensions,
const containerRef = useRef<HTMLDivElement>(null); editable: false,
const changeIndexRef = useRef<number[]>([]); });
useImperativeHandle(ref, () => ({ useEffect(() => {
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(() => {
if (!editor || !content) return; if (!editor || !content) return;
let decorationSet = DecorationSet.empty; 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); decorationSet = DecorationSet.create(docNew, decorations);
} catch (e) { } catch (e) {
console.error("History diff failed:", e); console.error("History diff failed:", e);
@@ -201,16 +180,15 @@ export const HistoryEditor = forwardRef<HistoryEditorHandle, HistoryEditorProps>
}); });
}, [title, content, editor, previousContent, highlightChanges]); }, [title, content, editor, previousContent, highlightChanges]);
return ( return (
<div ref={containerRef}> <div>
<Title order={1}>{title}</Title> <Title order={1}>{title}</Title>
{editor && ( {editor && (
<EditorContent <EditorContent
editor={editor} editor={editor}
className={historyClasses.historyEditor} className={historyClasses.historyEditor}
/> />
)} )}
</div> </div>
); );
}, }
);
@@ -7,10 +7,7 @@ import {
Switch, Switch,
Text, Text,
} from "@mantine/core"; } from "@mantine/core";
import { import { DiffCounts } from "@/features/page-history/components/history-editor";
DiffCounts,
HistoryEditorHandle,
} from "@/features/page-history/components/history-editor";
import HistoryList from "@/features/page-history/components/history-list"; import HistoryList from "@/features/page-history/components/history-list";
import classes from "./history.module.css"; import classes from "./history.module.css";
import { useAtom } from "jotai"; import { useAtom } from "jotai";
@@ -34,7 +31,7 @@ export default function HistoryModalBody({ pageId }: Props) {
const [highlightChanges, setHighlightChanges] = useState(true); const [highlightChanges, setHighlightChanges] = useState(true);
const [diffCounts, setDiffCounts] = useState<DiffCounts | null>(null); const [diffCounts, setDiffCounts] = useState<DiffCounts | null>(null);
const [currentChangeIndex, setCurrentChangeIndex] = useState(0); const [currentChangeIndex, setCurrentChangeIndex] = useState(0);
const historyEditorRef = useRef<HistoryEditorHandle>(null); const scrollViewportRef = useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
setActiveHistoryId(""); setActiveHistoryId("");
@@ -45,12 +42,24 @@ export default function HistoryModalBody({ pageId }: Props) {
setCurrentChangeIndex(0); setCurrentChangeIndex(0);
}, [activeHistoryId]); }, [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 = () => { const handlePrevChange = () => {
if (!diffCounts || diffCounts.total === 0) return; if (!diffCounts || diffCounts.total === 0) return;
const newIndex = const newIndex =
currentChangeIndex <= 1 ? diffCounts.total : currentChangeIndex - 1; currentChangeIndex <= 1 ? diffCounts.total : currentChangeIndex - 1;
setCurrentChangeIndex(newIndex); setCurrentChangeIndex(newIndex);
historyEditorRef.current?.scrollToChange(newIndex); scrollToChangeIndex(newIndex);
}; };
const handleNextChange = () => { const handleNextChange = () => {
@@ -58,7 +67,7 @@ export default function HistoryModalBody({ pageId }: Props) {
const newIndex = const newIndex =
currentChangeIndex >= diffCounts.total ? 1 : currentChangeIndex + 1; currentChangeIndex >= diffCounts.total ? 1 : currentChangeIndex + 1;
setCurrentChangeIndex(newIndex); setCurrentChangeIndex(newIndex);
historyEditorRef.current?.scrollToChange(newIndex); scrollToChangeIndex(newIndex);
}; };
return ( return (
@@ -70,11 +79,10 @@ export default function HistoryModalBody({ pageId }: Props) {
</nav> </nav>
<div style={{ position: "relative", flex: 1 }}> <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}> <div className={classes.sidebarRightSection}>
{activeHistoryId && ( {activeHistoryId && (
<HistoryView <HistoryView
ref={historyEditorRef}
historyId={activeHistoryId} historyId={activeHistoryId}
prevHistoryId={activeHistoryPrevId} prevHistoryId={activeHistoryPrevId}
highlightChanges={highlightChanges} highlightChanges={highlightChanges}
@@ -2,10 +2,8 @@ import { usePageHistoryQuery } from "@/features/page-history/queries/page-histor
import { import {
DiffCounts, DiffCounts,
HistoryEditor, HistoryEditor,
HistoryEditorHandle,
} from "@/features/page-history/components/history-editor"; } from "@/features/page-history/components/history-editor";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { forwardRef } from "react";
interface HistoryProps { interface HistoryProps {
historyId: string; historyId: string;
@@ -14,11 +12,12 @@ interface HistoryProps {
onDiffCalculated?: (counts: DiffCounts) => void; onDiffCalculated?: (counts: DiffCounts) => void;
} }
const HistoryView = forwardRef<HistoryEditorHandle, HistoryProps>( function HistoryView({
function HistoryView( historyId,
{ historyId, prevHistoryId, highlightChanges, onDiffCalculated }, prevHistoryId,
ref, highlightChanges,
) { onDiffCalculated,
}: HistoryProps) {
const { t } = useTranslation(); const { t } = useTranslation();
const { const {
data, data,
@@ -39,21 +38,19 @@ const HistoryView = forwardRef<HistoryEditorHandle, HistoryProps>(
return <div>{t("Error fetching page data.")}</div>; return <div>{t("Error fetching page data.")}</div>;
} }
return ( return (
data && ( data && (
<div> <div>
<HistoryEditor <HistoryEditor
ref={ref} content={data.content}
content={data.content} title={data.title}
title={data.title} previousContent={!isErrorPrev ? prevData?.content : undefined}
previousContent={!isErrorPrev ? prevData?.content : undefined} highlightChanges={highlightChanges}
highlightChanges={highlightChanges} onDiffCalculated={onDiffCalculated}
onDiffCalculated={onDiffCalculated} />
/> </div>
</div> )
) );
); }
},
);
export default HistoryView; export default HistoryView;