Files
suwayomi-material-you-webui/src/components/navbar/ReaderNavBar.tsx

391 lines
15 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-03-18 21:46:24 +03:30
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
2021-09-09 17:51:22 +04:30
import IconButton from '@mui/material/IconButton';
import CloseIcon from '@mui/icons-material/Close';
import KeyboardArrowLeftIcon from '@mui/icons-material/KeyboardArrowLeft';
import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight';
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp';
import makeStyles from '@mui/styles/makeStyles';
2021-03-18 21:46:24 +03:30
import React, { useContext, useEffect, useState } from 'react';
2021-09-09 17:51:22 +04:30
import Typography from '@mui/material/Typography';
2021-03-23 03:50:55 +04:30
import { useHistory, Link } from 'react-router-dom';
2021-09-09 17:51:22 +04:30
import Slide from '@mui/material/Slide';
import Fade from '@mui/material/Fade';
import Zoom from '@mui/material/Zoom';
import { Switch } from '@mui/material';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import MenuItem from '@mui/material/MenuItem';
import Select from '@mui/material/Select';
import ListItemText from '@mui/material/ListItemText';
import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
import Collapse from '@mui/material/Collapse';
import Button from '@mui/material/Button';
2021-05-17 01:36:31 +04:30
import NavBarContext from '../../context/NavbarContext';
2021-03-18 21:46:24 +03:30
const useStyles = (settings: IReaderSettings) => makeStyles((theme) => ({
2021-03-18 21:46:24 +03:30
// main container and root div need to change classes...
AppMainContainer: {
display: 'none',
},
AppRootElment: {
display: 'flex',
},
root: {
2021-03-19 14:52:20 +03:30
position: settings.staticNav ? 'sticky' : 'fixed',
2021-03-18 21:46:24 +03:30
top: 0,
left: 0,
width: '300px',
2021-08-29 02:09:56 +04:30
minWidth: '300px',
2021-03-18 21:46:24 +03:30
height: '100vh',
overflowY: 'auto',
backgroundColor: theme.palette.background.default,
2021-03-18 21:46:24 +03:30
'& header': {
backgroundColor:
theme.palette.mode === 'dark' ? theme.palette.grey[800] : theme.palette.grey[100],
2021-03-18 21:46:24 +03:30
display: 'flex',
alignItems: 'center',
minHeight: '64px',
paddingLeft: '24px',
paddingRight: '24px',
transition: 'left 2s ease',
'& button': {
flexGrow: 0,
flexShrink: 0,
},
'& button:nth-child(1)': {
marginRight: '16px',
},
'& button:nth-child(3)': {
marginRight: '-12px',
},
'& h1': {
fontSize: '1.25rem',
flexGrow: 1,
},
},
2021-03-23 03:50:55 +04:30
'& hr': {
margin: '0 16px',
height: '1px',
border: '0',
backgroundColor: theme.palette.mode === 'dark' ? theme.palette.grey[800] : theme.palette.grey[100],
2021-03-23 03:50:55 +04:30
},
},
navigation: {
margin: '0 16px',
'& > span:nth-child(1)': {
textAlign: 'center',
display: 'block',
marginTop: '16px',
},
'& $navigationChapters': {
display: 'grid',
gridTemplateColumns: '1fr 1fr',
gridTemplateAreas: '"prev next"',
gridColumnGap: '5px',
margin: '10px 0',
'& a': {
flexGrow: 1,
textDecoration: 'none',
'& button': {
width: '100%',
padding: '5px 8px',
textTransform: 'none',
},
},
},
},
navigationChapters: {}, // dummy rule
settingsCollapsseHeader: {
'& span': {
fontWeight: 'bold',
},
2021-03-18 21:46:24 +03:30
},
openDrawerButton: {
position: 'fixed',
top: 0 + 20,
left: 10 + 20,
height: '40px',
width: '40px',
borderRadius: 5,
backgroundColor: theme.palette.mode === 'dark' ? 'black' : 'white',
2021-03-18 21:46:24 +03:30
'&:hover': {
backgroundColor: theme.palette.mode === 'dark' ? theme.palette.grey[900] : theme.palette.grey[100],
2021-03-18 21:46:24 +03:30
},
},
}));
2021-03-18 21:46:24 +03:30
2021-03-19 14:52:20 +03:30
export const defaultReaderSettings = () => ({
staticNav: false,
showPageNumber: true,
2021-03-28 04:03:46 +04:30
continuesPageGap: false,
loadNextonEnding: false,
2021-05-15 23:22:37 +04:30
readerType: 'ContinuesVertical',
2021-03-19 14:52:20 +03:30
} as IReaderSettings);
2021-03-18 21:46:24 +03:30
interface IProps {
settings: IReaderSettings
setSettings: React.Dispatch<React.SetStateAction<IReaderSettings>>
2021-03-23 03:50:55 +04:30
manga: IManga | IMangaCard
chapter: IChapter
2021-03-23 03:50:55 +04:30
curPage: number
2021-03-18 21:46:24 +03:30
}
export default function ReaderNavBar(props: IProps) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2021-03-18 21:46:24 +03:30
const { title } = useContext(NavBarContext);
const history = useHistory();
2021-03-23 03:50:55 +04:30
const {
settings, setSettings, manga, chapter, curPage,
} = props;
2021-03-19 14:52:20 +03:30
const [drawerOpen, setDrawerOpen] = useState(false || settings.staticNav);
2021-03-18 21:46:24 +03:30
const [hideOpenButton, setHideOpenButton] = useState(false);
const [prevScrollPos, setPrevScrollPos] = useState(0);
2021-05-15 23:22:37 +04:30
const [settingsCollapseOpen, setSettingsCollapseOpen] = useState(true);
2021-03-18 21:46:24 +03:30
2021-03-19 14:52:20 +03:30
const classes = useStyles(settings)();
2021-03-18 21:46:24 +03:30
2021-03-19 14:52:20 +03:30
const setSettingValue = (key: string, value: any) => setSettings({ ...settings, [key]: value });
2021-03-18 21:46:24 +03:30
const handleScroll = () => {
const currentScrollPos = window.pageYOffset;
if (Math.abs(currentScrollPos - prevScrollPos) > 20) {
setHideOpenButton(currentScrollPos > prevScrollPos);
setPrevScrollPos(currentScrollPos);
}
};
useEffect(() => {
window.addEventListener('scroll', handleScroll);
const rootEl = document.querySelector('#root')!;
const mainContainer = document.querySelector('#appMainContainer')!;
rootEl.classList.add(classes.AppRootElment);
mainContainer.classList.add(classes.AppMainContainer);
return () => {
rootEl.classList.remove(classes.AppRootElment);
mainContainer.classList.remove(classes.AppMainContainer);
2021-03-23 04:28:23 +04:30
window.removeEventListener('scroll', handleScroll);
2021-03-18 21:46:24 +03:30
};
2021-03-19 14:52:20 +03:30
}, [handleScroll]);// handleScroll changes on every render
2021-03-18 21:46:24 +03:30
return (
<>
2021-05-15 23:22:37 +04:30
<Slide
direction="right"
in={drawerOpen}
timeout={200}
appear={false}
mountOnEnter
unmountOnExit
>
<div className={classes.root}>
<header>
2021-09-09 17:51:22 +04:30
<IconButton
edge="start"
color="inherit"
aria-label="menu"
disableRipple
onClick={() => setDrawerOpen(false)}
size="large"
>
<KeyboardArrowLeftIcon />
</IconButton>
<Typography variant="h1">
{/* {title} */}
{chapter.name}
</Typography>
<IconButton
edge="start"
color="inherit"
aria-label="menu"
disableRipple
onClick={() => history.goBack()}
2021-09-09 17:51:22 +04:30
size="large"
>
<CloseIcon />
</IconButton>
2021-05-15 23:22:37 +04:30
</header>
<ListItem ContainerComponent="div" className={classes.settingsCollapsseHeader}>
<ListItemText primary="Reader Settings" />
<ListItemSecondaryAction>
2021-03-23 03:50:55 +04:30
<IconButton
edge="start"
color="inherit"
aria-label="menu"
disableRipple
2021-05-15 23:22:37 +04:30
disableFocusRipple
onClick={() => setSettingsCollapseOpen(!settingsCollapseOpen)}
2021-09-09 17:51:22 +04:30
size="large"
2021-03-23 03:50:55 +04:30
>
2021-05-15 23:22:37 +04:30
{settingsCollapseOpen && <KeyboardArrowUpIcon />}
{!settingsCollapseOpen && <KeyboardArrowDownIcon />}
2021-03-23 03:50:55 +04:30
</IconButton>
2021-05-15 23:22:37 +04:30
</ListItemSecondaryAction>
</ListItem>
<Collapse in={settingsCollapseOpen} timeout="auto" unmountOnExit>
<List>
<ListItem>
<ListItemText primary="Static Navigation" />
<ListItemSecondaryAction>
<Switch
edge="end"
checked={settings.staticNav}
onChange={(e) => setSettingValue('staticNav', e.target.checked)}
/>
</ListItemSecondaryAction>
</ListItem>
<ListItem>
<ListItemText primary="Show page number" />
<ListItemSecondaryAction>
<Switch
edge="end"
checked={settings.showPageNumber}
onChange={(e) => setSettingValue('showPageNumber', e.target.checked)}
/>
</ListItemSecondaryAction>
</ListItem>
<ListItem>
<ListItemText primary="Load next chapter at ending" />
<ListItemSecondaryAction>
<Switch
edge="end"
checked={settings.loadNextonEnding}
onChange={(e) => setSettingValue('loadNextonEnding', e.target.checked)}
/>
</ListItemSecondaryAction>
</ListItem>
2021-05-15 23:22:37 +04:30
<ListItem>
<ListItemText primary="Reader Type" />
<Select
value={settings.readerType}
onChange={(e) => setSettingValue('readerType', e.target.value)}
2021-03-23 04:28:23 +04:30
>
2021-05-25 13:12:42 +04:30
<MenuItem value="SingleLTR">
Single Page (LTR)
2021-05-25 13:12:42 +04:30
</MenuItem>
<MenuItem value="SingleRTL">
Single Page (RTL)
2021-05-25 13:12:42 +04:30
</MenuItem>
2021-05-25 13:12:42 +04:30
{/* <MenuItem value="SingleVertical">
2021-09-09 17:51:22 +04:30
Vertical(WIP)
2021-05-25 13:12:42 +04:30
2021-09-09 17:51:22 +04:30
</MenuItem> */}
<MenuItem value="DoubleLTR">
Double Page (LTR)
</MenuItem>
<MenuItem value="DoubleRTL">
Double Page (RTL)
</MenuItem>
2021-05-25 13:12:42 +04:30
<MenuItem value="Webtoon">
Webtoon
</MenuItem>
<MenuItem value="ContinuesVertical">
Continues Vertical
</MenuItem>
<MenuItem value="ContinuesHorizontalLTR">
Horizontal (LTR)
</MenuItem>
<MenuItem value="ContinuesHorizontalRTL">
Horizontal (RTL)
2021-05-25 13:12:42 +04:30
2021-05-28 11:57:31 -07:00
</MenuItem>
2021-05-15 23:22:37 +04:30
</Select>
</ListItem>
</List>
</Collapse>
<hr />
<div className={classes.navigation}>
<span>
{`Currently on page ${curPage + 1} of ${chapter.pageCount}`}
2021-05-15 23:22:37 +04:30
</span>
<div className={classes.navigationChapters}>
{chapter.index > 1
2021-09-09 17:51:22 +04:30
&& (
<Link
replace
2021-09-09 17:51:22 +04:30
style={{ gridArea: 'prev' }}
to={`/manga/${manga.id}/chapter/${chapter.index - 1}`}
>
<Button
variant="outlined"
startIcon={<KeyboardArrowLeftIcon />}
2021-03-23 03:50:55 +04:30
>
2021-09-09 17:51:22 +04:30
Prev. Chapter
</Button>
</Link>
)}
2021-05-15 23:22:37 +04:30
{chapter.index < chapter.chapterCount
2021-09-09 17:51:22 +04:30
&& (
<Link
replace
2021-09-09 17:51:22 +04:30
style={{ gridArea: 'next' }}
to={`/manga/${manga.id}/chapter/${chapter.index + 1}`}
>
<Button
variant="outlined"
endIcon={<KeyboardArrowRightIcon />}
2021-03-23 03:50:55 +04:30
>
2021-09-09 17:51:22 +04:30
Next Chapter
</Button>
</Link>
)}
2021-03-23 03:50:55 +04:30
</div>
</div>
2021-05-15 23:22:37 +04:30
</div>
</Slide>
2021-03-18 21:46:24 +03:30
<Zoom in={!drawerOpen}>
<Fade in={!hideOpenButton}>
<IconButton
className={classes.openDrawerButton}
edge="start"
color="inherit"
aria-label="menu"
disableRipple
disableFocusRipple
onClick={() => setDrawerOpen(true)}
2021-09-09 17:51:22 +04:30
size="large"
2021-03-18 21:46:24 +03:30
>
<KeyboardArrowRightIcon />
</IconButton>
</Fade>
</Zoom>
</>
);
}