bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/presentation/component.jsx

642 lines
19 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { TransitionGroup, CSSTransition } from 'react-transition-group';
import WhiteboardOverlayContainer from '/imports/ui/components/whiteboard/whiteboard-overlay/container';
2017-04-19 08:54:51 +08:00
import WhiteboardToolbarContainer from '/imports/ui/components/whiteboard/whiteboard-toolbar/container';
import { HUNDRED_PERCENT, MAX_PERCENT } from '/imports/utils/slideCalcUtils';
2019-03-12 00:21:12 +08:00
import { defineMessages, injectIntl, intlShape } from 'react-intl';
import PresentationToolbarContainer from './presentation-toolbar/container';
2017-08-15 08:15:11 +08:00
import CursorWrapperContainer from './cursor/cursor-wrapper-container/container';
2017-07-27 20:35:55 +08:00
import AnnotationGroupContainer from '../whiteboard/annotation-group/container';
import PresentationOverlayContainer from './presentation-overlay/container';
import Slide from './slide/component';
2018-01-08 14:17:18 +08:00
import { styles } from './styles.scss';
2019-02-08 01:47:28 +08:00
import MediaService, { shouldEnableSwapLayout } from '../media/service';
import PresentationCloseButton from './presentation-close-button/component';
import DownloadPresentationButton from './download-presentation-button/component';
import FullscreenButtonContainer from '../video-provider/fullscreen-button/container';
2019-03-12 00:21:12 +08:00
const intlMessages = defineMessages({
presentationLabel: {
id: 'app.presentationUploder.title',
description: 'presentation area element label',
},
2019-05-10 01:57:06 +08:00
changeNotification: {
id: 'app.presentation.notificationLabel',
description: 'label displayed in toast when presentation switches',
},
2019-03-12 00:21:12 +08:00
});
class PresentationArea extends Component {
constructor() {
super();
this.state = {
2017-09-18 09:58:37 +08:00
presentationWidth: 0,
presentationHeight: 0,
showSlide: false,
zoom: 100,
2018-09-18 02:02:52 +08:00
touchZoom: false,
2018-09-05 01:37:36 +08:00
delta: {
x: 0,
y: 0,
},
2018-09-25 20:47:49 +08:00
fitToWidth: false,
};
this.getSvgRef = this.getSvgRef.bind(this);
this.zoomChanger = this.zoomChanger.bind(this);
2018-09-18 02:02:52 +08:00
this.touchUpdate = this.touchUpdate.bind(this);
2018-09-05 01:37:36 +08:00
this.pointUpdate = this.pointUpdate.bind(this);
2018-09-25 20:47:49 +08:00
this.fitToWidthHandler = this.fitToWidthHandler.bind(this);
}
2019-03-20 03:50:08 +08:00
componentDidUpdate(prevProps, prevState) {
2019-05-10 01:57:06 +08:00
const { currentPresentation, notify, intl } = this.props;
if (prevProps.currentPresentation.name !== currentPresentation.name) {
notify(
2019-05-10 01:57:06 +08:00
`${intl.formatMessage(intlMessages.changeNotification)} ${currentPresentation.name}`,
'info',
2019-05-10 01:57:06 +08:00
'presentation',
);
}
2019-03-20 03:50:08 +08:00
if (prevState.fitToWidth) {
// When presenter is changed or slide changed we reset fitToWidth
if ((prevProps.userIsPresenter && !this.props.userIsPresenter)
|| (prevProps.currentSlide.id !== this.props.currentSlide.id)) {
2019-03-20 03:50:08 +08:00
this.setState({
fitToWidth: false,
});
}
}
}
componentDidMount() {
// adding an event listener to scale the whiteboard on 'resize' events sent by chat/userlist etc
window.addEventListener('resize', () => {
setTimeout(this.handleResize.bind(this), 0);
});
2017-09-18 09:58:37 +08:00
this.getInitialPresentationSizes();
}
componentWillUnmount() {
2017-09-06 09:36:15 +08:00
window.removeEventListener('resize', () => {
setTimeout(this.handleResize.bind(this), 0);
});
}
// returns a ref to the svg element, which is required by a WhiteboardOverlay
// to transform screen coordinates to svg coordinate system
getSvgRef() {
return this.svggroup;
}
getToolbarHeight() {
const { refPresentationToolbar } = this;
let height = 0;
if (refPresentationToolbar) {
const { clientHeight } = refPresentationToolbar;
height = clientHeight;
}
return height;
}
2017-09-18 09:58:37 +08:00
getPresentationSizesAvailable() {
2019-02-12 21:35:52 +08:00
const { userIsPresenter, multiUser } = this.props;
2017-09-18 09:58:37 +08:00
const { refPresentationArea, refWhiteboardArea } = this;
const presentationSizes = {};
2017-09-18 09:58:37 +08:00
if (refPresentationArea && refWhiteboardArea) {
// By default presentation sizes are equal to the sizes of the refPresentationArea
// direct parent of the svg wrapper
let { clientWidth, clientHeight } = refPresentationArea;
2017-09-18 09:58:37 +08:00
// if a user is a presenter - this means there is a whiteboard toolbar on the right
// and we have to get the width/height of the refWhiteboardArea
// (inner hidden div with absolute position)
2019-02-08 01:47:28 +08:00
if (userIsPresenter || multiUser) {
({ clientWidth, clientHeight } = refWhiteboardArea);
}
presentationSizes.presentationHeight = clientHeight - this.getToolbarHeight();
2017-09-18 09:58:37 +08:00
presentationSizes.presentationWidth = clientWidth;
2017-09-06 09:36:15 +08:00
}
2017-09-18 09:58:37 +08:00
return presentationSizes;
2017-09-06 09:36:15 +08:00
}
2017-09-18 09:58:37 +08:00
getInitialPresentationSizes() {
// determining the presentationWidth and presentationHeight (available space for the svg)
// on the initial load
2018-10-19 01:03:11 +08:00
2017-09-18 09:58:37 +08:00
const presentationSizes = this.getPresentationSizesAvailable();
if (Object.keys(presentationSizes).length > 0) {
// setting the state of the available space for the svg
2017-09-06 09:36:15 +08:00
// and set the showSlide to true to start rendering the slide
this.setState({
presentationHeight: presentationSizes.presentationHeight,
presentationWidth: presentationSizes.presentationWidth,
showSlide: true,
});
2017-09-06 09:36:15 +08:00
}
}
handleResize() {
2017-09-18 09:58:37 +08:00
const presentationSizes = this.getPresentationSizesAvailable();
if (Object.keys(presentationSizes).length > 0) {
// updating the size of the space available for the slide
2019-03-12 21:56:05 +08:00
this.setState({
presentationHeight: presentationSizes.presentationHeight,
presentationWidth: presentationSizes.presentationWidth,
});
}
}
calculateSize() {
2019-03-20 03:50:08 +08:00
const { presentationHeight, presentationWidth, fitToWidth } = this.state;
2019-02-08 01:47:28 +08:00
const { currentSlide } = this.props;
2019-04-03 01:10:00 +08:00
const slideSizes = currentSlide
&& currentSlide.calculatedData
? currentSlide.calculatedData : {};
const originalWidth = slideSizes.width;
const originalHeight = slideSizes.height;
let adjustedWidth;
let adjustedHeight;
2019-03-28 19:51:38 +08:00
if (!originalHeight || !originalWidth) {
return {
width: 0,
height: 0,
};
}
2019-03-20 03:50:08 +08:00
if (!fitToWidth) {
// Slide has a portrait orientation
if (originalWidth <= originalHeight) {
adjustedWidth = (presentationHeight * originalWidth) / originalHeight;
if (presentationWidth < adjustedWidth) {
adjustedHeight = (presentationHeight * presentationWidth) / adjustedWidth;
adjustedWidth = presentationWidth;
} else {
adjustedHeight = presentationHeight;
}
// Slide has a landscape orientation
} else {
2019-03-20 03:50:08 +08:00
adjustedHeight = (presentationWidth * originalHeight) / originalWidth;
if (presentationHeight < adjustedHeight) {
adjustedWidth = (presentationWidth * presentationHeight) / adjustedHeight;
adjustedHeight = presentationHeight;
} else {
adjustedWidth = presentationWidth;
}
}
2019-03-20 03:50:08 +08:00
} else {
adjustedWidth = presentationWidth;
adjustedHeight = (adjustedWidth * originalHeight) / originalWidth;
if (adjustedHeight > presentationHeight) adjustedHeight = presentationHeight;
}
return {
width: adjustedWidth,
2019-02-19 20:27:54 +08:00
height: adjustedHeight,
};
}
2019-03-20 03:50:08 +08:00
// TODO: This could be replaced if we synchronize the fit-to-width state between users
checkFitToWidth() {
const { userIsPresenter, currentSlide } = this.props;
const { fitToWidth } = this.state;
if (userIsPresenter) {
return fitToWidth;
}
const {
width, height, viewBoxWidth, viewBoxHeight,
} = currentSlide.calculatedData;
const slideSizeRatio = width / height;
const viewBoxSizeRatio = viewBoxWidth / viewBoxHeight;
if (slideSizeRatio !== viewBoxSizeRatio) {
return true;
}
return false;
2019-03-20 03:50:08 +08:00
}
2019-02-08 01:47:28 +08:00
zoomChanger(incomingZoom) {
const { zoom } = this.state;
let newZoom = incomingZoom;
const isDifferent = newZoom !== zoom;
if (newZoom <= HUNDRED_PERCENT) {
newZoom = HUNDRED_PERCENT;
2019-02-08 01:47:28 +08:00
} else if (incomingZoom >= MAX_PERCENT) {
newZoom = MAX_PERCENT;
}
if (isDifferent) this.setState({ zoom: newZoom });
}
2018-09-05 01:37:36 +08:00
pointUpdate(pointX, pointY) {
this.setState({
delta: {
x: pointX,
y: pointY,
},
});
}
2018-09-18 02:02:52 +08:00
touchUpdate(bool) {
this.setState({
touchZoom: bool,
});
}
2018-09-25 20:47:49 +08:00
fitToWidthHandler() {
2019-02-08 01:47:28 +08:00
const { fitToWidth } = this.state;
2019-02-12 21:35:52 +08:00
this.setState({
fitToWidth: !fitToWidth,
});
2019-02-08 01:47:28 +08:00
}
isPresentationAccessible() {
const { currentSlide } = this.props;
// sometimes tomcat publishes the slide url, but the actual file is not accessible (why?)
return currentSlide && currentSlide.calculatedData;
2018-09-25 20:47:49 +08:00
}
2019-02-12 21:35:52 +08:00
renderPresentationClose() {
const { isFullscreen } = this.props;
if (!shouldEnableSwapLayout() || isFullscreen) {
return null;
}
return <PresentationCloseButton toggleSwapLayout={MediaService.toggleSwapLayout} />;
}
renderOverlays(slideObj, adjustedSizes) {
const {
userIsPresenter,
multiUser,
podId,
currentSlide,
} = this.props;
const {
delta,
zoom,
touchZoom,
fitToWidth,
} = this.state;
if (!userIsPresenter && !multiUser) {
return null;
}
// retrieving the pre-calculated data from the slide object
const {
x,
y,
width,
height,
viewBoxWidth,
viewBoxHeight,
} = slideObj.calculatedData;
return (
<PresentationOverlayContainer
podId={podId}
currentSlideNum={currentSlide.num}
slide={slideObj}
whiteboardId={slideObj.id}
slideWidth={width}
slideHeight={height}
delta={delta}
viewBoxWidth={viewBoxWidth}
viewBoxHeight={viewBoxHeight}
zoom={zoom}
zoomChanger={this.zoomChanger}
adjustedSizes={adjustedSizes}
getSvgRef={this.getSvgRef}
presentationSize={this.getPresentationSizesAvailable()}
touchZoom={touchZoom}
fitToWidth={fitToWidth}
>
<WhiteboardOverlayContainer
getSvgRef={this.getSvgRef}
whiteboardId={slideObj.id}
slideWidth={width}
slideHeight={height}
viewBoxX={x}
viewBoxY={y}
pointChanger={this.pointUpdate}
viewBoxWidth={viewBoxWidth}
viewBoxHeight={viewBoxHeight}
physicalSlideWidth={(adjustedSizes.width / slideObj.widthRatio) * 100}
physicalSlideHeight={(adjustedSizes.height / slideObj.heightRatio) * 100}
zoom={zoom}
zoomChanger={this.zoomChanger}
touchUpdate={this.touchUpdate}
/>
</PresentationOverlayContainer>
);
}
// renders the whole presentation area
2017-02-17 05:11:46 +08:00
renderPresentationArea() {
2019-03-20 03:50:08 +08:00
const { presentationWidth } = this.state;
const { podId, currentSlide, isFullscreen } = this.props;
2019-02-08 01:47:28 +08:00
if (!this.isPresentationAccessible()) return null;
// to control the size of the svg wrapper manually
// and adjust cursor's thickness, so that svg didn't scale it automatically
const adjustedSizes = this.calculateSize();
// a reference to the slide object
2019-02-08 01:47:28 +08:00
const slideObj = currentSlide;
// retrieving the pre-calculated data from the slide object
const {
x,
y,
width,
height,
viewBoxWidth,
viewBoxHeight,
imageUri,
} = slideObj.calculatedData;
2019-02-12 21:35:52 +08:00
2019-03-20 03:50:08 +08:00
const svgAreaDimensions = this.checkFitToWidth()
2019-02-12 21:35:52 +08:00
? {
position: 'absolute',
width: 'inherit',
2019-03-12 21:56:05 +08:00
height: adjustedSizes.height,
2019-02-12 21:35:52 +08:00
}
: {
position: 'absolute',
width: adjustedSizes.width,
height: adjustedSizes.height,
2019-02-19 20:27:54 +08:00
textAlign: 'center',
2019-02-12 21:35:52 +08:00
};
return (
<div
2019-02-19 20:27:54 +08:00
style={svgAreaDimensions}
>
{this.renderPresentationClose()}
{this.renderPresentationDownload()}
{isFullscreen ? null : this.renderPresentationFullscreen()}
<TransitionGroup>
<CSSTransition
key={slideObj.id}
classNames={{
enter: styles.enter,
enterActive: styles.enterActive,
appear: styles.appear,
appearActive: styles.appearActive,
}}
appear
enter
exit={false}
timeout={{ enter: 400 }}
>
<svg
2018-12-01 01:59:14 +08:00
data-test="whiteboard"
2019-02-08 01:47:28 +08:00
{...{
width,
height,
}}
ref={(ref) => { if (ref != null) { this.svggroup = ref; } }}
viewBox={`${x} ${y} ${viewBoxWidth} ${viewBoxHeight}`}
version="1.1"
xmlns="http://www.w3.org/2000/svg"
className={styles.svgStyles}
2019-03-20 03:50:08 +08:00
style={this.checkFitToWidth()
2019-03-12 21:56:05 +08:00
? {
position: 'absolute',
}
: null
}
>
<defs>
<clipPath id="viewBox">
<rect x={x} y={y} width="100%" height="100%" fill="none" />
</clipPath>
</defs>
<g clipPath="url(#viewBox)">
<Slide
imageUri={imageUri}
svgWidth={width}
svgHeight={height}
/>
<AnnotationGroupContainer
2019-02-08 01:47:28 +08:00
{...{
width,
height,
}}
whiteboardId={slideObj.id}
/>
<CursorWrapperContainer
2019-02-08 01:47:28 +08:00
podId={podId}
2018-04-10 07:18:49 +08:00
whiteboardId={slideObj.id}
widthRatio={slideObj.widthRatio}
2019-03-20 03:50:08 +08:00
physicalWidthRatio={this.checkFitToWidth() ? (presentationWidth / width) : (adjustedSizes.width / width)}
slideWidth={width}
slideHeight={height}
/>
</g>
{this.renderOverlays(slideObj, adjustedSizes)}
</svg>
</CSSTransition>
</TransitionGroup>
</div>
);
}
2019-02-12 21:35:52 +08:00
renderPresentationToolbar() {
const {
2019-02-12 21:35:52 +08:00
currentSlide,
podId,
isFullscreen,
2019-02-12 21:35:52 +08:00
} = this.props;
const { zoom, fitToWidth } = this.state;
2019-02-12 21:35:52 +08:00
if (!currentSlide) {
return null;
}
return (
<PresentationToolbarContainer
{...{
fitToWidth,
zoom,
podId,
}}
isFullscreen={isFullscreen}
fullscreenRef={this.refPresentationContainer}
2019-02-08 01:47:28 +08:00
currentSlideNum={currentSlide.num}
presentationId={currentSlide.presentationId}
zoomChanger={this.zoomChanger}
2018-09-25 20:47:49 +08:00
fitToWidthHandler={this.fitToWidthHandler}
/>
);
2016-08-06 02:39:24 +08:00
}
2017-04-19 08:54:51 +08:00
renderWhiteboardToolbar() {
2019-02-08 01:47:28 +08:00
const { currentSlide } = this.props;
if (!this.isPresentationAccessible()) return null;
2017-09-21 05:05:17 +08:00
const adjustedSizes = this.calculateSize();
return (
<WhiteboardToolbarContainer
2019-02-08 01:47:28 +08:00
whiteboardId={currentSlide.id}
2017-09-21 05:05:17 +08:00
height={adjustedSizes.height}
/>
);
2017-04-19 08:54:51 +08:00
}
renderPresentationDownload() {
const { presentationIsDownloadable, downloadPresentationUri } = this.props;
if (!presentationIsDownloadable) return null;
const handleDownloadPresentation = () => {
window.open(downloadPresentationUri);
};
return (
<DownloadPresentationButton
handleDownloadPresentation={handleDownloadPresentation}
dark
/>
);
}
2019-03-12 00:21:12 +08:00
renderPresentationFullscreen() {
const {
intl,
userIsPresenter,
} = this.props;
if (userIsPresenter) return null;
return (
<FullscreenButtonContainer
fullscreenRef={this.refPresentationContainer}
2019-03-12 00:21:12 +08:00
elementName={intl.formatMessage(intlMessages.presentationLabel)}
dark
/>
);
}
render() {
2019-03-12 00:21:12 +08:00
const {
userIsPresenter,
multiUser,
} = this.props;
2019-03-12 21:56:05 +08:00
const {
showSlide,
fitToWidth,
} = this.state;
2019-02-08 01:47:28 +08:00
2019-02-19 20:27:54 +08:00
const adjustedSizes = this.calculateSize();
const adjustedHeight = adjustedSizes.height;
2019-03-07 05:57:07 +08:00
const adjustedWidth = adjustedSizes.width;
2019-02-19 20:27:54 +08:00
const toolbarHeight = this.getToolbarHeight();
2019-03-07 23:52:12 +08:00
let toolbarWidth = 0;
if (this.refWhiteboardArea) {
2019-03-12 00:21:12 +08:00
const { clientWidth: areaWidth } = this.refWhiteboardArea;
if (adjustedWidth <= 400
&& adjustedWidth !== areaWidth
2019-03-12 21:56:05 +08:00
&& areaWidth > 400
&& fitToWidth === false) {
2019-03-07 23:52:12 +08:00
toolbarWidth = '400px';
2019-03-12 00:21:12 +08:00
} else if (adjustedWidth === areaWidth
2019-03-12 21:56:05 +08:00
|| areaWidth <= 400
|| fitToWidth === true) {
2019-03-07 23:52:12 +08:00
toolbarWidth = '100%';
} else {
toolbarWidth = adjustedWidth;
}
}
return (
<div
ref={(ref) => { this.refPresentationContainer = ref; }}
className={styles.presentationContainer}
>
<div
2017-09-18 09:58:37 +08:00
ref={(ref) => { this.refPresentationArea = ref; }}
className={styles.presentationArea}
>
<div
2017-09-18 09:58:37 +08:00
ref={(ref) => { this.refWhiteboardArea = ref; }}
className={styles.whiteboardSizeAvailable}
/>
2019-02-19 20:27:54 +08:00
<div
className={styles.svgContainer}
style={{
height: adjustedHeight + toolbarHeight,
}}
>
{showSlide
? this.renderPresentationArea()
: null}
{userIsPresenter || multiUser
? this.renderWhiteboardToolbar()
: null}
{userIsPresenter || multiUser
? (
<div
className={styles.presentationToolbar}
ref={(ref) => { this.refPresentationToolbar = ref; }}
2019-03-07 05:57:07 +08:00
style={
{
2019-03-07 23:52:12 +08:00
width: toolbarWidth,
2019-03-07 05:57:07 +08:00
}
}
2019-02-19 20:27:54 +08:00
>
{this.renderPresentationToolbar()}
</div>
)
: null}
</div>
</div>
</div>
);
}
}
2019-03-12 00:21:12 +08:00
export default injectIntl(PresentationArea);
PresentationArea.propTypes = {
2019-03-12 00:21:12 +08:00
intl: intlShape.isRequired,
podId: PropTypes.string.isRequired,
// Defines a boolean value to detect whether a current user is a presenter
userIsPresenter: PropTypes.bool.isRequired,
currentSlide: PropTypes.shape({
presentationId: PropTypes.string.isRequired,
current: PropTypes.bool.isRequired,
heightRatio: PropTypes.number.isRequired,
widthRatio: PropTypes.number.isRequired,
xOffset: PropTypes.number.isRequired,
yOffset: PropTypes.number.isRequired,
num: PropTypes.number.isRequired,
id: PropTypes.string.isRequired,
calculatedData: PropTypes.shape({
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
viewBoxWidth: PropTypes.number.isRequired,
viewBoxHeight: PropTypes.number.isRequired,
imageUri: PropTypes.string.isRequired,
}),
}),
2017-08-15 08:15:11 +08:00
// current multi-user status
multiUser: PropTypes.bool.isRequired,
};
PresentationArea.defaultProps = {
currentSlide: undefined,
};