pov #2
@ -1,6 +1,8 @@
|
|||||||
import React, {createContext} from 'react'
|
import React, {createContext} from 'react'
|
||||||
import OnePageView from './OnePageView'
|
import OnePageView from './OnePageView'
|
||||||
import TwoPagesView from './TwoPagesView'
|
import TwoPagesView from './TwoPagesView'
|
||||||
|
import BookComponentsController from './BookComponentsController'
|
||||||
|
import './index.css'
|
||||||
|
|
||||||
export const BookContext = createContext({})
|
export const BookContext = createContext({})
|
||||||
|
|
||||||
@ -10,16 +12,22 @@ export interface IBookPage {
|
|||||||
thumbnail?: string
|
thumbnail?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const bookViewModes = [
|
export const getBookViewModes = (currIndex = 0, srcLength = 0)=>[
|
||||||
{
|
{
|
||||||
text: 'One Page',
|
text: 'One Page',
|
||||||
value: 'one page',
|
value: 'one page',
|
||||||
icon: ''
|
icon: '',
|
||||||
|
pagesToTurn: 1,
|
||||||
|
nextDisable: currIndex >= srcLength - 1,
|
||||||
|
prevDisable: currIndex === 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'Two Pages',
|
text: 'Two Pages',
|
||||||
value: 'two pages',
|
value: 'two pages',
|
||||||
icon: ''
|
icon: '',
|
||||||
|
pagesToTurn: 2,
|
||||||
|
nextDisable: currIndex >= srcLength - 2,
|
||||||
|
prevDisable: currIndex <= 1,
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -37,6 +45,11 @@ export interface IBookContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const getPagesCountToTurn = (mode: string)=>{
|
||||||
|
let pagesToTurn = getBookViewModes().find(item=> item.value === mode)?.pagesToTurn
|
||||||
|
return pagesToTurn || 0
|
||||||
|
}
|
||||||
|
|
||||||
class BookComponent extends React.Component<any, any>{
|
class BookComponent extends React.Component<any, any>{
|
||||||
constructor(props: any){
|
constructor(props: any){
|
||||||
super(props)
|
super(props)
|
||||||
@ -51,17 +64,22 @@ class BookComponent extends React.Component<any, any>{
|
|||||||
...newState
|
...newState
|
||||||
}))
|
}))
|
||||||
},
|
},
|
||||||
changePage: (indexToGo: number, goToPrevious: boolean)=>{
|
changePage: this.changePage,
|
||||||
const newCurrIndex = this.state.currIndex + indexToGo
|
pagesToTurn: 1,
|
||||||
|
|
||||||
this.setState({
|
|
||||||
currIndex: newCurrIndex,
|
|
||||||
flagToReverse: goToPrevious
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
changePage = (goToPrevious: boolean, indexToGo?: number)=>{
|
||||||
|
const {mode, currIndex} = this.state
|
||||||
|
const pagesCountToTurn = indexToGo || getPagesCountToTurn(mode)
|
||||||
|
const prefixPageCount = goToPrevious? -1 : 1
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
currIndex: (pagesCountToTurn * prefixPageCount) + currIndex,
|
||||||
|
flagToReverse: goToPrevious
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
componentDidMount(){
|
componentDidMount(){
|
||||||
fetch('https://picsum.photos/v2/list')
|
fetch('https://picsum.photos/v2/list')
|
||||||
.then(response=>{
|
.then(response=>{
|
||||||
@ -80,6 +98,19 @@ class BookComponent extends React.Component<any, any>{
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getControlsDisable = ()=>{
|
||||||
|
const {mode, currIndex, src} = this.state
|
||||||
|
const {nextDisable, prevDisable} = getBookViewModes(currIndex, src.length).find(item=> item.value === mode) || {
|
||||||
|
nextDisable: true,
|
||||||
|
prevDisable: true
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
nextDisable,
|
||||||
|
prevDisable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render(){
|
render(){
|
||||||
const {mode} = this.state
|
const {mode} = this.state
|
||||||
|
|
||||||
@ -89,6 +120,16 @@ class BookComponent extends React.Component<any, any>{
|
|||||||
<>
|
<>
|
||||||
{mode === "one page" && <OnePageView {...state}/>}
|
{mode === "one page" && <OnePageView {...state}/>}
|
||||||
{mode === "two pages" && <TwoPagesView {...state}/>}
|
{mode === "two pages" && <TwoPagesView {...state}/>}
|
||||||
|
<BookComponentsController
|
||||||
|
onPrevClick={()=>{
|
||||||
|
this.changePage(true)
|
||||||
|
}}
|
||||||
|
onNextClick={()=>{
|
||||||
|
this.changePage(false)
|
||||||
|
}}
|
||||||
|
{...this.getControlsDisable()}
|
||||||
|
{...this.state}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</BookContext.Consumer>
|
</BookContext.Consumer>
|
||||||
|
@ -2,19 +2,22 @@ import React, {useState, useCallback, useEffect} from 'react'
|
|||||||
import {Slider, InputNumber} from 'antd'
|
import {Slider, InputNumber} from 'antd'
|
||||||
import styled from 'styled-components'
|
import styled from 'styled-components'
|
||||||
|
|
||||||
|
|
||||||
const Container = styled.div`
|
const Container = styled.div`
|
||||||
width: 100%;
|
width: 100%;
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
`
|
`
|
||||||
|
|
||||||
const JumpControls = (props: any)=>{
|
const JumpControls = (props: any)=>{
|
||||||
const {src, currIndex, setBookContextState} = props
|
const {src, currIndex, setBookContextState, pagesToTurn} = props
|
||||||
const [inputValue, setInputValue] = useState(currIndex)
|
const [inputValue, setInputValue] = useState(currIndex)
|
||||||
const [sliderValue, setSliderValue] = useState(currIndex)
|
const [sliderValue, setSliderValue] = useState(currIndex)
|
||||||
const onChange = useCallback(
|
const onChange = useCallback(
|
||||||
(value) => {
|
(value) => {
|
||||||
|
|
||||||
if(value){
|
if(value){
|
||||||
const newCurrIndex = value -1
|
const newCurrIndex = value -1
|
||||||
|
|
||||||
if(newCurrIndex < src.length && newCurrIndex >=0){
|
if(newCurrIndex < src.length && newCurrIndex >=0){
|
||||||
setBookContextState({
|
setBookContextState({
|
||||||
currIndex: value - 1,
|
currIndex: value - 1,
|
||||||
@ -46,11 +49,13 @@ const JumpControls = (props: any)=>{
|
|||||||
min={1}
|
min={1}
|
||||||
max={src.length}
|
max={src.length}
|
||||||
onAfterChange={onChange}
|
onAfterChange={onChange}
|
||||||
|
step={pagesToTurn}
|
||||||
defaultValue={typeof inputValue === 'number' ? inputValue : 1}
|
defaultValue={typeof inputValue === 'number' ? inputValue : 1}
|
||||||
{...sliderRestProps()}
|
{...sliderRestProps()}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<InputNumber
|
<InputNumber
|
||||||
|
className={"jump-control"}
|
||||||
min={1}
|
min={1}
|
||||||
max={src.length}
|
max={src.length}
|
||||||
style={{ margin: '0 8px' }}
|
style={{ margin: '0 8px' }}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import React, {useState, useEffect} from 'react'
|
import React from 'react'
|
||||||
import {Transition, animated} from 'react-spring/renderprops'
|
import {Transition, animated} from 'react-spring/renderprops'
|
||||||
import styled, { } from 'styled-components'
|
import styled, { } from 'styled-components'
|
||||||
import BookComponentsController from './BookComponentsController'
|
|
||||||
|
|
||||||
const ComponentContainer = styled.div`
|
const ComponentContainer = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
@ -41,16 +40,7 @@ const PageComponent = (props: any)=>{
|
|||||||
|
|
||||||
|
|
||||||
function BookComponent(props: any) {
|
function BookComponent(props: any) {
|
||||||
const {src, currIndex, flagToReverse, changePage} = props
|
const {src, currIndex, flagToReverse} = props
|
||||||
const [prevDisable, setPrevDisable] = useState(true)
|
|
||||||
const [nextDisable, setNextDisable] = useState(false)
|
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setNextDisable(currIndex >= src.length - 1)
|
|
||||||
setPrevDisable(currIndex <= 0)
|
|
||||||
}, [currIndex, src])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ComponentContainer>
|
<ComponentContainer>
|
||||||
<PageContainer>
|
<PageContainer>
|
||||||
@ -76,15 +66,6 @@ function BookComponent(props: any) {
|
|||||||
}
|
}
|
||||||
</Transition>
|
</Transition>
|
||||||
</PageContainer>
|
</PageContainer>
|
||||||
|
|
||||||
<BookComponentsController
|
|
||||||
prevDisable={prevDisable}
|
|
||||||
onPrevClick={()=> changePage(-1, true)}
|
|
||||||
nextDisable={nextDisable}
|
|
||||||
onNextClick={()=>changePage(1, false)}
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
|
|
||||||
</ComponentContainer>
|
</ComponentContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
import React, {useState, useEffect, useCallback} from 'react'
|
import React, {useState, useEffect} from 'react'
|
||||||
import {Transition, animated} from 'react-spring/renderprops'
|
import {Transition, animated} from 'react-spring/renderprops'
|
||||||
import BookComponentsController from './BookComponentsController'
|
|
||||||
import {ComponentContainer, PageComponent, PageContainer} from './OnePageView'
|
import {ComponentContainer, PageComponent, PageContainer} from './OnePageView'
|
||||||
import { IBookContext } from './BookComponent'
|
import { IBookContext } from './BookComponent'
|
||||||
|
|
||||||
export default function TwoPagesView(props: any) {
|
export default function TwoPagesView(props: any) {
|
||||||
let {src, currIndex, changePage, flagToReverse} = props
|
let {src, currIndex, flagToReverse} = props
|
||||||
const [pagesToshow, setPagesToShow] = useState<IBookContext[]>([])
|
const [pagesToshow, setPagesToShow] = useState<IBookContext[]>([])
|
||||||
const [prevDisable, setPrevDisable] = useState(true)
|
|
||||||
const [nextDisable, setNextDisable] = useState(false)
|
|
||||||
|
|
||||||
|
|
||||||
useEffect(()=>{
|
useEffect(()=>{
|
||||||
const shouldShowPage = (pageIndex: number)=>{
|
const shouldShowPage = (pageIndex: number)=>{
|
||||||
@ -30,19 +26,8 @@ export default function TwoPagesView(props: any) {
|
|||||||
}
|
}
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
setPrevDisable(newCurrIndex <= 1)
|
|
||||||
setNextDisable(newCurrIndex >= src.length - 2)
|
|
||||||
}, [currIndex, src])
|
}, [currIndex, src])
|
||||||
|
|
||||||
const pageChangeClick = useCallback((indexToGo, flagToReverse)=>{
|
|
||||||
const newIndex = currIndex + indexToGo
|
|
||||||
const shouldChangePage = newIndex >= 0 && newIndex < src.length
|
|
||||||
if(shouldChangePage){
|
|
||||||
changePage(indexToGo, flagToReverse)
|
|
||||||
}
|
|
||||||
}, [src.length, currIndex, changePage])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ComponentContainer>
|
<ComponentContainer>
|
||||||
<PageContainer>
|
<PageContainer>
|
||||||
@ -96,18 +81,6 @@ export default function TwoPagesView(props: any) {
|
|||||||
}
|
}
|
||||||
</Transition>
|
</Transition>
|
||||||
</PageContainer>
|
</PageContainer>
|
||||||
<BookComponentsController
|
|
||||||
prevDisable={prevDisable}
|
|
||||||
onPrevClick={()=> {
|
|
||||||
pageChangeClick(-2, true)
|
|
||||||
|
|
||||||
}}
|
|
||||||
nextDisable={nextDisable}
|
|
||||||
onNextClick={()=>{
|
|
||||||
pageChangeClick(2, false)
|
|
||||||
}}
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
</ComponentContainer>
|
</ComponentContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import React, {useCallback} from 'react'
|
import React, {useCallback} from 'react'
|
||||||
import {Select} from 'antd'
|
import {Select} from 'antd'
|
||||||
import {IBookContext, bookViewModes} from './BookComponent'
|
import {IBookContext, getBookViewModes} from './BookComponent'
|
||||||
|
|
||||||
|
|
||||||
const {Option} = Select
|
const {Option} = Select
|
||||||
@ -10,14 +10,15 @@ export default function ViewSwitcher(props: IBookContext) {
|
|||||||
|
|
||||||
const viewModeChange = useCallback((mode) => {
|
const viewModeChange = useCallback((mode) => {
|
||||||
setBookContextState({
|
setBookContextState({
|
||||||
mode
|
mode,
|
||||||
|
pagesToTurn: getBookViewModes().find(item=> item.value === mode)?.pagesToTurn || 0
|
||||||
})
|
})
|
||||||
},[setBookContextState])
|
},[setBookContextState])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Select value={mode} onSelect={viewModeChange}>
|
<Select value={mode} onSelect={viewModeChange}>
|
||||||
{
|
{
|
||||||
bookViewModes.map((item: any)=>{
|
getBookViewModes().map((item: any)=>{
|
||||||
return <Option value={item.value} key={item.value}>{item.text}</Option>
|
return <Option value={item.value} key={item.value}>{item.text}</Option>
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
3
src/components/index.css
Normal file
3
src/components/index.css
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.jump-control .ant-input-number-handler-wrap{
|
||||||
|
display: none;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user