Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the zoom handling under smartphone #41

Merged
merged 6 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 88 additions & 101 deletions pages/v.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState, useEffect, useCallback, useRef } from "react";
import _ from "lodash";
import { useSpring } from "react-spring";
import { Helmet } from "react-helmet";

import {
Expand All @@ -15,7 +14,6 @@ import { makeStyles } from "@material-ui/core/styles";
import { getMangaChapters } from "../src/db";
import TopBar from "../src/scanViewer/TopBar";
import DisplayImage from "../src/scanViewer/DisplayImage";
import ImageCaption from "../src/scanViewer/ImageCaption";
import ControlBar from "../src/scanViewer/ControlBar";

import titleCase from "../utils/titleCase";
Expand Down Expand Up @@ -259,14 +257,48 @@ function populateCacheImages(url) {
}
}

function goToLink(
link,
idChapter,
idScan,
state,
setState,
setResetPanAndZoom,
setDisplayResetButton,
setLoading,
setDisplayFlashScreen,
duringFlashScreenAnimation
) {
if (!isUndefinedOrNull(link)) {
setState({
...state,
idChapter: idChapter,
idScan: idScan,
});

setResetPanAndZoom(true);
setDisplayResetButton(false);
window.history.replaceState(
{ page: link },
`Manga ${state.idManga} - ${idChapter} ${idScan}`,
link
);
setLoading(true);
} else {
setDisplayFlashScreen(true);
setResetPanAndZoom(true);
setTimeout(() => {
duringFlashScreenAnimation();
}, 0);
}
}

function ViewDetail() {
// Initially, retrieve input parameters from the route.
const params = useParams();

const classes = useStyles();

const flashScreen = useRef(null);

if (
isUndefinedOrNull(params.idManga) ||
isUndefinedOrNull(params.idChapter) ||
Expand All @@ -282,6 +314,8 @@ function ViewDetail() {
idScan: null,
});

const flashScreen = useRef(null);

const [loading, setLoading] = useState(true);
const [displayFlashScreen, setDisplayFlashScreen] = useState(false);

Expand Down Expand Up @@ -362,45 +396,36 @@ function ViewDetail() {

const [displayResetButton, setDisplayResetButton] = useState(false);

const [{ x, y, zoom, scale }, set] = useSpring(() => ({
x: 0,
y: 0,
zoom: 0,
scale: 1,
config: { mass: 5, tension: 1350, friction: 150 },
}));

const resetPanAndZoom = useCallback(() => {
set.start({ x: 0, y: 0, zoom: 0, scale: 1 });
setDisplayResetButton(false);
}, [set]);
const [resetPanAndZoom, setResetPanAndZoom] = useState(false);

const goPreviousLink = useCallback(() => {
if (!isUndefinedOrNull(previousLink)) {
setState({
...state,
idChapter: previousIdChapter,
idScan: previousIdScan,
});
resetPanAndZoom();
window.history.replaceState(
{ page: previousLink },
`Manga ${state.idManga} - ${previousIdChapter} ${previousIdScan}`,
previousLink
);
}
goToLink(
previousLink,
previousIdChapter,
previousIdScan,
state,
setState,
setResetPanAndZoom,
setDisplayResetButton,
setLoading,
setDisplayFlashScreen,
duringFlashScreenAnimation
);
}, [previousLink, previousIdChapter, previousIdScan]);

const goNextLink = useCallback(() => {
if (!isUndefinedOrNull(nextLink)) {
setState({ ...state, idChapter: nextIdChapter, idScan: nextIdScan });
resetPanAndZoom();
window.history.replaceState(
{ page: nextLink },
`Manga ${state.idManga} - ${nextIdChapter} ${nextIdScan}`,
nextLink
);
}
goToLink(
nextLink,
nextIdChapter,
nextIdScan,
state,
setState,
setResetPanAndZoom,
setDisplayResetButton,
setLoading,
setDisplayFlashScreen,
duringFlashScreenAnimation
);
}, [nextLink, nextIdChapter, nextIdScan]);

// Replace the URL without using the React 'history' object, in a hacky way :
Expand All @@ -410,25 +435,9 @@ function ViewDetail() {
const handleKeyDown = useCallback(
(evt) => {
if (evt.key === "ArrowLeft") {
if (!isUndefinedOrNull(previousLink)) {
goPreviousLink();
setLoading(true);
} else {
setDisplayFlashScreen(true);
setTimeout(() => {
duringFlashScreenAnimation();
}, 0);
}
goPreviousLink();
} else if (evt.key === "ArrowRight") {
if (!isUndefinedOrNull(nextLink)) {
goNextLink();
setLoading(true);
} else {
setDisplayFlashScreen(true);
setTimeout(() => {
duringFlashScreenAnimation();
}, 0);
}
goNextLink();
} else if (evt.key === "f") {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
Expand All @@ -454,18 +463,20 @@ function ViewDetail() {
};
}, [handleKeyDown]);

const goScanAddress = useCallback((idManga_, idChapter_, idScan_) => {
// setIdChapter(idChapter_);
// setIdScan(idScan_);
setState({ ...state, idChapter: idChapter_, idScan: idScan_ });
resetPanAndZoom();
const newLink = computeLink(idManga_, idChapter_, idScan_);
window.history.replaceState(
{ page: newLink },
`Manga ${idManga_} - ${idChapter_} ${idScan_}`,
newLink
);
}, []);
const goScanAddress = useCallback(
(idManga_, idChapter_, idScan_) => {
setState({ ...state, idChapter: idChapter_, idScan: idScan_ });
setResetPanAndZoom(true);
setDisplayResetButton(false);
const newLink = computeLink(idManga_, idChapter_, idScan_);
window.history.replaceState(
{ page: newLink },
`Manga ${idManga_} - ${idChapter_} ${idScan_}`,
newLink
);
},
[state]
);

const duringFlashScreenAnimation = () => {
const flashScreenRef = flashScreen.current;
Expand Down Expand Up @@ -613,52 +624,28 @@ function ViewDetail() {
/>
<DisplayImage
imageURL={imageURL}
set={set}
displayResetButton={displayResetButton}
setDisplayResetButton={setDisplayResetButton}
springDict={{ x, y, zoom, scale }}
resetPanAndZoom={resetPanAndZoom}
setResetPanAndZoom={setResetPanAndZoom}
loading={loading}
setLoading={setLoading}
goPreviousLink={goPreviousLink}
goNextLink={goNextLink}
/>
<ImageCaption
displayResetButton={displayResetButton}
<ControlBar
idScan={state.idScan}
totalIdScan={imagesURL.length}
/>
<ControlBar
resetPanAndZoom={resetPanAndZoom}
setResetPanAndZoom={setResetPanAndZoom}
displayResetButton={displayResetButton}
setDisplayResetButton={setDisplayResetButton}
previousLink={previousLink}
nextLink={nextLink}
goNextLink={goNextLink}
goPreviousLink={goPreviousLink}
/>
<div style={{ marginBottom: "5em" }} />
</>
);
}
}

{
/* <div style={{ color: "white" }}>
<h3>
ID: params {params.idManga} {params.idChapter} {params.idScan}
</h3>
<h3>
ID: local variable {idManga} {idChapter} {idScan}
</h3>
<h3>previousLink: {previousLink}</h3>
<h3>nextLink: {nextLink}</h3>
<h3>
imagesURL:
<ul>
{imagesURL.map((url) => {
return <li key={url}>{url}</li>;
})}
</ul>
</h3>
<h3>imageURL: {imageURL}</h3>
<h3>previousIdChapter: {previousIdChapter}</h3>
<h3>previousIdScan: {previousIdScan}</h3>
<h3>nextIdChapter: {nextIdChapter}</h3>
<h3>nextIdScan: {nextIdScan}</h3>
</div> */
}
8 changes: 4 additions & 4 deletions src/GridCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ const useStyles = makeStyles((theme) => ({
paddingLeft: "15px",
paddingRight: "15px",
marginBottom: "0px",
boxShadow: "0 5px 18px 3px rgba(0, 0, 0,.2)",
// boxShadow: "0 5px 18px 3px rgba(0, 0, 0,.2)",
[theme.breakpoints.down("md")]: {
width: "125px",
paddingLeft: "12px",
paddingRight: "12px",
marginBottom: "0px",
boxShadow: "0 4px 15px 3px rgba(0, 0, 0,.2)",
// boxShadow: "0 4px 15px 3px rgba(0, 0, 0,.2)",
},
[theme.breakpoints.down("sm")]: {
width: "100px",
paddingLeft: "10px",
paddingRight: "10px",
marginBottom: "0px",
boxShadow: "0 3px 14px 3px rgba(0, 0, 0,.2)",
// boxShadow: "0 3px 14px 3px rgba(0, 0, 0,.2)",
},
[theme.breakpoints.down("xs")]: {
width: "80px",
paddingLeft: "8px",
paddingRight: "8px",
marginBottom: "0px",
boxShadow: "0 2px 14px 3px rgba(0, 0, 0,.2)",
// boxShadow: "0 2px 14px 3px rgba(0, 0, 0,.2)",
},
cursor: "pointer",

Expand Down
50 changes: 36 additions & 14 deletions src/scanViewer/ControlBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Grid from "@material-ui/core/Grid";
import KeyboardArrowLeftRoundedIcon from "@material-ui/icons/KeyboardArrowLeftRounded";
import KeyboardArrowRightRoundedIcon from "@material-ui/icons/KeyboardArrowRightRounded";
import RotateLeftRoundedIcon from "@material-ui/icons/RotateLeftRounded";

import Typography from "@material-ui/core/Typography";
// import Link from "../Link";

// import { Link } from "react-router-dom";
Expand Down Expand Up @@ -36,8 +36,11 @@ function ControlButton(props) {

export default function ControlBar(props) {
const {
resetPanAndZoom,
idScan,
totalIdScan,
setResetPanAndZoom,
displayResetButton,
setDisplayResetButton,
previousLink,
nextLink,
goNextLink,
Expand Down Expand Up @@ -71,18 +74,37 @@ export default function ControlBar(props) {
</ControlButton>
)}
</Grid>
<Grid
item
style={{ visibility: displayResetButton ? "inherit" : "hidden" }}
>
<ControlButton
onClick={(_) => {
resetPanAndZoom();
}}
>
<RotateLeftRoundedIcon fontSize="large" />
</ControlButton>
</Grid>
{displayResetButton ? (
<Grid item>
<ControlButton
onClick={() => {
setResetPanAndZoom(true);
setDisplayResetButton(false);
}}
>
<RotateLeftRoundedIcon fontSize="large" />
</ControlButton>
</Grid>
) : (
<Grid item>
<Typography
variant="subtitle1"
style={{
color: "white",
textAlign: "end",
borderRadius: "100px",
borderColor: "rgb(255, 255, 255)",
borderWidth: "1px",
borderStyle: "groove",
fontSize: "0.7rem",
lineHeight: "1",
padding: "0.2rem 0.75rem",
}}
>
{`${Number(idScan) + 1} / ${totalIdScan}`}
</Typography>
</Grid>
)}
<Grid item>
{nextLink !== null && (
<ControlButton
Expand Down
Loading