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

331 lines
9.4 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2016-07-16 04:45:54 +08:00
export default class Cursor extends Component {
2017-09-21 05:05:17 +08:00
static scale(attribute, widthRatio, physicalWidthRatio) {
return (attribute * widthRatio) / physicalWidthRatio;
2017-08-15 08:15:11 +08:00
}
2017-09-21 05:05:17 +08:00
static invertScale(attribute, widthRatio, physicalWidthRatio) {
return (attribute * physicalWidthRatio) / widthRatio;
2017-08-15 08:15:11 +08:00
}
2017-09-21 05:05:17 +08:00
static getCursorCoordinates(cursorX, cursorY, slideWidth, slideHeight) {
2017-08-30 04:58:48 +08:00
// main cursor x and y coordinates
2017-09-21 05:05:17 +08:00
const x = (cursorX / 100) * slideWidth;
const y = (cursorY / 100) * slideHeight;
2017-08-15 08:15:11 +08:00
2017-08-30 04:58:48 +08:00
return {
x,
y,
};
}
2017-08-15 08:15:11 +08:00
2017-09-21 05:05:17 +08:00
static getFillAndLabel(presenter, isMultiUser) {
2017-08-30 04:58:48 +08:00
const obj = {
fill: 'green',
displayLabel: false,
};
if (presenter) {
obj.fill = 'red';
}
if (isMultiUser) {
obj.displayLabel = true;
}
return obj;
}
static getScaledSizes(props) {
2019-03-20 03:50:08 +08:00
// TODO: This might need to change for the use case of fit-to-width portrait
// slides in non-presenter view. Some elements are still shrinking.
const scaleFactor = props.widthRatio / props.physicalWidthRatio;
2016-07-16 04:45:54 +08:00
return {
// Adjust the radius of the cursor according to zoom
// and divide it by the physicalWidth ratio, so that svg scaling wasn't applied to the cursor
2017-08-30 04:58:48 +08:00
finalRadius: props.radius * scaleFactor,
2017-08-15 08:15:11 +08:00
// scaling the properties for cursorLabel and the border (rect) around it
cursorLabelText: {
2017-08-30 04:58:48 +08:00
textDY: props.cursorLabelText.textDY * scaleFactor,
textDX: props.cursorLabelText.textDX * scaleFactor,
fontSize: props.cursorLabelText.fontSize * scaleFactor,
2017-08-15 08:15:11 +08:00
},
cursorLabelBox: {
2017-08-30 04:58:48 +08:00
xOffset: props.cursorLabelBox.xOffset * scaleFactor,
yOffset: props.cursorLabelBox.yOffset * scaleFactor,
// making width and height a little bit larger than the size of the text
// received from BBox, so that the text didn't touch the border
width: (props.labelBoxWidth + 3) * scaleFactor,
height: (props.labelBoxHeight + 3) * scaleFactor,
strokeWidth: props.cursorLabelBox.labelBoxStrokeWidth * scaleFactor,
2017-08-15 08:15:11 +08:00
},
};
}
2016-07-16 04:45:54 +08:00
componentWillMount() {
const {
cursorX,
cursorY,
slideWidth,
slideHeight,
presenter,
isMultiUser,
} = this.props;
// setting the initial cursor info
this.scaledSizes = Cursor.getScaledSizes(this.props);
this.cursorCoordinate = Cursor.getCursorCoordinates(
2017-09-21 05:05:17 +08:00
cursorX,
cursorY,
slideWidth,
slideHeight,
);
2017-08-30 04:58:48 +08:00
const { fill, displayLabel } = Cursor.getFillAndLabel(presenter, isMultiUser);
2017-08-30 04:58:48 +08:00
this.fill = fill;
this.displayLabel = displayLabel;
}
2017-08-15 08:15:11 +08:00
componentDidMount() {
// we need to find the BBox of the text, so that we could set a proper border box arount it
this.calculateCursorLabelBoxDimensions();
}
componentWillReceiveProps(nextProps) {
2017-08-30 04:58:48 +08:00
const {
presenter,
isMultiUser,
widthRatio,
physicalWidthRatio,
labelBoxWidth,
labelBoxHeight,
cursorX,
cursorY,
} = this.props;
2017-08-30 04:58:48 +08:00
if (presenter !== nextProps.presenter || isMultiUser !== nextProps.isMultiUser) {
2017-09-21 05:05:17 +08:00
const { fill, displayLabel } = Cursor.getFillAndLabel(
nextProps.presenter,
nextProps.isMultiUser,
);
2017-08-30 04:58:48 +08:00
this.displayLabel = displayLabel;
this.fill = fill;
}
if ((widthRatio !== nextProps.widthRatio
|| physicalWidthRatio !== nextProps.physicalWidthRatio)
|| (labelBoxWidth !== nextProps.labelBoxWidth
|| labelBoxHeight !== nextProps.labelBoxHeight)) {
this.scaledSizes = Cursor.getScaledSizes(nextProps);
2017-08-30 04:58:48 +08:00
}
if (cursorX !== nextProps.cursorX || cursorY !== nextProps.cursorY) {
2017-09-21 05:05:17 +08:00
const cursorCoordinate = Cursor.getCursorCoordinates(
nextProps.cursorX,
nextProps.cursorY,
nextProps.slideWidth,
nextProps.slideHeight,
);
2017-08-30 04:58:48 +08:00
this.cursorCoordinate = cursorCoordinate;
}
}
2017-08-15 08:15:11 +08:00
// this function retrieves the text node, measures its BBox and sets the size for the outer box
calculateCursorLabelBoxDimensions() {
const {
setLabelBoxDimensions,
} = this.props;
2017-08-15 08:15:11 +08:00
let labelBoxWidth = 0;
let labelBoxHeight = 0;
if (this.cursorLabelRef) {
const { width, height } = this.cursorLabelRef.getBBox();
const { widthRatio, physicalWidthRatio, cursorLabelBox } = this.props;
2017-09-21 05:05:17 +08:00
labelBoxWidth = Cursor.invertScale(width, widthRatio, physicalWidthRatio);
labelBoxHeight = Cursor.invertScale(height, widthRatio, physicalWidthRatio);
2017-08-15 08:15:11 +08:00
// if the width of the text node is bigger than the maxSize - set the width to maxWidth
if (labelBoxWidth > cursorLabelBox.maxWidth) {
labelBoxWidth = cursorLabelBox.maxWidth;
2017-08-15 08:15:11 +08:00
}
}
// updating labelBoxWidth and labelBoxHeight in the container, which then passes it down here
setLabelBoxDimensions(labelBoxWidth, labelBoxHeight);
2017-08-15 08:15:11 +08:00
}
2016-07-16 04:45:54 +08:00
render() {
const {
cursorId,
userName,
isRTL,
} = this.props;
2016-07-16 04:45:54 +08:00
const {
2017-08-30 04:58:48 +08:00
cursorCoordinate,
fill,
} = this;
const {
cursorLabelBox,
cursorLabelText,
finalRadius,
} = this.scaledSizes;
const {
x,
y,
} = cursorCoordinate;
2017-08-30 04:58:48 +08:00
const boxX = x + cursorLabelBox.xOffset;
const boxY = y + cursorLabelBox.yOffset;
2016-07-16 04:45:54 +08:00
return (
2017-08-30 04:58:48 +08:00
<g
x={x}
y={y}
>
2017-08-15 08:15:11 +08:00
<circle
2017-08-30 04:58:48 +08:00
cx={x}
cy={y}
r={finalRadius === Infinity ? 0 : finalRadius}
2017-08-30 04:58:48 +08:00
fill={fill}
2017-08-15 08:15:11 +08:00
fillOpacity="0.6"
/>
{this.displayLabel
? (
<g>
2017-08-30 04:58:48 +08:00
<rect
fill="white"
fillOpacity="0.8"
2017-08-30 04:58:48 +08:00
x={boxX}
y={boxY}
width={cursorLabelBox.width}
height={cursorLabelBox.height}
strokeWidth={cursorLabelBox.strokeWidth}
stroke={fill}
strokeOpacity="0.8"
2017-08-30 04:58:48 +08:00
/>
<text
ref={(ref) => { this.cursorLabelRef = ref; }}
x={x}
y={y}
dy={cursorLabelText.textDY}
dx={cursorLabelText.textDX}
fontFamily="Arial"
fontWeight="600"
fill={fill}
fillOpacity="0.8"
fontSize={cursorLabelText.fontSize}
clipPath={`url(#${cursorId})`}
textAnchor={isRTL ? 'end' : 'start'}
>
{userName}
</text>
<clipPath id={cursorId}>
<rect
x={boxX}
y={boxY}
width={cursorLabelBox.width}
height={cursorLabelBox.height}
/>
</clipPath>
</g>
)
: null }
2017-08-15 08:15:11 +08:00
</g>
2016-07-16 04:45:54 +08:00
);
}
}
Cursor.propTypes = {
// ESLint can't detect where all these propTypes are used, and they are not planning to fix it
// so the next line disables eslint's no-unused-prop-types rule for this file.
/* eslint-disable react/no-unused-prop-types */
2017-08-30 04:58:48 +08:00
// Current presenter status
presenter: PropTypes.bool.isRequired,
// Current multi-user status
isMultiUser: PropTypes.bool.isRequired,
// Defines the id of the current cursor
cursorId: PropTypes.string.isRequired,
// Defines the user name for the cursor label
userName: PropTypes.string.isRequired,
// Defines the cursor x position
cursorX: PropTypes.number.isRequired,
// Defines the cursor y position
cursorY: PropTypes.number.isRequired,
// Slide to view box width ratio
widthRatio: PropTypes.number.isRequired,
// Slide physical size to original size ratio
physicalWidthRatio: PropTypes.number.isRequired,
// Slide width (svg)
slideWidth: PropTypes.number.isRequired,
// Slide height (svg)
slideHeight: PropTypes.number.isRequired,
/**
* Defines the cursor radius (not scaled)
* @defaultValue 5
*/
radius: PropTypes.number,
2017-08-15 08:15:11 +08:00
cursorLabelBox: PropTypes.shape({
labelBoxStrokeWidth: PropTypes.number.isRequired,
2017-08-30 04:58:48 +08:00
xOffset: PropTypes.number.isRequired,
yOffset: PropTypes.number.isRequired,
2017-08-15 08:15:11 +08:00
maxWidth: PropTypes.number.isRequired,
}),
cursorLabelText: PropTypes.shape({
textDY: PropTypes.number.isRequired,
textDX: PropTypes.number.isRequired,
fontSize: PropTypes.number.isRequired,
}),
2017-08-30 04:58:48 +08:00
// Defines the width of the label box
labelBoxWidth: PropTypes.number.isRequired,
// Defines the height of the label box
labelBoxHeight: PropTypes.number.isRequired,
2017-08-15 08:15:11 +08:00
// Defines the function, which sets the state for the label box and passes it back down
// we need it, since we need to render the text first -> measure its dimensions ->
// set proper width and height of the border box -> pass it down ->
// catch in the 'componentWillReceiveProps' -> apply new values
setLabelBoxDimensions: PropTypes.func.isRequired,
// Defines the direction the client text should be displayed
isRTL: PropTypes.bool.isRequired,
};
Cursor.defaultProps = {
radius: 5,
2017-08-15 08:15:11 +08:00
cursorLabelText: {
2017-08-30 04:58:48 +08:00
// text Y shift (10 points down)
2017-08-15 08:15:11 +08:00
textDY: 10,
2017-08-30 04:58:48 +08:00
// text X shift (10 points to the right)
textDX: 10,
// Initial label's font-size
2017-08-15 08:15:11 +08:00
fontSize: 12,
},
cursorLabelBox: {
2017-08-30 04:58:48 +08:00
// The thickness of the label box's border
2017-08-15 08:15:11 +08:00
labelBoxStrokeWidth: 1,
2017-08-30 04:58:48 +08:00
// X offset of the label box (8 points to the right)
xOffset: 8,
// Y offset of the label box (-2 points up)
yOffset: -2,
// Maximum width of the box, for the case when the user's name is too long
2017-08-15 08:15:11 +08:00
maxWidth: 65,
},
};