From b919c276afa106fd7a7d111ab9c7e8c630ac861d Mon Sep 17 00:00:00 2001 From: pyphilia Date: Tue, 19 Jan 2021 15:44:27 +0100 Subject: [PATCH] feat: display pdf files --- src/components/phase/PhaseItem.js | 29 +++++- src/components/phase/PhasePdf.js | 163 ++++++++++++++++++++++++++++++ src/config/constants.js | 1 + 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/components/phase/PhasePdf.js diff --git a/src/components/phase/PhaseItem.js b/src/components/phase/PhaseItem.js index 19a7385b..e9dc84e9 100644 --- a/src/components/phase/PhaseItem.js +++ b/src/components/phase/PhaseItem.js @@ -9,19 +9,46 @@ import { APPLICATION, IFRAME, DEFAULT_PROTOCOL, + PDF, } from '../../config/constants'; import PhaseText from './PhaseText'; import PhaseImage from './PhaseImage'; import PhaseVideo from './PhaseVideo'; import PhaseApp from './PhaseApp'; +import PhasePdf from './PhasePdf'; // prop types gets confused when dealing with helper renderers // eslint-disable-next-line react/prop-types -const renderResource = ({ id, mimeType, content, asset, url, name }) => { +const renderResource = ({ + id, + mimeType, + content, + asset, + url, + name, + spaceId, + phaseId, + appInstance, +}) => { if (mimeType === TEXT) { return ; } + if (mimeType === PDF) { + return ( + + ); + } + if (IMAGE.test(mimeType)) { return ; } diff --git a/src/components/phase/PhasePdf.js b/src/components/phase/PhasePdf.js new file mode 100644 index 00000000..d69095a7 --- /dev/null +++ b/src/components/phase/PhasePdf.js @@ -0,0 +1,163 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { connect } from 'react-redux'; +import { Resizable } from 're-resizable'; +import SwapVerticalCircleIcon from '@material-ui/icons/SwapVerticalCircle'; +import PlayCircleFilledIcon from '@material-ui/icons/PlayCircleFilled'; +import './PhaseApp.css'; +import { getHeight, setHeight } from '../../actions/layout'; +import { + DEFAULT_APP_HEIGHT, + MAX_APP_HEIGHT, + MIN_APP_HEIGHT, +} from '../../config/layout'; +import { buildPhaseAppName } from '../../config/selectors'; + +const style = { + marginTop: '2rem', + marginBottom: '2rem', +}; + +const iconStyle = { background: 'white', borderRadius: '12px' }; + +class PhasePdf extends Component { + static propTypes = { + url: PropTypes.string, + asset: PropTypes.string, + name: PropTypes.string, + folder: PropTypes.string.isRequired, + id: PropTypes.string.isRequired, + }; + + static defaultProps = { + url: null, + asset: null, + name: 'Pdf', + }; + + state = { + height: DEFAULT_APP_HEIGHT, + }; + + constructor(props) { + super(props); + const { id } = props; + this.state.height = getHeight(id) || DEFAULT_APP_HEIGHT; + } + + componentDidMount() { + window.addEventListener('message', this.handleReceiveMessage); + } + + componentWillUnmount() { + window.removeEventListener('message', this.handleReceiveMessage); + } + + renderHandleIcon = () => { + const { height } = this.state; + if (height >= MAX_APP_HEIGHT) { + return ( + + ); + } + if (height <= MIN_APP_HEIGHT) { + return ( + + ); + } + return ; + }; + + render() { + const { url, asset, name, id, folder } = this.props; + + // replace 'download' by 'raw' to display the resource + // when the space is not saved + let uri = url.replace('download', 'raw'); + if (asset) { + // assets with absolute paths are usually for testing + if (asset.startsWith('/')) { + uri = `file://${asset}`; + } else { + uri = `file://${folder}/${asset}`; + } + } + + // get style + const { height } = this.state; + return ( + { + const { height: oldHeight } = this.state; + const newHeight = oldHeight + d.height; + this.setState({ + height: newHeight, + }); + setHeight(id, newHeight); + }} + handleComponent={{ + bottom: ( +
+ {this.renderHandleIcon()} +
+ ), + }} + > +
+