@folklore/hooks 0.0.32 → 0.0.34
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 +308 -308
- package/dist/es.js +309 -309
- package/package.json +5 -4
package/dist/es.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import raf from 'raf';
|
|
2
2
|
import { useState, useEffect, useRef, useMemo, useCallback } from 'react';
|
|
3
|
-
import { loadDailymotion,
|
|
3
|
+
import { loadDailymotion, loadVimeo, loadYouTube } from '@folklore/services';
|
|
4
4
|
import createDebug from 'debug';
|
|
5
5
|
import EventsManager from '@folklore/events';
|
|
6
6
|
import { cancelable } from 'cancelable-promise';
|
|
@@ -96,9 +96,8 @@ function usePlayerCurrentTime(player) {
|
|
|
96
96
|
return realCurrentTime.current;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
const useDailymotionPlayer = function () {
|
|
99
|
+
const NO_PLAYER_ERROR$3 = new Error('No player');
|
|
100
|
+
function useDailymotionPlayer() {
|
|
102
101
|
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
103
102
|
let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
104
103
|
const {
|
|
@@ -124,6 +123,7 @@ const useDailymotionPlayer = function () {
|
|
|
124
123
|
return match !== null ? match[1] : null;
|
|
125
124
|
}
|
|
126
125
|
} = params;
|
|
126
|
+
const debug = useMemo(() => createDebug('folklore:video:dailymotion'), []);
|
|
127
127
|
const [apiLoaded, setApiLoaded] = useState(typeof window !== 'undefined' && typeof window.DM !== 'undefined');
|
|
128
128
|
const [playerReady, setPlayerReady] = useState(false);
|
|
129
129
|
const [loaded, setLoaded] = useState(false);
|
|
@@ -153,12 +153,12 @@ const useDailymotionPlayer = function () {
|
|
|
153
153
|
useEffect(() => {
|
|
154
154
|
let canceled = false;
|
|
155
155
|
if (!apiLoaded && videoId !== null) {
|
|
156
|
-
debug
|
|
156
|
+
debug('Load API');
|
|
157
157
|
loadDailymotion().then(api => {
|
|
158
158
|
if (!canceled) {
|
|
159
159
|
apiRef.current = api;
|
|
160
160
|
setApiLoaded(true);
|
|
161
|
-
debug
|
|
161
|
+
debug('API Loaded');
|
|
162
162
|
}
|
|
163
163
|
});
|
|
164
164
|
}
|
|
@@ -197,7 +197,7 @@ const useDailymotionPlayer = function () {
|
|
|
197
197
|
player.load(videoId, {
|
|
198
198
|
params: playerParams
|
|
199
199
|
});
|
|
200
|
-
debug
|
|
200
|
+
debug('Load video [ID: %s]', videoId);
|
|
201
201
|
} else {
|
|
202
202
|
player = DM.player(element, {
|
|
203
203
|
video: videoId,
|
|
@@ -205,7 +205,7 @@ const useDailymotionPlayer = function () {
|
|
|
205
205
|
height,
|
|
206
206
|
params: playerParams
|
|
207
207
|
});
|
|
208
|
-
debug
|
|
208
|
+
debug('Create player [ID: %s]', videoId);
|
|
209
209
|
}
|
|
210
210
|
if (!playerReady) {
|
|
211
211
|
setPlayerReady(true);
|
|
@@ -224,7 +224,7 @@ const useDailymotionPlayer = function () {
|
|
|
224
224
|
let currentMetadata = metadata;
|
|
225
225
|
function onPlaybackReady() {
|
|
226
226
|
setLoaded(true);
|
|
227
|
-
debug
|
|
227
|
+
debug('onPlaybackReady [ID: %s]', videoId);
|
|
228
228
|
}
|
|
229
229
|
function onLoadedMetadata() {
|
|
230
230
|
currentMetadata = {
|
|
@@ -232,7 +232,7 @@ const useDailymotionPlayer = function () {
|
|
|
232
232
|
duration: player.duration
|
|
233
233
|
};
|
|
234
234
|
setMetadata(currentMetadata);
|
|
235
|
-
debug
|
|
235
|
+
debug('onLoadedMetadata [ID: %s]', videoId);
|
|
236
236
|
}
|
|
237
237
|
function onDurationChange() {
|
|
238
238
|
currentMetadata = {
|
|
@@ -240,12 +240,12 @@ const useDailymotionPlayer = function () {
|
|
|
240
240
|
duration: player.duration
|
|
241
241
|
};
|
|
242
242
|
setMetadata(currentMetadata);
|
|
243
|
-
debug
|
|
243
|
+
debug('onDurationChange [ID: %s]', videoId);
|
|
244
244
|
}
|
|
245
245
|
function onVolumeChange() {
|
|
246
246
|
setMuted(player.muted);
|
|
247
247
|
setVolumeState(player.volume);
|
|
248
|
-
debug
|
|
248
|
+
debug('onVolumeChange [ID: %s]', videoId);
|
|
249
249
|
}
|
|
250
250
|
function onPlay() {
|
|
251
251
|
currentPlayState = {
|
|
@@ -255,7 +255,7 @@ const useDailymotionPlayer = function () {
|
|
|
255
255
|
ended: false
|
|
256
256
|
};
|
|
257
257
|
setPlayState(currentPlayState);
|
|
258
|
-
debug
|
|
258
|
+
debug('onPlay [ID: %s]', videoId);
|
|
259
259
|
}
|
|
260
260
|
function onPause() {
|
|
261
261
|
currentPlayState = {
|
|
@@ -265,7 +265,7 @@ const useDailymotionPlayer = function () {
|
|
|
265
265
|
ended: false
|
|
266
266
|
};
|
|
267
267
|
setPlayState(currentPlayState);
|
|
268
|
-
debug
|
|
268
|
+
debug('onPause [ID: %s]', videoId);
|
|
269
269
|
}
|
|
270
270
|
function onEnd() {
|
|
271
271
|
currentPlayState = {
|
|
@@ -275,7 +275,7 @@ const useDailymotionPlayer = function () {
|
|
|
275
275
|
ended: true
|
|
276
276
|
};
|
|
277
277
|
setPlayState(currentPlayState);
|
|
278
|
-
debug
|
|
278
|
+
debug('onEnd [ID: %s]', videoId);
|
|
279
279
|
}
|
|
280
280
|
function onPlaying() {
|
|
281
281
|
currentPlayState = {
|
|
@@ -283,7 +283,7 @@ const useDailymotionPlayer = function () {
|
|
|
283
283
|
buffering: false
|
|
284
284
|
};
|
|
285
285
|
setPlayState(currentPlayState);
|
|
286
|
-
debug
|
|
286
|
+
debug('onPlaying [ID: %s]', videoId);
|
|
287
287
|
}
|
|
288
288
|
function onWaiting() {
|
|
289
289
|
currentPlayState = {
|
|
@@ -291,7 +291,7 @@ const useDailymotionPlayer = function () {
|
|
|
291
291
|
buffering: true
|
|
292
292
|
};
|
|
293
293
|
setPlayState(currentPlayState);
|
|
294
|
-
debug
|
|
294
|
+
debug('onWaiting [ID: %s]', videoId);
|
|
295
295
|
}
|
|
296
296
|
function onAdStart() {
|
|
297
297
|
currentPlayState = {
|
|
@@ -299,7 +299,7 @@ const useDailymotionPlayer = function () {
|
|
|
299
299
|
adPlaying: true
|
|
300
300
|
};
|
|
301
301
|
setPlayState(currentPlayState);
|
|
302
|
-
debug
|
|
302
|
+
debug('onAdStart [ID: %s]', videoId);
|
|
303
303
|
}
|
|
304
304
|
function onAdEnd() {
|
|
305
305
|
currentPlayState = {
|
|
@@ -307,7 +307,7 @@ const useDailymotionPlayer = function () {
|
|
|
307
307
|
adPlaying: false
|
|
308
308
|
};
|
|
309
309
|
setPlayState(currentPlayState);
|
|
310
|
-
debug
|
|
310
|
+
debug('onAdEnd [ID: %s]', videoId);
|
|
311
311
|
}
|
|
312
312
|
player.addEventListener('playback_ready', onPlaybackReady);
|
|
313
313
|
player.addEventListener('loadedmetadata', onLoadedMetadata);
|
|
@@ -338,37 +338,37 @@ const useDailymotionPlayer = function () {
|
|
|
338
338
|
const {
|
|
339
339
|
current: player
|
|
340
340
|
} = playerRef;
|
|
341
|
-
return player !== null ? player.play() : Promise.reject(
|
|
341
|
+
return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR$3);
|
|
342
342
|
}, []);
|
|
343
343
|
const pause = useCallback(() => {
|
|
344
344
|
const {
|
|
345
345
|
current: player
|
|
346
346
|
} = playerRef;
|
|
347
|
-
return player !== null ? player.pause() : Promise.reject(
|
|
347
|
+
return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR$3);
|
|
348
348
|
}, []);
|
|
349
349
|
const setVolume = useCallback(newVolume => {
|
|
350
350
|
const {
|
|
351
351
|
current: player
|
|
352
352
|
} = playerRef;
|
|
353
|
-
return player !== null ? player.setVolume(newVolume) : Promise.reject(
|
|
353
|
+
return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR$3);
|
|
354
354
|
}, []);
|
|
355
355
|
const mute = useCallback(() => {
|
|
356
356
|
const {
|
|
357
357
|
current: player
|
|
358
358
|
} = playerRef;
|
|
359
|
-
return player !== null ? player.setMute(true) : Promise.reject(
|
|
359
|
+
return player !== null ? player.setMute(true) : Promise.reject(NO_PLAYER_ERROR$3);
|
|
360
360
|
}, []);
|
|
361
361
|
const unmute = useCallback(() => {
|
|
362
362
|
const {
|
|
363
363
|
current: player
|
|
364
364
|
} = playerRef;
|
|
365
|
-
return player !== null ? player.setMute(false) : Promise.reject(
|
|
365
|
+
return player !== null ? player.setMute(false) : Promise.reject(NO_PLAYER_ERROR$3);
|
|
366
366
|
}, []);
|
|
367
367
|
const seek = useCallback(time => {
|
|
368
368
|
const {
|
|
369
369
|
current: player
|
|
370
370
|
} = playerRef;
|
|
371
|
-
return player !== null ? player.seek(time) : Promise.reject(
|
|
371
|
+
return player !== null ? player.seek(time) : Promise.reject(NO_PLAYER_ERROR$3);
|
|
372
372
|
}, []);
|
|
373
373
|
const {
|
|
374
374
|
playing
|
|
@@ -396,10 +396,10 @@ const useDailymotionPlayer = function () {
|
|
|
396
396
|
...metadata,
|
|
397
397
|
...playState
|
|
398
398
|
};
|
|
399
|
-
}
|
|
399
|
+
}
|
|
400
400
|
|
|
401
401
|
const eventsManager$1 = typeof document !== 'undefined' ? new EventsManager(document) : null;
|
|
402
|
-
|
|
402
|
+
function useDocumentEvent(event, callback) {
|
|
403
403
|
useEffect(() => {
|
|
404
404
|
if (eventsManager$1 !== null && callback !== null) {
|
|
405
405
|
eventsManager$1.subscribe(event, callback);
|
|
@@ -410,10 +410,10 @@ const useDocumentEvent = (event, callback) => {
|
|
|
410
410
|
}
|
|
411
411
|
};
|
|
412
412
|
}, [event, callback]);
|
|
413
|
-
}
|
|
413
|
+
}
|
|
414
414
|
|
|
415
415
|
const eventsManager = typeof window !== 'undefined' ? new EventsManager(window) : null;
|
|
416
|
-
|
|
416
|
+
function useWindowEvent(event, callback) {
|
|
417
417
|
useEffect(() => {
|
|
418
418
|
if (eventsManager !== null && callback !== null) {
|
|
419
419
|
eventsManager.subscribe(event, callback);
|
|
@@ -424,7 +424,7 @@ const useWindowEvent = (event, callback) => {
|
|
|
424
424
|
}
|
|
425
425
|
};
|
|
426
426
|
}, [event, callback]);
|
|
427
|
-
}
|
|
427
|
+
}
|
|
428
428
|
|
|
429
429
|
function useKeyboard() {
|
|
430
430
|
let keyMap = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
@@ -664,9 +664,8 @@ const useItemsPaginated = function (loader) {
|
|
|
664
664
|
};
|
|
665
665
|
};
|
|
666
666
|
|
|
667
|
-
const
|
|
668
|
-
|
|
669
|
-
const useNativeVideoPlayer = function (url) {
|
|
667
|
+
const NO_PLAYER_ERROR$2 = new Error('No player');
|
|
668
|
+
function useNativeVideoPlayer(url) {
|
|
670
669
|
let {
|
|
671
670
|
width = 0,
|
|
672
671
|
height = 0,
|
|
@@ -675,6 +674,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
675
674
|
timeUpdateInterval = 1000,
|
|
676
675
|
onTimeUpdate: customOnTimeUpdate = null
|
|
677
676
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
677
|
+
const debug = useMemo(() => createDebug('folklore:video:native'), []);
|
|
678
678
|
const elementRef = useRef(null);
|
|
679
679
|
const [loaded, setLoaded] = useState(false);
|
|
680
680
|
const [volume, setVolumeState] = useState(initialMuted ? 0 : 1);
|
|
@@ -693,13 +693,13 @@ const useNativeVideoPlayer = function (url) {
|
|
|
693
693
|
const {
|
|
694
694
|
current: player
|
|
695
695
|
} = elementRef;
|
|
696
|
-
return player !== null ? player.play() : Promise.reject(
|
|
696
|
+
return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR$2);
|
|
697
697
|
}, []);
|
|
698
698
|
const pause = useCallback(() => {
|
|
699
699
|
const {
|
|
700
700
|
current: player
|
|
701
701
|
} = elementRef;
|
|
702
|
-
return player !== null ? player.pause() : Promise.reject(
|
|
702
|
+
return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR$2);
|
|
703
703
|
}, []);
|
|
704
704
|
const setVolume = useCallback(newVolume => {
|
|
705
705
|
const {
|
|
@@ -709,7 +709,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
709
709
|
player.volume = newVolume;
|
|
710
710
|
return Promise.resolve(newVolume);
|
|
711
711
|
}
|
|
712
|
-
return Promise.reject(
|
|
712
|
+
return Promise.reject(NO_PLAYER_ERROR$2);
|
|
713
713
|
}, []);
|
|
714
714
|
const mute = useCallback(() => {
|
|
715
715
|
const {
|
|
@@ -719,7 +719,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
719
719
|
player.muted = true;
|
|
720
720
|
return Promise.resolve(true);
|
|
721
721
|
}
|
|
722
|
-
return Promise.reject(
|
|
722
|
+
return Promise.reject(NO_PLAYER_ERROR$2);
|
|
723
723
|
}, []);
|
|
724
724
|
const unmute = useCallback(() => {
|
|
725
725
|
const {
|
|
@@ -729,7 +729,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
729
729
|
player.muted = false;
|
|
730
730
|
return Promise.resolve(false);
|
|
731
731
|
}
|
|
732
|
-
return Promise.reject(
|
|
732
|
+
return Promise.reject(NO_PLAYER_ERROR$2);
|
|
733
733
|
}, []);
|
|
734
734
|
const seek = useCallback(newTime => {
|
|
735
735
|
const {
|
|
@@ -739,7 +739,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
739
739
|
player.currentTime = newTime;
|
|
740
740
|
return Promise.resolve(newTime);
|
|
741
741
|
}
|
|
742
|
-
return Promise.reject(
|
|
742
|
+
return Promise.reject(NO_PLAYER_ERROR$2);
|
|
743
743
|
}, []);
|
|
744
744
|
const setLoop = useCallback(newLoop => {
|
|
745
745
|
const {
|
|
@@ -749,7 +749,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
749
749
|
player.loop = newLoop;
|
|
750
750
|
return Promise.resolve(newLoop);
|
|
751
751
|
}
|
|
752
|
-
return Promise.reject(
|
|
752
|
+
return Promise.reject(NO_PLAYER_ERROR$2);
|
|
753
753
|
}, []);
|
|
754
754
|
|
|
755
755
|
// Bind player events
|
|
@@ -762,7 +762,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
762
762
|
}
|
|
763
763
|
const onCanPlay = () => {
|
|
764
764
|
setLoaded(true);
|
|
765
|
-
debug
|
|
765
|
+
debug('onCanPlay [URL: %s]', url);
|
|
766
766
|
};
|
|
767
767
|
const onMetadataLoaded = () => {
|
|
768
768
|
const newMetadata = {
|
|
@@ -771,7 +771,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
771
771
|
height: player.videoHeight
|
|
772
772
|
};
|
|
773
773
|
setMetadata(newMetadata);
|
|
774
|
-
debug
|
|
774
|
+
debug('onMetadataLoaded [URL: %s]', url);
|
|
775
775
|
};
|
|
776
776
|
const onPlay = () => {
|
|
777
777
|
setPlayState({
|
|
@@ -780,7 +780,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
780
780
|
ended: false,
|
|
781
781
|
buffering: false
|
|
782
782
|
});
|
|
783
|
-
debug
|
|
783
|
+
debug('onPlay [URL: %s]', url);
|
|
784
784
|
};
|
|
785
785
|
const onPause = () => {
|
|
786
786
|
setPlayState({
|
|
@@ -789,7 +789,7 @@ const useNativeVideoPlayer = function (url) {
|
|
|
789
789
|
ended: false,
|
|
790
790
|
buffering: false
|
|
791
791
|
});
|
|
792
|
-
debug
|
|
792
|
+
debug('onPause [URL: %s]', url);
|
|
793
793
|
};
|
|
794
794
|
const onEnded = () => {
|
|
795
795
|
setPlayState({
|
|
@@ -798,16 +798,16 @@ const useNativeVideoPlayer = function (url) {
|
|
|
798
798
|
ended: true,
|
|
799
799
|
buffering: false
|
|
800
800
|
});
|
|
801
|
-
debug
|
|
801
|
+
debug('onEnded [URL: %s]', url);
|
|
802
802
|
};
|
|
803
|
-
debug
|
|
803
|
+
debug('Bind events [URL: %s]', url);
|
|
804
804
|
player.addEventListener('canplay', onCanPlay);
|
|
805
805
|
player.addEventListener('metadataloaded', onMetadataLoaded);
|
|
806
806
|
player.addEventListener('play', onPlay);
|
|
807
807
|
player.addEventListener('pause', onPause);
|
|
808
808
|
player.addEventListener('ended', onEnded);
|
|
809
809
|
return () => {
|
|
810
|
-
debug
|
|
810
|
+
debug('Unbind events [URL: %s]', url);
|
|
811
811
|
player.removeEventListener('canplay', onCanPlay);
|
|
812
812
|
player.removeEventListener('metadataloaded', onMetadataLoaded);
|
|
813
813
|
player.removeEventListener('play', onPlay);
|
|
@@ -842,18 +842,18 @@ const useNativeVideoPlayer = function (url) {
|
|
|
842
842
|
...metadata,
|
|
843
843
|
...playState
|
|
844
844
|
};
|
|
845
|
-
}
|
|
845
|
+
}
|
|
846
846
|
|
|
847
847
|
const observersCache = new Map();
|
|
848
|
-
|
|
848
|
+
function getOptionsKey(_ref) {
|
|
849
849
|
let {
|
|
850
850
|
root = null,
|
|
851
851
|
rootMargin,
|
|
852
852
|
threshold = null
|
|
853
853
|
} = _ref;
|
|
854
854
|
return `root_${root}_rootMargin_${rootMargin || null}_threshold_${threshold}`;
|
|
855
|
-
}
|
|
856
|
-
|
|
855
|
+
}
|
|
856
|
+
function createObserver(Observer) {
|
|
857
857
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
858
858
|
let subscribers = [];
|
|
859
859
|
const addSubscriber = (element, callback) => {
|
|
@@ -916,8 +916,8 @@ const createObserver = function (Observer) {
|
|
|
916
916
|
unsubscribe,
|
|
917
917
|
observer
|
|
918
918
|
};
|
|
919
|
-
}
|
|
920
|
-
|
|
919
|
+
}
|
|
920
|
+
function getObserver() {
|
|
921
921
|
let Observer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
922
922
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
923
923
|
if (Observer === null) {
|
|
@@ -937,8 +937,8 @@ const getObserver = function () {
|
|
|
937
937
|
observersCache.set(Observer, observers);
|
|
938
938
|
}
|
|
939
939
|
return observers[observerKey];
|
|
940
|
-
}
|
|
941
|
-
|
|
940
|
+
}
|
|
941
|
+
function useObserver(Observer) {
|
|
942
942
|
let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
943
943
|
let initialEntry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
944
944
|
const {
|
|
@@ -987,7 +987,7 @@ const useObserver = function (Observer) {
|
|
|
987
987
|
ref: nodeRef,
|
|
988
988
|
entry
|
|
989
989
|
};
|
|
990
|
-
}
|
|
990
|
+
}
|
|
991
991
|
|
|
992
992
|
/**
|
|
993
993
|
* Intersection Observer
|
|
@@ -1003,7 +1003,7 @@ const intersectionObserverInitialEntry = {
|
|
|
1003
1003
|
boundingClientRect: null,
|
|
1004
1004
|
rootBounds: null
|
|
1005
1005
|
};
|
|
1006
|
-
|
|
1006
|
+
function useIntersectionObserver() {
|
|
1007
1007
|
let {
|
|
1008
1008
|
root = null,
|
|
1009
1009
|
rootMargin = '0px',
|
|
@@ -1016,7 +1016,7 @@ const useIntersectionObserver = function () {
|
|
|
1016
1016
|
threshold,
|
|
1017
1017
|
disabled
|
|
1018
1018
|
}, intersectionObserverInitialEntry);
|
|
1019
|
-
}
|
|
1019
|
+
}
|
|
1020
1020
|
|
|
1021
1021
|
/**
|
|
1022
1022
|
* Resize Observer
|
|
@@ -1027,255 +1027,17 @@ const resizeObserverInitialEntry = {
|
|
|
1027
1027
|
contentBoxSize: null,
|
|
1028
1028
|
borderBoxSize: null
|
|
1029
1029
|
};
|
|
1030
|
-
|
|
1030
|
+
function useResizeObserver() {
|
|
1031
1031
|
let {
|
|
1032
1032
|
disabled = false
|
|
1033
1033
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1034
1034
|
return useObserver(typeof ResizeObserver !== 'undefined' ? ResizeObserver : null, {
|
|
1035
1035
|
disabled
|
|
1036
1036
|
}, resizeObserverInitialEntry);
|
|
1037
|
-
};
|
|
1038
|
-
|
|
1039
|
-
const NO_PLAYER_ERROR$1 = new Error('No player');
|
|
1040
|
-
const debug$1 = createDebug('folklore:video:youtube');
|
|
1041
|
-
function useYouTubePlayer(id) {
|
|
1042
|
-
let {
|
|
1043
|
-
width = 0,
|
|
1044
|
-
height = 0,
|
|
1045
|
-
duration = 0,
|
|
1046
|
-
autoplay = false,
|
|
1047
|
-
controls = true,
|
|
1048
|
-
timeUpdateInterval = 1000,
|
|
1049
|
-
muted: initialMuted = false,
|
|
1050
|
-
onVolumeChange: customOnVolumeChange = null,
|
|
1051
|
-
onTimeUpdate: customOnTimeUpdate = null,
|
|
1052
|
-
getVideoId = url => {
|
|
1053
|
-
if (url === null || url.match(/^https?:/) === null) {
|
|
1054
|
-
return url;
|
|
1055
|
-
}
|
|
1056
|
-
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
|
|
1057
|
-
const match = url.match(regExp);
|
|
1058
|
-
return match && match[7].length === 11 ? match[7] : null;
|
|
1059
|
-
}
|
|
1060
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
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);
|
|
1063
|
-
const elementRef = useRef(null);
|
|
1064
|
-
const playerRef = useRef(null);
|
|
1065
|
-
const playerElementRef = useRef(elementRef.current);
|
|
1066
|
-
const elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1067
|
-
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
1068
|
-
const [ready, setReady] = useState(false);
|
|
1069
|
-
const [muted, setMuted] = useState(initialMuted);
|
|
1070
|
-
const [playState, setPlayState] = useState({
|
|
1071
|
-
playing: false,
|
|
1072
|
-
paused: false,
|
|
1073
|
-
ended: false,
|
|
1074
|
-
buffering: false
|
|
1075
|
-
});
|
|
1076
|
-
const [metadata, setMetadata] = useState({
|
|
1077
|
-
width,
|
|
1078
|
-
height,
|
|
1079
|
-
duration
|
|
1080
|
-
});
|
|
1081
|
-
useEffect(() => {
|
|
1082
|
-
let canceled = false;
|
|
1083
|
-
if (!apiLoaded && videoId !== null) {
|
|
1084
|
-
debug$1('Load API');
|
|
1085
|
-
loadYouTube().then(api => {
|
|
1086
|
-
if (!canceled) {
|
|
1087
|
-
apiRef.current = api;
|
|
1088
|
-
setApiLoaded(true);
|
|
1089
|
-
debug$1('API Loaded');
|
|
1090
|
-
}
|
|
1091
|
-
});
|
|
1092
|
-
}
|
|
1093
|
-
return () => {
|
|
1094
|
-
canceled = true;
|
|
1095
|
-
};
|
|
1096
|
-
}, [apiLoaded, videoId, setApiLoaded]);
|
|
1097
|
-
const play = useCallback(() => {
|
|
1098
|
-
const {
|
|
1099
|
-
current: player
|
|
1100
|
-
} = playerRef;
|
|
1101
|
-
return player !== null && typeof player.playVideo !== 'undefined' ? Promise.resolve(player.playVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1102
|
-
}, []);
|
|
1103
|
-
const pause = useCallback(() => {
|
|
1104
|
-
const {
|
|
1105
|
-
current: player
|
|
1106
|
-
} = playerRef;
|
|
1107
|
-
return player !== null && typeof player.pauseVideo !== 'undefined' ? Promise.resolve(player.pauseVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1108
|
-
}, []);
|
|
1109
|
-
const setVolume = useCallback(volume => {
|
|
1110
|
-
const {
|
|
1111
|
-
current: player
|
|
1112
|
-
} = playerRef;
|
|
1113
|
-
const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1114
|
-
if (customOnVolumeChange) {
|
|
1115
|
-
customOnVolumeChange(volume);
|
|
1116
|
-
}
|
|
1117
|
-
return promise;
|
|
1118
|
-
}, [customOnVolumeChange]);
|
|
1119
|
-
const mute = useCallback(() => {
|
|
1120
|
-
const {
|
|
1121
|
-
current: player
|
|
1122
|
-
} = playerRef;
|
|
1123
|
-
return (player !== null && typeof player.mute !== 'undefined' ? Promise.resolve(player.mute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(true));
|
|
1124
|
-
}, [setMuted]);
|
|
1125
|
-
const unmute = useCallback(() => {
|
|
1126
|
-
const {
|
|
1127
|
-
current: player
|
|
1128
|
-
} = playerRef;
|
|
1129
|
-
return (player !== null && typeof player.unMute !== 'undefined' ? Promise.resolve(player.unMute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(false));
|
|
1130
|
-
}, []);
|
|
1131
|
-
const seek = useCallback(time => {
|
|
1132
|
-
const {
|
|
1133
|
-
current: player
|
|
1134
|
-
} = playerRef;
|
|
1135
|
-
return player !== null && typeof player.seekTo !== 'undefined' ? Promise.resolve(player.seekTo(time)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1136
|
-
}, []);
|
|
1137
|
-
const setLoop = useCallback(loop => {
|
|
1138
|
-
const {
|
|
1139
|
-
current: player
|
|
1140
|
-
} = playerRef;
|
|
1141
|
-
return player !== null && typeof player.setLoop !== 'undefined' ? Promise.resolve(player.setLoop(loop)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1142
|
-
}, []);
|
|
1143
|
-
const destroyPlayer = useCallback(() => {
|
|
1144
|
-
if (playerRef.current !== null) {
|
|
1145
|
-
debug$1('Unset player');
|
|
1146
|
-
playerRef.current = null;
|
|
1147
|
-
}
|
|
1148
|
-
}, []);
|
|
1149
|
-
|
|
1150
|
-
// Detect iframe switch and destroy player
|
|
1151
|
-
|
|
1152
|
-
useEffect(() => {
|
|
1153
|
-
const {
|
|
1154
|
-
current: currentPlayer
|
|
1155
|
-
} = playerRef;
|
|
1156
|
-
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1157
|
-
debug$1('iFrame switched');
|
|
1158
|
-
destroyPlayer();
|
|
1159
|
-
}
|
|
1160
|
-
}, [elementHasChanged]);
|
|
1161
|
-
|
|
1162
|
-
// Create player
|
|
1163
|
-
useEffect(() => {
|
|
1164
|
-
const {
|
|
1165
|
-
current: YT = null
|
|
1166
|
-
} = apiRef;
|
|
1167
|
-
const {
|
|
1168
|
-
current: currentPlayer = null
|
|
1169
|
-
} = playerRef;
|
|
1170
|
-
const {
|
|
1171
|
-
current: element = null
|
|
1172
|
-
} = elementRef;
|
|
1173
|
-
if (!apiLoaded || videoId === null || element === null) {
|
|
1174
|
-
destroyPlayer();
|
|
1175
|
-
return;
|
|
1176
|
-
}
|
|
1177
|
-
let player = currentPlayer;
|
|
1178
|
-
if (player !== null && typeof player.loadVideoById !== 'undefined') {
|
|
1179
|
-
debug$1('Switch video [ID: %s]', videoId);
|
|
1180
|
-
player.loadVideoById(videoId);
|
|
1181
|
-
} else {
|
|
1182
|
-
debug$1('Create player [ID: %s]', videoId);
|
|
1183
|
-
const onReady = _ref => {
|
|
1184
|
-
let {
|
|
1185
|
-
target
|
|
1186
|
-
} = _ref;
|
|
1187
|
-
player = target;
|
|
1188
|
-
playerRef.current = target;
|
|
1189
|
-
setReady(true);
|
|
1190
|
-
const newDuration = target.getDuration();
|
|
1191
|
-
if (newDuration !== metadata.duration) {
|
|
1192
|
-
setMetadata({
|
|
1193
|
-
...metadata,
|
|
1194
|
-
duration: newDuration
|
|
1195
|
-
});
|
|
1196
|
-
}
|
|
1197
|
-
debug$1('onReady [ID: %s]', videoId);
|
|
1198
|
-
};
|
|
1199
|
-
const onStateChange = _ref2 => {
|
|
1200
|
-
let {
|
|
1201
|
-
data: state
|
|
1202
|
-
} = _ref2;
|
|
1203
|
-
const newState = {
|
|
1204
|
-
playing: state === YT.PlayerState.PLAYING,
|
|
1205
|
-
paused: state === YT.PlayerState.PAUSED,
|
|
1206
|
-
ended: state === YT.PlayerState.ENDED,
|
|
1207
|
-
buffering: state === YT.PlayerState.BUFFERING
|
|
1208
|
-
};
|
|
1209
|
-
setPlayState(newState);
|
|
1210
|
-
let stateLabel = null;
|
|
1211
|
-
if (newState.playing) {
|
|
1212
|
-
stateLabel = 'playing';
|
|
1213
|
-
} else if (newState.paused) {
|
|
1214
|
-
stateLabel = 'paused';
|
|
1215
|
-
} else if (newState.ended) {
|
|
1216
|
-
stateLabel = 'ended';
|
|
1217
|
-
} else if (newState.buffering) {
|
|
1218
|
-
stateLabel = 'buffering';
|
|
1219
|
-
} else if (state === -1) {
|
|
1220
|
-
stateLabel = 'not started';
|
|
1221
|
-
} else if (state === 0) {
|
|
1222
|
-
stateLabel = 'stopped';
|
|
1223
|
-
}
|
|
1224
|
-
debug$1('onStateChange %s [ID: %s]', stateLabel, videoId);
|
|
1225
|
-
};
|
|
1226
|
-
player = new YT.Player(element, {
|
|
1227
|
-
videoId,
|
|
1228
|
-
playerVars: {
|
|
1229
|
-
controls,
|
|
1230
|
-
autoplay: autoplay ? 1 : 0,
|
|
1231
|
-
mute: initialMuted,
|
|
1232
|
-
playsinline: true,
|
|
1233
|
-
rel: 0,
|
|
1234
|
-
showinfo: 0,
|
|
1235
|
-
modestbranding: 1
|
|
1236
|
-
},
|
|
1237
|
-
events: {
|
|
1238
|
-
onReady,
|
|
1239
|
-
onStateChange
|
|
1240
|
-
}
|
|
1241
|
-
});
|
|
1242
|
-
}
|
|
1243
|
-
playerRef.current = player;
|
|
1244
|
-
playerElementRef.current = element;
|
|
1245
|
-
}, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
|
|
1246
|
-
const {
|
|
1247
|
-
playing
|
|
1248
|
-
} = playState;
|
|
1249
|
-
const getCurrentTime = useCallback(p => p.getCurrentTime(), []);
|
|
1250
|
-
const currentTime = usePlayerCurrentTime(playerRef.current, {
|
|
1251
|
-
id: videoId,
|
|
1252
|
-
disabled: !playing || timeUpdateInterval === null,
|
|
1253
|
-
updateInterval: timeUpdateInterval,
|
|
1254
|
-
onUpdate: customOnTimeUpdate,
|
|
1255
|
-
getCurrentTime
|
|
1256
|
-
});
|
|
1257
|
-
return {
|
|
1258
|
-
ref: elementRef,
|
|
1259
|
-
player: playerRef.current,
|
|
1260
|
-
play,
|
|
1261
|
-
pause,
|
|
1262
|
-
mute,
|
|
1263
|
-
unmute,
|
|
1264
|
-
setVolume,
|
|
1265
|
-
seek,
|
|
1266
|
-
setLoop,
|
|
1267
|
-
ready,
|
|
1268
|
-
currentTime,
|
|
1269
|
-
muted,
|
|
1270
|
-
loaded: ready,
|
|
1271
|
-
...metadata,
|
|
1272
|
-
...playState
|
|
1273
|
-
};
|
|
1274
1037
|
}
|
|
1275
1038
|
|
|
1276
|
-
const
|
|
1277
|
-
|
|
1278
|
-
const useVimeoPlayer = function (id) {
|
|
1039
|
+
const NO_PLAYER_ERROR$1 = new Error('No player');
|
|
1040
|
+
function useVimeoPlayer(id) {
|
|
1279
1041
|
let {
|
|
1280
1042
|
width = 0,
|
|
1281
1043
|
height = 0,
|
|
@@ -1295,6 +1057,7 @@ const useVimeoPlayer = function (id) {
|
|
|
1295
1057
|
return match !== null ? match[1] : null;
|
|
1296
1058
|
}
|
|
1297
1059
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1060
|
+
const debug = useMemo(() => createDebug('folklore:video:vimeo'), []);
|
|
1298
1061
|
const [apiLoaded, setApiLoaded] = useState(false);
|
|
1299
1062
|
const apiRef = useRef(null);
|
|
1300
1063
|
const elementRef = useRef(null);
|
|
@@ -1338,43 +1101,43 @@ const useVimeoPlayer = function (id) {
|
|
|
1338
1101
|
const {
|
|
1339
1102
|
current: player
|
|
1340
1103
|
} = playerRef;
|
|
1341
|
-
return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR);
|
|
1104
|
+
return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1342
1105
|
}, []);
|
|
1343
1106
|
const pause = useCallback(() => {
|
|
1344
1107
|
const {
|
|
1345
1108
|
current: player
|
|
1346
1109
|
} = playerRef;
|
|
1347
|
-
return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR);
|
|
1110
|
+
return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1348
1111
|
}, []);
|
|
1349
1112
|
const setVolume = useCallback(newVolume => {
|
|
1350
1113
|
const {
|
|
1351
1114
|
current: player
|
|
1352
1115
|
} = playerRef;
|
|
1353
|
-
return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR);
|
|
1116
|
+
return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1354
1117
|
}, []);
|
|
1355
1118
|
const mute = useCallback(() => {
|
|
1356
1119
|
const {
|
|
1357
1120
|
current: player
|
|
1358
1121
|
} = playerRef;
|
|
1359
|
-
return player !== null ? player.setVolume(0) : Promise.reject(NO_PLAYER_ERROR);
|
|
1122
|
+
return player !== null ? player.setVolume(0) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1360
1123
|
}, []);
|
|
1361
1124
|
const unmute = useCallback(() => {
|
|
1362
1125
|
const {
|
|
1363
1126
|
current: player
|
|
1364
1127
|
} = playerRef;
|
|
1365
|
-
return player !== null ? player.setVolume(1) : Promise.reject(NO_PLAYER_ERROR);
|
|
1128
|
+
return player !== null ? player.setVolume(1) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1366
1129
|
}, []);
|
|
1367
1130
|
const seek = useCallback(time => {
|
|
1368
1131
|
const {
|
|
1369
1132
|
current: player
|
|
1370
1133
|
} = playerRef;
|
|
1371
|
-
return player !== null ? player.setCurrentTime(time) : Promise.reject(NO_PLAYER_ERROR);
|
|
1134
|
+
return player !== null ? player.setCurrentTime(time) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1372
1135
|
}, []);
|
|
1373
1136
|
const setLoop = useCallback(loop => {
|
|
1374
1137
|
const {
|
|
1375
1138
|
current: player
|
|
1376
1139
|
} = playerRef;
|
|
1377
|
-
return player !== null ? player.setLoop(loop) : Promise.reject(NO_PLAYER_ERROR);
|
|
1140
|
+
return player !== null ? player.setLoop(loop) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1378
1141
|
}, []);
|
|
1379
1142
|
const destroyVideo = useCallback(() => {
|
|
1380
1143
|
const {
|
|
@@ -1573,7 +1336,244 @@ const useVimeoPlayer = function (id) {
|
|
|
1573
1336
|
...metadata,
|
|
1574
1337
|
...playState
|
|
1575
1338
|
};
|
|
1576
|
-
}
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
const NO_PLAYER_ERROR = new Error('No player');
|
|
1342
|
+
function useYouTubePlayer(id) {
|
|
1343
|
+
let {
|
|
1344
|
+
width = 0,
|
|
1345
|
+
height = 0,
|
|
1346
|
+
duration = 0,
|
|
1347
|
+
autoplay = false,
|
|
1348
|
+
controls = true,
|
|
1349
|
+
timeUpdateInterval = 1000,
|
|
1350
|
+
muted: initialMuted = false,
|
|
1351
|
+
onVolumeChange: customOnVolumeChange = null,
|
|
1352
|
+
onTimeUpdate: customOnTimeUpdate = null,
|
|
1353
|
+
getVideoId = url => {
|
|
1354
|
+
if (url === null || url.match(/^https?:/) === null) {
|
|
1355
|
+
return url;
|
|
1356
|
+
}
|
|
1357
|
+
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
|
|
1358
|
+
const match = url.match(regExp);
|
|
1359
|
+
return match && match[7].length === 11 ? match[7] : null;
|
|
1360
|
+
}
|
|
1361
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1362
|
+
const debug = useMemo(() => createDebug('folklore:video:youtube'), []);
|
|
1363
|
+
const [apiLoaded, setApiLoaded] = useState(typeof window !== 'undefined' && typeof window.YT !== 'undefined');
|
|
1364
|
+
const apiRef = useRef(typeof window !== 'undefined' && typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1365
|
+
const elementRef = useRef(null);
|
|
1366
|
+
const playerRef = useRef(null);
|
|
1367
|
+
const playerElementRef = useRef(elementRef.current);
|
|
1368
|
+
const elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1369
|
+
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
1370
|
+
const [ready, setReady] = useState(false);
|
|
1371
|
+
const [muted, setMuted] = useState(initialMuted);
|
|
1372
|
+
const [playState, setPlayState] = useState({
|
|
1373
|
+
playing: false,
|
|
1374
|
+
paused: false,
|
|
1375
|
+
ended: false,
|
|
1376
|
+
buffering: false
|
|
1377
|
+
});
|
|
1378
|
+
const [metadata, setMetadata] = useState({
|
|
1379
|
+
width,
|
|
1380
|
+
height,
|
|
1381
|
+
duration
|
|
1382
|
+
});
|
|
1383
|
+
useEffect(() => {
|
|
1384
|
+
let canceled = false;
|
|
1385
|
+
if (!apiLoaded && videoId !== null) {
|
|
1386
|
+
debug('Load API');
|
|
1387
|
+
loadYouTube().then(api => {
|
|
1388
|
+
if (!canceled) {
|
|
1389
|
+
apiRef.current = api;
|
|
1390
|
+
setApiLoaded(true);
|
|
1391
|
+
debug('API Loaded');
|
|
1392
|
+
}
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1395
|
+
return () => {
|
|
1396
|
+
canceled = true;
|
|
1397
|
+
};
|
|
1398
|
+
}, [apiLoaded, videoId, setApiLoaded]);
|
|
1399
|
+
const play = useCallback(() => {
|
|
1400
|
+
const {
|
|
1401
|
+
current: player
|
|
1402
|
+
} = playerRef;
|
|
1403
|
+
return player !== null && typeof player.playVideo !== 'undefined' ? Promise.resolve(player.playVideo()) : Promise.reject(NO_PLAYER_ERROR);
|
|
1404
|
+
}, []);
|
|
1405
|
+
const pause = useCallback(() => {
|
|
1406
|
+
const {
|
|
1407
|
+
current: player
|
|
1408
|
+
} = playerRef;
|
|
1409
|
+
return player !== null && typeof player.pauseVideo !== 'undefined' ? Promise.resolve(player.pauseVideo()) : Promise.reject(NO_PLAYER_ERROR);
|
|
1410
|
+
}, []);
|
|
1411
|
+
const setVolume = useCallback(volume => {
|
|
1412
|
+
const {
|
|
1413
|
+
current: player
|
|
1414
|
+
} = playerRef;
|
|
1415
|
+
const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR);
|
|
1416
|
+
if (customOnVolumeChange) {
|
|
1417
|
+
customOnVolumeChange(volume);
|
|
1418
|
+
}
|
|
1419
|
+
return promise;
|
|
1420
|
+
}, [customOnVolumeChange]);
|
|
1421
|
+
const mute = useCallback(() => {
|
|
1422
|
+
const {
|
|
1423
|
+
current: player
|
|
1424
|
+
} = playerRef;
|
|
1425
|
+
return (player !== null && typeof player.mute !== 'undefined' ? Promise.resolve(player.mute()) : Promise.reject(NO_PLAYER_ERROR)).then(() => setMuted(true));
|
|
1426
|
+
}, [setMuted]);
|
|
1427
|
+
const unmute = useCallback(() => {
|
|
1428
|
+
const {
|
|
1429
|
+
current: player
|
|
1430
|
+
} = playerRef;
|
|
1431
|
+
return (player !== null && typeof player.unMute !== 'undefined' ? Promise.resolve(player.unMute()) : Promise.reject(NO_PLAYER_ERROR)).then(() => setMuted(false));
|
|
1432
|
+
}, []);
|
|
1433
|
+
const seek = useCallback(time => {
|
|
1434
|
+
const {
|
|
1435
|
+
current: player
|
|
1436
|
+
} = playerRef;
|
|
1437
|
+
return player !== null && typeof player.seekTo !== 'undefined' ? Promise.resolve(player.seekTo(time)) : Promise.reject(NO_PLAYER_ERROR);
|
|
1438
|
+
}, []);
|
|
1439
|
+
const setLoop = useCallback(loop => {
|
|
1440
|
+
const {
|
|
1441
|
+
current: player
|
|
1442
|
+
} = playerRef;
|
|
1443
|
+
return player !== null && typeof player.setLoop !== 'undefined' ? Promise.resolve(player.setLoop(loop)) : Promise.reject(NO_PLAYER_ERROR);
|
|
1444
|
+
}, []);
|
|
1445
|
+
const destroyPlayer = useCallback(() => {
|
|
1446
|
+
if (playerRef.current !== null) {
|
|
1447
|
+
debug('Unset player');
|
|
1448
|
+
playerRef.current = null;
|
|
1449
|
+
}
|
|
1450
|
+
}, []);
|
|
1451
|
+
|
|
1452
|
+
// Detect iframe switch and destroy player
|
|
1453
|
+
|
|
1454
|
+
useEffect(() => {
|
|
1455
|
+
const {
|
|
1456
|
+
current: currentPlayer
|
|
1457
|
+
} = playerRef;
|
|
1458
|
+
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1459
|
+
debug('iFrame switched');
|
|
1460
|
+
destroyPlayer();
|
|
1461
|
+
}
|
|
1462
|
+
}, [elementHasChanged]);
|
|
1463
|
+
|
|
1464
|
+
// Create player
|
|
1465
|
+
useEffect(() => {
|
|
1466
|
+
const {
|
|
1467
|
+
current: YT = null
|
|
1468
|
+
} = apiRef;
|
|
1469
|
+
const {
|
|
1470
|
+
current: currentPlayer = null
|
|
1471
|
+
} = playerRef;
|
|
1472
|
+
const {
|
|
1473
|
+
current: element = null
|
|
1474
|
+
} = elementRef;
|
|
1475
|
+
if (!apiLoaded || videoId === null || element === null) {
|
|
1476
|
+
destroyPlayer();
|
|
1477
|
+
return;
|
|
1478
|
+
}
|
|
1479
|
+
let player = currentPlayer;
|
|
1480
|
+
if (player !== null && typeof player.loadVideoById !== 'undefined') {
|
|
1481
|
+
debug('Switch video [ID: %s]', videoId);
|
|
1482
|
+
player.loadVideoById(videoId);
|
|
1483
|
+
} else {
|
|
1484
|
+
debug('Create player [ID: %s]', videoId);
|
|
1485
|
+
const onReady = _ref => {
|
|
1486
|
+
let {
|
|
1487
|
+
target
|
|
1488
|
+
} = _ref;
|
|
1489
|
+
player = target;
|
|
1490
|
+
playerRef.current = target;
|
|
1491
|
+
setReady(true);
|
|
1492
|
+
const newDuration = target.getDuration();
|
|
1493
|
+
if (newDuration !== metadata.duration) {
|
|
1494
|
+
setMetadata({
|
|
1495
|
+
...metadata,
|
|
1496
|
+
duration: newDuration
|
|
1497
|
+
});
|
|
1498
|
+
}
|
|
1499
|
+
debug('onReady [ID: %s]', videoId);
|
|
1500
|
+
};
|
|
1501
|
+
const onStateChange = _ref2 => {
|
|
1502
|
+
let {
|
|
1503
|
+
data: state
|
|
1504
|
+
} = _ref2;
|
|
1505
|
+
const newState = {
|
|
1506
|
+
playing: state === YT.PlayerState.PLAYING,
|
|
1507
|
+
paused: state === YT.PlayerState.PAUSED,
|
|
1508
|
+
ended: state === YT.PlayerState.ENDED,
|
|
1509
|
+
buffering: state === YT.PlayerState.BUFFERING
|
|
1510
|
+
};
|
|
1511
|
+
setPlayState(newState);
|
|
1512
|
+
let stateLabel = null;
|
|
1513
|
+
if (newState.playing) {
|
|
1514
|
+
stateLabel = 'playing';
|
|
1515
|
+
} else if (newState.paused) {
|
|
1516
|
+
stateLabel = 'paused';
|
|
1517
|
+
} else if (newState.ended) {
|
|
1518
|
+
stateLabel = 'ended';
|
|
1519
|
+
} else if (newState.buffering) {
|
|
1520
|
+
stateLabel = 'buffering';
|
|
1521
|
+
} else if (state === -1) {
|
|
1522
|
+
stateLabel = 'not started';
|
|
1523
|
+
} else if (state === 0) {
|
|
1524
|
+
stateLabel = 'stopped';
|
|
1525
|
+
}
|
|
1526
|
+
debug('onStateChange %s [ID: %s]', stateLabel, videoId);
|
|
1527
|
+
};
|
|
1528
|
+
player = new YT.Player(element, {
|
|
1529
|
+
videoId,
|
|
1530
|
+
playerVars: {
|
|
1531
|
+
controls,
|
|
1532
|
+
autoplay: autoplay ? 1 : 0,
|
|
1533
|
+
mute: initialMuted,
|
|
1534
|
+
playsinline: true,
|
|
1535
|
+
rel: 0,
|
|
1536
|
+
showinfo: 0,
|
|
1537
|
+
modestbranding: 1
|
|
1538
|
+
},
|
|
1539
|
+
events: {
|
|
1540
|
+
onReady,
|
|
1541
|
+
onStateChange
|
|
1542
|
+
}
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
1545
|
+
playerRef.current = player;
|
|
1546
|
+
playerElementRef.current = element;
|
|
1547
|
+
}, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
|
|
1548
|
+
const {
|
|
1549
|
+
playing
|
|
1550
|
+
} = playState;
|
|
1551
|
+
const getCurrentTime = useCallback(p => p.getCurrentTime(), []);
|
|
1552
|
+
const currentTime = usePlayerCurrentTime(playerRef.current, {
|
|
1553
|
+
id: videoId,
|
|
1554
|
+
disabled: !playing || timeUpdateInterval === null,
|
|
1555
|
+
updateInterval: timeUpdateInterval,
|
|
1556
|
+
onUpdate: customOnTimeUpdate,
|
|
1557
|
+
getCurrentTime
|
|
1558
|
+
});
|
|
1559
|
+
return {
|
|
1560
|
+
ref: elementRef,
|
|
1561
|
+
player: playerRef.current,
|
|
1562
|
+
play,
|
|
1563
|
+
pause,
|
|
1564
|
+
mute,
|
|
1565
|
+
unmute,
|
|
1566
|
+
setVolume,
|
|
1567
|
+
seek,
|
|
1568
|
+
setLoop,
|
|
1569
|
+
ready,
|
|
1570
|
+
currentTime,
|
|
1571
|
+
muted,
|
|
1572
|
+
loaded: ready,
|
|
1573
|
+
...metadata,
|
|
1574
|
+
...playState
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
1577
|
|
|
1578
1578
|
function useVideoPlayer(params) {
|
|
1579
1579
|
const {
|
|
@@ -1657,7 +1657,7 @@ const getWindowScroll = () => ({
|
|
|
1657
1657
|
y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
|
|
1658
1658
|
});
|
|
1659
1659
|
const currentScroll = getWindowScroll();
|
|
1660
|
-
|
|
1660
|
+
function useWindowScroll() {
|
|
1661
1661
|
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1662
1662
|
const {
|
|
1663
1663
|
onChange = null
|
|
@@ -1684,7 +1684,7 @@ const useWindowScroll = function () {
|
|
|
1684
1684
|
onScroll();
|
|
1685
1685
|
}, []);
|
|
1686
1686
|
return scroll;
|
|
1687
|
-
}
|
|
1687
|
+
}
|
|
1688
1688
|
|
|
1689
1689
|
const getWindowSize = () => ({
|
|
1690
1690
|
width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,
|