diff options
author | crupest <crupest@outlook.com> | 2020-06-12 00:51:39 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-06-12 00:51:39 +0800 |
commit | 41825ea3d063ebf5a1dca562e4bd048d647409e1 (patch) | |
tree | 03173c50ea71983b66635fe8eaaabc6ff181d1b5 /Timeline/ClientApp/src/app/timeline/TimelinePageTemplateUI.tsx | |
parent | 0fd02b075478ffc098e08f24f369d760573650ac (diff) | |
download | timeline-41825ea3d063ebf5a1dca562e4bd048d647409e1.tar.gz timeline-41825ea3d063ebf5a1dca562e4bd048d647409e1.tar.bz2 timeline-41825ea3d063ebf5a1dca562e4bd048d647409e1.zip |
feat(front): Fix #98 . Fix #99 .
Diffstat (limited to 'Timeline/ClientApp/src/app/timeline/TimelinePageTemplateUI.tsx')
-rw-r--r-- | Timeline/ClientApp/src/app/timeline/TimelinePageTemplateUI.tsx | 66 |
1 files changed, 48 insertions, 18 deletions
diff --git a/Timeline/ClientApp/src/app/timeline/TimelinePageTemplateUI.tsx b/Timeline/ClientApp/src/app/timeline/TimelinePageTemplateUI.tsx index 275e9ae0..5eb6a310 100644 --- a/Timeline/ClientApp/src/app/timeline/TimelinePageTemplateUI.tsx +++ b/Timeline/ClientApp/src/app/timeline/TimelinePageTemplateUI.tsx @@ -69,29 +69,58 @@ export default function TimelinePageTemplateUI< } }, []); + const timelineRef = React.useRef<HTMLDivElement | null>(null); + const [getResizeEvent, triggerResizeEvent] = useEventEmiiter(); React.useEffect(() => { - let scrollToBottom = true; - const disableScrollToBottom = (): void => { - scrollToBottom = false; - }; - - const subscriptions = [ - fromEvent(window, 'wheel').subscribe(disableScrollToBottom), - fromEvent(window, 'pointerdown').subscribe(disableScrollToBottom), - fromEvent(window, 'keydown').subscribe(disableScrollToBottom), - getResizeEvent().subscribe(() => { - if (scrollToBottom) { + const { current: timelineElement } = timelineRef; + if (timelineElement != null) { + let loadingScrollToBottom = true; + let pinBottom = false; + + const isAtBottom = (): boolean => + window.innerHeight + window.scrollY + 10 >= document.body.scrollHeight; + + const disableLoadingScrollToBottom = (): void => { + loadingScrollToBottom = false; + if (isAtBottom()) pinBottom = true; + }; + + const checkAndScrollToBottom = (): void => { + if (loadingScrollToBottom || pinBottom) { window.scrollTo(0, document.body.scrollHeight); } - }), - ]; - - return () => { - subscriptions.forEach((s) => s.unsubscribe()); - }; - }, [getResizeEvent, timeline, props.posts]); + }; + + const subscriptions = [ + fromEvent(timelineElement, 'wheel').subscribe( + disableLoadingScrollToBottom + ), + fromEvent(timelineElement, 'pointerdown').subscribe( + disableLoadingScrollToBottom + ), + fromEvent(timelineElement, 'keydown').subscribe( + disableLoadingScrollToBottom + ), + fromEvent(window, 'scroll').subscribe(() => { + if (loadingScrollToBottom) return; + + if (isAtBottom()) { + pinBottom = true; + } else { + pinBottom = false; + } + }), + fromEvent(window, 'resize').subscribe(checkAndScrollToBottom), + getResizeEvent().subscribe(checkAndScrollToBottom), + ]; + + return () => { + subscriptions.forEach((s) => s.unsubscribe()); + }; + } + }, [getResizeEvent, triggerResizeEvent, timeline, props.posts]); const [cardHeight, setCardHeight] = React.useState<number>(0); @@ -125,6 +154,7 @@ export default function TimelinePageTemplateUI< } else { timelineBody = ( <Timeline + containerRef={timelineRef} posts={props.posts} onDelete={props.onDelete} onResize={triggerResizeEvent} |