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 PencilDrawComponent extends Component {
|
2017-08-23 04:20:12 +08:00
|
|
|
static getInitialCoordinates(annotation, slideWidth, slideHeight) {
|
|
|
|
const { points } = annotation;
|
2016-06-09 01:12:24 +08:00
|
|
|
let i = 2;
|
|
|
|
let path = '';
|
|
|
|
if (points && points.length >= 2) {
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `M${denormalizeCoord(points[0], slideWidth)
|
|
|
|
}, ${denormalizeCoord(points[1], slideHeight)}`;
|
2016-06-09 01:12:24 +08:00
|
|
|
while (i < points.length) {
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `${path} L${denormalizeCoord(points[i], slideWidth)
|
|
|
|
}, ${denormalizeCoord(points[i + 1], slideHeight)}`;
|
2016-06-09 01:12:24 +08:00
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 04:20:12 +08:00
|
|
|
|
|
|
|
return { path, points };
|
|
|
|
}
|
|
|
|
|
|
|
|
static getFinalCoordinates(annotation, slideWidth, slideHeight) {
|
|
|
|
const { points, commands } = annotation;
|
|
|
|
|
|
|
|
let path = '';
|
|
|
|
let i;
|
|
|
|
let j;
|
|
|
|
for (i = 0, j = 0; i < commands.length; i += 1) {
|
|
|
|
switch (commands[i]) {
|
2018-01-23 07:07:29 +08:00
|
|
|
// MOVE_TO - consumes 1 pair of values
|
2017-08-23 04:20:12 +08:00
|
|
|
case 1:
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `${path} M${denormalizeCoord(points[j], slideWidth)} ${denormalizeCoord(points[j + 1], slideHeight)}`;
|
2017-08-23 04:20:12 +08:00
|
|
|
j += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// LINE_TO - consumes 1 pair of values
|
|
|
|
case 2:
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `${path} L${denormalizeCoord(points[j], slideWidth)} ${denormalizeCoord(points[j + 1], slideHeight)}`;
|
2017-08-23 04:20:12 +08:00
|
|
|
j += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// QUADRATIC_CURVE_TO - consumes 2 pairs of values
|
|
|
|
// 1st pair is a control point, second is a coordinate
|
|
|
|
case 3:
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `${path} Q${denormalizeCoord(points[j], slideWidth)}, ${
|
|
|
|
denormalizeCoord(points[j + 1], slideHeight)}, ${denormalizeCoord(points[j + 2], slideWidth)}, ${
|
|
|
|
denormalizeCoord(points[j + 3], slideHeight)}`;
|
2017-08-23 04:20:12 +08:00
|
|
|
j += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// CUBIC_CURVE_TO - consumes 3 pairs of values
|
|
|
|
// 1st and 2nd are control points, 3rd is an end coordinate
|
|
|
|
case 4:
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `${path} C${denormalizeCoord(points[j], slideWidth)}, ${
|
|
|
|
denormalizeCoord(points[j + 1], slideHeight)}, ${denormalizeCoord(points[j + 2], slideWidth)}, ${
|
|
|
|
denormalizeCoord(points[j + 3], slideHeight)}, ${denormalizeCoord(points[j + 4], slideWidth)}, ${
|
|
|
|
denormalizeCoord(points[j + 5], slideHeight)}`;
|
2017-08-23 04:20:12 +08:00
|
|
|
j += 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:45:47 +08:00
|
|
|
// If that's just one coordinate at the end (dot) - we want to display it.
|
|
|
|
// So adding L with the same X and Y values to the path
|
|
|
|
if (path && points.length === 2) {
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `${path} L${denormalizeCoord(points[0], slideWidth)} ${denormalizeCoord(points[1], slideHeight)}`;
|
2017-08-31 04:45:47 +08:00
|
|
|
}
|
|
|
|
|
2017-08-23 04:20:12 +08:00
|
|
|
return { path, points };
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
const { annotation, slideWidth, slideHeight } = this.props;
|
|
|
|
|
|
|
|
this.path = this.getCoordinates(annotation, slideWidth, slideHeight);
|
|
|
|
|
|
|
|
this.getCurrentPath = this.getCurrentPath.bind(this);
|
|
|
|
this.getCoordinates = this.getCoordinates.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps) {
|
2020-04-07 04:34:08 +08:00
|
|
|
const { version } = this.props;
|
|
|
|
return version !== nextProps.version;
|
2017-08-23 04:20:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-16 03:52:34 +08:00
|
|
|
componentWillUpdate(nextProps) {
|
|
|
|
const { annotation: nextAnnotation, slideWidth, slideHeight } = nextProps;
|
|
|
|
const { points: nextPoints } = nextAnnotation;
|
|
|
|
const { annotation } = this.props;
|
2020-04-07 04:34:08 +08:00
|
|
|
const { points } = annotation;
|
2020-07-16 03:52:34 +08:00
|
|
|
if (nextPoints.length !== points.length) {
|
|
|
|
this.path = this.getCoordinates(nextAnnotation, slideWidth, slideHeight);
|
2017-08-23 04:20:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCoordinates(annotation, slideWidth, slideHeight) {
|
2018-02-13 06:59:05 +08:00
|
|
|
if ((!annotation || annotation.points.length === 0)
|
|
|
|
|| (annotation.status === 'DRAW_END' && !annotation.commands)) {
|
2018-01-23 07:07:29 +08:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2017-09-06 09:36:15 +08:00
|
|
|
let data;
|
2017-08-30 04:58:48 +08:00
|
|
|
// Final message, display smoothes coordinates
|
2017-08-23 04:20:12 +08:00
|
|
|
if (annotation.status === 'DRAW_END') {
|
2017-09-06 09:36:15 +08:00
|
|
|
data = PencilDrawComponent.getFinalCoordinates(annotation, slideWidth, slideHeight);
|
2017-08-30 04:58:48 +08:00
|
|
|
// Not a final message, but rendering it for the first time, creating a new path
|
2017-08-23 04:20:12 +08:00
|
|
|
} else if (!this.path) {
|
2017-09-06 09:36:15 +08:00
|
|
|
data = PencilDrawComponent.getInitialCoordinates(annotation, slideWidth, slideHeight);
|
|
|
|
// If it's not the first 2 cases - means we just got an update, updating the coordinates
|
|
|
|
} else {
|
|
|
|
data = this.updateCoordinates(annotation, slideWidth, slideHeight);
|
2017-08-23 04:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.points = data.points;
|
|
|
|
return data.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentPath() {
|
|
|
|
return this.path ? this.path : 'M -1 -1';
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCoordinates(annotation, slideWidth, slideHeight) {
|
|
|
|
const { points } = annotation;
|
2020-04-07 04:34:08 +08:00
|
|
|
|
2017-08-23 04:20:12 +08:00
|
|
|
let i = this.points.length;
|
|
|
|
let path = '';
|
|
|
|
while (i < points.length) {
|
2020-04-07 04:34:08 +08:00
|
|
|
path = `${path} L${denormalizeCoord(points[i], slideWidth)
|
|
|
|
}, ${denormalizeCoord(points[i + 1], slideHeight)}`;
|
2017-08-23 04:20:12 +08:00
|
|
|
i += 2;
|
|
|
|
}
|
2020-04-07 04:34:08 +08:00
|
|
|
|
2017-08-23 04:20:12 +08:00
|
|
|
path = this.path + path;
|
|
|
|
|
|
|
|
return { path, points };
|
2016-06-09 01:12:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:07:02 +08:00
|
|
|
render() {
|
2017-08-20 14:32:01 +08:00
|
|
|
const { annotation, slideWidth } = this.props;
|
2016-05-31 06:07:02 +08:00
|
|
|
return (
|
|
|
|
<path
|
2016-06-09 01:12:24 +08:00
|
|
|
fill="none"
|
2020-04-07 04:34:08 +08:00
|
|
|
stroke={getFormattedColor(annotation.color)}
|
2017-08-23 04:20:12 +08:00
|
|
|
d={this.getCurrentPath()}
|
2020-04-07 04:34:08 +08:00
|
|
|
strokeWidth={getStrokeWidth(annotation.thickness, slideWidth)}
|
2016-06-09 01:12:24 +08:00
|
|
|
strokeLinejoin="round"
|
|
|
|
strokeLinecap="round"
|
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="pencilDraw"
|
2017-06-03 03:25:02 +08:00
|
|
|
/>
|
2016-05-31 06:07:02 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 01:12:24 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
PencilDrawComponent.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 with a pencil
|
|
|
|
annotation: PropTypes.shape({
|
|
|
|
points: PropTypes.arrayOf(PropTypes.number).isRequired,
|
|
|
|
color: PropTypes.number.isRequired,
|
|
|
|
thickness: PropTypes.number.isRequired,
|
|
|
|
}).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-09 01:12:24 +08:00
|
|
|
};
|