2017-03-01 02:35:56 +08:00
|
|
|
import React, { PropTypes } from 'react';
|
2017-05-03 00:04:05 +08:00
|
|
|
import styles from './styles.scss';
|
2017-05-02 05:34:24 +08:00
|
|
|
import { findDOMNode } from 'react-dom';
|
2017-05-03 00:04:05 +08:00
|
|
|
import cx from 'classnames';
|
2017-02-23 08:10:30 +08:00
|
|
|
|
2017-03-01 02:35:56 +08:00
|
|
|
export default class WhiteboardOverlay extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
2017-05-02 05:34:24 +08:00
|
|
|
//initial coordinates for "DRAW_START" message
|
|
|
|
initialCoordinates: {
|
|
|
|
x: undefined,
|
|
|
|
y: undefined,
|
|
|
|
},
|
2017-03-01 02:35:56 +08:00
|
|
|
|
2017-05-02 05:34:24 +08:00
|
|
|
currentShapeId: undefined,
|
|
|
|
count: 0,
|
2017-03-01 02:35:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.mouseDownHandler = this.mouseDownHandler.bind(this);
|
2017-05-02 05:34:24 +08:00
|
|
|
this.mouseMoveHandler = this.mouseMoveHandler.bind(this);
|
|
|
|
this.mouseUpHandler = this.mouseUpHandler.bind(this);
|
2017-03-01 02:35:56 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 05:34:24 +08:00
|
|
|
//main mouse down handler
|
|
|
|
//calls a mouseDown<AnnotationName> handler based on the tool selected
|
2017-03-01 02:35:56 +08:00
|
|
|
mouseDownHandler(event) {
|
2017-05-03 08:05:41 +08:00
|
|
|
if(this.props.drawSettings.tool) {
|
|
|
|
window.addEventListener('mouseup', this.mouseUpHandler);
|
|
|
|
window.addEventListener('mousemove', this.mouseMoveHandler, true);
|
|
|
|
this["mouseDown" + this.props.drawSettings.tool](event);
|
|
|
|
}
|
2017-05-02 05:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//main mouse up handler
|
|
|
|
//calls a mouseUp<AnnotationName> handler based on the tool selected
|
|
|
|
mouseUpHandler(event) {
|
|
|
|
window.removeEventListener('mouseup', this.mouseUpHandler);
|
|
|
|
window.removeEventListener('mousemove', this.mouseMoveHandler, true);
|
2017-05-03 08:05:41 +08:00
|
|
|
this["mouseUp" + this.props.drawSettings.tool](event);
|
2017-05-02 05:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//main mouse move handler
|
|
|
|
//calls a mouseMove<AnnotationName> handler based on the tool selected
|
|
|
|
mouseMoveHandler(event) {
|
2017-05-03 08:05:41 +08:00
|
|
|
this["mouseMove" + this.props.drawSettings.tool](event);
|
2017-05-02 05:34:24 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 22:33:16 +08:00
|
|
|
mouseDownHand(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-02 05:34:24 +08:00
|
|
|
mouseDownLine(event) {
|
|
|
|
this.commonMouseDown(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseDownEllipse(event) {
|
|
|
|
this.commonMouseDown(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseDownTriangle(event) {
|
|
|
|
this.commonMouseDown(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseDownRectangle(event) {
|
|
|
|
this.commonMouseDown(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseDownPencil(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseDownText(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Line / Ellipse / Rectangle / Triangle have the same actions on mouseDown
|
|
|
|
//so we just redirect their mouseDowns here
|
|
|
|
commonMouseDown(event) {
|
|
|
|
let x = (event.nativeEvent.offsetX + this.props.viewBoxX) / this.props.slideWidth * 100;
|
|
|
|
let y = (event.nativeEvent.offsetY + this.props.viewBoxY) / this.props.slideHeight * 100;
|
|
|
|
let id = (this.state.count + 1) + "-" + new Date().getTime();
|
2017-05-04 22:33:16 +08:00
|
|
|
this["handleDraw" + this.props.drawSettings.tool]({x: x, y: y}, {x: x, y: y}, "DRAW_START", id);
|
2017-05-02 05:34:24 +08:00
|
|
|
this.setState({
|
|
|
|
initialCoordinates: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
},
|
|
|
|
count: this.state.count + 1,
|
|
|
|
currentShapeId: id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-04 22:33:16 +08:00
|
|
|
mouseMoveHand(event){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-02 05:34:24 +08:00
|
|
|
mouseMoveLine(event) {
|
|
|
|
this.commonMouseMove(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseMoveEllipse(event) {
|
|
|
|
this.commonMouseMove(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseMoveTriangle(event) {
|
|
|
|
this.commonMouseMove(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseMoveRectangle(event) {
|
|
|
|
this.commonMouseMove(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseMovePencil(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseMoveText(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Line / Ellipse / Rectangle / Triangle have the same actions on mouseMove
|
|
|
|
//so we just redirect their mouseMoves here
|
|
|
|
commonMouseMove(event) {
|
|
|
|
const svggroup = this.props.getSvgRef();
|
|
|
|
var svgObject = findDOMNode(svggroup);
|
|
|
|
var svgPoint = svgObject.createSVGPoint();
|
|
|
|
svgPoint.x = event.clientX;
|
|
|
|
svgPoint.y = event.clientY;
|
|
|
|
let transformedSvgPoint = this.coordinateTransform(svgPoint, svgObject);
|
|
|
|
transformedSvgPoint.x = transformedSvgPoint.x / this.props.slideWidth * 100;
|
|
|
|
transformedSvgPoint.y = transformedSvgPoint.y / this.props.slideHeight * 100;
|
|
|
|
|
2017-05-03 08:05:41 +08:00
|
|
|
this["handleDraw" + this.props.drawSettings.tool](this.state.initialCoordinates, transformedSvgPoint, "DRAW_UPDATE", this.state.currentShapeId);
|
2017-05-02 05:34:24 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 22:33:16 +08:00
|
|
|
mouseUpHand(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-02 05:34:24 +08:00
|
|
|
mouseUpLine(event) {
|
|
|
|
this.commonMouseUp(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseUpEllipse(event) {
|
|
|
|
this.commonMouseUp(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseUpTriangle(event) {
|
|
|
|
this.commonMouseUp(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseUpRectangle(event) {
|
|
|
|
this.commonMouseUp(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseUpPencil(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mouseUpText(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Line / Ellipse / Rectangle / Triangle have the same actions on mouseUp
|
|
|
|
//so we just redirect their mouseUps here
|
|
|
|
commonMouseUp(event) {
|
|
|
|
const svggroup = this.props.getSvgRef();
|
|
|
|
var svgObject = findDOMNode(svggroup);
|
|
|
|
var svgPoint = svgObject.createSVGPoint();
|
|
|
|
svgPoint.x = event.clientX;
|
|
|
|
svgPoint.y = event.clientY;
|
|
|
|
let transformedSvgPoint = this.coordinateTransform(svgPoint, svgObject);
|
|
|
|
transformedSvgPoint.x = transformedSvgPoint.x / this.props.slideWidth * 100;
|
|
|
|
transformedSvgPoint.y = transformedSvgPoint.y / this.props.slideHeight * 100;
|
|
|
|
|
2017-05-03 08:05:41 +08:00
|
|
|
this["handleDraw" + this.props.drawSettings.tool](this.state.initialCoordinates, transformedSvgPoint, "DRAW_END", this.state.currentShapeId);
|
2017-05-02 05:34:24 +08:00
|
|
|
this.setState({
|
|
|
|
initialCoordinates: {
|
|
|
|
x: undefined,
|
|
|
|
y: undefined,
|
|
|
|
},
|
|
|
|
currentShapeId: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDrawRectangle(startPoint, endPoint, status, id) {
|
|
|
|
this.handleDrawCommonAnnotation(startPoint, endPoint, status, id, "rectangle");
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDrawEllipse(startPoint, endPoint, status, id) {
|
|
|
|
this.handleDrawCommonAnnotation(startPoint, endPoint, status, id, "ellipse");
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDrawTriangle(startPoint, endPoint, status, id) {
|
|
|
|
this.handleDrawCommonAnnotation(startPoint, endPoint, status, id, "triangle");
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDrawLine(startPoint, endPoint, status, id) {
|
|
|
|
this.handleDrawCommonAnnotation(startPoint, endPoint, status, id, "line");
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDrawText() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDrawPencil() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//since Rectangle / Triangle / Ellipse / Line have the same coordinate structure
|
|
|
|
//we use the same function for all of them
|
|
|
|
handleDrawCommonAnnotation(startPoint, endPoint, status, id, shapeType) {
|
|
|
|
let shape = {
|
|
|
|
annotation: {
|
|
|
|
type: shapeType,
|
|
|
|
points: [
|
|
|
|
startPoint.x,
|
|
|
|
startPoint.y,
|
|
|
|
endPoint.x,
|
|
|
|
endPoint.y,
|
|
|
|
],
|
2017-05-04 22:33:16 +08:00
|
|
|
color: this.props.drawSettings.color,
|
2017-05-02 05:34:24 +08:00
|
|
|
transparency: false,
|
|
|
|
status: status,
|
2017-05-04 22:33:16 +08:00
|
|
|
thickness: this.props.drawSettings.thickness,
|
2017-05-02 05:34:24 +08:00
|
|
|
status: status,
|
|
|
|
id: id,
|
|
|
|
whiteboardId: this.props.whiteboardId,
|
|
|
|
},
|
|
|
|
whiteboard_id: this.props.whiteboardId,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.props.sendAnnotation(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
//a function to transform a screen point to svg point
|
|
|
|
//accepts and returns a point of type SvgPoint and an svg object
|
|
|
|
coordinateTransform(screenPoint, someSvgObject) {
|
|
|
|
var CTM = someSvgObject.getScreenCTM();
|
|
|
|
return screenPoint.matrixTransform(CTM.inverse());
|
2017-03-01 02:35:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-05-03 08:05:41 +08:00
|
|
|
let tool = this.props.drawSettings.tool;
|
2017-03-01 02:35:56 +08:00
|
|
|
return (
|
2017-05-03 00:04:05 +08:00
|
|
|
<div
|
|
|
|
className={
|
|
|
|
cx(
|
2017-05-03 08:05:41 +08:00
|
|
|
tool == "Pencil" ? styles.pencil : '',
|
|
|
|
tool == "Triangle" ? styles.triangle : '',
|
|
|
|
tool == "Rectangle" ? styles.rectangle : '',
|
|
|
|
tool == "Ellipse" ? styles.ellipse : '',
|
|
|
|
tool == "Line" ? styles.line : '',
|
|
|
|
tool == "Text" ? styles.text : ''
|
2017-05-03 00:04:05 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
style={{ width: '100%', height: '100%' }}
|
|
|
|
onMouseDown={this.mouseDownHandler}
|
|
|
|
/>
|
2017-03-01 02:35:56 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|