You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimelineAxis.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React, { MouseEvent, useCallback, useEffect, useRef, useState } from 'react';
  2. import { useDispatch, useSelector } from 'react-redux';
  3. import { IReduxState } from '../../../app/types';
  4. import { addToOffset, addToOffsetLeft, addToOffsetRight, setTimelinePanning } from '../../actions.any';
  5. import { MIN_HANDLER_WIDTH } from '../../constants';
  6. import { getCurrentDuration, getTimelineBoundaries } from '../../functions';
  7. import TimeElapsed from './TimeElapsed';
  8. const TimelineAxis = () => {
  9. const currentDuration = useSelector((state: IReduxState) => getCurrentDuration(state)) ?? 0;
  10. const { left, right } = useSelector((state: IReduxState) => getTimelineBoundaries(state));
  11. const { timelinePanning } = useSelector((state: IReduxState) => state['features/speaker-stats']);
  12. const dispatch = useDispatch();
  13. const axisRef = useRef<HTMLDivElement>(null);
  14. const [ dragLeft, setDragLeft ] = useState(false);
  15. const [ dragRight, setDragRight ] = useState(false);
  16. const getPointOnAxis = useCallback((event: MouseEvent) => {
  17. const axisRect = event.currentTarget.getBoundingClientRect();
  18. const eventOffsetX = event.pageX - axisRect.left;
  19. return (eventOffsetX * currentDuration) / axisRect.width;
  20. }, [ currentDuration ]);
  21. const startResizeHandlerLeft = useCallback((event: MouseEvent) => {
  22. if (!timelinePanning.active && !dragRight) {
  23. setDragLeft(true);
  24. }
  25. event.preventDefault();
  26. event.stopPropagation();
  27. }, [ dragRight, timelinePanning, setDragLeft ]);
  28. const stopResizeLeft = () => {
  29. setDragLeft(false);
  30. };
  31. const resizeHandlerLeft = useCallback((event: MouseEvent) => {
  32. if (dragLeft) {
  33. const point = getPointOnAxis(event);
  34. if (point >= 0 && point < right) {
  35. const value = point - left;
  36. dispatch(addToOffsetLeft(value));
  37. }
  38. }
  39. }, [ dragLeft, getPointOnAxis, dispatch, addToOffsetLeft ]);
  40. const startResizeHandlerRight = useCallback((event: MouseEvent) => {
  41. if (!timelinePanning.active && !dragRight) {
  42. setDragRight(true);
  43. }
  44. event.preventDefault();
  45. event.stopPropagation();
  46. }, [ timelinePanning, dragRight ]);
  47. const stopResizeRight = useCallback(() => {
  48. setDragRight(false);
  49. }, [ setDragRight ]);
  50. const resizeHandlerRight = (event: MouseEvent) => {
  51. if (dragRight) {
  52. const point = getPointOnAxis(event);
  53. if (point > left && point <= currentDuration) {
  54. const value = point - right;
  55. dispatch(addToOffsetRight(value));
  56. }
  57. }
  58. };
  59. const startMoveHandler = useCallback((event: MouseEvent) => {
  60. if (!dragLeft && !dragRight) {
  61. const point = getPointOnAxis(event);
  62. dispatch(setTimelinePanning(
  63. {
  64. active: true,
  65. x: point
  66. }
  67. ));
  68. }
  69. event.preventDefault();
  70. event.stopPropagation();
  71. }, [ dragLeft, dragRight, getPointOnAxis, dispatch, setTimelinePanning ]);
  72. const stopMoveHandler = () => {
  73. dispatch(setTimelinePanning({ ...timelinePanning,
  74. active: false }));
  75. };
  76. const moveHandler = useCallback((event: MouseEvent) => {
  77. const { active, x } = timelinePanning;
  78. if (active) {
  79. const point = getPointOnAxis(event);
  80. dispatch(addToOffset(point - x));
  81. dispatch(setTimelinePanning({ ...timelinePanning,
  82. x: point }));
  83. }
  84. }, [ timelinePanning, getPointOnAxis, dispatch, addToOffset, setTimelinePanning ]);
  85. const handleOnMouseMove = useCallback((event: MouseEvent) => {
  86. resizeHandlerLeft(event);
  87. resizeHandlerRight(event);
  88. moveHandler(event);
  89. }, [ resizeHandlerLeft, resizeHandlerRight ]);
  90. const handleOnMouseUp = useCallback(() => {
  91. stopResizeLeft();
  92. stopResizeRight();
  93. stopMoveHandler();
  94. }, [ stopResizeLeft, stopResizeRight, stopMoveHandler ]);
  95. const getHandlerStyle = useCallback(() => {
  96. let marginLeft = 100 / (currentDuration / left);
  97. let width = 100 / (currentDuration / (right - left));
  98. if (axisRef.current) {
  99. const axisWidth = axisRef.current.getBoundingClientRect().width;
  100. let handlerWidth = (width / 100) * axisWidth;
  101. if (handlerWidth < MIN_HANDLER_WIDTH) {
  102. const newLeft = right - ((currentDuration * MIN_HANDLER_WIDTH) / axisWidth);
  103. handlerWidth = MIN_HANDLER_WIDTH;
  104. marginLeft = 100 / (currentDuration / newLeft);
  105. width = 100 / (currentDuration / (right - newLeft));
  106. }
  107. if (marginLeft + width > 100) {
  108. return {
  109. marginLeft: `calc(100% - ${handlerWidth}px)`,
  110. width: handlerWidth
  111. };
  112. }
  113. }
  114. return {
  115. marginLeft: `${marginLeft > 0 ? marginLeft : 0}%`,
  116. width: `${width}%`
  117. };
  118. }, [ currentDuration, left, right, axisRef ]);
  119. useEffect(() => {
  120. window.addEventListener('mouseup', handleOnMouseUp);
  121. return () => window.removeEventListener('mouseup', handleOnMouseUp);
  122. }, []);
  123. return (
  124. <div
  125. className = 'axis-container'
  126. onMouseMove = { handleOnMouseMove }
  127. ref = { axisRef }>
  128. <div
  129. className = 'axis'>
  130. <div className = 'left-bound'>
  131. <TimeElapsed time = { 0 } />
  132. </div>
  133. <div className = 'right-bound'>
  134. <TimeElapsed time = { currentDuration } />
  135. </div>
  136. <div
  137. className = 'handler'
  138. onMouseDown = { startMoveHandler }
  139. style = { getHandlerStyle() } >
  140. <div
  141. className = 'resize'
  142. id = 'left'
  143. onMouseDown = { startResizeHandlerLeft } />
  144. <div
  145. className = 'resize'
  146. id = 'right'
  147. onMouseDown = { startResizeHandlerRight } />
  148. </div>
  149. </div>
  150. </div>
  151. );
  152. };
  153. export default TimelineAxis;