Adding simple scroll position restore. Fixes #18 (#162)

* Adding simple scroll position restore. Fixes #18

* Removing isBrowser check from community.tsx.
This commit is contained in:
Dessalines 2021-02-02 13:36:59 -05:00 committed by GitHub
parent a28a8d059e
commit 192ec14441
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 10 deletions

View file

@ -48,12 +48,13 @@ import {
notifyPost, notifyPost,
setIsoData, setIsoData,
wsSubscribe, wsSubscribe,
isBrowser,
communityRSSUrl, communityRSSUrl,
wsUserOp, wsUserOp,
wsClient, wsClient,
authField, authField,
setOptionalAuth, setOptionalAuth,
saveScrollPosition,
restoreScrollPosition,
} from '../utils'; } from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
@ -144,11 +145,10 @@ export class Community extends Component<any, State> {
} }
componentWillUnmount() { componentWillUnmount() {
if (isBrowser()) { saveScrollPosition(this.context);
this.subscription.unsubscribe(); this.subscription.unsubscribe();
window.isoData.path = undefined; window.isoData.path = undefined;
} }
}
static getDerivedStateFromProps(props: any): CommunityProps { static getDerivedStateFromProps(props: any): CommunityProps {
return { return {
@ -482,6 +482,7 @@ export class Community extends Component<any, State> {
this.state.posts = data.posts; this.state.posts = data.posts;
this.state.postsLoading = false; this.state.postsLoading = false;
this.setState(this.state); this.setState(this.state);
restoreScrollPosition(this.context);
setupTippy(); setupTippy();
} else if ( } else if (
op == UserOperation.EditPost || op == UserOperation.EditPost ||

View file

@ -53,11 +53,12 @@ import {
notifyPost, notifyPost,
setIsoData, setIsoData,
wsSubscribe, wsSubscribe,
isBrowser,
wsUserOp, wsUserOp,
setOptionalAuth, setOptionalAuth,
wsClient, wsClient,
authField, authField,
saveScrollPosition,
restoreScrollPosition,
} from '../utils'; } from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
import { T } from 'inferno-i18next'; import { T } from 'inferno-i18next';
@ -169,11 +170,10 @@ export class Main extends Component<any, MainState> {
} }
componentWillUnmount() { componentWillUnmount() {
if (isBrowser()) { saveScrollPosition(this.context);
this.subscription.unsubscribe(); this.subscription.unsubscribe();
window.isoData.path = undefined; window.isoData.path = undefined;
} }
}
static getDerivedStateFromProps(props: any): MainProps { static getDerivedStateFromProps(props: any): MainProps {
return { return {
@ -757,6 +757,7 @@ export class Main extends Component<any, MainState> {
this.state.posts = data.posts; this.state.posts = data.posts;
this.state.loading = false; this.state.loading = false;
this.setState(this.state); this.setState(this.state);
restoreScrollPosition(this.context);
setupTippy(); setupTippy();
} else if (op == UserOperation.CreatePost) { } else if (op == UserOperation.CreatePost) {
let data = wsJsonToRes<PostResponse>(msg).data; let data = wsJsonToRes<PostResponse>(msg).data;

View file

@ -50,6 +50,8 @@ import {
wsClient, wsClient,
authField, authField,
setOptionalAuth, setOptionalAuth,
saveScrollPosition,
restoreScrollPosition,
} from '../utils'; } from '../utils';
import { PostListing } from './post-listing'; import { PostListing } from './post-listing';
import { Sidebar } from './sidebar'; import { Sidebar } from './sidebar';
@ -155,6 +157,7 @@ export class Post extends Component<any, PostState> {
componentWillUnmount() { componentWillUnmount() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
window.isoData.path = undefined; window.isoData.path = undefined;
saveScrollPosition(this.context);
} }
componentDidMount() { componentDidMount() {
@ -483,6 +486,7 @@ export class Post extends Component<any, PostState> {
this.fetchCrossPosts(); this.fetchCrossPosts();
this.setState(this.state); this.setState(this.state);
setupTippy(); setupTippy();
restoreScrollPosition(this.context);
} else if (op == UserOperation.CreateComment) { } else if (op == UserOperation.CreateComment) {
let data = wsJsonToRes<CommentResponse>(msg).data; let data = wsJsonToRes<CommentResponse>(msg).data;

View file

@ -30,6 +30,8 @@ import {
wsClient, wsClient,
authField, authField,
setOptionalAuth, setOptionalAuth,
saveScrollPosition,
restoreScrollPosition,
} from '../utils'; } from '../utils';
import { PostListing } from './post-listing'; import { PostListing } from './post-listing';
import { HtmlTags } from './html-tags'; import { HtmlTags } from './html-tags';
@ -123,6 +125,7 @@ export class Search extends Component<any, SearchState> {
componentWillUnmount() { componentWillUnmount() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
saveScrollPosition(this.context);
} }
static getDerivedStateFromProps(props: any): SearchProps { static getDerivedStateFromProps(props: any): SearchProps {
@ -516,6 +519,7 @@ export class Search extends Component<any, SearchState> {
this.state.loading = false; this.state.loading = false;
window.scrollTo(0, 0); window.scrollTo(0, 0);
this.setState(this.state); this.setState(this.state);
restoreScrollPosition(this.context);
} else if (op == UserOperation.CreateCommentLike) { } else if (op == UserOperation.CreateCommentLike) {
let data = wsJsonToRes<CommentResponse>(msg).data; let data = wsJsonToRes<CommentResponse>(msg).data;
createCommentLikeRes( createCommentLikeRes(

View file

@ -45,6 +45,8 @@ import {
wsClient, wsClient,
authField, authField,
setOptionalAuth, setOptionalAuth,
saveScrollPosition,
restoreScrollPosition,
} from '../utils'; } from '../utils';
import { UserListing } from './user-listing'; import { UserListing } from './user-listing';
import { HtmlTags } from './html-tags'; import { HtmlTags } from './html-tags';
@ -231,6 +233,7 @@ export class User extends Component<any, UserState> {
componentWillUnmount() { componentWillUnmount() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
saveScrollPosition(this.context);
} }
static getDerivedStateFromProps(props: any): UserProps { static getDerivedStateFromProps(props: any): UserProps {
@ -1113,6 +1116,7 @@ export class User extends Component<any, UserState> {
this.setUserInfo(); this.setUserInfo();
this.state.loading = false; this.state.loading = false;
this.setState(this.state); this.setState(this.state);
restoreScrollPosition(this.context);
} else if (op == UserOperation.SaveUserSettings) { } else if (op == UserOperation.SaveUserSettings) {
let data = wsJsonToRes<LoginResponse>(msg).data; let data = wsJsonToRes<LoginResponse>(msg).data;
UserService.Instance.login(data); UserService.Instance.login(data);

View file

@ -1173,3 +1173,15 @@ moment.updateLocale('en', {
yy: '%dY', yy: '%dY',
}, },
}); });
export function saveScrollPosition(context: any) {
let path: string = context.router.route.location.pathname;
let y = window.scrollY;
sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
}
export function restoreScrollPosition(context: any) {
let path: string = context.router.route.location.pathname;
let y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
window.scrollTo(0, y);
}