Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -32,7 +32,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';
Expand Down Expand Up @@ -233,13 +233,39 @@ const DetailPageImpl = memo(function DetailPageImpl() {
[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 [scrolled, setScrolled] = useState(false);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to useRef. scrolled value changes may not retrigger the useEffect

const handleScroll = useCallback(
(e: React.UIEvent<HTMLDivElement>) => {
const scrollTop = e.currentTarget.scrollTop;

const hasScrollTop = scrollTop > 0;
setHasScrollTop(hasScrollTop);
view.setScrollPosition(scrollTop);
},
[view]
);

useEffect(() => {
if (mode === 'page') {
setTimeout(() => {
scrollViewportRef?.current?.scrollTo(
0,
view.scrollPositions.get(view.history.location) || 0
);
setScrolled(true);
}, 50);
}
}, [mode, scrolled, view]);

return (
<FrameworkScope scope={editor.scope}>
<ViewHeader>
Expand All @@ -256,7 +282,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,
Expand Down
10 changes: 10 additions & 0 deletions packages/frontend/core/src/modules/workbench/entities/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class View extends Entity<{

sidebarTabs$ = new LiveData<SidebarTab[]>([]);

scrollPositions = new WeakMap<Location, number>();

// _activeTabId may point to a non-existent tab.
// In this case, we still retain the activeTabId data and wait for the non-existent tab to be mounted.
_activeSidebarTabId$ = new LiveData<string | null>(null);
Expand Down Expand Up @@ -161,6 +163,14 @@ export class View extends Entity<{
this._activeSidebarTabId$.next(id);
}

getScrollPosition(location: Location): number {
return this.scrollPositions.get(location) ?? 0;
}

setScrollPosition(position: number) {
this.scrollPositions.set(this.history.location, position);
}

setTitle(title: string) {
this.title$.next(title);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export function useBindWorkbenchToBrowserRouter(
if (update.action === 'POP') {
// This is because the history of view and browser are two different stacks,
// the POP action cannot be synchronized.
throw new Error('POP view history is not allowed on browser');
}

if (update.location.state === 'fromBrowser') {
return;
}

Expand All @@ -48,13 +44,11 @@ export function useBindWorkbenchToBrowserRouter(
basename
);

if (locationIsEqual(browserLocation, newBrowserLocation)) {
return;
}

navigate(newBrowserLocation, {
state: 'fromView',
replace: update.action === 'REPLACE',
state: 'fromView,' + newBrowserLocation.key,
replace:
update.action === 'REPLACE' ||
newBrowserLocation.state === 'fromBrowser',
});
});
}, [basename, browserLocation, navigate, view]);
Expand All @@ -67,7 +61,25 @@ export function useBindWorkbenchToBrowserRouter(
if (newLocation === null) {
return;
}

if (
typeof newLocation.state === 'string' &&
newLocation.state.startsWith('fromView')
) {
const fromViewKey = newLocation.state.substring('fromView,'.length);
if (fromViewKey === view.location$.value.key) {
return;
} else {
const target = view.history.entries.findIndex(
entry => entry.key === fromViewKey
);
if (target !== -1) {
const now = view.history.index;
const delta = target - now;
view.history.go(delta);
return;
}
}
}
view.history.push(newLocation, 'fromBrowser');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skip if from view?

}, [basename, browserLocation, view]);
}
Expand All @@ -94,12 +106,3 @@ function viewLocationToBrowserLocation(
pathname: `${basename}${location.pathname}`,
};
}

function locationIsEqual(a: Location, b: Location) {
return (
a.hash === b.hash &&
a.pathname === b.pathname &&
a.search === b.search &&
a.state === b.state
);
}
Loading