mirror of
https://github.com/tachiyomiorg/tachiyomi-extensions-inspector.git
synced 2024-12-27 01:01:48 +01:00
Improve continuous horizontal reader (#110)
* differentiate ContinuesHorizontalLTR and ContinuesHorizontalRTL * fix displaying pages in horizontal viewer * add scroll handler for horizontal mode * update curPage when images pass through center of the screen * add click events to navigate pages * remove console.log * fix click mapping for ContinuesHorizontalRTL * remove disable eslint inline comment * fix ContinuesHorizontalRTL not updating curPage on scroll * add ability to click to drag * add margin in between images
This commit is contained in:
parent
aa5a1083d0
commit
3777cc646e
@ -11,15 +11,20 @@ import React, { useEffect, useRef } from 'react';
|
|||||||
import SpinnerImage from 'components/SpinnerImage';
|
import SpinnerImage from 'components/SpinnerImage';
|
||||||
|
|
||||||
function imageStyle(settings: IReaderSettings): CSSProperties {
|
function imageStyle(settings: IReaderSettings): CSSProperties {
|
||||||
if (settings.readerType === 'DoubleLTR' || settings.readerType === 'DoubleRTL' || settings.readerType === 'ContinuesHorizontal') {
|
if (settings.readerType === 'DoubleLTR'
|
||||||
|
|| settings.readerType === 'DoubleRTL'
|
||||||
|
|| settings.readerType === 'ContinuesHorizontalLTR'
|
||||||
|
|| settings.readerType === 'ContinuesHorizontalRTL') {
|
||||||
return {
|
return {
|
||||||
display: 'block',
|
display: 'block',
|
||||||
marginBottom: 0,
|
marginLeft: '7px',
|
||||||
|
marginRight: '7px',
|
||||||
width: 'auto',
|
width: 'auto',
|
||||||
minHeight: '99vh',
|
minHeight: '99vh',
|
||||||
height: 'auto',
|
height: 'auto',
|
||||||
maxHeight: '99vh',
|
maxHeight: '99vh',
|
||||||
objectFit: 'contain',
|
objectFit: 'contain',
|
||||||
|
pointerEvents: 'none',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +69,7 @@ const Page = React.forwardRef((props: IProps, ref: any) => {
|
|||||||
const classes = useStyles(settings)();
|
const classes = useStyles(settings)();
|
||||||
const imgRef = useRef<HTMLImageElement>(null);
|
const imgRef = useRef<HTMLImageElement>(null);
|
||||||
|
|
||||||
const handleScroll = () => {
|
const handleVerticalScroll = () => {
|
||||||
if (imgRef.current) {
|
if (imgRef.current) {
|
||||||
const rect = imgRef.current.getBoundingClientRect();
|
const rect = imgRef.current.getBoundingClientRect();
|
||||||
if (rect.y < 0 && rect.y + rect.height > 0) {
|
if (rect.y < 0 && rect.y + rect.height > 0) {
|
||||||
@ -73,15 +78,29 @@ const Page = React.forwardRef((props: IProps, ref: any) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
const handleHorizontalScroll = () => {
|
||||||
if (settings.readerType === 'Webtoon' || settings.readerType === 'ContinuesVertical') {
|
if (imgRef.current) {
|
||||||
window.addEventListener('scroll', handleScroll);
|
const rect = imgRef.current.getBoundingClientRect();
|
||||||
|
if (rect.left <= window.innerWidth / 2 && rect.right > window.innerWidth / 2) {
|
||||||
|
setCurPage(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return () => {
|
useEffect(() => {
|
||||||
window.removeEventListener('scroll', handleScroll);
|
switch (settings.readerType) {
|
||||||
};
|
case 'Webtoon':
|
||||||
} return () => {};
|
case 'ContinuesVertical':
|
||||||
}, [handleScroll]);
|
window.addEventListener('scroll', handleVerticalScroll);
|
||||||
|
return () => window.removeEventListener('scroll', handleVerticalScroll);
|
||||||
|
case 'ContinuesHorizontalLTR':
|
||||||
|
case 'ContinuesHorizontalRTL':
|
||||||
|
window.addEventListener('scroll', handleHorizontalScroll);
|
||||||
|
return () => window.removeEventListener('scroll', handleHorizontalScroll);
|
||||||
|
default:
|
||||||
|
return () => {};
|
||||||
|
}
|
||||||
|
}, [handleVerticalScroll]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={ref} style={{ margin: '0 auto' }}>
|
<div ref={ref} style={{ margin: '0 auto' }}>
|
||||||
|
@ -6,34 +6,129 @@
|
|||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
import { makeStyles } from '@material-ui/core/styles';
|
import { makeStyles } from '@material-ui/core/styles';
|
||||||
import React from 'react';
|
import React, { useEffect, useRef } from 'react';
|
||||||
import Page from '../Page';
|
import Page from '../Page';
|
||||||
|
|
||||||
const useStyles = makeStyles({
|
const useStyles = (settings: IReaderSettings) => makeStyles({
|
||||||
reader: {
|
reader: {
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
flexDirection: 'row',
|
flexDirection: (settings.readerType === 'ContinuesHorizontalLTR') ? 'row' : 'row-reverse',
|
||||||
justifyContent: 'center',
|
justifyContent: (settings.readerType === 'ContinuesHorizontalLTR') ? 'flex-start' : 'flex-end',
|
||||||
margin: '0 auto',
|
margin: '0 auto',
|
||||||
width: '100%',
|
width: 'auto',
|
||||||
height: '100vh',
|
height: 'auto',
|
||||||
overflowX: 'scroll',
|
overflowX: 'visible',
|
||||||
|
userSelect: 'none',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
interface IProps {
|
export default function HorizontalPager(props: IReaderProps) {
|
||||||
pages: Array<IReaderPage>
|
const {
|
||||||
setCurPage: React.Dispatch<React.SetStateAction<number>>
|
pages, curPage, settings, setCurPage, prevChapter, nextChapter,
|
||||||
settings: IReaderSettings
|
} = props;
|
||||||
}
|
|
||||||
|
|
||||||
export default function HorizontalPager(props: IProps) {
|
const classes = useStyles(settings)();
|
||||||
const { pages, settings, setCurPage } = props;
|
|
||||||
|
|
||||||
const classes = useStyles();
|
const selfRef = useRef<HTMLDivElement>(null);
|
||||||
|
const pagesRef = useRef<HTMLDivElement[]>([]);
|
||||||
|
|
||||||
|
function nextPage() {
|
||||||
|
if (curPage < pages.length - 1) {
|
||||||
|
pagesRef.current[curPage + 1]?.scrollIntoView({ inline: 'center' });
|
||||||
|
setCurPage((page) => page + 1);
|
||||||
|
} else if (settings.loadNextonEnding) {
|
||||||
|
nextChapter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function prevPage() {
|
||||||
|
if (curPage > 0) {
|
||||||
|
pagesRef.current[curPage - 1]?.scrollIntoView({ inline: 'center' });
|
||||||
|
setCurPage(curPage - 1);
|
||||||
|
} else if (curPage === 0) {
|
||||||
|
prevChapter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function goLeft() {
|
||||||
|
if (settings.readerType === 'ContinuesHorizontalLTR') {
|
||||||
|
prevPage();
|
||||||
|
} else {
|
||||||
|
nextPage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function goRight() {
|
||||||
|
if (settings.readerType === 'ContinuesHorizontalLTR') {
|
||||||
|
nextPage();
|
||||||
|
} else {
|
||||||
|
prevPage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mouseXPos = useRef<number>(0);
|
||||||
|
|
||||||
|
function dragScreen(e: MouseEvent) {
|
||||||
|
window.scrollBy(mouseXPos.current - e.pageX, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function dragControl(e:MouseEvent) {
|
||||||
|
mouseXPos.current = e.pageX;
|
||||||
|
selfRef.current?.addEventListener('mousemove', dragScreen);
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeDragControl() {
|
||||||
|
selfRef.current?.removeEventListener('mousemove', dragScreen);
|
||||||
|
}
|
||||||
|
|
||||||
|
function clickControl(e:MouseEvent) {
|
||||||
|
if (e.clientX >= window.innerWidth * 0.85) {
|
||||||
|
goRight();
|
||||||
|
} else if (e.clientX <= window.innerWidth * 0.15) {
|
||||||
|
goLeft();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleLoadNextonEnding = () => {
|
||||||
|
if (settings.readerType === 'ContinuesHorizontalLTR') {
|
||||||
|
if (window.scrollX + window.innerWidth >= document.body.scrollWidth) {
|
||||||
|
nextChapter();
|
||||||
|
}
|
||||||
|
} else if (settings.readerType === 'ContinuesHorizontalRTL') {
|
||||||
|
if (window.scrollX <= window.innerWidth) {
|
||||||
|
nextChapter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
pagesRef.current[curPage]?.scrollIntoView({ inline: 'center' });
|
||||||
|
}, [settings.readerType]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
selfRef.current?.addEventListener('mousedown', dragControl);
|
||||||
|
selfRef.current?.addEventListener('mouseup', removeDragControl);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
selfRef.current?.removeEventListener('mousedown', dragControl);
|
||||||
|
selfRef.current?.removeEventListener('mouseup', removeDragControl);
|
||||||
|
};
|
||||||
|
}, [selfRef]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (settings.loadNextonEnding) {
|
||||||
|
document.addEventListener('scroll', handleLoadNextonEnding);
|
||||||
|
}
|
||||||
|
selfRef.current?.addEventListener('mousedown', clickControl);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('scroll', handleLoadNextonEnding);
|
||||||
|
selfRef.current?.removeEventListener('mousedown', clickControl);
|
||||||
|
};
|
||||||
|
}, [selfRef, curPage]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classes.reader}>
|
<div ref={selfRef} className={classes.reader}>
|
||||||
{
|
{
|
||||||
pages.map((page) => (
|
pages.map((page) => (
|
||||||
<Page
|
<Page
|
||||||
@ -43,6 +138,7 @@ export default function HorizontalPager(props: IProps) {
|
|||||||
onImageLoad={() => {}}
|
onImageLoad={() => {}}
|
||||||
setCurPage={setCurPage}
|
setCurPage={setCurPage}
|
||||||
settings={settings}
|
settings={settings}
|
||||||
|
ref={(e:HTMLDivElement) => { pagesRef.current[page.index] = e; }}
|
||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -313,8 +313,12 @@ export default function ReaderNavBar(props: IProps) {
|
|||||||
Continues Vertical
|
Continues Vertical
|
||||||
|
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem value="ContinuesHorizontal">
|
<MenuItem value="ContinuesHorizontalLTR">
|
||||||
Horizontal(WIP)
|
Horizontal (LTR)
|
||||||
|
|
||||||
|
</MenuItem>
|
||||||
|
<MenuItem value="ContinuesHorizontalRTL">
|
||||||
|
Horizontal (RTL)
|
||||||
|
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
</Select>
|
</Select>
|
||||||
|
@ -46,7 +46,8 @@ const getReaderComponent = (readerType: ReaderType) => {
|
|||||||
case 'DoubleLTR':
|
case 'DoubleLTR':
|
||||||
return DoublePagedPager;
|
return DoublePagedPager;
|
||||||
break;
|
break;
|
||||||
case 'ContinuesHorizontal':
|
case 'ContinuesHorizontalLTR':
|
||||||
|
case 'ContinuesHorizontalRTL':
|
||||||
return HorizontalPager;
|
return HorizontalPager;
|
||||||
default:
|
default:
|
||||||
return VerticalPager;
|
return VerticalPager;
|
||||||
|
3
webUI/react/src/typings.d.ts
vendored
3
webUI/react/src/typings.d.ts
vendored
@ -117,7 +117,8 @@ type ReaderType =
|
|||||||
'DoubleVertical' |
|
'DoubleVertical' |
|
||||||
'DoubleRTL' |
|
'DoubleRTL' |
|
||||||
'DoubleLTR' |
|
'DoubleLTR' |
|
||||||
'ContinuesHorizontal';
|
'ContinuesHorizontalLTR'|
|
||||||
|
'ContinuesHorizontalRTL';
|
||||||
|
|
||||||
interface IReaderSettings{
|
interface IReaderSettings{
|
||||||
staticNav: boolean
|
staticNav: boolean
|
||||||
|
Loading…
Reference in New Issue
Block a user