@ldelia/react-media 0.2.8 → 0.4.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/README.md +1 -4
- 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 +127 -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/src/components/reproduction-widget/README.md +27 -0
- package/src/components/reproduction-widget/ReproductionWidget.js +29 -0
- package/src/components/reproduction-widget/ReproductionWidget.tsx +68 -0
- package/src/components/reproduction-widget/index.js +1 -0
- package/src/components/reproduction-widget/index.tsx +1 -0
- package/src/components/reproduction-widget/inner-players/PlayAlongInnerPlayer.js +5 -0
- package/src/components/reproduction-widget/inner-players/PlayAlongInnerPlayer.tsx +10 -0
- package/src/components/reproduction-widget/inner-players/YouTubeInnerPlayer.js +21 -0
- package/src/components/reproduction-widget/inner-players/YouTubeInnerPlayer.tsx +36 -0
- package/src/components/reproduction-widget/models/Player/PlayAlongPlayer.js +97 -0
- package/src/components/reproduction-widget/models/Player/PlayAlongPlayer.ts +129 -0
- package/src/components/reproduction-widget/models/Player/PlayerEvents.js +4 -0
- package/src/components/reproduction-widget/models/Player/PlayerEvents.ts +4 -0
- package/src/components/reproduction-widget/models/Player/YouTubePlayer.js +126 -0
- package/src/components/reproduction-widget/models/Player/YouTubePlayer.ts +171 -0
- package/src/components/reproduction-widget/models/Reproduction.js +214 -0
- package/src/components/reproduction-widget/models/Reproduction.ts +272 -0
- package/src/components/reproduction-widget/models/ReproductionBuilder.js +51 -0
- package/src/components/reproduction-widget/models/ReproductionBuilder.ts +70 -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/reproduction-widget.stories.js +16 -0
- package/src/stories/reproduction-widget.stories.tsx +23 -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
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { PLAYER_EVENTS } from './Player/PlayerEvents';
|
|
2
|
+
import { ReproductionBuilder } from './ReproductionBuilder';
|
|
3
|
+
import { PlayAlongPlayer } from './Player/PlayAlongPlayer';
|
|
4
|
+
import { YouTubePlayer } from './Player/YouTubePlayer';
|
|
5
|
+
|
|
6
|
+
type Player = PlayAlongPlayer | YouTubePlayer;
|
|
7
|
+
|
|
8
|
+
const STATES = {
|
|
9
|
+
STOPPED: 0,
|
|
10
|
+
COUNTING_IN: 1,
|
|
11
|
+
PLAYING: 2,
|
|
12
|
+
PAUSED: 3,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const EVENTS = {
|
|
16
|
+
READY: 'READY',
|
|
17
|
+
START: 'START',
|
|
18
|
+
COUNTING_IN: 'COUNTING_IN',
|
|
19
|
+
PLAY: 'PLAY',
|
|
20
|
+
PLAYING: 'PLAYING',
|
|
21
|
+
PAUSED: 'PAUSED',
|
|
22
|
+
FINISH: 'FINISH',
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
const dispatchOnReadyHandlers = Symbol();
|
|
26
|
+
const dispatchOnSongStartHandlers = Symbol();
|
|
27
|
+
const dispatchOnCountingInHandlers = Symbol();
|
|
28
|
+
const dispatchOnPlayHandlers = Symbol();
|
|
29
|
+
const dispatchOnPlayingHandlers = Symbol();
|
|
30
|
+
const dispatchOnPausedHandlers = Symbol();
|
|
31
|
+
const dispatchOnFinishHandlers = Symbol();
|
|
32
|
+
|
|
33
|
+
export class Reproduction {
|
|
34
|
+
private player: Player;
|
|
35
|
+
private requiresCountingIn: boolean;
|
|
36
|
+
|
|
37
|
+
private songTempo: number;
|
|
38
|
+
private state: number;
|
|
39
|
+
private ready: boolean;
|
|
40
|
+
private interval: ReturnType<typeof setInterval> | null;
|
|
41
|
+
private countingInCounter: number;
|
|
42
|
+
private [dispatchOnReadyHandlers]: (() => void)[];
|
|
43
|
+
private [dispatchOnSongStartHandlers]: (() => void)[];
|
|
44
|
+
private [dispatchOnCountingInHandlers]: (() => void)[];
|
|
45
|
+
private [dispatchOnPlayHandlers]: (() => void)[];
|
|
46
|
+
private [dispatchOnPlayingHandlers]: (() => void)[];
|
|
47
|
+
private [dispatchOnPausedHandlers]: (() => void)[];
|
|
48
|
+
private [dispatchOnFinishHandlers]: (() => void)[];
|
|
49
|
+
|
|
50
|
+
static get EVENTS() {
|
|
51
|
+
return EVENTS;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static get STATES() {
|
|
55
|
+
return STATES;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
constructor(player: Player, requiresCountingIn: boolean, songTempo: number) {
|
|
59
|
+
this[dispatchOnReadyHandlers] = [];
|
|
60
|
+
this[dispatchOnSongStartHandlers] = [];
|
|
61
|
+
this[dispatchOnCountingInHandlers] = [];
|
|
62
|
+
this[dispatchOnPlayHandlers] = [];
|
|
63
|
+
this[dispatchOnPlayingHandlers] = [];
|
|
64
|
+
this[dispatchOnPausedHandlers] = [];
|
|
65
|
+
this[dispatchOnFinishHandlers] = [];
|
|
66
|
+
|
|
67
|
+
this.songTempo = songTempo;
|
|
68
|
+
this.player = player;
|
|
69
|
+
this.ready = false;
|
|
70
|
+
|
|
71
|
+
this.state = Reproduction.STATES.STOPPED;
|
|
72
|
+
this.interval = null;
|
|
73
|
+
|
|
74
|
+
this.requiresCountingIn = requiresCountingIn;
|
|
75
|
+
this.countingInCounter = 0;
|
|
76
|
+
|
|
77
|
+
this.player.on(PLAYER_EVENTS.READY, () => {
|
|
78
|
+
this.ready = true;
|
|
79
|
+
this.dispatch(Reproduction.EVENTS.READY);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
this.player.on(PLAYER_EVENTS.FINISH, () => {
|
|
83
|
+
this.state = Reproduction.STATES.STOPPED;
|
|
84
|
+
clearInterval(this.interval as NodeJS.Timeout);
|
|
85
|
+
this.dispatch(Reproduction.EVENTS.FINISH);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
on(eventName: keyof typeof Reproduction.EVENTS, handler: () => void) {
|
|
90
|
+
switch (eventName) {
|
|
91
|
+
case Reproduction.EVENTS.READY:
|
|
92
|
+
return this[dispatchOnReadyHandlers].push(handler);
|
|
93
|
+
case Reproduction.EVENTS.START:
|
|
94
|
+
return this[dispatchOnSongStartHandlers].push(handler);
|
|
95
|
+
case Reproduction.EVENTS.COUNTING_IN:
|
|
96
|
+
return this[dispatchOnCountingInHandlers].push(handler);
|
|
97
|
+
case Reproduction.EVENTS.PLAY:
|
|
98
|
+
return this[dispatchOnPlayHandlers].push(handler);
|
|
99
|
+
case Reproduction.EVENTS.PLAYING:
|
|
100
|
+
return this[dispatchOnPlayingHandlers].push(handler);
|
|
101
|
+
case Reproduction.EVENTS.PAUSED:
|
|
102
|
+
return this[dispatchOnPausedHandlers].push(handler);
|
|
103
|
+
case Reproduction.EVENTS.FINISH:
|
|
104
|
+
return this[dispatchOnFinishHandlers].push(handler);
|
|
105
|
+
default:
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
dispatch(eventName: keyof typeof Reproduction.EVENTS) {
|
|
111
|
+
let handler, i, len;
|
|
112
|
+
|
|
113
|
+
let ref: (() => void)[] = [];
|
|
114
|
+
|
|
115
|
+
switch (eventName) {
|
|
116
|
+
case Reproduction.EVENTS.READY:
|
|
117
|
+
ref = this[dispatchOnReadyHandlers];
|
|
118
|
+
break;
|
|
119
|
+
case Reproduction.EVENTS.START:
|
|
120
|
+
ref = this[dispatchOnSongStartHandlers];
|
|
121
|
+
break;
|
|
122
|
+
case Reproduction.EVENTS.COUNTING_IN:
|
|
123
|
+
ref = this[dispatchOnCountingInHandlers];
|
|
124
|
+
break;
|
|
125
|
+
case Reproduction.EVENTS.PLAY:
|
|
126
|
+
ref = this[dispatchOnPlayHandlers];
|
|
127
|
+
break;
|
|
128
|
+
case Reproduction.EVENTS.PLAYING:
|
|
129
|
+
ref = this[dispatchOnPlayingHandlers];
|
|
130
|
+
break;
|
|
131
|
+
case Reproduction.EVENTS.PAUSED:
|
|
132
|
+
ref = this[dispatchOnPausedHandlers];
|
|
133
|
+
break;
|
|
134
|
+
case Reproduction.EVENTS.FINISH:
|
|
135
|
+
ref = this[dispatchOnFinishHandlers];
|
|
136
|
+
break;
|
|
137
|
+
default:
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
|
142
|
+
handler = ref[i];
|
|
143
|
+
handler();
|
|
144
|
+
// setTimeout(handler, 0);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private countIn(timeout: number, limit: number) {
|
|
149
|
+
// the initial count starts instantly, no waiting
|
|
150
|
+
this.countingInCounter++;
|
|
151
|
+
this.dispatch(Reproduction.EVENTS.COUNTING_IN);
|
|
152
|
+
|
|
153
|
+
const interval = setInterval(() => {
|
|
154
|
+
this.countingInCounter++;
|
|
155
|
+
if (this.countingInCounter === limit) {
|
|
156
|
+
clearInterval(interval);
|
|
157
|
+
this.countingInCounter = 0;
|
|
158
|
+
if (limit !== 5) {
|
|
159
|
+
this.countIn(this.getBPMInterval(), 5);
|
|
160
|
+
} else {
|
|
161
|
+
this.play();
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
this.dispatch(Reproduction.EVENTS.COUNTING_IN);
|
|
165
|
+
}
|
|
166
|
+
}, timeout);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
start() {
|
|
170
|
+
if (this.state === Reproduction.STATES.STOPPED) {
|
|
171
|
+
this.dispatch(Reproduction.EVENTS.START);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (this.requiresCountingIn && this.getCurrentTime() === 0) {
|
|
175
|
+
this.state = Reproduction.STATES.COUNTING_IN;
|
|
176
|
+
this.countIn(this.getBPMInterval() * 2, 3);
|
|
177
|
+
} else {
|
|
178
|
+
this.play();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
play() {
|
|
183
|
+
this.state = Reproduction.STATES.PLAYING;
|
|
184
|
+
this.dispatch(Reproduction.EVENTS.PLAY);
|
|
185
|
+
this.player.play();
|
|
186
|
+
|
|
187
|
+
const intervalTimeout = 200;
|
|
188
|
+
|
|
189
|
+
this.interval = setInterval(() => {
|
|
190
|
+
if (this.isPlaying()) {
|
|
191
|
+
this.dispatch(Reproduction.EVENTS.PLAYING);
|
|
192
|
+
}
|
|
193
|
+
}, intervalTimeout);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
pause() {
|
|
197
|
+
this.state = Reproduction.STATES.PAUSED;
|
|
198
|
+
this.player.pause();
|
|
199
|
+
clearInterval(this.interval as NodeJS.Timeout);
|
|
200
|
+
|
|
201
|
+
this.dispatch(Reproduction.EVENTS.PAUSED);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
stop() {
|
|
205
|
+
this.state = Reproduction.STATES.STOPPED;
|
|
206
|
+
this.player.stop();
|
|
207
|
+
clearInterval(this.interval as NodeJS.Timeout);
|
|
208
|
+
this.dispatch(Reproduction.EVENTS.FINISH);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
isReady() {
|
|
212
|
+
// It's necessary to avoid play the reproduction-widget when the player is not ready
|
|
213
|
+
return this.ready;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
isPlaying() {
|
|
217
|
+
return this.state === Reproduction.STATES.PLAYING;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
isStopped() {
|
|
221
|
+
return this.state === Reproduction.STATES.STOPPED;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
isPaused() {
|
|
225
|
+
return this.state === Reproduction.STATES.PAUSED;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
isCountingIn() {
|
|
229
|
+
return this.state === Reproduction.STATES.COUNTING_IN;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
getPlayer() {
|
|
233
|
+
return this.player;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
getTempo() {
|
|
237
|
+
return this.songTempo;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
getCurrentTime() {
|
|
241
|
+
// in seconds with milliseconds.
|
|
242
|
+
return this.player.getCurrentTime();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
getDuration() {
|
|
246
|
+
return this.player.getDuration();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
seekTo(seconds: number) {
|
|
250
|
+
this.player.seekTo(seconds);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
getAvailablePlaybackRates() {
|
|
254
|
+
return this.player.getAvailablePlaybackRates();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
setPlaybackRate(playbackRate: number) {
|
|
258
|
+
this.player.setPlaybackRate(playbackRate);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
isAvailable() {
|
|
262
|
+
return this.player.isAvailable();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
getBPMInterval() {
|
|
266
|
+
return 60000 / this.getTempo();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static newBuilder() {
|
|
270
|
+
return new ReproductionBuilder();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { YouTubePlayer } from './Player/YouTubePlayer';
|
|
2
|
+
import { PlayAlongPlayer } from './Player/PlayAlongPlayer';
|
|
3
|
+
import { Reproduction } from './Reproduction';
|
|
4
|
+
export class ReproductionBuilder {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.trainingMode = false;
|
|
7
|
+
this.requiresCountingIn = false;
|
|
8
|
+
this.songDuration = null;
|
|
9
|
+
this.songTempo = null;
|
|
10
|
+
this.innerPlayer = null;
|
|
11
|
+
}
|
|
12
|
+
withSongDuration(songDuration) {
|
|
13
|
+
this.songDuration = songDuration;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
withSongTempo(songTempo) {
|
|
17
|
+
this.songTempo = songTempo;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
withTrainingMode(trainingMode) {
|
|
21
|
+
this.trainingMode = trainingMode;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
withCountingIn(requiresCountingIn) {
|
|
25
|
+
this.requiresCountingIn = requiresCountingIn;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
withInnerPlayer(innerPlayer) {
|
|
29
|
+
this.innerPlayer = innerPlayer;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
createReproduction() {
|
|
33
|
+
if (this.requiresCountingIn && this.songTempo === null) {
|
|
34
|
+
throw new Error('The song tempo is mandatory');
|
|
35
|
+
}
|
|
36
|
+
if (this.innerPlayer === null) {
|
|
37
|
+
throw new Error('The inner player was not provided.');
|
|
38
|
+
}
|
|
39
|
+
let player;
|
|
40
|
+
if (this.trainingMode) {
|
|
41
|
+
player = new YouTubePlayer(this.innerPlayer);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
if (this.songDuration === null) {
|
|
45
|
+
throw new Error('The song duration is mandatory');
|
|
46
|
+
}
|
|
47
|
+
player = new PlayAlongPlayer(this.songDuration, this.innerPlayer);
|
|
48
|
+
}
|
|
49
|
+
return new Reproduction(player, this.requiresCountingIn, this.songTempo || 0);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { YouTubePlayer } from './Player/YouTubePlayer';
|
|
2
|
+
import { PlayAlongPlayer } from './Player/PlayAlongPlayer';
|
|
3
|
+
import { Reproduction } from './Reproduction';
|
|
4
|
+
import { YouTubePlayer as InnerYouTubePlayer } from 'react-youtube';
|
|
5
|
+
|
|
6
|
+
export class ReproductionBuilder {
|
|
7
|
+
private trainingMode: boolean;
|
|
8
|
+
private requiresCountingIn: boolean;
|
|
9
|
+
private songDuration: number | null;
|
|
10
|
+
private songTempo: number | null;
|
|
11
|
+
private innerPlayer: InnerYouTubePlayer | string;
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
this.trainingMode = false;
|
|
15
|
+
this.requiresCountingIn = false;
|
|
16
|
+
this.songDuration = null;
|
|
17
|
+
this.songTempo = null;
|
|
18
|
+
this.innerPlayer = null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
withSongDuration(songDuration: number) {
|
|
22
|
+
this.songDuration = songDuration;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
withSongTempo(songTempo: number) {
|
|
27
|
+
this.songTempo = songTempo;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
withTrainingMode(trainingMode: boolean) {
|
|
32
|
+
this.trainingMode = trainingMode;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
withCountingIn(requiresCountingIn: boolean) {
|
|
37
|
+
this.requiresCountingIn = requiresCountingIn;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
withInnerPlayer(innerPlayer: InnerYouTubePlayer | string) {
|
|
42
|
+
this.innerPlayer = innerPlayer;
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
createReproduction() {
|
|
47
|
+
if (this.requiresCountingIn && this.songTempo === null) {
|
|
48
|
+
throw new Error('The song tempo is mandatory');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (this.innerPlayer === null) {
|
|
52
|
+
throw new Error('The inner player was not provided.');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let player;
|
|
56
|
+
if (this.trainingMode) {
|
|
57
|
+
player = new YouTubePlayer(this.innerPlayer);
|
|
58
|
+
} else {
|
|
59
|
+
if (this.songDuration === null) {
|
|
60
|
+
throw new Error('The song duration is mandatory');
|
|
61
|
+
}
|
|
62
|
+
player = new PlayAlongPlayer(this.songDuration, this.innerPlayer);
|
|
63
|
+
}
|
|
64
|
+
return new Reproduction(
|
|
65
|
+
player,
|
|
66
|
+
this.requiresCountingIn,
|
|
67
|
+
this.songTempo || 0
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -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
|
+
};
|