@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
@@ -1,12 +1,34 @@
1
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
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const react_1 = __importDefault(require("react"));
29
+ const react_1 = __importStar(require("react"));
7
30
  const styled_components_1 = __importDefault(require("styled-components"));
8
31
  const index_1 = require("./index");
9
- const react_2 = require("react");
10
32
  const utils_1 = require("./utils/utils");
11
33
  const OverlayCanvas = styled_components_1.default.canvas `
12
34
  position: absolute;
@@ -16,17 +38,28 @@ const OverlayCanvas = styled_components_1.default.canvas `
16
38
  height: 100%;
17
39
  color: #c9c9c9;
18
40
  `;
41
+ const PreValueLine = styled_components_1.default.span `
42
+ position: absolute;
43
+ left: 0;
44
+ height: 100%;
45
+ `;
19
46
  const ValueLine = styled_components_1.default.span `
20
47
  position: absolute;
21
48
  width: 1px;
22
49
  height: 100%;
23
50
  background-color: #575757;
24
51
  `;
52
+ const PostValueLine = styled_components_1.default.span `
53
+ position: absolute;
54
+ height: 100%;
55
+ `;
25
56
  const VaLueLineCanvas = ({ blockStartingTimes = [], value, }) => {
26
- const canvasRef = (0, react_2.useRef)(null);
27
- const valueLineRef = (0, react_2.useRef)(null);
28
- const zoomContextValue = (0, react_2.useContext)(index_1.ZoomContext);
29
- const showBlocks = (0, react_2.useCallback)((canvas) => {
57
+ const canvasRef = (0, react_1.useRef)(null);
58
+ const preValueLineRef = (0, react_1.useRef)(null);
59
+ const valueLineRef = (0, react_1.useRef)(null);
60
+ const postValueLineRef = (0, react_1.useRef)(null);
61
+ const zoomContextValue = (0, react_1.useContext)(index_1.ZoomContext);
62
+ const showBlocks = (0, react_1.useCallback)((canvas) => {
30
63
  const blockHeight = 20;
31
64
  const context = canvas.getContext('2d');
32
65
  for (const blockStartingTime of blockStartingTimes) {
@@ -43,18 +76,25 @@ const VaLueLineCanvas = ({ blockStartingTimes = [], value, }) => {
43
76
  context.stroke();
44
77
  }
45
78
  }, [blockStartingTimes, zoomContextValue]);
46
- const showValueLine = (0, react_2.useCallback)(() => {
47
- const linePosition = (0, utils_1.secondsToPixel)(zoomContextValue, value);
79
+ const showValueLine = (0, react_1.useCallback)(() => {
80
+ const canvas = canvasRef.current;
81
+ const preValueLineElement = preValueLineRef.current;
48
82
  const valueLineElement = valueLineRef.current;
49
- const elementWidthCSSProperty = window
50
- .getComputedStyle(valueLineElement)
51
- .getPropertyValue('width')
52
- .replace(/[^-\d]/g, '');
53
- const elementWidth = parseInt(elementWidthCSSProperty);
54
- const linePositionAtValueLineMiddle = linePosition - elementWidth / 2;
55
- valueLineElement.style.left = linePositionAtValueLineMiddle + 'px';
83
+ const postValueLineElement = postValueLineRef.current;
84
+ const valueLineWidth = (0, utils_1.getComputedElementWidth)(valueLineElement);
85
+ const linePosition = (0, utils_1.secondsToPixel)(zoomContextValue, value);
86
+ const valueLinePositionBeginningConsideringWidth = linePosition - valueLineWidth / 2;
87
+ // configure preValueLineElement
88
+ preValueLineElement.style.width = (0, utils_1.numberToPxString)(valueLinePositionBeginningConsideringWidth);
89
+ // configure valueLineElement
90
+ valueLineElement.style.left = (0, utils_1.numberToPxString)(valueLinePositionBeginningConsideringWidth);
91
+ // configure postValueLineElement
92
+ const postValueLinePosition = valueLinePositionBeginningConsideringWidth + valueLineWidth;
93
+ const postValueLineWidth = canvas.width - postValueLinePosition;
94
+ postValueLineElement.style.left = (0, utils_1.numberToPxString)(postValueLinePosition);
95
+ postValueLineElement.style.width = (0, utils_1.numberToPxString)(postValueLineWidth);
56
96
  }, [value, zoomContextValue]);
57
- (0, react_2.useEffect)(() => {
97
+ (0, react_1.useEffect)(() => {
58
98
  const canvas = canvasRef.current;
59
99
  // https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
60
100
  canvas.width = canvas.offsetWidth;
@@ -64,6 +104,8 @@ const VaLueLineCanvas = ({ blockStartingTimes = [], value, }) => {
64
104
  }, [showBlocks, showValueLine]);
65
105
  return (react_1.default.createElement(react_1.default.Fragment, null,
66
106
  react_1.default.createElement(OverlayCanvas, { ref: canvasRef, className: 'media-timeline-value-line-canvas' }),
67
- react_1.default.createElement(ValueLine, { ref: valueLineRef, className: 'media-timeline-value-line' })));
107
+ react_1.default.createElement(PreValueLine, { ref: preValueLineRef, className: 'media-timeline-pre-value-line' }),
108
+ react_1.default.createElement(ValueLine, { ref: valueLineRef, className: 'media-timeline-value-line' }),
109
+ react_1.default.createElement(PostValueLine, { ref: postValueLineRef, className: 'media-timeline-post-value-line' })));
68
110
  };
69
111
  exports.default = VaLueLineCanvas;
@@ -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,3 +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
+ export declare const getComputedElementWidth: (element: HTMLElement) => number;
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.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
  };
@@ -10,3 +10,28 @@ const pixelToSeconds = (zoomContextValue, pixel) => {
10
10
  return Math.round((seconds + Number.EPSILON) * 100) / 100;
11
11
  };
12
12
  exports.pixelToSeconds = pixelToSeconds;
13
+ const getComputedElementWidth = (element) => {
14
+ const elementWidthCSSProperty = window
15
+ .getComputedStyle(element)
16
+ .getPropertyValue('width')
17
+ .replace(/[^-\d]/g, '');
18
+ return parseInt(elementWidthCSSProperty);
19
+ };
20
+ exports.getComputedElementWidth = getComputedElementWidth;
21
+ const numberToPxString = (number) => {
22
+ return `${number}px`;
23
+ };
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;
@@ -1,9 +1,9 @@
1
1
  import { TimelineProps } from '../components/timeline';
2
- import { Meta } from '@storybook/react';
3
2
  import './timeline.stories.custom.css';
4
- declare const _default: Meta;
3
+ declare const _default: import("@storybook/types").ComponentAnnotations<import("@storybook/react/dist/types-bf5e6555").R, import("@storybook/types").Args>;
5
4
  export default _default;
6
- export declare const Default: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react").ReactRenderer, TimelineProps>;
7
- export declare const WithSelectedRange: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react").ReactRenderer, TimelineProps>;
8
- export declare const WithCustomClassName: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react").ReactRenderer, TimelineProps>;
9
- export declare const WithOutTimeBlocks: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react").ReactRenderer, TimelineProps>;
5
+ export declare const Default: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-bf5e6555").R, TimelineProps>;
6
+ export declare const WithSelectedRange: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-bf5e6555").R, TimelineProps>;
7
+ export declare const WithCustomClassName: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-bf5e6555").R, TimelineProps>;
8
+ export declare const WithoutTimeBlocks: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-bf5e6555").R, TimelineProps>;
9
+ export declare const Minimalist: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-bf5e6555").R, TimelineProps>;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WithOutTimeBlocks = exports.WithCustomClassName = exports.WithSelectedRange = exports.Default = void 0;
6
+ exports.Minimalist = exports.WithoutTimeBlocks = exports.WithCustomClassName = exports.WithSelectedRange = exports.Default = void 0;
7
7
  const react_1 = __importDefault(require("react"));
8
8
  const timeline_1 = require("../components/timeline");
9
9
  const styled_components_1 = __importDefault(require("styled-components"));
@@ -41,12 +41,20 @@ exports.WithCustomClassName.args = {
41
41
  duration: 305,
42
42
  value: 31,
43
43
  zoomLevel: 0,
44
- className: 'this-class-redefines-values',
44
+ className: 'this-class-redefines-styles',
45
45
  };
46
- exports.WithOutTimeBlocks = Template.bind({});
47
- exports.WithOutTimeBlocks.args = {
46
+ exports.WithoutTimeBlocks = Template.bind({});
47
+ exports.WithoutTimeBlocks.args = {
48
48
  duration: 305,
49
49
  value: 31,
50
50
  zoomLevel: 0,
51
51
  withTimeBlocks: false,
52
52
  };
53
+ exports.Minimalist = Template.bind({});
54
+ exports.Minimalist.args = {
55
+ duration: 305,
56
+ value: 31,
57
+ zoomLevel: 0,
58
+ withTimeBlocks: false,
59
+ className: 'minimalist',
60
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ldelia/react-media",
3
- "version": "0.2.6",
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;
@@ -18,6 +24,10 @@ const TimelineContainer = styled.div`
18
24
  const TimelineWrapper = styled.div`
19
25
  position: absolute;
20
26
  height: 100%;
27
+ display: flex;
28
+ flex-direction: row;
29
+ align-items: center;
30
+ overflow: hidden;
21
31
  `;
22
32
 
23
33
  export interface TimelineProps {
@@ -31,15 +41,6 @@ export interface TimelineProps {
31
41
  onRangeChange?: (value: number[]) => void;
32
42
  }
33
43
 
34
- export type ZoomContextType = {
35
- blockOffset: number;
36
- pixelsInSecond: number;
37
- };
38
- export const ZoomContext = React.createContext<ZoomContextType>({
39
- blockOffset: 0,
40
- pixelsInSecond: 0,
41
- });
42
-
43
44
  export const Timeline: React.FC<TimelineProps> = ({
44
45
  duration,
45
46
  value,
@@ -50,7 +51,14 @@ export const Timeline: React.FC<TimelineProps> = ({
50
51
  onRangeChange = () => {},
51
52
  className = '',
52
53
  }) => {
53
- 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
+ });
54
62
 
55
63
  let zoomLevelValue = zoomLevel ? zoomLevel : 0;
56
64
  if (zoomLevelValue < 0 || zoomLevelValue >= zoomLevelConfigurations.length) {
@@ -79,46 +87,51 @@ export const Timeline: React.FC<TimelineProps> = ({
79
87
  console.warn('The selected range is inconsistent.');
80
88
  }
81
89
 
82
- const zoomParams = useMemo(() => {
83
- return {
84
- blockOffset: zoomLevelConfigurations[zoomLevelValue][0],
85
- pixelsInSecond: zoomLevelConfigurations[zoomLevelValue][1],
86
- };
87
- }, [zoomLevelValue]);
90
+ useEffect(() => {
91
+ if (!timeLineContainerRef.current) return;
88
92
 
89
- let blockStartingTimes = [0];
90
- const blockCounts: number = Math.ceil(duration / zoomParams.blockOffset);
91
- for (let i: number = 1; i < blockCounts; i++) {
92
- blockStartingTimes.push(
93
- blockStartingTimes[blockStartingTimes.length - 1] +
94
- zoomParams.blockOffset,
93
+ const timelineWrapperWidth = getTimelineWrapperWidth(
94
+ timeLineContainerRef.current.offsetWidth,
95
+ zoomLevelValue,
95
96
  );
96
- }
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]);
97
108
 
98
109
  useEffect(() => {
99
110
  const timeLineWrapper: HTMLElement = timeLineContainerRef.current!;
100
- const scrollPosition: number = value * zoomParams.pixelsInSecond - 300;
111
+ const scrollPosition: number =
112
+ value * zoomContextValue.pixelsInSecond - 300;
101
113
  timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
102
- }, [value, zoomParams]);
114
+ }, [duration, value, zoomContextValue]);
103
115
 
104
116
  return (
105
117
  <TimelineContainer ref={timeLineContainerRef} className={className}>
106
118
  <TimelineWrapper
107
- style={{ width: duration * zoomParams.pixelsInSecond + 'px' }}
119
+ style={{
120
+ width: numberToPxString(zoomContextValue.timelineWrapperWidth),
121
+ }}
108
122
  >
109
- <ZoomContext.Provider value={zoomParams}>
110
- <VaLueLineCanvas
111
- blockStartingTimes={withTimeBlocks ? blockStartingTimes : []}
112
- value={value}
123
+ <ZoomContext.Provider value={zoomContextValue}>
124
+ <TimelineCanvas
125
+ ref={canvasRef}
126
+ duration={duration}
127
+ withTimeBlocks={withTimeBlocks}
113
128
  />
129
+ <TimelineValue value={value} canvasRef={canvasRef} />
114
130
  <RangeSelectorCanvas
115
131
  selectedRange={selectedRange}
116
132
  onChange={onChange}
117
133
  onRangeChange={onRangeChange}
118
134
  />
119
- {withTimeBlocks && (
120
- <TickTimeCollectionDisplay tickTimes={blockStartingTimes} />
121
- )}
122
135
  </ZoomContext.Provider>
123
136
  </TimelineWrapper>
124
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
+ };
@@ -0,0 +1,87 @@
1
+ import React, { useCallback, useContext, useEffect, useRef } from 'react';
2
+ import styled from 'styled-components';
3
+ import {
4
+ getComputedElementWidth,
5
+ numberToPxString,
6
+ secondsToPixel,
7
+ } from '../utils/utils';
8
+ import { ZoomContext, ZoomContextType } from '../ZoomContext/ZoomContext';
9
+
10
+ const PreValueLine = styled.span`
11
+ position: absolute;
12
+ left: 0;
13
+ height: 100%;
14
+ `;
15
+
16
+ const ValueLine = styled.span`
17
+ position: absolute;
18
+ width: 1px;
19
+ height: 100%;
20
+ background-color: #575757;
21
+ `;
22
+
23
+ const PostValueLine = styled.span`
24
+ position: absolute;
25
+ height: 100%;
26
+ `;
27
+
28
+ interface Props {
29
+ canvasRef: React.RefObject<HTMLCanvasElement>;
30
+ value: number;
31
+ }
32
+
33
+ export const TimelineValue: React.FC<Props> = ({ canvasRef, value }) => {
34
+ const preValueLineRef = useRef(null);
35
+ const valueLineRef = useRef(null);
36
+ const postValueLineRef = useRef(null);
37
+
38
+ const zoomContextValue: ZoomContextType = useContext(ZoomContext);
39
+
40
+ const showValueLine = useCallback(() => {
41
+ const canvas: HTMLCanvasElement = canvasRef.current!;
42
+
43
+ const preValueLineElement: HTMLElement = preValueLineRef.current!;
44
+ const valueLineElement: HTMLElement = valueLineRef.current!;
45
+ const postValueLineElement: HTMLElement = postValueLineRef.current!;
46
+
47
+ const valueLineWidth: number = getComputedElementWidth(valueLineElement);
48
+ const linePosition: number = secondsToPixel(zoomContextValue, value);
49
+ const valueLinePositionBeginningConsideringWidth: number =
50
+ linePosition - valueLineWidth / 2;
51
+
52
+ // configure preValueLineElement
53
+ preValueLineElement.style.width = numberToPxString(
54
+ valueLinePositionBeginningConsideringWidth,
55
+ );
56
+
57
+ // configure valueLineElement
58
+ valueLineElement.style.left = numberToPxString(
59
+ valueLinePositionBeginningConsideringWidth,
60
+ );
61
+
62
+ // configure postValueLineElement
63
+ const postValueLinePosition =
64
+ valueLinePositionBeginningConsideringWidth + valueLineWidth;
65
+ const postValueLineWidth = canvas.width - postValueLinePosition;
66
+ postValueLineElement.style.left = numberToPxString(postValueLinePosition);
67
+ postValueLineElement.style.width = numberToPxString(postValueLineWidth);
68
+ }, [canvasRef, value, zoomContextValue]);
69
+
70
+ useEffect(() => {
71
+ showValueLine();
72
+ }, [showValueLine]);
73
+
74
+ return (
75
+ <>
76
+ <PreValueLine
77
+ ref={preValueLineRef}
78
+ className={'media-timeline-pre-value-line'}
79
+ />
80
+ <ValueLine ref={valueLineRef} className={'media-timeline-value-line'} />
81
+ <PostValueLine
82
+ ref={postValueLineRef}
83
+ className={'media-timeline-post-value-line'}
84
+ />
85
+ </>
86
+ );
87
+ };
@@ -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
+ );