Skip to content

Commit 032cd5a

Browse files
committed
Use DragRecognizer in AudioTrimmer
1 parent b0dbe9e commit 032cd5a

File tree

1 file changed

+20
-35
lines changed

1 file changed

+20
-35
lines changed

src/containers/audio-trimmer.jsx

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import bindAll from 'lodash.bindall';
44
import AudioTrimmerComponent from '../components/audio-trimmer/audio-trimmer.jsx';
5-
import {getEventXY} from '../lib/touch-utils';
5+
import DragRecognizer from '../lib/drag-recognizer';
66

77
const MIN_LENGTH = 0.01; // Used to stop sounds being trimmed smaller than 1%
88

@@ -14,54 +14,39 @@ class AudioTrimmer extends React.Component {
1414
'handleTrimEndMouseDown',
1515
'handleTrimStartMouseMove',
1616
'handleTrimEndMouseMove',
17-
'handleTrimStartMouseUp',
18-
'handleTrimEndMouseUp',
1917
'storeRef'
2018
]);
19+
this.trimStartDragRecognizer = new DragRecognizer({
20+
onDrag: this.handleTrimStartMouseMove,
21+
touchDragAngle: 90,
22+
distanceThreshold: 0
23+
});
24+
this.trimEndDragRecognizer = new DragRecognizer({
25+
onDrag: this.handleTrimEndMouseMove,
26+
touchDragAngle: 90,
27+
distanceThreshold: 0
28+
});
29+
2130
}
22-
handleTrimStartMouseMove (e) {
23-
const containerSize = this.containerElement.getBoundingClientRect().width;
24-
const dx = (getEventXY(e).x - this.initialX) / containerSize;
31+
handleTrimStartMouseMove (currentOffset, initialOffset) {
32+
const dx = (currentOffset.x - initialOffset.x) / this.containerSize;
2533
const newTrim = Math.max(0, Math.min(this.props.trimEnd - MIN_LENGTH, this.initialTrim + dx));
2634
this.props.onSetTrimStart(newTrim);
27-
e.preventDefault();
2835
}
29-
handleTrimEndMouseMove (e) {
30-
const containerSize = this.containerElement.getBoundingClientRect().width;
31-
const dx = (getEventXY(e).x - this.initialX) / containerSize;
36+
handleTrimEndMouseMove (currentOffset, initialOffset) {
37+
const dx = (currentOffset.x - initialOffset.x) / this.containerSize;
3238
const newTrim = Math.min(1, Math.max(this.props.trimStart + MIN_LENGTH, this.initialTrim + dx));
3339
this.props.onSetTrimEnd(newTrim);
34-
e.preventDefault();
35-
}
36-
handleTrimStartMouseUp () {
37-
window.removeEventListener('mousemove', this.handleTrimStartMouseMove);
38-
window.removeEventListener('mouseup', this.handleTrimStartMouseUp);
39-
window.removeEventListener('touchmove', this.handleTrimStartMouseMove);
40-
window.removeEventListener('touchend', this.handleTrimStartMouseUp);
41-
}
42-
handleTrimEndMouseUp () {
43-
window.removeEventListener('mousemove', this.handleTrimEndMouseMove);
44-
window.removeEventListener('mouseup', this.handleTrimEndMouseUp);
45-
window.removeEventListener('touchmove', this.handleTrimEndMouseMove);
46-
window.removeEventListener('touchend', this.handleTrimEndMouseUp);
4740
}
4841
handleTrimStartMouseDown (e) {
49-
this.initialX = getEventXY(e).x;
42+
this.containerSize = this.containerElement.getBoundingClientRect().width;
43+
this.trimStartDragRecognizer.start(e);
5044
this.initialTrim = this.props.trimStart;
51-
window.addEventListener('mousemove', this.handleTrimStartMouseMove);
52-
window.addEventListener('mouseup', this.handleTrimStartMouseUp);
53-
window.addEventListener('touchmove', this.handleTrimStartMouseMove);
54-
window.addEventListener('touchend', this.handleTrimStartMouseUp);
55-
e.preventDefault();
5645
}
5746
handleTrimEndMouseDown (e) {
58-
this.initialX = getEventXY(e).x;
47+
this.containerSize = this.containerElement.getBoundingClientRect().width;
48+
this.trimEndDragRecognizer.start(e);
5949
this.initialTrim = this.props.trimEnd;
60-
window.addEventListener('mousemove', this.handleTrimEndMouseMove);
61-
window.addEventListener('mouseup', this.handleTrimEndMouseUp);
62-
window.addEventListener('touchmove', this.handleTrimEndMouseMove);
63-
window.addEventListener('touchend', this.handleTrimEndMouseUp);
64-
e.preventDefault();
6550
}
6651
storeRef (el) {
6752
this.containerElement = el;

0 commit comments

Comments
 (0)