Skip to content

Commit

Permalink
added draggability to playhead (#175)
Browse files Browse the repository at this point in the history
* added draggability to playhead

* removed console logs and unnecesary comments

* no need to have the playhead itself clickable
  • Loading branch information
4d4mm committed Oct 23, 2018
1 parent b071b19 commit d2f401f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/components/Playhead/Playhead.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
position: absolute;
right: -10px;
top: 5px;
cursor: ew-resize;
}
}
13 changes: 12 additions & 1 deletion src/components/TimelineScrubber/TimelineScrubber.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class TimelineScrubber extends Component {
showTimes: PropTypes.bool,
/** current viewport position */
x: PropTypes.number,
/** playhead is dragging */
isPlayheadUpdating: PropTypes.bool,
/** playhead Drag X */
playheadX: PropTypes.number,
};

static defaultProps = {
Expand All @@ -47,6 +51,8 @@ class TimelineScrubber extends Component {
zoom: 1.0,
dragStart: () => {},
showTimes: false,
isPlayheadUpdating: false,
playheadX: 0,
};

timeToPercent = time => (time / this.props.runTime) * 100; //* this.props.zoom;
Expand Down Expand Up @@ -84,6 +90,8 @@ class TimelineScrubber extends Component {
x,
width,
zoom,
isPlayheadUpdating,
playheadX,
} = this.props;
return (
<div
Expand Down Expand Up @@ -120,7 +128,10 @@ class TimelineScrubber extends Component {
)}
</TimelineMarker>
))}
<PlayHead x={this.timeToPercent(currentTime)} isUpdating={false} />
<PlayHead
x={this.timeToPercent(isPlayheadUpdating ? playheadX : currentTime)}
isUpdating={isPlayheadUpdating}
/>
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/TimelineScrubber/TimelineScrubber.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
border-bottom: 2px solid transparent;
border-top: 2px solid transparent;
background: #eeeeee;
cursor: pointer;
}
43 changes: 41 additions & 2 deletions src/containers/BubbleEditor/BubbleEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class BubbleEditor extends React.Component {
startX: 0,
deltaX: 0,
viewportX: 0,
viewportStartX: -1,
viewportStartX: 0,
isPlayheadUpdating: false,
playheadX: 0,
scrobberBounds: null,
};
}

Expand Down Expand Up @@ -68,6 +71,33 @@ class BubbleEditor extends React.Component {
}
};

playheadDragMove = ev => {
if (this.state.isPlayheadUpdating) {
// in order to smooth drag
this.clearTextSelection();
const positionRatio =
(ev.pageX - this.state.scrobberBounds.left) /
this.state.scrobberBounds.width;
const time = positionRatio * this.props.runTime;
this.setState({
playheadX: time,
});
}
};

playheadDragEnd = ev => {
if (this.state.isPlayheadUpdating) {
this.props.onUpdateTime(this.state.playheadX);
this.setState({
selectedPoint: -1,
isPlayheadUpdating: false,
playheadX: 0,
});
}
document.body.removeEventListener('mousemove', this.playheadDragMove);
document.body.removeEventListener('mouseup', this.playheadDragEnd);
};

dragStart = ev => {
if (
ev.target === ev.target.parentNode.firstChild ||
Expand All @@ -83,7 +113,14 @@ class BubbleEditor extends React.Component {
const positionRatio =
(ev.pageX - scrobberBounds.left) / scrobberBounds.width;
const time = positionRatio * this.props.runTime;
this.props.onUpdateTime(time);
document.body.addEventListener('mousemove', this.playheadDragMove);
document.body.addEventListener('mouseup', this.playheadDragEnd);
this.setState({
selectedPoint: -1,
isPlayheadUpdating: true,
playheadX: time,
scrobberBounds,
});
return;
}
const selectedPoint = Array.prototype.indexOf.call(
Expand Down Expand Up @@ -285,6 +322,8 @@ class BubbleEditor extends React.Component {
dragStart={this.dragStart}
selectedPoint={this.state.selectedPoint}
showTimes={this.props.showTimes}
isPlayheadUpdating={this.state.isPlayheadUpdating}
playheadX={this.state.playheadX}
/>
</div>
)}
Expand Down

0 comments on commit d2f401f

Please sign in to comment.