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';
|
2018-06-12 07:12:43 +08:00
|
|
|
import RenderInBrowser from 'react-render-in-browser';
|
2017-08-20 14:32:01 +08:00
|
|
|
import AnnotationHelpers from '../helpers';
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2017-09-22 07:44:32 +08:00
|
|
|
const DRAW_END = Meteor.settings.public.whiteboard.annotations.status.end;
|
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
export default class TextDrawComponent extends Component {
|
|
|
|
static getViewerStyles(results) {
|
2017-06-03 03:25:02 +08:00
|
|
|
const styles = {
|
2016-05-31 06:07:02 +08:00
|
|
|
WebkitTapHighlightColor: 'rgba(0, 0, 0, 0)',
|
2016-06-15 04:01:11 +08:00
|
|
|
pointerEvents: 'none',
|
|
|
|
fontStyle: 'normal',
|
|
|
|
fontVariant: 'normal',
|
|
|
|
fontWeight: 'normal',
|
|
|
|
fontStretch: 'normal',
|
|
|
|
lineHeight: 'normal',
|
|
|
|
fontFamily: 'Arial',
|
2017-05-13 03:43:06 +08:00
|
|
|
whiteSpace: 'pre-wrap',
|
2016-06-15 04:01:11 +08:00
|
|
|
wordWrap: 'break-word',
|
|
|
|
wordBreak: 'normal',
|
|
|
|
textAlign: 'left',
|
2016-06-23 06:17:31 +08:00
|
|
|
margin: 0,
|
2017-06-17 10:32:41 +08:00
|
|
|
|
|
|
|
// padding to match the border of the text area and the flash client's default 1px padding
|
|
|
|
padding: 1,
|
2016-06-15 04:01:11 +08:00
|
|
|
color: results.fontColor,
|
|
|
|
fontSize: results.calcedFontSize,
|
2016-05-31 06:07:02 +08:00
|
|
|
};
|
2016-06-15 04:01:11 +08:00
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
static getPresenterStyles(results) {
|
2017-06-11 10:21:37 +08:00
|
|
|
const styles = {
|
2017-06-17 10:32:41 +08:00
|
|
|
fontFamily: 'Arial',
|
2017-06-11 10:21:37 +08:00
|
|
|
border: '1px solid black',
|
2017-08-20 14:32:01 +08:00
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
2017-06-11 10:21:37 +08:00
|
|
|
resize: 'none',
|
|
|
|
overflow: 'hidden',
|
|
|
|
outline: 'none',
|
|
|
|
color: results.fontColor,
|
|
|
|
fontSize: results.calcedFontSize,
|
2017-08-20 14:32:01 +08:00
|
|
|
padding: '0',
|
2017-06-11 10:21:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return styles;
|
|
|
|
}
|
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.handleFocus = this.handleFocus.bind(this);
|
|
|
|
this.handleOnBlur = this.handleOnBlur.bind(this);
|
|
|
|
this.onChangeHandler = this.onChangeHandler.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-11-22 08:51:40 +08:00
|
|
|
// iOS doesn't show the keyboard if the input field was focused by event NOT invoked by a user
|
|
|
|
// by it still technically moves the focus there
|
|
|
|
// that's why we have a separate case for iOS - we don't focus here automatically
|
|
|
|
// but we focus on the next "tap" invoked by a user
|
|
|
|
const iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0;
|
2017-12-07 04:00:18 +08:00
|
|
|
const Android = navigator.userAgent.toLowerCase().indexOf('android') > -1;
|
2017-11-24 07:47:36 +08:00
|
|
|
|
|
|
|
// unsupported Firefox condition (not iOS though) can be removed when FF 59 is released
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=1409113
|
|
|
|
const unsupportedFirefox = navigator.userAgent.indexOf('Firefox/57') !== -1
|
|
|
|
|| navigator.userAgent.indexOf('Firefox/58') !== -1;
|
|
|
|
|
2017-12-06 09:22:24 +08:00
|
|
|
if (iOS || (Android && unsupportedFirefox)) { return; }
|
2017-11-22 08:51:40 +08:00
|
|
|
|
2017-09-22 07:44:32 +08:00
|
|
|
if (this.props.isActive && this.props.annotation.status !== DRAW_END) {
|
2017-08-20 14:32:01 +08:00
|
|
|
this.handleFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
return this.props.version !== nextProps.version ||
|
|
|
|
this.props.isActive !== nextProps.isActive;
|
|
|
|
}
|
|
|
|
|
2017-08-24 11:35:34 +08:00
|
|
|
// If the user is drawing a text shape and clicks Undo - reset textShapeId
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.props.isActive) {
|
|
|
|
this.props.resetTextShapeActiveId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
onChangeHandler(event) {
|
|
|
|
this.props.setTextShapeValue(event.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
getCoordinates() {
|
|
|
|
const { annotation, slideWidth, slideHeight } = this.props;
|
2017-09-06 09:36:15 +08:00
|
|
|
const {
|
2017-08-20 14:32:01 +08:00
|
|
|
x,
|
|
|
|
y,
|
2017-09-06 09:36:15 +08:00
|
|
|
textBoxWidth,
|
|
|
|
textBoxHeight,
|
2017-08-20 14:32:01 +08:00
|
|
|
fontColor,
|
2017-09-06 09:36:15 +08:00
|
|
|
fontSize,
|
2017-08-20 14:32:01 +08:00
|
|
|
calcedFontSize,
|
2017-09-06 09:36:15 +08:00
|
|
|
text,
|
|
|
|
} = annotation;
|
|
|
|
|
|
|
|
const _x = (x / 100) * slideWidth;
|
|
|
|
const _y = (y / 100) * slideHeight;
|
|
|
|
const _width = (textBoxWidth / 100) * slideWidth;
|
|
|
|
const _height = (textBoxHeight / 100) * slideHeight;
|
|
|
|
const _fontColor = AnnotationHelpers.getFormattedColor(fontColor);
|
|
|
|
const _fontSize = fontSize;
|
|
|
|
const _calcedFontSize = (calcedFontSize / 100) * slideHeight;
|
|
|
|
const _text = text;
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: _x,
|
|
|
|
y: _y,
|
|
|
|
text: _text,
|
|
|
|
width: _width,
|
|
|
|
height: _height,
|
|
|
|
fontSize: _fontSize,
|
|
|
|
fontColor: _fontColor,
|
|
|
|
calcedFontSize: _calcedFontSize,
|
2017-08-20 14:32:01 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOnBlur() {
|
|
|
|
// it'd be better to use ref to focus onBlur (handleFocus), but it doesn't want to work in FF
|
|
|
|
// so we are back to the old way of doing things, getElementById and setTimeout
|
|
|
|
const node = document.getElementById(this.props.annotation.id);
|
|
|
|
setTimeout(() => { node.focus(); }, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFocus() {
|
|
|
|
// Explicitly focus the text input using the raw DOM API
|
|
|
|
this.textArea.focus();
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:21:37 +08:00
|
|
|
renderViewerTextShape(results) {
|
2017-08-20 14:32:01 +08:00
|
|
|
const styles = TextDrawComponent.getViewerStyles(results);
|
2017-06-11 10:21:37 +08:00
|
|
|
|
2016-05-31 06:07:02 +08:00
|
|
|
return (
|
2017-06-07 08:14:36 +08:00
|
|
|
<g>
|
2018-06-12 07:12:43 +08:00
|
|
|
<RenderInBrowser only firefox>
|
|
|
|
<clipPath id={this.props.annotation.id}>
|
|
|
|
<rect
|
|
|
|
x={results.x}
|
|
|
|
y={results.y}
|
|
|
|
width={results.width}
|
|
|
|
height={results.height}
|
|
|
|
/>
|
|
|
|
</clipPath>
|
|
|
|
</RenderInBrowser>
|
2017-06-07 08:14:36 +08:00
|
|
|
<foreignObject
|
2017-08-17 10:24:59 +08:00
|
|
|
clipPath={`url(#${this.props.annotation.id})`}
|
2016-06-15 04:01:11 +08:00
|
|
|
x={results.x}
|
|
|
|
y={results.y}
|
|
|
|
width={results.width}
|
|
|
|
height={results.height}
|
2017-06-07 08:14:36 +08:00
|
|
|
>
|
|
|
|
<p style={styles}>
|
|
|
|
{results.text}
|
|
|
|
</p>
|
|
|
|
</foreignObject>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
}
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2017-06-11 10:21:37 +08:00
|
|
|
renderPresenterTextShape(results) {
|
2017-08-20 14:32:01 +08:00
|
|
|
const styles = TextDrawComponent.getPresenterStyles(results);
|
2017-06-11 10:21:37 +08:00
|
|
|
|
2017-06-07 08:14:36 +08:00
|
|
|
return (
|
2017-06-11 10:21:37 +08:00
|
|
|
<g>
|
|
|
|
<foreignObject
|
2017-06-17 10:32:41 +08:00
|
|
|
x={results.x}
|
|
|
|
y={results.y}
|
|
|
|
width={results.width}
|
|
|
|
height={results.height}
|
2017-08-20 14:32:01 +08:00
|
|
|
style={{ pointerEvents: 'none' }}
|
2017-06-11 10:21:37 +08:00
|
|
|
>
|
|
|
|
<textarea
|
2017-08-17 10:24:59 +08:00
|
|
|
id={this.props.annotation.id}
|
2017-06-11 10:21:37 +08:00
|
|
|
maxLength="1024"
|
|
|
|
ref={(ref) => { this.textArea = ref; }}
|
2017-08-20 14:32:01 +08:00
|
|
|
onChange={this.onChangeHandler}
|
2017-06-17 04:33:10 +08:00
|
|
|
onBlur={this.handleOnBlur}
|
2017-06-11 10:21:37 +08:00
|
|
|
style={styles}
|
2017-12-06 09:22:24 +08:00
|
|
|
spellCheck="false"
|
2017-06-11 10:21:37 +08:00
|
|
|
/>
|
|
|
|
</foreignObject>
|
|
|
|
</g>
|
2016-05-31 06:07:02 +08:00
|
|
|
);
|
|
|
|
}
|
2017-06-07 08:14:36 +08:00
|
|
|
|
|
|
|
render() {
|
2017-08-20 14:32:01 +08:00
|
|
|
const results = this.getCoordinates();
|
2017-06-07 08:14:36 +08:00
|
|
|
|
2017-09-22 07:44:32 +08:00
|
|
|
if (this.props.isActive && this.props.annotation.status !== DRAW_END) {
|
2017-06-11 10:21:37 +08:00
|
|
|
return this.renderPresenterTextShape(results);
|
2017-06-07 08:14:36 +08:00
|
|
|
}
|
2017-08-20 14:32:01 +08:00
|
|
|
return this.renderViewerTextShape(results);
|
2017-06-07 08:14:36 +08:00
|
|
|
}
|
2016-05-31 06:07:02 +08:00
|
|
|
}
|
2016-06-15 04:01:11 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
TextDrawComponent.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 text shape
|
|
|
|
annotation: PropTypes.shape({
|
|
|
|
calcedFontSize: PropTypes.number.isRequired,
|
|
|
|
fontColor: PropTypes.number.isRequired,
|
|
|
|
fontSize: PropTypes.number.isRequired,
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
status: PropTypes.string.isRequired,
|
|
|
|
text: PropTypes.string.isRequired,
|
|
|
|
textBoxWidth: PropTypes.number.isRequired,
|
|
|
|
textBoxHeight: PropTypes.number.isRequired,
|
|
|
|
x: PropTypes.number.isRequired,
|
|
|
|
y: 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,
|
|
|
|
// Defines a flag which indicates that this shape is currently used by a presenter to draw text
|
|
|
|
isActive: PropTypes.bool.isRequired,
|
|
|
|
// Defines a function that sends updates from the active text shape to the server
|
|
|
|
setTextShapeValue: PropTypes.func.isRequired,
|
2017-08-24 11:35:34 +08:00
|
|
|
// Defines a function that resets the textShape active Id in case if a user clicks Undo
|
|
|
|
// while drawing a shape
|
|
|
|
resetTextShapeActiveId: PropTypes.func.isRequired,
|
2016-06-15 04:01:11 +08:00
|
|
|
};
|