Skip to content

Commit

Permalink
fix: scroll into view when article selected
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglun committed Jan 1, 2024
1 parent 48be4d2 commit 0e94c5e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/components/ArticleList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ArticleListRefType {
innerRef: React.RefObject<HTMLDivElement>;
}

export const ArticleList = React.memo((props: ArticleListProps) => {
export const ArticleList = React.memo(React.forwardRef<HTMLDivElement, any>((props: ArticleListProps, ref) => {
const { articles, isEmpty, isLoading, isReachingEnd, size, setSize } = props;
const loadRef = useRef<HTMLDivElement | null>(null);
const entry = useIntersectionObserver(loadRef, {});
Expand Down Expand Up @@ -54,7 +54,7 @@ export const ArticleList = React.memo((props: ArticleListProps) => {
}, [loadRefVisible, isReachingEnd]);

return (
<div className="overflow-y-auto h-[calc(100vh_-_var(--app-toolbar-height))]">
<div className="overflow-y-auto h-[calc(100vh_-_var(--app-toolbar-height))]" ref={ref}>
{isEmpty ? (
<div className="absolute top-1/2 -translate-y-1/2 w-full flex flex-col justify-center items-center gap-1 text-muted-foreground">
<Snail size={34} strokeWidth={1} />
Expand All @@ -80,4 +80,4 @@ export const ArticleList = React.memo((props: ArticleListProps) => {
</div>
</div>
);
});
}));
72 changes: 0 additions & 72 deletions src/hooks/useAutoScroll.ts

This file was deleted.

43 changes: 42 additions & 1 deletion src/layout/Article/Layout1.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from "react";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { useParams } from "react-router-dom";
import { ArticleList } from "@/components/ArticleList";
import { useBearStore } from "@/stores";
Expand Down Expand Up @@ -29,6 +29,7 @@ export const Layout1 = React.memo(
const params: { name: string } = useParams();
const [isSyncing, setIsSyncing] = useState(false);
const [currentUuid, setCurrentUuid] = useState<string>();
const listRef = useRef<HTMLDivElement>(null);

const store = useBearStore((state) => ({
viewMeta: state.viewMeta,
Expand Down Expand Up @@ -98,6 +99,43 @@ export const Layout1 = React.memo(
}
};

function calculateItemPosition(
direction: "up" | "down",
article: ArticleResItem | null
) {
if (!article?.uuid) {
return;
}

const $li = document.getElementById(article.uuid);
const bounding = $li?.getBoundingClientRect();
const winH = window.innerHeight;

if (
(direction === "up" || direction === "down") &&
bounding &&
bounding.top < 58
) {
const offset = 58 - bounding.top;
const scrollTop = (listRef?.current?.scrollTop || 0) - offset;

listRef?.current?.scrollTo(0, scrollTop);
} else if (
(direction === "up" || direction === "down") &&
bounding &&
bounding.bottom > winH
) {
const offset = bounding.bottom - winH;
const scrollTop = (listRef?.current?.scrollTop || 0) + offset;

console.log(
"🚀 ~ file: index.tsx:324 ~ ArticleContainer ~ scrollTop:",
scrollTop
);
listRef?.current?.scrollTo(0, scrollTop);
}
}

function goPreviousArticle() {
let previousItem: ArticleResItem;
let uuid = store.article?.uuid;
Expand All @@ -110,6 +148,7 @@ export const Layout1 = React.memo(
store.updateArticleStatus(previousItem, ArticleReadStatus.READ);
setCurrentUuid((_) => previousItem.uuid);
store.setArticle(previousItem);
calculateItemPosition("up", previousItem);

break;
}
Expand Down Expand Up @@ -138,6 +177,7 @@ export const Layout1 = React.memo(
nextItem.read_status = ArticleReadStatus.READ;
store.updateArticleStatus(nextItem, ArticleReadStatus.READ);
store.setArticle(nextItem);
calculateItemPosition("down", nextItem);

return [false];
};
Expand Down Expand Up @@ -229,6 +269,7 @@ export const Layout1 = React.memo(
</div>
<div className="relative h-full">
<ArticleList
ref={listRef}
articles={articles}
title={params.name}
type={type}
Expand Down

0 comments on commit 0e94c5e

Please sign in to comment.