import React, { Component } from 'react' import { Route } from 'react-router-dom' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import actions from '../../../actions' import { ParagraphElementLookup } from '../components/paragraph.types' const floatLT = (a,b) => ((a*10|0) < (b*10|0)) const floatLTE = (a,b) => (a*10|0 === b*10|0 || floatLT(a,b)) class ParagraphList extends Component { state = { paragraphs: [], currentParagraph: -1, currentAnnotation: -1, } constructor(props) { super(props) this.onAnnotationClick = this.onAnnotationClick.bind(this) this.onParagraphDoubleClick = this.onParagraphDoubleClick.bind(this) } componentDidMount() { this.build() } componentDidUpdate(prevProps) { if (this.props.audio.play_ts === prevProps.audio.play_ts) return this.setCurrentParagraph() } setCurrentParagraph() { const { play_ts } = this.props.audio this.state.paragraphs.some(paragraph => { if (floatLTE(paragraph.start_ts, play_ts) && floatLT(play_ts, paragraph.end_ts)) { this.setCurrentAnnotation(paragraph, play_ts) return true } return false }) } setCurrentAnnotation(paragraph, play_ts) { const { id: currentParagraph, annotations } = paragraph let currentAnnotation let annotation let i = 0 let len = annotations.length for (let i = 0; i < len - 1; i++) { if (floatLT(play_ts, annotations[i+1].start_ts)) { currentAnnotation = annotations[i].id break } } if (!currentAnnotation) { currentAnnotation = annotations[len-1].id } this.setState({ currentParagraph, currentAnnotation }) } build() { const { order: annotationOrder, lookup: annotationLookup } = this.props.annotation const { lookup: paragraphLookup } = this.props.paragraph let currentParagraph = {} const paragraphs = [] annotationOrder.forEach((annotation_id, i) => { const annotation = annotationLookup[annotation_id] const paragraph = paragraphLookup[annotation.paragraph_id] if (annotation.paragraph_id !== currentParagraph.id) { const paragraph_type = getParagraphType(annotation, paragraph) currentParagraph = { id: annotation.paragraph_id || ('index_' + i), type: paragraph_type, start_ts: annotation.start_ts, end_ts: 0, annotations: [], } paragraphs.push(currentParagraph) } if (annotation.type === 'paragraph_end') { currentParagraph.end_ts = annotation.start_ts } else { currentParagraph.annotations.push(annotation) } }) for (let i = 0; i < (paragraphs.length - 1); i++) { if (!paragraphs[i].end_ts) { paragraphs[i].end_ts = paragraphs[i+1].start_ts - 0.1 } } this.setState({ paragraphs }) } onAnnotationClick(e, paragraph, annotation){ actions.audio.seek(annotation.start_ts) } onParagraphDoubleClick(e, paragraph) { // } render() { const { paragraphs, currentParagraph, currentAnnotation } = this.state return (
{paragraphs.map(paragraph => { if (paragraph.type in ParagraphElementLookup) { const ParagraphElement = ParagraphElementLookup[paragraph.type] return ( ) } else { return
{'(empty)'}
} })}
) } } const getParagraphType = (annotation, paragraph) => { if (!paragraph) { return annotation.type } return paragraph.type } const mapStateToProps = state => ({ paragraph: state.paragraph.index, annotation: state.annotation.index, audio: state.audio, }) const mapDispatchToProps = dispatch => ({ }) export default connect(mapStateToProps, mapDispatchToProps)(ParagraphList)