@ldelia/react-media 0.2.3 → 0.2.4
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/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`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ldelia/react-media",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
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
|
+
};
|