Skip to content

Commit 2e4f0bc

Browse files
committed
Autoblink (fixes #54)
1 parent d4b7d0a commit 2e4f0bc

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

webdiff/static/js/components.jsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ var ImageDiff = React.createClass({
337337
'blink': ImageBlinker,
338338
'onion-skin': ImageOnionSkin,
339339
'swipe': ImageSwipe
340-
}[this.props.imageDiffMode];
340+
}[mode];
341341
var image = component({
342342
filePair: this.props.filePair,
343343
shrinkToFit: this.state.shrinkToFit
@@ -449,19 +449,31 @@ var ImageBlinker = React.createClass({
449449
filePair: React.PropTypes.object.isRequired,
450450
shrinkToFit: React.PropTypes.bool
451451
},
452+
mixins: [SetIntervalMixin],
452453
getInitialState: function() {
453-
return {idx: 0};
454+
return {idx: 0, autoBlink: true};
455+
},
456+
toggleAutoBlink: function(e) {
457+
this.setState({
458+
autoBlink: $(this.refs.autoblink.getDOMNode()).is(':checked')
459+
});
454460
},
455461
render: function() {
456462
var pair = this.props.filePair;
457463
var side = ['a', 'b'][this.state.idx];
458464
var path = [pair.a, pair.b][this.state.idx];
459465
var maxWidth = this.props.shrinkToFit ? window.innerWidth - 30 : null;
460-
return <table id="imagediff">
461-
<tr className="image-diff-content">
462-
<td><AnnotatedImage filePair={pair} side={side} maxWidth={maxWidth} /></td>
463-
</tr>
464-
</table>;
466+
return (
467+
<div>
468+
<input ref="autoblink" type="checkbox" id="autoblink" checked={this.state.autoBlink} onChange={this.toggleAutoBlink} />
469+
<label htmlFor="autoblink"> Auto-blink (hit ‘b’ to blink manually)</label>
470+
<table id="imagediff">
471+
<tr className="image-diff-content">
472+
<td><AnnotatedImage filePair={pair} side={side} maxWidth={maxWidth} /></td>
473+
</tr>
474+
</table>
475+
</div>
476+
);
465477
},
466478
componentDidMount: function() {
467479
var toggle = () => {
@@ -473,9 +485,16 @@ var ImageBlinker = React.createClass({
473485
$(document).on('keydown.blink', (e) => {
474486
if (!isLegitKeypress(e)) return;
475487
if (e.keyCode == 66) { // 'b'
476-
toggle();
488+
if (this.isMounted()) {
489+
this.setState({autoBlink: false});
490+
toggle();
491+
}
477492
}
478493
}).on('click.blink', 'a[value="blink"]', toggle);
494+
495+
this.setInterval(() => {
496+
if (this.state.autoBlink) toggle();
497+
}, 500);
479498
},
480499
componentDidUnmount: function(e) {
481500
$(document).off('keydown.blink').off('click.blink');

webdiff/static/js/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ function filePairDisplayName(filePair) {
4747
function isOneSided(filePair) {
4848
return (filePair.type == 'add' || filePair.type == 'delete');
4949
}
50+
51+
52+
// From http://facebook.github.io/react/docs/reusable-components.html
53+
var SetIntervalMixin = {
54+
componentWillMount: function() {
55+
this.intervals = [];
56+
},
57+
setInterval: function() {
58+
this.intervals.push(setInterval.apply(null, arguments));
59+
},
60+
componentWillUnmount: function() {
61+
this.intervals.map(clearInterval);
62+
}
63+
};

0 commit comments

Comments
 (0)