@folklore/hooks 0.0.27 → 0.0.28
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 +921 -840
- package/dist/es.js +920 -838
- package/package.json +4 -2
package/dist/es.js
CHANGED
|
@@ -1,40 +1,34 @@
|
|
|
1
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
1
|
import raf from 'raf';
|
|
3
2
|
import { useState, useEffect, useRef, useMemo, useCallback } from 'react';
|
|
4
|
-
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
5
3
|
import { loadDailymotion, loadYouTube, loadVimeo } from '@folklore/services';
|
|
6
4
|
import createDebug from 'debug';
|
|
7
5
|
import EventsManager from '@folklore/events';
|
|
8
|
-
import
|
|
6
|
+
import { cancelable } from 'cancelable-promise';
|
|
7
|
+
import isNumber from 'lodash/isNumber';
|
|
9
8
|
|
|
10
9
|
function useCounter(desiredValue, _ref) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
var startTime = null;
|
|
24
|
-
var duration = 0;
|
|
25
|
-
var canceled = false;
|
|
26
|
-
var startValue = currentValue;
|
|
27
|
-
var delta = desiredValue - startValue;
|
|
10
|
+
let {
|
|
11
|
+
maxDuration = 2000,
|
|
12
|
+
speed = 1 / 10
|
|
13
|
+
} = _ref;
|
|
14
|
+
const [currentValue, setCurrentValue] = useState(desiredValue);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
let animationFrame = null;
|
|
17
|
+
let startTime = null;
|
|
18
|
+
let duration = 0;
|
|
19
|
+
let canceled = false;
|
|
20
|
+
const startValue = currentValue;
|
|
21
|
+
const delta = desiredValue - startValue;
|
|
28
22
|
|
|
29
23
|
function loop() {
|
|
30
24
|
if (canceled) {
|
|
31
25
|
return;
|
|
32
26
|
}
|
|
33
27
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
const currentTime = Date.now();
|
|
29
|
+
const elapsedTime = currentTime - startTime;
|
|
30
|
+
const progress = Math.min(elapsedTime / duration, 1);
|
|
31
|
+
const newValue = Math.round(startValue + progress * delta);
|
|
38
32
|
setCurrentValue(newValue);
|
|
39
33
|
|
|
40
34
|
if (newValue !== desiredValue) {
|
|
@@ -48,7 +42,7 @@ function useCounter(desiredValue, _ref) {
|
|
|
48
42
|
animationFrame = raf(loop);
|
|
49
43
|
}
|
|
50
44
|
|
|
51
|
-
return
|
|
45
|
+
return () => {
|
|
52
46
|
canceled = true;
|
|
53
47
|
|
|
54
48
|
if (animationFrame !== null) {
|
|
@@ -60,28 +54,17 @@ function useCounter(desiredValue, _ref) {
|
|
|
60
54
|
}
|
|
61
55
|
|
|
62
56
|
function usePlayerCurrentTime(player) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return p.currentTime;
|
|
75
|
-
} : _ref$getCurrentTime;
|
|
76
|
-
|
|
77
|
-
var _useState = useState(0),
|
|
78
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
79
|
-
currentTime = _useState2[0],
|
|
80
|
-
setCurrentTime = _useState2[1];
|
|
81
|
-
|
|
82
|
-
var realCurrentTime = useRef(currentTime);
|
|
83
|
-
var lastIdRef = useRef(id);
|
|
84
|
-
var idChanged = lastIdRef.current !== id;
|
|
57
|
+
let {
|
|
58
|
+
id = null,
|
|
59
|
+
disabled = false,
|
|
60
|
+
updateInterval = 1000,
|
|
61
|
+
onUpdate: customOnUpdate = null,
|
|
62
|
+
getCurrentTime = p => p.currentTime
|
|
63
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
64
|
+
const [currentTime, setCurrentTime] = useState(0);
|
|
65
|
+
const realCurrentTime = useRef(currentTime);
|
|
66
|
+
const lastIdRef = useRef(id);
|
|
67
|
+
const idChanged = lastIdRef.current !== id;
|
|
85
68
|
|
|
86
69
|
if (idChanged) {
|
|
87
70
|
realCurrentTime.current = 0;
|
|
@@ -89,14 +72,14 @@ function usePlayerCurrentTime(player) {
|
|
|
89
72
|
} // Check time update
|
|
90
73
|
|
|
91
74
|
|
|
92
|
-
useEffect(
|
|
75
|
+
useEffect(() => {
|
|
93
76
|
if (disabled || player === null) {
|
|
94
|
-
return
|
|
77
|
+
return () => {};
|
|
95
78
|
}
|
|
96
79
|
|
|
97
|
-
|
|
80
|
+
let canceled = false;
|
|
98
81
|
|
|
99
|
-
|
|
82
|
+
const updateTime = time => {
|
|
100
83
|
if (canceled) {
|
|
101
84
|
return;
|
|
102
85
|
}
|
|
@@ -109,8 +92,8 @@ function usePlayerCurrentTime(player) {
|
|
|
109
92
|
}
|
|
110
93
|
};
|
|
111
94
|
|
|
112
|
-
|
|
113
|
-
|
|
95
|
+
const interval = setInterval(() => {
|
|
96
|
+
const time = getCurrentTime(player);
|
|
114
97
|
|
|
115
98
|
if (typeof time.then !== 'undefined') {
|
|
116
99
|
time.then(updateTime);
|
|
@@ -118,7 +101,7 @@ function usePlayerCurrentTime(player) {
|
|
|
118
101
|
updateTime(time);
|
|
119
102
|
}
|
|
120
103
|
}, updateInterval);
|
|
121
|
-
return
|
|
104
|
+
return () => {
|
|
122
105
|
canceled = true;
|
|
123
106
|
clearInterval(interval);
|
|
124
107
|
};
|
|
@@ -126,115 +109,67 @@ function usePlayerCurrentTime(player) {
|
|
|
126
109
|
return realCurrentTime.current;
|
|
127
110
|
}
|
|
128
111
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
var match = url.match(/\/video\/([^/?]+)/);
|
|
173
|
-
return match !== null ? match[1] : null;
|
|
174
|
-
} : _params$getVideoId;
|
|
175
|
-
|
|
176
|
-
var _useState = useState(typeof window.DM !== 'undefined'),
|
|
177
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
178
|
-
apiLoaded = _useState2[0],
|
|
179
|
-
setApiLoaded = _useState2[1];
|
|
180
|
-
|
|
181
|
-
var _useState3 = useState(false),
|
|
182
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
183
|
-
playerReady = _useState4[0],
|
|
184
|
-
setPlayerReady = _useState4[1];
|
|
185
|
-
|
|
186
|
-
var _useState5 = useState(false),
|
|
187
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
188
|
-
loaded = _useState6[0],
|
|
189
|
-
setLoaded = _useState6[1];
|
|
190
|
-
|
|
191
|
-
var apiRef = useRef(typeof window.DM !== 'undefined' ? window.DM : null);
|
|
192
|
-
var ready = apiLoaded && playerReady;
|
|
193
|
-
var videoId = useMemo(function () {
|
|
194
|
-
return getVideoId(id);
|
|
195
|
-
}, [id]);
|
|
196
|
-
var elementRef = useRef(null);
|
|
197
|
-
var playerRef = useRef(null);
|
|
198
|
-
var playerElementRef = useRef(elementRef.current);
|
|
199
|
-
var elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
200
|
-
|
|
201
|
-
var _useState7 = useState(initialMuted),
|
|
202
|
-
_useState8 = _slicedToArray(_useState7, 2),
|
|
203
|
-
muted = _useState8[0],
|
|
204
|
-
setMuted = _useState8[1];
|
|
205
|
-
|
|
206
|
-
var _useState9 = useState(initialMuted ? 0 : 1),
|
|
207
|
-
_useState10 = _slicedToArray(_useState9, 2),
|
|
208
|
-
volume = _useState10[0],
|
|
209
|
-
setVolumeState = _useState10[1];
|
|
210
|
-
|
|
211
|
-
var _useState11 = useState({
|
|
112
|
+
const noPlayerError$1 = new Error('No player');
|
|
113
|
+
const debug$3 = createDebug('folklore:video:dailymotion');
|
|
114
|
+
|
|
115
|
+
const useDailymotionPlayer = function () {
|
|
116
|
+
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
117
|
+
let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
118
|
+
const {
|
|
119
|
+
width = 0,
|
|
120
|
+
height = 0,
|
|
121
|
+
duration = 0,
|
|
122
|
+
autoplay = false,
|
|
123
|
+
muted: initialMuted = false,
|
|
124
|
+
start = 0,
|
|
125
|
+
controls = true,
|
|
126
|
+
queueAutoplayNext = false,
|
|
127
|
+
queueEnable = false,
|
|
128
|
+
sharingEnable = false,
|
|
129
|
+
uiLogo = false,
|
|
130
|
+
uiStartScreenInfo = true,
|
|
131
|
+
timeUpdateInterval = 1000,
|
|
132
|
+
onTimeUpdate: customOnTimeUpdate = null,
|
|
133
|
+
getVideoId = url => {
|
|
134
|
+
if (url === null || url.match(/^https?:/) === null) {
|
|
135
|
+
return url;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const match = url.match(/\/video\/([^/?]+)/);
|
|
139
|
+
return match !== null ? match[1] : null;
|
|
140
|
+
}
|
|
141
|
+
} = params;
|
|
142
|
+
const [apiLoaded, setApiLoaded] = useState(typeof window.DM !== 'undefined');
|
|
143
|
+
const [playerReady, setPlayerReady] = useState(false);
|
|
144
|
+
const [loaded, setLoaded] = useState(false);
|
|
145
|
+
const apiRef = useRef(typeof window.DM !== 'undefined' ? window.DM : null);
|
|
146
|
+
const ready = apiLoaded && playerReady;
|
|
147
|
+
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
148
|
+
const elementRef = useRef(null);
|
|
149
|
+
const playerRef = useRef(null);
|
|
150
|
+
const playerElementRef = useRef(elementRef.current);
|
|
151
|
+
const elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
152
|
+
const [muted, setMuted] = useState(initialMuted);
|
|
153
|
+
const [volume, setVolumeState] = useState(initialMuted ? 0 : 1);
|
|
154
|
+
const [playState, setPlayState] = useState({
|
|
212
155
|
playing: false,
|
|
213
156
|
paused: false,
|
|
214
157
|
ended: false,
|
|
215
158
|
buffering: false,
|
|
216
159
|
adPlaying: false
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
width: width,
|
|
224
|
-
height: height,
|
|
225
|
-
duration: duration
|
|
226
|
-
}),
|
|
227
|
-
_useState14 = _slicedToArray(_useState13, 2),
|
|
228
|
-
metadata = _useState14[0],
|
|
229
|
-
setMetadata = _useState14[1]; // Load SDK
|
|
230
|
-
|
|
160
|
+
});
|
|
161
|
+
const [metadata, setMetadata] = useState({
|
|
162
|
+
width,
|
|
163
|
+
height,
|
|
164
|
+
duration
|
|
165
|
+
}); // Load SDK
|
|
231
166
|
|
|
232
|
-
useEffect(
|
|
233
|
-
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
let canceled = false;
|
|
234
169
|
|
|
235
170
|
if (!apiLoaded && videoId !== null) {
|
|
236
171
|
debug$3('Load API');
|
|
237
|
-
loadDailymotion().then(
|
|
172
|
+
loadDailymotion().then(api => {
|
|
238
173
|
if (!canceled) {
|
|
239
174
|
apiRef.current = api;
|
|
240
175
|
setApiLoaded(true);
|
|
@@ -243,35 +178,38 @@ var useDailymotionPlayer = function useDailymotionPlayer() {
|
|
|
243
178
|
});
|
|
244
179
|
}
|
|
245
180
|
|
|
246
|
-
return
|
|
181
|
+
return () => {
|
|
247
182
|
canceled = true;
|
|
248
183
|
};
|
|
249
184
|
}, [videoId, apiLoaded, setApiLoaded]); // Create or update player
|
|
250
185
|
|
|
251
|
-
useEffect(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
const {
|
|
188
|
+
current: DM = null
|
|
189
|
+
} = apiRef;
|
|
190
|
+
const {
|
|
191
|
+
current: currentPlayer = null
|
|
192
|
+
} = playerRef;
|
|
193
|
+
const {
|
|
194
|
+
current: element = null
|
|
195
|
+
} = elementRef;
|
|
258
196
|
|
|
259
197
|
if (!apiLoaded || videoId === null || element === null) {
|
|
260
198
|
return;
|
|
261
199
|
}
|
|
262
200
|
|
|
263
|
-
|
|
201
|
+
const playerParams = {
|
|
264
202
|
'autoplay-d': autoplay,
|
|
265
203
|
// muted,
|
|
266
|
-
start
|
|
267
|
-
controls
|
|
204
|
+
start,
|
|
205
|
+
controls,
|
|
268
206
|
'queue-autoplay-next': queueAutoplayNext,
|
|
269
207
|
'queue-enable': queueEnable,
|
|
270
208
|
'sharing-enable': sharingEnable,
|
|
271
209
|
'ui-logo': uiLogo,
|
|
272
210
|
'ui-start-screen-info': uiStartScreenInfo
|
|
273
211
|
};
|
|
274
|
-
|
|
212
|
+
let player = currentPlayer;
|
|
275
213
|
|
|
276
214
|
if (player !== null) {
|
|
277
215
|
player.load(videoId, {
|
|
@@ -281,8 +219,8 @@ var useDailymotionPlayer = function useDailymotionPlayer() {
|
|
|
281
219
|
} else {
|
|
282
220
|
player = DM.player(element, {
|
|
283
221
|
video: videoId,
|
|
284
|
-
width
|
|
285
|
-
height
|
|
222
|
+
width,
|
|
223
|
+
height,
|
|
286
224
|
params: playerParams
|
|
287
225
|
});
|
|
288
226
|
debug$3('Create player [ID: %s]', videoId);
|
|
@@ -295,16 +233,17 @@ var useDailymotionPlayer = function useDailymotionPlayer() {
|
|
|
295
233
|
playerRef.current = player;
|
|
296
234
|
playerElementRef.current = element;
|
|
297
235
|
}, [apiLoaded, elementHasChanged, videoId, width, height, autoplay, muted, start, controls, queueAutoplayNext, queueEnable, sharingEnable, uiLogo, uiStartScreenInfo]);
|
|
298
|
-
useEffect(
|
|
299
|
-
|
|
300
|
-
|
|
236
|
+
useEffect(() => {
|
|
237
|
+
const {
|
|
238
|
+
current: player = null
|
|
239
|
+
} = playerRef;
|
|
301
240
|
|
|
302
241
|
if (player === null) {
|
|
303
|
-
return
|
|
242
|
+
return () => {};
|
|
304
243
|
}
|
|
305
244
|
|
|
306
|
-
|
|
307
|
-
|
|
245
|
+
let currentPlayState = playState;
|
|
246
|
+
let currentMetadata = metadata;
|
|
308
247
|
|
|
309
248
|
function onPlaybackReady() {
|
|
310
249
|
setLoaded(true);
|
|
@@ -312,17 +251,17 @@ var useDailymotionPlayer = function useDailymotionPlayer() {
|
|
|
312
251
|
}
|
|
313
252
|
|
|
314
253
|
function onLoadedMetadata() {
|
|
315
|
-
currentMetadata =
|
|
254
|
+
currentMetadata = { ...currentMetadata,
|
|
316
255
|
duration: player.duration
|
|
317
|
-
}
|
|
256
|
+
};
|
|
318
257
|
setMetadata(currentMetadata);
|
|
319
258
|
debug$3('onLoadedMetadata [ID: %s]', videoId);
|
|
320
259
|
}
|
|
321
260
|
|
|
322
261
|
function onDurationChange() {
|
|
323
|
-
currentMetadata =
|
|
262
|
+
currentMetadata = { ...currentMetadata,
|
|
324
263
|
duration: player.duration
|
|
325
|
-
}
|
|
264
|
+
};
|
|
326
265
|
setMetadata(currentMetadata);
|
|
327
266
|
debug$3('onDurationChange [ID: %s]', videoId);
|
|
328
267
|
}
|
|
@@ -334,63 +273,63 @@ var useDailymotionPlayer = function useDailymotionPlayer() {
|
|
|
334
273
|
}
|
|
335
274
|
|
|
336
275
|
function onPlay() {
|
|
337
|
-
currentPlayState =
|
|
276
|
+
currentPlayState = { ...currentPlayState,
|
|
338
277
|
playing: true,
|
|
339
278
|
paused: false,
|
|
340
279
|
ended: false
|
|
341
|
-
}
|
|
280
|
+
};
|
|
342
281
|
setPlayState(currentPlayState);
|
|
343
282
|
debug$3('onPlay [ID: %s]', videoId);
|
|
344
283
|
}
|
|
345
284
|
|
|
346
285
|
function onPause() {
|
|
347
|
-
currentPlayState =
|
|
286
|
+
currentPlayState = { ...currentPlayState,
|
|
348
287
|
playing: false,
|
|
349
288
|
paused: true,
|
|
350
289
|
ended: false
|
|
351
|
-
}
|
|
290
|
+
};
|
|
352
291
|
setPlayState(currentPlayState);
|
|
353
292
|
debug$3('onPause [ID: %s]', videoId);
|
|
354
293
|
}
|
|
355
294
|
|
|
356
295
|
function onEnd() {
|
|
357
|
-
currentPlayState =
|
|
296
|
+
currentPlayState = { ...currentPlayState,
|
|
358
297
|
playing: false,
|
|
359
298
|
paused: false,
|
|
360
299
|
ended: true
|
|
361
|
-
}
|
|
300
|
+
};
|
|
362
301
|
setPlayState(currentPlayState);
|
|
363
302
|
debug$3('onEnd [ID: %s]', videoId);
|
|
364
303
|
}
|
|
365
304
|
|
|
366
305
|
function onPlaying() {
|
|
367
|
-
currentPlayState =
|
|
306
|
+
currentPlayState = { ...currentPlayState,
|
|
368
307
|
buffering: false
|
|
369
|
-
}
|
|
308
|
+
};
|
|
370
309
|
setPlayState(currentPlayState);
|
|
371
310
|
debug$3('onPlaying [ID: %s]', videoId);
|
|
372
311
|
}
|
|
373
312
|
|
|
374
313
|
function onWaiting() {
|
|
375
|
-
currentPlayState =
|
|
314
|
+
currentPlayState = { ...currentPlayState,
|
|
376
315
|
buffering: true
|
|
377
|
-
}
|
|
316
|
+
};
|
|
378
317
|
setPlayState(currentPlayState);
|
|
379
318
|
debug$3('onWaiting [ID: %s]', videoId);
|
|
380
319
|
}
|
|
381
320
|
|
|
382
321
|
function onAdStart() {
|
|
383
|
-
currentPlayState =
|
|
322
|
+
currentPlayState = { ...currentPlayState,
|
|
384
323
|
adPlaying: true
|
|
385
|
-
}
|
|
324
|
+
};
|
|
386
325
|
setPlayState(currentPlayState);
|
|
387
326
|
debug$3('onAdStart [ID: %s]', videoId);
|
|
388
327
|
}
|
|
389
328
|
|
|
390
329
|
function onAdEnd() {
|
|
391
|
-
currentPlayState =
|
|
330
|
+
currentPlayState = { ...currentPlayState,
|
|
392
331
|
adPlaying: false
|
|
393
|
-
}
|
|
332
|
+
};
|
|
394
333
|
setPlayState(currentPlayState);
|
|
395
334
|
debug$3('onAdEnd [ID: %s]', videoId);
|
|
396
335
|
}
|
|
@@ -406,7 +345,7 @@ var useDailymotionPlayer = function useDailymotionPlayer() {
|
|
|
406
345
|
player.addEventListener('waiting', onWaiting);
|
|
407
346
|
player.addEventListener('ad_start', onAdStart);
|
|
408
347
|
player.addEventListener('ad_end', onAdEnd);
|
|
409
|
-
return
|
|
348
|
+
return () => {
|
|
410
349
|
player.removeEventListener('playback_ready', onPlaybackReady);
|
|
411
350
|
player.removeEventListener('loadedmetadata', onLoadedMetadata);
|
|
412
351
|
player.removeEventListener('durationchange', onDurationChange);
|
|
@@ -420,63 +359,79 @@ var useDailymotionPlayer = function useDailymotionPlayer() {
|
|
|
420
359
|
player.removeEventListener('ad_end', onAdEnd);
|
|
421
360
|
};
|
|
422
361
|
}, [playerRef.current, playerReady, videoId, setLoaded, setPlayState, setMetadata, setVolumeState, setMuted]);
|
|
423
|
-
|
|
424
|
-
|
|
362
|
+
const play = useCallback(() => {
|
|
363
|
+
const {
|
|
364
|
+
current: player
|
|
365
|
+
} = playerRef;
|
|
425
366
|
return player !== null ? player.play() : Promise.reject(noPlayerError$1);
|
|
426
367
|
}, []);
|
|
427
|
-
|
|
428
|
-
|
|
368
|
+
const pause = useCallback(() => {
|
|
369
|
+
const {
|
|
370
|
+
current: player
|
|
371
|
+
} = playerRef;
|
|
429
372
|
return player !== null ? player.pause() : Promise.reject(noPlayerError$1);
|
|
430
373
|
}, []);
|
|
431
|
-
|
|
432
|
-
|
|
374
|
+
const setVolume = useCallback(newVolume => {
|
|
375
|
+
const {
|
|
376
|
+
current: player
|
|
377
|
+
} = playerRef;
|
|
433
378
|
return player !== null ? player.setVolume(newVolume) : Promise.reject(noPlayerError$1);
|
|
434
379
|
}, []);
|
|
435
|
-
|
|
436
|
-
|
|
380
|
+
const mute = useCallback(() => {
|
|
381
|
+
const {
|
|
382
|
+
current: player
|
|
383
|
+
} = playerRef;
|
|
437
384
|
return player !== null ? player.setMute(true) : Promise.reject(noPlayerError$1);
|
|
438
385
|
}, []);
|
|
439
|
-
|
|
440
|
-
|
|
386
|
+
const unmute = useCallback(() => {
|
|
387
|
+
const {
|
|
388
|
+
current: player
|
|
389
|
+
} = playerRef;
|
|
441
390
|
return player !== null ? player.setMute(false) : Promise.reject(noPlayerError$1);
|
|
442
391
|
}, []);
|
|
443
|
-
|
|
444
|
-
|
|
392
|
+
const seek = useCallback(time => {
|
|
393
|
+
const {
|
|
394
|
+
current: player
|
|
395
|
+
} = playerRef;
|
|
445
396
|
return player !== null ? player.seek(time) : Promise.reject(noPlayerError$1);
|
|
446
397
|
}, []);
|
|
447
|
-
|
|
448
|
-
|
|
398
|
+
const {
|
|
399
|
+
playing
|
|
400
|
+
} = playState;
|
|
401
|
+
const currentTime = usePlayerCurrentTime(playerRef.current, {
|
|
449
402
|
id: videoId,
|
|
450
403
|
disabled: !playing || timeUpdateInterval === null,
|
|
451
404
|
updateInterval: timeUpdateInterval,
|
|
452
405
|
onUpdate: customOnTimeUpdate
|
|
453
406
|
});
|
|
454
|
-
return
|
|
407
|
+
return {
|
|
455
408
|
ref: elementRef,
|
|
456
409
|
player: playerRef.current,
|
|
457
|
-
ready
|
|
458
|
-
play
|
|
459
|
-
pause
|
|
460
|
-
mute
|
|
461
|
-
unmute
|
|
462
|
-
setVolume
|
|
463
|
-
seek
|
|
464
|
-
currentTime
|
|
465
|
-
loaded
|
|
466
|
-
muted
|
|
467
|
-
volume
|
|
468
|
-
|
|
410
|
+
ready,
|
|
411
|
+
play,
|
|
412
|
+
pause,
|
|
413
|
+
mute,
|
|
414
|
+
unmute,
|
|
415
|
+
setVolume,
|
|
416
|
+
seek,
|
|
417
|
+
currentTime,
|
|
418
|
+
loaded,
|
|
419
|
+
muted,
|
|
420
|
+
volume,
|
|
421
|
+
...metadata,
|
|
422
|
+
...playState
|
|
423
|
+
};
|
|
469
424
|
};
|
|
470
425
|
|
|
471
|
-
|
|
426
|
+
const eventsManager$1 = typeof document !== 'undefined' ? new EventsManager(document) : null;
|
|
472
427
|
|
|
473
|
-
|
|
474
|
-
useEffect(
|
|
428
|
+
const useDocumentEvent = (event, callback) => {
|
|
429
|
+
useEffect(() => {
|
|
475
430
|
if (eventsManager$1 !== null && callback !== null) {
|
|
476
431
|
eventsManager$1.subscribe(event, callback);
|
|
477
432
|
}
|
|
478
433
|
|
|
479
|
-
return
|
|
434
|
+
return () => {
|
|
480
435
|
if (eventsManager$1 !== null && callback !== null) {
|
|
481
436
|
eventsManager$1.unsubscribe(event, callback);
|
|
482
437
|
}
|
|
@@ -484,15 +439,15 @@ var useDocumentEvent = function useDocumentEvent(event, callback) {
|
|
|
484
439
|
}, [event, callback]);
|
|
485
440
|
};
|
|
486
441
|
|
|
487
|
-
|
|
442
|
+
const eventsManager = typeof window !== 'undefined' ? new EventsManager(window) : null;
|
|
488
443
|
|
|
489
|
-
|
|
490
|
-
useEffect(
|
|
444
|
+
const useWindowEvent = (event, callback) => {
|
|
445
|
+
useEffect(() => {
|
|
491
446
|
if (eventsManager !== null && callback !== null) {
|
|
492
447
|
eventsManager.subscribe(event, callback);
|
|
493
448
|
}
|
|
494
449
|
|
|
495
|
-
return
|
|
450
|
+
return () => {
|
|
496
451
|
if (eventsManager !== null && callback !== null) {
|
|
497
452
|
eventsManager.unsubscribe(event, callback);
|
|
498
453
|
}
|
|
@@ -501,10 +456,12 @@ var useWindowEvent = function useWindowEvent(event, callback) {
|
|
|
501
456
|
};
|
|
502
457
|
|
|
503
458
|
function useKeyboard() {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
459
|
+
let keyMap = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
460
|
+
const onKeyDown = useCallback(event => {
|
|
461
|
+
const {
|
|
462
|
+
key
|
|
463
|
+
} = event;
|
|
464
|
+
let callback = null;
|
|
508
465
|
|
|
509
466
|
if (typeof keyMap === 'function') {
|
|
510
467
|
callback = keyMap;
|
|
@@ -516,9 +473,11 @@ function useKeyboard() {
|
|
|
516
473
|
callback(event);
|
|
517
474
|
}
|
|
518
475
|
}, [keyMap]);
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
476
|
+
const onKeyUp = useCallback(event => {
|
|
477
|
+
const {
|
|
478
|
+
key
|
|
479
|
+
} = event;
|
|
480
|
+
let callback = null;
|
|
522
481
|
|
|
523
482
|
if (typeof keyMap === 'function') {
|
|
524
483
|
callback = keyMap;
|
|
@@ -534,68 +493,274 @@ function useKeyboard() {
|
|
|
534
493
|
useWindowEvent('keyup', keyMap !== null ? onKeyUp : null);
|
|
535
494
|
}
|
|
536
495
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
496
|
+
const useItemsPaginated = function (loader) {
|
|
497
|
+
let {
|
|
498
|
+
page = null,
|
|
499
|
+
count = 10,
|
|
500
|
+
pages: initialPages = null,
|
|
501
|
+
getPageFromResponse = _ref2 => {
|
|
502
|
+
let {
|
|
503
|
+
pagination: {
|
|
504
|
+
page: currentPage,
|
|
505
|
+
last_page: lastPage,
|
|
506
|
+
total
|
|
507
|
+
},
|
|
508
|
+
data: items
|
|
509
|
+
} = _ref2;
|
|
510
|
+
return {
|
|
511
|
+
page: parseInt(currentPage, 10),
|
|
512
|
+
lastPage: parseInt(lastPage, 10),
|
|
513
|
+
total: parseInt(total, 10),
|
|
514
|
+
items
|
|
515
|
+
};
|
|
516
|
+
},
|
|
517
|
+
onLoaded = null,
|
|
518
|
+
onError = null,
|
|
519
|
+
query = null
|
|
520
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
521
|
+
const lastState = useRef(null);
|
|
522
|
+
const initialState = useMemo(() => {
|
|
523
|
+
const finalInitialPages = initialPages !== null ? initialPages.map(it => getPageFromResponse(it)) : null;
|
|
524
|
+
const finalLastPage = finalInitialPages !== null ? finalInitialPages.reduce((currentLastPage, _ref3) => {
|
|
525
|
+
let {
|
|
526
|
+
lastPage: initialLastPage
|
|
527
|
+
} = _ref3;
|
|
528
|
+
return initialLastPage > currentLastPage ? initialLastPage : currentLastPage;
|
|
529
|
+
}, -1) : -1;
|
|
530
|
+
return {
|
|
531
|
+
lastPage: finalLastPage,
|
|
532
|
+
total: finalInitialPages !== null ? finalInitialPages[0].total : 0,
|
|
533
|
+
loaded: finalLastPage !== -1 && finalInitialPages !== null && finalInitialPages.length === finalLastPage,
|
|
534
|
+
loading: false,
|
|
535
|
+
pages: finalInitialPages !== null ? finalInitialPages : null
|
|
536
|
+
};
|
|
537
|
+
}, [initialPages]);
|
|
538
|
+
const currentPagesRef = useRef(initialState.pages);
|
|
539
|
+
const [state, setState] = useState(initialState);
|
|
540
|
+
const {
|
|
541
|
+
lastPage,
|
|
542
|
+
loaded,
|
|
543
|
+
loading,
|
|
544
|
+
pages,
|
|
545
|
+
total
|
|
546
|
+
} = state;
|
|
547
|
+
const items = pages !== null ? pages.reduce((pagesItems, _ref4) => {
|
|
548
|
+
let {
|
|
549
|
+
items: pageItems
|
|
550
|
+
} = _ref4;
|
|
551
|
+
return pagesItems.concat(pageItems);
|
|
552
|
+
}, []) : null;
|
|
553
|
+
|
|
554
|
+
const updateState = update => setState({ ...state,
|
|
555
|
+
...update
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
const updateFromResponse = function (response) {
|
|
559
|
+
let error = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
560
|
+
let reset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
561
|
+
|
|
562
|
+
if (error !== null) {
|
|
563
|
+
updateState({
|
|
564
|
+
loaded: false,
|
|
565
|
+
loading: false
|
|
566
|
+
});
|
|
567
|
+
throw error;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const newPage = getPageFromResponse(response);
|
|
571
|
+
const currentPages = currentPagesRef.current || [];
|
|
572
|
+
const newPages = (reset ? [newPage] : [...currentPages.filter(it => it.page !== newPage.page), newPage]).sort((a, b) => {
|
|
573
|
+
const {
|
|
574
|
+
page: aPage = null
|
|
575
|
+
} = a;
|
|
576
|
+
const {
|
|
577
|
+
page: bPage = null
|
|
578
|
+
} = b;
|
|
579
|
+
const hasObject = aPage !== null && bPage !== null;
|
|
580
|
+
|
|
581
|
+
if (hasObject) {
|
|
582
|
+
if (aPage === bPage) {
|
|
583
|
+
return 0;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return aPage > bPage ? 1 : -1;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (isNumber(a) && isNumber(b)) {
|
|
590
|
+
if (a === b) {
|
|
591
|
+
return 0;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
return a > b ? 1 : -1;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
return 0;
|
|
598
|
+
});
|
|
599
|
+
currentPagesRef.current = newPages;
|
|
600
|
+
updateState({
|
|
601
|
+
loaded: true,
|
|
602
|
+
loading: false,
|
|
603
|
+
lastPage: newPage.lastPage,
|
|
604
|
+
total: newPage.total,
|
|
605
|
+
pages: newPages
|
|
606
|
+
});
|
|
607
|
+
return newPage;
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
const getNextPage = () => {
|
|
611
|
+
const allPages = lastPage !== -1 ? Array.call(null, ...Array(lastPage)).map((it, index) => index + 1) : [];
|
|
612
|
+
const currentPages = currentPagesRef.current || [];
|
|
613
|
+
const remainingPages = allPages.filter(pageNumber => currentPages.findIndex(it => it.page === pageNumber) === -1);
|
|
614
|
+
const firstItem = remainingPages.length > 0 ? remainingPages.shift() : null;
|
|
615
|
+
return firstItem !== null ? firstItem : null;
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
const loadItems = requestPage => {
|
|
619
|
+
updateState({
|
|
620
|
+
loading: true
|
|
621
|
+
});
|
|
622
|
+
return cancelable(loader(requestPage, count)).then(response => updateFromResponse(response)).catch(error => updateFromResponse(null, error)).then(response => {
|
|
623
|
+
if (onLoaded !== null) {
|
|
624
|
+
onLoaded(response);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
return response;
|
|
628
|
+
}).catch(error => {
|
|
629
|
+
if (onError !== null) {
|
|
630
|
+
onError(error);
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
const loadPage = pageToLoad => {
|
|
636
|
+
if (loading) {
|
|
637
|
+
return Promise.reject();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const currentPages = currentPagesRef.current || [];
|
|
641
|
+
const foundPage = currentPages.find(it => it.page === pageToLoad) || null;
|
|
642
|
+
|
|
643
|
+
if (foundPage !== null) {
|
|
644
|
+
return Promise.resolve(foundPage);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return loadItems(pageToLoad);
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
const loadNextPage = () => {
|
|
651
|
+
if (loading) {
|
|
652
|
+
return Promise.reject();
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
const nextPage = getNextPage();
|
|
656
|
+
return nextPage !== null ? loadItems(nextPage) : Promise.resolve();
|
|
657
|
+
}; // Reset all on query change
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
useEffect(() => {
|
|
661
|
+
const hadState = lastState.current !== null;
|
|
662
|
+
|
|
663
|
+
if (hadState) {
|
|
664
|
+
currentPagesRef.current = null;
|
|
665
|
+
updateState({
|
|
666
|
+
loaded: true,
|
|
667
|
+
loading: false,
|
|
668
|
+
lastPage: null,
|
|
669
|
+
total: 0,
|
|
670
|
+
pages: []
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
}, [query]);
|
|
674
|
+
useEffect(() => {
|
|
675
|
+
const hadState = lastState.current !== null;
|
|
676
|
+
lastState.current = initialState;
|
|
677
|
+
|
|
678
|
+
if (hadState) {
|
|
679
|
+
currentPagesRef.current = initialState.pages;
|
|
680
|
+
setState(initialState);
|
|
681
|
+
}
|
|
682
|
+
}, [initialState]);
|
|
683
|
+
useEffect(() => {
|
|
684
|
+
if (loader === null) {
|
|
685
|
+
return () => {};
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
let loadPromise = null;
|
|
689
|
+
const pageToLoad = initialPages === null && page === null ? 1 : page;
|
|
690
|
+
|
|
691
|
+
if (pageToLoad !== null) {
|
|
692
|
+
loadPromise = loadItems(pageToLoad);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
return () => {
|
|
696
|
+
if (loadPromise !== null) {
|
|
697
|
+
loadPromise.cancel();
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
}, [loader, page]);
|
|
701
|
+
const currentPage = pages !== null ? pages.find(_ref5 => {
|
|
702
|
+
let {
|
|
703
|
+
page: pageNumber
|
|
704
|
+
} = _ref5;
|
|
705
|
+
return parseInt(pageNumber, 10) === parseInt(page, 10);
|
|
706
|
+
}) || null : null;
|
|
707
|
+
return {
|
|
708
|
+
items,
|
|
709
|
+
pages,
|
|
710
|
+
currentPage,
|
|
711
|
+
pageItems: currentPage !== null ? currentPage.items : null,
|
|
712
|
+
total,
|
|
713
|
+
lastPage,
|
|
714
|
+
loaded,
|
|
715
|
+
allLoaded: lastPage !== -1 && pages.length === lastPage,
|
|
716
|
+
loading,
|
|
717
|
+
loadNextPage,
|
|
718
|
+
loadPage
|
|
719
|
+
};
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
const debug$2 = createDebug('folklore:video:native');
|
|
723
|
+
const noPlayerError = new Error('No player');
|
|
724
|
+
|
|
725
|
+
const useNativeVideoPlayer = function (url) {
|
|
726
|
+
let {
|
|
727
|
+
width = 0,
|
|
728
|
+
height = 0,
|
|
729
|
+
duration = 0,
|
|
730
|
+
initialMuted = false,
|
|
731
|
+
timeUpdateInterval = 1000,
|
|
732
|
+
onTimeUpdate: customOnTimeUpdate = null
|
|
733
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
734
|
+
const elementRef = useRef(null);
|
|
735
|
+
const [loaded, setLoaded] = useState(false);
|
|
736
|
+
const [volume, setVolumeState] = useState(initialMuted ? 0 : 1);
|
|
737
|
+
const [playState, setPlayState] = useState({
|
|
571
738
|
playing: false,
|
|
572
739
|
paused: false,
|
|
573
740
|
ended: false,
|
|
574
741
|
buffering: false
|
|
575
|
-
})
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
_useState8 = _slicedToArray(_useState7, 2),
|
|
586
|
-
metadata = _useState8[0],
|
|
587
|
-
setMetadata = _useState8[1];
|
|
588
|
-
|
|
589
|
-
var play = useCallback(function () {
|
|
590
|
-
var player = elementRef.current;
|
|
742
|
+
});
|
|
743
|
+
const [metadata, setMetadata] = useState({
|
|
744
|
+
width,
|
|
745
|
+
height,
|
|
746
|
+
duration
|
|
747
|
+
});
|
|
748
|
+
const play = useCallback(() => {
|
|
749
|
+
const {
|
|
750
|
+
current: player
|
|
751
|
+
} = elementRef;
|
|
591
752
|
return player !== null ? player.play() : Promise.reject(noPlayerError);
|
|
592
753
|
}, []);
|
|
593
|
-
|
|
594
|
-
|
|
754
|
+
const pause = useCallback(() => {
|
|
755
|
+
const {
|
|
756
|
+
current: player
|
|
757
|
+
} = elementRef;
|
|
595
758
|
return player !== null ? player.pause() : Promise.reject(noPlayerError);
|
|
596
759
|
}, []);
|
|
597
|
-
|
|
598
|
-
|
|
760
|
+
const setVolume = useCallback(newVolume => {
|
|
761
|
+
const {
|
|
762
|
+
current: player
|
|
763
|
+
} = elementRef;
|
|
599
764
|
|
|
600
765
|
if (player !== null) {
|
|
601
766
|
player.volume = newVolume;
|
|
@@ -604,8 +769,10 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
604
769
|
|
|
605
770
|
return Promise.reject(noPlayerError);
|
|
606
771
|
}, []);
|
|
607
|
-
|
|
608
|
-
|
|
772
|
+
const mute = useCallback(() => {
|
|
773
|
+
const {
|
|
774
|
+
current: player
|
|
775
|
+
} = elementRef;
|
|
609
776
|
|
|
610
777
|
if (player !== null) {
|
|
611
778
|
player.muted = true;
|
|
@@ -614,8 +781,10 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
614
781
|
|
|
615
782
|
return Promise.reject(noPlayerError);
|
|
616
783
|
}, []);
|
|
617
|
-
|
|
618
|
-
|
|
784
|
+
const unmute = useCallback(() => {
|
|
785
|
+
const {
|
|
786
|
+
current: player
|
|
787
|
+
} = elementRef;
|
|
619
788
|
|
|
620
789
|
if (player !== null) {
|
|
621
790
|
player.muted = false;
|
|
@@ -624,8 +793,10 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
624
793
|
|
|
625
794
|
return Promise.reject(noPlayerError);
|
|
626
795
|
}, []);
|
|
627
|
-
|
|
628
|
-
|
|
796
|
+
const seek = useCallback(newTime => {
|
|
797
|
+
const {
|
|
798
|
+
current: player
|
|
799
|
+
} = elementRef;
|
|
629
800
|
|
|
630
801
|
if (player !== null) {
|
|
631
802
|
player.currentTime = newTime;
|
|
@@ -634,8 +805,10 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
634
805
|
|
|
635
806
|
return Promise.reject(noPlayerError);
|
|
636
807
|
}, []);
|
|
637
|
-
|
|
638
|
-
|
|
808
|
+
const setLoop = useCallback(newLoop => {
|
|
809
|
+
const {
|
|
810
|
+
current: player
|
|
811
|
+
} = elementRef;
|
|
639
812
|
|
|
640
813
|
if (player !== null) {
|
|
641
814
|
player.loop = newLoop;
|
|
@@ -645,20 +818,22 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
645
818
|
return Promise.reject(noPlayerError);
|
|
646
819
|
}, []); // Bind player events
|
|
647
820
|
|
|
648
|
-
useEffect(
|
|
649
|
-
|
|
821
|
+
useEffect(() => {
|
|
822
|
+
const {
|
|
823
|
+
current: player
|
|
824
|
+
} = elementRef;
|
|
650
825
|
|
|
651
826
|
if (player === null) {
|
|
652
|
-
return
|
|
827
|
+
return () => {};
|
|
653
828
|
}
|
|
654
829
|
|
|
655
|
-
|
|
830
|
+
const onCanPlay = () => {
|
|
656
831
|
setLoaded(true);
|
|
657
832
|
debug$2('onCanPlay [URL: %s]', url);
|
|
658
833
|
};
|
|
659
834
|
|
|
660
|
-
|
|
661
|
-
|
|
835
|
+
const onMetadataLoaded = () => {
|
|
836
|
+
const newMetadata = {
|
|
662
837
|
duration: player.duration,
|
|
663
838
|
width: player.videoWidth,
|
|
664
839
|
height: player.videoHeight
|
|
@@ -667,7 +842,7 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
667
842
|
debug$2('onMetadataLoaded [URL: %s]', url);
|
|
668
843
|
};
|
|
669
844
|
|
|
670
|
-
|
|
845
|
+
const onPlay = () => {
|
|
671
846
|
setPlayState({
|
|
672
847
|
playing: true,
|
|
673
848
|
paused: false,
|
|
@@ -677,7 +852,7 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
677
852
|
debug$2('onPlay [URL: %s]', url);
|
|
678
853
|
};
|
|
679
854
|
|
|
680
|
-
|
|
855
|
+
const onPause = () => {
|
|
681
856
|
setPlayState({
|
|
682
857
|
playing: false,
|
|
683
858
|
paused: true,
|
|
@@ -687,7 +862,7 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
687
862
|
debug$2('onPause [URL: %s]', url);
|
|
688
863
|
};
|
|
689
864
|
|
|
690
|
-
|
|
865
|
+
const onEnded = () => {
|
|
691
866
|
setPlayState({
|
|
692
867
|
playing: false,
|
|
693
868
|
paused: false,
|
|
@@ -703,7 +878,7 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
703
878
|
player.addEventListener('play', onPlay);
|
|
704
879
|
player.addEventListener('pause', onPause);
|
|
705
880
|
player.addEventListener('ended', onEnded);
|
|
706
|
-
return
|
|
881
|
+
return () => {
|
|
707
882
|
debug$2('Unbind events [URL: %s]', url);
|
|
708
883
|
player.removeEventListener('canplay', onCanPlay);
|
|
709
884
|
player.removeEventListener('metadataloaded', onMetadataLoaded);
|
|
@@ -712,90 +887,79 @@ var useNativeVideoPlayer = function useNativeVideoPlayer(url) {
|
|
|
712
887
|
player.removeEventListener('ended', onEnded);
|
|
713
888
|
};
|
|
714
889
|
}, [url, elementRef.current, setPlayState, setMetadata, setVolumeState, setLoaded]);
|
|
715
|
-
|
|
716
|
-
|
|
890
|
+
const {
|
|
891
|
+
playing
|
|
892
|
+
} = playState;
|
|
893
|
+
const currentTime = usePlayerCurrentTime(elementRef.current, {
|
|
717
894
|
id: url,
|
|
718
895
|
disabled: !playing || timeUpdateInterval === null,
|
|
719
896
|
updateInterval: timeUpdateInterval,
|
|
720
897
|
onUpdate: customOnTimeUpdate
|
|
721
898
|
});
|
|
722
|
-
return
|
|
899
|
+
return {
|
|
723
900
|
ref: elementRef,
|
|
724
901
|
player: elementRef.current,
|
|
725
|
-
play
|
|
726
|
-
pause
|
|
727
|
-
mute
|
|
728
|
-
unmute
|
|
729
|
-
setVolume
|
|
730
|
-
seek
|
|
731
|
-
setLoop
|
|
902
|
+
play,
|
|
903
|
+
pause,
|
|
904
|
+
mute,
|
|
905
|
+
unmute,
|
|
906
|
+
setVolume,
|
|
907
|
+
seek,
|
|
908
|
+
setLoop,
|
|
732
909
|
ready: true,
|
|
733
|
-
currentTime
|
|
734
|
-
loaded
|
|
910
|
+
currentTime,
|
|
911
|
+
loaded,
|
|
735
912
|
muted: volume === 0,
|
|
736
|
-
volume
|
|
737
|
-
|
|
913
|
+
volume,
|
|
914
|
+
...metadata,
|
|
915
|
+
...playState
|
|
916
|
+
};
|
|
738
917
|
};
|
|
739
918
|
|
|
740
|
-
|
|
919
|
+
const observersCache = new Map();
|
|
741
920
|
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
rootMargin = _ref.rootMargin,
|
|
749
|
-
_ref$threshold = _ref.threshold,
|
|
750
|
-
threshold = _ref$threshold === void 0 ? null : _ref$threshold;
|
|
921
|
+
const getOptionsKey = _ref => {
|
|
922
|
+
let {
|
|
923
|
+
root = null,
|
|
924
|
+
rootMargin,
|
|
925
|
+
threshold = null
|
|
926
|
+
} = _ref;
|
|
751
927
|
return "root_".concat(root, "_rootMargin_").concat(rootMargin || null, "_threshold_").concat(threshold);
|
|
752
928
|
};
|
|
753
929
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
930
|
+
const createObserver = function (Observer) {
|
|
931
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
932
|
+
let subscribers = [];
|
|
757
933
|
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
return it.element === element;
|
|
761
|
-
}) || null;
|
|
934
|
+
const addSubscriber = (element, callback) => {
|
|
935
|
+
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
762
936
|
|
|
763
937
|
if (currentSubscriber !== null) {
|
|
764
|
-
return subscribers.map(
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
}) : it;
|
|
768
|
-
}).filter(function (it) {
|
|
769
|
-
return it.callbacks.length > 0;
|
|
770
|
-
});
|
|
938
|
+
return subscribers.map(it => it.element === element && it.callbacks.indexOf(callback) === -1 ? { ...it,
|
|
939
|
+
callbacks: [...it.callbacks, callback]
|
|
940
|
+
} : it).filter(it => it.callbacks.length > 0);
|
|
771
941
|
}
|
|
772
942
|
|
|
773
|
-
return [
|
|
774
|
-
element
|
|
943
|
+
return [...subscribers, {
|
|
944
|
+
element,
|
|
775
945
|
callbacks: [callback]
|
|
776
|
-
}]
|
|
946
|
+
}];
|
|
777
947
|
};
|
|
778
948
|
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
callbacks: it.callbacks.filter(function (subCallback) {
|
|
783
|
-
return subCallback !== callback;
|
|
784
|
-
})
|
|
785
|
-
}) : it;
|
|
786
|
-
}).filter(function (it) {
|
|
787
|
-
return it.callbacks.length > 0;
|
|
788
|
-
});
|
|
789
|
-
};
|
|
949
|
+
const removeSubscriber = (element, callback) => subscribers.map(it => it.element === element ? { ...it,
|
|
950
|
+
callbacks: it.callbacks.filter(subCallback => subCallback !== callback)
|
|
951
|
+
} : it).filter(it => it.callbacks.length > 0);
|
|
790
952
|
|
|
791
|
-
|
|
792
|
-
entries.forEach(
|
|
793
|
-
subscribers.forEach(
|
|
794
|
-
|
|
795
|
-
|
|
953
|
+
const onUpdate = entries => {
|
|
954
|
+
entries.forEach(entry => {
|
|
955
|
+
subscribers.forEach(_ref2 => {
|
|
956
|
+
let {
|
|
957
|
+
element,
|
|
958
|
+
callbacks
|
|
959
|
+
} = _ref2;
|
|
796
960
|
|
|
797
961
|
if (element === entry.target) {
|
|
798
|
-
callbacks.forEach(
|
|
962
|
+
callbacks.forEach(callback => {
|
|
799
963
|
callback(entry);
|
|
800
964
|
});
|
|
801
965
|
}
|
|
@@ -803,33 +967,29 @@ var createObserver = function createObserver(Observer) {
|
|
|
803
967
|
});
|
|
804
968
|
};
|
|
805
969
|
|
|
806
|
-
|
|
970
|
+
const observer = new Observer(onUpdate, options);
|
|
807
971
|
|
|
808
|
-
|
|
809
|
-
|
|
972
|
+
const unsubscribe = function (element) {
|
|
973
|
+
let callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
810
974
|
subscribers = removeSubscriber(element, callback);
|
|
811
975
|
|
|
812
976
|
if (typeof observer.unobserve === 'undefined') {
|
|
813
977
|
observer.disconnect();
|
|
814
|
-
subscribers.forEach(
|
|
978
|
+
subscribers.forEach(subscriber => {
|
|
815
979
|
observer.observe(subscriber.element);
|
|
816
980
|
});
|
|
817
981
|
return;
|
|
818
982
|
}
|
|
819
983
|
|
|
820
|
-
|
|
821
|
-
return it.element === element;
|
|
822
|
-
}) || null;
|
|
984
|
+
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
823
985
|
|
|
824
986
|
if (currentSubscriber === null) {
|
|
825
987
|
observer.unobserve(element);
|
|
826
988
|
}
|
|
827
989
|
};
|
|
828
990
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
return it.element === element;
|
|
832
|
-
}) || null;
|
|
991
|
+
const subscribe = (element, callback) => {
|
|
992
|
+
const currentSubscriber = subscribers.find(it => it.element === element) || null;
|
|
833
993
|
subscribers = addSubscriber(element, callback);
|
|
834
994
|
|
|
835
995
|
if (currentSubscriber === null) {
|
|
@@ -838,21 +998,21 @@ var createObserver = function createObserver(Observer) {
|
|
|
838
998
|
};
|
|
839
999
|
|
|
840
1000
|
return {
|
|
841
|
-
subscribe
|
|
842
|
-
unsubscribe
|
|
843
|
-
observer
|
|
1001
|
+
subscribe,
|
|
1002
|
+
unsubscribe,
|
|
1003
|
+
observer
|
|
844
1004
|
};
|
|
845
1005
|
};
|
|
846
1006
|
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
1007
|
+
const getObserver = function (Observer) {
|
|
1008
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1009
|
+
const observerKey = getOptionsKey(options);
|
|
850
1010
|
|
|
851
1011
|
if (!observersCache.has(Observer)) {
|
|
852
1012
|
observersCache.set(Observer, {});
|
|
853
1013
|
}
|
|
854
1014
|
|
|
855
|
-
|
|
1015
|
+
const observers = observersCache.get(Observer);
|
|
856
1016
|
|
|
857
1017
|
if (typeof observers[observerKey] === 'undefined') {
|
|
858
1018
|
observers[observerKey] = createObserver(Observer, options);
|
|
@@ -861,37 +1021,30 @@ var getObserver = function getObserver(Observer) {
|
|
|
861
1021
|
|
|
862
1022
|
return observers[observerKey];
|
|
863
1023
|
};
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
var nodeElement = nodeRef.current;
|
|
886
|
-
|
|
887
|
-
var callback = function callback(newEntry) {
|
|
888
|
-
return setEntry(newEntry);
|
|
889
|
-
};
|
|
890
|
-
|
|
891
|
-
var unsubscribe = null;
|
|
1024
|
+
const useObserver = function (Observer) {
|
|
1025
|
+
let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1026
|
+
let initialEntry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1027
|
+
const {
|
|
1028
|
+
root = null,
|
|
1029
|
+
rootMargin = null,
|
|
1030
|
+
threshold = null,
|
|
1031
|
+
disabled = false
|
|
1032
|
+
} = opts;
|
|
1033
|
+
const [entry, setEntry] = useState(initialEntry);
|
|
1034
|
+
const nodeRef = useRef(null);
|
|
1035
|
+
const currentElement = useRef(null);
|
|
1036
|
+
const elementChanged = nodeRef.current !== currentElement.current;
|
|
1037
|
+
useEffect(() => {
|
|
1038
|
+
const {
|
|
1039
|
+
current: nodeElement
|
|
1040
|
+
} = nodeRef;
|
|
1041
|
+
|
|
1042
|
+
const callback = newEntry => setEntry(newEntry);
|
|
1043
|
+
|
|
1044
|
+
let unsubscribe = null;
|
|
892
1045
|
|
|
893
1046
|
if (nodeElement !== null) {
|
|
894
|
-
|
|
1047
|
+
const newOpts = {};
|
|
895
1048
|
|
|
896
1049
|
if (root !== null) {
|
|
897
1050
|
newOpts.root = root;
|
|
@@ -905,16 +1058,16 @@ var useObserver = function useObserver(Observer) {
|
|
|
905
1058
|
newOpts.threshold = threshold;
|
|
906
1059
|
}
|
|
907
1060
|
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
1061
|
+
const {
|
|
1062
|
+
subscribe,
|
|
1063
|
+
unsubscribe: localUnsubscribe
|
|
1064
|
+
} = getObserver(Observer, newOpts);
|
|
912
1065
|
unsubscribe = localUnsubscribe;
|
|
913
1066
|
subscribe(nodeElement, callback);
|
|
914
1067
|
}
|
|
915
1068
|
|
|
916
1069
|
currentElement.current = nodeElement;
|
|
917
|
-
return
|
|
1070
|
+
return () => {
|
|
918
1071
|
if (unsubscribe !== null) {
|
|
919
1072
|
unsubscribe(nodeElement, callback);
|
|
920
1073
|
}
|
|
@@ -922,14 +1075,14 @@ var useObserver = function useObserver(Observer) {
|
|
|
922
1075
|
}, [Observer, elementChanged, disabled, root, rootMargin, threshold]);
|
|
923
1076
|
return {
|
|
924
1077
|
ref: nodeRef,
|
|
925
|
-
entry
|
|
1078
|
+
entry
|
|
926
1079
|
};
|
|
927
1080
|
};
|
|
928
1081
|
/**
|
|
929
1082
|
* Intersection Observer
|
|
930
1083
|
*/
|
|
931
1084
|
|
|
932
|
-
|
|
1085
|
+
const intersectionObserverInitialEntry = {
|
|
933
1086
|
target: null,
|
|
934
1087
|
time: null,
|
|
935
1088
|
isVisible: false,
|
|
@@ -939,130 +1092,89 @@ var intersectionObserverInitialEntry = {
|
|
|
939
1092
|
boundingClientRect: null,
|
|
940
1093
|
rootBounds: null
|
|
941
1094
|
};
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
threshold = _ref3$threshold === void 0 ? [0, 1.0] : _ref3$threshold,
|
|
950
|
-
_ref3$disabled = _ref3.disabled,
|
|
951
|
-
disabled = _ref3$disabled === void 0 ? false : _ref3$disabled;
|
|
952
|
-
|
|
1095
|
+
const useIntersectionObserver = function () {
|
|
1096
|
+
let {
|
|
1097
|
+
root = null,
|
|
1098
|
+
rootMargin = '0px',
|
|
1099
|
+
threshold = [0, 1.0],
|
|
1100
|
+
disabled = false
|
|
1101
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
953
1102
|
return useObserver(IntersectionObserver, {
|
|
954
|
-
root
|
|
955
|
-
rootMargin
|
|
956
|
-
threshold
|
|
957
|
-
disabled
|
|
1103
|
+
root,
|
|
1104
|
+
rootMargin,
|
|
1105
|
+
threshold,
|
|
1106
|
+
disabled
|
|
958
1107
|
}, intersectionObserverInitialEntry);
|
|
959
1108
|
};
|
|
960
1109
|
/**
|
|
961
1110
|
* Resize Observer
|
|
962
1111
|
*/
|
|
963
1112
|
|
|
964
|
-
|
|
1113
|
+
const resizeObserverInitialEntry = {
|
|
965
1114
|
target: null,
|
|
966
1115
|
contentRect: null,
|
|
967
1116
|
contentBoxSize: null,
|
|
968
1117
|
borderBoxSize: null
|
|
969
1118
|
};
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
1119
|
+
const useResizeObserver = function () {
|
|
1120
|
+
let {
|
|
1121
|
+
disabled = false
|
|
1122
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
975
1123
|
return useObserver(ResizeObserver, {
|
|
976
|
-
disabled
|
|
1124
|
+
disabled
|
|
977
1125
|
}, resizeObserverInitialEntry);
|
|
978
1126
|
};
|
|
979
1127
|
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$1(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
983
|
-
var NO_PLAYER_ERROR$1 = new Error('No player');
|
|
984
|
-
var debug$1 = createDebug('folklore:video:youtube');
|
|
1128
|
+
const NO_PLAYER_ERROR$1 = new Error('No player');
|
|
1129
|
+
const debug$1 = createDebug('folklore:video:youtube');
|
|
985
1130
|
|
|
986
1131
|
function useYouTubePlayer(id) {
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
initialMuted = _ref$muted === void 0 ? false : _ref$muted,
|
|
1002
|
-
_ref$onVolumeChange = _ref.onVolumeChange,
|
|
1003
|
-
customOnVolumeChange = _ref$onVolumeChange === void 0 ? null : _ref$onVolumeChange,
|
|
1004
|
-
_ref$onTimeUpdate = _ref.onTimeUpdate,
|
|
1005
|
-
customOnTimeUpdate = _ref$onTimeUpdate === void 0 ? null : _ref$onTimeUpdate,
|
|
1006
|
-
_ref$getVideoId = _ref.getVideoId,
|
|
1007
|
-
getVideoId = _ref$getVideoId === void 0 ? function (url) {
|
|
1008
|
-
if (url === null || url.match(/^https?:/) === null) {
|
|
1009
|
-
return url;
|
|
1010
|
-
}
|
|
1011
|
-
|
|
1012
|
-
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
|
|
1013
|
-
var match = url.match(regExp);
|
|
1014
|
-
return match && match[7].length === 11 ? match[7] : null;
|
|
1015
|
-
} : _ref$getVideoId;
|
|
1016
|
-
|
|
1017
|
-
var _useState = useState(typeof window.YT !== 'undefined'),
|
|
1018
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
1019
|
-
apiLoaded = _useState2[0],
|
|
1020
|
-
setApiLoaded = _useState2[1];
|
|
1021
|
-
|
|
1022
|
-
var apiRef = useRef(typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1023
|
-
var elementRef = useRef(null);
|
|
1024
|
-
var playerRef = useRef(null);
|
|
1025
|
-
var playerElementRef = useRef(elementRef.current);
|
|
1026
|
-
var elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1027
|
-
var videoId = useMemo(function () {
|
|
1028
|
-
return getVideoId(id);
|
|
1029
|
-
}, [id]);
|
|
1030
|
-
|
|
1031
|
-
var _useState3 = useState(false),
|
|
1032
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
1033
|
-
ready = _useState4[0],
|
|
1034
|
-
setReady = _useState4[1];
|
|
1035
|
-
|
|
1036
|
-
var _useState5 = useState(initialMuted),
|
|
1037
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
1038
|
-
muted = _useState6[0],
|
|
1039
|
-
setMuted = _useState6[1];
|
|
1132
|
+
let {
|
|
1133
|
+
width = 0,
|
|
1134
|
+
height = 0,
|
|
1135
|
+
duration = 0,
|
|
1136
|
+
autoplay = false,
|
|
1137
|
+
controls = true,
|
|
1138
|
+
timeUpdateInterval = 1000,
|
|
1139
|
+
muted: initialMuted = false,
|
|
1140
|
+
onVolumeChange: customOnVolumeChange = null,
|
|
1141
|
+
onTimeUpdate: customOnTimeUpdate = null,
|
|
1142
|
+
getVideoId = url => {
|
|
1143
|
+
if (url === null || url.match(/^https?:/) === null) {
|
|
1144
|
+
return url;
|
|
1145
|
+
}
|
|
1040
1146
|
|
|
1041
|
-
|
|
1147
|
+
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
|
|
1148
|
+
const match = url.match(regExp);
|
|
1149
|
+
return match && match[7].length === 11 ? match[7] : null;
|
|
1150
|
+
}
|
|
1151
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1152
|
+
const [apiLoaded, setApiLoaded] = useState(typeof window.YT !== 'undefined');
|
|
1153
|
+
const apiRef = useRef(typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1154
|
+
const elementRef = useRef(null);
|
|
1155
|
+
const playerRef = useRef(null);
|
|
1156
|
+
const playerElementRef = useRef(elementRef.current);
|
|
1157
|
+
const elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1158
|
+
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
1159
|
+
const [ready, setReady] = useState(false);
|
|
1160
|
+
const [muted, setMuted] = useState(initialMuted);
|
|
1161
|
+
const [playState, setPlayState] = useState({
|
|
1042
1162
|
playing: false,
|
|
1043
1163
|
paused: false,
|
|
1044
1164
|
ended: false,
|
|
1045
1165
|
buffering: false
|
|
1046
|
-
})
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
duration: duration
|
|
1055
|
-
}),
|
|
1056
|
-
_useState10 = _slicedToArray(_useState9, 2),
|
|
1057
|
-
metadata = _useState10[0],
|
|
1058
|
-
setMetadata = _useState10[1];
|
|
1059
|
-
|
|
1060
|
-
useEffect(function () {
|
|
1061
|
-
var canceled = false;
|
|
1166
|
+
});
|
|
1167
|
+
const [metadata, setMetadata] = useState({
|
|
1168
|
+
width,
|
|
1169
|
+
height,
|
|
1170
|
+
duration
|
|
1171
|
+
});
|
|
1172
|
+
useEffect(() => {
|
|
1173
|
+
let canceled = false;
|
|
1062
1174
|
|
|
1063
1175
|
if (!apiLoaded && videoId !== null) {
|
|
1064
1176
|
debug$1('Load API');
|
|
1065
|
-
loadYouTube().then(
|
|
1177
|
+
loadYouTube().then(api => {
|
|
1066
1178
|
if (!canceled) {
|
|
1067
1179
|
apiRef.current = api;
|
|
1068
1180
|
setApiLoaded(true);
|
|
@@ -1071,21 +1183,27 @@ function useYouTubePlayer(id) {
|
|
|
1071
1183
|
});
|
|
1072
1184
|
}
|
|
1073
1185
|
|
|
1074
|
-
return
|
|
1186
|
+
return () => {
|
|
1075
1187
|
canceled = true;
|
|
1076
1188
|
};
|
|
1077
1189
|
}, [apiLoaded, videoId, setApiLoaded]);
|
|
1078
|
-
|
|
1079
|
-
|
|
1190
|
+
const play = useCallback(() => {
|
|
1191
|
+
const {
|
|
1192
|
+
current: player
|
|
1193
|
+
} = playerRef;
|
|
1080
1194
|
return player !== null && typeof player.playVideo !== 'undefined' ? Promise.resolve(player.playVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1081
1195
|
}, []);
|
|
1082
|
-
|
|
1083
|
-
|
|
1196
|
+
const pause = useCallback(() => {
|
|
1197
|
+
const {
|
|
1198
|
+
current: player
|
|
1199
|
+
} = playerRef;
|
|
1084
1200
|
return player !== null && typeof player.pauseVideo !== 'undefined' ? Promise.resolve(player.pauseVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1085
1201
|
}, []);
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1202
|
+
const setVolume = useCallback(volume => {
|
|
1203
|
+
const {
|
|
1204
|
+
current: player
|
|
1205
|
+
} = playerRef;
|
|
1206
|
+
const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1089
1207
|
|
|
1090
1208
|
if (customOnVolumeChange) {
|
|
1091
1209
|
customOnVolumeChange(volume);
|
|
@@ -1093,35 +1211,41 @@ function useYouTubePlayer(id) {
|
|
|
1093
1211
|
|
|
1094
1212
|
return promise;
|
|
1095
1213
|
}, [customOnVolumeChange]);
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1214
|
+
const mute = useCallback(() => {
|
|
1215
|
+
const {
|
|
1216
|
+
current: player
|
|
1217
|
+
} = playerRef;
|
|
1218
|
+
return (player !== null && typeof player.mute !== 'undefined' ? Promise.resolve(player.mute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(true));
|
|
1101
1219
|
}, [setMuted]);
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1220
|
+
const unmute = useCallback(() => {
|
|
1221
|
+
const {
|
|
1222
|
+
current: player
|
|
1223
|
+
} = playerRef;
|
|
1224
|
+
return (player !== null && typeof player.unMute !== 'undefined' ? Promise.resolve(player.unMute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(false));
|
|
1107
1225
|
}, []);
|
|
1108
|
-
|
|
1109
|
-
|
|
1226
|
+
const seek = useCallback(time => {
|
|
1227
|
+
const {
|
|
1228
|
+
current: player
|
|
1229
|
+
} = playerRef;
|
|
1110
1230
|
return player !== null && typeof player.seekTo !== 'undefined' ? Promise.resolve(player.seekTo(time)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1111
1231
|
}, []);
|
|
1112
|
-
|
|
1113
|
-
|
|
1232
|
+
const setLoop = useCallback(loop => {
|
|
1233
|
+
const {
|
|
1234
|
+
current: player
|
|
1235
|
+
} = playerRef;
|
|
1114
1236
|
return player !== null && typeof player.setLoop !== 'undefined' ? Promise.resolve(player.setLoop(loop)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1115
1237
|
}, []);
|
|
1116
|
-
|
|
1238
|
+
const destroyPlayer = useCallback(() => {
|
|
1117
1239
|
if (playerRef.current !== null) {
|
|
1118
1240
|
debug$1('Unset player');
|
|
1119
1241
|
playerRef.current = null;
|
|
1120
1242
|
}
|
|
1121
1243
|
}, []); // Detect iframe switch and destroy player
|
|
1122
1244
|
|
|
1123
|
-
useEffect(
|
|
1124
|
-
|
|
1245
|
+
useEffect(() => {
|
|
1246
|
+
const {
|
|
1247
|
+
current: currentPlayer
|
|
1248
|
+
} = playerRef;
|
|
1125
1249
|
|
|
1126
1250
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1127
1251
|
debug$1('iFrame switched');
|
|
@@ -1129,20 +1253,23 @@ function useYouTubePlayer(id) {
|
|
|
1129
1253
|
}
|
|
1130
1254
|
}, [elementHasChanged]); // Create player
|
|
1131
1255
|
|
|
1132
|
-
useEffect(
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1256
|
+
useEffect(() => {
|
|
1257
|
+
const {
|
|
1258
|
+
current: YT = null
|
|
1259
|
+
} = apiRef;
|
|
1260
|
+
const {
|
|
1261
|
+
current: currentPlayer = null
|
|
1262
|
+
} = playerRef;
|
|
1263
|
+
const {
|
|
1264
|
+
current: element = null
|
|
1265
|
+
} = elementRef;
|
|
1139
1266
|
|
|
1140
1267
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1141
1268
|
destroyPlayer();
|
|
1142
1269
|
return;
|
|
1143
1270
|
}
|
|
1144
1271
|
|
|
1145
|
-
|
|
1272
|
+
let player = currentPlayer;
|
|
1146
1273
|
|
|
1147
1274
|
if (player !== null && typeof player.loadVideoById !== 'undefined') {
|
|
1148
1275
|
debug$1('Switch video [ID: %s]', videoId);
|
|
@@ -1150,32 +1277,36 @@ function useYouTubePlayer(id) {
|
|
|
1150
1277
|
} else {
|
|
1151
1278
|
debug$1('Create player [ID: %s]', videoId);
|
|
1152
1279
|
|
|
1153
|
-
|
|
1154
|
-
|
|
1280
|
+
const onReady = _ref => {
|
|
1281
|
+
let {
|
|
1282
|
+
target
|
|
1283
|
+
} = _ref;
|
|
1155
1284
|
player = target;
|
|
1156
1285
|
playerRef.current = target;
|
|
1157
1286
|
setReady(true);
|
|
1158
|
-
|
|
1287
|
+
const newDuration = target.getDuration();
|
|
1159
1288
|
|
|
1160
1289
|
if (newDuration !== metadata.duration) {
|
|
1161
|
-
setMetadata(
|
|
1290
|
+
setMetadata({ ...metadata,
|
|
1162
1291
|
duration: newDuration
|
|
1163
|
-
})
|
|
1292
|
+
});
|
|
1164
1293
|
}
|
|
1165
1294
|
|
|
1166
1295
|
debug$1('onReady [ID: %s]', videoId);
|
|
1167
1296
|
};
|
|
1168
1297
|
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1298
|
+
const onStateChange = _ref2 => {
|
|
1299
|
+
let {
|
|
1300
|
+
data: state
|
|
1301
|
+
} = _ref2;
|
|
1302
|
+
const newState = {
|
|
1172
1303
|
playing: state === YT.PlayerState.PLAYING,
|
|
1173
1304
|
paused: state === YT.PlayerState.PAUSED,
|
|
1174
1305
|
ended: state === YT.PlayerState.ENDED,
|
|
1175
1306
|
buffering: state === YT.PlayerState.BUFFERING
|
|
1176
1307
|
};
|
|
1177
1308
|
setPlayState(newState);
|
|
1178
|
-
|
|
1309
|
+
let stateLabel = null;
|
|
1179
1310
|
|
|
1180
1311
|
if (newState.playing) {
|
|
1181
1312
|
stateLabel = 'playing';
|
|
@@ -1195,9 +1326,9 @@ function useYouTubePlayer(id) {
|
|
|
1195
1326
|
};
|
|
1196
1327
|
|
|
1197
1328
|
player = new YT.Player(element, {
|
|
1198
|
-
videoId
|
|
1329
|
+
videoId,
|
|
1199
1330
|
playerVars: {
|
|
1200
|
-
controls
|
|
1331
|
+
controls,
|
|
1201
1332
|
autoplay: autoplay ? 1 : 0,
|
|
1202
1333
|
mute: initialMuted,
|
|
1203
1334
|
playsinline: true,
|
|
@@ -1206,8 +1337,8 @@ function useYouTubePlayer(id) {
|
|
|
1206
1337
|
modestbranding: 1
|
|
1207
1338
|
},
|
|
1208
1339
|
events: {
|
|
1209
|
-
onReady
|
|
1210
|
-
onStateChange
|
|
1340
|
+
onReady,
|
|
1341
|
+
onStateChange
|
|
1211
1342
|
}
|
|
1212
1343
|
});
|
|
1213
1344
|
}
|
|
@@ -1215,127 +1346,88 @@ function useYouTubePlayer(id) {
|
|
|
1215
1346
|
playerRef.current = player;
|
|
1216
1347
|
playerElementRef.current = element;
|
|
1217
1348
|
}, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1349
|
+
const {
|
|
1350
|
+
playing
|
|
1351
|
+
} = playState;
|
|
1352
|
+
const getCurrentTime = useCallback(p => p.getCurrentTime(), []);
|
|
1353
|
+
const currentTime = usePlayerCurrentTime(playerRef.current, {
|
|
1223
1354
|
id: videoId,
|
|
1224
1355
|
disabled: !playing || timeUpdateInterval === null,
|
|
1225
1356
|
updateInterval: timeUpdateInterval,
|
|
1226
1357
|
onUpdate: customOnTimeUpdate,
|
|
1227
|
-
getCurrentTime
|
|
1358
|
+
getCurrentTime
|
|
1228
1359
|
});
|
|
1229
|
-
return
|
|
1360
|
+
return {
|
|
1230
1361
|
ref: elementRef,
|
|
1231
1362
|
player: playerRef.current,
|
|
1232
|
-
play
|
|
1233
|
-
pause
|
|
1234
|
-
mute
|
|
1235
|
-
unmute
|
|
1236
|
-
setVolume
|
|
1237
|
-
seek
|
|
1238
|
-
setLoop
|
|
1239
|
-
ready
|
|
1240
|
-
currentTime
|
|
1241
|
-
muted
|
|
1242
|
-
loaded: ready
|
|
1243
|
-
|
|
1363
|
+
play,
|
|
1364
|
+
pause,
|
|
1365
|
+
mute,
|
|
1366
|
+
unmute,
|
|
1367
|
+
setVolume,
|
|
1368
|
+
seek,
|
|
1369
|
+
setLoop,
|
|
1370
|
+
ready,
|
|
1371
|
+
currentTime,
|
|
1372
|
+
muted,
|
|
1373
|
+
loaded: ready,
|
|
1374
|
+
...metadata,
|
|
1375
|
+
...playState
|
|
1376
|
+
};
|
|
1244
1377
|
}
|
|
1245
1378
|
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
byline = _ref$byline === void 0 ? false : _ref$byline,
|
|
1266
|
-
_ref$controls = _ref.controls,
|
|
1267
|
-
controls = _ref$controls === void 0 ? false : _ref$controls,
|
|
1268
|
-
_ref$initialMuted = _ref.initialMuted,
|
|
1269
|
-
initialMuted = _ref$initialMuted === void 0 ? false : _ref$initialMuted,
|
|
1270
|
-
_ref$timeUpdateInterv = _ref.timeUpdateInterval,
|
|
1271
|
-
timeUpdateInterval = _ref$timeUpdateInterv === void 0 ? 1000 : _ref$timeUpdateInterv,
|
|
1272
|
-
_ref$onTimeUpdate = _ref.onTimeUpdate,
|
|
1273
|
-
customOnTimeUpdate = _ref$onTimeUpdate === void 0 ? null : _ref$onTimeUpdate,
|
|
1274
|
-
_ref$getVideoId = _ref.getVideoId,
|
|
1275
|
-
getVideoId = _ref$getVideoId === void 0 ? function (url) {
|
|
1276
|
-
if (url === null || url.match(/^[0-9]+$/) !== null) {
|
|
1277
|
-
return url;
|
|
1278
|
-
}
|
|
1279
|
-
|
|
1280
|
-
var match = url.match(/\/[0-9]+/);
|
|
1281
|
-
return match !== null ? match[1] : null;
|
|
1282
|
-
} : _ref$getVideoId;
|
|
1283
|
-
|
|
1284
|
-
var _useState = useState(false),
|
|
1285
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
1286
|
-
apiLoaded = _useState2[0],
|
|
1287
|
-
setApiLoaded = _useState2[1];
|
|
1288
|
-
|
|
1289
|
-
var apiRef = useRef(null);
|
|
1290
|
-
var elementRef = useRef(null);
|
|
1291
|
-
var playerRef = useRef(null);
|
|
1292
|
-
var playerElementRef = useRef(elementRef.current);
|
|
1293
|
-
var elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1294
|
-
var videoId = useMemo(function () {
|
|
1295
|
-
return getVideoId(id);
|
|
1296
|
-
}, [id]);
|
|
1297
|
-
|
|
1298
|
-
var _useState3 = useState(false),
|
|
1299
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
1300
|
-
ready = _useState4[0],
|
|
1301
|
-
setReady = _useState4[1];
|
|
1302
|
-
|
|
1303
|
-
var _useState5 = useState(false),
|
|
1304
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
1305
|
-
loaded = _useState6[0],
|
|
1306
|
-
setLoaded = _useState6[1];
|
|
1307
|
-
|
|
1308
|
-
var _useState7 = useState(initialMuted ? 0 : 1),
|
|
1309
|
-
_useState8 = _slicedToArray(_useState7, 2),
|
|
1310
|
-
volume = _useState8[0],
|
|
1311
|
-
setVolumeState = _useState8[1];
|
|
1379
|
+
const debug = createDebug('folklore:video:vimeo');
|
|
1380
|
+
const NO_PLAYER_ERROR = new Error('No player');
|
|
1381
|
+
|
|
1382
|
+
const useVimeoPlayer = function (id) {
|
|
1383
|
+
let {
|
|
1384
|
+
width = 0,
|
|
1385
|
+
height = 0,
|
|
1386
|
+
duration = 0,
|
|
1387
|
+
autoplay = false,
|
|
1388
|
+
autopause = true,
|
|
1389
|
+
byline = false,
|
|
1390
|
+
controls = false,
|
|
1391
|
+
initialMuted = false,
|
|
1392
|
+
timeUpdateInterval = 1000,
|
|
1393
|
+
onTimeUpdate: customOnTimeUpdate = null,
|
|
1394
|
+
getVideoId = url => {
|
|
1395
|
+
if (url === null || url.match(/^[0-9]+$/) !== null) {
|
|
1396
|
+
return url;
|
|
1397
|
+
}
|
|
1312
1398
|
|
|
1313
|
-
|
|
1399
|
+
const match = url.match(/\/[0-9]+/);
|
|
1400
|
+
return match !== null ? match[1] : null;
|
|
1401
|
+
}
|
|
1402
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1403
|
+
const [apiLoaded, setApiLoaded] = useState(false);
|
|
1404
|
+
const apiRef = useRef(null);
|
|
1405
|
+
const elementRef = useRef(null);
|
|
1406
|
+
const playerRef = useRef(null);
|
|
1407
|
+
const playerElementRef = useRef(elementRef.current);
|
|
1408
|
+
const elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1409
|
+
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
1410
|
+
const [ready, setReady] = useState(false);
|
|
1411
|
+
const [loaded, setLoaded] = useState(false);
|
|
1412
|
+
const [volume, setVolumeState] = useState(initialMuted ? 0 : 1);
|
|
1413
|
+
const [playState, setPlayState] = useState({
|
|
1314
1414
|
playing: false,
|
|
1315
1415
|
paused: false,
|
|
1316
1416
|
ended: false,
|
|
1317
1417
|
buffering: false
|
|
1318
|
-
})
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
width: width,
|
|
1325
|
-
height: height,
|
|
1326
|
-
duration: duration
|
|
1327
|
-
}),
|
|
1328
|
-
_useState12 = _slicedToArray(_useState11, 2),
|
|
1329
|
-
metadata = _useState12[0],
|
|
1330
|
-
setMetadata = _useState12[1]; // Load SDK
|
|
1331
|
-
|
|
1418
|
+
});
|
|
1419
|
+
const [metadata, setMetadata] = useState({
|
|
1420
|
+
width,
|
|
1421
|
+
height,
|
|
1422
|
+
duration
|
|
1423
|
+
}); // Load SDK
|
|
1332
1424
|
|
|
1333
|
-
useEffect(
|
|
1334
|
-
|
|
1425
|
+
useEffect(() => {
|
|
1426
|
+
let canceled = false;
|
|
1335
1427
|
|
|
1336
1428
|
if (!apiLoaded && id !== null) {
|
|
1337
1429
|
debug('Load API');
|
|
1338
|
-
loadVimeo().then(
|
|
1430
|
+
loadVimeo().then(api => {
|
|
1339
1431
|
if (!canceled) {
|
|
1340
1432
|
apiRef.current = api;
|
|
1341
1433
|
setApiLoaded(true);
|
|
@@ -1344,40 +1436,56 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1344
1436
|
});
|
|
1345
1437
|
}
|
|
1346
1438
|
|
|
1347
|
-
return
|
|
1439
|
+
return () => {
|
|
1348
1440
|
canceled = true;
|
|
1349
1441
|
};
|
|
1350
1442
|
}, [id]);
|
|
1351
|
-
|
|
1352
|
-
|
|
1443
|
+
const play = useCallback(() => {
|
|
1444
|
+
const {
|
|
1445
|
+
current: player
|
|
1446
|
+
} = playerRef;
|
|
1353
1447
|
return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR);
|
|
1354
1448
|
}, []);
|
|
1355
|
-
|
|
1356
|
-
|
|
1449
|
+
const pause = useCallback(() => {
|
|
1450
|
+
const {
|
|
1451
|
+
current: player
|
|
1452
|
+
} = playerRef;
|
|
1357
1453
|
return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR);
|
|
1358
1454
|
}, []);
|
|
1359
|
-
|
|
1360
|
-
|
|
1455
|
+
const setVolume = useCallback(newVolume => {
|
|
1456
|
+
const {
|
|
1457
|
+
current: player
|
|
1458
|
+
} = playerRef;
|
|
1361
1459
|
return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR);
|
|
1362
1460
|
}, []);
|
|
1363
|
-
|
|
1364
|
-
|
|
1461
|
+
const mute = useCallback(() => {
|
|
1462
|
+
const {
|
|
1463
|
+
current: player
|
|
1464
|
+
} = playerRef;
|
|
1365
1465
|
return player !== null ? player.setVolume(0) : Promise.reject(NO_PLAYER_ERROR);
|
|
1366
1466
|
}, []);
|
|
1367
|
-
|
|
1368
|
-
|
|
1467
|
+
const unmute = useCallback(() => {
|
|
1468
|
+
const {
|
|
1469
|
+
current: player
|
|
1470
|
+
} = playerRef;
|
|
1369
1471
|
return player !== null ? player.setVolume(1) : Promise.reject(NO_PLAYER_ERROR);
|
|
1370
1472
|
}, []);
|
|
1371
|
-
|
|
1372
|
-
|
|
1473
|
+
const seek = useCallback(time => {
|
|
1474
|
+
const {
|
|
1475
|
+
current: player
|
|
1476
|
+
} = playerRef;
|
|
1373
1477
|
return player !== null ? player.setCurrentTime(time) : Promise.reject(NO_PLAYER_ERROR);
|
|
1374
1478
|
}, []);
|
|
1375
|
-
|
|
1376
|
-
|
|
1479
|
+
const setLoop = useCallback(loop => {
|
|
1480
|
+
const {
|
|
1481
|
+
current: player
|
|
1482
|
+
} = playerRef;
|
|
1377
1483
|
return player !== null ? player.setLoop(loop) : Promise.reject(NO_PLAYER_ERROR);
|
|
1378
1484
|
}, []);
|
|
1379
|
-
|
|
1380
|
-
|
|
1485
|
+
const destroyVideo = useCallback(() => {
|
|
1486
|
+
const {
|
|
1487
|
+
current: player
|
|
1488
|
+
} = playerRef;
|
|
1381
1489
|
|
|
1382
1490
|
if (player !== null) {
|
|
1383
1491
|
debug('Unload video');
|
|
@@ -1389,8 +1497,10 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1389
1497
|
playerRef.current = null;
|
|
1390
1498
|
}
|
|
1391
1499
|
}, []);
|
|
1392
|
-
useEffect(
|
|
1393
|
-
|
|
1500
|
+
useEffect(() => {
|
|
1501
|
+
const {
|
|
1502
|
+
current: currentPlayer
|
|
1503
|
+
} = playerRef;
|
|
1394
1504
|
|
|
1395
1505
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1396
1506
|
debug('iFrame switched');
|
|
@@ -1398,40 +1508,41 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1398
1508
|
}
|
|
1399
1509
|
}, [elementHasChanged]); // Create player
|
|
1400
1510
|
|
|
1401
|
-
useEffect(
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1511
|
+
useEffect(() => {
|
|
1512
|
+
const {
|
|
1513
|
+
current: Player = null
|
|
1514
|
+
} = apiRef;
|
|
1515
|
+
const {
|
|
1516
|
+
current: currentPlayer = null
|
|
1517
|
+
} = playerRef;
|
|
1518
|
+
const {
|
|
1519
|
+
current: element = null
|
|
1520
|
+
} = elementRef;
|
|
1408
1521
|
|
|
1409
1522
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1410
1523
|
destroyVideo();
|
|
1411
1524
|
return;
|
|
1412
1525
|
}
|
|
1413
1526
|
|
|
1414
|
-
|
|
1527
|
+
let player = currentPlayer;
|
|
1415
1528
|
|
|
1416
1529
|
if (player === null) {
|
|
1417
1530
|
debug('Create player [ID: %s]', videoId);
|
|
1418
1531
|
player = new Player(element, {
|
|
1419
1532
|
id: videoId,
|
|
1420
|
-
autoplay
|
|
1421
|
-
autopause
|
|
1422
|
-
byline
|
|
1423
|
-
controls
|
|
1533
|
+
autoplay,
|
|
1534
|
+
autopause,
|
|
1535
|
+
byline,
|
|
1536
|
+
controls,
|
|
1424
1537
|
muted: initialMuted,
|
|
1425
1538
|
background: !controls
|
|
1426
1539
|
});
|
|
1427
|
-
player.ready().then(
|
|
1428
|
-
return setReady(true);
|
|
1429
|
-
}).catch(function (e) {
|
|
1540
|
+
player.ready().then(() => setReady(true)).catch(e => {
|
|
1430
1541
|
debug('ERROR: %o', e);
|
|
1431
1542
|
});
|
|
1432
1543
|
} else {
|
|
1433
1544
|
debug('Load video [ID: %s]', videoId);
|
|
1434
|
-
player.loadVideo(videoId).catch(
|
|
1545
|
+
player.loadVideo(videoId).catch(e => {
|
|
1435
1546
|
debug('ERROR: %o', e);
|
|
1436
1547
|
});
|
|
1437
1548
|
}
|
|
@@ -1440,28 +1551,26 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1440
1551
|
playerElementRef.current = element;
|
|
1441
1552
|
}, [apiLoaded, videoId, elementHasChanged, setReady, destroyVideo, setLoaded]); // Bind player events
|
|
1442
1553
|
|
|
1443
|
-
useEffect(
|
|
1444
|
-
|
|
1554
|
+
useEffect(() => {
|
|
1555
|
+
const {
|
|
1556
|
+
current: player
|
|
1557
|
+
} = playerRef;
|
|
1445
1558
|
|
|
1446
1559
|
if (player === null) {
|
|
1447
|
-
return
|
|
1560
|
+
return () => {};
|
|
1448
1561
|
}
|
|
1449
1562
|
|
|
1450
|
-
|
|
1563
|
+
let canceled = false;
|
|
1451
1564
|
|
|
1452
|
-
|
|
1453
|
-
Promise.all([player.getDuration(), player.getVideoWidth(), player.getVideoHeight(), player.getMuted()]).then(
|
|
1454
|
-
|
|
1455
|
-
newDuration = _ref3[0],
|
|
1456
|
-
newWidth = _ref3[1],
|
|
1457
|
-
newHeight = _ref3[2],
|
|
1458
|
-
newMuted = _ref3[3];
|
|
1565
|
+
const onLoaded = () => {
|
|
1566
|
+
Promise.all([player.getDuration(), player.getVideoWidth(), player.getVideoHeight(), player.getMuted()]).then(_ref => {
|
|
1567
|
+
let [newDuration, newWidth, newHeight, newMuted] = _ref;
|
|
1459
1568
|
|
|
1460
1569
|
if (canceled) {
|
|
1461
1570
|
return;
|
|
1462
1571
|
}
|
|
1463
1572
|
|
|
1464
|
-
|
|
1573
|
+
const newMetadata = {
|
|
1465
1574
|
duration: newDuration,
|
|
1466
1575
|
width: newWidth,
|
|
1467
1576
|
height: newHeight
|
|
@@ -1477,7 +1586,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1477
1586
|
debug('onLoaded [ID: %s]', videoId);
|
|
1478
1587
|
};
|
|
1479
1588
|
|
|
1480
|
-
|
|
1589
|
+
const onPlay = () => {
|
|
1481
1590
|
setPlayState({
|
|
1482
1591
|
playing: true,
|
|
1483
1592
|
paused: false,
|
|
@@ -1485,14 +1594,14 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1485
1594
|
buffering: false
|
|
1486
1595
|
});
|
|
1487
1596
|
debug('onPlay [ID: %s]', videoId);
|
|
1488
|
-
player.getMuted().then(
|
|
1597
|
+
player.getMuted().then(newMuted => {
|
|
1489
1598
|
if (!canceled && newMuted) {
|
|
1490
1599
|
setVolumeState(0);
|
|
1491
1600
|
}
|
|
1492
|
-
}).catch(
|
|
1601
|
+
}).catch(() => {});
|
|
1493
1602
|
};
|
|
1494
1603
|
|
|
1495
|
-
|
|
1604
|
+
const onPause = () => {
|
|
1496
1605
|
setPlayState({
|
|
1497
1606
|
playing: false,
|
|
1498
1607
|
paused: true,
|
|
@@ -1502,13 +1611,15 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1502
1611
|
debug('onPause [ID: %s]', videoId);
|
|
1503
1612
|
};
|
|
1504
1613
|
|
|
1505
|
-
|
|
1506
|
-
|
|
1614
|
+
const onVolumeChange = _ref2 => {
|
|
1615
|
+
let {
|
|
1616
|
+
volume: newVolume
|
|
1617
|
+
} = _ref2;
|
|
1507
1618
|
setVolumeState(newVolume);
|
|
1508
1619
|
debug('onVolumeChange [ID: %s]', videoId);
|
|
1509
1620
|
};
|
|
1510
1621
|
|
|
1511
|
-
|
|
1622
|
+
const onEnded = () => {
|
|
1512
1623
|
setPlayState({
|
|
1513
1624
|
playing: false,
|
|
1514
1625
|
paused: false,
|
|
@@ -1518,7 +1629,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1518
1629
|
debug('onEnded [ID: %s]', videoId);
|
|
1519
1630
|
};
|
|
1520
1631
|
|
|
1521
|
-
|
|
1632
|
+
const onBufferStart = () => {
|
|
1522
1633
|
setPlayState({
|
|
1523
1634
|
playing: true,
|
|
1524
1635
|
paused: false,
|
|
@@ -1528,7 +1639,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1528
1639
|
debug('onBufferStart [ID: %s]', videoId);
|
|
1529
1640
|
};
|
|
1530
1641
|
|
|
1531
|
-
|
|
1642
|
+
const onBufferEnded = () => {
|
|
1532
1643
|
setPlayState({
|
|
1533
1644
|
playing: true,
|
|
1534
1645
|
paused: false,
|
|
@@ -1546,7 +1657,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1546
1657
|
player.on('ended', onEnded);
|
|
1547
1658
|
player.on('bufferstart', onBufferStart);
|
|
1548
1659
|
player.on('bufferend', onBufferEnded);
|
|
1549
|
-
return
|
|
1660
|
+
return () => {
|
|
1550
1661
|
canceled = true;
|
|
1551
1662
|
debug('Unbind events [ID: %s]', videoId);
|
|
1552
1663
|
player.off('loaded', onLoaded);
|
|
@@ -1558,63 +1669,55 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1558
1669
|
player.off('bufferend', onBufferEnded);
|
|
1559
1670
|
};
|
|
1560
1671
|
}, [videoId, playerRef.current, setPlayState, setMetadata, setVolumeState, setLoaded]);
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1672
|
+
const {
|
|
1673
|
+
playing
|
|
1674
|
+
} = playState;
|
|
1675
|
+
const getCurrentTime = useCallback(p => p.getCurrentTime(), []);
|
|
1676
|
+
const currentTime = usePlayerCurrentTime(playerRef.current, {
|
|
1566
1677
|
id: videoId,
|
|
1567
1678
|
disabled: !playing || timeUpdateInterval === null,
|
|
1568
1679
|
updateInterval: timeUpdateInterval,
|
|
1569
1680
|
onUpdate: customOnTimeUpdate,
|
|
1570
|
-
getCurrentTime
|
|
1681
|
+
getCurrentTime
|
|
1571
1682
|
});
|
|
1572
|
-
return
|
|
1683
|
+
return {
|
|
1573
1684
|
ref: elementRef,
|
|
1574
1685
|
player: playerRef.current,
|
|
1575
|
-
play
|
|
1576
|
-
pause
|
|
1577
|
-
mute
|
|
1578
|
-
unmute
|
|
1579
|
-
setVolume
|
|
1580
|
-
seek
|
|
1581
|
-
setLoop
|
|
1582
|
-
ready
|
|
1583
|
-
currentTime
|
|
1584
|
-
loaded
|
|
1686
|
+
play,
|
|
1687
|
+
pause,
|
|
1688
|
+
mute,
|
|
1689
|
+
unmute,
|
|
1690
|
+
setVolume,
|
|
1691
|
+
seek,
|
|
1692
|
+
setLoop,
|
|
1693
|
+
ready,
|
|
1694
|
+
currentTime,
|
|
1695
|
+
loaded,
|
|
1585
1696
|
muted: volume === 0,
|
|
1586
|
-
volume
|
|
1587
|
-
|
|
1697
|
+
volume,
|
|
1698
|
+
...metadata,
|
|
1699
|
+
...playState
|
|
1700
|
+
};
|
|
1588
1701
|
};
|
|
1589
1702
|
|
|
1590
1703
|
function useVideoPlayer(params) {
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
_ref$onBufferStart = _ref.onBufferStart,
|
|
1609
|
-
customOnBufferStart = _ref$onBufferStart === void 0 ? null : _ref$onBufferStart,
|
|
1610
|
-
_ref$onBufferEnded = _ref.onBufferEnded,
|
|
1611
|
-
customOnBufferEnded = _ref$onBufferEnded === void 0 ? null : _ref$onBufferEnded;
|
|
1612
|
-
|
|
1613
|
-
var dailymotionPlayer = useDailymotionPlayer(service === 'dailymotion' ? videoId || url : null, params);
|
|
1614
|
-
var youtubePlayer = useYouTubePlayer(service === 'youtube' ? videoId || url : null, params);
|
|
1615
|
-
var vimeoPlayer = useVimeoPlayer(service === 'vimeo' ? videoId || url : null, params);
|
|
1616
|
-
var nativePlayer = useNativeVideoPlayer(url, params);
|
|
1617
|
-
var player = null;
|
|
1704
|
+
const {
|
|
1705
|
+
service = null,
|
|
1706
|
+
videoId = null,
|
|
1707
|
+
url = null,
|
|
1708
|
+
onLoaded: customOnLoaded = null,
|
|
1709
|
+
onPlay: customOnPlay = null,
|
|
1710
|
+
onPause: customOnPause = null,
|
|
1711
|
+
onEnd: customOnEnd = null,
|
|
1712
|
+
onMetadataChange: customOnMetadataChange = null,
|
|
1713
|
+
onBufferStart: customOnBufferStart = null,
|
|
1714
|
+
onBufferEnded: customOnBufferEnded = null
|
|
1715
|
+
} = params || {};
|
|
1716
|
+
const dailymotionPlayer = useDailymotionPlayer(service === 'dailymotion' ? videoId || url : null, params);
|
|
1717
|
+
const youtubePlayer = useYouTubePlayer(service === 'youtube' ? videoId || url : null, params);
|
|
1718
|
+
const vimeoPlayer = useVimeoPlayer(service === 'vimeo' ? videoId || url : null, params);
|
|
1719
|
+
const nativePlayer = useNativeVideoPlayer(url, params);
|
|
1720
|
+
let player = null;
|
|
1618
1721
|
|
|
1619
1722
|
if (service === 'dailymotion') {
|
|
1620
1723
|
player = dailymotionPlayer;
|
|
@@ -1626,53 +1729,45 @@ function useVideoPlayer(params) {
|
|
|
1626
1729
|
player = nativePlayer;
|
|
1627
1730
|
}
|
|
1628
1731
|
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
_ref2$width = _ref2.width,
|
|
1641
|
-
metaWidth = _ref2$width === void 0 ? null : _ref2$width,
|
|
1642
|
-
_ref2$height = _ref2.height,
|
|
1643
|
-
metaHeight = _ref2$height === void 0 ? null : _ref2$height,
|
|
1644
|
-
_ref2$duration = _ref2.duration,
|
|
1645
|
-
metaDuration = _ref2$duration === void 0 ? null : _ref2$duration;
|
|
1646
|
-
|
|
1647
|
-
useEffect(function () {
|
|
1732
|
+
const {
|
|
1733
|
+
playing = false,
|
|
1734
|
+
paused = false,
|
|
1735
|
+
buffering = false,
|
|
1736
|
+
ended = false,
|
|
1737
|
+
ready = false,
|
|
1738
|
+
width: metaWidth = null,
|
|
1739
|
+
height: metaHeight = null,
|
|
1740
|
+
duration: metaDuration = null
|
|
1741
|
+
} = player || {};
|
|
1742
|
+
useEffect(() => {
|
|
1648
1743
|
if (ready && customOnLoaded !== null) {
|
|
1649
1744
|
customOnLoaded();
|
|
1650
1745
|
}
|
|
1651
1746
|
}, [ready, customOnLoaded]);
|
|
1652
|
-
useEffect(
|
|
1747
|
+
useEffect(() => {
|
|
1653
1748
|
if (playing && customOnPlay !== null) {
|
|
1654
1749
|
customOnPlay();
|
|
1655
1750
|
}
|
|
1656
1751
|
}, [playing, customOnPlay]);
|
|
1657
|
-
useEffect(
|
|
1752
|
+
useEffect(() => {
|
|
1658
1753
|
if (paused && customOnPause !== null) {
|
|
1659
1754
|
customOnPause();
|
|
1660
1755
|
}
|
|
1661
1756
|
}, [paused, customOnPause]);
|
|
1662
|
-
useEffect(
|
|
1757
|
+
useEffect(() => {
|
|
1663
1758
|
if (buffering && customOnBufferStart !== null) {
|
|
1664
1759
|
customOnBufferStart();
|
|
1665
1760
|
} else if (!buffering && customOnBufferEnded !== null) {
|
|
1666
1761
|
customOnBufferEnded();
|
|
1667
1762
|
}
|
|
1668
1763
|
}, [buffering, customOnBufferStart, customOnBufferEnded]);
|
|
1669
|
-
useEffect(
|
|
1764
|
+
useEffect(() => {
|
|
1670
1765
|
if (ended && customOnEnd !== null) {
|
|
1671
1766
|
customOnEnd();
|
|
1672
1767
|
}
|
|
1673
1768
|
}, [ended, customOnEnd]);
|
|
1674
|
-
useEffect(
|
|
1675
|
-
|
|
1769
|
+
useEffect(() => {
|
|
1770
|
+
const hasMetadata = metaWidth !== null || metaHeight !== null || metaDuration !== null;
|
|
1676
1771
|
|
|
1677
1772
|
if (hasMetadata && customOnMetadataChange !== null) {
|
|
1678
1773
|
customOnMetadataChange({
|
|
@@ -1685,27 +1780,21 @@ function useVideoPlayer(params) {
|
|
|
1685
1780
|
return player;
|
|
1686
1781
|
}
|
|
1687
1782
|
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
};
|
|
1693
|
-
};
|
|
1694
|
-
|
|
1695
|
-
var currentScroll = getWindowScroll();
|
|
1783
|
+
const getWindowScroll = () => ({
|
|
1784
|
+
x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
|
|
1785
|
+
y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
|
|
1786
|
+
});
|
|
1696
1787
|
|
|
1697
|
-
|
|
1698
|
-
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1699
|
-
var _opts$onChange = opts.onChange,
|
|
1700
|
-
onChange = _opts$onChange === void 0 ? null : _opts$onChange;
|
|
1788
|
+
const currentScroll = getWindowScroll();
|
|
1701
1789
|
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1790
|
+
const useWindowScroll = function () {
|
|
1791
|
+
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1792
|
+
const {
|
|
1793
|
+
onChange = null
|
|
1794
|
+
} = opts;
|
|
1795
|
+
const [scroll, setScroll] = useState(currentScroll);
|
|
1796
|
+
const updateScroll = useCallback(() => {
|
|
1797
|
+
const newScroll = getWindowScroll();
|
|
1709
1798
|
|
|
1710
1799
|
if (currentScroll.x !== newScroll.x || currentScroll.y !== newScroll.y) {
|
|
1711
1800
|
currentScroll.x = newScroll.x;
|
|
@@ -1716,40 +1805,33 @@ var useWindowScroll = function useWindowScroll() {
|
|
|
1716
1805
|
|
|
1717
1806
|
return null;
|
|
1718
1807
|
}, [setScroll]);
|
|
1719
|
-
|
|
1720
|
-
|
|
1808
|
+
const onScroll = useCallback(() => {
|
|
1809
|
+
const newScroll = updateScroll();
|
|
1721
1810
|
|
|
1722
1811
|
if (newScroll !== null && onChange !== null) {
|
|
1723
1812
|
onChange(newScroll);
|
|
1724
1813
|
}
|
|
1725
1814
|
}, [updateScroll, onChange]);
|
|
1726
1815
|
useWindowEvent('scroll', onScroll);
|
|
1727
|
-
useEffect(
|
|
1816
|
+
useEffect(() => {
|
|
1728
1817
|
onScroll();
|
|
1729
1818
|
}, []);
|
|
1730
1819
|
return scroll;
|
|
1731
1820
|
};
|
|
1732
1821
|
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
};
|
|
1739
|
-
var currentSize = getWindowSize();
|
|
1822
|
+
const getWindowSize = () => ({
|
|
1823
|
+
width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,
|
|
1824
|
+
height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
|
|
1825
|
+
});
|
|
1826
|
+
let currentSize = getWindowSize();
|
|
1740
1827
|
|
|
1741
1828
|
function useWindowSize() {
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
size = _useState2[0],
|
|
1749
|
-
setSize = _useState2[1];
|
|
1750
|
-
|
|
1751
|
-
var updateSize = useCallback(function () {
|
|
1752
|
-
var newSize = getWindowSize();
|
|
1829
|
+
let {
|
|
1830
|
+
onChange = null
|
|
1831
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1832
|
+
const [size, setSize] = useState(currentSize);
|
|
1833
|
+
const updateSize = useCallback(() => {
|
|
1834
|
+
const newSize = getWindowSize();
|
|
1753
1835
|
|
|
1754
1836
|
if (currentSize.width !== newSize.width || currentSize.height !== newSize.height) {
|
|
1755
1837
|
currentSize = newSize;
|
|
@@ -1759,18 +1841,18 @@ function useWindowSize() {
|
|
|
1759
1841
|
|
|
1760
1842
|
return null;
|
|
1761
1843
|
}, [setSize]);
|
|
1762
|
-
|
|
1763
|
-
|
|
1844
|
+
const onResize = useCallback(() => {
|
|
1845
|
+
const newSize = updateSize();
|
|
1764
1846
|
|
|
1765
1847
|
if (newSize !== null && onChange !== null) {
|
|
1766
1848
|
onChange(newSize);
|
|
1767
1849
|
}
|
|
1768
1850
|
}, [onChange]);
|
|
1769
1851
|
useWindowEvent('resize', onResize);
|
|
1770
|
-
useEffect(
|
|
1852
|
+
useEffect(() => {
|
|
1771
1853
|
onResize();
|
|
1772
1854
|
}, []);
|
|
1773
1855
|
return size;
|
|
1774
1856
|
}
|
|
1775
1857
|
|
|
1776
|
-
export { eventsManager$1 as documentEventsManager, getObserver, useCounter, useDailymotionPlayer, useDocumentEvent, useIntersectionObserver, useKeyboard, useNativeVideoPlayer, useObserver, usePlayerCurrentTime, useResizeObserver, useVideoPlayer, useVimeoPlayer, useWindowEvent, useWindowScroll, useWindowSize, useYouTubePlayer, eventsManager as windowEventsManager };
|
|
1858
|
+
export { eventsManager$1 as documentEventsManager, getObserver, useCounter, useDailymotionPlayer, useDocumentEvent, useIntersectionObserver, useItemsPaginated, useKeyboard, useNativeVideoPlayer, useObserver, usePlayerCurrentTime, useResizeObserver, useVideoPlayer, useVimeoPlayer, useWindowEvent, useWindowScroll, useWindowSize, useYouTubePlayer, eventsManager as windowEventsManager };
|