-
-
Save AgtLucas/12525ec43b5d8b961529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @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' /> | |
{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