@ldelia/react-media 0.5.5 → 0.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/timeline/RangeSelectorCanvas/RangeSelectorCanvas.js +43 -71
- package/dist/components/timeline/TimelineMarkers/TimelineMarkers.js +1 -1
- package/dist/components/timeline/TimelineValue/TimelineValue.js +1 -1
- package/dist/stories/timeline.stories.js +1 -0
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useContext, useEffect, useMemo, useRef } from 'react';
|
|
1
|
+
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
2
|
import styled from 'styled-components';
|
|
3
3
|
import { ZoomContext } from '../ZoomContext/ZoomContext';
|
|
4
4
|
import { pixelToSeconds, secondsToPixel } from '../utils/utils';
|
|
@@ -9,11 +9,13 @@ const OverlayCanvas = styled.canvas `
|
|
|
9
9
|
width: 100%;
|
|
10
10
|
height: 100%;
|
|
11
11
|
color: cadetblue;
|
|
12
|
+
cursor: pointer;
|
|
12
13
|
`;
|
|
13
14
|
const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
14
15
|
const canvasRef = useRef(null);
|
|
15
16
|
const zoomContextValue = useContext(ZoomContext);
|
|
16
|
-
|
|
17
|
+
const [selection, setSelection] = useState({ start: null, end: null });
|
|
18
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
17
19
|
let selectedRangeInPixels = useMemo(() => [], []);
|
|
18
20
|
if (selectedRange.length === 2) {
|
|
19
21
|
selectedRangeInPixels = [
|
|
@@ -21,7 +23,6 @@ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
|
21
23
|
secondsToPixel(zoomContextValue, selectedRange[1]),
|
|
22
24
|
];
|
|
23
25
|
}
|
|
24
|
-
let lastValidSelectedRangeInPixels = [];
|
|
25
26
|
const getMousePointerPixelPosition = (e) => {
|
|
26
27
|
const canvas = canvasRef.current;
|
|
27
28
|
let rect = canvas.getBoundingClientRect();
|
|
@@ -37,81 +38,52 @@ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
|
37
38
|
context.fillRect(pixelX0, 0, pixelX1 - pixelX0, context.canvas.height);
|
|
38
39
|
context.globalAlpha = 1.0;
|
|
39
40
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
return false;
|
|
51
|
-
};
|
|
52
|
-
const onCanvasMouseDown = (e) => {
|
|
53
|
-
const mouseCurrentPosition = getMousePointerPixelPosition(e);
|
|
54
|
-
if (!isPixelNearSelectedRange(mouseCurrentPosition)) {
|
|
55
|
-
// Keep track of the first position of the new range.
|
|
56
|
-
selectedRangeInPixels = [mouseCurrentPosition, mouseCurrentPosition];
|
|
57
|
-
}
|
|
58
|
-
isSelectingRange = true;
|
|
41
|
+
// Handle mouse down (start of selection)
|
|
42
|
+
const handleMouseDown = (event) => {
|
|
43
|
+
const canvas = canvasRef.current;
|
|
44
|
+
if (!canvas)
|
|
45
|
+
return;
|
|
46
|
+
const pixel = getMousePointerPixelPosition(event);
|
|
47
|
+
const second = pixelToSeconds(zoomContextValue, pixel);
|
|
48
|
+
setSelection({ start: second, end: null });
|
|
49
|
+
setIsDragging(true);
|
|
59
50
|
};
|
|
60
|
-
|
|
51
|
+
// Handle mouse up (end of selection or single click)
|
|
52
|
+
const handleMouseUp = (event) => {
|
|
61
53
|
const canvas = canvasRef.current;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return;
|
|
71
|
-
if (mouseCurrentPosition < selectedRangeInPixels[0]) {
|
|
72
|
-
// The left side must be enlarged.
|
|
73
|
-
selectedRangeInPixels[0] = mouseCurrentPosition;
|
|
74
|
-
}
|
|
75
|
-
else if (mouseCurrentPosition > selectedRangeInPixels[1]) {
|
|
76
|
-
// The right side must be enlarged.
|
|
77
|
-
selectedRangeInPixels[1] = mouseCurrentPosition;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
const diffX0 = mouseCurrentPosition - selectedRangeInPixels[0];
|
|
81
|
-
const diffX1 = selectedRangeInPixels[1] - mouseCurrentPosition;
|
|
82
|
-
if (diffX0 < diffX1) {
|
|
83
|
-
// The left side must be shrunk.
|
|
84
|
-
selectedRangeInPixels[0] = mouseCurrentPosition;
|
|
54
|
+
if (!canvas)
|
|
55
|
+
return;
|
|
56
|
+
const pixel = getMousePointerPixelPosition(event);
|
|
57
|
+
const second = pixelToSeconds(zoomContextValue, pixel);
|
|
58
|
+
if (isDragging) {
|
|
59
|
+
setSelection(prevSelection => {
|
|
60
|
+
if (prevSelection.start === second) {
|
|
61
|
+
onChange(second);
|
|
85
62
|
}
|
|
86
63
|
else {
|
|
87
|
-
|
|
88
|
-
selectedRangeInPixels[1] = mouseCurrentPosition;
|
|
64
|
+
onRangeChange([prevSelection.start, second]);
|
|
89
65
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
66
|
+
return {
|
|
67
|
+
start: prevSelection.start,
|
|
68
|
+
end: second,
|
|
69
|
+
};
|
|
70
|
+
});
|
|
93
71
|
}
|
|
72
|
+
setIsDragging(false);
|
|
94
73
|
};
|
|
95
|
-
|
|
96
|
-
|
|
74
|
+
// Handle mouse move (for dragging)
|
|
75
|
+
const handleMouseMove = (event) => {
|
|
76
|
+
if (!isDragging)
|
|
97
77
|
return;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
lastValidSelectedRangeInPixels = pixelRange;
|
|
110
|
-
onRangeChange([
|
|
111
|
-
pixelToSeconds(zoomContextValue, pixelRange[0]),
|
|
112
|
-
pixelToSeconds(zoomContextValue, pixelRange[1]),
|
|
113
|
-
]);
|
|
114
|
-
}
|
|
78
|
+
const canvas = canvasRef.current;
|
|
79
|
+
if (!canvas)
|
|
80
|
+
return;
|
|
81
|
+
const pixel = getMousePointerPixelPosition(event);
|
|
82
|
+
const second = pixelToSeconds(zoomContextValue, pixel);
|
|
83
|
+
setSelection(prevSelection => ({
|
|
84
|
+
start: prevSelection.start,
|
|
85
|
+
end: second,
|
|
86
|
+
}));
|
|
115
87
|
};
|
|
116
88
|
useEffect(() => {
|
|
117
89
|
const canvas = canvasRef.current;
|
|
@@ -122,6 +94,6 @@ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
|
122
94
|
return;
|
|
123
95
|
drawRect(selectedRangeInPixels[0], selectedRangeInPixels[1]);
|
|
124
96
|
}, [selectedRangeInPixels]);
|
|
125
|
-
return (React.createElement(OverlayCanvas, { ref: canvasRef,
|
|
97
|
+
return (React.createElement(OverlayCanvas, { ref: canvasRef, onMouseDown: handleMouseDown, onMouseMove: handleMouseMove, onMouseUp: handleMouseUp, className: 'media-timeline-range-selector-canvas' }));
|
|
126
98
|
};
|
|
127
99
|
export default React.memo(RangeSelectorCanvas);
|
|
@@ -21,6 +21,6 @@ const MarkerInner = styled.div `
|
|
|
21
21
|
`;
|
|
22
22
|
export const TimelineMarkers = ({ markers }) => {
|
|
23
23
|
const zoomContextValue = useContext(ZoomContext);
|
|
24
|
-
return (React.createElement(React.Fragment, null, markers.map((marker, index) => (React.createElement(MarkerContainer, { key: index, style: { left: secondsToPixel(zoomContextValue, marker) } },
|
|
24
|
+
return (React.createElement(React.Fragment, null, markers.map((marker, index) => (React.createElement(MarkerContainer, { key: index, style: { left: secondsToPixel(zoomContextValue, marker) }, className: 'media-timeline-marker' },
|
|
25
25
|
React.createElement(MarkerInner, null))))));
|
|
26
26
|
};
|
|
@@ -30,7 +30,7 @@ export const TimelineValue = ({ canvasRef, value }) => {
|
|
|
30
30
|
const postValueLineElement = postValueLineRef.current;
|
|
31
31
|
const valueLineWidth = getComputedElementWidth(valueLineElement);
|
|
32
32
|
const linePosition = secondsToPixel(zoomContextValue, value);
|
|
33
|
-
const valueLinePositionBeginningConsideringWidth = linePosition - valueLineWidth / 2;
|
|
33
|
+
const valueLinePositionBeginningConsideringWidth = Math.max(linePosition - valueLineWidth / 2, 0);
|
|
34
34
|
// configure preValueLineElement
|
|
35
35
|
preValueLineElement.style.width = numberToPxString(valueLinePositionBeginningConsideringWidth);
|
|
36
36
|
// configure valueLineElement
|