@ldelia/react-media 0.2.8 → 0.3.0
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/.storybook/main.js +25 -2
- package/config/env.js +104 -0
- package/config/getHttpsConfig.js +66 -0
- package/config/jest/babelTransform.js +29 -0
- package/config/jest/cssTransform.js +14 -0
- package/config/jest/fileTransform.js +40 -0
- package/config/modules.js +134 -0
- package/config/paths.js +77 -0
- package/config/webpack/persistentCache/createEnvironmentHash.js +9 -0
- package/config/webpack.config.js +755 -0
- package/config/webpackDevServer.config.js +127 -0
- package/dist/stories/timeline.stories.d.ts +6 -6
- package/package.json +126 -18
- package/scripts/build.js +217 -0
- package/scripts/start.js +154 -0
- package/scripts/test.js +52 -0
- package/src/components/__tests__/timeline/timeline.test.js +22 -0
- package/src/components/index.js +1 -0
- package/{dist/components/timeline → src/components/timeline/RangeSelectorCanvas}/RangeSelectorCanvas.js +16 -22
- package/src/components/timeline/Timeline.js +77 -0
- package/src/components/timeline/TimelineCanvas/TickTime.js +26 -0
- package/src/components/timeline/TimelineCanvas/TickTimeCollectionDisplay.js +19 -0
- package/src/components/timeline/TimelineCanvas/TimelineCanvas.js +44 -0
- package/src/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.js +28 -0
- package/src/components/timeline/TimelineValue/TimelineValue.js +50 -0
- package/src/components/timeline/ZoomContext/ZoomContext.js +2 -0
- package/src/components/timeline/constants.js +13 -0
- package/src/components/timeline/index.js +1 -0
- package/src/components/timeline/utils/utils.js +28 -0
- package/src/index.js +1 -0
- package/src/react-app-env.d.ts +71 -1
- package/src/react-docgen-types.d.ts +37 -0
- package/src/setupTests.js +5 -0
- package/src/stories/timeline.stories.js +54 -0
- package/storybook-static/favicon.svg +1 -0
- package/storybook-static/index.html +173 -0
- package/storybook-static/index.json +1 -0
- package/storybook-static/nunito-sans-bold-italic.woff2 +0 -0
- package/storybook-static/nunito-sans-bold.woff2 +0 -0
- package/storybook-static/nunito-sans-italic.woff2 +0 -0
- package/storybook-static/nunito-sans-regular.woff2 +0 -0
- package/storybook-static/project.json +1 -0
- package/storybook-static/sb-addons/essentials-actions-3/manager-bundle.js +3 -0
- package/storybook-static/sb-addons/essentials-actions-3/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/essentials-backgrounds-4/manager-bundle.js +12 -0
- package/storybook-static/sb-addons/essentials-backgrounds-4/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/essentials-controls-2/manager-bundle.js +412 -0
- package/storybook-static/sb-addons/essentials-controls-2/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/essentials-measure-7/manager-bundle.js +3 -0
- package/storybook-static/sb-addons/essentials-measure-7/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/essentials-outline-8/manager-bundle.js +3 -0
- package/storybook-static/sb-addons/essentials-outline-8/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/essentials-toolbars-6/manager-bundle.js +3 -0
- package/storybook-static/sb-addons/essentials-toolbars-6/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/essentials-viewport-5/manager-bundle.js +3 -0
- package/storybook-static/sb-addons/essentials-viewport-5/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/links-1/manager-bundle.js +3 -0
- package/storybook-static/sb-addons/links-1/manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-addons/storybook-core-core-server-presets-0/common-manager-bundle.js +3 -0
- package/storybook-static/sb-addons/storybook-core-core-server-presets-0/common-manager-bundle.js.LEGAL.txt +0 -0
- package/storybook-static/sb-common-assets/favicon.svg +1 -0
- package/storybook-static/sb-common-assets/nunito-sans-bold-italic.woff2 +0 -0
- package/storybook-static/sb-common-assets/nunito-sans-bold.woff2 +0 -0
- package/storybook-static/sb-common-assets/nunito-sans-italic.woff2 +0 -0
- package/storybook-static/sb-common-assets/nunito-sans-regular.woff2 +0 -0
- package/storybook-static/sb-manager/globals-module-info.js +995 -0
- package/storybook-static/sb-manager/globals-runtime.js +53527 -0
- package/storybook-static/sb-manager/globals.js +48 -0
- package/storybook-static/sb-manager/runtime.js +11885 -0
- package/storybook-static/sb-preview/globals.js +33 -0
- package/storybook-static/sb-preview/runtime.js +9483 -0
- package/tsconfig.json +8 -7
- package/.storybook/main.ts +0 -18
- package/.storybook/preview.ts +0 -14
- package/dist/components/timeline/RangeSelectorCanvas.d.ts +0 -8
- package/dist/components/timeline/TickTime.d.ts +0 -7
- package/dist/components/timeline/TickTime.js +0 -31
- package/dist/components/timeline/TickTimeCollectionDisplay.d.ts +0 -6
- package/dist/components/timeline/TickTimeCollectionDisplay.js +0 -24
- package/dist/components/timeline/VaLueLineCanvas.d.ts +0 -7
- package/dist/components/timeline/VaLueLineCanvas.js +0 -111
- package/dist/reportWebVitals.d.ts +0 -3
- package/dist/reportWebVitals.js +0 -37
- package/src/reportWebVitals.ts +0 -15
package/scripts/start.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Do this as the first thing so that any code reading it knows the right env.
|
|
4
|
+
process.env.BABEL_ENV = 'development';
|
|
5
|
+
process.env.NODE_ENV = 'development';
|
|
6
|
+
|
|
7
|
+
// Makes the script crash on unhandled rejections instead of silently
|
|
8
|
+
// ignoring them. In the future, promise rejections that are not handled will
|
|
9
|
+
// terminate the Node.js process with a non-zero exit code.
|
|
10
|
+
process.on('unhandledRejection', err => {
|
|
11
|
+
throw err;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
// Ensure environment variables are read.
|
|
15
|
+
require('../config/env');
|
|
16
|
+
|
|
17
|
+
const fs = require('fs');
|
|
18
|
+
const chalk = require('react-dev-utils/chalk');
|
|
19
|
+
const webpack = require('webpack');
|
|
20
|
+
const WebpackDevServer = require('webpack-dev-server');
|
|
21
|
+
const clearConsole = require('react-dev-utils/clearConsole');
|
|
22
|
+
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
|
23
|
+
const {
|
|
24
|
+
choosePort,
|
|
25
|
+
createCompiler,
|
|
26
|
+
prepareProxy,
|
|
27
|
+
prepareUrls,
|
|
28
|
+
} = require('react-dev-utils/WebpackDevServerUtils');
|
|
29
|
+
const openBrowser = require('react-dev-utils/openBrowser');
|
|
30
|
+
const semver = require('semver');
|
|
31
|
+
const paths = require('../config/paths');
|
|
32
|
+
const configFactory = require('../config/webpack.config');
|
|
33
|
+
const createDevServerConfig = require('../config/webpackDevServer.config');
|
|
34
|
+
const getClientEnvironment = require('../config/env');
|
|
35
|
+
const react = require(require.resolve('react', { paths: [paths.appPath] }));
|
|
36
|
+
|
|
37
|
+
const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
|
|
38
|
+
const useYarn = fs.existsSync(paths.yarnLockFile);
|
|
39
|
+
const isInteractive = process.stdout.isTTY;
|
|
40
|
+
|
|
41
|
+
// Warn and crash if required files are missing
|
|
42
|
+
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Tools like Cloud9 rely on this.
|
|
47
|
+
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
|
|
48
|
+
const HOST = process.env.HOST || '0.0.0.0';
|
|
49
|
+
|
|
50
|
+
if (process.env.HOST) {
|
|
51
|
+
console.log(
|
|
52
|
+
chalk.cyan(
|
|
53
|
+
`Attempting to bind to HOST environment variable: ${chalk.yellow(
|
|
54
|
+
chalk.bold(process.env.HOST)
|
|
55
|
+
)}`
|
|
56
|
+
)
|
|
57
|
+
);
|
|
58
|
+
console.log(
|
|
59
|
+
`If this was unintentional, check that you haven't mistakenly set it in your shell.`
|
|
60
|
+
);
|
|
61
|
+
console.log(
|
|
62
|
+
`Learn more here: ${chalk.yellow('https://cra.link/advanced-config')}`
|
|
63
|
+
);
|
|
64
|
+
console.log();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// We require that you explicitly set browsers and do not fall back to
|
|
68
|
+
// browserslist defaults.
|
|
69
|
+
const { checkBrowsers } = require('react-dev-utils/browsersHelper');
|
|
70
|
+
checkBrowsers(paths.appPath, isInteractive)
|
|
71
|
+
.then(() => {
|
|
72
|
+
// We attempt to use the default port but if it is busy, we offer the user to
|
|
73
|
+
// run on a different port. `choosePort()` Promise resolves to the next free port.
|
|
74
|
+
return choosePort(HOST, DEFAULT_PORT);
|
|
75
|
+
})
|
|
76
|
+
.then(port => {
|
|
77
|
+
if (port == null) {
|
|
78
|
+
// We have not found a port.
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const config = configFactory('development');
|
|
83
|
+
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
|
|
84
|
+
const appName = require(paths.appPackageJson).name;
|
|
85
|
+
|
|
86
|
+
const useTypeScript = fs.existsSync(paths.appTsConfig);
|
|
87
|
+
const urls = prepareUrls(
|
|
88
|
+
protocol,
|
|
89
|
+
HOST,
|
|
90
|
+
port,
|
|
91
|
+
paths.publicUrlOrPath.slice(0, -1)
|
|
92
|
+
);
|
|
93
|
+
// Create a webpack compiler that is configured with custom messages.
|
|
94
|
+
const compiler = createCompiler({
|
|
95
|
+
appName,
|
|
96
|
+
config,
|
|
97
|
+
urls,
|
|
98
|
+
useYarn,
|
|
99
|
+
useTypeScript,
|
|
100
|
+
webpack,
|
|
101
|
+
});
|
|
102
|
+
// Load proxy config
|
|
103
|
+
const proxySetting = require(paths.appPackageJson).proxy;
|
|
104
|
+
const proxyConfig = prepareProxy(
|
|
105
|
+
proxySetting,
|
|
106
|
+
paths.appPublic,
|
|
107
|
+
paths.publicUrlOrPath
|
|
108
|
+
);
|
|
109
|
+
// Serve webpack assets generated by the compiler over a web server.
|
|
110
|
+
const serverConfig = {
|
|
111
|
+
...createDevServerConfig(proxyConfig, urls.lanUrlForConfig),
|
|
112
|
+
host: HOST,
|
|
113
|
+
port,
|
|
114
|
+
};
|
|
115
|
+
const devServer = new WebpackDevServer(serverConfig, compiler);
|
|
116
|
+
// Launch WebpackDevServer.
|
|
117
|
+
devServer.startCallback(() => {
|
|
118
|
+
if (isInteractive) {
|
|
119
|
+
clearConsole();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (env.raw.FAST_REFRESH && semver.lt(react.version, '16.10.0')) {
|
|
123
|
+
console.log(
|
|
124
|
+
chalk.yellow(
|
|
125
|
+
`Fast Refresh requires React 16.10 or higher. You are using React ${react.version}.`
|
|
126
|
+
)
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
console.log(chalk.cyan('Starting the development server...\n'));
|
|
131
|
+
openBrowser(urls.localUrlForBrowser);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
['SIGINT', 'SIGTERM'].forEach(function (sig) {
|
|
135
|
+
process.on(sig, function () {
|
|
136
|
+
devServer.close();
|
|
137
|
+
process.exit();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
if (process.env.CI !== 'true') {
|
|
142
|
+
// Gracefully exit when stdin ends
|
|
143
|
+
process.stdin.on('end', function () {
|
|
144
|
+
devServer.close();
|
|
145
|
+
process.exit();
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
.catch(err => {
|
|
150
|
+
if (err && err.message) {
|
|
151
|
+
console.log(err.message);
|
|
152
|
+
}
|
|
153
|
+
process.exit(1);
|
|
154
|
+
});
|
package/scripts/test.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Do this as the first thing so that any code reading it knows the right env.
|
|
4
|
+
process.env.BABEL_ENV = 'test';
|
|
5
|
+
process.env.NODE_ENV = 'test';
|
|
6
|
+
process.env.PUBLIC_URL = '';
|
|
7
|
+
|
|
8
|
+
// Makes the script crash on unhandled rejections instead of silently
|
|
9
|
+
// ignoring them. In the future, promise rejections that are not handled will
|
|
10
|
+
// terminate the Node.js process with a non-zero exit code.
|
|
11
|
+
process.on('unhandledRejection', err => {
|
|
12
|
+
throw err;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Ensure environment variables are read.
|
|
16
|
+
require('../config/env');
|
|
17
|
+
|
|
18
|
+
const jest = require('jest');
|
|
19
|
+
const execSync = require('child_process').execSync;
|
|
20
|
+
let argv = process.argv.slice(2);
|
|
21
|
+
|
|
22
|
+
function isInGitRepository() {
|
|
23
|
+
try {
|
|
24
|
+
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
|
|
25
|
+
return true;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isInMercurialRepository() {
|
|
32
|
+
try {
|
|
33
|
+
execSync('hg --cwd . root', { stdio: 'ignore' });
|
|
34
|
+
return true;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Watch unless on CI or explicitly running all tests
|
|
41
|
+
if (
|
|
42
|
+
!process.env.CI &&
|
|
43
|
+
argv.indexOf('--watchAll') === -1 &&
|
|
44
|
+
argv.indexOf('--watchAll=false') === -1
|
|
45
|
+
) {
|
|
46
|
+
// https://github.com/facebook/create-react-app/issues/5210
|
|
47
|
+
const hasSourceControl = isInGitRepository() || isInMercurialRepository();
|
|
48
|
+
argv.push(hasSourceControl ? '--watch' : '--watchAll');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
jest.run(argv);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
|
+
import { Timeline } from '../../timeline';
|
|
4
|
+
describe('Timeline', () => {
|
|
5
|
+
let props;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
props = {
|
|
8
|
+
duration: 300,
|
|
9
|
+
value: 15,
|
|
10
|
+
onChange: jest.fn(),
|
|
11
|
+
onRangeChange: jest.fn(),
|
|
12
|
+
zoomLevel: 0,
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
describe('render()', () => {
|
|
16
|
+
it('renders a timeline', () => {
|
|
17
|
+
const rootElement = document.createElement('div');
|
|
18
|
+
const root = createRoot(rootElement);
|
|
19
|
+
root.render(React.createElement(Timeline, Object.assign({}, props)));
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './timeline';
|
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
const react_1 = __importDefault(require("react"));
|
|
7
|
-
const styled_components_1 = __importDefault(require("styled-components"));
|
|
8
|
-
const index_1 = require("./index");
|
|
9
|
-
const react_2 = require("react");
|
|
10
|
-
const utils_1 = require("./utils/utils");
|
|
11
|
-
const OverlayCanvas = styled_components_1.default.canvas `
|
|
1
|
+
import React, { useContext, useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
import { ZoomContext } from '../ZoomContext/ZoomContext';
|
|
4
|
+
import { pixelToSeconds, secondsToPixel } from '../utils/utils';
|
|
5
|
+
const OverlayCanvas = styled.canvas `
|
|
12
6
|
position: absolute;
|
|
13
7
|
top: 0;
|
|
14
8
|
left: 0;
|
|
@@ -17,14 +11,14 @@ const OverlayCanvas = styled_components_1.default.canvas `
|
|
|
17
11
|
color: cadetblue;
|
|
18
12
|
`;
|
|
19
13
|
const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
20
|
-
const canvasRef =
|
|
21
|
-
const zoomContextValue =
|
|
14
|
+
const canvasRef = useRef(null);
|
|
15
|
+
const zoomContextValue = useContext(ZoomContext);
|
|
22
16
|
let isSelectingRange = false;
|
|
23
|
-
let selectedRangeInPixels =
|
|
17
|
+
let selectedRangeInPixels = useMemo(() => [], []);
|
|
24
18
|
if (selectedRange.length === 2) {
|
|
25
19
|
selectedRangeInPixels = [
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
secondsToPixel(zoomContextValue, selectedRange[0]),
|
|
21
|
+
secondsToPixel(zoomContextValue, selectedRange[1]),
|
|
28
22
|
];
|
|
29
23
|
}
|
|
30
24
|
let lastValidSelectedRangeInPixels = [];
|
|
@@ -45,7 +39,7 @@ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
|
45
39
|
};
|
|
46
40
|
const onCanvasDoubleClick = (e) => {
|
|
47
41
|
const x0 = getMousePointerPixelPosition(e);
|
|
48
|
-
onChange(
|
|
42
|
+
onChange(pixelToSeconds(zoomContextValue, x0));
|
|
49
43
|
selectedRangeInPixels = lastValidSelectedRangeInPixels;
|
|
50
44
|
};
|
|
51
45
|
const isPixelNearSelectedRange = (x0) => {
|
|
@@ -114,12 +108,12 @@ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
|
114
108
|
else {
|
|
115
109
|
lastValidSelectedRangeInPixels = pixelRange;
|
|
116
110
|
onRangeChange([
|
|
117
|
-
|
|
118
|
-
|
|
111
|
+
pixelToSeconds(zoomContextValue, pixelRange[0]),
|
|
112
|
+
pixelToSeconds(zoomContextValue, pixelRange[1]),
|
|
119
113
|
]);
|
|
120
114
|
}
|
|
121
115
|
};
|
|
122
|
-
|
|
116
|
+
useEffect(() => {
|
|
123
117
|
const canvas = canvasRef.current;
|
|
124
118
|
// https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
|
|
125
119
|
canvas.width = canvas.offsetWidth;
|
|
@@ -128,6 +122,6 @@ const RangeSelectorCanvas = ({ selectedRange, onChange, onRangeChange, }) => {
|
|
|
128
122
|
return;
|
|
129
123
|
drawRect(selectedRangeInPixels[0], selectedRangeInPixels[1]);
|
|
130
124
|
}, [selectedRangeInPixels]);
|
|
131
|
-
return (
|
|
125
|
+
return (React.createElement(OverlayCanvas, { ref: canvasRef, onDoubleClick: onCanvasDoubleClick, onMouseDown: onCanvasMouseDown, onMouseMove: onCanvasMouseMove, onMouseUp: onCanvasMouseUp, className: 'media-timeline-range-selector-canvas' }));
|
|
132
126
|
};
|
|
133
|
-
|
|
127
|
+
export default React.memo(RangeSelectorCanvas);
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import TimelineCanvas from './TimelineCanvas/TimelineCanvas';
|
|
3
|
+
import RangeSelectorCanvas from './RangeSelectorCanvas/RangeSelectorCanvas';
|
|
4
|
+
import { zoomLevelConfigurations } from './constants';
|
|
5
|
+
import styled from 'styled-components';
|
|
6
|
+
import { TimelineValue } from './TimelineValue/TimelineValue';
|
|
7
|
+
import { ZoomContext } from './ZoomContext/ZoomContext';
|
|
8
|
+
import { getBlockOffsetForZoomLevel, getTimelineWrapperWidth, numberToPxString, } from './utils/utils';
|
|
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
|
+
display: flex;
|
|
23
|
+
flex-direction: row;
|
|
24
|
+
align-items: center;
|
|
25
|
+
overflow: hidden;
|
|
26
|
+
`;
|
|
27
|
+
export const Timeline = ({ duration, value, zoomLevel = 0, selectedRange = [], withTimeBlocks = true, onChange = () => { }, onRangeChange = () => { }, className = '', }) => {
|
|
28
|
+
const timeLineContainerRef = useRef(null);
|
|
29
|
+
const canvasRef = useRef(null);
|
|
30
|
+
const [zoomContextValue, setZoomContextValue] = useState({
|
|
31
|
+
blockOffset: 0,
|
|
32
|
+
pixelsInSecond: 0,
|
|
33
|
+
timelineWrapperWidth: 0,
|
|
34
|
+
});
|
|
35
|
+
let zoomLevelValue = zoomLevel ? zoomLevel : 0;
|
|
36
|
+
if (zoomLevelValue < 0 || zoomLevelValue >= zoomLevelConfigurations.length) {
|
|
37
|
+
console.warn('Invalid value for property zoomLevel.');
|
|
38
|
+
zoomLevelValue = 0;
|
|
39
|
+
}
|
|
40
|
+
if (value < 0 || value > duration) {
|
|
41
|
+
console.warn('Invalid value.');
|
|
42
|
+
}
|
|
43
|
+
if (!(selectedRange.length === 0 || selectedRange.length === 2)) {
|
|
44
|
+
selectedRange = [];
|
|
45
|
+
console.warn('The selected range must contain only two values.');
|
|
46
|
+
}
|
|
47
|
+
if (selectedRange.length === 2 &&
|
|
48
|
+
!(0 <= selectedRange[0] &&
|
|
49
|
+
selectedRange[0] < selectedRange[1] &&
|
|
50
|
+
selectedRange[1] <= duration)) {
|
|
51
|
+
selectedRange = [];
|
|
52
|
+
console.warn('The selected range is inconsistent.');
|
|
53
|
+
}
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (!timeLineContainerRef.current)
|
|
56
|
+
return;
|
|
57
|
+
const timelineWrapperWidth = getTimelineWrapperWidth(timeLineContainerRef.current.offsetWidth, zoomLevelValue);
|
|
58
|
+
setZoomContextValue({
|
|
59
|
+
blockOffset: getBlockOffsetForZoomLevel(zoomLevelValue, duration, timelineWrapperWidth),
|
|
60
|
+
pixelsInSecond: timelineWrapperWidth / duration,
|
|
61
|
+
timelineWrapperWidth: timelineWrapperWidth,
|
|
62
|
+
});
|
|
63
|
+
}, [timeLineContainerRef.current, zoomLevelValue, duration]);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
const timeLineWrapper = timeLineContainerRef.current;
|
|
66
|
+
const scrollPosition = value * zoomContextValue.pixelsInSecond - 300;
|
|
67
|
+
timeLineWrapper.scrollLeft = Math.max(0, scrollPosition);
|
|
68
|
+
}, [duration, value, zoomContextValue]);
|
|
69
|
+
return (React.createElement(TimelineContainer, { ref: timeLineContainerRef, className: className },
|
|
70
|
+
React.createElement(TimelineWrapper, { style: {
|
|
71
|
+
width: numberToPxString(zoomContextValue.timelineWrapperWidth),
|
|
72
|
+
} },
|
|
73
|
+
React.createElement(ZoomContext.Provider, { value: zoomContextValue },
|
|
74
|
+
React.createElement(TimelineCanvas, { ref: canvasRef, duration: duration, withTimeBlocks: withTimeBlocks }),
|
|
75
|
+
React.createElement(TimelineValue, { value: value, canvasRef: canvasRef }),
|
|
76
|
+
React.createElement(RangeSelectorCanvas, { selectedRange: selectedRange, onChange: onChange, onRangeChange: onRangeChange })))));
|
|
77
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
const TickTimeContainer = styled.span.attrs((props) => ({
|
|
4
|
+
style: {
|
|
5
|
+
left: props.left,
|
|
6
|
+
},
|
|
7
|
+
})) `
|
|
8
|
+
background: transparent;
|
|
9
|
+
position: absolute;
|
|
10
|
+
padding-left: 8px;
|
|
11
|
+
color: #646464;
|
|
12
|
+
user-select: none;
|
|
13
|
+
min-width: 20px;
|
|
14
|
+
max-width: 20px;
|
|
15
|
+
align-self: flex-end;
|
|
16
|
+
`;
|
|
17
|
+
const TickTime = ({ start, leftPosition }) => {
|
|
18
|
+
const minutes = Math.floor(start / 60);
|
|
19
|
+
let seconds = start - minutes * 60;
|
|
20
|
+
let secondsFormatted = seconds < 10 ? '0' + seconds : seconds.toString();
|
|
21
|
+
return (React.createElement(TickTimeContainer, { left: leftPosition, className: 'media-timeline-tick-time' },
|
|
22
|
+
minutes,
|
|
23
|
+
":",
|
|
24
|
+
secondsFormatted));
|
|
25
|
+
};
|
|
26
|
+
export default TickTime;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
import TickTime from './TickTime';
|
|
4
|
+
import { ZoomContext } from '../ZoomContext/ZoomContext';
|
|
5
|
+
const TickTimeCollectionDisplayContainer = styled.div `
|
|
6
|
+
position: absolute;
|
|
7
|
+
display: flex;
|
|
8
|
+
align-self: flex-end;
|
|
9
|
+
height: 100%;
|
|
10
|
+
`;
|
|
11
|
+
const TickTimeCollectionDisplay = ({ tickTimes = [], }) => {
|
|
12
|
+
return (React.createElement(ZoomContext.Consumer, null, (value) => {
|
|
13
|
+
return (React.createElement(TickTimeCollectionDisplayContainer, null, tickTimes.map((tickTimeValue, index) => {
|
|
14
|
+
const leftPosition = tickTimeValue * value.pixelsInSecond + 'px';
|
|
15
|
+
return (React.createElement(TickTime, { key: index, start: tickTimeValue, leftPosition: leftPosition }));
|
|
16
|
+
})));
|
|
17
|
+
}));
|
|
18
|
+
};
|
|
19
|
+
export default TickTimeCollectionDisplay;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, } from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
import { ZoomContext } from '../ZoomContext/ZoomContext';
|
|
4
|
+
import TickTimeCollectionDisplay from './TickTimeCollectionDisplay';
|
|
5
|
+
import { drawTimeBlocksOnCanvas } from './drawTimeBlocksOnCanvas';
|
|
6
|
+
const OverlayCanvas = styled.canvas `
|
|
7
|
+
position: absolute;
|
|
8
|
+
top: 0;
|
|
9
|
+
left: 0;
|
|
10
|
+
width: 100%;
|
|
11
|
+
height: 100%;
|
|
12
|
+
color: #c9c9c9;
|
|
13
|
+
`;
|
|
14
|
+
const TimelineCanvas = forwardRef(({ duration, withTimeBlocks }, ref) => {
|
|
15
|
+
const internalCanvasRef = useRef(null);
|
|
16
|
+
const zoomContextValue = useContext(ZoomContext);
|
|
17
|
+
const blockStartingTimes = (() => {
|
|
18
|
+
if (!withTimeBlocks || !zoomContextValue.blockOffset)
|
|
19
|
+
return [];
|
|
20
|
+
let blockStartingTimes = [0];
|
|
21
|
+
let secondsToCover = duration;
|
|
22
|
+
while (secondsToCover > 0) {
|
|
23
|
+
blockStartingTimes.push(blockStartingTimes[blockStartingTimes.length - 1] +
|
|
24
|
+
zoomContextValue.blockOffset);
|
|
25
|
+
secondsToCover = secondsToCover - zoomContextValue.blockOffset;
|
|
26
|
+
}
|
|
27
|
+
blockStartingTimes.splice(-1);
|
|
28
|
+
return blockStartingTimes;
|
|
29
|
+
})();
|
|
30
|
+
// Pass the internal ref's current value directly to the forwarded ref
|
|
31
|
+
useImperativeHandle(ref, () => internalCanvasRef.current);
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const canvas = internalCanvasRef.current;
|
|
34
|
+
// https://stackoverflow.com/questions/8696631/canvas-drawings-like-lines-are-blurry
|
|
35
|
+
canvas.width = canvas.offsetWidth;
|
|
36
|
+
canvas.height = canvas.offsetHeight;
|
|
37
|
+
if (withTimeBlocks)
|
|
38
|
+
drawTimeBlocksOnCanvas(canvas, blockStartingTimes, zoomContextValue);
|
|
39
|
+
}, [blockStartingTimes, withTimeBlocks, zoomContextValue]);
|
|
40
|
+
return (React.createElement(React.Fragment, null,
|
|
41
|
+
React.createElement(OverlayCanvas, { ref: internalCanvasRef, className: 'media-timeline-value-line-canvas' }),
|
|
42
|
+
withTimeBlocks && (React.createElement(TickTimeCollectionDisplay, { tickTimes: blockStartingTimes }))));
|
|
43
|
+
});
|
|
44
|
+
export default TimelineCanvas;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { secondsToPixel } from '../utils/utils';
|
|
2
|
+
export const drawTimeBlocksOnCanvas = (canvas, blockStartingTimes, zoomContextValue) => {
|
|
3
|
+
const blockHeight = 20;
|
|
4
|
+
const context = canvas.getContext('2d');
|
|
5
|
+
// draw top line
|
|
6
|
+
context.beginPath();
|
|
7
|
+
context.moveTo(0, canvas.height - blockHeight);
|
|
8
|
+
context.lineTo(zoomContextValue.timelineWrapperWidth, canvas.height - blockHeight);
|
|
9
|
+
context.strokeStyle = window
|
|
10
|
+
.getComputedStyle(canvas)
|
|
11
|
+
.getPropertyValue('color');
|
|
12
|
+
context.stroke();
|
|
13
|
+
for (const blockStartingTime of blockStartingTimes) {
|
|
14
|
+
const x0Pixel = secondsToPixel(zoomContextValue, blockStartingTime);
|
|
15
|
+
const x1Pixel = secondsToPixel(zoomContextValue, blockStartingTime + zoomContextValue.blockOffset);
|
|
16
|
+
context.beginPath();
|
|
17
|
+
// draw left line
|
|
18
|
+
context.moveTo(x0Pixel, canvas.height);
|
|
19
|
+
context.lineTo(x0Pixel, canvas.height - blockHeight);
|
|
20
|
+
// draw right line
|
|
21
|
+
context.moveTo(x1Pixel, canvas.height);
|
|
22
|
+
context.lineTo(x1Pixel, canvas.height - blockHeight);
|
|
23
|
+
context.strokeStyle = window
|
|
24
|
+
.getComputedStyle(canvas)
|
|
25
|
+
.getPropertyValue('color');
|
|
26
|
+
context.stroke();
|
|
27
|
+
}
|
|
28
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import React, { useCallback, useContext, useEffect, useRef } from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
import { getComputedElementWidth, numberToPxString, secondsToPixel, } from '../utils/utils';
|
|
4
|
+
import { ZoomContext } from '../ZoomContext/ZoomContext';
|
|
5
|
+
const PreValueLine = styled.span `
|
|
6
|
+
position: absolute;
|
|
7
|
+
left: 0;
|
|
8
|
+
height: 100%;
|
|
9
|
+
`;
|
|
10
|
+
const ValueLine = styled.span `
|
|
11
|
+
position: absolute;
|
|
12
|
+
width: 1px;
|
|
13
|
+
height: 100%;
|
|
14
|
+
background-color: #575757;
|
|
15
|
+
`;
|
|
16
|
+
const PostValueLine = styled.span `
|
|
17
|
+
position: absolute;
|
|
18
|
+
height: 100%;
|
|
19
|
+
`;
|
|
20
|
+
export const TimelineValue = ({ canvasRef, value }) => {
|
|
21
|
+
const preValueLineRef = useRef(null);
|
|
22
|
+
const valueLineRef = useRef(null);
|
|
23
|
+
const postValueLineRef = useRef(null);
|
|
24
|
+
const zoomContextValue = useContext(ZoomContext);
|
|
25
|
+
const showValueLine = useCallback(() => {
|
|
26
|
+
const canvas = canvasRef.current;
|
|
27
|
+
const preValueLineElement = preValueLineRef.current;
|
|
28
|
+
const valueLineElement = valueLineRef.current;
|
|
29
|
+
const postValueLineElement = postValueLineRef.current;
|
|
30
|
+
const valueLineWidth = getComputedElementWidth(valueLineElement);
|
|
31
|
+
const linePosition = secondsToPixel(zoomContextValue, value);
|
|
32
|
+
const valueLinePositionBeginningConsideringWidth = linePosition - valueLineWidth / 2;
|
|
33
|
+
// configure preValueLineElement
|
|
34
|
+
preValueLineElement.style.width = numberToPxString(valueLinePositionBeginningConsideringWidth);
|
|
35
|
+
// configure valueLineElement
|
|
36
|
+
valueLineElement.style.left = numberToPxString(valueLinePositionBeginningConsideringWidth);
|
|
37
|
+
// configure postValueLineElement
|
|
38
|
+
const postValueLinePosition = valueLinePositionBeginningConsideringWidth + valueLineWidth;
|
|
39
|
+
const postValueLineWidth = canvas.width - postValueLinePosition;
|
|
40
|
+
postValueLineElement.style.left = numberToPxString(postValueLinePosition);
|
|
41
|
+
postValueLineElement.style.width = numberToPxString(postValueLineWidth);
|
|
42
|
+
}, [canvasRef, value, zoomContextValue]);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
showValueLine();
|
|
45
|
+
}, [showValueLine]);
|
|
46
|
+
return (React.createElement(React.Fragment, null,
|
|
47
|
+
React.createElement(PreValueLine, { ref: preValueLineRef, className: 'media-timeline-pre-value-line' }),
|
|
48
|
+
React.createElement(ValueLine, { ref: valueLineRef, className: 'media-timeline-value-line' }),
|
|
49
|
+
React.createElement(PostValueLine, { ref: postValueLineRef, className: 'media-timeline-post-value-line' })));
|
|
50
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//[blockOffset, pixelsInSecond] when zoomLevel === 0, each block has an offset of 20 seconds, and each second has a width of 7px.
|
|
2
|
+
export const zoomLevelConfigurations = [
|
|
3
|
+
[20, 7],
|
|
4
|
+
[10, 10],
|
|
5
|
+
[10, 15],
|
|
6
|
+
[5, 20],
|
|
7
|
+
[5, 25],
|
|
8
|
+
[2, 35],
|
|
9
|
+
[2, 50],
|
|
10
|
+
[1, 60],
|
|
11
|
+
[1, 75],
|
|
12
|
+
[1, 90],
|
|
13
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Timeline';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const secondsToPixel = (zoomContextValue, seconds) => {
|
|
2
|
+
return zoomContextValue.pixelsInSecond * seconds;
|
|
3
|
+
};
|
|
4
|
+
export const pixelToSeconds = (zoomContextValue, pixel) => {
|
|
5
|
+
const seconds = pixel / zoomContextValue.pixelsInSecond;
|
|
6
|
+
return Math.round((seconds + Number.EPSILON) * 100) / 100;
|
|
7
|
+
};
|
|
8
|
+
export const getComputedElementWidth = (element) => {
|
|
9
|
+
const elementWidthCSSProperty = window
|
|
10
|
+
.getComputedStyle(element)
|
|
11
|
+
.getPropertyValue('width')
|
|
12
|
+
.replace(/[^-\d]/g, '');
|
|
13
|
+
return parseInt(elementWidthCSSProperty);
|
|
14
|
+
};
|
|
15
|
+
export const numberToPxString = (number) => {
|
|
16
|
+
return `${number}px`;
|
|
17
|
+
};
|
|
18
|
+
export const getTimelineWrapperWidth = (timeLineContainerWidth, zoomLevel) => {
|
|
19
|
+
return timeLineContainerWidth * Math.pow(1.25, zoomLevel) - 2; //-2px to account for the border
|
|
20
|
+
};
|
|
21
|
+
export function getBlockOffsetForZoomLevel(zoomLevel, duration, timelineWrapperWidth) {
|
|
22
|
+
const offsets = [10, 5, 5, 5, 2, 2, 2, 2, 2, 1];
|
|
23
|
+
let optimalOffset = offsets[zoomLevel];
|
|
24
|
+
while ((duration / optimalOffset) * 40 > timelineWrapperWidth) {
|
|
25
|
+
optimalOffset = optimalOffset * 1.25;
|
|
26
|
+
}
|
|
27
|
+
return Math.ceil(optimalOffset);
|
|
28
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './components';
|