@ldelia/react-media 0.5.6 → 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.
@@ -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
- let isSelectingRange = false;
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
- const onCanvasDoubleClick = (e) => {
41
- const x0 = getMousePointerPixelPosition(e);
42
- onChange(pixelToSeconds(zoomContextValue, x0));
43
- selectedRangeInPixels = lastValidSelectedRangeInPixels;
44
- };
45
- const isPixelNearSelectedRange = (x0) => {
46
- if (selectedRangeInPixels.length === 2) {
47
- const diff = Math.min(Math.abs(selectedRangeInPixels[0] - x0), Math.abs(selectedRangeInPixels[1] - x0));
48
- return diff <= zoomContextValue.pixelsInSecond / 2;
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
- const onCanvasMouseMove = (e) => {
51
+ // Handle mouse up (end of selection or single click)
52
+ const handleMouseUp = (event) => {
61
53
  const canvas = canvasRef.current;
62
- const context = canvas.getContext('2d');
63
- const mouseCurrentPosition = getMousePointerPixelPosition(e);
64
- if (selectedRangeInPixels.length === 2) {
65
- const diff = Math.min(Math.abs(selectedRangeInPixels[0] - mouseCurrentPosition), Math.abs(selectedRangeInPixels[1] - mouseCurrentPosition));
66
- // Change the mouse's cursor if it's near a selected range.
67
- canvas.style.cursor =
68
- diff <= zoomContextValue.pixelsInSecond / 2 ? 'col-resize' : 'default';
69
- if (!isSelectingRange)
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
- // The right side must be shrunk.
88
- selectedRangeInPixels[1] = mouseCurrentPosition;
64
+ onRangeChange([prevSelection.start, second]);
89
65
  }
90
- }
91
- context.clearRect(0, 0, canvas.width, canvas.height);
92
- drawRect(selectedRangeInPixels[0], selectedRangeInPixels[1]);
66
+ return {
67
+ start: prevSelection.start,
68
+ end: second,
69
+ };
70
+ });
93
71
  }
72
+ setIsDragging(false);
94
73
  };
95
- const onCanvasMouseUp = (e) => {
96
- if (selectedRangeInPixels.length !== 2)
74
+ // Handle mouse move (for dragging)
75
+ const handleMouseMove = (event) => {
76
+ if (!isDragging)
97
77
  return;
98
- isSelectingRange = false;
99
- const mouseCurrentPosition = getMousePointerPixelPosition(e);
100
- const pixelRange = [
101
- Math.min(selectedRangeInPixels[0], mouseCurrentPosition),
102
- Math.max(selectedRangeInPixels[1], mouseCurrentPosition),
103
- ];
104
- if (pixelRange[1] - pixelRange[0] <= 1) {
105
- // It was just a click
106
- selectedRangeInPixels = lastValidSelectedRangeInPixels;
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, onDoubleClick: onCanvasDoubleClick, onMouseDown: onCanvasMouseDown, onMouseMove: onCanvasMouseMove, onMouseUp: onCanvasMouseUp, className: 'media-timeline-range-selector-canvas' }));
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);
@@ -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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ldelia/react-media",
3
- "version": "0.5.6",
3
+ "version": "0.5.7",
4
4
  "description": "A React components collection for media-related features.",
5
5
  "private": false,
6
6
  "keywords": [