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

403 lines
12 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';
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 PresentationToolbarContainer from './presentation-toolbar/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';
export default 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);
}
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;
}
2017-09-18 09:58:37 +08:00
getPresentationSizesAvailable() {
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)
if (this.props.userIsPresenter || this.props.multiUser) {
({ clientWidth, clientHeight } = refWhiteboardArea);
}
2017-09-18 09:58:37 +08:00
presentationSizes.presentationHeight = clientHeight;
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
2017-09-18 09:58:37 +08:00
this.setState(presentationSizes);
}
}
calculateSize() {
const originalWidth = this.props.currentSlide.calculatedData.width;
const originalHeight = this.props.currentSlide.calculatedData.height;
2017-09-18 09:58:37 +08:00
const { presentationHeight, presentationWidth } = this.state;
let adjustedWidth;
let adjustedHeight;
// Slide has a portrait orientation
if (originalWidth <= originalHeight) {
2017-09-18 09:58:37 +08:00
adjustedWidth = (presentationHeight * originalWidth) / originalHeight;
if (presentationWidth < adjustedWidth) {
adjustedHeight = (presentationHeight * presentationWidth) / adjustedWidth;
adjustedWidth = presentationWidth;
} else {
2017-09-18 09:58:37 +08:00
adjustedHeight = presentationHeight;
}
// Slide has a landscape orientation
} else {
2017-09-18 09:58:37 +08:00
adjustedHeight = (presentationWidth * originalHeight) / originalWidth;
if (presentationHeight < adjustedHeight) {
adjustedWidth = (presentationWidth * presentationHeight) / adjustedHeight;
adjustedHeight = presentationHeight;
} else {
2017-09-18 09:58:37 +08:00
adjustedWidth = presentationWidth;
}
}
return {
width: adjustedWidth,
height: adjustedHeight,
};
}
zoomChanger(zoom) {
let newZoom = zoom;
const isDifferent = newZoom !== this.state.zoom;
if (newZoom <= HUNDRED_PERCENT) {
newZoom = HUNDRED_PERCENT;
} else if (zoom >= 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() {
this.setState({
fitToWidth: !this.state.fitToWidth,
});
}
// renders the whole presentation area
2017-02-17 05:11:46 +08:00
renderPresentationArea() {
// sometimes tomcat publishes the slide url, but the actual file is not accessible (why?)
if (!this.props.currentSlide
|| !this.props.currentSlide.calculatedData) {
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
const slideObj = this.props.currentSlide;
// retrieving the pre-calculated data from the slide object
const {
x,
y,
width,
height,
viewBoxWidth,
viewBoxHeight,
imageUri,
} = slideObj.calculatedData;
2018-09-25 20:47:49 +08:00
const svgDimensions = this.state.fitToWidth ? {
width: 'inherit',
} : {
width: adjustedSizes.width,
height: adjustedSizes.height,
};
return (
<div
2018-09-25 20:47:49 +08:00
style={svgDimensions}
>
<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"
width={width}
height={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}
>
<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
width={width}
height={height}
whiteboardId={slideObj.id}
/>
<CursorWrapperContainer
podId={this.props.podId}
2018-04-10 07:18:49 +08:00
whiteboardId={slideObj.id}
widthRatio={slideObj.widthRatio}
physicalWidthRatio={adjustedSizes.width / width}
slideWidth={width}
slideHeight={height}
2018-09-25 20:47:49 +08:00
radius={this.state.fitToWidth ? 2 : 5}
/>
</g>
{this.renderOverlays(slideObj, adjustedSizes)}
</svg>
</CSSTransition>
</TransitionGroup>
</div>
);
}
renderOverlays(slideObj, adjustedSizes) {
if (!this.props.userIsPresenter && !this.props.multiUser) {
return null;
}
// retrieving the pre-calculated data from the slide object
const {
x,
y,
width,
height,
viewBoxWidth,
viewBoxHeight,
} = slideObj.calculatedData;
return (
<PresentationOverlayContainer
2018-08-14 03:29:38 +08:00
podId={this.props.podId}
currentSlideNum={this.props.currentSlide.num}
slide={slideObj}
2018-04-10 07:18:49 +08:00
whiteboardId={slideObj.id}
slideWidth={width}
slideHeight={height}
2018-09-05 01:37:36 +08:00
delta={this.state.delta}
viewBoxWidth={viewBoxWidth}
viewBoxHeight={viewBoxHeight}
zoom={this.state.zoom}
zoomChanger={this.zoomChanger}
2018-08-14 03:29:38 +08:00
adjustedSizes={adjustedSizes}
getSvgRef={this.getSvgRef}
presentationSize={this.getPresentationSizesAvailable()}
2018-09-18 02:02:52 +08:00
touchZoom={this.state.touchZoom}
2018-09-25 20:47:49 +08:00
fitToWidth={this.state.fitToWidth}
>
<WhiteboardOverlayContainer
getSvgRef={this.getSvgRef}
whiteboardId={slideObj.id}
slideWidth={width}
slideHeight={height}
viewBoxX={x}
viewBoxY={y}
2018-09-05 01:37:36 +08:00
pointChanger={this.pointUpdate}
viewBoxWidth={viewBoxWidth}
viewBoxHeight={viewBoxHeight}
physicalSlideWidth={(adjustedSizes.width / slideObj.widthRatio) * 100}
physicalSlideHeight={(adjustedSizes.height / slideObj.heightRatio) * 100}
2018-09-18 02:02:52 +08:00
zoom={this.state.zoom}
zoomChanger={this.zoomChanger}
touchUpdate={this.touchUpdate}
/>
</PresentationOverlayContainer>
);
}
renderPresentationToolbar() {
if (!this.props.currentSlide) {
return null;
2016-08-06 02:39:24 +08:00
}
return (
<PresentationToolbarContainer
podId={this.props.podId}
currentSlideNum={this.props.currentSlide.num}
presentationId={this.props.currentSlide.presentationId}
zoom={this.state.zoom}
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() {
if (!this.props.currentSlide
|| !this.props.currentSlide.calculatedData) {
2017-09-21 05:05:17 +08:00
return null;
}
2017-09-21 05:05:17 +08:00
const adjustedSizes = this.calculateSize();
return (
<WhiteboardToolbarContainer
whiteboardId={this.props.currentSlide.id}
height={adjustedSizes.height}
/>
);
2017-04-19 08:54:51 +08:00
}
render() {
return (
2017-02-17 05:11:46 +08:00
<div 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}
/>
{this.state.showSlide
? this.renderPresentationArea()
: null }
{this.props.userIsPresenter || this.props.multiUser
? this.renderWhiteboardToolbar()
2017-04-19 08:54:51 +08:00
: null }
</div>
{this.renderPresentationToolbar()}
</div>
);
}
}
PresentationArea.propTypes = {
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,
};