@@ -8,6 +8,7 @@ import { faQuestionCircle, faMousePointer, faICursor, faUserEdit, faKeyboard, fa
88import {
99 Editor ,
1010 EditorState ,
11+ SelectionState ,
1112 CompositeDecorator ,
1213 convertFromRaw ,
1314 convertToRaw ,
@@ -43,6 +44,7 @@ class TimedTextEditor extends React.Component {
4344 }
4445
4546 componentDidMount ( ) {
47+ this . props . hookSetFocus ( this . setFocusOnCurrentWord ) ;
4648 this . loadData ( ) ;
4749 }
4850
@@ -481,6 +483,59 @@ class TimedTextEditor extends React.Component {
481483 return currentWord ;
482484 }
483485
486+ getCurrentFocus = ( ) => {
487+ const currentFocus = {
488+ blockKey : 'NA' ,
489+ offset : 'NA'
490+ } ;
491+
492+ if ( this . state . transcriptData ) {
493+ const contentState = this . state . editorState . getCurrentContent ( ) ;
494+ // TODO: using convertToRaw here might be slowing down performance(?)
495+ const contentStateConvertEdToRaw = convertToRaw ( contentState ) ;
496+ const blocks = contentStateConvertEdToRaw . blocks ;
497+
498+ for ( var blockIdx in blocks ) {
499+ const currentBlock = blocks [ blockIdx ] ;
500+ const blockKey = currentBlock . key ;
501+ const words = currentBlock . data . words ;
502+ const ranges = currentBlock . entityRanges ;
503+
504+ for ( var idx in words ) {
505+ const range = ranges [ idx ] ;
506+ const word = words [ idx ] ;
507+
508+ if ( word . start <= this . props . currentTime ) {
509+ currentFocus . blockKey = blockKey ;
510+ currentFocus . offset = range . offset + range . length ;
511+ currentFocus . start = word . start ;
512+ } else {
513+ break ;
514+ }
515+ }
516+ }
517+ }
518+
519+ return currentFocus ;
520+ }
521+
522+ setFocusOnCurrentWord = ( ) => {
523+
524+ const currentFocus = this . getCurrentFocus ( ) ;
525+
526+ const selectionState = this . state . editorState . getSelection ( ) ;
527+ const selection = selectionState . merge ( {
528+ anchorOffset : currentFocus . offset ,
529+ focusOffset : currentFocus . offset ,
530+ focusKey : currentFocus . blockKey ,
531+ anchorKey : currentFocus . blockKey ,
532+ } ) ;
533+
534+ const newState = EditorState . forceSelection ( this . state . editorState , selection ) ;
535+ const newEditorState = EditorState . push ( newState , this . state . editorState . getCurrentContent ( ) ) ;
536+ this . setState ( { editorState : newEditorState } ) ;
537+ }
538+
484539 render ( ) {
485540 const helpMessage = < div className = { style . helpMessage } >
486541 < span > < FontAwesomeIcon className = { style . icon } icon = { faMousePointer } /> Double click on a word or timestamp to jump to that point in the video.</ span >
@@ -576,7 +631,8 @@ TimedTextEditor.propTypes = {
576631 isScrollIntoViewOn : PropTypes . bool ,
577632 isPauseWhileTypingOn : PropTypes . bool ,
578633 timecodeOffset : PropTypes . number ,
579- handleAnalyticsEvents : PropTypes . func
634+ handleAnalyticsEvents : PropTypes . func ,
635+ hookSetFocus : PropTypes . func ,
580636} ;
581637
582638export default TimedTextEditor ;
0 commit comments