2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-07-16 04:45:54 +08:00
|
|
|
|
2017-07-29 08:29:40 +08:00
|
|
|
export default class Cursor extends Component {
|
2017-09-21 05:05:17 +08:00
|
|
|
static scale(attribute, widthRatio, physicalWidthRatio) {
|
2019-07-18 08:30:28 +08:00
|
|
|
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) {
|
2019-07-18 08:30:28 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
static getScaledSizes(props, state) {
|
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.
|
2019-07-18 08:30:28 +08:00
|
|
|
const scaleFactor = props.widthRatio / props.physicalWidthRatio;
|
2016-07-16 04:45:54 +08:00
|
|
|
|
2017-07-29 08:29:40 +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
|
2020-08-08 04:32:46 +08:00
|
|
|
width: (state.labelBoxWidth + 3) * scaleFactor,
|
|
|
|
height: (state.labelBoxHeight + 3) * scaleFactor,
|
2017-08-30 04:58:48 +08:00
|
|
|
strokeWidth: props.cursorLabelBox.labelBoxStrokeWidth * scaleFactor,
|
2017-08-15 08:15:11 +08:00
|
|
|
},
|
2017-07-29 08:29:40 +08:00
|
|
|
};
|
|
|
|
}
|
2016-07-16 04:45:54 +08:00
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
scaledSizes: null,
|
|
|
|
labelBoxWidth: 0,
|
|
|
|
labelBoxHeight: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setLabelBoxDimensions = this.setLabelBoxDimensions.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-04-04 01:45:53 +08:00
|
|
|
const {
|
2019-07-18 08:30:28 +08:00
|
|
|
cursorX,
|
|
|
|
cursorY,
|
|
|
|
slideWidth,
|
|
|
|
slideHeight,
|
|
|
|
presenter,
|
|
|
|
isMultiUser,
|
2019-04-04 01:45:53 +08:00
|
|
|
} = this.props;
|
2017-09-21 05:34:22 +08:00
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
this.setState({
|
|
|
|
scaledSizes: Cursor.getScaledSizes(this.props, this.state),
|
|
|
|
});
|
2017-09-21 05:34:22 +08:00
|
|
|
this.cursorCoordinate = Cursor.getCursorCoordinates(
|
2017-09-21 05:05:17 +08:00
|
|
|
cursorX,
|
|
|
|
cursorY,
|
|
|
|
slideWidth,
|
|
|
|
slideHeight,
|
|
|
|
);
|
2017-08-30 04:58:48 +08:00
|
|
|
|
2017-09-21 05:34:22 +08:00
|
|
|
const { fill, displayLabel } = Cursor.getFillAndLabel(presenter, isMultiUser);
|
2017-08-30 04:58:48 +08:00
|
|
|
this.fill = fill;
|
|
|
|
this.displayLabel = displayLabel;
|
2020-07-16 03:52:34 +08:00
|
|
|
// we need to find the BBox of the text, so that we could set a proper border box arount it
|
|
|
|
}
|
2020-04-27 22:34:41 +08:00
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
const {
|
|
|
|
scaledSizes,
|
|
|
|
} = this.state;
|
|
|
|
if (!prevState.scaledSizes && scaledSizes) {
|
|
|
|
this.calculateCursorLabelBoxDimensions();
|
|
|
|
}
|
|
|
|
|
2017-08-30 04:58:48 +08:00
|
|
|
const {
|
|
|
|
presenter,
|
|
|
|
isMultiUser,
|
|
|
|
widthRatio,
|
|
|
|
physicalWidthRatio,
|
|
|
|
cursorX,
|
|
|
|
cursorY,
|
2020-08-08 04:32:46 +08:00
|
|
|
slideWidth,
|
|
|
|
slideHeight,
|
2017-08-30 04:58:48 +08:00
|
|
|
} = this.props;
|
2020-08-08 04:32:46 +08:00
|
|
|
const {
|
|
|
|
labelBoxWidth,
|
|
|
|
labelBoxHeight,
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
const {
|
|
|
|
labelBoxWidth: prevLabelBoxWidth,
|
|
|
|
labelBoxHeight: prevLabelBoxHeight,
|
|
|
|
} = prevState;
|
2017-03-23 07:24:36 +08:00
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
if (presenter !== prevProps.presenter || isMultiUser !== prevProps.isMultiUser) {
|
2017-09-21 05:05:17 +08:00
|
|
|
const { fill, displayLabel } = Cursor.getFillAndLabel(
|
2020-08-08 04:32:46 +08:00
|
|
|
presenter,
|
|
|
|
isMultiUser,
|
2017-09-21 05:05:17 +08:00
|
|
|
);
|
2017-08-30 04:58:48 +08:00
|
|
|
this.displayLabel = displayLabel;
|
|
|
|
this.fill = fill;
|
|
|
|
}
|
2017-03-23 07:24:36 +08:00
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
if ((widthRatio !== prevProps.widthRatio
|
|
|
|
|| physicalWidthRatio !== prevProps.physicalWidthRatio)
|
|
|
|
|| (labelBoxWidth !== prevLabelBoxWidth
|
|
|
|
|| labelBoxHeight !== prevLabelBoxHeight)) {
|
|
|
|
this.setState({
|
|
|
|
scaledSizes: Cursor.getScaledSizes(this.props, this.state),
|
|
|
|
});
|
2017-08-30 04:58:48 +08:00
|
|
|
}
|
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
if (cursorX !== prevProps.cursorX || cursorY !== prevProps.cursorY) {
|
2017-09-21 05:05:17 +08:00
|
|
|
const cursorCoordinate = Cursor.getCursorCoordinates(
|
2020-08-08 04:32:46 +08:00
|
|
|
cursorX,
|
|
|
|
cursorY,
|
|
|
|
slideWidth,
|
|
|
|
slideHeight,
|
2017-09-21 05:05:17 +08:00
|
|
|
);
|
2017-08-30 04:58:48 +08:00
|
|
|
this.cursorCoordinate = cursorCoordinate;
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 08:15:11 +08:00
|
|
|
|
2020-08-08 04:32:46 +08:00
|
|
|
setLabelBoxDimensions(labelBoxWidth, labelBoxHeight) {
|
|
|
|
this.setState({
|
|
|
|
labelBoxWidth,
|
|
|
|
labelBoxHeight,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
let labelBoxWidth = 0;
|
|
|
|
let labelBoxHeight = 0;
|
2020-08-08 04:32:46 +08:00
|
|
|
|
2017-08-21 08:16:39 +08:00
|
|
|
if (this.cursorLabelRef) {
|
|
|
|
const { width, height } = this.cursorLabelRef.getBBox();
|
2019-06-25 23:04:45 +08:00
|
|
|
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
|
2019-07-18 08:30:28 +08:00
|
|
|
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
|
2020-08-08 04:32:46 +08:00
|
|
|
this.setLabelBoxDimensions(labelBoxWidth, labelBoxHeight);
|
2017-08-15 08:15:11 +08:00
|
|
|
}
|
|
|
|
|
2016-07-16 04:45:54 +08:00
|
|
|
render() {
|
2020-08-08 04:32:46 +08:00
|
|
|
const {
|
|
|
|
scaledSizes,
|
|
|
|
} = this.state;
|
2019-07-18 08:30:28 +08:00
|
|
|
const {
|
|
|
|
cursorId,
|
|
|
|
userName,
|
2019-07-26 07:29:52 +08:00
|
|
|
isRTL,
|
2019-07-18 08:30:28 +08:00
|
|
|
} = this.props;
|
2020-08-08 04:32:46 +08:00
|
|
|
|
2016-07-16 04:45:54 +08:00
|
|
|
const {
|
2017-08-30 04:58:48 +08:00
|
|
|
cursorCoordinate,
|
|
|
|
fill,
|
|
|
|
} = this;
|
2020-08-08 04:32:46 +08:00
|
|
|
|
|
|
|
if (!scaledSizes) return null;
|
2019-07-18 08:30:28 +08:00
|
|
|
const {
|
|
|
|
cursorLabelBox,
|
|
|
|
cursorLabelText,
|
|
|
|
finalRadius,
|
2020-08-08 04:32:46 +08:00
|
|
|
} = scaledSizes;
|
2019-07-18 08:30:28 +08:00
|
|
|
|
|
|
|
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}
|
2020-08-08 04:32:46 +08:00
|
|
|
r={finalRadius}
|
2017-08-30 04:58:48 +08:00
|
|
|
fill={fill}
|
2017-08-15 08:15:11 +08:00
|
|
|
fillOpacity="0.6"
|
2017-04-28 06:21:04 +08:00
|
|
|
/>
|
2019-04-04 01:45:53 +08:00
|
|
|
{this.displayLabel
|
|
|
|
? (
|
|
|
|
<g>
|
2017-08-30 04:58:48 +08:00
|
|
|
<rect
|
2019-04-04 01:45:53 +08:00
|
|
|
fill="white"
|
|
|
|
fillOpacity="0.8"
|
2017-08-30 04:58:48 +08:00
|
|
|
x={boxX}
|
|
|
|
y={boxY}
|
|
|
|
width={cursorLabelBox.width}
|
|
|
|
height={cursorLabelBox.height}
|
2019-04-04 01:45:53 +08:00
|
|
|
strokeWidth={cursorLabelBox.strokeWidth}
|
|
|
|
stroke={fill}
|
|
|
|
strokeOpacity="0.8"
|
2017-08-30 04:58:48 +08:00
|
|
|
/>
|
2019-04-04 01:45:53 +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}
|
2019-07-18 08:30:28 +08:00
|
|
|
clipPath={`url(#${cursorId})`}
|
2019-06-25 23:04:45 +08:00
|
|
|
textAnchor={isRTL ? 'end' : 'start'}
|
2019-04-04 01:45:53 +08:00
|
|
|
>
|
2019-07-18 08:30:28 +08:00
|
|
|
{userName}
|
2019-04-04 01:45:53 +08:00
|
|
|
</text>
|
2019-07-18 08:30:28 +08:00
|
|
|
<clipPath id={cursorId}>
|
2019-04-04 01:45:53 +08:00
|
|
|
<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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 08:29:40 +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,
|
|
|
|
|
2017-07-29 08:29:40 +08:00
|
|
|
// 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,
|
2017-08-21 08:16:39 +08:00
|
|
|
|
2017-09-22 07:44:32 +08:00
|
|
|
// Slide width (svg)
|
|
|
|
slideWidth: PropTypes.number.isRequired,
|
|
|
|
// Slide height (svg)
|
|
|
|
slideHeight: PropTypes.number.isRequired,
|
|
|
|
|
2017-07-29 08:29:40 +08:00
|
|
|
/**
|
2017-08-21 08:16:39 +08:00
|
|
|
* Defines the cursor radius (not scaled)
|
2017-07-29 08:29:40 +08:00
|
|
|
* @defaultValue 5
|
|
|
|
*/
|
2019-07-18 08:30:28 +08:00
|
|
|
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,
|
|
|
|
}),
|
|
|
|
|
2019-06-25 23:04:45 +08:00
|
|
|
// Defines the direction the client text should be displayed
|
|
|
|
isRTL: PropTypes.bool.isRequired,
|
2017-07-29 08:29:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
2017-07-29 08:29:40 +08:00
|
|
|
};
|