@ldelia/react-media 0.2.3 → 0.2.5
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.
- package/README.md +11 -1
- package/dist/components/timeline/Timeline.d.ts +18 -0
- package/dist/components/timeline/Timeline.js +85 -0
- package/dist/components/timeline/index.d.ts +1 -16
- package/dist/components/timeline/index.js +14 -74
- package/dist/stories/timeline.stories.d.ts +3 -0
- package/dist/stories/timeline.stories.js +16 -1
- package/package.json +4 -2
- package/src/components/timeline/Timeline.css +15 -0
- package/src/components/timeline/Timeline.tsx +116 -0
- package/src/components/timeline/index.tsx +1 -123
- package/src/stories/timeline.stories.custom.css +3 -0
- package/src/stories/timeline.stories.tsx +19 -0
package/README.md
CHANGED
|
@@ -23,4 +23,14 @@ Make sure storybook is already installed by running npx storybook@latest init
|
|
|
23
23
|
Launch the test suite collection:
|
|
24
24
|
|
|
25
25
|
##### `npm test`
|
|
26
|
-
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Publishing on npm
|
|
29
|
+
|
|
30
|
+
Build the project
|
|
31
|
+
|
|
32
|
+
##### `npm run build`
|
|
33
|
+
|
|
34
|
+
Publish the project
|
|
35
|
+
|
|
36
|
+
##### `npm publish`
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import './Timeline.css';
|
|
3
|
+
export interface TimelineProps {
|
|
4
|
+
duration: number;
|
|
5
|
+
value: number;
|
|
6
|
+
zoomLevel?: number;
|
|
7
|
+
className?: string;
|
|
8
|
+
selectedRange?: number[];
|
|
9
|
+
withTimeBlocks?: boolean;
|
|
10
|
+
onChange?: (value: number) => void;
|
|
11
|
+
onRangeChange?: (value: number[]) => void;
|
|
12
|
+
}
|
|
13
|
+
export type ZoomContextType = {
|
|
14
|
+
blockOffset: number;
|
|
15
|
+
pixelsInSecond: number;
|
|
16
|
+
};
|
|
17
|
+
export declare const ZoomContext: React.Context<ZoomContextType>;
|
|
18
|
+
export declare const Timeline: React.FC<TimelineProps>;
|
|
@@ -0,0 +1,85 @@
|
|
|
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.Timeline = exports.ZoomContext = void 0;
|
|
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"));
|
|
34
|
+
const constants_1 = require("./constants");
|
|
35
|
+
require("./Timeline.css");
|
|
36
|
+
exports.ZoomContext = react_1.default.createContext({
|
|
37
|
+
blockOffset: 0,
|
|
38
|
+
pixelsInSecond: 0,
|
|
39
|
+
});
|
|
40
|
+
const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], withTimeBlocks = true, onChange = () => { }, onRangeChange = () => { }, className = '', }) => {
|
|
41
|
+
const timeLineContainerRef = (0, react_1.useRef)(null);
|
|
42
|
+
let zoomLevelValue = zoomLevel ? zoomLevel : 0;
|
|
43
|
+
if (zoomLevelValue < 0 || zoomLevelValue >= constants_1.zoomLevelConfigurations.length) {
|
|
44
|
+
console.warn('Invalid value for property zoomLevel.');
|
|
45
|
+
zoomLevelValue = 0;
|
|
46
|
+
}
|
|
47
|
+
if (value < 0 || value > duration) {
|
|
48
|
+
console.warn('Invalid value.');
|
|
49
|
+
}
|
|
50
|
+
if (!(selectedRange.length === 0 || selectedRange.length === 2)) {
|
|
51
|
+
selectedRange = [];
|
|
52
|
+
console.warn('The selected range must contain only two values.');
|
|
53
|
+
}
|
|
54
|
+
if (selectedRange.length === 2 &&
|
|
55
|
+
!(0 <= selectedRange[0] &&
|
|
56
|
+
selectedRange[0] < selectedRange[1] &&
|
|
57
|
+
selectedRange[1] <= duration)) {
|
|
58
|
+
selectedRange = [];
|
|
59
|
+
console.warn('The selected range is inconsistent.');
|
|
60
|
+
}
|
|
61
|
+
const zoomParams = (0, react_1.useMemo)(() => {
|
|
62
|
+
return {
|
|
63
|
+
blockOffset: constants_1.zoomLevelConfigurations[zoomLevelValue][0],
|
|
64
|
+
pixelsInSecond: constants_1.zoomLevelConfigurations[zoomLevelValue][1],
|
|
65
|
+
};
|
|
66
|
+
}, [zoomLevelValue]);
|
|
67
|
+
let blockStartingTimes = [0];
|
|
68
|
+
const blockCounts = Math.ceil(duration / zoomParams.blockOffset);
|
|
69
|
+
for (let i = 1; i < blockCounts; i++) {
|
|
70
|
+
blockStartingTimes.push(blockStartingTimes[blockStartingTimes.length - 1] +
|
|
71
|
+
zoomParams.blockOffset);
|
|
72
|
+
}
|
|
73
|
+
(0, react_1.useEffect)(() => {
|
|
74
|
+
const timeLineWrapper = timeLineContainerRef.current;
|
|
75
|
+
const scrollPosition = value * zoomParams.pixelsInSecond - 300;
|
|
76
|
+
timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
|
|
77
|
+
}, [value, zoomParams]);
|
|
78
|
+
return (react_1.default.createElement("div", { ref: timeLineContainerRef, className: `timeline-container ${className}` },
|
|
79
|
+
react_1.default.createElement("div", { className: 'timeline-wrapper', style: { width: duration * zoomParams.pixelsInSecond + 'px' } },
|
|
80
|
+
react_1.default.createElement(exports.ZoomContext.Provider, { value: zoomParams },
|
|
81
|
+
react_1.default.createElement(VaLueLineCanvas_1.default, { blockStartingTimes: withTimeBlocks ? blockStartingTimes : [], value: value }),
|
|
82
|
+
react_1.default.createElement(RangeSelectorCanvas_1.default, { selectedRange: selectedRange, onChange: onChange, onRangeChange: onRangeChange }),
|
|
83
|
+
withTimeBlocks && (react_1.default.createElement(TickTimeCollectionDisplay_1.default, { tickTimes: blockStartingTimes }))))));
|
|
84
|
+
};
|
|
85
|
+
exports.Timeline = Timeline;
|
|
@@ -1,16 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export interface TimelineProps {
|
|
3
|
-
duration: number;
|
|
4
|
-
value: number;
|
|
5
|
-
zoomLevel?: number;
|
|
6
|
-
className?: string;
|
|
7
|
-
selectedRange?: number[];
|
|
8
|
-
onChange?: (value: number) => void;
|
|
9
|
-
onRangeChange?: (value: number[]) => void;
|
|
10
|
-
}
|
|
11
|
-
export type ZoomContextType = {
|
|
12
|
-
blockOffset: number;
|
|
13
|
-
pixelsInSecond: number;
|
|
14
|
-
};
|
|
15
|
-
export declare const ZoomContext: React.Context<ZoomContextType>;
|
|
16
|
-
export declare const Timeline: React.FC<TimelineProps>;
|
|
1
|
+
export * from './Timeline';
|
|
@@ -1,77 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const react_1 = __importDefault(require("react"));
|
|
8
|
-
const styled_components_1 = __importDefault(require("styled-components"));
|
|
9
|
-
const react_2 = require("react");
|
|
10
|
-
const TickTimeCollectionDisplay_1 = __importDefault(require("./TickTimeCollectionDisplay"));
|
|
11
|
-
const VaLueLineCanvas_1 = __importDefault(require("./VaLueLineCanvas"));
|
|
12
|
-
const RangeSelectorCanvas_1 = __importDefault(require("./RangeSelectorCanvas"));
|
|
13
|
-
const constants_1 = require("./constants");
|
|
14
|
-
const TimelineContainer = styled_components_1.default.div `
|
|
15
|
-
background-color: #f0f0f0;
|
|
16
|
-
border: 1px solid #c9c9c9;
|
|
17
|
-
height: 80px;
|
|
18
|
-
width: 100%;
|
|
19
|
-
max-width: 100%;
|
|
20
|
-
overflow-x: auto;
|
|
21
|
-
position: relative;
|
|
22
|
-
display: flex;
|
|
23
|
-
`;
|
|
24
|
-
const TimelineWrapper = styled_components_1.default.div `
|
|
25
|
-
position: absolute;
|
|
26
|
-
height: 100%;
|
|
27
|
-
`;
|
|
28
|
-
exports.ZoomContext = react_1.default.createContext({
|
|
29
|
-
blockOffset: 0,
|
|
30
|
-
pixelsInSecond: 0,
|
|
31
|
-
});
|
|
32
|
-
const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], onChange = () => { }, onRangeChange = () => { }, className = '', }) => {
|
|
33
|
-
const timeLineContainerRef = (0, react_2.useRef)(null);
|
|
34
|
-
let zoomLevelValue = zoomLevel ? zoomLevel : 0;
|
|
35
|
-
if (zoomLevelValue < 0 || zoomLevelValue >= constants_1.zoomLevelConfigurations.length) {
|
|
36
|
-
console.warn('Invalid value for property zoomLevel.');
|
|
37
|
-
zoomLevelValue = 0;
|
|
38
|
-
}
|
|
39
|
-
if (value < 0 || value > duration) {
|
|
40
|
-
console.warn('Invalid value.');
|
|
41
|
-
}
|
|
42
|
-
if (!(selectedRange.length === 0 || selectedRange.length === 2)) {
|
|
43
|
-
selectedRange = [];
|
|
44
|
-
console.warn('The selected range must contain only two values.');
|
|
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]; } };
|
|
45
7
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const zoomParams = (0, react_2.useMemo)(() => {
|
|
54
|
-
return {
|
|
55
|
-
blockOffset: constants_1.zoomLevelConfigurations[zoomLevelValue][0],
|
|
56
|
-
pixelsInSecond: constants_1.zoomLevelConfigurations[zoomLevelValue][1],
|
|
57
|
-
};
|
|
58
|
-
}, [zoomLevelValue]);
|
|
59
|
-
let blockStartingTimes = [0];
|
|
60
|
-
const blockCounts = Math.ceil(duration / zoomParams.blockOffset);
|
|
61
|
-
for (let i = 1; i < blockCounts; i++) {
|
|
62
|
-
blockStartingTimes.push(blockStartingTimes[blockStartingTimes.length - 1] +
|
|
63
|
-
zoomParams.blockOffset);
|
|
64
|
-
}
|
|
65
|
-
(0, react_2.useEffect)(() => {
|
|
66
|
-
const timeLineWrapper = timeLineContainerRef.current;
|
|
67
|
-
const scrollPosition = value * zoomParams.pixelsInSecond - 300;
|
|
68
|
-
timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
|
|
69
|
-
}, [value, zoomParams]);
|
|
70
|
-
return (react_1.default.createElement(TimelineContainer, { ref: timeLineContainerRef, className: className },
|
|
71
|
-
react_1.default.createElement(TimelineWrapper, { style: { width: duration * zoomParams.pixelsInSecond + 'px' } },
|
|
72
|
-
react_1.default.createElement(exports.ZoomContext.Provider, { value: zoomParams },
|
|
73
|
-
react_1.default.createElement(VaLueLineCanvas_1.default, { blockStartingTimes: blockStartingTimes, value: value }),
|
|
74
|
-
react_1.default.createElement(RangeSelectorCanvas_1.default, { selectedRange: selectedRange, onChange: onChange, onRangeChange: onRangeChange }),
|
|
75
|
-
react_1.default.createElement(TickTimeCollectionDisplay_1.default, { tickTimes: blockStartingTimes })))));
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
76
15
|
};
|
|
77
|
-
exports
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Timeline"), exports);
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { TimelineProps } from '../components/timeline';
|
|
2
|
+
import './timeline.stories.custom.css';
|
|
2
3
|
declare const _default: import("@storybook/types").ComponentAnnotations<import("@storybook/react/dist/types-bf5e6555").R, import("@storybook/types").Args>;
|
|
3
4
|
export default _default;
|
|
4
5
|
export declare const Default: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-bf5e6555").R, TimelineProps>;
|
|
5
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>;
|
|
@@ -3,10 +3,11 @@ 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.WithSelectedRange = exports.Default = void 0;
|
|
6
|
+
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"));
|
|
10
|
+
require("./timeline.stories.custom.css");
|
|
10
11
|
const StyledTimeline = (0, styled_components_1.default)(timeline_1.Timeline) `
|
|
11
12
|
.media-timeline-value-line {
|
|
12
13
|
background-color: red;
|
|
@@ -35,3 +36,17 @@ exports.WithSelectedRange.args = {
|
|
|
35
36
|
zoomLevel: 0,
|
|
36
37
|
selectedRange: [20, 30],
|
|
37
38
|
};
|
|
39
|
+
exports.WithCustomClassName = Template.bind({});
|
|
40
|
+
exports.WithCustomClassName.args = {
|
|
41
|
+
duration: 305,
|
|
42
|
+
value: 31,
|
|
43
|
+
zoomLevel: 0,
|
|
44
|
+
className: 'this-class-redefines-values',
|
|
45
|
+
};
|
|
46
|
+
exports.WithOutTimeBlocks = Template.bind({});
|
|
47
|
+
exports.WithOutTimeBlocks.args = {
|
|
48
|
+
duration: 305,
|
|
49
|
+
value: 31,
|
|
50
|
+
zoomLevel: 0,
|
|
51
|
+
withTimeBlocks: false,
|
|
52
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ldelia/react-media",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "A React components collection for media-related features.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
@@ -36,7 +36,9 @@
|
|
|
36
36
|
"plugin:storybook/recommended",
|
|
37
37
|
"prettier"
|
|
38
38
|
],
|
|
39
|
-
"plugins": [
|
|
39
|
+
"plugins": [
|
|
40
|
+
"prettier"
|
|
41
|
+
],
|
|
40
42
|
"rules": {
|
|
41
43
|
"prettier/prettier": "error"
|
|
42
44
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
.timeline-container {
|
|
2
|
+
background-color: #f0f0f0;
|
|
3
|
+
border: 1px solid #c9c9c9;
|
|
4
|
+
height: 80px;
|
|
5
|
+
width: 100%;
|
|
6
|
+
max-width: 100%;
|
|
7
|
+
overflow-x: auto;
|
|
8
|
+
position: relative;
|
|
9
|
+
display: flex;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.timeline-wrapper {
|
|
13
|
+
position: absolute;
|
|
14
|
+
height: 100%;
|
|
15
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import TickTimeCollectionDisplay from './TickTimeCollectionDisplay';
|
|
3
|
+
import VaLueLineCanvas from './VaLueLineCanvas';
|
|
4
|
+
import RangeSelectorCanvas from './RangeSelectorCanvas';
|
|
5
|
+
import { zoomLevelConfigurations } from './constants';
|
|
6
|
+
|
|
7
|
+
import './Timeline.css';
|
|
8
|
+
|
|
9
|
+
export interface TimelineProps {
|
|
10
|
+
duration: number; // duration in seconds
|
|
11
|
+
value: number; // value in seconds
|
|
12
|
+
zoomLevel?: number; //0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
13
|
+
className?: string;
|
|
14
|
+
selectedRange?: number[];
|
|
15
|
+
withTimeBlocks?: boolean;
|
|
16
|
+
onChange?: (value: number) => void;
|
|
17
|
+
onRangeChange?: (value: number[]) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ZoomContextType = {
|
|
21
|
+
blockOffset: number;
|
|
22
|
+
pixelsInSecond: number;
|
|
23
|
+
};
|
|
24
|
+
export const ZoomContext = React.createContext<ZoomContextType>({
|
|
25
|
+
blockOffset: 0,
|
|
26
|
+
pixelsInSecond: 0,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const Timeline: React.FC<TimelineProps> = ({
|
|
30
|
+
duration,
|
|
31
|
+
value,
|
|
32
|
+
zoomLevel = 0,
|
|
33
|
+
selectedRange = [],
|
|
34
|
+
withTimeBlocks = true,
|
|
35
|
+
onChange = () => {},
|
|
36
|
+
onRangeChange = () => {},
|
|
37
|
+
className = '',
|
|
38
|
+
}) => {
|
|
39
|
+
const timeLineContainerRef = useRef(null);
|
|
40
|
+
|
|
41
|
+
let zoomLevelValue = zoomLevel ? zoomLevel : 0;
|
|
42
|
+
if (zoomLevelValue < 0 || zoomLevelValue >= zoomLevelConfigurations.length) {
|
|
43
|
+
console.warn('Invalid value for property zoomLevel.');
|
|
44
|
+
zoomLevelValue = 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (value < 0 || value > duration) {
|
|
48
|
+
console.warn('Invalid value.');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!(selectedRange.length === 0 || selectedRange.length === 2)) {
|
|
52
|
+
selectedRange = [];
|
|
53
|
+
console.warn('The selected range must contain only two values.');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
selectedRange.length === 2 &&
|
|
58
|
+
!(
|
|
59
|
+
0 <= selectedRange[0] &&
|
|
60
|
+
selectedRange[0] < selectedRange[1] &&
|
|
61
|
+
selectedRange[1] <= duration
|
|
62
|
+
)
|
|
63
|
+
) {
|
|
64
|
+
selectedRange = [];
|
|
65
|
+
console.warn('The selected range is inconsistent.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const zoomParams = useMemo(() => {
|
|
69
|
+
return {
|
|
70
|
+
blockOffset: zoomLevelConfigurations[zoomLevelValue][0],
|
|
71
|
+
pixelsInSecond: zoomLevelConfigurations[zoomLevelValue][1],
|
|
72
|
+
};
|
|
73
|
+
}, [zoomLevelValue]);
|
|
74
|
+
|
|
75
|
+
let blockStartingTimes = [0];
|
|
76
|
+
const blockCounts: number = Math.ceil(duration / zoomParams.blockOffset);
|
|
77
|
+
for (let i: number = 1; i < blockCounts; i++) {
|
|
78
|
+
blockStartingTimes.push(
|
|
79
|
+
blockStartingTimes[blockStartingTimes.length - 1] +
|
|
80
|
+
zoomParams.blockOffset,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const timeLineWrapper: HTMLElement = timeLineContainerRef.current!;
|
|
86
|
+
const scrollPosition: number = value * zoomParams.pixelsInSecond - 300;
|
|
87
|
+
timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
|
|
88
|
+
}, [value, zoomParams]);
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div
|
|
92
|
+
ref={timeLineContainerRef}
|
|
93
|
+
className={`timeline-container ${className}`}
|
|
94
|
+
>
|
|
95
|
+
<div
|
|
96
|
+
className={'timeline-wrapper'}
|
|
97
|
+
style={{ width: duration * zoomParams.pixelsInSecond + 'px' }}
|
|
98
|
+
>
|
|
99
|
+
<ZoomContext.Provider value={zoomParams}>
|
|
100
|
+
<VaLueLineCanvas
|
|
101
|
+
blockStartingTimes={withTimeBlocks ? blockStartingTimes : []}
|
|
102
|
+
value={value}
|
|
103
|
+
/>
|
|
104
|
+
<RangeSelectorCanvas
|
|
105
|
+
selectedRange={selectedRange}
|
|
106
|
+
onChange={onChange}
|
|
107
|
+
onRangeChange={onRangeChange}
|
|
108
|
+
/>
|
|
109
|
+
{withTimeBlocks && (
|
|
110
|
+
<TickTimeCollectionDisplay tickTimes={blockStartingTimes} />
|
|
111
|
+
)}
|
|
112
|
+
</ZoomContext.Provider>
|
|
113
|
+
</div>
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
};
|
|
@@ -1,123 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import styled from 'styled-components';
|
|
3
|
-
import { useEffect, useMemo, useRef } from 'react';
|
|
4
|
-
import TickTimeCollectionDisplay from './TickTimeCollectionDisplay';
|
|
5
|
-
import VaLueLineCanvas from './VaLueLineCanvas';
|
|
6
|
-
import RangeSelectorCanvas from './RangeSelectorCanvas';
|
|
7
|
-
import { zoomLevelConfigurations } from './constants';
|
|
8
|
-
|
|
9
|
-
const TimelineContainer = styled.div`
|
|
10
|
-
background-color: #f0f0f0;
|
|
11
|
-
border: 1px solid #c9c9c9;
|
|
12
|
-
height: 80px;
|
|
13
|
-
width: 100%;
|
|
14
|
-
max-width: 100%;
|
|
15
|
-
overflow-x: auto;
|
|
16
|
-
position: relative;
|
|
17
|
-
display: flex;
|
|
18
|
-
`;
|
|
19
|
-
const TimelineWrapper = styled.div`
|
|
20
|
-
position: absolute;
|
|
21
|
-
height: 100%;
|
|
22
|
-
`;
|
|
23
|
-
|
|
24
|
-
export interface TimelineProps {
|
|
25
|
-
duration: number;
|
|
26
|
-
value: number;
|
|
27
|
-
zoomLevel?: number; //0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
28
|
-
className?: string;
|
|
29
|
-
selectedRange?: number[];
|
|
30
|
-
onChange?: (value: number) => void;
|
|
31
|
-
onRangeChange?: (value: number[]) => void;
|
|
32
|
-
}
|
|
33
|
-
|
|
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
|
-
export const Timeline: React.FC<TimelineProps> = ({
|
|
44
|
-
duration,
|
|
45
|
-
value,
|
|
46
|
-
zoomLevel = 0,
|
|
47
|
-
selectedRange = [],
|
|
48
|
-
onChange = () => {},
|
|
49
|
-
onRangeChange = () => {},
|
|
50
|
-
className = '',
|
|
51
|
-
}) => {
|
|
52
|
-
const timeLineContainerRef = useRef(null);
|
|
53
|
-
|
|
54
|
-
let zoomLevelValue = zoomLevel ? zoomLevel : 0;
|
|
55
|
-
if (zoomLevelValue < 0 || zoomLevelValue >= zoomLevelConfigurations.length) {
|
|
56
|
-
console.warn('Invalid value for property zoomLevel.');
|
|
57
|
-
zoomLevelValue = 0;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (value < 0 || value > duration) {
|
|
61
|
-
console.warn('Invalid value.');
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!(selectedRange.length === 0 || selectedRange.length === 2)) {
|
|
65
|
-
selectedRange = [];
|
|
66
|
-
console.warn('The selected range must contain only two values.');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (
|
|
70
|
-
selectedRange.length === 2 &&
|
|
71
|
-
!(
|
|
72
|
-
0 <= selectedRange[0] &&
|
|
73
|
-
selectedRange[0] < selectedRange[1] &&
|
|
74
|
-
selectedRange[1] <= duration
|
|
75
|
-
)
|
|
76
|
-
) {
|
|
77
|
-
selectedRange = [];
|
|
78
|
-
console.warn('The selected range is inconsistent.');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const zoomParams = useMemo(() => {
|
|
82
|
-
return {
|
|
83
|
-
blockOffset: zoomLevelConfigurations[zoomLevelValue][0],
|
|
84
|
-
pixelsInSecond: zoomLevelConfigurations[zoomLevelValue][1],
|
|
85
|
-
};
|
|
86
|
-
}, [zoomLevelValue]);
|
|
87
|
-
|
|
88
|
-
let blockStartingTimes = [0];
|
|
89
|
-
const blockCounts: number = Math.ceil(duration / zoomParams.blockOffset);
|
|
90
|
-
for (let i: number = 1; i < blockCounts; i++) {
|
|
91
|
-
blockStartingTimes.push(
|
|
92
|
-
blockStartingTimes[blockStartingTimes.length - 1] +
|
|
93
|
-
zoomParams.blockOffset,
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
useEffect(() => {
|
|
98
|
-
const timeLineWrapper: HTMLElement = timeLineContainerRef.current!;
|
|
99
|
-
const scrollPosition: number = value * zoomParams.pixelsInSecond - 300;
|
|
100
|
-
timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
|
|
101
|
-
}, [value, zoomParams]);
|
|
102
|
-
|
|
103
|
-
return (
|
|
104
|
-
<TimelineContainer ref={timeLineContainerRef} className={className}>
|
|
105
|
-
<TimelineWrapper
|
|
106
|
-
style={{ width: duration * zoomParams.pixelsInSecond + 'px' }}
|
|
107
|
-
>
|
|
108
|
-
<ZoomContext.Provider value={zoomParams}>
|
|
109
|
-
<VaLueLineCanvas
|
|
110
|
-
blockStartingTimes={blockStartingTimes}
|
|
111
|
-
value={value}
|
|
112
|
-
/>
|
|
113
|
-
<RangeSelectorCanvas
|
|
114
|
-
selectedRange={selectedRange}
|
|
115
|
-
onChange={onChange}
|
|
116
|
-
onRangeChange={onRangeChange}
|
|
117
|
-
/>
|
|
118
|
-
<TickTimeCollectionDisplay tickTimes={blockStartingTimes} />
|
|
119
|
-
</ZoomContext.Provider>
|
|
120
|
-
</TimelineWrapper>
|
|
121
|
-
</TimelineContainer>
|
|
122
|
-
);
|
|
123
|
-
};
|
|
1
|
+
export * from './Timeline';
|
|
@@ -4,6 +4,8 @@ import { Timeline, TimelineProps } from '../components/timeline';
|
|
|
4
4
|
import { Meta, StoryFn } from '@storybook/react';
|
|
5
5
|
import styled from 'styled-components';
|
|
6
6
|
|
|
7
|
+
import './timeline.stories.custom.css';
|
|
8
|
+
|
|
7
9
|
const StyledTimeline = styled(Timeline)`
|
|
8
10
|
.media-timeline-value-line {
|
|
9
11
|
background-color: red;
|
|
@@ -29,6 +31,7 @@ Default.args = {
|
|
|
29
31
|
value: 301,
|
|
30
32
|
zoomLevel: 0,
|
|
31
33
|
};
|
|
34
|
+
|
|
32
35
|
export const WithSelectedRange = Template.bind({});
|
|
33
36
|
WithSelectedRange.args = {
|
|
34
37
|
duration: 305,
|
|
@@ -36,3 +39,19 @@ WithSelectedRange.args = {
|
|
|
36
39
|
zoomLevel: 0,
|
|
37
40
|
selectedRange: [20, 30],
|
|
38
41
|
};
|
|
42
|
+
|
|
43
|
+
export const WithCustomClassName = Template.bind({});
|
|
44
|
+
WithCustomClassName.args = {
|
|
45
|
+
duration: 305,
|
|
46
|
+
value: 31,
|
|
47
|
+
zoomLevel: 0,
|
|
48
|
+
className: 'this-class-redefines-values',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const WithOutTimeBlocks = Template.bind({});
|
|
52
|
+
WithOutTimeBlocks.args = {
|
|
53
|
+
duration: 305,
|
|
54
|
+
value: 31,
|
|
55
|
+
zoomLevel: 0,
|
|
56
|
+
withTimeBlocks: false,
|
|
57
|
+
};
|