@ldelia/react-media 0.2.7 → 0.2.8

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 (28) hide show
  1. package/dist/components/timeline/RangeSelectorCanvas/RangeSelectorCanvas.d.ts +8 -0
  2. package/dist/components/timeline/RangeSelectorCanvas/RangeSelectorCanvas.js +155 -0
  3. package/dist/components/timeline/Timeline.d.ts +0 -5
  4. package/dist/components/timeline/Timeline.js +32 -27
  5. package/dist/components/timeline/TimelineCanvas/TickTime.d.ts +7 -0
  6. package/dist/components/timeline/TimelineCanvas/TickTime.js +31 -0
  7. package/dist/components/timeline/TimelineCanvas/TickTimeCollectionDisplay.d.ts +6 -0
  8. package/dist/components/timeline/TimelineCanvas/TickTimeCollectionDisplay.js +24 -0
  9. package/dist/components/timeline/TimelineCanvas/TimelineCanvas.d.ts +7 -0
  10. package/dist/components/timeline/TimelineCanvas/TimelineCanvas.js +72 -0
  11. package/dist/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.d.ts +2 -0
  12. package/dist/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.js +32 -0
  13. package/dist/components/timeline/TimelineValue/TimelineValue.d.ts +7 -0
  14. package/dist/components/timeline/TimelineValue/TimelineValue.js +80 -0
  15. package/dist/components/timeline/ZoomContext/ZoomContext.d.ts +7 -0
  16. package/dist/components/timeline/ZoomContext/ZoomContext.js +8 -0
  17. package/dist/components/timeline/utils/utils.d.ts +3 -1
  18. package/dist/components/timeline/utils/utils.js +14 -1
  19. package/package.json +1 -1
  20. package/src/components/timeline/{RangeSelectorCanvas.tsx → RangeSelectorCanvas/RangeSelectorCanvas.tsx} +9 -11
  21. package/src/components/timeline/Timeline.tsx +47 -37
  22. package/src/components/timeline/{TickTimeCollectionDisplay.tsx → TimelineCanvas/TickTimeCollectionDisplay.tsx} +5 -5
  23. package/src/components/timeline/TimelineCanvas/TimelineCanvas.tsx +79 -0
  24. package/src/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.ts +43 -0
  25. package/src/components/timeline/{VaLueLineCanvas.tsx → TimelineValue/TimelineValue.tsx} +9 -64
  26. package/src/components/timeline/ZoomContext/ZoomContext.ts +10 -0
  27. package/src/components/timeline/utils/utils.ts +22 -1
  28. /package/src/components/timeline/{TickTime.tsx → TimelineCanvas/TickTime.tsx} +0 -0
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ export interface RangeSelectorCanvasProps {
3
+ selectedRange: number[];
4
+ onChange: (value: number) => void;
5
+ onRangeChange: (value: number[]) => void;
6
+ }
7
+ declare const _default: React.NamedExoticComponent<RangeSelectorCanvasProps>;
8
+ export default _default;
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const react_1 = __importStar(require("react"));
30
+ const styled_components_1 = __importDefault(require("styled-components"));
31
+ const ZoomContext_1 = require("../ZoomContext/ZoomContext");
32
+ const utils_1 = require("../utils/utils");
33
+ const OverlayCanvas = styled_components_1.default.canvas `
34
+ position: absolute;
35
+ top: 0;
36
+ left: 0;
37
+ width: 100%;
38
+ height: 100%;
39
+ color: cadetblue;
40
+ `;
41
+ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
42
+ const canvasRef = (0, react_1.useRef)(null);
43
+ const zoomContextValue = (0, react_1.useContext)(ZoomContext_1.ZoomContext);
44
+ let isSelectingRange = false;
45
+ let selectedRangeInPixels = (0, react_1.useMemo)(() => [], []);
46
+ if (selectedRange.length === 2) {
47
+ selectedRangeInPixels = [
48
+ (0, utils_1.secondsToPixel)(zoomContextValue, selectedRange[0]),
49
+ (0, utils_1.secondsToPixel)(zoomContextValue, selectedRange[1]),
50
+ ];
51
+ }
52
+ let lastValidSelectedRangeInPixels = [];
53
+ const getMousePointerPixelPosition = (e) => {
54
+ const canvas = canvasRef.current;
55
+ let rect = canvas.getBoundingClientRect();
56
+ return e.clientX - rect.left;
57
+ };
58
+ const drawRect = (pixelX0, pixelX1) => {
59
+ const canvas = canvasRef.current;
60
+ const context = canvas.getContext('2d');
61
+ context.globalAlpha = 0.3;
62
+ context.fillStyle = window
63
+ .getComputedStyle(canvas)
64
+ .getPropertyValue('color');
65
+ context.fillRect(pixelX0, 0, pixelX1 - pixelX0, context.canvas.height);
66
+ context.globalAlpha = 1.0;
67
+ };
68
+ const onCanvasDoubleClick = (e) => {
69
+ const x0 = getMousePointerPixelPosition(e);
70
+ onChange((0, utils_1.pixelToSeconds)(zoomContextValue, x0));
71
+ selectedRangeInPixels = lastValidSelectedRangeInPixels;
72
+ };
73
+ const isPixelNearSelectedRange = (x0) => {
74
+ if (selectedRangeInPixels.length === 2) {
75
+ const diff = Math.min(Math.abs(selectedRangeInPixels[0] - x0), Math.abs(selectedRangeInPixels[1] - x0));
76
+ return diff <= zoomContextValue.pixelsInSecond / 2;
77
+ }
78
+ return false;
79
+ };
80
+ const onCanvasMouseDown = (e) => {
81
+ const mouseCurrentPosition = getMousePointerPixelPosition(e);
82
+ if (!isPixelNearSelectedRange(mouseCurrentPosition)) {
83
+ // Keep track of the first position of the new range.
84
+ selectedRangeInPixels = [mouseCurrentPosition, mouseCurrentPosition];
85
+ }
86
+ isSelectingRange = true;
87
+ };
88
+ const onCanvasMouseMove = (e) => {
89
+ const canvas = canvasRef.current;
90
+ const context = canvas.getContext('2d');
91
+ const mouseCurrentPosition = getMousePointerPixelPosition(e);
92
+ if (selectedRangeInPixels.length === 2) {
93
+ const diff = Math.min(Math.abs(selectedRangeInPixels[0] - mouseCurrentPosition), Math.abs(selectedRangeInPixels[1] - mouseCurrentPosition));
94
+ // Change the mouse's cursor if it's near a selected range.
95
+ canvas.style.cursor =
96
+ diff <= zoomContextValue.pixelsInSecond / 2 ? 'col-resize' : 'default';
97
+ if (!isSelectingRange)
98
+ return;
99
+ if (mouseCurrentPosition < selectedRangeInPixels[0]) {
100
+ // The left side must be enlarged.
101
+ selectedRangeInPixels[0] = mouseCurrentPosition;
102
+ }
103
+ else if (mouseCurrentPosition > selectedRangeInPixels[1]) {
104
+ // The right side must be enlarged.
105
+ selectedRangeInPixels[1] = mouseCurrentPosition;
106
+ }
107
+ else {
108
+ const diffX0 = mouseCurrentPosition - selectedRangeInPixels[0];
109
+ const diffX1 = selectedRangeInPixels[1] - mouseCurrentPosition;
110
+ if (diffX0 < diffX1) {
111
+ // The left side must be shrunk.
112
+ selectedRangeInPixels[0] = mouseCurrentPosition;
113
+ }
114
+ else {
115
+ // The right side must be shrunk.
116
+ selectedRangeInPixels[1] = mouseCurrentPosition;
117
+ }
118
+ }
119
+ context.clearRect(0, 0, canvas.width, canvas.height);
120
+ drawRect(selectedRangeInPixels[0], selectedRangeInPixels[1]);
121
+ }
122
+ };
123
+ const onCanvasMouseUp = (e) => {
124
+ if (selectedRangeInPixels.length !== 2)
125
+ return;
126
+ isSelectingRange = false;
127
+ const mouseCurrentPosition = getMousePointerPixelPosition(e);
128
+ const pixelRange = [
129
+ Math.min(selectedRangeInPixels[0], mouseCurrentPosition),
130
+ Math.max(selectedRangeInPixels[1], mouseCurrentPosition),
131
+ ];
132
+ if (pixelRange[1] - pixelRange[0] <= 1) {
133
+ // It was just a click
134
+ selectedRangeInPixels = lastValidSelectedRangeInPixels;
135
+ }
136
+ else {
137
+ lastValidSelectedRangeInPixels = pixelRange;
138
+ onRangeChange([
139
+ (0, utils_1.pixelToSeconds)(zoomContextValue, pixelRange[0]),
140
+ (0, utils_1.pixelToSeconds)(zoomContextValue, pixelRange[1]),
141
+ ]);
142
+ }
143
+ };
144
+ (0, react_1.useEffect)(() => {
145
+ const canvas = canvasRef.current;
146
+ // https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
147
+ canvas.width = canvas.offsetWidth;
148
+ canvas.height = canvas.offsetHeight;
149
+ if (selectedRangeInPixels.length === 0)
150
+ return;
151
+ drawRect(selectedRangeInPixels[0], selectedRangeInPixels[1]);
152
+ }, [selectedRangeInPixels]);
153
+ return (react_1.default.createElement(OverlayCanvas, { ref: canvasRef, onDoubleClick: onCanvasDoubleClick, onMouseDown: onCanvasMouseDown, onMouseMove: onCanvasMouseMove, onMouseUp: onCanvasMouseUp, className: 'media-timeline-range-selector-canvas' }));
154
+ };
155
+ exports.default = react_1.default.memo(RangeSelectorCanvas);
@@ -9,9 +9,4 @@ export interface TimelineProps {
9
9
  onChange?: (value: number) => void;
10
10
  onRangeChange?: (value: number[]) => void;
11
11
  }
12
- export type ZoomContextType = {
13
- blockOffset: number;
14
- pixelsInSecond: number;
15
- };
16
- export declare const ZoomContext: React.Context<ZoomContextType>;
17
12
  export declare const Timeline: React.FC<TimelineProps>;
@@ -26,13 +26,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.Timeline = exports.ZoomContext = void 0;
29
+ exports.Timeline = void 0;
30
30
  const react_1 = __importStar(require("react"));
31
- const TickTimeCollectionDisplay_1 = __importDefault(require("./TickTimeCollectionDisplay"));
32
- const VaLueLineCanvas_1 = __importDefault(require("./VaLueLineCanvas"));
33
- const RangeSelectorCanvas_1 = __importDefault(require("./RangeSelectorCanvas"));
31
+ const TimelineCanvas_1 = __importDefault(require("./TimelineCanvas/TimelineCanvas"));
32
+ const RangeSelectorCanvas_1 = __importDefault(require("./RangeSelectorCanvas/RangeSelectorCanvas"));
34
33
  const constants_1 = require("./constants");
35
34
  const styled_components_1 = __importDefault(require("styled-components"));
35
+ const TimelineValue_1 = require("./TimelineValue/TimelineValue");
36
+ const ZoomContext_1 = require("./ZoomContext/ZoomContext");
37
+ const utils_1 = require("./utils/utils");
36
38
  const TimelineContainer = styled_components_1.default.div `
37
39
  background-color: #f0f0f0;
38
40
  border: 1px solid #c9c9c9;
@@ -49,13 +51,16 @@ const TimelineWrapper = styled_components_1.default.div `
49
51
  display: flex;
50
52
  flex-direction: row;
51
53
  align-items: center;
54
+ overflow: hidden;
52
55
  `;
53
- exports.ZoomContext = react_1.default.createContext({
54
- blockOffset: 0,
55
- pixelsInSecond: 0,
56
- });
57
56
  const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], withTimeBlocks = true, onChange = () => { }, onRangeChange = () => { }, className = '', }) => {
58
57
  const timeLineContainerRef = (0, react_1.useRef)(null);
58
+ const canvasRef = (0, react_1.useRef)(null);
59
+ const [zoomContextValue, setZoomContextValue] = (0, react_1.useState)({
60
+ blockOffset: 0,
61
+ pixelsInSecond: 0,
62
+ timelineWrapperWidth: 0,
63
+ });
59
64
  let zoomLevelValue = zoomLevel ? zoomLevel : 0;
60
65
  if (zoomLevelValue < 0 || zoomLevelValue >= constants_1.zoomLevelConfigurations.length) {
61
66
  console.warn('Invalid value for property zoomLevel.');
@@ -75,28 +80,28 @@ const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], withTime
75
80
  selectedRange = [];
76
81
  console.warn('The selected range is inconsistent.');
77
82
  }
78
- const zoomParams = (0, react_1.useMemo)(() => {
79
- return {
80
- blockOffset: constants_1.zoomLevelConfigurations[zoomLevelValue][0],
81
- pixelsInSecond: constants_1.zoomLevelConfigurations[zoomLevelValue][1],
82
- };
83
- }, [zoomLevelValue]);
84
- let blockStartingTimes = [0];
85
- const blockCounts = Math.ceil(duration / zoomParams.blockOffset);
86
- for (let i = 1; i < blockCounts; i++) {
87
- blockStartingTimes.push(blockStartingTimes[blockStartingTimes.length - 1] +
88
- zoomParams.blockOffset);
89
- }
83
+ (0, react_1.useEffect)(() => {
84
+ if (!timeLineContainerRef.current)
85
+ return;
86
+ const timelineWrapperWidth = (0, utils_1.getTimelineWrapperWidth)(timeLineContainerRef.current.offsetWidth, zoomLevelValue);
87
+ setZoomContextValue({
88
+ blockOffset: (0, utils_1.getBlockOffsetForZoomLevel)(zoomLevelValue, duration, timelineWrapperWidth),
89
+ pixelsInSecond: timelineWrapperWidth / duration,
90
+ timelineWrapperWidth: timelineWrapperWidth,
91
+ });
92
+ }, [timeLineContainerRef.current, zoomLevelValue, duration]);
90
93
  (0, react_1.useEffect)(() => {
91
94
  const timeLineWrapper = timeLineContainerRef.current;
92
- const scrollPosition = value * zoomParams.pixelsInSecond - 300;
95
+ const scrollPosition = value * zoomContextValue.pixelsInSecond - 300;
93
96
  timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
94
- }, [value, zoomParams]);
97
+ }, [duration, value, zoomContextValue]);
95
98
  return (react_1.default.createElement(TimelineContainer, { ref: timeLineContainerRef, className: className },
96
- react_1.default.createElement(TimelineWrapper, { style: { width: duration * zoomParams.pixelsInSecond + 'px' } },
97
- react_1.default.createElement(exports.ZoomContext.Provider, { value: zoomParams },
98
- react_1.default.createElement(VaLueLineCanvas_1.default, { blockStartingTimes: withTimeBlocks ? blockStartingTimes : [], value: value }),
99
- react_1.default.createElement(RangeSelectorCanvas_1.default, { selectedRange: selectedRange, onChange: onChange, onRangeChange: onRangeChange }),
100
- withTimeBlocks && (react_1.default.createElement(TickTimeCollectionDisplay_1.default, { tickTimes: blockStartingTimes }))))));
99
+ react_1.default.createElement(TimelineWrapper, { style: {
100
+ width: (0, utils_1.numberToPxString)(zoomContextValue.timelineWrapperWidth),
101
+ } },
102
+ react_1.default.createElement(ZoomContext_1.ZoomContext.Provider, { value: zoomContextValue },
103
+ react_1.default.createElement(TimelineCanvas_1.default, { ref: canvasRef, duration: duration, withTimeBlocks: withTimeBlocks }),
104
+ react_1.default.createElement(TimelineValue_1.TimelineValue, { value: value, canvasRef: canvasRef }),
105
+ react_1.default.createElement(RangeSelectorCanvas_1.default, { selectedRange: selectedRange, onChange: onChange, onRangeChange: onRangeChange })))));
101
106
  };
102
107
  exports.Timeline = Timeline;
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ export type Props = {
3
+ start: number;
4
+ leftPosition: string;
5
+ };
6
+ declare const TickTime: React.FC<Props>;
7
+ export default TickTime;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const styled_components_1 = __importDefault(require("styled-components"));
8
+ const TickTimeContainer = styled_components_1.default.span.attrs((props) => ({
9
+ style: {
10
+ left: props.left,
11
+ },
12
+ })) `
13
+ background: transparent;
14
+ position: absolute;
15
+ padding-left: 8px;
16
+ color: #646464;
17
+ user-select: none;
18
+ min-width: 20px;
19
+ max-width: 20px;
20
+ align-self: flex-end;
21
+ `;
22
+ const TickTime = ({ start, leftPosition }) => {
23
+ const minutes = Math.floor(start / 60);
24
+ let seconds = start - minutes * 60;
25
+ let secondsFormatted = seconds < 10 ? '0' + seconds : seconds.toString();
26
+ return (react_1.default.createElement(TickTimeContainer, { left: leftPosition, className: 'media-timeline-tick-time' },
27
+ minutes,
28
+ ":",
29
+ secondsFormatted));
30
+ };
31
+ exports.default = TickTime;
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ export interface TickTimeCollectionDisplayProps {
3
+ tickTimes: number[];
4
+ }
5
+ declare const TickTimeCollectionDisplay: React.FC<TickTimeCollectionDisplayProps>;
6
+ export default TickTimeCollectionDisplay;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const styled_components_1 = __importDefault(require("styled-components"));
8
+ const TickTime_1 = __importDefault(require("./TickTime"));
9
+ const ZoomContext_1 = require("../ZoomContext/ZoomContext");
10
+ const TickTimeCollectionDisplayContainer = styled_components_1.default.div `
11
+ position: absolute;
12
+ display: flex;
13
+ align-self: flex-end;
14
+ height: 100%;
15
+ `;
16
+ const TickTimeCollectionDisplay = ({ tickTimes = [], }) => {
17
+ return (react_1.default.createElement(ZoomContext_1.ZoomContext.Consumer, null, (value) => {
18
+ return (react_1.default.createElement(TickTimeCollectionDisplayContainer, null, tickTimes.map((tickTimeValue, index) => {
19
+ const leftPosition = tickTimeValue * value.pixelsInSecond + 'px';
20
+ return (react_1.default.createElement(TickTime_1.default, { key: index, start: tickTimeValue, leftPosition: leftPosition }));
21
+ })));
22
+ }));
23
+ };
24
+ exports.default = TickTimeCollectionDisplay;
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ export interface VaLueLineCanvasProps {
3
+ duration: number;
4
+ withTimeBlocks: boolean;
5
+ }
6
+ declare const TimelineCanvas: React.ForwardRefExoticComponent<VaLueLineCanvasProps & React.RefAttributes<HTMLCanvasElement>>;
7
+ export default TimelineCanvas;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const react_1 = __importStar(require("react"));
30
+ const styled_components_1 = __importDefault(require("styled-components"));
31
+ const ZoomContext_1 = require("../ZoomContext/ZoomContext");
32
+ const TickTimeCollectionDisplay_1 = __importDefault(require("./TickTimeCollectionDisplay"));
33
+ const drawTimeBlocksOnCanvas_1 = require("./drawTimeBlocksOnCanvas");
34
+ const OverlayCanvas = styled_components_1.default.canvas `
35
+ position: absolute;
36
+ top: 0;
37
+ left: 0;
38
+ width: 100%;
39
+ height: 100%;
40
+ color: #c9c9c9;
41
+ `;
42
+ const TimelineCanvas = (0, react_1.forwardRef)(({ duration, withTimeBlocks }, ref) => {
43
+ const internalCanvasRef = (0, react_1.useRef)(null);
44
+ const zoomContextValue = (0, react_1.useContext)(ZoomContext_1.ZoomContext);
45
+ const blockStartingTimes = (() => {
46
+ if (!withTimeBlocks || !zoomContextValue.blockOffset)
47
+ return [];
48
+ let blockStartingTimes = [0];
49
+ let secondsToCover = duration;
50
+ while (secondsToCover > 0) {
51
+ blockStartingTimes.push(blockStartingTimes[blockStartingTimes.length - 1] +
52
+ zoomContextValue.blockOffset);
53
+ secondsToCover = secondsToCover - zoomContextValue.blockOffset;
54
+ }
55
+ blockStartingTimes.splice(-1);
56
+ return blockStartingTimes;
57
+ })();
58
+ // Pass the internal ref's current value directly to the forwarded ref
59
+ (0, react_1.useImperativeHandle)(ref, () => internalCanvasRef.current);
60
+ (0, react_1.useEffect)(() => {
61
+ const canvas = internalCanvasRef.current;
62
+ // https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
63
+ canvas.width = canvas.offsetWidth;
64
+ canvas.height = canvas.offsetHeight;
65
+ if (withTimeBlocks)
66
+ (0, drawTimeBlocksOnCanvas_1.drawTimeBlocksOnCanvas)(canvas, blockStartingTimes, zoomContextValue);
67
+ }, [blockStartingTimes, withTimeBlocks, zoomContextValue]);
68
+ return (react_1.default.createElement(react_1.default.Fragment, null,
69
+ react_1.default.createElement(OverlayCanvas, { ref: internalCanvasRef, className: 'media-timeline-value-line-canvas' }),
70
+ withTimeBlocks && (react_1.default.createElement(TickTimeCollectionDisplay_1.default, { tickTimes: blockStartingTimes }))));
71
+ });
72
+ exports.default = TimelineCanvas;
@@ -0,0 +1,2 @@
1
+ import { ZoomContextType } from '../ZoomContext/ZoomContext';
2
+ export declare const drawTimeBlocksOnCanvas: (canvas: HTMLCanvasElement, blockStartingTimes: number[], zoomContextValue: ZoomContextType) => void;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.drawTimeBlocksOnCanvas = void 0;
4
+ const utils_1 = require("../utils/utils");
5
+ const drawTimeBlocksOnCanvas = (canvas, blockStartingTimes, zoomContextValue) => {
6
+ const blockHeight = 20;
7
+ const context = canvas.getContext('2d');
8
+ // draw top line
9
+ context.beginPath();
10
+ context.moveTo(0, canvas.height - blockHeight);
11
+ context.lineTo(zoomContextValue.timelineWrapperWidth, canvas.height - blockHeight);
12
+ context.strokeStyle = window
13
+ .getComputedStyle(canvas)
14
+ .getPropertyValue('color');
15
+ context.stroke();
16
+ for (const blockStartingTime of blockStartingTimes) {
17
+ const x0Pixel = (0, utils_1.secondsToPixel)(zoomContextValue, blockStartingTime);
18
+ const x1Pixel = (0, utils_1.secondsToPixel)(zoomContextValue, blockStartingTime + zoomContextValue.blockOffset);
19
+ context.beginPath();
20
+ // draw left line
21
+ context.moveTo(x0Pixel, canvas.height);
22
+ context.lineTo(x0Pixel, canvas.height - blockHeight);
23
+ // draw right line
24
+ context.moveTo(x1Pixel, canvas.height);
25
+ context.lineTo(x1Pixel, canvas.height - blockHeight);
26
+ context.strokeStyle = window
27
+ .getComputedStyle(canvas)
28
+ .getPropertyValue('color');
29
+ context.stroke();
30
+ }
31
+ };
32
+ exports.drawTimeBlocksOnCanvas = drawTimeBlocksOnCanvas;
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ interface Props {
3
+ canvasRef: React.RefObject<HTMLCanvasElement>;
4
+ value: number;
5
+ }
6
+ export declare const TimelineValue: React.FC<Props>;
7
+ export {};
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.TimelineValue = void 0;
30
+ const react_1 = __importStar(require("react"));
31
+ const styled_components_1 = __importDefault(require("styled-components"));
32
+ const utils_1 = require("../utils/utils");
33
+ const ZoomContext_1 = require("../ZoomContext/ZoomContext");
34
+ const PreValueLine = styled_components_1.default.span `
35
+ position: absolute;
36
+ left: 0;
37
+ height: 100%;
38
+ `;
39
+ const ValueLine = styled_components_1.default.span `
40
+ position: absolute;
41
+ width: 1px;
42
+ height: 100%;
43
+ background-color: #575757;
44
+ `;
45
+ const PostValueLine = styled_components_1.default.span `
46
+ position: absolute;
47
+ height: 100%;
48
+ `;
49
+ const TimelineValue = ({ canvasRef, value }) => {
50
+ const preValueLineRef = (0, react_1.useRef)(null);
51
+ const valueLineRef = (0, react_1.useRef)(null);
52
+ const postValueLineRef = (0, react_1.useRef)(null);
53
+ const zoomContextValue = (0, react_1.useContext)(ZoomContext_1.ZoomContext);
54
+ const showValueLine = (0, react_1.useCallback)(() => {
55
+ const canvas = canvasRef.current;
56
+ const preValueLineElement = preValueLineRef.current;
57
+ const valueLineElement = valueLineRef.current;
58
+ const postValueLineElement = postValueLineRef.current;
59
+ const valueLineWidth = (0, utils_1.getComputedElementWidth)(valueLineElement);
60
+ const linePosition = (0, utils_1.secondsToPixel)(zoomContextValue, value);
61
+ const valueLinePositionBeginningConsideringWidth = linePosition - valueLineWidth / 2;
62
+ // configure preValueLineElement
63
+ preValueLineElement.style.width = (0, utils_1.numberToPxString)(valueLinePositionBeginningConsideringWidth);
64
+ // configure valueLineElement
65
+ valueLineElement.style.left = (0, utils_1.numberToPxString)(valueLinePositionBeginningConsideringWidth);
66
+ // configure postValueLineElement
67
+ const postValueLinePosition = valueLinePositionBeginningConsideringWidth + valueLineWidth;
68
+ const postValueLineWidth = canvas.width - postValueLinePosition;
69
+ postValueLineElement.style.left = (0, utils_1.numberToPxString)(postValueLinePosition);
70
+ postValueLineElement.style.width = (0, utils_1.numberToPxString)(postValueLineWidth);
71
+ }, [canvasRef, value, zoomContextValue]);
72
+ (0, react_1.useEffect)(() => {
73
+ showValueLine();
74
+ }, [showValueLine]);
75
+ return (react_1.default.createElement(react_1.default.Fragment, null,
76
+ react_1.default.createElement(PreValueLine, { ref: preValueLineRef, className: 'media-timeline-pre-value-line' }),
77
+ react_1.default.createElement(ValueLine, { ref: valueLineRef, className: 'media-timeline-value-line' }),
78
+ react_1.default.createElement(PostValueLine, { ref: postValueLineRef, className: 'media-timeline-post-value-line' })));
79
+ };
80
+ exports.TimelineValue = TimelineValue;
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ export type ZoomContextType = {
3
+ blockOffset: number;
4
+ pixelsInSecond: number;
5
+ timelineWrapperWidth: number;
6
+ };
7
+ export declare const ZoomContext: React.Context<ZoomContextType>;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ZoomContext = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ exports.ZoomContext = react_1.default.createContext(null);
@@ -1,5 +1,7 @@
1
- import { ZoomContextType } from '../index';
1
+ import { ZoomContextType } from '../ZoomContext/ZoomContext';
2
2
  export declare const secondsToPixel: (zoomContextValue: ZoomContextType, seconds: number) => number;
3
3
  export declare const pixelToSeconds: (zoomContextValue: ZoomContextType, pixel: number) => number;
4
4
  export declare const getComputedElementWidth: (element: HTMLElement) => number;
5
5
  export declare const numberToPxString: (number: number) => string;
6
+ export declare const getTimelineWrapperWidth: (timeLineContainerWidth: number, zoomLevel: number) => number;
7
+ export declare function getBlockOffsetForZoomLevel(zoomLevel: number, duration: number, timelineWrapperWidth: number): number;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.numberToPxString = exports.getComputedElementWidth = exports.pixelToSeconds = exports.secondsToPixel = void 0;
3
+ exports.getBlockOffsetForZoomLevel = exports.getTimelineWrapperWidth = exports.numberToPxString = exports.getComputedElementWidth = exports.pixelToSeconds = exports.secondsToPixel = void 0;
4
4
  const secondsToPixel = (zoomContextValue, seconds) => {
5
5
  return zoomContextValue.pixelsInSecond * seconds;
6
6
  };
@@ -22,3 +22,16 @@ const numberToPxString = (number) => {
22
22
  return `${number}px`;
23
23
  };
24
24
  exports.numberToPxString = numberToPxString;
25
+ const getTimelineWrapperWidth = (timeLineContainerWidth, zoomLevel) => {
26
+ return timeLineContainerWidth * Math.pow(1.25, zoomLevel) - 2; //-2px to account for the border
27
+ };
28
+ exports.getTimelineWrapperWidth = getTimelineWrapperWidth;
29
+ function getBlockOffsetForZoomLevel(zoomLevel, duration, timelineWrapperWidth) {
30
+ const offsets = [10, 5, 5, 5, 2, 2, 2, 2, 2, 1];
31
+ let optimalOffset = offsets[zoomLevel];
32
+ while ((duration / optimalOffset) * 40 > timelineWrapperWidth) {
33
+ optimalOffset = optimalOffset * 1.25;
34
+ }
35
+ return Math.ceil(optimalOffset);
36
+ }
37
+ exports.getBlockOffsetForZoomLevel = getBlockOffsetForZoomLevel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ldelia/react-media",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "A React components collection for media-related features.",
5
5
  "private": false,
6
6
  "keywords": [
@@ -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,
@@ -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>
@@ -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,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,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
+ };
@@ -1,26 +1,11 @@
1
1
  import React, { useCallback, useContext, useEffect, useRef } from 'react';
2
2
  import styled from 'styled-components';
3
-
4
- import { ZoomContext, ZoomContextType } from './index';
5
3
  import {
6
4
  getComputedElementWidth,
7
5
  numberToPxString,
8
6
  secondsToPixel,
9
- } from './utils/utils';
10
-
11
- export interface VaLueLineCanvasProps {
12
- blockStartingTimes: number[];
13
- value: number;
14
- }
15
-
16
- const OverlayCanvas = styled.canvas`
17
- position: absolute;
18
- top: 0;
19
- left: 0;
20
- width: 100%;
21
- height: 100%;
22
- color: #c9c9c9;
23
- `;
7
+ } from '../utils/utils';
8
+ import { ZoomContext, ZoomContextType } from '../ZoomContext/ZoomContext';
24
9
 
25
10
  const PreValueLine = styled.span`
26
11
  position: absolute;
@@ -40,45 +25,18 @@ const PostValueLine = styled.span`
40
25
  height: 100%;
41
26
  `;
42
27
 
43
- const VaLueLineCanvas: React.FC<VaLueLineCanvasProps> = ({
44
- blockStartingTimes = [],
45
- value,
46
- }) => {
47
- const canvasRef = useRef(null);
28
+ interface Props {
29
+ canvasRef: React.RefObject<HTMLCanvasElement>;
30
+ value: number;
31
+ }
48
32
 
33
+ export const TimelineValue: React.FC<Props> = ({ canvasRef, value }) => {
49
34
  const preValueLineRef = useRef(null);
50
35
  const valueLineRef = useRef(null);
51
36
  const postValueLineRef = useRef(null);
52
37
 
53
38
  const zoomContextValue: ZoomContextType = useContext(ZoomContext);
54
39
 
55
- const showBlocks = useCallback(
56
- (canvas: HTMLCanvasElement) => {
57
- const blockHeight = 20;
58
- const context: CanvasRenderingContext2D = canvas.getContext('2d')!;
59
-
60
- for (const blockStartingTime of blockStartingTimes) {
61
- const x0Pixel = secondsToPixel(zoomContextValue, blockStartingTime);
62
- const x1Pixel = secondsToPixel(
63
- zoomContextValue,
64
- blockStartingTime + zoomContextValue.blockOffset,
65
- );
66
-
67
- context.beginPath();
68
- context.moveTo(x0Pixel, canvas.height);
69
- context.lineTo(x0Pixel, canvas.height - blockHeight);
70
- context.lineTo(x1Pixel, canvas.height - blockHeight);
71
- context.lineTo(x1Pixel, canvas.height);
72
-
73
- context.strokeStyle = window
74
- .getComputedStyle(canvas)
75
- .getPropertyValue('color');
76
- context.stroke();
77
- }
78
- },
79
- [blockStartingTimes, zoomContextValue],
80
- );
81
-
82
40
  const showValueLine = useCallback(() => {
83
41
  const canvas: HTMLCanvasElement = canvasRef.current!;
84
42
 
@@ -107,26 +65,14 @@ const VaLueLineCanvas: React.FC<VaLueLineCanvasProps> = ({
107
65
  const postValueLineWidth = canvas.width - postValueLinePosition;
108
66
  postValueLineElement.style.left = numberToPxString(postValueLinePosition);
109
67
  postValueLineElement.style.width = numberToPxString(postValueLineWidth);
110
- }, [value, zoomContextValue]);
68
+ }, [canvasRef, value, zoomContextValue]);
111
69
 
112
70
  useEffect(() => {
113
- const canvas: HTMLCanvasElement = canvasRef.current!;
114
-
115
- // https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
116
- canvas.width = canvas.offsetWidth;
117
- canvas.height = canvas.offsetHeight;
118
-
119
- showBlocks(canvas);
120
71
  showValueLine();
121
- }, [showBlocks, showValueLine]);
72
+ }, [showValueLine]);
122
73
 
123
74
  return (
124
75
  <>
125
- <OverlayCanvas
126
- ref={canvasRef}
127
- className={'media-timeline-value-line-canvas'}
128
- />
129
-
130
76
  <PreValueLine
131
77
  ref={preValueLineRef}
132
78
  className={'media-timeline-pre-value-line'}
@@ -139,4 +85,3 @@ const VaLueLineCanvas: React.FC<VaLueLineCanvasProps> = ({
139
85
  </>
140
86
  );
141
87
  };
142
- export default VaLueLineCanvas;
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+
3
+ export type ZoomContextType = {
4
+ blockOffset: number;
5
+ pixelsInSecond: number;
6
+ timelineWrapperWidth: number;
7
+ };
8
+ export const ZoomContext = React.createContext<ZoomContextType>(
9
+ null as unknown as ZoomContextType,
10
+ );
@@ -1,4 +1,4 @@
1
- import { ZoomContextType } from '../index';
1
+ import { ZoomContextType } from '../ZoomContext/ZoomContext';
2
2
 
3
3
  export const secondsToPixel = (
4
4
  zoomContextValue: ZoomContextType,
@@ -26,3 +26,24 @@ export const getComputedElementWidth = (element: HTMLElement): number => {
26
26
  export const numberToPxString = (number: number): string => {
27
27
  return `${number}px`;
28
28
  };
29
+
30
+ export const getTimelineWrapperWidth = (
31
+ timeLineContainerWidth: number,
32
+ zoomLevel: number,
33
+ ): number => {
34
+ return timeLineContainerWidth * Math.pow(1.25, zoomLevel) - 2; //-2px to account for the border
35
+ };
36
+
37
+ export function getBlockOffsetForZoomLevel(
38
+ zoomLevel: number,
39
+ duration: number,
40
+ timelineWrapperWidth: number,
41
+ ): number {
42
+ const offsets = [10, 5, 5, 5, 2, 2, 2, 2, 2, 1];
43
+
44
+ let optimalOffset = offsets[zoomLevel];
45
+ while ((duration / optimalOffset) * 40 > timelineWrapperWidth) {
46
+ optimalOffset = optimalOffset * 1.25;
47
+ }
48
+ return Math.ceil(optimalOffset);
49
+ }