Skip to content

Commit

Permalink
Add pdf slideshow partial
Browse files Browse the repository at this point in the history
  • Loading branch information
nygrenh committed Dec 27, 2018
1 parent 493184a commit 81bc44e
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
71 changes: 71 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"interactjs": "^1.3.4",
"jquery": "^3.3.1",
"moment": "^2.23.0",
"pdf-slideshow": "^0.1.2",
"prismjs": "^1.15.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
Expand Down
7 changes: 7 additions & 0 deletions src/partials/PdfSlideshow/PdfSlideshowLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'
import PdfSlideshow, { setPdfJsWorkerPath } from 'pdf-slideshow'
import { withPrefix } from 'gatsby'

setPdfJsWorkerPath(withPrefix('/pdf.worker.min.js'))

export default props => <PdfSlideshow {...props} />
56 changes: 56 additions & 0 deletions src/partials/PdfSlideshow/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { Fragment, lazy, Suspense } from 'react'
import { Paper } from '@material-ui/core'
import styled from 'styled-components'
import withSimpleErrorBoundary from '../../util/withSimpleErrorBoundary'
import Loading from '../../components/Loading'
const PdfSlideshow = lazy(() => import('./PdfSlideshowLoader'))

const HiddenLinkWrapper = styled.div`
display: none;
`

const StyledPaper = styled(Paper)`
@media only screen and (max-width: 800px) {
overflow-y: scroll;
}
`

class PdfSlideshowWrapper extends React.Component {
state = {
render: false,
path: undefined,
}

constructor(props) {
super(props)
this.linkContainer = React.createRef()
}

componentDidMount() {
const links = this.linkContainer.current
const path = links.querySelector('a').href
this.setState({ render: true, path })
}

render() {
if (!this.state.render) {
return (
<Fragment>
<Loading loading={true} heightHint="540px" />
<HiddenLinkWrapper ref={this.linkContainer}>
{this.props.children}
</HiddenLinkWrapper>
</Fragment>
)
}
return (
<Suspense fallback={<div style={{ height: '540px' }}>Loading...</div>}>
<StyledPaper>
<PdfSlideshow slideWidth={800} pdfLocation={this.state.path} />
</StyledPaper>
</Suspense>
)
}
}

export default withSimpleErrorBoundary(PdfSlideshowWrapper)
2 changes: 2 additions & 0 deletions src/partials/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Quiznator from './Quiznator'
import PleaseLogin from './PleaseLogin'
import FloatImageRight from './FloatImageRight'
import CodeStatesVisualizer from './CodeStatesVisualizer'
import PdfSlideshow from './PdfSlideshow'

import {
Table,
Expand All @@ -52,6 +53,7 @@ const mapping = {
'please-login': PleaseLogin,
'float-image-right': FloatImageRight,
'code-states-visualizer': CodeStatesVisualizer,
'pdf-slideshow': PdfSlideshow,
youtube: Youtube,
quiznator: Quiznator,
table: Table,
Expand Down
1 change: 1 addition & 0 deletions static/pdf.worker.min.js

Large diffs are not rendered by default.

0 comments on commit 81bc44e

Please sign in to comment.