Skip to content

Commit fd5beaf

Browse files
committed
Move cursor to current word when hotkey-stop is used
1 parent 1f00138 commit fd5beaf

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/lib/TranscriptEditor/MediaPlayer/defaultHotKeys.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ function returnHotKeys(self) {
44
priority: 1,
55
handler: () => {
66
self.togglePlayMedia();
7+
if (!self.isPlaying()) {
8+
self.props.setFocus();
9+
}
710

811
self.props.handleAnalyticsEvents({
912
category: 'defaultHotKeys',

src/lib/TranscriptEditor/MediaPlayer/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ MediaPlayer.propTypes = {
468468
mediaUrl: PropTypes.string,
469469
hookOnTimeUpdate: PropTypes.func,
470470
rollBackValueInSeconds: PropTypes.number,
471-
timecodeOffset: PropTypes.number
471+
timecodeOffset: PropTypes.number,
472+
setFocus: PropTypes.func,
472473
};
473474

474475
export default hotkeys(MediaPlayer);

src/lib/TranscriptEditor/TimedTextEditor/index.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { faQuestionCircle, faMousePointer, faICursor, faUserEdit, faKeyboard, fa
88
import {
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

582638
export default TimedTextEditor;

src/lib/TranscriptEditor/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ class TranscriptEditor extends React.Component {
218218
}
219219
}
220220

221+
handleSetFocus = () => {
222+
this.setFocus();
223+
}
224+
221225
getEditorContent = (exportFormat) => {
222226
return this.timedTextEditorRef.current.getEditorContent(exportFormat);
223227
}
@@ -239,6 +243,7 @@ class TranscriptEditor extends React.Component {
239243
// ref={ 'MediaPlayer' }
240244
handleAnalyticsEvents={ this.props.handleAnalyticsEvents }
241245
handleSaveTranscript={ this.handleSaveTranscript }
246+
setFocus = { this.handleSetFocus }
242247
/>;
243248

244249
const settings = <Settings
@@ -279,6 +284,7 @@ class TranscriptEditor extends React.Component {
279284
showSpeakers={ this.state.showSpeakers }
280285
ref={ this.timedTextEditorRef }
281286
handleAnalyticsEvents={ this.props.handleAnalyticsEvents }
287+
hookSetFocus={ foo => this.setFocus = foo }
282288
/>;
283289

284290
return (

0 commit comments

Comments
 (0)