feat(core): remember the scroll position of doc when routing forward and backward (#8631)

close AF-1011

https://github.com/user-attachments/assets/d2dfeee2-926f-4760-b3fb-8baf5ff90aa9
This commit is contained in:
JimmFly
2024-11-05 06:59:34 +00:00
parent 15749def2a
commit 9e41918a1a
7 changed files with 190 additions and 65 deletions

View File

@@ -1,4 +1,4 @@
import { notify, Scrollable, useHasScrollTop } from '@affine/component';
import { notify, Scrollable } from '@affine/component';
import { PageDetailSkeleton } from '@affine/component/page-detail-skeleton';
import type { ChatPanel } from '@affine/core/blocksuite/presets/ai';
import { AIProvider } from '@affine/core/blocksuite/presets/ai';
@@ -33,7 +33,7 @@ import {
WorkspaceService,
} from '@toeverything/infra';
import clsx from 'clsx';
import { memo, useCallback, useEffect, useRef } from 'react';
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { useParams } from 'react-router-dom';
import { AffineErrorBoundary } from '../../../../components/affine/affine-error-boundary';
@@ -227,28 +227,36 @@ const DetailPageImpl = memo(function DetailPageImpl() {
})
);
editor.setEditorContainer(editorContainer);
const unbind = editor.bindEditorContainer(
editorContainer,
(editorContainer as any).docTitle // set from proxy
(editorContainer as any).docTitle, // set from proxy
scrollViewportRef.current
);
return () => {
unbind();
editor.setEditorContainer(null);
disposable.dispose();
};
},
[editor, openPage, docCollection.id, jumpToPageBlock, t]
);
const [refCallback, hasScrollTop] = useHasScrollTop();
const [hasScrollTop, setHasScrollTop] = useState(false);
const openOutlinePanel = useCallback(() => {
workbench.openSidebar();
view.activeSidebarTab('outline');
}, [workbench, view]);
const scrollViewportRef = useRef<HTMLDivElement | null>(null);
const handleScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
const scrollTop = e.currentTarget.scrollTop;
const hasScrollTop = scrollTop > 0;
setHasScrollTop(hasScrollTop);
}, []);
return (
<FrameworkScope scope={editor.scope}>
<ViewHeader>
@@ -265,7 +273,8 @@ const DetailPageImpl = memo(function DetailPageImpl() {
<TopTip pageId={doc.id} workspace={workspace} />
<Scrollable.Root>
<Scrollable.Viewport
ref={refCallback}
onScroll={handleScroll}
ref={scrollViewportRef}
className={clsx(
'affine-page-viewport',
styles.affineDocViewport,

View File

@@ -234,11 +234,7 @@ const SharePageInner = ({
if (!editor) {
return;
}
editor.setEditorContainer(editorContainer);
const unbind = editor.bindEditorContainer(
editorContainer,
(editorContainer as any).docTitle
);
const unbind = editor.bindEditorContainer(editorContainer);
const disposable = new DisposableGroup();
const refNodeSlots =
@@ -263,7 +259,6 @@ const SharePageInner = ({
return () => {
unbind();
editor.setEditorContainer(null);
};
},
[editor, setActiveBlocksuiteEditor, jumpToPageBlock, openPage, workspaceId]