diff --git a/apps/client/src/features/page-history/components/history-editor.tsx b/apps/client/src/features/page-history/components/history-editor.tsx index 8f18bf22..3d012c50 100644 --- a/apps/client/src/features/page-history/components/history-editor.tsx +++ b/apps/client/src/features/page-history/components/history-editor.tsx @@ -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( - function HistoryEditor( - { title, content, previousContent, highlightChanges = true, onDiffCalculated }, - ref, - ) { - const editor = useEditor({ - extensions: mainExtensions, - editable: false, - }); - const containerRef = useRef(null); - const changeIndexRef = useRef([]); +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 } } - 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 }); }, [title, content, editor, previousContent, highlightChanges]); - return ( -
- {title} - {editor && ( - - )} -
- ); - }, -); + return ( +
+ {title} + {editor && ( + + )} +
+ ); +} diff --git a/apps/client/src/features/page-history/components/history-modal-body.tsx b/apps/client/src/features/page-history/components/history-modal-body.tsx index 2c75853e..df8d322f 100644 --- a/apps/client/src/features/page-history/components/history-modal-body.tsx +++ b/apps/client/src/features/page-history/components/history-modal-body.tsx @@ -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(null); const [currentChangeIndex, setCurrentChangeIndex] = useState(0); - const historyEditorRef = useRef(null); + const scrollViewportRef = useRef(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) {
- +
{activeHistoryId && ( void; } -const HistoryView = forwardRef( - 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( return
{t("Error fetching page data.")}
; } - return ( - data && ( -
- -
- ) - ); - }, -); + return ( + data && ( +
+ +
+ ) + ); +} export default HistoryView;