@ldelia/react-media 0.2.7 → 0.3.0

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.
Files changed (105) hide show
  1. package/.storybook/main.js +25 -2
  2. package/config/env.js +104 -0
  3. package/config/getHttpsConfig.js +66 -0
  4. package/config/jest/babelTransform.js +29 -0
  5. package/config/jest/cssTransform.js +14 -0
  6. package/config/jest/fileTransform.js +40 -0
  7. package/config/modules.js +134 -0
  8. package/config/paths.js +77 -0
  9. package/config/webpack/persistentCache/createEnvironmentHash.js +9 -0
  10. package/config/webpack.config.js +755 -0
  11. package/config/webpackDevServer.config.js +127 -0
  12. package/dist/components/timeline/{RangeSelectorCanvas.js → RangeSelectorCanvas/RangeSelectorCanvas.js} +30 -8
  13. package/dist/components/timeline/Timeline.d.ts +0 -5
  14. package/dist/components/timeline/Timeline.js +32 -27
  15. package/dist/components/timeline/{TickTimeCollectionDisplay.js → TimelineCanvas/TickTimeCollectionDisplay.js} +2 -2
  16. package/dist/components/timeline/TimelineCanvas/TimelineCanvas.d.ts +7 -0
  17. package/dist/components/timeline/TimelineCanvas/TimelineCanvas.js +72 -0
  18. package/dist/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.d.ts +2 -0
  19. package/dist/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.js +32 -0
  20. package/dist/components/timeline/TimelineValue/TimelineValue.d.ts +7 -0
  21. package/dist/components/timeline/{VaLueLineCanvas.js → TimelineValue/TimelineValue.js} +8 -39
  22. package/dist/components/timeline/ZoomContext/ZoomContext.d.ts +7 -0
  23. package/dist/components/timeline/ZoomContext/ZoomContext.js +8 -0
  24. package/dist/components/timeline/utils/utils.d.ts +3 -1
  25. package/dist/components/timeline/utils/utils.js +14 -1
  26. package/dist/stories/timeline.stories.d.ts +6 -6
  27. package/package.json +126 -18
  28. package/scripts/build.js +217 -0
  29. package/scripts/start.js +154 -0
  30. package/scripts/test.js +52 -0
  31. package/src/components/__tests__/timeline/timeline.test.js +22 -0
  32. package/src/components/index.js +1 -0
  33. package/src/components/timeline/RangeSelectorCanvas/RangeSelectorCanvas.js +127 -0
  34. package/src/components/timeline/{RangeSelectorCanvas.tsx → RangeSelectorCanvas/RangeSelectorCanvas.tsx} +9 -11
  35. package/src/components/timeline/Timeline.js +77 -0
  36. package/src/components/timeline/Timeline.tsx +47 -37
  37. package/src/components/timeline/TimelineCanvas/TickTime.js +26 -0
  38. package/src/components/timeline/TimelineCanvas/TickTimeCollectionDisplay.js +19 -0
  39. package/src/components/timeline/{TickTimeCollectionDisplay.tsx → TimelineCanvas/TickTimeCollectionDisplay.tsx} +5 -5
  40. package/src/components/timeline/TimelineCanvas/TimelineCanvas.js +44 -0
  41. package/src/components/timeline/TimelineCanvas/TimelineCanvas.tsx +79 -0
  42. package/src/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.js +28 -0
  43. package/src/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.ts +43 -0
  44. package/src/components/timeline/TimelineValue/TimelineValue.js +50 -0
  45. package/src/components/timeline/{VaLueLineCanvas.tsx → TimelineValue/TimelineValue.tsx} +9 -64
  46. package/src/components/timeline/ZoomContext/ZoomContext.js +2 -0
  47. package/src/components/timeline/ZoomContext/ZoomContext.ts +10 -0
  48. package/src/components/timeline/constants.js +13 -0
  49. package/src/components/timeline/index.js +1 -0
  50. package/src/components/timeline/utils/utils.js +28 -0
  51. package/src/components/timeline/utils/utils.ts +22 -1
  52. package/src/index.js +1 -0
  53. package/src/react-app-env.d.ts +71 -1
  54. package/src/react-docgen-types.d.ts +37 -0
  55. package/src/setupTests.js +5 -0
  56. package/src/stories/timeline.stories.js +54 -0
  57. package/storybook-static/favicon.svg +1 -0
  58. package/storybook-static/index.html +173 -0
  59. package/storybook-static/index.json +1 -0
  60. package/storybook-static/nunito-sans-bold-italic.woff2 +0 -0
  61. package/storybook-static/nunito-sans-bold.woff2 +0 -0
  62. package/storybook-static/nunito-sans-italic.woff2 +0 -0
  63. package/storybook-static/nunito-sans-regular.woff2 +0 -0
  64. package/storybook-static/project.json +1 -0
  65. package/storybook-static/sb-addons/essentials-actions-3/manager-bundle.js +3 -0
  66. package/storybook-static/sb-addons/essentials-actions-3/manager-bundle.js.LEGAL.txt +0 -0
  67. package/storybook-static/sb-addons/essentials-backgrounds-4/manager-bundle.js +12 -0
  68. package/storybook-static/sb-addons/essentials-backgrounds-4/manager-bundle.js.LEGAL.txt +0 -0
  69. package/storybook-static/sb-addons/essentials-controls-2/manager-bundle.js +412 -0
  70. package/storybook-static/sb-addons/essentials-controls-2/manager-bundle.js.LEGAL.txt +0 -0
  71. package/storybook-static/sb-addons/essentials-measure-7/manager-bundle.js +3 -0
  72. package/storybook-static/sb-addons/essentials-measure-7/manager-bundle.js.LEGAL.txt +0 -0
  73. package/storybook-static/sb-addons/essentials-outline-8/manager-bundle.js +3 -0
  74. package/storybook-static/sb-addons/essentials-outline-8/manager-bundle.js.LEGAL.txt +0 -0
  75. package/storybook-static/sb-addons/essentials-toolbars-6/manager-bundle.js +3 -0
  76. package/storybook-static/sb-addons/essentials-toolbars-6/manager-bundle.js.LEGAL.txt +0 -0
  77. package/storybook-static/sb-addons/essentials-viewport-5/manager-bundle.js +3 -0
  78. package/storybook-static/sb-addons/essentials-viewport-5/manager-bundle.js.LEGAL.txt +0 -0
  79. package/storybook-static/sb-addons/links-1/manager-bundle.js +3 -0
  80. package/storybook-static/sb-addons/links-1/manager-bundle.js.LEGAL.txt +0 -0
  81. package/storybook-static/sb-addons/storybook-core-core-server-presets-0/common-manager-bundle.js +3 -0
  82. package/storybook-static/sb-addons/storybook-core-core-server-presets-0/common-manager-bundle.js.LEGAL.txt +0 -0
  83. package/storybook-static/sb-common-assets/favicon.svg +1 -0
  84. package/storybook-static/sb-common-assets/nunito-sans-bold-italic.woff2 +0 -0
  85. package/storybook-static/sb-common-assets/nunito-sans-bold.woff2 +0 -0
  86. package/storybook-static/sb-common-assets/nunito-sans-italic.woff2 +0 -0
  87. package/storybook-static/sb-common-assets/nunito-sans-regular.woff2 +0 -0
  88. package/storybook-static/sb-manager/globals-module-info.js +995 -0
  89. package/storybook-static/sb-manager/globals-runtime.js +53527 -0
  90. package/storybook-static/sb-manager/globals.js +48 -0
  91. package/storybook-static/sb-manager/runtime.js +11885 -0
  92. package/storybook-static/sb-preview/globals.js +33 -0
  93. package/storybook-static/sb-preview/runtime.js +9483 -0
  94. package/tsconfig.json +8 -7
  95. package/.storybook/main.ts +0 -18
  96. package/.storybook/preview.ts +0 -14
  97. package/dist/components/timeline/VaLueLineCanvas.d.ts +0 -7
  98. package/dist/reportWebVitals.d.ts +0 -3
  99. package/dist/reportWebVitals.js +0 -37
  100. package/src/reportWebVitals.ts +0 -15
  101. /package/dist/components/timeline/{RangeSelectorCanvas.d.ts → RangeSelectorCanvas/RangeSelectorCanvas.d.ts} +0 -0
  102. /package/dist/components/timeline/{TickTime.d.ts → TimelineCanvas/TickTime.d.ts} +0 -0
  103. /package/dist/components/timeline/{TickTime.js → TimelineCanvas/TickTime.js} +0 -0
  104. /package/dist/components/timeline/{TickTimeCollectionDisplay.d.ts → TimelineCanvas/TickTimeCollectionDisplay.d.ts} +0 -0
  105. /package/src/components/timeline/{TickTime.tsx → TimelineCanvas/TickTime.tsx} +0 -0
@@ -0,0 +1,127 @@
1
+ import React, { useContext, useEffect, useMemo, useRef } from 'react';
2
+ import styled from 'styled-components';
3
+ import { ZoomContext } from '../ZoomContext/ZoomContext';
4
+ import { pixelToSeconds, secondsToPixel } from '../utils/utils';
5
+ const OverlayCanvas = styled.canvas `
6
+ position: absolute;
7
+ top: 0;
8
+ left: 0;
9
+ width: 100%;
10
+ height: 100%;
11
+ color: cadetblue;
12
+ `;
13
+ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
14
+ const canvasRef = useRef(null);
15
+ const zoomContextValue = useContext(ZoomContext);
16
+ let isSelectingRange = false;
17
+ let selectedRangeInPixels = useMemo(() => [], []);
18
+ if (selectedRange.length === 2) {
19
+ selectedRangeInPixels = [
20
+ secondsToPixel(zoomContextValue, selectedRange[0]),
21
+ secondsToPixel(zoomContextValue, selectedRange[1]),
22
+ ];
23
+ }
24
+ let lastValidSelectedRangeInPixels = [];
25
+ const getMousePointerPixelPosition = (e) => {
26
+ const canvas = canvasRef.current;
27
+ let rect = canvas.getBoundingClientRect();
28
+ return e.clientX - rect.left;
29
+ };
30
+ const drawRect = (pixelX0, pixelX1) => {
31
+ const canvas = canvasRef.current;
32
+ const context = canvas.getContext('2d');
33
+ context.globalAlpha = 0.3;
34
+ context.fillStyle = window
35
+ .getComputedStyle(canvas)
36
+ .getPropertyValue('color');
37
+ context.fillRect(pixelX0, 0, pixelX1 - pixelX0, context.canvas.height);
38
+ context.globalAlpha = 1.0;
39
+ };
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;
59
+ };
60
+ const onCanvasMouseMove = (e) => {
61
+ 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;
85
+ }
86
+ else {
87
+ // The right side must be shrunk.
88
+ selectedRangeInPixels[1] = mouseCurrentPosition;
89
+ }
90
+ }
91
+ context.clearRect(0, 0, canvas.width, canvas.height);
92
+ drawRect(selectedRangeInPixels[0], selectedRangeInPixels[1]);
93
+ }
94
+ };
95
+ const onCanvasMouseUp = (e) => {
96
+ if (selectedRangeInPixels.length !== 2)
97
+ 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
+ }
115
+ };
116
+ useEffect(() => {
117
+ const canvas = canvasRef.current;
118
+ // https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
119
+ canvas.width = canvas.offsetWidth;
120
+ canvas.height = canvas.offsetHeight;
121
+ if (selectedRangeInPixels.length === 0)
122
+ return;
123
+ drawRect(selectedRangeInPixels[0], selectedRangeInPixels[1]);
124
+ }, [selectedRangeInPixels]);
125
+ return (React.createElement(OverlayCanvas, { ref: canvasRef, onDoubleClick: onCanvasDoubleClick, onMouseDown: onCanvasMouseDown, onMouseMove: onCanvasMouseMove, onMouseUp: onCanvasMouseUp, className: 'media-timeline-range-selector-canvas' }));
126
+ };
127
+ export default React.memo(RangeSelectorCanvas);
@@ -1,15 +1,7 @@
1
- import React from 'react';
1
+ import React, { useContext, useEffect, useMemo, useRef } from 'react';
2
2
  import styled from 'styled-components';
3
-
4
- import { ZoomContext, ZoomContextType } from './index';
5
- import { useContext, useEffect, useMemo, useRef } from 'react';
6
- import { pixelToSeconds, secondsToPixel } from './utils/utils';
7
-
8
- export interface RangeSelectorCanvasProps {
9
- selectedRange: number[];
10
- onChange: (value: number) => void;
11
- onRangeChange: (value: number[]) => void;
12
- }
3
+ import { ZoomContext, ZoomContextType } from '../ZoomContext/ZoomContext';
4
+ import { pixelToSeconds, secondsToPixel } from '../utils/utils';
13
5
 
14
6
  const OverlayCanvas = styled.canvas`
15
7
  position: absolute;
@@ -20,6 +12,12 @@ const OverlayCanvas = styled.canvas`
20
12
  color: cadetblue;
21
13
  `;
22
14
 
15
+ export interface RangeSelectorCanvasProps {
16
+ selectedRange: number[];
17
+ onChange: (value: number) => void;
18
+ onRangeChange: (value: number[]) => void;
19
+ }
20
+
23
21
  const RangeSelectorCanvas: React.FC<RangeSelectorCanvasProps> = ({
24
22
  selectedRange,
25
23
  onChange,
@@ -0,0 +1,77 @@
1
+ import React, { useEffect, useRef, useState } from 'react';
2
+ import TimelineCanvas from './TimelineCanvas/TimelineCanvas';
3
+ import RangeSelectorCanvas from './RangeSelectorCanvas/RangeSelectorCanvas';
4
+ import { zoomLevelConfigurations } from './constants';
5
+ import styled from 'styled-components';
6
+ import { TimelineValue } from './TimelineValue/TimelineValue';
7
+ import { ZoomContext } from './ZoomContext/ZoomContext';
8
+ import { getBlockOffsetForZoomLevel, getTimelineWrapperWidth, numberToPxString, } from './utils/utils';
9
+ const TimelineContainer = styled.div `
10
+ background-color: #f0f0f0;
11
+ border: 1px solid #c9c9c9;
12
+ height: 80px;
13
+ width: 100%;
14
+ max-width: 100%;
15
+ overflow-x: auto;
16
+ position: relative;
17
+ display: flex;
18
+ `;
19
+ const TimelineWrapper = styled.div `
20
+ position: absolute;
21
+ height: 100%;
22
+ display: flex;
23
+ flex-direction: row;
24
+ align-items: center;
25
+ overflow: hidden;
26
+ `;
27
+ export const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], withTimeBlocks = true, onChange = () => { }, onRangeChange = () => { }, className = '', }) => {
28
+ const timeLineContainerRef = useRef(null);
29
+ const canvasRef = useRef(null);
30
+ const [zoomContextValue, setZoomContextValue] = useState({
31
+ blockOffset: 0,
32
+ pixelsInSecond: 0,
33
+ timelineWrapperWidth: 0,
34
+ });
35
+ let zoomLevelValue = zoomLevel ? zoomLevel : 0;
36
+ if (zoomLevelValue < 0 || zoomLevelValue >= zoomLevelConfigurations.length) {
37
+ console.warn('Invalid value for property zoomLevel.');
38
+ zoomLevelValue = 0;
39
+ }
40
+ if (value < 0 || value > duration) {
41
+ console.warn('Invalid value.');
42
+ }
43
+ if (!(selectedRange.length === 0 || selectedRange.length === 2)) {
44
+ selectedRange = [];
45
+ console.warn('The selected range must contain only two values.');
46
+ }
47
+ if (selectedRange.length === 2 &&
48
+ !(0 <= selectedRange[0] &&
49
+ selectedRange[0] < selectedRange[1] &&
50
+ selectedRange[1] <= duration)) {
51
+ selectedRange = [];
52
+ console.warn('The selected range is inconsistent.');
53
+ }
54
+ useEffect(() => {
55
+ if (!timeLineContainerRef.current)
56
+ return;
57
+ const timelineWrapperWidth = getTimelineWrapperWidth(timeLineContainerRef.current.offsetWidth, zoomLevelValue);
58
+ setZoomContextValue({
59
+ blockOffset: getBlockOffsetForZoomLevel(zoomLevelValue, duration, timelineWrapperWidth),
60
+ pixelsInSecond: timelineWrapperWidth / duration,
61
+ timelineWrapperWidth: timelineWrapperWidth,
62
+ });
63
+ }, [timeLineContainerRef.current, zoomLevelValue, duration]);
64
+ useEffect(() => {
65
+ const timeLineWrapper = timeLineContainerRef.current;
66
+ const scrollPosition = value * zoomContextValue.pixelsInSecond - 300;
67
+ timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
68
+ }, [duration, value, zoomContextValue]);
69
+ return (React.createElement(TimelineContainer, { ref: timeLineContainerRef, className: className },
70
+ React.createElement(TimelineWrapper, { style: {
71
+ width: numberToPxString(zoomContextValue.timelineWrapperWidth),
72
+ } },
73
+ React.createElement(ZoomContext.Provider, { value: zoomContextValue },
74
+ React.createElement(TimelineCanvas, { ref: canvasRef, duration: duration, withTimeBlocks: withTimeBlocks }),
75
+ React.createElement(TimelineValue, { value: value, canvasRef: canvasRef }),
76
+ React.createElement(RangeSelectorCanvas, { selectedRange: selectedRange, onChange: onChange, onRangeChange: onRangeChange })))));
77
+ };
@@ -1,9 +1,15 @@
1
- import React, { useEffect, useMemo, useRef } from 'react';
2
- import TickTimeCollectionDisplay from './TickTimeCollectionDisplay';
3
- import VaLueLineCanvas from './VaLueLineCanvas';
4
- import RangeSelectorCanvas from './RangeSelectorCanvas';
1
+ import React, { useEffect, useRef, useState } from 'react';
2
+ import TimelineCanvas from './TimelineCanvas/TimelineCanvas';
3
+ import RangeSelectorCanvas from './RangeSelectorCanvas/RangeSelectorCanvas';
5
4
  import { zoomLevelConfigurations } from './constants';
6
5
  import styled from 'styled-components';
6
+ import { TimelineValue } from './TimelineValue/TimelineValue';
7
+ import { ZoomContext, ZoomContextType } from './ZoomContext/ZoomContext';
8
+ import {
9
+ getBlockOffsetForZoomLevel,
10
+ getTimelineWrapperWidth,
11
+ numberToPxString,
12
+ } from './utils/utils';
7
13
 
8
14
  const TimelineContainer = styled.div`
9
15
  background-color: #f0f0f0;
@@ -21,6 +27,7 @@ const TimelineWrapper = styled.div`
21
27
  display: flex;
22
28
  flex-direction: row;
23
29
  align-items: center;
30
+ overflow: hidden;
24
31
  `;
25
32
 
26
33
  export interface TimelineProps {
@@ -34,15 +41,6 @@ export interface TimelineProps {
34
41
  onRangeChange?: (value: number[]) => void;
35
42
  }
36
43
 
37
- export type ZoomContextType = {
38
- blockOffset: number;
39
- pixelsInSecond: number;
40
- };
41
- export const ZoomContext = React.createContext<ZoomContextType>({
42
- blockOffset: 0,
43
- pixelsInSecond: 0,
44
- });
45
-
46
44
  export const Timeline: React.FC<TimelineProps> = ({
47
45
  duration,
48
46
  value,
@@ -53,7 +51,14 @@ export const Timeline: React.FC<TimelineProps> = ({
53
51
  onRangeChange = () => {},
54
52
  className = '',
55
53
  }) => {
56
- const timeLineContainerRef = useRef(null);
54
+ const timeLineContainerRef = useRef<HTMLDivElement>(null);
55
+ const canvasRef = useRef(null);
56
+
57
+ const [zoomContextValue, setZoomContextValue] = useState<ZoomContextType>({
58
+ blockOffset: 0,
59
+ pixelsInSecond: 0,
60
+ timelineWrapperWidth: 0,
61
+ });
57
62
 
58
63
  let zoomLevelValue = zoomLevel ? zoomLevel : 0;
59
64
  if (zoomLevelValue < 0 || zoomLevelValue >= zoomLevelConfigurations.length) {
@@ -82,46 +87,51 @@ export const Timeline: React.FC<TimelineProps> = ({
82
87
  console.warn('The selected range is inconsistent.');
83
88
  }
84
89
 
85
- const zoomParams = useMemo(() => {
86
- return {
87
- blockOffset: zoomLevelConfigurations[zoomLevelValue][0],
88
- pixelsInSecond: zoomLevelConfigurations[zoomLevelValue][1],
89
- };
90
- }, [zoomLevelValue]);
90
+ useEffect(() => {
91
+ if (!timeLineContainerRef.current) return;
91
92
 
92
- let blockStartingTimes = [0];
93
- const blockCounts: number = Math.ceil(duration / zoomParams.blockOffset);
94
- for (let i: number = 1; i < blockCounts; i++) {
95
- blockStartingTimes.push(
96
- blockStartingTimes[blockStartingTimes.length - 1] +
97
- zoomParams.blockOffset,
93
+ const timelineWrapperWidth = getTimelineWrapperWidth(
94
+ timeLineContainerRef.current.offsetWidth,
95
+ zoomLevelValue,
98
96
  );
99
- }
97
+
98
+ setZoomContextValue({
99
+ blockOffset: getBlockOffsetForZoomLevel(
100
+ zoomLevelValue,
101
+ duration,
102
+ timelineWrapperWidth,
103
+ ),
104
+ pixelsInSecond: timelineWrapperWidth / duration,
105
+ timelineWrapperWidth: timelineWrapperWidth,
106
+ });
107
+ }, [timeLineContainerRef.current, zoomLevelValue, duration]);
100
108
 
101
109
  useEffect(() => {
102
110
  const timeLineWrapper: HTMLElement = timeLineContainerRef.current!;
103
- const scrollPosition: number = value * zoomParams.pixelsInSecond - 300;
111
+ const scrollPosition: number =
112
+ value * zoomContextValue.pixelsInSecond - 300;
104
113
  timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
105
- }, [value, zoomParams]);
114
+ }, [duration, value, zoomContextValue]);
106
115
 
107
116
  return (
108
117
  <TimelineContainer ref={timeLineContainerRef} className={className}>
109
118
  <TimelineWrapper
110
- style={{ width: duration * zoomParams.pixelsInSecond + 'px' }}
119
+ style={{
120
+ width: numberToPxString(zoomContextValue.timelineWrapperWidth),
121
+ }}
111
122
  >
112
- <ZoomContext.Provider value={zoomParams}>
113
- <VaLueLineCanvas
114
- blockStartingTimes={withTimeBlocks ? blockStartingTimes : []}
115
- value={value}
123
+ <ZoomContext.Provider value={zoomContextValue}>
124
+ <TimelineCanvas
125
+ ref={canvasRef}
126
+ duration={duration}
127
+ withTimeBlocks={withTimeBlocks}
116
128
  />
129
+ <TimelineValue value={value} canvasRef={canvasRef} />
117
130
  <RangeSelectorCanvas
118
131
  selectedRange={selectedRange}
119
132
  onChange={onChange}
120
133
  onRangeChange={onRangeChange}
121
134
  />
122
- {withTimeBlocks && (
123
- <TickTimeCollectionDisplay tickTimes={blockStartingTimes} />
124
- )}
125
135
  </ZoomContext.Provider>
126
136
  </TimelineWrapper>
127
137
  </TimelineContainer>
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import styled from 'styled-components';
3
+ const TickTimeContainer = styled.span.attrs((props) => ({
4
+ style: {
5
+ left: props.left,
6
+ },
7
+ })) `
8
+ background: transparent;
9
+ position: absolute;
10
+ padding-left: 8px;
11
+ color: #646464;
12
+ user-select: none;
13
+ min-width: 20px;
14
+ max-width: 20px;
15
+ align-self: flex-end;
16
+ `;
17
+ const TickTime = ({ start, leftPosition }) => {
18
+ const minutes = Math.floor(start / 60);
19
+ let seconds = start - minutes * 60;
20
+ let secondsFormatted = seconds < 10 ? '0' + seconds : seconds.toString();
21
+ return (React.createElement(TickTimeContainer, { left: leftPosition, className: 'media-timeline-tick-time' },
22
+ minutes,
23
+ ":",
24
+ secondsFormatted));
25
+ };
26
+ export default TickTime;
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import styled from 'styled-components';
3
+ import TickTime from './TickTime';
4
+ import { ZoomContext } from '../ZoomContext/ZoomContext';
5
+ const TickTimeCollectionDisplayContainer = styled.div `
6
+ position: absolute;
7
+ display: flex;
8
+ align-self: flex-end;
9
+ height: 100%;
10
+ `;
11
+ const TickTimeCollectionDisplay = ({ tickTimes = [], }) => {
12
+ return (React.createElement(ZoomContext.Consumer, null, (value) => {
13
+ return (React.createElement(TickTimeCollectionDisplayContainer, null, tickTimes.map((tickTimeValue, index) => {
14
+ const leftPosition = tickTimeValue * value.pixelsInSecond + 'px';
15
+ return (React.createElement(TickTime, { key: index, start: tickTimeValue, leftPosition: leftPosition }));
16
+ })));
17
+ }));
18
+ };
19
+ export default TickTimeCollectionDisplay;
@@ -1,11 +1,7 @@
1
1
  import React from 'react';
2
2
  import styled from 'styled-components';
3
3
  import TickTime from './TickTime';
4
- import { ZoomContext } from './index';
5
-
6
- export interface TickTimeCollectionDisplayProps {
7
- tickTimes: number[];
8
- }
4
+ import { ZoomContext } from '../ZoomContext/ZoomContext';
9
5
 
10
6
  const TickTimeCollectionDisplayContainer = styled.div`
11
7
  position: absolute;
@@ -14,6 +10,10 @@ const TickTimeCollectionDisplayContainer = styled.div`
14
10
  height: 100%;
15
11
  `;
16
12
 
13
+ export interface TickTimeCollectionDisplayProps {
14
+ tickTimes: number[];
15
+ }
16
+
17
17
  const TickTimeCollectionDisplay: React.FC<TickTimeCollectionDisplayProps> = ({
18
18
  tickTimes = [],
19
19
  }) => {
@@ -0,0 +1,44 @@
1
+ import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, } from 'react';
2
+ import styled from 'styled-components';
3
+ import { ZoomContext } from '../ZoomContext/ZoomContext';
4
+ import TickTimeCollectionDisplay from './TickTimeCollectionDisplay';
5
+ import { drawTimeBlocksOnCanvas } from './drawTimeBlocksOnCanvas';
6
+ const OverlayCanvas = styled.canvas `
7
+ position: absolute;
8
+ top: 0;
9
+ left: 0;
10
+ width: 100%;
11
+ height: 100%;
12
+ color: #c9c9c9;
13
+ `;
14
+ const TimelineCanvas = forwardRef(({ duration, withTimeBlocks }, ref) => {
15
+ const internalCanvasRef = useRef(null);
16
+ const zoomContextValue = useContext(ZoomContext);
17
+ const blockStartingTimes = (() => {
18
+ if (!withTimeBlocks || !zoomContextValue.blockOffset)
19
+ return [];
20
+ let blockStartingTimes = [0];
21
+ let secondsToCover = duration;
22
+ while (secondsToCover > 0) {
23
+ blockStartingTimes.push(blockStartingTimes[blockStartingTimes.length - 1] +
24
+ zoomContextValue.blockOffset);
25
+ secondsToCover = secondsToCover - zoomContextValue.blockOffset;
26
+ }
27
+ blockStartingTimes.splice(-1);
28
+ return blockStartingTimes;
29
+ })();
30
+ // Pass the internal ref's current value directly to the forwarded ref
31
+ useImperativeHandle(ref, () => internalCanvasRef.current);
32
+ useEffect(() => {
33
+ const canvas = internalCanvasRef.current;
34
+ // https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
35
+ canvas.width = canvas.offsetWidth;
36
+ canvas.height = canvas.offsetHeight;
37
+ if (withTimeBlocks)
38
+ drawTimeBlocksOnCanvas(canvas, blockStartingTimes, zoomContextValue);
39
+ }, [blockStartingTimes, withTimeBlocks, zoomContextValue]);
40
+ return (React.createElement(React.Fragment, null,
41
+ React.createElement(OverlayCanvas, { ref: internalCanvasRef, className: 'media-timeline-value-line-canvas' }),
42
+ withTimeBlocks && (React.createElement(TickTimeCollectionDisplay, { tickTimes: blockStartingTimes }))));
43
+ });
44
+ export default TimelineCanvas;
@@ -0,0 +1,79 @@
1
+ import React, {
2
+ forwardRef,
3
+ useContext,
4
+ useEffect,
5
+ useImperativeHandle,
6
+ useRef,
7
+ } from 'react';
8
+ import styled from 'styled-components';
9
+ import { ZoomContext, ZoomContextType } from '../ZoomContext/ZoomContext';
10
+ import TickTimeCollectionDisplay from './TickTimeCollectionDisplay';
11
+ import { drawTimeBlocksOnCanvas } from './drawTimeBlocksOnCanvas';
12
+
13
+ const OverlayCanvas = styled.canvas`
14
+ position: absolute;
15
+ top: 0;
16
+ left: 0;
17
+ width: 100%;
18
+ height: 100%;
19
+ color: #c9c9c9;
20
+ `;
21
+
22
+ export interface VaLueLineCanvasProps {
23
+ duration: number;
24
+ withTimeBlocks: boolean;
25
+ }
26
+
27
+ const TimelineCanvas = forwardRef<HTMLCanvasElement, VaLueLineCanvasProps>(
28
+ ({ duration, withTimeBlocks }, ref) => {
29
+ const internalCanvasRef = useRef<HTMLCanvasElement>(null);
30
+
31
+ const zoomContextValue: ZoomContextType = useContext(ZoomContext);
32
+
33
+ const blockStartingTimes = (() => {
34
+ if (!withTimeBlocks || !zoomContextValue.blockOffset) return [];
35
+
36
+ let blockStartingTimes = [0];
37
+ let secondsToCover = duration;
38
+ while (secondsToCover > 0) {
39
+ blockStartingTimes.push(
40
+ blockStartingTimes[blockStartingTimes.length - 1] +
41
+ zoomContextValue.blockOffset,
42
+ );
43
+ secondsToCover = secondsToCover - zoomContextValue.blockOffset;
44
+ }
45
+ blockStartingTimes.splice(-1);
46
+ return blockStartingTimes;
47
+ })();
48
+
49
+ // Pass the internal ref's current value directly to the forwarded ref
50
+ useImperativeHandle(
51
+ ref,
52
+ () => internalCanvasRef.current as HTMLCanvasElement,
53
+ );
54
+
55
+ useEffect(() => {
56
+ const canvas: HTMLCanvasElement = internalCanvasRef.current!;
57
+
58
+ // https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
59
+ canvas.width = canvas.offsetWidth;
60
+ canvas.height = canvas.offsetHeight;
61
+
62
+ if (withTimeBlocks)
63
+ drawTimeBlocksOnCanvas(canvas, blockStartingTimes, zoomContextValue);
64
+ }, [blockStartingTimes, withTimeBlocks, zoomContextValue]);
65
+
66
+ return (
67
+ <>
68
+ <OverlayCanvas
69
+ ref={internalCanvasRef}
70
+ className={'media-timeline-value-line-canvas'}
71
+ />
72
+ {withTimeBlocks && (
73
+ <TickTimeCollectionDisplay tickTimes={blockStartingTimes} />
74
+ )}
75
+ </>
76
+ );
77
+ },
78
+ );
79
+ export default TimelineCanvas;
@@ -0,0 +1,28 @@
1
+ import { secondsToPixel } from '../utils/utils';
2
+ export const drawTimeBlocksOnCanvas = (canvas, blockStartingTimes, zoomContextValue) => {
3
+ const blockHeight = 20;
4
+ const context = canvas.getContext('2d');
5
+ // draw top line
6
+ context.beginPath();
7
+ context.moveTo(0, canvas.height - blockHeight);
8
+ context.lineTo(zoomContextValue.timelineWrapperWidth, canvas.height - blockHeight);
9
+ context.strokeStyle = window
10
+ .getComputedStyle(canvas)
11
+ .getPropertyValue('color');
12
+ context.stroke();
13
+ for (const blockStartingTime of blockStartingTimes) {
14
+ const x0Pixel = secondsToPixel(zoomContextValue, blockStartingTime);
15
+ const x1Pixel = secondsToPixel(zoomContextValue, blockStartingTime + zoomContextValue.blockOffset);
16
+ context.beginPath();
17
+ // draw left line
18
+ context.moveTo(x0Pixel, canvas.height);
19
+ context.lineTo(x0Pixel, canvas.height - blockHeight);
20
+ // draw right line
21
+ context.moveTo(x1Pixel, canvas.height);
22
+ context.lineTo(x1Pixel, canvas.height - blockHeight);
23
+ context.strokeStyle = window
24
+ .getComputedStyle(canvas)
25
+ .getPropertyValue('color');
26
+ context.stroke();
27
+ }
28
+ };
@@ -0,0 +1,43 @@
1
+ import { secondsToPixel } from '../utils/utils';
2
+ import { ZoomContextType } from '../ZoomContext/ZoomContext';
3
+
4
+ export const drawTimeBlocksOnCanvas = (
5
+ canvas: HTMLCanvasElement,
6
+ blockStartingTimes: number[],
7
+ zoomContextValue: ZoomContextType,
8
+ ): void => {
9
+ const blockHeight = 20;
10
+ const context: CanvasRenderingContext2D = canvas.getContext('2d')!;
11
+
12
+ // draw top line
13
+ context.beginPath();
14
+ context.moveTo(0, canvas.height - blockHeight);
15
+ context.lineTo(
16
+ zoomContextValue.timelineWrapperWidth,
17
+ canvas.height - blockHeight,
18
+ );
19
+ context.strokeStyle = window
20
+ .getComputedStyle(canvas)
21
+ .getPropertyValue('color');
22
+ context.stroke();
23
+
24
+ for (const blockStartingTime of blockStartingTimes) {
25
+ const x0Pixel = secondsToPixel(zoomContextValue, blockStartingTime);
26
+ const x1Pixel = secondsToPixel(
27
+ zoomContextValue,
28
+ blockStartingTime + zoomContextValue.blockOffset,
29
+ );
30
+ context.beginPath();
31
+ // draw left line
32
+ context.moveTo(x0Pixel, canvas.height);
33
+ context.lineTo(x0Pixel, canvas.height - blockHeight);
34
+ // draw right line
35
+ context.moveTo(x1Pixel, canvas.height);
36
+ context.lineTo(x1Pixel, canvas.height - blockHeight);
37
+
38
+ context.strokeStyle = window
39
+ .getComputedStyle(canvas)
40
+ .getPropertyValue('color');
41
+ context.stroke();
42
+ }
43
+ };