@folklore/hooks 0.0.31 → 0.0.32
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/dist/cjs.js +65 -204
- package/dist/es.js +65 -204
- package/package.json +2 -2
package/dist/es.js
CHANGED
|
@@ -19,32 +19,26 @@ function useCounter(desiredValue, _ref) {
|
|
|
19
19
|
let canceled = false;
|
|
20
20
|
const startValue = currentValue;
|
|
21
21
|
const delta = desiredValue - startValue;
|
|
22
|
-
|
|
23
22
|
function loop() {
|
|
24
23
|
if (canceled) {
|
|
25
24
|
return;
|
|
26
25
|
}
|
|
27
|
-
|
|
28
26
|
const currentTime = Date.now();
|
|
29
27
|
const elapsedTime = currentTime - startTime;
|
|
30
28
|
const progress = Math.min(elapsedTime / duration, 1);
|
|
31
29
|
const newValue = Math.round(startValue + progress * delta);
|
|
32
30
|
setCurrentValue(newValue);
|
|
33
|
-
|
|
34
31
|
if (newValue !== desiredValue) {
|
|
35
32
|
animationFrame = raf(loop);
|
|
36
33
|
}
|
|
37
34
|
}
|
|
38
|
-
|
|
39
35
|
if (delta !== 0) {
|
|
40
36
|
duration = Math.min(maxDuration, Math.abs(delta) * speed * 1000);
|
|
41
37
|
startTime = Date.now();
|
|
42
38
|
animationFrame = raf(loop);
|
|
43
39
|
}
|
|
44
|
-
|
|
45
40
|
return () => {
|
|
46
41
|
canceled = true;
|
|
47
|
-
|
|
48
42
|
if (animationFrame !== null) {
|
|
49
43
|
raf.cancel(animationFrame);
|
|
50
44
|
}
|
|
@@ -65,36 +59,29 @@ function usePlayerCurrentTime(player) {
|
|
|
65
59
|
const realCurrentTime = useRef(currentTime);
|
|
66
60
|
const lastIdRef = useRef(id);
|
|
67
61
|
const idChanged = lastIdRef.current !== id;
|
|
68
|
-
|
|
69
62
|
if (idChanged) {
|
|
70
63
|
realCurrentTime.current = 0;
|
|
71
64
|
lastIdRef.current = id;
|
|
72
|
-
}
|
|
73
|
-
|
|
65
|
+
}
|
|
74
66
|
|
|
67
|
+
// Check time update
|
|
75
68
|
useEffect(() => {
|
|
76
69
|
if (disabled || player === null) {
|
|
77
70
|
return () => {};
|
|
78
71
|
}
|
|
79
|
-
|
|
80
72
|
let canceled = false;
|
|
81
|
-
|
|
82
73
|
const updateTime = time => {
|
|
83
74
|
if (canceled) {
|
|
84
75
|
return;
|
|
85
76
|
}
|
|
86
|
-
|
|
87
77
|
realCurrentTime.current = time;
|
|
88
78
|
setCurrentTime(time);
|
|
89
|
-
|
|
90
79
|
if (customOnUpdate !== null) {
|
|
91
80
|
customOnUpdate(time);
|
|
92
81
|
}
|
|
93
82
|
};
|
|
94
|
-
|
|
95
83
|
const interval = setInterval(() => {
|
|
96
84
|
const time = getCurrentTime(player);
|
|
97
|
-
|
|
98
85
|
if (typeof time.then !== 'undefined') {
|
|
99
86
|
time.then(updateTime);
|
|
100
87
|
} else {
|
|
@@ -111,7 +98,6 @@ function usePlayerCurrentTime(player) {
|
|
|
111
98
|
|
|
112
99
|
const noPlayerError$1 = new Error('No player');
|
|
113
100
|
const debug$3 = createDebug('folklore:video:dailymotion');
|
|
114
|
-
|
|
115
101
|
const useDailymotionPlayer = function () {
|
|
116
102
|
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
117
103
|
let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -134,15 +120,14 @@ const useDailymotionPlayer = function () {
|
|
|
134
120
|
if (url === null || url.match(/^https?:/) === null) {
|
|
135
121
|
return url;
|
|
136
122
|
}
|
|
137
|
-
|
|
138
123
|
const match = url.match(/\/video\/([^/?]+)/);
|
|
139
124
|
return match !== null ? match[1] : null;
|
|
140
125
|
}
|
|
141
126
|
} = params;
|
|
142
|
-
const [apiLoaded, setApiLoaded] = useState(typeof window.DM !== 'undefined');
|
|
127
|
+
const [apiLoaded, setApiLoaded] = useState(typeof window !== 'undefined' && typeof window.DM !== 'undefined');
|
|
143
128
|
const [playerReady, setPlayerReady] = useState(false);
|
|
144
129
|
const [loaded, setLoaded] = useState(false);
|
|
145
|
-
const apiRef = useRef(typeof window.DM !== 'undefined' ? window.DM : null);
|
|
130
|
+
const apiRef = useRef(typeof window !== 'undefined' && typeof window.DM !== 'undefined' ? window.DM : null);
|
|
146
131
|
const ready = apiLoaded && playerReady;
|
|
147
132
|
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
148
133
|
const elementRef = useRef(null);
|
|
@@ -162,11 +147,11 @@ const useDailymotionPlayer = function () {
|
|
|
162
147
|
width,
|
|
163
148
|
height,
|
|
164
149
|
duration
|
|
165
|
-
});
|
|
150
|
+
});
|
|
166
151
|
|
|
152
|
+
// Load SDK
|
|
167
153
|
useEffect(() => {
|
|
168
154
|
let canceled = false;
|
|
169
|
-
|
|
170
155
|
if (!apiLoaded && videoId !== null) {
|
|
171
156
|
debug$3('Load API');
|
|
172
157
|
loadDailymotion().then(api => {
|
|
@@ -177,12 +162,12 @@ const useDailymotionPlayer = function () {
|
|
|
177
162
|
}
|
|
178
163
|
});
|
|
179
164
|
}
|
|
180
|
-
|
|
181
165
|
return () => {
|
|
182
166
|
canceled = true;
|
|
183
167
|
};
|
|
184
|
-
}, [videoId, apiLoaded, setApiLoaded]);
|
|
168
|
+
}, [videoId, apiLoaded, setApiLoaded]);
|
|
185
169
|
|
|
170
|
+
// Create or update player
|
|
186
171
|
useEffect(() => {
|
|
187
172
|
const {
|
|
188
173
|
current: DM = null
|
|
@@ -193,11 +178,9 @@ const useDailymotionPlayer = function () {
|
|
|
193
178
|
const {
|
|
194
179
|
current: element = null
|
|
195
180
|
} = elementRef;
|
|
196
|
-
|
|
197
181
|
if (!apiLoaded || videoId === null || element === null) {
|
|
198
182
|
return;
|
|
199
183
|
}
|
|
200
|
-
|
|
201
184
|
const playerParams = {
|
|
202
185
|
'autoplay-d': autoplay,
|
|
203
186
|
// muted,
|
|
@@ -210,7 +193,6 @@ const useDailymotionPlayer = function () {
|
|
|
210
193
|
'ui-start-screen-info': uiStartScreenInfo
|
|
211
194
|
};
|
|
212
195
|
let player = currentPlayer;
|
|
213
|
-
|
|
214
196
|
if (player !== null) {
|
|
215
197
|
player.load(videoId, {
|
|
216
198
|
params: playerParams
|
|
@@ -225,11 +207,9 @@ const useDailymotionPlayer = function () {
|
|
|
225
207
|
});
|
|
226
208
|
debug$3('Create player [ID: %s]', videoId);
|
|
227
209
|
}
|
|
228
|
-
|
|
229
210
|
if (!playerReady) {
|
|
230
211
|
setPlayerReady(true);
|
|
231
212
|
}
|
|
232
|
-
|
|
233
213
|
playerRef.current = player;
|
|
234
214
|
playerElementRef.current = element;
|
|
235
215
|
}, [apiLoaded, elementHasChanged, videoId, width, height, autoplay, muted, start, controls, queueAutoplayNext, queueEnable, sharingEnable, uiLogo, uiStartScreenInfo]);
|
|
@@ -237,43 +217,39 @@ const useDailymotionPlayer = function () {
|
|
|
237
217
|
const {
|
|
238
218
|
current: player = null
|
|
239
219
|
} = playerRef;
|
|
240
|
-
|
|
241
220
|
if (player === null) {
|
|
242
221
|
return () => {};
|
|
243
222
|
}
|
|
244
|
-
|
|
245
223
|
let currentPlayState = playState;
|
|
246
224
|
let currentMetadata = metadata;
|
|
247
|
-
|
|
248
225
|
function onPlaybackReady() {
|
|
249
226
|
setLoaded(true);
|
|
250
227
|
debug$3('onPlaybackReady [ID: %s]', videoId);
|
|
251
228
|
}
|
|
252
|
-
|
|
253
229
|
function onLoadedMetadata() {
|
|
254
|
-
currentMetadata = {
|
|
230
|
+
currentMetadata = {
|
|
231
|
+
...currentMetadata,
|
|
255
232
|
duration: player.duration
|
|
256
233
|
};
|
|
257
234
|
setMetadata(currentMetadata);
|
|
258
235
|
debug$3('onLoadedMetadata [ID: %s]', videoId);
|
|
259
236
|
}
|
|
260
|
-
|
|
261
237
|
function onDurationChange() {
|
|
262
|
-
currentMetadata = {
|
|
238
|
+
currentMetadata = {
|
|
239
|
+
...currentMetadata,
|
|
263
240
|
duration: player.duration
|
|
264
241
|
};
|
|
265
242
|
setMetadata(currentMetadata);
|
|
266
243
|
debug$3('onDurationChange [ID: %s]', videoId);
|
|
267
244
|
}
|
|
268
|
-
|
|
269
245
|
function onVolumeChange() {
|
|
270
246
|
setMuted(player.muted);
|
|
271
247
|
setVolumeState(player.volume);
|
|
272
248
|
debug$3('onVolumeChange [ID: %s]', videoId);
|
|
273
249
|
}
|
|
274
|
-
|
|
275
250
|
function onPlay() {
|
|
276
|
-
currentPlayState = {
|
|
251
|
+
currentPlayState = {
|
|
252
|
+
...currentPlayState,
|
|
277
253
|
playing: true,
|
|
278
254
|
paused: false,
|
|
279
255
|
ended: false
|
|
@@ -281,9 +257,9 @@ const useDailymotionPlayer = function () {
|
|
|
281
257
|
setPlayState(currentPlayState);
|
|
282
258
|
debug$3('onPlay [ID: %s]', videoId);
|
|
283
259
|
}
|
|
284
|
-
|
|
285
260
|
function onPause() {
|
|
286
|
-
currentPlayState = {
|
|
261
|
+
currentPlayState = {
|
|
262
|
+
...currentPlayState,
|
|
287
263
|
playing: false,
|
|
288
264
|
paused: true,
|
|
289
265
|
ended: false
|
|
@@ -291,9 +267,9 @@ const useDailymotionPlayer = function () {
|
|
|
291
267
|
setPlayState(currentPlayState);
|
|
292
268
|
debug$3('onPause [ID: %s]', videoId);
|
|
293
269
|
}
|
|
294
|
-
|
|
295
270
|
function onEnd() {
|
|
296
|
-
currentPlayState = {
|
|
271
|
+
currentPlayState = {
|
|
272
|
+
...currentPlayState,
|
|
297
273
|
playing: false,
|
|
298
274
|
paused: false,
|
|
299
275
|
ended: true
|
|
@@ -301,39 +277,38 @@ const useDailymotionPlayer = function () {
|
|
|
301
277
|
setPlayState(currentPlayState);
|
|
302
278
|
debug$3('onEnd [ID: %s]', videoId);
|
|
303
279
|
}
|
|
304
|
-
|
|
305
280
|
function onPlaying() {
|
|
306
|
-
currentPlayState = {
|
|
281
|
+
currentPlayState = {
|
|
282
|
+
...currentPlayState,
|
|
307
283
|
buffering: false
|
|
308
284
|
};
|
|
309
285
|
setPlayState(currentPlayState);
|
|
310
286
|
debug$3('onPlaying [ID: %s]', videoId);
|
|
311
287
|
}
|
|
312
|
-
|
|
313
288
|
function onWaiting() {
|
|
314
|
-
currentPlayState = {
|
|
289
|
+
currentPlayState = {
|
|
290
|
+
...currentPlayState,
|
|
315
291
|
buffering: true
|
|
316
292
|
};
|
|
317
293
|
setPlayState(currentPlayState);
|
|
318
294
|
debug$3('onWaiting [ID: %s]', videoId);
|
|
319
295
|
}
|
|
320
|
-
|
|
321
296
|
function onAdStart() {
|
|
322
|
-
currentPlayState = {
|
|
297
|
+
currentPlayState = {
|
|
298
|
+
...currentPlayState,
|
|
323
299
|
adPlaying: true
|
|
324
300
|
};
|
|
325
301
|
setPlayState(currentPlayState);
|
|
326
302
|
debug$3('onAdStart [ID: %s]', videoId);
|
|
327
303
|
}
|
|
328
|
-
|
|
329
304
|
function onAdEnd() {
|
|
330
|
-
currentPlayState = {
|
|
305
|
+
currentPlayState = {
|
|
306
|
+
...currentPlayState,
|
|
331
307
|
adPlaying: false
|
|
332
308
|
};
|
|
333
309
|
setPlayState(currentPlayState);
|
|
334
310
|
debug$3('onAdEnd [ID: %s]', videoId);
|
|
335
311
|
}
|
|
336
|
-
|
|
337
312
|
player.addEventListener('playback_ready', onPlaybackReady);
|
|
338
313
|
player.addEventListener('loadedmetadata', onLoadedMetadata);
|
|
339
314
|
player.addEventListener('durationchange', onDurationChange);
|
|
@@ -424,13 +399,11 @@ const useDailymotionPlayer = function () {
|
|
|
424
399
|
};
|
|
425
400
|
|
|
426
401
|
const eventsManager$1 = typeof document !== 'undefined' ? new EventsManager(document) : null;
|
|
427
|
-
|
|
428
402
|
const useDocumentEvent = (event, callback) => {
|
|
429
403
|
useEffect(() => {
|
|
430
404
|
if (eventsManager$1 !== null && callback !== null) {
|
|
431
405
|
eventsManager$1.subscribe(event, callback);
|
|
432
406
|
}
|
|
433
|
-
|
|
434
407
|
return () => {
|
|
435
408
|
if (eventsManager$1 !== null && callback !== null) {
|
|
436
409
|
eventsManager$1.unsubscribe(event, callback);
|
|
@@ -440,13 +413,11 @@ const useDocumentEvent = (event, callback) => {
|
|
|
440
413
|
};
|
|
441
414
|
|
|
442
415
|
const eventsManager = typeof window !== 'undefined' ? new EventsManager(window) : null;
|
|
443
|
-
|
|
444
416
|
const useWindowEvent = (event, callback) => {
|
|
445
417
|
useEffect(() => {
|
|
446
418
|
if (eventsManager !== null && callback !== null) {
|
|
447
419
|
eventsManager.subscribe(event, callback);
|
|
448
420
|
}
|
|
449
|
-
|
|
450
421
|
return () => {
|
|
451
422
|
if (eventsManager !== null && callback !== null) {
|
|
452
423
|
eventsManager.unsubscribe(event, callback);
|
|
@@ -462,13 +433,11 @@ function useKeyboard() {
|
|
|
462
433
|
key
|
|
463
434
|
} = event;
|
|
464
435
|
let callback = null;
|
|
465
|
-
|
|
466
436
|
if (typeof keyMap === 'function') {
|
|
467
437
|
callback = keyMap;
|
|
468
438
|
} else if (typeof keyMap[key] !== 'undefined') {
|
|
469
439
|
callback = typeof keyMap[key] === 'function' ? keyMap[key] : (keyMap[key] || {}).down || null;
|
|
470
440
|
}
|
|
471
|
-
|
|
472
441
|
if (callback !== null) {
|
|
473
442
|
callback(event);
|
|
474
443
|
}
|
|
@@ -478,13 +447,11 @@ function useKeyboard() {
|
|
|
478
447
|
key
|
|
479
448
|
} = event;
|
|
480
449
|
let callback = null;
|
|
481
|
-
|
|
482
450
|
if (typeof keyMap === 'function') {
|
|
483
451
|
callback = keyMap;
|
|
484
452
|
} else if (typeof keyMap[key] !== 'undefined') {
|
|
485
453
|
callback = typeof keyMap[key] === 'function' ? keyMap[key] : (keyMap[key] || {}).up || null;
|
|
486
454
|
}
|
|
487
|
-
|
|
488
455
|
if (callback !== null) {
|
|
489
456
|
callback(event);
|
|
490
457
|
}
|
|
@@ -550,15 +517,13 @@ const useItemsPaginated = function (loader) {
|
|
|
550
517
|
} = _ref4;
|
|
551
518
|
return pagesItems.concat(pageItems);
|
|
552
519
|
}, []) : null;
|
|
553
|
-
|
|
554
|
-
|
|
520
|
+
const updateState = update => setState({
|
|
521
|
+
...state,
|
|
555
522
|
...update
|
|
556
523
|
});
|
|
557
|
-
|
|
558
524
|
const updateFromResponse = function (response) {
|
|
559
525
|
let error = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
560
526
|
let reset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
561
|
-
|
|
562
527
|
if (error !== null) {
|
|
563
528
|
updateState({
|
|
564
529
|
loaded: false,
|
|
@@ -566,7 +531,6 @@ const useItemsPaginated = function (loader) {
|
|
|
566
531
|
});
|
|
567
532
|
throw error;
|
|
568
533
|
}
|
|
569
|
-
|
|
570
534
|
const newPage = getPageFromResponse(response);
|
|
571
535
|
const currentPages = currentPagesRef.current || [];
|
|
572
536
|
const newPages = (reset ? [newPage] : [...currentPages.filter(it => it.page !== newPage.page), newPage]).sort((a, b) => {
|
|
@@ -577,23 +541,18 @@ const useItemsPaginated = function (loader) {
|
|
|
577
541
|
page: bPage = null
|
|
578
542
|
} = b;
|
|
579
543
|
const hasObject = aPage !== null && bPage !== null;
|
|
580
|
-
|
|
581
544
|
if (hasObject) {
|
|
582
545
|
if (aPage === bPage) {
|
|
583
546
|
return 0;
|
|
584
547
|
}
|
|
585
|
-
|
|
586
548
|
return aPage > bPage ? 1 : -1;
|
|
587
549
|
}
|
|
588
|
-
|
|
589
550
|
if (isNumber(a) && isNumber(b)) {
|
|
590
551
|
if (a === b) {
|
|
591
552
|
return 0;
|
|
592
553
|
}
|
|
593
|
-
|
|
594
554
|
return a > b ? 1 : -1;
|
|
595
555
|
}
|
|
596
|
-
|
|
597
556
|
return 0;
|
|
598
557
|
});
|
|
599
558
|
currentPagesRef.current = newPages;
|
|
@@ -606,7 +565,6 @@ const useItemsPaginated = function (loader) {
|
|
|
606
565
|
});
|
|
607
566
|
return newPage;
|
|
608
567
|
};
|
|
609
|
-
|
|
610
568
|
const getNextPage = () => {
|
|
611
569
|
const allPages = lastPage !== -1 ? Array.call(null, ...Array(lastPage)).map((it, index) => index + 1) : [];
|
|
612
570
|
const currentPages = currentPagesRef.current || [];
|
|
@@ -614,7 +572,6 @@ const useItemsPaginated = function (loader) {
|
|
|
614
572
|
const firstItem = remainingPages.length > 0 ? remainingPages.shift() : null;
|
|
615
573
|
return firstItem !== null ? firstItem : null;
|
|
616
574
|
};
|
|
617
|
-
|
|
618
575
|
const loadItems = requestPage => {
|
|
619
576
|
updateState({
|
|
620
577
|
loading: true
|
|
@@ -623,7 +580,6 @@ const useItemsPaginated = function (loader) {
|
|
|
623
580
|
if (onLoaded !== null) {
|
|
624
581
|
onLoaded(response);
|
|
625
582
|
}
|
|
626
|
-
|
|
627
583
|
return response;
|
|
628
584
|
}).catch(error => {
|
|
629
585
|
if (onError !== null) {
|
|
@@ -631,35 +587,28 @@ const useItemsPaginated = function (loader) {
|
|
|
631
587
|
}
|
|
632
588
|
});
|
|
633
589
|
};
|
|
634
|
-
|
|
635
590
|
const loadPage = pageToLoad => {
|
|
636
591
|
if (loading) {
|
|
637
592
|
return Promise.reject();
|
|
638
593
|
}
|
|
639
|
-
|
|
640
594
|
const currentPages = currentPagesRef.current || [];
|
|
641
595
|
const foundPage = currentPages.find(it => it.page === pageToLoad) || null;
|
|
642
|
-
|
|
643
596
|
if (foundPage !== null) {
|
|
644
597
|
return Promise.resolve(foundPage);
|
|
645
598
|
}
|
|
646
|
-
|
|
647
599
|
return loadItems(pageToLoad);
|
|
648
600
|
};
|
|
649
|
-
|
|
650
601
|
const loadNextPage = () => {
|
|
651
602
|
if (loading) {
|
|
652
603
|
return Promise.reject();
|
|
653
604
|
}
|
|
654
|
-
|
|
655
605
|
const nextPage = getNextPage();
|
|
656
606
|
return nextPage !== null ? loadItems(nextPage) : Promise.resolve();
|
|
657
|
-
};
|
|
658
|
-
|
|
607
|
+
};
|
|
659
608
|
|
|
609
|
+
// Reset all on query change
|
|
660
610
|
useEffect(() => {
|
|
661
611
|
const hadState = lastState.current !== null;
|
|
662
|
-
|
|
663
612
|
if (hadState) {
|
|
664
613
|
currentPagesRef.current = null;
|
|
665
614
|
updateState({
|
|
@@ -674,7 +623,6 @@ const useItemsPaginated = function (loader) {
|
|
|
674
623
|
useEffect(() => {
|
|
675
624
|
const hadState = lastState.current !== null;
|
|
676
625
|
lastState.current = initialState;
|
|
677
|
-
|
|
678
626
|
if (hadState) {
|
|
679
627
|
currentPagesRef.current = initialState.pages;
|
|
680
628
|
setState(initialState);
|
|
@@ -684,14 +632,11 @@ const useItemsPaginated = function (loader) {
|
|
|
684
632
|
if (loader === null) {
|
|
685
633
|
return () => {};
|
|
686
634
|
}
|
|
687
|
-
|
|
688
635
|
let loadPromise = null;
|
|
689
636
|
const pageToLoad = initialPages === null && page === null ? 1 : page;
|
|
690
|
-
|
|
691
637
|
if (pageToLoad !== null) {
|
|
692
638
|
loadPromise = loadItems(pageToLoad);
|
|
693
639
|
}
|
|
694
|
-
|
|
695
640
|
return () => {
|
|
696
641
|
if (loadPromise !== null) {
|
|
697
642
|
loadPromise.cancel();
|
|
@@ -721,7 +666,6 @@ const useItemsPaginated = function (loader) {
|
|
|
721
666
|
|
|
722
667
|
const debug$2 = createDebug('folklore:video:native');
|
|
723
668
|
const noPlayerError = new Error('No player');
|
|
724
|
-
|
|
725
669
|
const useNativeVideoPlayer = function (url) {
|
|
726
670
|
let {
|
|
727
671
|
width = 0,
|
|
@@ -761,77 +705,65 @@ const useNativeVideoPlayer = function (url) {
|
|
|
761
705
|
const {
|
|
762
706
|
current: player
|
|
763
707
|
} = elementRef;
|
|
764
|
-
|
|
765
708
|
if (player !== null) {
|
|
766
709
|
player.volume = newVolume;
|
|
767
710
|
return Promise.resolve(newVolume);
|
|
768
711
|
}
|
|
769
|
-
|
|
770
712
|
return Promise.reject(noPlayerError);
|
|
771
713
|
}, []);
|
|
772
714
|
const mute = useCallback(() => {
|
|
773
715
|
const {
|
|
774
716
|
current: player
|
|
775
717
|
} = elementRef;
|
|
776
|
-
|
|
777
718
|
if (player !== null) {
|
|
778
719
|
player.muted = true;
|
|
779
720
|
return Promise.resolve(true);
|
|
780
721
|
}
|
|
781
|
-
|
|
782
722
|
return Promise.reject(noPlayerError);
|
|
783
723
|
}, []);
|
|
784
724
|
const unmute = useCallback(() => {
|
|
785
725
|
const {
|
|
786
726
|
current: player
|
|
787
727
|
} = elementRef;
|
|
788
|
-
|
|
789
728
|
if (player !== null) {
|
|
790
729
|
player.muted = false;
|
|
791
730
|
return Promise.resolve(false);
|
|
792
731
|
}
|
|
793
|
-
|
|
794
732
|
return Promise.reject(noPlayerError);
|
|
795
733
|
}, []);
|
|
796
734
|
const seek = useCallback(newTime => {
|
|
797
735
|
const {
|
|
798
736
|
current: player
|
|
799
737
|
} = elementRef;
|
|
800
|
-
|
|
801
738
|
if (player !== null) {
|
|
802
739
|
player.currentTime = newTime;
|
|
803
740
|
return Promise.resolve(newTime);
|
|
804
741
|
}
|
|
805
|
-
|
|
806
742
|
return Promise.reject(noPlayerError);
|
|
807
743
|
}, []);
|
|
808
744
|
const setLoop = useCallback(newLoop => {
|
|
809
745
|
const {
|
|
810
746
|
current: player
|
|
811
747
|
} = elementRef;
|
|
812
|
-
|
|
813
748
|
if (player !== null) {
|
|
814
749
|
player.loop = newLoop;
|
|
815
750
|
return Promise.resolve(newLoop);
|
|
816
751
|
}
|
|
817
|
-
|
|
818
752
|
return Promise.reject(noPlayerError);
|
|
819
|
-
}, []);
|
|
753
|
+
}, []);
|
|
820
754
|
|
|
755
|
+
// Bind player events
|
|
821
756
|
useEffect(() => {
|
|
822
757
|
const {
|
|
823
758
|
current: player
|
|
824
759
|
} = elementRef;
|
|
825
|
-
|
|
826
760
|
if (player === null) {
|
|
827
761
|
return () => {};
|
|
828
762
|
}
|
|
829
|
-
|
|
830
763
|
const onCanPlay = () => {
|
|
831
764
|
setLoaded(true);
|
|
832
765
|
debug$2('onCanPlay [URL: %s]', url);
|
|
833
766
|
};
|
|
834
|
-
|
|
835
767
|
const onMetadataLoaded = () => {
|
|
836
768
|
const newMetadata = {
|
|
837
769
|
duration: player.duration,
|
|
@@ -841,7 +773,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
841
773
|
setMetadata(newMetadata);
|
|
842
774
|
debug$2('onMetadataLoaded [URL: %s]', url);
|
|
843
775
|
};
|
|
844
|
-
|
|
845
776
|
const onPlay = () => {
|
|
846
777
|
setPlayState({
|
|
847
778
|
playing: true,
|
|
@@ -851,7 +782,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
851
782
|
});
|
|
852
783
|
debug$2('onPlay [URL: %s]', url);
|
|
853
784
|
};
|
|
854
|
-
|
|
855
785
|
const onPause = () => {
|
|
856
786
|
setPlayState({
|
|
857
787
|
playing: false,
|
|
@@ -861,7 +791,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
861
791
|
});
|
|
862
792
|
debug$2('onPause [URL: %s]', url);
|
|
863
793
|
};
|
|
864
|
-
|
|
865
794
|
const onEnded = () => {
|
|
866
795
|
setPlayState({
|
|
867
796
|
playing: false,
|
|
@@ -871,7 +800,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
871
800
|
});
|
|
872
801
|
debug$2('onEnded [URL: %s]', url);
|
|
873
802
|
};
|
|
874
|
-
|
|
875
803
|
debug$2('Bind events [URL: %s]', url);
|
|
876
804
|
player.addEventListener('canplay', onCanPlay);
|
|
877
805
|
player.addEventListener('metadataloaded', onMetadataLoaded);
|
|
@@ -917,39 +845,34 @@ const useNativeVideoPlayer = function (url) {
|
|
|
917
845
|
};
|
|
918
846
|
|
|
919
847
|
const observersCache = new Map();
|
|
920
|
-
|
|
921
848
|
const getOptionsKey = _ref => {
|
|
922
849
|
let {
|
|
923
850
|
root = null,
|
|
924
851
|
rootMargin,
|
|
925
852
|
threshold = null
|
|
926
853
|
} = _ref;
|
|
927
|
-
return
|
|
854
|
+
return `root_${root}_rootMargin_${rootMargin || null}_threshold_${threshold}`;
|
|
928
855
|
};
|
|
929
|
-
|
|
930
856
|
const createObserver = function (Observer) {
|
|
931
857
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
932
858
|
let subscribers = [];
|
|
933
|
-
|
|
934
859
|
const addSubscriber = (element, callback) => {
|
|
935
860
|
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
936
|
-
|
|
937
861
|
if (currentSubscriber !== null) {
|
|
938
|
-
return subscribers.map(it => it.element === element && it.callbacks.indexOf(callback) === -1 ? {
|
|
862
|
+
return subscribers.map(it => it.element === element && it.callbacks.indexOf(callback) === -1 ? {
|
|
863
|
+
...it,
|
|
939
864
|
callbacks: [...it.callbacks, callback]
|
|
940
865
|
} : it).filter(it => it.callbacks.length > 0);
|
|
941
866
|
}
|
|
942
|
-
|
|
943
867
|
return [...subscribers, {
|
|
944
868
|
element,
|
|
945
869
|
callbacks: [callback]
|
|
946
870
|
}];
|
|
947
871
|
};
|
|
948
|
-
|
|
949
|
-
|
|
872
|
+
const removeSubscriber = (element, callback) => subscribers.map(it => it.element === element ? {
|
|
873
|
+
...it,
|
|
950
874
|
callbacks: it.callbacks.filter(subCallback => subCallback !== callback)
|
|
951
875
|
} : it).filter(it => it.callbacks.length > 0);
|
|
952
|
-
|
|
953
876
|
const onUpdate = entries => {
|
|
954
877
|
entries.forEach(entry => {
|
|
955
878
|
subscribers.forEach(_ref2 => {
|
|
@@ -957,7 +880,6 @@ const createObserver = function (Observer) {
|
|
|
957
880
|
element,
|
|
958
881
|
callbacks
|
|
959
882
|
} = _ref2;
|
|
960
|
-
|
|
961
883
|
if (element === entry.target) {
|
|
962
884
|
callbacks.forEach(callback => {
|
|
963
885
|
callback(entry);
|
|
@@ -966,13 +888,10 @@ const createObserver = function (Observer) {
|
|
|
966
888
|
});
|
|
967
889
|
});
|
|
968
890
|
};
|
|
969
|
-
|
|
970
891
|
const observer = new Observer(onUpdate, options);
|
|
971
|
-
|
|
972
892
|
const unsubscribe = function (element) {
|
|
973
893
|
let callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
974
894
|
subscribers = removeSubscriber(element, callback);
|
|
975
|
-
|
|
976
895
|
if (typeof observer.unobserve === 'undefined') {
|
|
977
896
|
observer.disconnect();
|
|
978
897
|
subscribers.forEach(subscriber => {
|
|
@@ -980,45 +899,43 @@ const createObserver = function (Observer) {
|
|
|
980
899
|
});
|
|
981
900
|
return;
|
|
982
901
|
}
|
|
983
|
-
|
|
984
902
|
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
985
|
-
|
|
986
903
|
if (currentSubscriber === null) {
|
|
987
904
|
observer.unobserve(element);
|
|
988
905
|
}
|
|
989
906
|
};
|
|
990
|
-
|
|
991
907
|
const subscribe = (element, callback) => {
|
|
992
908
|
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
993
909
|
subscribers = addSubscriber(element, callback);
|
|
994
|
-
|
|
995
910
|
if (currentSubscriber === null) {
|
|
996
911
|
observer.observe(element);
|
|
997
912
|
}
|
|
998
913
|
};
|
|
999
|
-
|
|
1000
914
|
return {
|
|
1001
915
|
subscribe,
|
|
1002
916
|
unsubscribe,
|
|
1003
917
|
observer
|
|
1004
918
|
};
|
|
1005
919
|
};
|
|
1006
|
-
|
|
1007
|
-
|
|
920
|
+
const getObserver = function () {
|
|
921
|
+
let Observer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
1008
922
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
923
|
+
if (Observer === null) {
|
|
924
|
+
return {
|
|
925
|
+
observer: null,
|
|
926
|
+
subscribe: () => {},
|
|
927
|
+
unsubscribe: () => {}
|
|
928
|
+
};
|
|
929
|
+
}
|
|
1009
930
|
const observerKey = getOptionsKey(options);
|
|
1010
|
-
|
|
1011
931
|
if (!observersCache.has(Observer)) {
|
|
1012
932
|
observersCache.set(Observer, {});
|
|
1013
933
|
}
|
|
1014
|
-
|
|
1015
934
|
const observers = observersCache.get(Observer);
|
|
1016
|
-
|
|
1017
935
|
if (typeof observers[observerKey] === 'undefined') {
|
|
1018
936
|
observers[observerKey] = createObserver(Observer, options);
|
|
1019
937
|
observersCache.set(Observer, observers);
|
|
1020
938
|
}
|
|
1021
|
-
|
|
1022
939
|
return observers[observerKey];
|
|
1023
940
|
};
|
|
1024
941
|
const useObserver = function (Observer) {
|
|
@@ -1039,26 +956,19 @@ const useObserver = function (Observer) {
|
|
|
1039
956
|
const {
|
|
1040
957
|
current: nodeElement
|
|
1041
958
|
} = nodeRef;
|
|
1042
|
-
|
|
1043
959
|
const callback = newEntry => setEntry(newEntry);
|
|
1044
|
-
|
|
1045
960
|
let unsubscribe = null;
|
|
1046
|
-
|
|
1047
961
|
if (nodeElement !== null) {
|
|
1048
962
|
const newOpts = {};
|
|
1049
|
-
|
|
1050
963
|
if (root !== null) {
|
|
1051
964
|
newOpts.root = root;
|
|
1052
965
|
}
|
|
1053
|
-
|
|
1054
966
|
if (rootMargin !== null) {
|
|
1055
967
|
newOpts.rootMargin = rootMargin;
|
|
1056
968
|
}
|
|
1057
|
-
|
|
1058
969
|
if (threshold !== null) {
|
|
1059
970
|
newOpts.threshold = threshold;
|
|
1060
971
|
}
|
|
1061
|
-
|
|
1062
972
|
const {
|
|
1063
973
|
subscribe,
|
|
1064
974
|
unsubscribe: localUnsubscribe
|
|
@@ -1066,7 +976,6 @@ const useObserver = function (Observer) {
|
|
|
1066
976
|
unsubscribe = localUnsubscribe;
|
|
1067
977
|
subscribe(nodeElement, callback);
|
|
1068
978
|
}
|
|
1069
|
-
|
|
1070
979
|
currentElement.current = nodeElement;
|
|
1071
980
|
return () => {
|
|
1072
981
|
if (unsubscribe !== null) {
|
|
@@ -1079,10 +988,10 @@ const useObserver = function (Observer) {
|
|
|
1079
988
|
entry
|
|
1080
989
|
};
|
|
1081
990
|
};
|
|
991
|
+
|
|
1082
992
|
/**
|
|
1083
993
|
* Intersection Observer
|
|
1084
994
|
*/
|
|
1085
|
-
|
|
1086
995
|
const defaultThreshold = [0, 1.0];
|
|
1087
996
|
const intersectionObserverInitialEntry = {
|
|
1088
997
|
target: null,
|
|
@@ -1101,17 +1010,17 @@ const useIntersectionObserver = function () {
|
|
|
1101
1010
|
threshold = defaultThreshold,
|
|
1102
1011
|
disabled = false
|
|
1103
1012
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1104
|
-
return useObserver(IntersectionObserver, {
|
|
1013
|
+
return useObserver(typeof IntersectionObserver !== 'undefined' ? IntersectionObserver : null, {
|
|
1105
1014
|
root,
|
|
1106
1015
|
rootMargin,
|
|
1107
1016
|
threshold,
|
|
1108
1017
|
disabled
|
|
1109
1018
|
}, intersectionObserverInitialEntry);
|
|
1110
1019
|
};
|
|
1020
|
+
|
|
1111
1021
|
/**
|
|
1112
1022
|
* Resize Observer
|
|
1113
1023
|
*/
|
|
1114
|
-
|
|
1115
1024
|
const resizeObserverInitialEntry = {
|
|
1116
1025
|
target: null,
|
|
1117
1026
|
contentRect: null,
|
|
@@ -1122,14 +1031,13 @@ const useResizeObserver = function () {
|
|
|
1122
1031
|
let {
|
|
1123
1032
|
disabled = false
|
|
1124
1033
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1125
|
-
return useObserver(ResizeObserver, {
|
|
1034
|
+
return useObserver(typeof ResizeObserver !== 'undefined' ? ResizeObserver : null, {
|
|
1126
1035
|
disabled
|
|
1127
1036
|
}, resizeObserverInitialEntry);
|
|
1128
1037
|
};
|
|
1129
1038
|
|
|
1130
1039
|
const NO_PLAYER_ERROR$1 = new Error('No player');
|
|
1131
1040
|
const debug$1 = createDebug('folklore:video:youtube');
|
|
1132
|
-
|
|
1133
1041
|
function useYouTubePlayer(id) {
|
|
1134
1042
|
let {
|
|
1135
1043
|
width = 0,
|
|
@@ -1145,14 +1053,13 @@ function useYouTubePlayer(id) {
|
|
|
1145
1053
|
if (url === null || url.match(/^https?:/) === null) {
|
|
1146
1054
|
return url;
|
|
1147
1055
|
}
|
|
1148
|
-
|
|
1149
1056
|
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
|
|
1150
1057
|
const match = url.match(regExp);
|
|
1151
1058
|
return match && match[7].length === 11 ? match[7] : null;
|
|
1152
1059
|
}
|
|
1153
1060
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1154
|
-
const [apiLoaded, setApiLoaded] = useState(typeof window.YT !== 'undefined');
|
|
1155
|
-
const apiRef = useRef(typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1061
|
+
const [apiLoaded, setApiLoaded] = useState(typeof window !== 'undefined' && typeof window.YT !== 'undefined');
|
|
1062
|
+
const apiRef = useRef(typeof window !== 'undefined' && typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1156
1063
|
const elementRef = useRef(null);
|
|
1157
1064
|
const playerRef = useRef(null);
|
|
1158
1065
|
const playerElementRef = useRef(elementRef.current);
|
|
@@ -1173,7 +1080,6 @@ function useYouTubePlayer(id) {
|
|
|
1173
1080
|
});
|
|
1174
1081
|
useEffect(() => {
|
|
1175
1082
|
let canceled = false;
|
|
1176
|
-
|
|
1177
1083
|
if (!apiLoaded && videoId !== null) {
|
|
1178
1084
|
debug$1('Load API');
|
|
1179
1085
|
loadYouTube().then(api => {
|
|
@@ -1184,7 +1090,6 @@ function useYouTubePlayer(id) {
|
|
|
1184
1090
|
}
|
|
1185
1091
|
});
|
|
1186
1092
|
}
|
|
1187
|
-
|
|
1188
1093
|
return () => {
|
|
1189
1094
|
canceled = true;
|
|
1190
1095
|
};
|
|
@@ -1206,11 +1111,9 @@ function useYouTubePlayer(id) {
|
|
|
1206
1111
|
current: player
|
|
1207
1112
|
} = playerRef;
|
|
1208
1113
|
const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1209
|
-
|
|
1210
1114
|
if (customOnVolumeChange) {
|
|
1211
1115
|
customOnVolumeChange(volume);
|
|
1212
1116
|
}
|
|
1213
|
-
|
|
1214
1117
|
return promise;
|
|
1215
1118
|
}, [customOnVolumeChange]);
|
|
1216
1119
|
const mute = useCallback(() => {
|
|
@@ -1242,19 +1145,21 @@ function useYouTubePlayer(id) {
|
|
|
1242
1145
|
debug$1('Unset player');
|
|
1243
1146
|
playerRef.current = null;
|
|
1244
1147
|
}
|
|
1245
|
-
}, []);
|
|
1148
|
+
}, []);
|
|
1149
|
+
|
|
1150
|
+
// Detect iframe switch and destroy player
|
|
1246
1151
|
|
|
1247
1152
|
useEffect(() => {
|
|
1248
1153
|
const {
|
|
1249
1154
|
current: currentPlayer
|
|
1250
1155
|
} = playerRef;
|
|
1251
|
-
|
|
1252
1156
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1253
1157
|
debug$1('iFrame switched');
|
|
1254
1158
|
destroyPlayer();
|
|
1255
1159
|
}
|
|
1256
|
-
}, [elementHasChanged]);
|
|
1160
|
+
}, [elementHasChanged]);
|
|
1257
1161
|
|
|
1162
|
+
// Create player
|
|
1258
1163
|
useEffect(() => {
|
|
1259
1164
|
const {
|
|
1260
1165
|
current: YT = null
|
|
@@ -1265,20 +1170,16 @@ function useYouTubePlayer(id) {
|
|
|
1265
1170
|
const {
|
|
1266
1171
|
current: element = null
|
|
1267
1172
|
} = elementRef;
|
|
1268
|
-
|
|
1269
1173
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1270
1174
|
destroyPlayer();
|
|
1271
1175
|
return;
|
|
1272
1176
|
}
|
|
1273
|
-
|
|
1274
1177
|
let player = currentPlayer;
|
|
1275
|
-
|
|
1276
1178
|
if (player !== null && typeof player.loadVideoById !== 'undefined') {
|
|
1277
1179
|
debug$1('Switch video [ID: %s]', videoId);
|
|
1278
1180
|
player.loadVideoById(videoId);
|
|
1279
1181
|
} else {
|
|
1280
1182
|
debug$1('Create player [ID: %s]', videoId);
|
|
1281
|
-
|
|
1282
1183
|
const onReady = _ref => {
|
|
1283
1184
|
let {
|
|
1284
1185
|
target
|
|
@@ -1287,16 +1188,14 @@ function useYouTubePlayer(id) {
|
|
|
1287
1188
|
playerRef.current = target;
|
|
1288
1189
|
setReady(true);
|
|
1289
1190
|
const newDuration = target.getDuration();
|
|
1290
|
-
|
|
1291
1191
|
if (newDuration !== metadata.duration) {
|
|
1292
|
-
setMetadata({
|
|
1192
|
+
setMetadata({
|
|
1193
|
+
...metadata,
|
|
1293
1194
|
duration: newDuration
|
|
1294
1195
|
});
|
|
1295
1196
|
}
|
|
1296
|
-
|
|
1297
1197
|
debug$1('onReady [ID: %s]', videoId);
|
|
1298
1198
|
};
|
|
1299
|
-
|
|
1300
1199
|
const onStateChange = _ref2 => {
|
|
1301
1200
|
let {
|
|
1302
1201
|
data: state
|
|
@@ -1309,7 +1208,6 @@ function useYouTubePlayer(id) {
|
|
|
1309
1208
|
};
|
|
1310
1209
|
setPlayState(newState);
|
|
1311
1210
|
let stateLabel = null;
|
|
1312
|
-
|
|
1313
1211
|
if (newState.playing) {
|
|
1314
1212
|
stateLabel = 'playing';
|
|
1315
1213
|
} else if (newState.paused) {
|
|
@@ -1323,10 +1221,8 @@ function useYouTubePlayer(id) {
|
|
|
1323
1221
|
} else if (state === 0) {
|
|
1324
1222
|
stateLabel = 'stopped';
|
|
1325
1223
|
}
|
|
1326
|
-
|
|
1327
1224
|
debug$1('onStateChange %s [ID: %s]', stateLabel, videoId);
|
|
1328
1225
|
};
|
|
1329
|
-
|
|
1330
1226
|
player = new YT.Player(element, {
|
|
1331
1227
|
videoId,
|
|
1332
1228
|
playerVars: {
|
|
@@ -1344,7 +1240,6 @@ function useYouTubePlayer(id) {
|
|
|
1344
1240
|
}
|
|
1345
1241
|
});
|
|
1346
1242
|
}
|
|
1347
|
-
|
|
1348
1243
|
playerRef.current = player;
|
|
1349
1244
|
playerElementRef.current = element;
|
|
1350
1245
|
}, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
|
|
@@ -1380,7 +1275,6 @@ function useYouTubePlayer(id) {
|
|
|
1380
1275
|
|
|
1381
1276
|
const debug = createDebug('folklore:video:vimeo');
|
|
1382
1277
|
const NO_PLAYER_ERROR = new Error('No player');
|
|
1383
|
-
|
|
1384
1278
|
const useVimeoPlayer = function (id) {
|
|
1385
1279
|
let {
|
|
1386
1280
|
width = 0,
|
|
@@ -1397,7 +1291,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1397
1291
|
if (url === null || url.match(/^[0-9]+$/) !== null) {
|
|
1398
1292
|
return url;
|
|
1399
1293
|
}
|
|
1400
|
-
|
|
1401
1294
|
const match = url.match(/\/[0-9]+/);
|
|
1402
1295
|
return match !== null ? match[1] : null;
|
|
1403
1296
|
}
|
|
@@ -1422,11 +1315,11 @@ const useVimeoPlayer = function (id) {
|
|
|
1422
1315
|
width,
|
|
1423
1316
|
height,
|
|
1424
1317
|
duration
|
|
1425
|
-
});
|
|
1318
|
+
});
|
|
1426
1319
|
|
|
1320
|
+
// Load SDK
|
|
1427
1321
|
useEffect(() => {
|
|
1428
1322
|
let canceled = false;
|
|
1429
|
-
|
|
1430
1323
|
if (!apiLoaded && id !== null) {
|
|
1431
1324
|
debug('Load API');
|
|
1432
1325
|
loadVimeo().then(api => {
|
|
@@ -1437,7 +1330,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1437
1330
|
}
|
|
1438
1331
|
});
|
|
1439
1332
|
}
|
|
1440
|
-
|
|
1441
1333
|
return () => {
|
|
1442
1334
|
canceled = true;
|
|
1443
1335
|
};
|
|
@@ -1488,12 +1380,10 @@ const useVimeoPlayer = function (id) {
|
|
|
1488
1380
|
const {
|
|
1489
1381
|
current: player
|
|
1490
1382
|
} = playerRef;
|
|
1491
|
-
|
|
1492
1383
|
if (player !== null) {
|
|
1493
1384
|
debug('Unload video');
|
|
1494
1385
|
player.unload();
|
|
1495
1386
|
}
|
|
1496
|
-
|
|
1497
1387
|
if (player !== null) {
|
|
1498
1388
|
debug('Unset video');
|
|
1499
1389
|
playerRef.current = null;
|
|
@@ -1503,13 +1393,13 @@ const useVimeoPlayer = function (id) {
|
|
|
1503
1393
|
const {
|
|
1504
1394
|
current: currentPlayer
|
|
1505
1395
|
} = playerRef;
|
|
1506
|
-
|
|
1507
1396
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1508
1397
|
debug('iFrame switched');
|
|
1509
1398
|
destroyVideo();
|
|
1510
1399
|
}
|
|
1511
|
-
}, [elementHasChanged]);
|
|
1400
|
+
}, [elementHasChanged]);
|
|
1512
1401
|
|
|
1402
|
+
// Create player
|
|
1513
1403
|
useEffect(() => {
|
|
1514
1404
|
const {
|
|
1515
1405
|
current: Player = null
|
|
@@ -1520,14 +1410,11 @@ const useVimeoPlayer = function (id) {
|
|
|
1520
1410
|
const {
|
|
1521
1411
|
current: element = null
|
|
1522
1412
|
} = elementRef;
|
|
1523
|
-
|
|
1524
1413
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1525
1414
|
destroyVideo();
|
|
1526
1415
|
return;
|
|
1527
1416
|
}
|
|
1528
|
-
|
|
1529
1417
|
let player = currentPlayer;
|
|
1530
|
-
|
|
1531
1418
|
if (player === null) {
|
|
1532
1419
|
debug('Create player [ID: %s]', videoId);
|
|
1533
1420
|
player = new Player(element, {
|
|
@@ -1548,46 +1435,38 @@ const useVimeoPlayer = function (id) {
|
|
|
1548
1435
|
debug('ERROR: %o', e);
|
|
1549
1436
|
});
|
|
1550
1437
|
}
|
|
1551
|
-
|
|
1552
1438
|
playerRef.current = player;
|
|
1553
1439
|
playerElementRef.current = element;
|
|
1554
|
-
}, [apiLoaded, videoId, elementHasChanged, setReady, destroyVideo, setLoaded]);
|
|
1440
|
+
}, [apiLoaded, videoId, elementHasChanged, setReady, destroyVideo, setLoaded]);
|
|
1555
1441
|
|
|
1442
|
+
// Bind player events
|
|
1556
1443
|
useEffect(() => {
|
|
1557
1444
|
const {
|
|
1558
1445
|
current: player
|
|
1559
1446
|
} = playerRef;
|
|
1560
|
-
|
|
1561
1447
|
if (player === null) {
|
|
1562
1448
|
return () => {};
|
|
1563
1449
|
}
|
|
1564
|
-
|
|
1565
1450
|
let canceled = false;
|
|
1566
|
-
|
|
1567
1451
|
const onLoaded = () => {
|
|
1568
1452
|
Promise.all([player.getDuration(), player.getVideoWidth(), player.getVideoHeight(), player.getMuted()]).then(_ref => {
|
|
1569
1453
|
let [newDuration, newWidth, newHeight, newMuted] = _ref;
|
|
1570
|
-
|
|
1571
1454
|
if (canceled) {
|
|
1572
1455
|
return;
|
|
1573
1456
|
}
|
|
1574
|
-
|
|
1575
1457
|
const newMetadata = {
|
|
1576
1458
|
duration: newDuration,
|
|
1577
1459
|
width: newWidth,
|
|
1578
1460
|
height: newHeight
|
|
1579
1461
|
};
|
|
1580
1462
|
setMetadata(newMetadata);
|
|
1581
|
-
|
|
1582
1463
|
if (newMuted) {
|
|
1583
1464
|
setVolumeState(0);
|
|
1584
1465
|
}
|
|
1585
|
-
|
|
1586
1466
|
setLoaded(true);
|
|
1587
1467
|
});
|
|
1588
1468
|
debug('onLoaded [ID: %s]', videoId);
|
|
1589
1469
|
};
|
|
1590
|
-
|
|
1591
1470
|
const onPlay = () => {
|
|
1592
1471
|
setPlayState({
|
|
1593
1472
|
playing: true,
|
|
@@ -1602,7 +1481,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1602
1481
|
}
|
|
1603
1482
|
}).catch(() => {});
|
|
1604
1483
|
};
|
|
1605
|
-
|
|
1606
1484
|
const onPause = () => {
|
|
1607
1485
|
setPlayState({
|
|
1608
1486
|
playing: false,
|
|
@@ -1612,7 +1490,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1612
1490
|
});
|
|
1613
1491
|
debug('onPause [ID: %s]', videoId);
|
|
1614
1492
|
};
|
|
1615
|
-
|
|
1616
1493
|
const onVolumeChange = _ref2 => {
|
|
1617
1494
|
let {
|
|
1618
1495
|
volume: newVolume
|
|
@@ -1620,7 +1497,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1620
1497
|
setVolumeState(newVolume);
|
|
1621
1498
|
debug('onVolumeChange [ID: %s]', videoId);
|
|
1622
1499
|
};
|
|
1623
|
-
|
|
1624
1500
|
const onEnded = () => {
|
|
1625
1501
|
setPlayState({
|
|
1626
1502
|
playing: false,
|
|
@@ -1630,7 +1506,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1630
1506
|
});
|
|
1631
1507
|
debug('onEnded [ID: %s]', videoId);
|
|
1632
1508
|
};
|
|
1633
|
-
|
|
1634
1509
|
const onBufferStart = () => {
|
|
1635
1510
|
setPlayState({
|
|
1636
1511
|
playing: true,
|
|
@@ -1640,7 +1515,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1640
1515
|
});
|
|
1641
1516
|
debug('onBufferStart [ID: %s]', videoId);
|
|
1642
1517
|
};
|
|
1643
|
-
|
|
1644
1518
|
const onBufferEnded = () => {
|
|
1645
1519
|
setPlayState({
|
|
1646
1520
|
playing: true,
|
|
@@ -1650,7 +1524,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1650
1524
|
});
|
|
1651
1525
|
debug('onBufferStart [ID: %s]', videoId);
|
|
1652
1526
|
};
|
|
1653
|
-
|
|
1654
1527
|
debug('Bind events [ID: %s]', videoId);
|
|
1655
1528
|
player.on('loaded', onLoaded);
|
|
1656
1529
|
player.on('play', onPlay);
|
|
@@ -1720,7 +1593,6 @@ function useVideoPlayer(params) {
|
|
|
1720
1593
|
const vimeoPlayer = useVimeoPlayer(service === 'vimeo' ? videoId || url : null, params);
|
|
1721
1594
|
const nativePlayer = useNativeVideoPlayer(url, params);
|
|
1722
1595
|
let player = null;
|
|
1723
|
-
|
|
1724
1596
|
if (service === 'dailymotion') {
|
|
1725
1597
|
player = dailymotionPlayer;
|
|
1726
1598
|
} else if (service === 'youtube') {
|
|
@@ -1730,7 +1602,6 @@ function useVideoPlayer(params) {
|
|
|
1730
1602
|
} else {
|
|
1731
1603
|
player = nativePlayer;
|
|
1732
1604
|
}
|
|
1733
|
-
|
|
1734
1605
|
const {
|
|
1735
1606
|
playing = false,
|
|
1736
1607
|
paused = false,
|
|
@@ -1770,7 +1641,6 @@ function useVideoPlayer(params) {
|
|
|
1770
1641
|
}, [ended, customOnEnd]);
|
|
1771
1642
|
useEffect(() => {
|
|
1772
1643
|
const hasMetadata = metaWidth !== null || metaHeight !== null || metaDuration !== null;
|
|
1773
|
-
|
|
1774
1644
|
if (hasMetadata && customOnMetadataChange !== null) {
|
|
1775
1645
|
customOnMetadataChange({
|
|
1776
1646
|
width: metaWidth,
|
|
@@ -1786,9 +1656,7 @@ const getWindowScroll = () => ({
|
|
|
1786
1656
|
x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
|
|
1787
1657
|
y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
|
|
1788
1658
|
});
|
|
1789
|
-
|
|
1790
1659
|
const currentScroll = getWindowScroll();
|
|
1791
|
-
|
|
1792
1660
|
const useWindowScroll = function () {
|
|
1793
1661
|
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1794
1662
|
const {
|
|
@@ -1797,19 +1665,16 @@ const useWindowScroll = function () {
|
|
|
1797
1665
|
const [scroll, setScroll] = useState(currentScroll);
|
|
1798
1666
|
const updateScroll = useCallback(() => {
|
|
1799
1667
|
const newScroll = getWindowScroll();
|
|
1800
|
-
|
|
1801
1668
|
if (currentScroll.x !== newScroll.x || currentScroll.y !== newScroll.y) {
|
|
1802
1669
|
currentScroll.x = newScroll.x;
|
|
1803
1670
|
currentScroll.y = newScroll.y;
|
|
1804
1671
|
setScroll(newScroll);
|
|
1805
1672
|
return newScroll;
|
|
1806
1673
|
}
|
|
1807
|
-
|
|
1808
1674
|
return null;
|
|
1809
1675
|
}, [setScroll]);
|
|
1810
1676
|
const onScroll = useCallback(() => {
|
|
1811
1677
|
const newScroll = updateScroll();
|
|
1812
|
-
|
|
1813
1678
|
if (newScroll !== null && onChange !== null) {
|
|
1814
1679
|
onChange(newScroll);
|
|
1815
1680
|
}
|
|
@@ -1826,7 +1691,6 @@ const getWindowSize = () => ({
|
|
|
1826
1691
|
height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
|
|
1827
1692
|
});
|
|
1828
1693
|
let currentSize = getWindowSize();
|
|
1829
|
-
|
|
1830
1694
|
function useWindowSize() {
|
|
1831
1695
|
let {
|
|
1832
1696
|
onChange = null
|
|
@@ -1834,18 +1698,15 @@ function useWindowSize() {
|
|
|
1834
1698
|
const [size, setSize] = useState(currentSize);
|
|
1835
1699
|
const updateSize = useCallback(() => {
|
|
1836
1700
|
const newSize = getWindowSize();
|
|
1837
|
-
|
|
1838
1701
|
if (currentSize.width !== newSize.width || currentSize.height !== newSize.height) {
|
|
1839
1702
|
currentSize = newSize;
|
|
1840
1703
|
setSize(newSize);
|
|
1841
1704
|
return newSize;
|
|
1842
1705
|
}
|
|
1843
|
-
|
|
1844
1706
|
return null;
|
|
1845
1707
|
}, [setSize]);
|
|
1846
1708
|
const onResize = useCallback(() => {
|
|
1847
1709
|
const newSize = updateSize();
|
|
1848
|
-
|
|
1849
1710
|
if (newSize !== null && onChange !== null) {
|
|
1850
1711
|
onChange(newSize);
|
|
1851
1712
|
}
|