Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted DPE adapter #166

Closed
7 changes: 6 additions & 1 deletion demo/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React from 'react';

// https://github.com/maicki/why-did-you-update
// NOTE: This slows down performance, even during development
// if (process.env.NODE_ENV !== 'production') {
// const { whyDidYouUpdate } = require('why-did-you-update');
// whyDidYouUpdate(React, { exclude: [ /^HotKeysWrapper/ ] } );
// }
import TranscriptEditor from '../packages/components/transcript-editor';
import SttTypeSelect from './select-stt-json-type';
import ExportFormatSelect from './select-export-format';
Expand Down
1 change: 0 additions & 1 deletion demo/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ body {
@media (max-width: 767px) {
body {
padding: 0;
text-align: center;
}

.demoNavItem {
Expand Down
27 changes: 17 additions & 10 deletions demo/select-export-format.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import React from 'react';
import PropTypes from 'prop-types';

const ExportFormatSelect = props => {
return <select className={ props.className } name={ props.name } value={ props.value } onChange={ props.handleChange }>
<option value="draftjs">Draft Js</option>
<option value="txt">Text file</option>
<option value="txtspeakertimecodes">Text file - with Speakers and Timecodes</option>
<option value="html" disabled>HTML</option>
<option value="word" disabled>MS Word</option>
<option value="digitalpaperedit">Digital Paper Edit</option>
</select>;
};
// using PureComponent to minimise number of unnecessary re-renders
class ExportFormatSelect extends React.PureComponent {

render() {
const { props } = this;

return (<select className={ props.className } name={ props.name } value={ props.value } onChange={ props.handleChange }>
<option value="draftjs">Draft Js</option>
<option value="txt">Text file</option>
<option value="txtspeakertimecodes">Text file - with Speakers and Timecodes</option>
<option value="html" disabled>HTML</option>
<option value="word" disabled>MS Word</option>
<option value="digitalpaperedit">Digital Paper Edit</option>
</select>
);
}
}

ExportFormatSelect.propTypes = {
className: PropTypes.string,
Expand Down
1 change: 1 addition & 0 deletions demo/select-stt-json-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const SttTypeSelect = props => {
<option value="vtt-youtube" disabled>Youtube VTT</option>
<option value="amazontranscribe">Amazon Transcribe</option>
<option value="digitalpaperedit">Digital Paper Edit</option>
<option value="google-stt">Google STT</option>
</select>;
};

Expand Down
330 changes: 330 additions & 0 deletions docs/notes/2019-05-16-prevent-unnecessary-re-renders-in-react.md

Large diffs are not rendered by default.

49 changes: 36 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
"@fortawesome/free-solid-svg-icons": "^5.6.3",
"@fortawesome/react-fontawesome": "^0.1.3",
"babel-polyfill": "^6.26.0",
"diff-match-patch": "^1.0.4",
"difflib": "^0.2.4",
"draft-js": "^0.10.5",
"everpolate": "0.0.3",
"mousetrap": "1.5.2",
"number-to-words": "^1.2.4",
"prop-types": "^15.6.2",
"react-keyboard-shortcuts": "^1.1.3",
"react-simple-tooltip": "^2.3.3"
"react-simple-tooltip": "^2.3.3",
"react-visibility-sensor": "^5.0.2"
},
"peerDependencies": {
"react": "^16.6.0",
Expand Down Expand Up @@ -98,7 +98,8 @@
"style-loader": "^0.23.1",
"stylelint-config-standard": "^18.2.0",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0"
"webpack-cli": "^3.3.0",
"why-did-you-update": "^1.0.6"
},
"contributors": [
{
Expand Down
74 changes: 48 additions & 26 deletions packages/components/media-player/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { hotkeys } from 'react-keyboard-shortcuts';
import PlayerControls from './src/PlayerControls';
import PlayerControls from './src/PlayerControls/index.js';
import ProgressBar from './src/ProgressBar';

import returnHotKeys from './src/defaultHotKeys';
Expand All @@ -12,20 +12,7 @@ import {
timecodeToSeconds
} from '../../util/timecode-converter';

const PLAYBACK_RATES = [
{ value: 0.2, label: '0.2' },
{ value: 0.25, label: '0.25' },
{ value: 0.5, label: '0.5' },
{ value: 0.75, label: '0.75' },
{ value: 1, label: '1' },
{ value: 1.25, label: '1.25' },
{ value: 1.5, label: '1.5' },
{ value: 1.75, label: '1.75' },
{ value: 2, label: '2' },
{ value: 2.5, label: '2.5' },
{ value: 3, label: '3' },
{ value: 3.5, label: '3.5' }
];
import PLAYBACK_RATES from './src/PLAYBACK_RATES.js';

class MediaPlayer extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -67,11 +54,40 @@ class MediaPlayer extends React.Component {

componentDidMount() {
// TODO: Should these hook functions be optional? are they needed? what do they actually do?
// TODO: these hook functions need refactoring, they are causing performance problems
this.props.hookSeek(this.setCurrentTime);
this.props.hookPlayMedia(this.togglePlayMedia);
this.props.hookIsPlaying(this.isPlaying);
}

shouldComponentUpdate(nextProps, nextState) {
if (nextProps.rollBackValueInSeconds !== this.state.rollBackValueInSeconds) {
return true;
}
if (nextProps.timecodeOffset !== this.state.timecodeOffset) {
return true;
}
// TODO: workaround to keep the hook functions, only call re-renders
// if current time has changed. And it seems eliminate component's unecessary re-renders.
if (nextProps.currentTime !== this.props.currentTime) {
return true;
}

if (nextState.playbackRate !== this.state.playbackRate) {
return true;
}

if (nextProps.mediaDuration !== this.props.mediaDuration ) {
return true;
}

if (nextState.isMute !== this.state.isMute) {
return true;
}

return false;
}

setCurrentTime = newCurrentTime => {
if (newCurrentTime !== '' && newCurrentTime !== null) {
// hh:mm:ss:ff - mm:ss - m:ss - ss - seconds number or string and hh:mm:ss
Expand Down Expand Up @@ -392,20 +408,26 @@ class MediaPlayer extends React.Component {
}
};

// performance optimization
getProgressBarMax = () => {
return this.props.videoRef.current !== null
? parseInt(this.props.videoRef.current.duration).toString()
: '100';
}

// performance optimization
getProgressBarValue = () => {
return this.props.videoRef.current !== null
? parseInt(this.props.videoRef.current.currentTime)
: 0;
}

render() {
const progressBar = (
<ProgressBar
max={
this.props.videoRef.current !== null
? parseInt(this.props.videoRef.current.duration).toString()
: '100'
}
value={
this.props.videoRef.current !== null
? parseInt(this.props.videoRef.current.currentTime)
: 0
}
buttonClick={ this.handleProgressBarClick.bind(this) }
max={ this.getProgressBarMax() }
value={ this.getProgressBarValue() }
buttonClick={ this.handleProgressBarClick }
/>
);

Expand Down
16 changes: 16 additions & 0 deletions packages/components/media-player/src/PLAYBACK_RATES.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const PLAYBACK_RATES = [
{ value: 0.2, label: '0.2' },
{ value: 0.25, label: '0.25' },
{ value: 0.5, label: '0.5' },
{ value: 0.75, label: '0.75' },
{ value: 1, label: '1' },
{ value: 1.25, label: '1.25' },
{ value: 1.5, label: '1.5' },
{ value: 1.75, label: '1.75' },
{ value: 2, label: '2' },
{ value: 2.5, label: '2.5' },
{ value: 3, label: '3' },
{ value: 3.5, label: '3.5' }
];

export default PLAYBACK_RATES;
41 changes: 23 additions & 18 deletions packages/components/media-player/src/PlaybackRate.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import React from 'react';
import PropTypes from 'prop-types';
import styles from './PlaybackRate.module.css';
// import styles from './PlaybackRate.module.css';
import Select from './Select';
import style from './PlayerControls/index.module.css';

class PlaybackRate extends React.Component {

// to avoid unnecessary re-renders
shouldComponentUpdate(nextProps) {
if (nextProps.playbackRate !== this.props.playbackRate) {
return true;
}

return false;
}
render() {
return (
<div>
<p className={ styles.helpText }>Playback Rate
<output className={ styles.playbackRateValue } >{ ` x${ this.props.playBackRate } ` }</output>
</p>
<input
type="range"
min="0.2"
value={ this.props.playBackRate }
max="3.5"
step="0.1"
onChange={ this.props.handlePlayBackRateChange }
<span className={ style.playBackRate }
title="Playback rate: alt - & alt + ">
<Select
options={ this.props.playbackRateOptions }
currentValue={ this.props.playbackRate.toString() }
name={ 'playbackRate' }
handleChange={ this.props.handlePlayBackRateChange }
/>
<br/>
<button type="button" onClick={ () => { this.props.setPlayBackRate(1); } }>Reset Playback Rate</button>
</div>
</span>
);
}
}

PlaybackRate.propTypes = {
handlePlayBackRateChange: PropTypes.func,
playBackRate: PropTypes.number,
setPlayBackRate: PropTypes.func
playbackRateOptions: PropTypes.array,
playbackRate: PropTypes.number,
handlePlayBackRateChange: PropTypes.func
};

export default PlaybackRate;
Loading