Skip to content

Instantly share code, notes, and snippets.

@biesnecker
Created October 15, 2014 17:33
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save biesnecker/b22da45a56dd55c5c0ab to your computer and use it in GitHub Desktop.
Save biesnecker/b22da45a56dd55c5c0ab to your computer and use it in GitHub Desktop.
/** @jsx React.DOM */
var ReadingTimeWidget = React.createClass({
getInitialState: function() {
return ({
secondsRequired: null,
});
},
componentWillMount: function() {
var cdiv = this.props.contentDiv;
var wpm = 250;
if (typeof this.props.wpm !== 'undefined') {
wpm = this.props.wpm;
}
var text = cdiv.text()
.replace(/\s+/g, ' ')
.split(' ')
.filter(function(e) {
return (e.length > 1);
});
this.setState({ secondsRequired: Math.round(text.length / wpm * 60) });
},
render: function() {
var message = '';
if (this.state.secondsRequired != null) {
var seconds = this.state.secondsRequired;
var minutes = Math.round(seconds / 60);
message = minutes + ' minute read';
} else {
message = 'Calculating reading time...';
}
return (
<span id='reading_time'>
<span className='iconic clock' />&nbsp;
{message}
</span>
);
}
});
$(document).ready(function() {
var mountPoint = $('#reading_time_mount');
if (mountPoint) {
var contentDiv = $('#main_content');
React.renderComponent(
<ReadingTimeWidget contentDiv={contentDiv} wpm={200} />,
mountPoint.get(0));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment