@ldelia/react-media 0.2.6 → 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 (34) 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 +35 -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/VaLueLineCanvas.js +59 -17
  16. package/dist/components/timeline/ZoomContext/ZoomContext.d.ts +7 -0
  17. package/dist/components/timeline/ZoomContext/ZoomContext.js +8 -0
  18. package/dist/components/timeline/utils/utils.d.ts +5 -1
  19. package/dist/components/timeline/utils/utils.js +26 -1
  20. package/dist/stories/timeline.stories.d.ts +6 -6
  21. package/dist/stories/timeline.stories.js +12 -4
  22. package/package.json +1 -1
  23. package/src/components/timeline/{RangeSelectorCanvas.tsx → RangeSelectorCanvas/RangeSelectorCanvas.tsx} +9 -11
  24. package/src/components/timeline/Timeline.tsx +50 -37
  25. package/src/components/timeline/{TickTimeCollectionDisplay.tsx → TimelineCanvas/TickTimeCollectionDisplay.tsx} +5 -5
  26. package/src/components/timeline/TimelineCanvas/TimelineCanvas.tsx +79 -0
  27. package/src/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.ts +43 -0
  28. package/src/components/timeline/TimelineValue/TimelineValue.tsx +87 -0
  29. package/src/components/timeline/ZoomContext/ZoomContext.ts +10 -0
  30. package/src/components/timeline/utils/utils.ts +34 -1
  31. package/src/stories/timeline.stories.custom.css +27 -1
  32. package/src/stories/timeline.stories.tsx +12 -3
  33. package/src/components/timeline/VaLueLineCanvas.tsx +0 -101
  34. /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;
@@ -46,13 +48,19 @@ const TimelineContainer = styled_components_1.default.div `
46
48
  const TimelineWrapper = styled_components_1.default.div `
47
49
  position: absolute;
48
50
  height: 100%;
51
+ display: flex;
52
+ flex-direction: row;
53
+ align-items: center;
54
+ overflow: hidden;
49
55
  `;
50
- exports.ZoomContext = react_1.default.createContext({
51
- blockOffset: 0,
52
- pixelsInSecond: 0,
53
- });
54
56
  const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], withTimeBlocks = true, onChange = () => { }, onRangeChange = () => { }, className = '', }) => {
55
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
+ });
56
64
  let zoomLevelValue = zoomLevel ? zoomLevel : 0;
57
65
  if (zoomLevelValue < 0 || zoomLevelValue >= constants_1.zoomLevelConfigurations.length) {
58
66
  console.warn('Invalid value for property zoomLevel.');
@@ -72,28 +80,28 @@ const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], withTime
72
80
  selectedRange = [];
73
81
  console.warn('The selected range is inconsistent.');
74
82
  }
75
- const zoomParams = (0, react_1.useMemo)(() => {
76
- return {
77
- blockOffset: constants_1.zoomLevelConfigurations[zoomLevelValue][0],
78
- pixelsInSecond: constants_1.zoomLevelConfigurations[zoomLevelValue][1],
79
- };
80
- }, [zoomLevelValue]);
81
- let blockStartingTimes = [0];
82
- const blockCounts = Math.ceil(duration / zoomParams.blockOffset);
83
- for (let i = 1; i < blockCounts; i++) {
84
- blockStartingTimes.push(blockStartingTimes[blockStartingTimes.length - 1] +
85
- zoomParams.blockOffset);
86
- }
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]);
87
93
  (0, react_1.useEffect)(() => {
88
94
  const timeLineWrapper = timeLineContainerRef.current;
89
- const scrollPosition = value * zoomParams.pixelsInSecond - 300;
95
+ const scrollPosition = value * zoomContextValue.pixelsInSecond - 300;
90
96
  timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
91
- }, [value, zoomParams]);
97
+ }, [duration, value, zoomContextValue]);
92
98
  return (react_1.default.createElement(TimelineContainer, { ref: timeLineContainerRef, className: className },
93
- react_1.default.createElement(TimelineWrapper, { style: { width: duration * zoomParams.pixelsInSecond + 'px' } },
94
- react_1.default.createElement(exports.ZoomContext.Provider, { value: zoomParams },
95
- react_1.default.createElement(VaLueLineCanvas_1.default, { blockStartingTimes: withTimeBlocks ? blockStartingTimes : [], value: value }),
96
- react_1.default.createElement(RangeSelectorCanvas_1.default, { selectedRange: selectedRange, onChange: onChange, onRangeChange: onRangeChange }),
97
- 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 })))));
98
106
  };
99
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;