2022-03-25 03:05:20 +08:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-04-13 23:00:45 +08:00
|
|
|
import UserContainer from './user/container';
|
2022-03-25 03:05:20 +08:00
|
|
|
|
|
|
|
const CAPTIONS_CONFIG = Meteor.settings.public.captions;
|
|
|
|
|
|
|
|
class LiveCaptions extends PureComponent {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = { clear: true };
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { clear } = this.state;
|
|
|
|
|
|
|
|
if (clear) {
|
2022-04-13 23:00:45 +08:00
|
|
|
const { transcript } = this.props;
|
|
|
|
if (prevProps.transcript !== transcript) {
|
2022-03-25 03:05:20 +08:00
|
|
|
// eslint-disable-next-line react/no-did-update-set-state
|
|
|
|
this.setState({ clear: false });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.resetTimer();
|
|
|
|
this.timer = setTimeout(() => this.setState({ clear: true }), CAPTIONS_CONFIG.time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.resetTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
resetTimer() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2022-04-13 23:00:45 +08:00
|
|
|
const {
|
|
|
|
transcript,
|
|
|
|
transcriptId,
|
|
|
|
} = this.props;
|
|
|
|
|
2022-03-25 03:05:20 +08:00
|
|
|
const { clear } = this.state;
|
2022-04-06 23:02:57 +08:00
|
|
|
|
2022-04-13 23:00:45 +08:00
|
|
|
const hasContent = transcript.length > 0 && !clear;
|
2022-04-09 00:53:29 +08:00
|
|
|
|
2022-04-06 23:02:57 +08:00
|
|
|
const wrapperStyles = {
|
2022-04-13 23:00:45 +08:00
|
|
|
display: 'flex',
|
2022-04-06 23:02:57 +08:00
|
|
|
};
|
2022-03-25 03:05:20 +08:00
|
|
|
|
|
|
|
const captionStyles = {
|
2022-04-06 23:02:57 +08:00
|
|
|
whiteSpace: 'pre-line',
|
2022-03-25 03:05:20 +08:00
|
|
|
wordWrap: 'break-word',
|
2022-04-09 03:41:04 +08:00
|
|
|
fontFamily: 'Verdana, Arial, Helvetica, sans-serif',
|
2022-04-06 23:02:57 +08:00
|
|
|
fontSize: '1.5rem',
|
2022-04-14 22:33:28 +08:00
|
|
|
background: '#000000a0',
|
2022-04-06 23:02:57 +08:00
|
|
|
color: 'white',
|
2022-04-13 23:00:45 +08:00
|
|
|
padding: hasContent ? '.5rem' : undefined,
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const visuallyHidden = {
|
|
|
|
position: 'absolute',
|
|
|
|
overflow: 'hidden',
|
|
|
|
clip: 'rect(0 0 0 0)',
|
|
|
|
height: '1px',
|
|
|
|
width: '1px',
|
|
|
|
margin: '-1px',
|
|
|
|
padding: '0',
|
|
|
|
border: '0',
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-04-06 23:02:57 +08:00
|
|
|
<div style={wrapperStyles}>
|
2022-04-13 23:00:45 +08:00
|
|
|
{clear ? null : (
|
|
|
|
<UserContainer
|
2022-04-14 22:33:28 +08:00
|
|
|
background="#000000a0"
|
2022-04-13 23:00:45 +08:00
|
|
|
transcriptId={transcriptId}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-25 03:05:20 +08:00
|
|
|
<div style={captionStyles}>
|
2022-04-13 23:00:45 +08:00
|
|
|
{clear ? '' : transcript}
|
2022-03-25 03:05:20 +08:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
style={visuallyHidden}
|
|
|
|
aria-atomic
|
|
|
|
aria-live="polite"
|
|
|
|
>
|
2022-04-13 23:00:45 +08:00
|
|
|
{clear ? '' : transcript}
|
2022-03-25 03:05:20 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveCaptions.propTypes = {
|
2022-04-13 23:00:45 +08:00
|
|
|
transcript: PropTypes.string.isRequired,
|
|
|
|
transcriptId: PropTypes.string.isRequired,
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default LiveCaptions;
|