2017-08-20 14:32:01 +08:00
|
|
|
import React, { Component } from 'react';
|
2017-06-04 10:40:14 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2020-04-07 04:34:08 +08:00
|
|
|
import { getFormattedColor, getStrokeWidth, denormalizeCoord } from '../helpers';
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
export default class RectangleDrawComponent extends Component {
|
|
|
|
shouldComponentUpdate(nextProps) {
|
2020-04-07 04:34:08 +08:00
|
|
|
const { version } = this.props;
|
|
|
|
return version !== nextProps.version;
|
2017-05-10 04:34:46 +08:00
|
|
|
}
|
|
|
|
|
2016-06-10 02:58:41 +08:00
|
|
|
getCoordinates() {
|
2020-04-07 04:34:08 +08:00
|
|
|
const { slideWidth, slideHeight, annotation } = this.props;
|
|
|
|
const { points } = annotation;
|
|
|
|
/* eslint-disable prefer-destructuring */
|
2017-07-27 20:35:55 +08:00
|
|
|
// x1 and y1 are the coordinates of the top left corner of the annotation
|
|
|
|
// x2 and y2 are the coordinates of the bottom right corner of the annotation
|
2017-08-20 14:32:01 +08:00
|
|
|
let x1 = points[0];
|
|
|
|
let y1 = points[1];
|
|
|
|
let x2 = points[2];
|
|
|
|
let y2 = points[3];
|
2016-06-10 02:58:41 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// Presenter pulled rectangle to the left
|
2016-06-10 02:58:41 +08:00
|
|
|
if (x2 < x1) {
|
2017-08-20 14:32:01 +08:00
|
|
|
x1 = points[2];
|
|
|
|
x2 = points[0];
|
2016-06-10 02:58:41 +08:00
|
|
|
}
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// Presenter pulled Rectangle to the top
|
2016-06-10 02:58:41 +08:00
|
|
|
if (y2 < y1) {
|
2017-08-20 14:32:01 +08:00
|
|
|
y1 = points[3];
|
|
|
|
y2 = points[1];
|
2016-06-10 02:58:41 +08:00
|
|
|
}
|
2020-04-07 04:34:08 +08:00
|
|
|
/* eslint-enable prefer-destructuring */
|
|
|
|
const x = denormalizeCoord(x1, slideWidth);
|
|
|
|
const y = denormalizeCoord(y1, slideHeight);
|
|
|
|
const width = denormalizeCoord((x2 - x1), slideWidth);
|
|
|
|
const height = denormalizeCoord((y2 - y1), slideHeight);
|
2016-06-10 02:58:41 +08:00
|
|
|
|
|
|
|
return {
|
2017-06-03 03:25:02 +08:00
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
2016-05-31 06:07:02 +08:00
|
|
|
};
|
2016-06-10 02:58:41 +08:00
|
|
|
}
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2016-06-10 02:58:41 +08:00
|
|
|
render() {
|
2017-06-03 03:25:02 +08:00
|
|
|
const results = this.getCoordinates();
|
2017-08-20 14:32:01 +08:00
|
|
|
const { annotation, slideWidth } = this.props;
|
2020-10-28 13:38:44 +08:00
|
|
|
const { fill } = annotation;
|
2017-08-20 14:32:01 +08:00
|
|
|
|
2016-06-10 02:58:41 +08:00
|
|
|
return (
|
2016-05-31 06:07:02 +08:00
|
|
|
<rect
|
2016-06-10 02:58:41 +08:00
|
|
|
x={results.x}
|
|
|
|
y={results.y}
|
|
|
|
width={results.width}
|
|
|
|
height={results.height}
|
2020-10-28 13:38:44 +08:00
|
|
|
fill={ fill ? getFormattedColor(annotation.color) : "none" }
|
2020-04-07 04:34:08 +08:00
|
|
|
stroke={getFormattedColor(annotation.color)}
|
|
|
|
strokeWidth={getStrokeWidth(annotation.thickness, slideWidth)}
|
2017-08-20 14:32:01 +08:00
|
|
|
style={{ WebkitTapHighlightColor: 'rgba(0, 0, 0, 0)' }}
|
2020-10-27 23:54:55 +08:00
|
|
|
data-test="drawnRectangle"
|
2017-06-03 03:25:02 +08:00
|
|
|
/>
|
2016-05-31 06:07:02 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-06-10 02:58:41 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
RectangleDrawComponent.propTypes = {
|
|
|
|
// Defines a version of the shape, so that we know if we need to update the component or not
|
|
|
|
version: PropTypes.number.isRequired,
|
|
|
|
// Defines an annotation object, which contains all the basic info we need to draw a rectangle
|
|
|
|
annotation: PropTypes.shape({
|
|
|
|
points: PropTypes.arrayOf(PropTypes.number).isRequired,
|
|
|
|
color: PropTypes.number.isRequired,
|
|
|
|
thickness: PropTypes.number.isRequired,
|
2020-10-28 13:38:44 +08:00
|
|
|
fill: PropTypes.bool.isRequired,
|
2017-08-20 14:32:01 +08:00
|
|
|
}).isRequired,
|
|
|
|
// Defines the width of the slide (svg coordinate system), which needed in calculations
|
|
|
|
slideWidth: PropTypes.number.isRequired,
|
|
|
|
// Defines the height of the slide (svg coordinate system), which needed in calculations
|
|
|
|
slideHeight: PropTypes.number.isRequired,
|
2016-06-10 02:58:41 +08:00
|
|
|
};
|