|
@@ -3,11 +3,12 @@ import React, { useEffect, useState, useRef } from 'react';
|
|
import Icon from '../Icon';
|
|
import Icon from '../Icon';
|
|
import Drawer from '../Drawer';
|
|
import Drawer from '../Drawer';
|
|
import Thumbnail from '../Thumbnail';
|
|
import Thumbnail from '../Thumbnail';
|
|
-import { Wrapper, Head, Body, IconWrapper } from '../../global/sidebarStyled'
|
|
|
|
import { ScrollStateType } from '../../constants/type';
|
|
import { ScrollStateType } from '../../constants/type';
|
|
-
|
|
|
|
import { watchScroll, scrollIntoView } from '../../helpers/utility';
|
|
import { watchScroll, scrollIntoView } from '../../helpers/utility';
|
|
|
|
|
|
|
|
+import {
|
|
|
|
+ Wrapper, Head, Body, IconWrapper,
|
|
|
|
+} from '../../global/sidebarStyled';
|
|
import { ThumbnailsWrapper } from './styled';
|
|
import { ThumbnailsWrapper } from './styled';
|
|
|
|
|
|
type Props = {
|
|
type Props = {
|
|
@@ -18,45 +19,51 @@ type Props = {
|
|
getPdfImage: (page: number) => Promise<string>;
|
|
getPdfImage: (page: number) => Promise<string>;
|
|
};
|
|
};
|
|
|
|
|
|
-const Thumbnails : React.FunctionComponent<Props> = ({
|
|
|
|
|
|
+const Thumbnails: React.FunctionComponent<Props> = ({
|
|
isActive = false,
|
|
isActive = false,
|
|
close,
|
|
close,
|
|
currentPage,
|
|
currentPage,
|
|
totalPage,
|
|
totalPage,
|
|
getPdfImage,
|
|
getPdfImage,
|
|
-}) => {
|
|
|
|
|
|
+}: Props) => {
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const [scrollIndex, setScrollIndex] = useState(currentPage);
|
|
const [scrollIndex, setScrollIndex] = useState(currentPage);
|
|
const [elements, setElement] = useState<React.ReactNode[]>([]);
|
|
const [elements, setElement] = useState<React.ReactNode[]>([]);
|
|
-
|
|
|
|
- const scrollUpdate = (state: ScrollStateType) => {
|
|
|
|
|
|
+
|
|
|
|
+ const scrollUpdate = (state: ScrollStateType): void => {
|
|
const height = 220;
|
|
const height = 220;
|
|
const index = Math.round((state.lastY + height) / height);
|
|
const index = Math.round((state.lastY + height) / height);
|
|
setScrollIndex(index);
|
|
setScrollIndex(index);
|
|
};
|
|
};
|
|
|
|
|
|
- const createThumbnails = () => {
|
|
|
|
|
|
+ const createThumbnails = (): void => {
|
|
const eleContent: React.ReactNode[] = [];
|
|
const eleContent: React.ReactNode[] = [];
|
|
- for(let i = 1; i <= totalPage; i++) {
|
|
|
|
|
|
+ for (let i = 1; i <= totalPage; i += 1) {
|
|
const component = (
|
|
const component = (
|
|
<Thumbnail
|
|
<Thumbnail
|
|
key={`thumbnail_${i}`}
|
|
key={`thumbnail_${i}`}
|
|
pageNum={i}
|
|
pageNum={i}
|
|
getPdfImage={getPdfImage}
|
|
getPdfImage={getPdfImage}
|
|
- renderingState={[1,2,3,4,5].includes(i) ? 'RENDERING' : 'LOADING'}
|
|
|
|
|
|
+ renderingState={[1, 2, 3, 4, 5].includes(i) ? 'RENDERING' : 'LOADING'}
|
|
/>
|
|
/>
|
|
);
|
|
);
|
|
eleContent.push(component);
|
|
eleContent.push(component);
|
|
}
|
|
}
|
|
setElement(eleContent);
|
|
setElement(eleContent);
|
|
- }
|
|
|
|
-
|
|
|
|
- const updateThumbnails = () => {
|
|
|
|
- const renderingIndexQueue = [scrollIndex - 1 ,scrollIndex, scrollIndex + 1, scrollIndex + 2, scrollIndex + 3];
|
|
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const updateThumbnails = (): void => {
|
|
|
|
+ const renderingIndexQueue = [
|
|
|
|
+ scrollIndex - 1,
|
|
|
|
+ scrollIndex,
|
|
|
|
+ scrollIndex + 1,
|
|
|
|
+ scrollIndex + 2,
|
|
|
|
+ scrollIndex + 3,
|
|
|
|
+ ];
|
|
let index = scrollIndex - 5;
|
|
let index = scrollIndex - 5;
|
|
const end = scrollIndex + 5;
|
|
const end = scrollIndex + 5;
|
|
|
|
|
|
- while (true) {
|
|
|
|
|
|
+ while (index >= 0) {
|
|
if (elements[index]) {
|
|
if (elements[index]) {
|
|
const pageNum = index + 1;
|
|
const pageNum = index + 1;
|
|
|
|
|
|
@@ -67,7 +74,7 @@ const Thumbnails : React.FunctionComponent<Props> = ({
|
|
getPdfImage={getPdfImage}
|
|
getPdfImage={getPdfImage}
|
|
renderingState={renderingIndexQueue.includes(pageNum) ? 'RENDERING' : 'LOADING'}
|
|
renderingState={renderingIndexQueue.includes(pageNum) ? 'RENDERING' : 'LOADING'}
|
|
/>
|
|
/>
|
|
- )
|
|
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
index += 1;
|
|
index += 1;
|
|
@@ -84,7 +91,7 @@ const Thumbnails : React.FunctionComponent<Props> = ({
|
|
watchScroll(containerRef.current, scrollUpdate);
|
|
watchScroll(containerRef.current, scrollUpdate);
|
|
}
|
|
}
|
|
}, [containerRef]);
|
|
}, [containerRef]);
|
|
-
|
|
|
|
|
|
+
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
if (isActive && !elements.length) {
|
|
if (isActive && !elements.length) {
|
|
createThumbnails();
|
|
createThumbnails();
|
|
@@ -97,7 +104,7 @@ const Thumbnails : React.FunctionComponent<Props> = ({
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
if (isActive && elements.length) {
|
|
if (isActive && elements.length) {
|
|
- const ele : HTMLElement = document.getElementById(`thumbnail_${currentPage}`)!;
|
|
|
|
|
|
+ const ele: HTMLElement = document.getElementById(`thumbnail_${currentPage}`) as HTMLElement;
|
|
scrollIntoView(ele);
|
|
scrollIntoView(ele);
|
|
}
|
|
}
|
|
}, [currentPage, isActive]);
|
|
}, [currentPage, isActive]);
|