@folklore/hooks 0.0.30 → 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 +3 -3
package/dist/cjs.js
CHANGED
|
@@ -30,32 +30,26 @@ function useCounter(desiredValue, _ref) {
|
|
|
30
30
|
let canceled = false;
|
|
31
31
|
const startValue = currentValue;
|
|
32
32
|
const delta = desiredValue - startValue;
|
|
33
|
-
|
|
34
33
|
function loop() {
|
|
35
34
|
if (canceled) {
|
|
36
35
|
return;
|
|
37
36
|
}
|
|
38
|
-
|
|
39
37
|
const currentTime = Date.now();
|
|
40
38
|
const elapsedTime = currentTime - startTime;
|
|
41
39
|
const progress = Math.min(elapsedTime / duration, 1);
|
|
42
40
|
const newValue = Math.round(startValue + progress * delta);
|
|
43
41
|
setCurrentValue(newValue);
|
|
44
|
-
|
|
45
42
|
if (newValue !== desiredValue) {
|
|
46
43
|
animationFrame = raf__default["default"](loop);
|
|
47
44
|
}
|
|
48
45
|
}
|
|
49
|
-
|
|
50
46
|
if (delta !== 0) {
|
|
51
47
|
duration = Math.min(maxDuration, Math.abs(delta) * speed * 1000);
|
|
52
48
|
startTime = Date.now();
|
|
53
49
|
animationFrame = raf__default["default"](loop);
|
|
54
50
|
}
|
|
55
|
-
|
|
56
51
|
return () => {
|
|
57
52
|
canceled = true;
|
|
58
|
-
|
|
59
53
|
if (animationFrame !== null) {
|
|
60
54
|
raf__default["default"].cancel(animationFrame);
|
|
61
55
|
}
|
|
@@ -76,36 +70,29 @@ function usePlayerCurrentTime(player) {
|
|
|
76
70
|
const realCurrentTime = react.useRef(currentTime);
|
|
77
71
|
const lastIdRef = react.useRef(id);
|
|
78
72
|
const idChanged = lastIdRef.current !== id;
|
|
79
|
-
|
|
80
73
|
if (idChanged) {
|
|
81
74
|
realCurrentTime.current = 0;
|
|
82
75
|
lastIdRef.current = id;
|
|
83
|
-
}
|
|
84
|
-
|
|
76
|
+
}
|
|
85
77
|
|
|
78
|
+
// Check time update
|
|
86
79
|
react.useEffect(() => {
|
|
87
80
|
if (disabled || player === null) {
|
|
88
81
|
return () => {};
|
|
89
82
|
}
|
|
90
|
-
|
|
91
83
|
let canceled = false;
|
|
92
|
-
|
|
93
84
|
const updateTime = time => {
|
|
94
85
|
if (canceled) {
|
|
95
86
|
return;
|
|
96
87
|
}
|
|
97
|
-
|
|
98
88
|
realCurrentTime.current = time;
|
|
99
89
|
setCurrentTime(time);
|
|
100
|
-
|
|
101
90
|
if (customOnUpdate !== null) {
|
|
102
91
|
customOnUpdate(time);
|
|
103
92
|
}
|
|
104
93
|
};
|
|
105
|
-
|
|
106
94
|
const interval = setInterval(() => {
|
|
107
95
|
const time = getCurrentTime(player);
|
|
108
|
-
|
|
109
96
|
if (typeof time.then !== 'undefined') {
|
|
110
97
|
time.then(updateTime);
|
|
111
98
|
} else {
|
|
@@ -122,7 +109,6 @@ function usePlayerCurrentTime(player) {
|
|
|
122
109
|
|
|
123
110
|
const noPlayerError$1 = new Error('No player');
|
|
124
111
|
const debug$3 = createDebug__default["default"]('folklore:video:dailymotion');
|
|
125
|
-
|
|
126
112
|
const useDailymotionPlayer = function () {
|
|
127
113
|
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
128
114
|
let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -145,15 +131,14 @@ const useDailymotionPlayer = function () {
|
|
|
145
131
|
if (url === null || url.match(/^https?:/) === null) {
|
|
146
132
|
return url;
|
|
147
133
|
}
|
|
148
|
-
|
|
149
134
|
const match = url.match(/\/video\/([^/?]+)/);
|
|
150
135
|
return match !== null ? match[1] : null;
|
|
151
136
|
}
|
|
152
137
|
} = params;
|
|
153
|
-
const [apiLoaded, setApiLoaded] = react.useState(typeof window.DM !== 'undefined');
|
|
138
|
+
const [apiLoaded, setApiLoaded] = react.useState(typeof window !== 'undefined' && typeof window.DM !== 'undefined');
|
|
154
139
|
const [playerReady, setPlayerReady] = react.useState(false);
|
|
155
140
|
const [loaded, setLoaded] = react.useState(false);
|
|
156
|
-
const apiRef = react.useRef(typeof window.DM !== 'undefined' ? window.DM : null);
|
|
141
|
+
const apiRef = react.useRef(typeof window !== 'undefined' && typeof window.DM !== 'undefined' ? window.DM : null);
|
|
157
142
|
const ready = apiLoaded && playerReady;
|
|
158
143
|
const videoId = react.useMemo(() => getVideoId(id), [id]);
|
|
159
144
|
const elementRef = react.useRef(null);
|
|
@@ -173,11 +158,11 @@ const useDailymotionPlayer = function () {
|
|
|
173
158
|
width,
|
|
174
159
|
height,
|
|
175
160
|
duration
|
|
176
|
-
});
|
|
161
|
+
});
|
|
177
162
|
|
|
163
|
+
// Load SDK
|
|
178
164
|
react.useEffect(() => {
|
|
179
165
|
let canceled = false;
|
|
180
|
-
|
|
181
166
|
if (!apiLoaded && videoId !== null) {
|
|
182
167
|
debug$3('Load API');
|
|
183
168
|
services.loadDailymotion().then(api => {
|
|
@@ -188,12 +173,12 @@ const useDailymotionPlayer = function () {
|
|
|
188
173
|
}
|
|
189
174
|
});
|
|
190
175
|
}
|
|
191
|
-
|
|
192
176
|
return () => {
|
|
193
177
|
canceled = true;
|
|
194
178
|
};
|
|
195
|
-
}, [videoId, apiLoaded, setApiLoaded]);
|
|
179
|
+
}, [videoId, apiLoaded, setApiLoaded]);
|
|
196
180
|
|
|
181
|
+
// Create or update player
|
|
197
182
|
react.useEffect(() => {
|
|
198
183
|
const {
|
|
199
184
|
current: DM = null
|
|
@@ -204,11 +189,9 @@ const useDailymotionPlayer = function () {
|
|
|
204
189
|
const {
|
|
205
190
|
current: element = null
|
|
206
191
|
} = elementRef;
|
|
207
|
-
|
|
208
192
|
if (!apiLoaded || videoId === null || element === null) {
|
|
209
193
|
return;
|
|
210
194
|
}
|
|
211
|
-
|
|
212
195
|
const playerParams = {
|
|
213
196
|
'autoplay-d': autoplay,
|
|
214
197
|
// muted,
|
|
@@ -221,7 +204,6 @@ const useDailymotionPlayer = function () {
|
|
|
221
204
|
'ui-start-screen-info': uiStartScreenInfo
|
|
222
205
|
};
|
|
223
206
|
let player = currentPlayer;
|
|
224
|
-
|
|
225
207
|
if (player !== null) {
|
|
226
208
|
player.load(videoId, {
|
|
227
209
|
params: playerParams
|
|
@@ -236,11 +218,9 @@ const useDailymotionPlayer = function () {
|
|
|
236
218
|
});
|
|
237
219
|
debug$3('Create player [ID: %s]', videoId);
|
|
238
220
|
}
|
|
239
|
-
|
|
240
221
|
if (!playerReady) {
|
|
241
222
|
setPlayerReady(true);
|
|
242
223
|
}
|
|
243
|
-
|
|
244
224
|
playerRef.current = player;
|
|
245
225
|
playerElementRef.current = element;
|
|
246
226
|
}, [apiLoaded, elementHasChanged, videoId, width, height, autoplay, muted, start, controls, queueAutoplayNext, queueEnable, sharingEnable, uiLogo, uiStartScreenInfo]);
|
|
@@ -248,43 +228,39 @@ const useDailymotionPlayer = function () {
|
|
|
248
228
|
const {
|
|
249
229
|
current: player = null
|
|
250
230
|
} = playerRef;
|
|
251
|
-
|
|
252
231
|
if (player === null) {
|
|
253
232
|
return () => {};
|
|
254
233
|
}
|
|
255
|
-
|
|
256
234
|
let currentPlayState = playState;
|
|
257
235
|
let currentMetadata = metadata;
|
|
258
|
-
|
|
259
236
|
function onPlaybackReady() {
|
|
260
237
|
setLoaded(true);
|
|
261
238
|
debug$3('onPlaybackReady [ID: %s]', videoId);
|
|
262
239
|
}
|
|
263
|
-
|
|
264
240
|
function onLoadedMetadata() {
|
|
265
|
-
currentMetadata = {
|
|
241
|
+
currentMetadata = {
|
|
242
|
+
...currentMetadata,
|
|
266
243
|
duration: player.duration
|
|
267
244
|
};
|
|
268
245
|
setMetadata(currentMetadata);
|
|
269
246
|
debug$3('onLoadedMetadata [ID: %s]', videoId);
|
|
270
247
|
}
|
|
271
|
-
|
|
272
248
|
function onDurationChange() {
|
|
273
|
-
currentMetadata = {
|
|
249
|
+
currentMetadata = {
|
|
250
|
+
...currentMetadata,
|
|
274
251
|
duration: player.duration
|
|
275
252
|
};
|
|
276
253
|
setMetadata(currentMetadata);
|
|
277
254
|
debug$3('onDurationChange [ID: %s]', videoId);
|
|
278
255
|
}
|
|
279
|
-
|
|
280
256
|
function onVolumeChange() {
|
|
281
257
|
setMuted(player.muted);
|
|
282
258
|
setVolumeState(player.volume);
|
|
283
259
|
debug$3('onVolumeChange [ID: %s]', videoId);
|
|
284
260
|
}
|
|
285
|
-
|
|
286
261
|
function onPlay() {
|
|
287
|
-
currentPlayState = {
|
|
262
|
+
currentPlayState = {
|
|
263
|
+
...currentPlayState,
|
|
288
264
|
playing: true,
|
|
289
265
|
paused: false,
|
|
290
266
|
ended: false
|
|
@@ -292,9 +268,9 @@ const useDailymotionPlayer = function () {
|
|
|
292
268
|
setPlayState(currentPlayState);
|
|
293
269
|
debug$3('onPlay [ID: %s]', videoId);
|
|
294
270
|
}
|
|
295
|
-
|
|
296
271
|
function onPause() {
|
|
297
|
-
currentPlayState = {
|
|
272
|
+
currentPlayState = {
|
|
273
|
+
...currentPlayState,
|
|
298
274
|
playing: false,
|
|
299
275
|
paused: true,
|
|
300
276
|
ended: false
|
|
@@ -302,9 +278,9 @@ const useDailymotionPlayer = function () {
|
|
|
302
278
|
setPlayState(currentPlayState);
|
|
303
279
|
debug$3('onPause [ID: %s]', videoId);
|
|
304
280
|
}
|
|
305
|
-
|
|
306
281
|
function onEnd() {
|
|
307
|
-
currentPlayState = {
|
|
282
|
+
currentPlayState = {
|
|
283
|
+
...currentPlayState,
|
|
308
284
|
playing: false,
|
|
309
285
|
paused: false,
|
|
310
286
|
ended: true
|
|
@@ -312,39 +288,38 @@ const useDailymotionPlayer = function () {
|
|
|
312
288
|
setPlayState(currentPlayState);
|
|
313
289
|
debug$3('onEnd [ID: %s]', videoId);
|
|
314
290
|
}
|
|
315
|
-
|
|
316
291
|
function onPlaying() {
|
|
317
|
-
currentPlayState = {
|
|
292
|
+
currentPlayState = {
|
|
293
|
+
...currentPlayState,
|
|
318
294
|
buffering: false
|
|
319
295
|
};
|
|
320
296
|
setPlayState(currentPlayState);
|
|
321
297
|
debug$3('onPlaying [ID: %s]', videoId);
|
|
322
298
|
}
|
|
323
|
-
|
|
324
299
|
function onWaiting() {
|
|
325
|
-
currentPlayState = {
|
|
300
|
+
currentPlayState = {
|
|
301
|
+
...currentPlayState,
|
|
326
302
|
buffering: true
|
|
327
303
|
};
|
|
328
304
|
setPlayState(currentPlayState);
|
|
329
305
|
debug$3('onWaiting [ID: %s]', videoId);
|
|
330
306
|
}
|
|
331
|
-
|
|
332
307
|
function onAdStart() {
|
|
333
|
-
currentPlayState = {
|
|
308
|
+
currentPlayState = {
|
|
309
|
+
...currentPlayState,
|
|
334
310
|
adPlaying: true
|
|
335
311
|
};
|
|
336
312
|
setPlayState(currentPlayState);
|
|
337
313
|
debug$3('onAdStart [ID: %s]', videoId);
|
|
338
314
|
}
|
|
339
|
-
|
|
340
315
|
function onAdEnd() {
|
|
341
|
-
currentPlayState = {
|
|
316
|
+
currentPlayState = {
|
|
317
|
+
...currentPlayState,
|
|
342
318
|
adPlaying: false
|
|
343
319
|
};
|
|
344
320
|
setPlayState(currentPlayState);
|
|
345
321
|
debug$3('onAdEnd [ID: %s]', videoId);
|
|
346
322
|
}
|
|
347
|
-
|
|
348
323
|
player.addEventListener('playback_ready', onPlaybackReady);
|
|
349
324
|
player.addEventListener('loadedmetadata', onLoadedMetadata);
|
|
350
325
|
player.addEventListener('durationchange', onDurationChange);
|
|
@@ -435,13 +410,11 @@ const useDailymotionPlayer = function () {
|
|
|
435
410
|
};
|
|
436
411
|
|
|
437
412
|
const eventsManager$1 = typeof document !== 'undefined' ? new EventsManager__default["default"](document) : null;
|
|
438
|
-
|
|
439
413
|
const useDocumentEvent = (event, callback) => {
|
|
440
414
|
react.useEffect(() => {
|
|
441
415
|
if (eventsManager$1 !== null && callback !== null) {
|
|
442
416
|
eventsManager$1.subscribe(event, callback);
|
|
443
417
|
}
|
|
444
|
-
|
|
445
418
|
return () => {
|
|
446
419
|
if (eventsManager$1 !== null && callback !== null) {
|
|
447
420
|
eventsManager$1.unsubscribe(event, callback);
|
|
@@ -451,13 +424,11 @@ const useDocumentEvent = (event, callback) => {
|
|
|
451
424
|
};
|
|
452
425
|
|
|
453
426
|
const eventsManager = typeof window !== 'undefined' ? new EventsManager__default["default"](window) : null;
|
|
454
|
-
|
|
455
427
|
const useWindowEvent = (event, callback) => {
|
|
456
428
|
react.useEffect(() => {
|
|
457
429
|
if (eventsManager !== null && callback !== null) {
|
|
458
430
|
eventsManager.subscribe(event, callback);
|
|
459
431
|
}
|
|
460
|
-
|
|
461
432
|
return () => {
|
|
462
433
|
if (eventsManager !== null && callback !== null) {
|
|
463
434
|
eventsManager.unsubscribe(event, callback);
|
|
@@ -473,13 +444,11 @@ function useKeyboard() {
|
|
|
473
444
|
key
|
|
474
445
|
} = event;
|
|
475
446
|
let callback = null;
|
|
476
|
-
|
|
477
447
|
if (typeof keyMap === 'function') {
|
|
478
448
|
callback = keyMap;
|
|
479
449
|
} else if (typeof keyMap[key] !== 'undefined') {
|
|
480
450
|
callback = typeof keyMap[key] === 'function' ? keyMap[key] : (keyMap[key] || {}).down || null;
|
|
481
451
|
}
|
|
482
|
-
|
|
483
452
|
if (callback !== null) {
|
|
484
453
|
callback(event);
|
|
485
454
|
}
|
|
@@ -489,13 +458,11 @@ function useKeyboard() {
|
|
|
489
458
|
key
|
|
490
459
|
} = event;
|
|
491
460
|
let callback = null;
|
|
492
|
-
|
|
493
461
|
if (typeof keyMap === 'function') {
|
|
494
462
|
callback = keyMap;
|
|
495
463
|
} else if (typeof keyMap[key] !== 'undefined') {
|
|
496
464
|
callback = typeof keyMap[key] === 'function' ? keyMap[key] : (keyMap[key] || {}).up || null;
|
|
497
465
|
}
|
|
498
|
-
|
|
499
466
|
if (callback !== null) {
|
|
500
467
|
callback(event);
|
|
501
468
|
}
|
|
@@ -561,15 +528,13 @@ const useItemsPaginated = function (loader) {
|
|
|
561
528
|
} = _ref4;
|
|
562
529
|
return pagesItems.concat(pageItems);
|
|
563
530
|
}, []) : null;
|
|
564
|
-
|
|
565
|
-
|
|
531
|
+
const updateState = update => setState({
|
|
532
|
+
...state,
|
|
566
533
|
...update
|
|
567
534
|
});
|
|
568
|
-
|
|
569
535
|
const updateFromResponse = function (response) {
|
|
570
536
|
let error = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
571
537
|
let reset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
572
|
-
|
|
573
538
|
if (error !== null) {
|
|
574
539
|
updateState({
|
|
575
540
|
loaded: false,
|
|
@@ -577,7 +542,6 @@ const useItemsPaginated = function (loader) {
|
|
|
577
542
|
});
|
|
578
543
|
throw error;
|
|
579
544
|
}
|
|
580
|
-
|
|
581
545
|
const newPage = getPageFromResponse(response);
|
|
582
546
|
const currentPages = currentPagesRef.current || [];
|
|
583
547
|
const newPages = (reset ? [newPage] : [...currentPages.filter(it => it.page !== newPage.page), newPage]).sort((a, b) => {
|
|
@@ -588,23 +552,18 @@ const useItemsPaginated = function (loader) {
|
|
|
588
552
|
page: bPage = null
|
|
589
553
|
} = b;
|
|
590
554
|
const hasObject = aPage !== null && bPage !== null;
|
|
591
|
-
|
|
592
555
|
if (hasObject) {
|
|
593
556
|
if (aPage === bPage) {
|
|
594
557
|
return 0;
|
|
595
558
|
}
|
|
596
|
-
|
|
597
559
|
return aPage > bPage ? 1 : -1;
|
|
598
560
|
}
|
|
599
|
-
|
|
600
561
|
if (isNumber__default["default"](a) && isNumber__default["default"](b)) {
|
|
601
562
|
if (a === b) {
|
|
602
563
|
return 0;
|
|
603
564
|
}
|
|
604
|
-
|
|
605
565
|
return a > b ? 1 : -1;
|
|
606
566
|
}
|
|
607
|
-
|
|
608
567
|
return 0;
|
|
609
568
|
});
|
|
610
569
|
currentPagesRef.current = newPages;
|
|
@@ -617,7 +576,6 @@ const useItemsPaginated = function (loader) {
|
|
|
617
576
|
});
|
|
618
577
|
return newPage;
|
|
619
578
|
};
|
|
620
|
-
|
|
621
579
|
const getNextPage = () => {
|
|
622
580
|
const allPages = lastPage !== -1 ? Array.call(null, ...Array(lastPage)).map((it, index) => index + 1) : [];
|
|
623
581
|
const currentPages = currentPagesRef.current || [];
|
|
@@ -625,7 +583,6 @@ const useItemsPaginated = function (loader) {
|
|
|
625
583
|
const firstItem = remainingPages.length > 0 ? remainingPages.shift() : null;
|
|
626
584
|
return firstItem !== null ? firstItem : null;
|
|
627
585
|
};
|
|
628
|
-
|
|
629
586
|
const loadItems = requestPage => {
|
|
630
587
|
updateState({
|
|
631
588
|
loading: true
|
|
@@ -634,7 +591,6 @@ const useItemsPaginated = function (loader) {
|
|
|
634
591
|
if (onLoaded !== null) {
|
|
635
592
|
onLoaded(response);
|
|
636
593
|
}
|
|
637
|
-
|
|
638
594
|
return response;
|
|
639
595
|
}).catch(error => {
|
|
640
596
|
if (onError !== null) {
|
|
@@ -642,35 +598,28 @@ const useItemsPaginated = function (loader) {
|
|
|
642
598
|
}
|
|
643
599
|
});
|
|
644
600
|
};
|
|
645
|
-
|
|
646
601
|
const loadPage = pageToLoad => {
|
|
647
602
|
if (loading) {
|
|
648
603
|
return Promise.reject();
|
|
649
604
|
}
|
|
650
|
-
|
|
651
605
|
const currentPages = currentPagesRef.current || [];
|
|
652
606
|
const foundPage = currentPages.find(it => it.page === pageToLoad) || null;
|
|
653
|
-
|
|
654
607
|
if (foundPage !== null) {
|
|
655
608
|
return Promise.resolve(foundPage);
|
|
656
609
|
}
|
|
657
|
-
|
|
658
610
|
return loadItems(pageToLoad);
|
|
659
611
|
};
|
|
660
|
-
|
|
661
612
|
const loadNextPage = () => {
|
|
662
613
|
if (loading) {
|
|
663
614
|
return Promise.reject();
|
|
664
615
|
}
|
|
665
|
-
|
|
666
616
|
const nextPage = getNextPage();
|
|
667
617
|
return nextPage !== null ? loadItems(nextPage) : Promise.resolve();
|
|
668
|
-
};
|
|
669
|
-
|
|
618
|
+
};
|
|
670
619
|
|
|
620
|
+
// Reset all on query change
|
|
671
621
|
react.useEffect(() => {
|
|
672
622
|
const hadState = lastState.current !== null;
|
|
673
|
-
|
|
674
623
|
if (hadState) {
|
|
675
624
|
currentPagesRef.current = null;
|
|
676
625
|
updateState({
|
|
@@ -685,7 +634,6 @@ const useItemsPaginated = function (loader) {
|
|
|
685
634
|
react.useEffect(() => {
|
|
686
635
|
const hadState = lastState.current !== null;
|
|
687
636
|
lastState.current = initialState;
|
|
688
|
-
|
|
689
637
|
if (hadState) {
|
|
690
638
|
currentPagesRef.current = initialState.pages;
|
|
691
639
|
setState(initialState);
|
|
@@ -695,14 +643,11 @@ const useItemsPaginated = function (loader) {
|
|
|
695
643
|
if (loader === null) {
|
|
696
644
|
return () => {};
|
|
697
645
|
}
|
|
698
|
-
|
|
699
646
|
let loadPromise = null;
|
|
700
647
|
const pageToLoad = initialPages === null && page === null ? 1 : page;
|
|
701
|
-
|
|
702
648
|
if (pageToLoad !== null) {
|
|
703
649
|
loadPromise = loadItems(pageToLoad);
|
|
704
650
|
}
|
|
705
|
-
|
|
706
651
|
return () => {
|
|
707
652
|
if (loadPromise !== null) {
|
|
708
653
|
loadPromise.cancel();
|
|
@@ -732,7 +677,6 @@ const useItemsPaginated = function (loader) {
|
|
|
732
677
|
|
|
733
678
|
const debug$2 = createDebug__default["default"]('folklore:video:native');
|
|
734
679
|
const noPlayerError = new Error('No player');
|
|
735
|
-
|
|
736
680
|
const useNativeVideoPlayer = function (url) {
|
|
737
681
|
let {
|
|
738
682
|
width = 0,
|
|
@@ -772,77 +716,65 @@ const useNativeVideoPlayer = function (url) {
|
|
|
772
716
|
const {
|
|
773
717
|
current: player
|
|
774
718
|
} = elementRef;
|
|
775
|
-
|
|
776
719
|
if (player !== null) {
|
|
777
720
|
player.volume = newVolume;
|
|
778
721
|
return Promise.resolve(newVolume);
|
|
779
722
|
}
|
|
780
|
-
|
|
781
723
|
return Promise.reject(noPlayerError);
|
|
782
724
|
}, []);
|
|
783
725
|
const mute = react.useCallback(() => {
|
|
784
726
|
const {
|
|
785
727
|
current: player
|
|
786
728
|
} = elementRef;
|
|
787
|
-
|
|
788
729
|
if (player !== null) {
|
|
789
730
|
player.muted = true;
|
|
790
731
|
return Promise.resolve(true);
|
|
791
732
|
}
|
|
792
|
-
|
|
793
733
|
return Promise.reject(noPlayerError);
|
|
794
734
|
}, []);
|
|
795
735
|
const unmute = react.useCallback(() => {
|
|
796
736
|
const {
|
|
797
737
|
current: player
|
|
798
738
|
} = elementRef;
|
|
799
|
-
|
|
800
739
|
if (player !== null) {
|
|
801
740
|
player.muted = false;
|
|
802
741
|
return Promise.resolve(false);
|
|
803
742
|
}
|
|
804
|
-
|
|
805
743
|
return Promise.reject(noPlayerError);
|
|
806
744
|
}, []);
|
|
807
745
|
const seek = react.useCallback(newTime => {
|
|
808
746
|
const {
|
|
809
747
|
current: player
|
|
810
748
|
} = elementRef;
|
|
811
|
-
|
|
812
749
|
if (player !== null) {
|
|
813
750
|
player.currentTime = newTime;
|
|
814
751
|
return Promise.resolve(newTime);
|
|
815
752
|
}
|
|
816
|
-
|
|
817
753
|
return Promise.reject(noPlayerError);
|
|
818
754
|
}, []);
|
|
819
755
|
const setLoop = react.useCallback(newLoop => {
|
|
820
756
|
const {
|
|
821
757
|
current: player
|
|
822
758
|
} = elementRef;
|
|
823
|
-
|
|
824
759
|
if (player !== null) {
|
|
825
760
|
player.loop = newLoop;
|
|
826
761
|
return Promise.resolve(newLoop);
|
|
827
762
|
}
|
|
828
|
-
|
|
829
763
|
return Promise.reject(noPlayerError);
|
|
830
|
-
}, []);
|
|
764
|
+
}, []);
|
|
831
765
|
|
|
766
|
+
// Bind player events
|
|
832
767
|
react.useEffect(() => {
|
|
833
768
|
const {
|
|
834
769
|
current: player
|
|
835
770
|
} = elementRef;
|
|
836
|
-
|
|
837
771
|
if (player === null) {
|
|
838
772
|
return () => {};
|
|
839
773
|
}
|
|
840
|
-
|
|
841
774
|
const onCanPlay = () => {
|
|
842
775
|
setLoaded(true);
|
|
843
776
|
debug$2('onCanPlay [URL: %s]', url);
|
|
844
777
|
};
|
|
845
|
-
|
|
846
778
|
const onMetadataLoaded = () => {
|
|
847
779
|
const newMetadata = {
|
|
848
780
|
duration: player.duration,
|
|
@@ -852,7 +784,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
852
784
|
setMetadata(newMetadata);
|
|
853
785
|
debug$2('onMetadataLoaded [URL: %s]', url);
|
|
854
786
|
};
|
|
855
|
-
|
|
856
787
|
const onPlay = () => {
|
|
857
788
|
setPlayState({
|
|
858
789
|
playing: true,
|
|
@@ -862,7 +793,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
862
793
|
});
|
|
863
794
|
debug$2('onPlay [URL: %s]', url);
|
|
864
795
|
};
|
|
865
|
-
|
|
866
796
|
const onPause = () => {
|
|
867
797
|
setPlayState({
|
|
868
798
|
playing: false,
|
|
@@ -872,7 +802,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
872
802
|
});
|
|
873
803
|
debug$2('onPause [URL: %s]', url);
|
|
874
804
|
};
|
|
875
|
-
|
|
876
805
|
const onEnded = () => {
|
|
877
806
|
setPlayState({
|
|
878
807
|
playing: false,
|
|
@@ -882,7 +811,6 @@ const useNativeVideoPlayer = function (url) {
|
|
|
882
811
|
});
|
|
883
812
|
debug$2('onEnded [URL: %s]', url);
|
|
884
813
|
};
|
|
885
|
-
|
|
886
814
|
debug$2('Bind events [URL: %s]', url);
|
|
887
815
|
player.addEventListener('canplay', onCanPlay);
|
|
888
816
|
player.addEventListener('metadataloaded', onMetadataLoaded);
|
|
@@ -928,39 +856,34 @@ const useNativeVideoPlayer = function (url) {
|
|
|
928
856
|
};
|
|
929
857
|
|
|
930
858
|
const observersCache = new Map();
|
|
931
|
-
|
|
932
859
|
const getOptionsKey = _ref => {
|
|
933
860
|
let {
|
|
934
861
|
root = null,
|
|
935
862
|
rootMargin,
|
|
936
863
|
threshold = null
|
|
937
864
|
} = _ref;
|
|
938
|
-
return
|
|
865
|
+
return `root_${root}_rootMargin_${rootMargin || null}_threshold_${threshold}`;
|
|
939
866
|
};
|
|
940
|
-
|
|
941
867
|
const createObserver = function (Observer) {
|
|
942
868
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
943
869
|
let subscribers = [];
|
|
944
|
-
|
|
945
870
|
const addSubscriber = (element, callback) => {
|
|
946
871
|
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
947
|
-
|
|
948
872
|
if (currentSubscriber !== null) {
|
|
949
|
-
return subscribers.map(it => it.element === element && it.callbacks.indexOf(callback) === -1 ? {
|
|
873
|
+
return subscribers.map(it => it.element === element && it.callbacks.indexOf(callback) === -1 ? {
|
|
874
|
+
...it,
|
|
950
875
|
callbacks: [...it.callbacks, callback]
|
|
951
876
|
} : it).filter(it => it.callbacks.length > 0);
|
|
952
877
|
}
|
|
953
|
-
|
|
954
878
|
return [...subscribers, {
|
|
955
879
|
element,
|
|
956
880
|
callbacks: [callback]
|
|
957
881
|
}];
|
|
958
882
|
};
|
|
959
|
-
|
|
960
|
-
|
|
883
|
+
const removeSubscriber = (element, callback) => subscribers.map(it => it.element === element ? {
|
|
884
|
+
...it,
|
|
961
885
|
callbacks: it.callbacks.filter(subCallback => subCallback !== callback)
|
|
962
886
|
} : it).filter(it => it.callbacks.length > 0);
|
|
963
|
-
|
|
964
887
|
const onUpdate = entries => {
|
|
965
888
|
entries.forEach(entry => {
|
|
966
889
|
subscribers.forEach(_ref2 => {
|
|
@@ -968,7 +891,6 @@ const createObserver = function (Observer) {
|
|
|
968
891
|
element,
|
|
969
892
|
callbacks
|
|
970
893
|
} = _ref2;
|
|
971
|
-
|
|
972
894
|
if (element === entry.target) {
|
|
973
895
|
callbacks.forEach(callback => {
|
|
974
896
|
callback(entry);
|
|
@@ -977,13 +899,10 @@ const createObserver = function (Observer) {
|
|
|
977
899
|
});
|
|
978
900
|
});
|
|
979
901
|
};
|
|
980
|
-
|
|
981
902
|
const observer = new Observer(onUpdate, options);
|
|
982
|
-
|
|
983
903
|
const unsubscribe = function (element) {
|
|
984
904
|
let callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
985
905
|
subscribers = removeSubscriber(element, callback);
|
|
986
|
-
|
|
987
906
|
if (typeof observer.unobserve === 'undefined') {
|
|
988
907
|
observer.disconnect();
|
|
989
908
|
subscribers.forEach(subscriber => {
|
|
@@ -991,45 +910,43 @@ const createObserver = function (Observer) {
|
|
|
991
910
|
});
|
|
992
911
|
return;
|
|
993
912
|
}
|
|
994
|
-
|
|
995
913
|
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
996
|
-
|
|
997
914
|
if (currentSubscriber === null) {
|
|
998
915
|
observer.unobserve(element);
|
|
999
916
|
}
|
|
1000
917
|
};
|
|
1001
|
-
|
|
1002
918
|
const subscribe = (element, callback) => {
|
|
1003
919
|
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
1004
920
|
subscribers = addSubscriber(element, callback);
|
|
1005
|
-
|
|
1006
921
|
if (currentSubscriber === null) {
|
|
1007
922
|
observer.observe(element);
|
|
1008
923
|
}
|
|
1009
924
|
};
|
|
1010
|
-
|
|
1011
925
|
return {
|
|
1012
926
|
subscribe,
|
|
1013
927
|
unsubscribe,
|
|
1014
928
|
observer
|
|
1015
929
|
};
|
|
1016
930
|
};
|
|
1017
|
-
|
|
1018
|
-
|
|
931
|
+
const getObserver = function () {
|
|
932
|
+
let Observer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
1019
933
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
934
|
+
if (Observer === null) {
|
|
935
|
+
return {
|
|
936
|
+
observer: null,
|
|
937
|
+
subscribe: () => {},
|
|
938
|
+
unsubscribe: () => {}
|
|
939
|
+
};
|
|
940
|
+
}
|
|
1020
941
|
const observerKey = getOptionsKey(options);
|
|
1021
|
-
|
|
1022
942
|
if (!observersCache.has(Observer)) {
|
|
1023
943
|
observersCache.set(Observer, {});
|
|
1024
944
|
}
|
|
1025
|
-
|
|
1026
945
|
const observers = observersCache.get(Observer);
|
|
1027
|
-
|
|
1028
946
|
if (typeof observers[observerKey] === 'undefined') {
|
|
1029
947
|
observers[observerKey] = createObserver(Observer, options);
|
|
1030
948
|
observersCache.set(Observer, observers);
|
|
1031
949
|
}
|
|
1032
|
-
|
|
1033
950
|
return observers[observerKey];
|
|
1034
951
|
};
|
|
1035
952
|
const useObserver = function (Observer) {
|
|
@@ -1050,26 +967,19 @@ const useObserver = function (Observer) {
|
|
|
1050
967
|
const {
|
|
1051
968
|
current: nodeElement
|
|
1052
969
|
} = nodeRef;
|
|
1053
|
-
|
|
1054
970
|
const callback = newEntry => setEntry(newEntry);
|
|
1055
|
-
|
|
1056
971
|
let unsubscribe = null;
|
|
1057
|
-
|
|
1058
972
|
if (nodeElement !== null) {
|
|
1059
973
|
const newOpts = {};
|
|
1060
|
-
|
|
1061
974
|
if (root !== null) {
|
|
1062
975
|
newOpts.root = root;
|
|
1063
976
|
}
|
|
1064
|
-
|
|
1065
977
|
if (rootMargin !== null) {
|
|
1066
978
|
newOpts.rootMargin = rootMargin;
|
|
1067
979
|
}
|
|
1068
|
-
|
|
1069
980
|
if (threshold !== null) {
|
|
1070
981
|
newOpts.threshold = threshold;
|
|
1071
982
|
}
|
|
1072
|
-
|
|
1073
983
|
const {
|
|
1074
984
|
subscribe,
|
|
1075
985
|
unsubscribe: localUnsubscribe
|
|
@@ -1077,7 +987,6 @@ const useObserver = function (Observer) {
|
|
|
1077
987
|
unsubscribe = localUnsubscribe;
|
|
1078
988
|
subscribe(nodeElement, callback);
|
|
1079
989
|
}
|
|
1080
|
-
|
|
1081
990
|
currentElement.current = nodeElement;
|
|
1082
991
|
return () => {
|
|
1083
992
|
if (unsubscribe !== null) {
|
|
@@ -1090,10 +999,10 @@ const useObserver = function (Observer) {
|
|
|
1090
999
|
entry
|
|
1091
1000
|
};
|
|
1092
1001
|
};
|
|
1002
|
+
|
|
1093
1003
|
/**
|
|
1094
1004
|
* Intersection Observer
|
|
1095
1005
|
*/
|
|
1096
|
-
|
|
1097
1006
|
const defaultThreshold = [0, 1.0];
|
|
1098
1007
|
const intersectionObserverInitialEntry = {
|
|
1099
1008
|
target: null,
|
|
@@ -1112,17 +1021,17 @@ const useIntersectionObserver = function () {
|
|
|
1112
1021
|
threshold = defaultThreshold,
|
|
1113
1022
|
disabled = false
|
|
1114
1023
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1115
|
-
return useObserver(IntersectionObserver, {
|
|
1024
|
+
return useObserver(typeof IntersectionObserver !== 'undefined' ? IntersectionObserver : null, {
|
|
1116
1025
|
root,
|
|
1117
1026
|
rootMargin,
|
|
1118
1027
|
threshold,
|
|
1119
1028
|
disabled
|
|
1120
1029
|
}, intersectionObserverInitialEntry);
|
|
1121
1030
|
};
|
|
1031
|
+
|
|
1122
1032
|
/**
|
|
1123
1033
|
* Resize Observer
|
|
1124
1034
|
*/
|
|
1125
|
-
|
|
1126
1035
|
const resizeObserverInitialEntry = {
|
|
1127
1036
|
target: null,
|
|
1128
1037
|
contentRect: null,
|
|
@@ -1133,14 +1042,13 @@ const useResizeObserver = function () {
|
|
|
1133
1042
|
let {
|
|
1134
1043
|
disabled = false
|
|
1135
1044
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1136
|
-
return useObserver(ResizeObserver, {
|
|
1045
|
+
return useObserver(typeof ResizeObserver !== 'undefined' ? ResizeObserver : null, {
|
|
1137
1046
|
disabled
|
|
1138
1047
|
}, resizeObserverInitialEntry);
|
|
1139
1048
|
};
|
|
1140
1049
|
|
|
1141
1050
|
const NO_PLAYER_ERROR$1 = new Error('No player');
|
|
1142
1051
|
const debug$1 = createDebug__default["default"]('folklore:video:youtube');
|
|
1143
|
-
|
|
1144
1052
|
function useYouTubePlayer(id) {
|
|
1145
1053
|
let {
|
|
1146
1054
|
width = 0,
|
|
@@ -1156,14 +1064,13 @@ function useYouTubePlayer(id) {
|
|
|
1156
1064
|
if (url === null || url.match(/^https?:/) === null) {
|
|
1157
1065
|
return url;
|
|
1158
1066
|
}
|
|
1159
|
-
|
|
1160
1067
|
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
|
|
1161
1068
|
const match = url.match(regExp);
|
|
1162
1069
|
return match && match[7].length === 11 ? match[7] : null;
|
|
1163
1070
|
}
|
|
1164
1071
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1165
|
-
const [apiLoaded, setApiLoaded] = react.useState(typeof window.YT !== 'undefined');
|
|
1166
|
-
const apiRef = react.useRef(typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1072
|
+
const [apiLoaded, setApiLoaded] = react.useState(typeof window !== 'undefined' && typeof window.YT !== 'undefined');
|
|
1073
|
+
const apiRef = react.useRef(typeof window !== 'undefined' && typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1167
1074
|
const elementRef = react.useRef(null);
|
|
1168
1075
|
const playerRef = react.useRef(null);
|
|
1169
1076
|
const playerElementRef = react.useRef(elementRef.current);
|
|
@@ -1184,7 +1091,6 @@ function useYouTubePlayer(id) {
|
|
|
1184
1091
|
});
|
|
1185
1092
|
react.useEffect(() => {
|
|
1186
1093
|
let canceled = false;
|
|
1187
|
-
|
|
1188
1094
|
if (!apiLoaded && videoId !== null) {
|
|
1189
1095
|
debug$1('Load API');
|
|
1190
1096
|
services.loadYouTube().then(api => {
|
|
@@ -1195,7 +1101,6 @@ function useYouTubePlayer(id) {
|
|
|
1195
1101
|
}
|
|
1196
1102
|
});
|
|
1197
1103
|
}
|
|
1198
|
-
|
|
1199
1104
|
return () => {
|
|
1200
1105
|
canceled = true;
|
|
1201
1106
|
};
|
|
@@ -1217,11 +1122,9 @@ function useYouTubePlayer(id) {
|
|
|
1217
1122
|
current: player
|
|
1218
1123
|
} = playerRef;
|
|
1219
1124
|
const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1220
|
-
|
|
1221
1125
|
if (customOnVolumeChange) {
|
|
1222
1126
|
customOnVolumeChange(volume);
|
|
1223
1127
|
}
|
|
1224
|
-
|
|
1225
1128
|
return promise;
|
|
1226
1129
|
}, [customOnVolumeChange]);
|
|
1227
1130
|
const mute = react.useCallback(() => {
|
|
@@ -1253,19 +1156,21 @@ function useYouTubePlayer(id) {
|
|
|
1253
1156
|
debug$1('Unset player');
|
|
1254
1157
|
playerRef.current = null;
|
|
1255
1158
|
}
|
|
1256
|
-
}, []);
|
|
1159
|
+
}, []);
|
|
1160
|
+
|
|
1161
|
+
// Detect iframe switch and destroy player
|
|
1257
1162
|
|
|
1258
1163
|
react.useEffect(() => {
|
|
1259
1164
|
const {
|
|
1260
1165
|
current: currentPlayer
|
|
1261
1166
|
} = playerRef;
|
|
1262
|
-
|
|
1263
1167
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1264
1168
|
debug$1('iFrame switched');
|
|
1265
1169
|
destroyPlayer();
|
|
1266
1170
|
}
|
|
1267
|
-
}, [elementHasChanged]);
|
|
1171
|
+
}, [elementHasChanged]);
|
|
1268
1172
|
|
|
1173
|
+
// Create player
|
|
1269
1174
|
react.useEffect(() => {
|
|
1270
1175
|
const {
|
|
1271
1176
|
current: YT = null
|
|
@@ -1276,20 +1181,16 @@ function useYouTubePlayer(id) {
|
|
|
1276
1181
|
const {
|
|
1277
1182
|
current: element = null
|
|
1278
1183
|
} = elementRef;
|
|
1279
|
-
|
|
1280
1184
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1281
1185
|
destroyPlayer();
|
|
1282
1186
|
return;
|
|
1283
1187
|
}
|
|
1284
|
-
|
|
1285
1188
|
let player = currentPlayer;
|
|
1286
|
-
|
|
1287
1189
|
if (player !== null && typeof player.loadVideoById !== 'undefined') {
|
|
1288
1190
|
debug$1('Switch video [ID: %s]', videoId);
|
|
1289
1191
|
player.loadVideoById(videoId);
|
|
1290
1192
|
} else {
|
|
1291
1193
|
debug$1('Create player [ID: %s]', videoId);
|
|
1292
|
-
|
|
1293
1194
|
const onReady = _ref => {
|
|
1294
1195
|
let {
|
|
1295
1196
|
target
|
|
@@ -1298,16 +1199,14 @@ function useYouTubePlayer(id) {
|
|
|
1298
1199
|
playerRef.current = target;
|
|
1299
1200
|
setReady(true);
|
|
1300
1201
|
const newDuration = target.getDuration();
|
|
1301
|
-
|
|
1302
1202
|
if (newDuration !== metadata.duration) {
|
|
1303
|
-
setMetadata({
|
|
1203
|
+
setMetadata({
|
|
1204
|
+
...metadata,
|
|
1304
1205
|
duration: newDuration
|
|
1305
1206
|
});
|
|
1306
1207
|
}
|
|
1307
|
-
|
|
1308
1208
|
debug$1('onReady [ID: %s]', videoId);
|
|
1309
1209
|
};
|
|
1310
|
-
|
|
1311
1210
|
const onStateChange = _ref2 => {
|
|
1312
1211
|
let {
|
|
1313
1212
|
data: state
|
|
@@ -1320,7 +1219,6 @@ function useYouTubePlayer(id) {
|
|
|
1320
1219
|
};
|
|
1321
1220
|
setPlayState(newState);
|
|
1322
1221
|
let stateLabel = null;
|
|
1323
|
-
|
|
1324
1222
|
if (newState.playing) {
|
|
1325
1223
|
stateLabel = 'playing';
|
|
1326
1224
|
} else if (newState.paused) {
|
|
@@ -1334,10 +1232,8 @@ function useYouTubePlayer(id) {
|
|
|
1334
1232
|
} else if (state === 0) {
|
|
1335
1233
|
stateLabel = 'stopped';
|
|
1336
1234
|
}
|
|
1337
|
-
|
|
1338
1235
|
debug$1('onStateChange %s [ID: %s]', stateLabel, videoId);
|
|
1339
1236
|
};
|
|
1340
|
-
|
|
1341
1237
|
player = new YT.Player(element, {
|
|
1342
1238
|
videoId,
|
|
1343
1239
|
playerVars: {
|
|
@@ -1355,7 +1251,6 @@ function useYouTubePlayer(id) {
|
|
|
1355
1251
|
}
|
|
1356
1252
|
});
|
|
1357
1253
|
}
|
|
1358
|
-
|
|
1359
1254
|
playerRef.current = player;
|
|
1360
1255
|
playerElementRef.current = element;
|
|
1361
1256
|
}, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
|
|
@@ -1391,7 +1286,6 @@ function useYouTubePlayer(id) {
|
|
|
1391
1286
|
|
|
1392
1287
|
const debug = createDebug__default["default"]('folklore:video:vimeo');
|
|
1393
1288
|
const NO_PLAYER_ERROR = new Error('No player');
|
|
1394
|
-
|
|
1395
1289
|
const useVimeoPlayer = function (id) {
|
|
1396
1290
|
let {
|
|
1397
1291
|
width = 0,
|
|
@@ -1408,7 +1302,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1408
1302
|
if (url === null || url.match(/^[0-9]+$/) !== null) {
|
|
1409
1303
|
return url;
|
|
1410
1304
|
}
|
|
1411
|
-
|
|
1412
1305
|
const match = url.match(/\/[0-9]+/);
|
|
1413
1306
|
return match !== null ? match[1] : null;
|
|
1414
1307
|
}
|
|
@@ -1433,11 +1326,11 @@ const useVimeoPlayer = function (id) {
|
|
|
1433
1326
|
width,
|
|
1434
1327
|
height,
|
|
1435
1328
|
duration
|
|
1436
|
-
});
|
|
1329
|
+
});
|
|
1437
1330
|
|
|
1331
|
+
// Load SDK
|
|
1438
1332
|
react.useEffect(() => {
|
|
1439
1333
|
let canceled = false;
|
|
1440
|
-
|
|
1441
1334
|
if (!apiLoaded && id !== null) {
|
|
1442
1335
|
debug('Load API');
|
|
1443
1336
|
services.loadVimeo().then(api => {
|
|
@@ -1448,7 +1341,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1448
1341
|
}
|
|
1449
1342
|
});
|
|
1450
1343
|
}
|
|
1451
|
-
|
|
1452
1344
|
return () => {
|
|
1453
1345
|
canceled = true;
|
|
1454
1346
|
};
|
|
@@ -1499,12 +1391,10 @@ const useVimeoPlayer = function (id) {
|
|
|
1499
1391
|
const {
|
|
1500
1392
|
current: player
|
|
1501
1393
|
} = playerRef;
|
|
1502
|
-
|
|
1503
1394
|
if (player !== null) {
|
|
1504
1395
|
debug('Unload video');
|
|
1505
1396
|
player.unload();
|
|
1506
1397
|
}
|
|
1507
|
-
|
|
1508
1398
|
if (player !== null) {
|
|
1509
1399
|
debug('Unset video');
|
|
1510
1400
|
playerRef.current = null;
|
|
@@ -1514,13 +1404,13 @@ const useVimeoPlayer = function (id) {
|
|
|
1514
1404
|
const {
|
|
1515
1405
|
current: currentPlayer
|
|
1516
1406
|
} = playerRef;
|
|
1517
|
-
|
|
1518
1407
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1519
1408
|
debug('iFrame switched');
|
|
1520
1409
|
destroyVideo();
|
|
1521
1410
|
}
|
|
1522
|
-
}, [elementHasChanged]);
|
|
1411
|
+
}, [elementHasChanged]);
|
|
1523
1412
|
|
|
1413
|
+
// Create player
|
|
1524
1414
|
react.useEffect(() => {
|
|
1525
1415
|
const {
|
|
1526
1416
|
current: Player = null
|
|
@@ -1531,14 +1421,11 @@ const useVimeoPlayer = function (id) {
|
|
|
1531
1421
|
const {
|
|
1532
1422
|
current: element = null
|
|
1533
1423
|
} = elementRef;
|
|
1534
|
-
|
|
1535
1424
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1536
1425
|
destroyVideo();
|
|
1537
1426
|
return;
|
|
1538
1427
|
}
|
|
1539
|
-
|
|
1540
1428
|
let player = currentPlayer;
|
|
1541
|
-
|
|
1542
1429
|
if (player === null) {
|
|
1543
1430
|
debug('Create player [ID: %s]', videoId);
|
|
1544
1431
|
player = new Player(element, {
|
|
@@ -1559,46 +1446,38 @@ const useVimeoPlayer = function (id) {
|
|
|
1559
1446
|
debug('ERROR: %o', e);
|
|
1560
1447
|
});
|
|
1561
1448
|
}
|
|
1562
|
-
|
|
1563
1449
|
playerRef.current = player;
|
|
1564
1450
|
playerElementRef.current = element;
|
|
1565
|
-
}, [apiLoaded, videoId, elementHasChanged, setReady, destroyVideo, setLoaded]);
|
|
1451
|
+
}, [apiLoaded, videoId, elementHasChanged, setReady, destroyVideo, setLoaded]);
|
|
1566
1452
|
|
|
1453
|
+
// Bind player events
|
|
1567
1454
|
react.useEffect(() => {
|
|
1568
1455
|
const {
|
|
1569
1456
|
current: player
|
|
1570
1457
|
} = playerRef;
|
|
1571
|
-
|
|
1572
1458
|
if (player === null) {
|
|
1573
1459
|
return () => {};
|
|
1574
1460
|
}
|
|
1575
|
-
|
|
1576
1461
|
let canceled = false;
|
|
1577
|
-
|
|
1578
1462
|
const onLoaded = () => {
|
|
1579
1463
|
Promise.all([player.getDuration(), player.getVideoWidth(), player.getVideoHeight(), player.getMuted()]).then(_ref => {
|
|
1580
1464
|
let [newDuration, newWidth, newHeight, newMuted] = _ref;
|
|
1581
|
-
|
|
1582
1465
|
if (canceled) {
|
|
1583
1466
|
return;
|
|
1584
1467
|
}
|
|
1585
|
-
|
|
1586
1468
|
const newMetadata = {
|
|
1587
1469
|
duration: newDuration,
|
|
1588
1470
|
width: newWidth,
|
|
1589
1471
|
height: newHeight
|
|
1590
1472
|
};
|
|
1591
1473
|
setMetadata(newMetadata);
|
|
1592
|
-
|
|
1593
1474
|
if (newMuted) {
|
|
1594
1475
|
setVolumeState(0);
|
|
1595
1476
|
}
|
|
1596
|
-
|
|
1597
1477
|
setLoaded(true);
|
|
1598
1478
|
});
|
|
1599
1479
|
debug('onLoaded [ID: %s]', videoId);
|
|
1600
1480
|
};
|
|
1601
|
-
|
|
1602
1481
|
const onPlay = () => {
|
|
1603
1482
|
setPlayState({
|
|
1604
1483
|
playing: true,
|
|
@@ -1613,7 +1492,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1613
1492
|
}
|
|
1614
1493
|
}).catch(() => {});
|
|
1615
1494
|
};
|
|
1616
|
-
|
|
1617
1495
|
const onPause = () => {
|
|
1618
1496
|
setPlayState({
|
|
1619
1497
|
playing: false,
|
|
@@ -1623,7 +1501,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1623
1501
|
});
|
|
1624
1502
|
debug('onPause [ID: %s]', videoId);
|
|
1625
1503
|
};
|
|
1626
|
-
|
|
1627
1504
|
const onVolumeChange = _ref2 => {
|
|
1628
1505
|
let {
|
|
1629
1506
|
volume: newVolume
|
|
@@ -1631,7 +1508,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1631
1508
|
setVolumeState(newVolume);
|
|
1632
1509
|
debug('onVolumeChange [ID: %s]', videoId);
|
|
1633
1510
|
};
|
|
1634
|
-
|
|
1635
1511
|
const onEnded = () => {
|
|
1636
1512
|
setPlayState({
|
|
1637
1513
|
playing: false,
|
|
@@ -1641,7 +1517,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1641
1517
|
});
|
|
1642
1518
|
debug('onEnded [ID: %s]', videoId);
|
|
1643
1519
|
};
|
|
1644
|
-
|
|
1645
1520
|
const onBufferStart = () => {
|
|
1646
1521
|
setPlayState({
|
|
1647
1522
|
playing: true,
|
|
@@ -1651,7 +1526,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1651
1526
|
});
|
|
1652
1527
|
debug('onBufferStart [ID: %s]', videoId);
|
|
1653
1528
|
};
|
|
1654
|
-
|
|
1655
1529
|
const onBufferEnded = () => {
|
|
1656
1530
|
setPlayState({
|
|
1657
1531
|
playing: true,
|
|
@@ -1661,7 +1535,6 @@ const useVimeoPlayer = function (id) {
|
|
|
1661
1535
|
});
|
|
1662
1536
|
debug('onBufferStart [ID: %s]', videoId);
|
|
1663
1537
|
};
|
|
1664
|
-
|
|
1665
1538
|
debug('Bind events [ID: %s]', videoId);
|
|
1666
1539
|
player.on('loaded', onLoaded);
|
|
1667
1540
|
player.on('play', onPlay);
|
|
@@ -1731,7 +1604,6 @@ function useVideoPlayer(params) {
|
|
|
1731
1604
|
const vimeoPlayer = useVimeoPlayer(service === 'vimeo' ? videoId || url : null, params);
|
|
1732
1605
|
const nativePlayer = useNativeVideoPlayer(url, params);
|
|
1733
1606
|
let player = null;
|
|
1734
|
-
|
|
1735
1607
|
if (service === 'dailymotion') {
|
|
1736
1608
|
player = dailymotionPlayer;
|
|
1737
1609
|
} else if (service === 'youtube') {
|
|
@@ -1741,7 +1613,6 @@ function useVideoPlayer(params) {
|
|
|
1741
1613
|
} else {
|
|
1742
1614
|
player = nativePlayer;
|
|
1743
1615
|
}
|
|
1744
|
-
|
|
1745
1616
|
const {
|
|
1746
1617
|
playing = false,
|
|
1747
1618
|
paused = false,
|
|
@@ -1781,7 +1652,6 @@ function useVideoPlayer(params) {
|
|
|
1781
1652
|
}, [ended, customOnEnd]);
|
|
1782
1653
|
react.useEffect(() => {
|
|
1783
1654
|
const hasMetadata = metaWidth !== null || metaHeight !== null || metaDuration !== null;
|
|
1784
|
-
|
|
1785
1655
|
if (hasMetadata && customOnMetadataChange !== null) {
|
|
1786
1656
|
customOnMetadataChange({
|
|
1787
1657
|
width: metaWidth,
|
|
@@ -1797,9 +1667,7 @@ const getWindowScroll = () => ({
|
|
|
1797
1667
|
x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
|
|
1798
1668
|
y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
|
|
1799
1669
|
});
|
|
1800
|
-
|
|
1801
1670
|
const currentScroll = getWindowScroll();
|
|
1802
|
-
|
|
1803
1671
|
const useWindowScroll = function () {
|
|
1804
1672
|
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1805
1673
|
const {
|
|
@@ -1808,19 +1676,16 @@ const useWindowScroll = function () {
|
|
|
1808
1676
|
const [scroll, setScroll] = react.useState(currentScroll);
|
|
1809
1677
|
const updateScroll = react.useCallback(() => {
|
|
1810
1678
|
const newScroll = getWindowScroll();
|
|
1811
|
-
|
|
1812
1679
|
if (currentScroll.x !== newScroll.x || currentScroll.y !== newScroll.y) {
|
|
1813
1680
|
currentScroll.x = newScroll.x;
|
|
1814
1681
|
currentScroll.y = newScroll.y;
|
|
1815
1682
|
setScroll(newScroll);
|
|
1816
1683
|
return newScroll;
|
|
1817
1684
|
}
|
|
1818
|
-
|
|
1819
1685
|
return null;
|
|
1820
1686
|
}, [setScroll]);
|
|
1821
1687
|
const onScroll = react.useCallback(() => {
|
|
1822
1688
|
const newScroll = updateScroll();
|
|
1823
|
-
|
|
1824
1689
|
if (newScroll !== null && onChange !== null) {
|
|
1825
1690
|
onChange(newScroll);
|
|
1826
1691
|
}
|
|
@@ -1837,7 +1702,6 @@ const getWindowSize = () => ({
|
|
|
1837
1702
|
height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
|
|
1838
1703
|
});
|
|
1839
1704
|
let currentSize = getWindowSize();
|
|
1840
|
-
|
|
1841
1705
|
function useWindowSize() {
|
|
1842
1706
|
let {
|
|
1843
1707
|
onChange = null
|
|
@@ -1845,18 +1709,15 @@ function useWindowSize() {
|
|
|
1845
1709
|
const [size, setSize] = react.useState(currentSize);
|
|
1846
1710
|
const updateSize = react.useCallback(() => {
|
|
1847
1711
|
const newSize = getWindowSize();
|
|
1848
|
-
|
|
1849
1712
|
if (currentSize.width !== newSize.width || currentSize.height !== newSize.height) {
|
|
1850
1713
|
currentSize = newSize;
|
|
1851
1714
|
setSize(newSize);
|
|
1852
1715
|
return newSize;
|
|
1853
1716
|
}
|
|
1854
|
-
|
|
1855
1717
|
return null;
|
|
1856
1718
|
}, [setSize]);
|
|
1857
1719
|
const onResize = react.useCallback(() => {
|
|
1858
1720
|
const newSize = updateSize();
|
|
1859
|
-
|
|
1860
1721
|
if (newSize !== null && onChange !== null) {
|
|
1861
1722
|
onChange(newSize);
|
|
1862
1723
|
}
|