@folklore/hooks 0.0.27 → 0.0.29
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 +923 -840
- package/dist/es.js +922 -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,31 @@ 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
|
-
|
|
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: defaultThreshold = null,
|
|
1031
|
+
disabled = false
|
|
1032
|
+
} = opts;
|
|
1033
|
+
const [entry, setEntry] = useState(initialEntry);
|
|
1034
|
+
const threshold = useMemo(() => defaultThreshold, [defaultThreshold]);
|
|
1035
|
+
const nodeRef = useRef(null);
|
|
1036
|
+
const currentElement = useRef(null);
|
|
1037
|
+
const elementChanged = nodeRef.current !== currentElement.current;
|
|
1038
|
+
useEffect(() => {
|
|
1039
|
+
const {
|
|
1040
|
+
current: nodeElement
|
|
1041
|
+
} = nodeRef;
|
|
1042
|
+
|
|
1043
|
+
const callback = newEntry => setEntry(newEntry);
|
|
1044
|
+
|
|
1045
|
+
let unsubscribe = null;
|
|
892
1046
|
|
|
893
1047
|
if (nodeElement !== null) {
|
|
894
|
-
|
|
1048
|
+
const newOpts = {};
|
|
895
1049
|
|
|
896
1050
|
if (root !== null) {
|
|
897
1051
|
newOpts.root = root;
|
|
@@ -905,16 +1059,16 @@ var useObserver = function useObserver(Observer) {
|
|
|
905
1059
|
newOpts.threshold = threshold;
|
|
906
1060
|
}
|
|
907
1061
|
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
1062
|
+
const {
|
|
1063
|
+
subscribe,
|
|
1064
|
+
unsubscribe: localUnsubscribe
|
|
1065
|
+
} = getObserver(Observer, newOpts);
|
|
912
1066
|
unsubscribe = localUnsubscribe;
|
|
913
1067
|
subscribe(nodeElement, callback);
|
|
914
1068
|
}
|
|
915
1069
|
|
|
916
1070
|
currentElement.current = nodeElement;
|
|
917
|
-
return
|
|
1071
|
+
return () => {
|
|
918
1072
|
if (unsubscribe !== null) {
|
|
919
1073
|
unsubscribe(nodeElement, callback);
|
|
920
1074
|
}
|
|
@@ -922,14 +1076,15 @@ var useObserver = function useObserver(Observer) {
|
|
|
922
1076
|
}, [Observer, elementChanged, disabled, root, rootMargin, threshold]);
|
|
923
1077
|
return {
|
|
924
1078
|
ref: nodeRef,
|
|
925
|
-
entry
|
|
1079
|
+
entry
|
|
926
1080
|
};
|
|
927
1081
|
};
|
|
928
1082
|
/**
|
|
929
1083
|
* Intersection Observer
|
|
930
1084
|
*/
|
|
931
1085
|
|
|
932
|
-
|
|
1086
|
+
const defaultThreshold = [0, 1.0];
|
|
1087
|
+
const intersectionObserverInitialEntry = {
|
|
933
1088
|
target: null,
|
|
934
1089
|
time: null,
|
|
935
1090
|
isVisible: false,
|
|
@@ -939,130 +1094,89 @@ var intersectionObserverInitialEntry = {
|
|
|
939
1094
|
boundingClientRect: null,
|
|
940
1095
|
rootBounds: null
|
|
941
1096
|
};
|
|
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
|
-
|
|
1097
|
+
const useIntersectionObserver = function () {
|
|
1098
|
+
let {
|
|
1099
|
+
root = null,
|
|
1100
|
+
rootMargin = '0px',
|
|
1101
|
+
threshold = defaultThreshold,
|
|
1102
|
+
disabled = false
|
|
1103
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
953
1104
|
return useObserver(IntersectionObserver, {
|
|
954
|
-
root
|
|
955
|
-
rootMargin
|
|
956
|
-
threshold
|
|
957
|
-
disabled
|
|
1105
|
+
root,
|
|
1106
|
+
rootMargin,
|
|
1107
|
+
threshold,
|
|
1108
|
+
disabled
|
|
958
1109
|
}, intersectionObserverInitialEntry);
|
|
959
1110
|
};
|
|
960
1111
|
/**
|
|
961
1112
|
* Resize Observer
|
|
962
1113
|
*/
|
|
963
1114
|
|
|
964
|
-
|
|
1115
|
+
const resizeObserverInitialEntry = {
|
|
965
1116
|
target: null,
|
|
966
1117
|
contentRect: null,
|
|
967
1118
|
contentBoxSize: null,
|
|
968
1119
|
borderBoxSize: null
|
|
969
1120
|
};
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
1121
|
+
const useResizeObserver = function () {
|
|
1122
|
+
let {
|
|
1123
|
+
disabled = false
|
|
1124
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
975
1125
|
return useObserver(ResizeObserver, {
|
|
976
|
-
disabled
|
|
1126
|
+
disabled
|
|
977
1127
|
}, resizeObserverInitialEntry);
|
|
978
1128
|
};
|
|
979
1129
|
|
|
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');
|
|
1130
|
+
const NO_PLAYER_ERROR$1 = new Error('No player');
|
|
1131
|
+
const debug$1 = createDebug('folklore:video:youtube');
|
|
985
1132
|
|
|
986
1133
|
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];
|
|
1134
|
+
let {
|
|
1135
|
+
width = 0,
|
|
1136
|
+
height = 0,
|
|
1137
|
+
duration = 0,
|
|
1138
|
+
autoplay = false,
|
|
1139
|
+
controls = true,
|
|
1140
|
+
timeUpdateInterval = 1000,
|
|
1141
|
+
muted: initialMuted = false,
|
|
1142
|
+
onVolumeChange: customOnVolumeChange = null,
|
|
1143
|
+
onTimeUpdate: customOnTimeUpdate = null,
|
|
1144
|
+
getVideoId = url => {
|
|
1145
|
+
if (url === null || url.match(/^https?:/) === null) {
|
|
1146
|
+
return url;
|
|
1147
|
+
}
|
|
1040
1148
|
|
|
1041
|
-
|
|
1149
|
+
const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
|
|
1150
|
+
const match = url.match(regExp);
|
|
1151
|
+
return match && match[7].length === 11 ? match[7] : null;
|
|
1152
|
+
}
|
|
1153
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1154
|
+
const [apiLoaded, setApiLoaded] = useState(typeof window.YT !== 'undefined');
|
|
1155
|
+
const apiRef = useRef(typeof window.YT !== 'undefined' ? window.YT : null);
|
|
1156
|
+
const elementRef = useRef(null);
|
|
1157
|
+
const playerRef = useRef(null);
|
|
1158
|
+
const playerElementRef = useRef(elementRef.current);
|
|
1159
|
+
const elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1160
|
+
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
1161
|
+
const [ready, setReady] = useState(false);
|
|
1162
|
+
const [muted, setMuted] = useState(initialMuted);
|
|
1163
|
+
const [playState, setPlayState] = useState({
|
|
1042
1164
|
playing: false,
|
|
1043
1165
|
paused: false,
|
|
1044
1166
|
ended: false,
|
|
1045
1167
|
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;
|
|
1168
|
+
});
|
|
1169
|
+
const [metadata, setMetadata] = useState({
|
|
1170
|
+
width,
|
|
1171
|
+
height,
|
|
1172
|
+
duration
|
|
1173
|
+
});
|
|
1174
|
+
useEffect(() => {
|
|
1175
|
+
let canceled = false;
|
|
1062
1176
|
|
|
1063
1177
|
if (!apiLoaded && videoId !== null) {
|
|
1064
1178
|
debug$1('Load API');
|
|
1065
|
-
loadYouTube().then(
|
|
1179
|
+
loadYouTube().then(api => {
|
|
1066
1180
|
if (!canceled) {
|
|
1067
1181
|
apiRef.current = api;
|
|
1068
1182
|
setApiLoaded(true);
|
|
@@ -1071,21 +1185,27 @@ function useYouTubePlayer(id) {
|
|
|
1071
1185
|
});
|
|
1072
1186
|
}
|
|
1073
1187
|
|
|
1074
|
-
return
|
|
1188
|
+
return () => {
|
|
1075
1189
|
canceled = true;
|
|
1076
1190
|
};
|
|
1077
1191
|
}, [apiLoaded, videoId, setApiLoaded]);
|
|
1078
|
-
|
|
1079
|
-
|
|
1192
|
+
const play = useCallback(() => {
|
|
1193
|
+
const {
|
|
1194
|
+
current: player
|
|
1195
|
+
} = playerRef;
|
|
1080
1196
|
return player !== null && typeof player.playVideo !== 'undefined' ? Promise.resolve(player.playVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1081
1197
|
}, []);
|
|
1082
|
-
|
|
1083
|
-
|
|
1198
|
+
const pause = useCallback(() => {
|
|
1199
|
+
const {
|
|
1200
|
+
current: player
|
|
1201
|
+
} = playerRef;
|
|
1084
1202
|
return player !== null && typeof player.pauseVideo !== 'undefined' ? Promise.resolve(player.pauseVideo()) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1085
1203
|
}, []);
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1204
|
+
const setVolume = useCallback(volume => {
|
|
1205
|
+
const {
|
|
1206
|
+
current: player
|
|
1207
|
+
} = playerRef;
|
|
1208
|
+
const promise = player !== null && typeof player.setVolume !== 'undefined' ? Promise.resolve(player.setVolume(volume * 100)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1089
1209
|
|
|
1090
1210
|
if (customOnVolumeChange) {
|
|
1091
1211
|
customOnVolumeChange(volume);
|
|
@@ -1093,35 +1213,41 @@ function useYouTubePlayer(id) {
|
|
|
1093
1213
|
|
|
1094
1214
|
return promise;
|
|
1095
1215
|
}, [customOnVolumeChange]);
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1216
|
+
const mute = useCallback(() => {
|
|
1217
|
+
const {
|
|
1218
|
+
current: player
|
|
1219
|
+
} = playerRef;
|
|
1220
|
+
return (player !== null && typeof player.mute !== 'undefined' ? Promise.resolve(player.mute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(true));
|
|
1101
1221
|
}, [setMuted]);
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1222
|
+
const unmute = useCallback(() => {
|
|
1223
|
+
const {
|
|
1224
|
+
current: player
|
|
1225
|
+
} = playerRef;
|
|
1226
|
+
return (player !== null && typeof player.unMute !== 'undefined' ? Promise.resolve(player.unMute()) : Promise.reject(NO_PLAYER_ERROR$1)).then(() => setMuted(false));
|
|
1107
1227
|
}, []);
|
|
1108
|
-
|
|
1109
|
-
|
|
1228
|
+
const seek = useCallback(time => {
|
|
1229
|
+
const {
|
|
1230
|
+
current: player
|
|
1231
|
+
} = playerRef;
|
|
1110
1232
|
return player !== null && typeof player.seekTo !== 'undefined' ? Promise.resolve(player.seekTo(time)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1111
1233
|
}, []);
|
|
1112
|
-
|
|
1113
|
-
|
|
1234
|
+
const setLoop = useCallback(loop => {
|
|
1235
|
+
const {
|
|
1236
|
+
current: player
|
|
1237
|
+
} = playerRef;
|
|
1114
1238
|
return player !== null && typeof player.setLoop !== 'undefined' ? Promise.resolve(player.setLoop(loop)) : Promise.reject(NO_PLAYER_ERROR$1);
|
|
1115
1239
|
}, []);
|
|
1116
|
-
|
|
1240
|
+
const destroyPlayer = useCallback(() => {
|
|
1117
1241
|
if (playerRef.current !== null) {
|
|
1118
1242
|
debug$1('Unset player');
|
|
1119
1243
|
playerRef.current = null;
|
|
1120
1244
|
}
|
|
1121
1245
|
}, []); // Detect iframe switch and destroy player
|
|
1122
1246
|
|
|
1123
|
-
useEffect(
|
|
1124
|
-
|
|
1247
|
+
useEffect(() => {
|
|
1248
|
+
const {
|
|
1249
|
+
current: currentPlayer
|
|
1250
|
+
} = playerRef;
|
|
1125
1251
|
|
|
1126
1252
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1127
1253
|
debug$1('iFrame switched');
|
|
@@ -1129,20 +1255,23 @@ function useYouTubePlayer(id) {
|
|
|
1129
1255
|
}
|
|
1130
1256
|
}, [elementHasChanged]); // Create player
|
|
1131
1257
|
|
|
1132
|
-
useEffect(
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1258
|
+
useEffect(() => {
|
|
1259
|
+
const {
|
|
1260
|
+
current: YT = null
|
|
1261
|
+
} = apiRef;
|
|
1262
|
+
const {
|
|
1263
|
+
current: currentPlayer = null
|
|
1264
|
+
} = playerRef;
|
|
1265
|
+
const {
|
|
1266
|
+
current: element = null
|
|
1267
|
+
} = elementRef;
|
|
1139
1268
|
|
|
1140
1269
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1141
1270
|
destroyPlayer();
|
|
1142
1271
|
return;
|
|
1143
1272
|
}
|
|
1144
1273
|
|
|
1145
|
-
|
|
1274
|
+
let player = currentPlayer;
|
|
1146
1275
|
|
|
1147
1276
|
if (player !== null && typeof player.loadVideoById !== 'undefined') {
|
|
1148
1277
|
debug$1('Switch video [ID: %s]', videoId);
|
|
@@ -1150,32 +1279,36 @@ function useYouTubePlayer(id) {
|
|
|
1150
1279
|
} else {
|
|
1151
1280
|
debug$1('Create player [ID: %s]', videoId);
|
|
1152
1281
|
|
|
1153
|
-
|
|
1154
|
-
|
|
1282
|
+
const onReady = _ref => {
|
|
1283
|
+
let {
|
|
1284
|
+
target
|
|
1285
|
+
} = _ref;
|
|
1155
1286
|
player = target;
|
|
1156
1287
|
playerRef.current = target;
|
|
1157
1288
|
setReady(true);
|
|
1158
|
-
|
|
1289
|
+
const newDuration = target.getDuration();
|
|
1159
1290
|
|
|
1160
1291
|
if (newDuration !== metadata.duration) {
|
|
1161
|
-
setMetadata(
|
|
1292
|
+
setMetadata({ ...metadata,
|
|
1162
1293
|
duration: newDuration
|
|
1163
|
-
})
|
|
1294
|
+
});
|
|
1164
1295
|
}
|
|
1165
1296
|
|
|
1166
1297
|
debug$1('onReady [ID: %s]', videoId);
|
|
1167
1298
|
};
|
|
1168
1299
|
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1300
|
+
const onStateChange = _ref2 => {
|
|
1301
|
+
let {
|
|
1302
|
+
data: state
|
|
1303
|
+
} = _ref2;
|
|
1304
|
+
const newState = {
|
|
1172
1305
|
playing: state === YT.PlayerState.PLAYING,
|
|
1173
1306
|
paused: state === YT.PlayerState.PAUSED,
|
|
1174
1307
|
ended: state === YT.PlayerState.ENDED,
|
|
1175
1308
|
buffering: state === YT.PlayerState.BUFFERING
|
|
1176
1309
|
};
|
|
1177
1310
|
setPlayState(newState);
|
|
1178
|
-
|
|
1311
|
+
let stateLabel = null;
|
|
1179
1312
|
|
|
1180
1313
|
if (newState.playing) {
|
|
1181
1314
|
stateLabel = 'playing';
|
|
@@ -1195,9 +1328,9 @@ function useYouTubePlayer(id) {
|
|
|
1195
1328
|
};
|
|
1196
1329
|
|
|
1197
1330
|
player = new YT.Player(element, {
|
|
1198
|
-
videoId
|
|
1331
|
+
videoId,
|
|
1199
1332
|
playerVars: {
|
|
1200
|
-
controls
|
|
1333
|
+
controls,
|
|
1201
1334
|
autoplay: autoplay ? 1 : 0,
|
|
1202
1335
|
mute: initialMuted,
|
|
1203
1336
|
playsinline: true,
|
|
@@ -1206,8 +1339,8 @@ function useYouTubePlayer(id) {
|
|
|
1206
1339
|
modestbranding: 1
|
|
1207
1340
|
},
|
|
1208
1341
|
events: {
|
|
1209
|
-
onReady
|
|
1210
|
-
onStateChange
|
|
1342
|
+
onReady,
|
|
1343
|
+
onStateChange
|
|
1211
1344
|
}
|
|
1212
1345
|
});
|
|
1213
1346
|
}
|
|
@@ -1215,127 +1348,88 @@ function useYouTubePlayer(id) {
|
|
|
1215
1348
|
playerRef.current = player;
|
|
1216
1349
|
playerElementRef.current = element;
|
|
1217
1350
|
}, [apiLoaded, videoId, elementHasChanged, setPlayState, setReady, setMetadata, destroyPlayer]);
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1351
|
+
const {
|
|
1352
|
+
playing
|
|
1353
|
+
} = playState;
|
|
1354
|
+
const getCurrentTime = useCallback(p => p.getCurrentTime(), []);
|
|
1355
|
+
const currentTime = usePlayerCurrentTime(playerRef.current, {
|
|
1223
1356
|
id: videoId,
|
|
1224
1357
|
disabled: !playing || timeUpdateInterval === null,
|
|
1225
1358
|
updateInterval: timeUpdateInterval,
|
|
1226
1359
|
onUpdate: customOnTimeUpdate,
|
|
1227
|
-
getCurrentTime
|
|
1360
|
+
getCurrentTime
|
|
1228
1361
|
});
|
|
1229
|
-
return
|
|
1362
|
+
return {
|
|
1230
1363
|
ref: elementRef,
|
|
1231
1364
|
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
|
-
|
|
1365
|
+
play,
|
|
1366
|
+
pause,
|
|
1367
|
+
mute,
|
|
1368
|
+
unmute,
|
|
1369
|
+
setVolume,
|
|
1370
|
+
seek,
|
|
1371
|
+
setLoop,
|
|
1372
|
+
ready,
|
|
1373
|
+
currentTime,
|
|
1374
|
+
muted,
|
|
1375
|
+
loaded: ready,
|
|
1376
|
+
...metadata,
|
|
1377
|
+
...playState
|
|
1378
|
+
};
|
|
1244
1379
|
}
|
|
1245
1380
|
|
|
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];
|
|
1381
|
+
const debug = createDebug('folklore:video:vimeo');
|
|
1382
|
+
const NO_PLAYER_ERROR = new Error('No player');
|
|
1383
|
+
|
|
1384
|
+
const useVimeoPlayer = function (id) {
|
|
1385
|
+
let {
|
|
1386
|
+
width = 0,
|
|
1387
|
+
height = 0,
|
|
1388
|
+
duration = 0,
|
|
1389
|
+
autoplay = false,
|
|
1390
|
+
autopause = true,
|
|
1391
|
+
byline = false,
|
|
1392
|
+
controls = false,
|
|
1393
|
+
initialMuted = false,
|
|
1394
|
+
timeUpdateInterval = 1000,
|
|
1395
|
+
onTimeUpdate: customOnTimeUpdate = null,
|
|
1396
|
+
getVideoId = url => {
|
|
1397
|
+
if (url === null || url.match(/^[0-9]+$/) !== null) {
|
|
1398
|
+
return url;
|
|
1399
|
+
}
|
|
1312
1400
|
|
|
1313
|
-
|
|
1401
|
+
const match = url.match(/\/[0-9]+/);
|
|
1402
|
+
return match !== null ? match[1] : null;
|
|
1403
|
+
}
|
|
1404
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1405
|
+
const [apiLoaded, setApiLoaded] = useState(false);
|
|
1406
|
+
const apiRef = useRef(null);
|
|
1407
|
+
const elementRef = useRef(null);
|
|
1408
|
+
const playerRef = useRef(null);
|
|
1409
|
+
const playerElementRef = useRef(elementRef.current);
|
|
1410
|
+
const elementHasChanged = elementRef.current !== playerElementRef.current;
|
|
1411
|
+
const videoId = useMemo(() => getVideoId(id), [id]);
|
|
1412
|
+
const [ready, setReady] = useState(false);
|
|
1413
|
+
const [loaded, setLoaded] = useState(false);
|
|
1414
|
+
const [volume, setVolumeState] = useState(initialMuted ? 0 : 1);
|
|
1415
|
+
const [playState, setPlayState] = useState({
|
|
1314
1416
|
playing: false,
|
|
1315
1417
|
paused: false,
|
|
1316
1418
|
ended: false,
|
|
1317
1419
|
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
|
-
|
|
1420
|
+
});
|
|
1421
|
+
const [metadata, setMetadata] = useState({
|
|
1422
|
+
width,
|
|
1423
|
+
height,
|
|
1424
|
+
duration
|
|
1425
|
+
}); // Load SDK
|
|
1332
1426
|
|
|
1333
|
-
useEffect(
|
|
1334
|
-
|
|
1427
|
+
useEffect(() => {
|
|
1428
|
+
let canceled = false;
|
|
1335
1429
|
|
|
1336
1430
|
if (!apiLoaded && id !== null) {
|
|
1337
1431
|
debug('Load API');
|
|
1338
|
-
loadVimeo().then(
|
|
1432
|
+
loadVimeo().then(api => {
|
|
1339
1433
|
if (!canceled) {
|
|
1340
1434
|
apiRef.current = api;
|
|
1341
1435
|
setApiLoaded(true);
|
|
@@ -1344,40 +1438,56 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1344
1438
|
});
|
|
1345
1439
|
}
|
|
1346
1440
|
|
|
1347
|
-
return
|
|
1441
|
+
return () => {
|
|
1348
1442
|
canceled = true;
|
|
1349
1443
|
};
|
|
1350
1444
|
}, [id]);
|
|
1351
|
-
|
|
1352
|
-
|
|
1445
|
+
const play = useCallback(() => {
|
|
1446
|
+
const {
|
|
1447
|
+
current: player
|
|
1448
|
+
} = playerRef;
|
|
1353
1449
|
return player !== null ? player.play() : Promise.reject(NO_PLAYER_ERROR);
|
|
1354
1450
|
}, []);
|
|
1355
|
-
|
|
1356
|
-
|
|
1451
|
+
const pause = useCallback(() => {
|
|
1452
|
+
const {
|
|
1453
|
+
current: player
|
|
1454
|
+
} = playerRef;
|
|
1357
1455
|
return player !== null ? player.pause() : Promise.reject(NO_PLAYER_ERROR);
|
|
1358
1456
|
}, []);
|
|
1359
|
-
|
|
1360
|
-
|
|
1457
|
+
const setVolume = useCallback(newVolume => {
|
|
1458
|
+
const {
|
|
1459
|
+
current: player
|
|
1460
|
+
} = playerRef;
|
|
1361
1461
|
return player !== null ? player.setVolume(newVolume) : Promise.reject(NO_PLAYER_ERROR);
|
|
1362
1462
|
}, []);
|
|
1363
|
-
|
|
1364
|
-
|
|
1463
|
+
const mute = useCallback(() => {
|
|
1464
|
+
const {
|
|
1465
|
+
current: player
|
|
1466
|
+
} = playerRef;
|
|
1365
1467
|
return player !== null ? player.setVolume(0) : Promise.reject(NO_PLAYER_ERROR);
|
|
1366
1468
|
}, []);
|
|
1367
|
-
|
|
1368
|
-
|
|
1469
|
+
const unmute = useCallback(() => {
|
|
1470
|
+
const {
|
|
1471
|
+
current: player
|
|
1472
|
+
} = playerRef;
|
|
1369
1473
|
return player !== null ? player.setVolume(1) : Promise.reject(NO_PLAYER_ERROR);
|
|
1370
1474
|
}, []);
|
|
1371
|
-
|
|
1372
|
-
|
|
1475
|
+
const seek = useCallback(time => {
|
|
1476
|
+
const {
|
|
1477
|
+
current: player
|
|
1478
|
+
} = playerRef;
|
|
1373
1479
|
return player !== null ? player.setCurrentTime(time) : Promise.reject(NO_PLAYER_ERROR);
|
|
1374
1480
|
}, []);
|
|
1375
|
-
|
|
1376
|
-
|
|
1481
|
+
const setLoop = useCallback(loop => {
|
|
1482
|
+
const {
|
|
1483
|
+
current: player
|
|
1484
|
+
} = playerRef;
|
|
1377
1485
|
return player !== null ? player.setLoop(loop) : Promise.reject(NO_PLAYER_ERROR);
|
|
1378
1486
|
}, []);
|
|
1379
|
-
|
|
1380
|
-
|
|
1487
|
+
const destroyVideo = useCallback(() => {
|
|
1488
|
+
const {
|
|
1489
|
+
current: player
|
|
1490
|
+
} = playerRef;
|
|
1381
1491
|
|
|
1382
1492
|
if (player !== null) {
|
|
1383
1493
|
debug('Unload video');
|
|
@@ -1389,8 +1499,10 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1389
1499
|
playerRef.current = null;
|
|
1390
1500
|
}
|
|
1391
1501
|
}, []);
|
|
1392
|
-
useEffect(
|
|
1393
|
-
|
|
1502
|
+
useEffect(() => {
|
|
1503
|
+
const {
|
|
1504
|
+
current: currentPlayer
|
|
1505
|
+
} = playerRef;
|
|
1394
1506
|
|
|
1395
1507
|
if (playerElementRef.current !== elementRef.current && currentPlayer !== null) {
|
|
1396
1508
|
debug('iFrame switched');
|
|
@@ -1398,40 +1510,41 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1398
1510
|
}
|
|
1399
1511
|
}, [elementHasChanged]); // Create player
|
|
1400
1512
|
|
|
1401
|
-
useEffect(
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1513
|
+
useEffect(() => {
|
|
1514
|
+
const {
|
|
1515
|
+
current: Player = null
|
|
1516
|
+
} = apiRef;
|
|
1517
|
+
const {
|
|
1518
|
+
current: currentPlayer = null
|
|
1519
|
+
} = playerRef;
|
|
1520
|
+
const {
|
|
1521
|
+
current: element = null
|
|
1522
|
+
} = elementRef;
|
|
1408
1523
|
|
|
1409
1524
|
if (!apiLoaded || videoId === null || element === null) {
|
|
1410
1525
|
destroyVideo();
|
|
1411
1526
|
return;
|
|
1412
1527
|
}
|
|
1413
1528
|
|
|
1414
|
-
|
|
1529
|
+
let player = currentPlayer;
|
|
1415
1530
|
|
|
1416
1531
|
if (player === null) {
|
|
1417
1532
|
debug('Create player [ID: %s]', videoId);
|
|
1418
1533
|
player = new Player(element, {
|
|
1419
1534
|
id: videoId,
|
|
1420
|
-
autoplay
|
|
1421
|
-
autopause
|
|
1422
|
-
byline
|
|
1423
|
-
controls
|
|
1535
|
+
autoplay,
|
|
1536
|
+
autopause,
|
|
1537
|
+
byline,
|
|
1538
|
+
controls,
|
|
1424
1539
|
muted: initialMuted,
|
|
1425
1540
|
background: !controls
|
|
1426
1541
|
});
|
|
1427
|
-
player.ready().then(
|
|
1428
|
-
return setReady(true);
|
|
1429
|
-
}).catch(function (e) {
|
|
1542
|
+
player.ready().then(() => setReady(true)).catch(e => {
|
|
1430
1543
|
debug('ERROR: %o', e);
|
|
1431
1544
|
});
|
|
1432
1545
|
} else {
|
|
1433
1546
|
debug('Load video [ID: %s]', videoId);
|
|
1434
|
-
player.loadVideo(videoId).catch(
|
|
1547
|
+
player.loadVideo(videoId).catch(e => {
|
|
1435
1548
|
debug('ERROR: %o', e);
|
|
1436
1549
|
});
|
|
1437
1550
|
}
|
|
@@ -1440,28 +1553,26 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1440
1553
|
playerElementRef.current = element;
|
|
1441
1554
|
}, [apiLoaded, videoId, elementHasChanged, setReady, destroyVideo, setLoaded]); // Bind player events
|
|
1442
1555
|
|
|
1443
|
-
useEffect(
|
|
1444
|
-
|
|
1556
|
+
useEffect(() => {
|
|
1557
|
+
const {
|
|
1558
|
+
current: player
|
|
1559
|
+
} = playerRef;
|
|
1445
1560
|
|
|
1446
1561
|
if (player === null) {
|
|
1447
|
-
return
|
|
1562
|
+
return () => {};
|
|
1448
1563
|
}
|
|
1449
1564
|
|
|
1450
|
-
|
|
1565
|
+
let canceled = false;
|
|
1451
1566
|
|
|
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];
|
|
1567
|
+
const onLoaded = () => {
|
|
1568
|
+
Promise.all([player.getDuration(), player.getVideoWidth(), player.getVideoHeight(), player.getMuted()]).then(_ref => {
|
|
1569
|
+
let [newDuration, newWidth, newHeight, newMuted] = _ref;
|
|
1459
1570
|
|
|
1460
1571
|
if (canceled) {
|
|
1461
1572
|
return;
|
|
1462
1573
|
}
|
|
1463
1574
|
|
|
1464
|
-
|
|
1575
|
+
const newMetadata = {
|
|
1465
1576
|
duration: newDuration,
|
|
1466
1577
|
width: newWidth,
|
|
1467
1578
|
height: newHeight
|
|
@@ -1477,7 +1588,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1477
1588
|
debug('onLoaded [ID: %s]', videoId);
|
|
1478
1589
|
};
|
|
1479
1590
|
|
|
1480
|
-
|
|
1591
|
+
const onPlay = () => {
|
|
1481
1592
|
setPlayState({
|
|
1482
1593
|
playing: true,
|
|
1483
1594
|
paused: false,
|
|
@@ -1485,14 +1596,14 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1485
1596
|
buffering: false
|
|
1486
1597
|
});
|
|
1487
1598
|
debug('onPlay [ID: %s]', videoId);
|
|
1488
|
-
player.getMuted().then(
|
|
1599
|
+
player.getMuted().then(newMuted => {
|
|
1489
1600
|
if (!canceled && newMuted) {
|
|
1490
1601
|
setVolumeState(0);
|
|
1491
1602
|
}
|
|
1492
|
-
}).catch(
|
|
1603
|
+
}).catch(() => {});
|
|
1493
1604
|
};
|
|
1494
1605
|
|
|
1495
|
-
|
|
1606
|
+
const onPause = () => {
|
|
1496
1607
|
setPlayState({
|
|
1497
1608
|
playing: false,
|
|
1498
1609
|
paused: true,
|
|
@@ -1502,13 +1613,15 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1502
1613
|
debug('onPause [ID: %s]', videoId);
|
|
1503
1614
|
};
|
|
1504
1615
|
|
|
1505
|
-
|
|
1506
|
-
|
|
1616
|
+
const onVolumeChange = _ref2 => {
|
|
1617
|
+
let {
|
|
1618
|
+
volume: newVolume
|
|
1619
|
+
} = _ref2;
|
|
1507
1620
|
setVolumeState(newVolume);
|
|
1508
1621
|
debug('onVolumeChange [ID: %s]', videoId);
|
|
1509
1622
|
};
|
|
1510
1623
|
|
|
1511
|
-
|
|
1624
|
+
const onEnded = () => {
|
|
1512
1625
|
setPlayState({
|
|
1513
1626
|
playing: false,
|
|
1514
1627
|
paused: false,
|
|
@@ -1518,7 +1631,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1518
1631
|
debug('onEnded [ID: %s]', videoId);
|
|
1519
1632
|
};
|
|
1520
1633
|
|
|
1521
|
-
|
|
1634
|
+
const onBufferStart = () => {
|
|
1522
1635
|
setPlayState({
|
|
1523
1636
|
playing: true,
|
|
1524
1637
|
paused: false,
|
|
@@ -1528,7 +1641,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1528
1641
|
debug('onBufferStart [ID: %s]', videoId);
|
|
1529
1642
|
};
|
|
1530
1643
|
|
|
1531
|
-
|
|
1644
|
+
const onBufferEnded = () => {
|
|
1532
1645
|
setPlayState({
|
|
1533
1646
|
playing: true,
|
|
1534
1647
|
paused: false,
|
|
@@ -1546,7 +1659,7 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1546
1659
|
player.on('ended', onEnded);
|
|
1547
1660
|
player.on('bufferstart', onBufferStart);
|
|
1548
1661
|
player.on('bufferend', onBufferEnded);
|
|
1549
|
-
return
|
|
1662
|
+
return () => {
|
|
1550
1663
|
canceled = true;
|
|
1551
1664
|
debug('Unbind events [ID: %s]', videoId);
|
|
1552
1665
|
player.off('loaded', onLoaded);
|
|
@@ -1558,63 +1671,55 @@ var useVimeoPlayer = function useVimeoPlayer(id) {
|
|
|
1558
1671
|
player.off('bufferend', onBufferEnded);
|
|
1559
1672
|
};
|
|
1560
1673
|
}, [videoId, playerRef.current, setPlayState, setMetadata, setVolumeState, setLoaded]);
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1674
|
+
const {
|
|
1675
|
+
playing
|
|
1676
|
+
} = playState;
|
|
1677
|
+
const getCurrentTime = useCallback(p => p.getCurrentTime(), []);
|
|
1678
|
+
const currentTime = usePlayerCurrentTime(playerRef.current, {
|
|
1566
1679
|
id: videoId,
|
|
1567
1680
|
disabled: !playing || timeUpdateInterval === null,
|
|
1568
1681
|
updateInterval: timeUpdateInterval,
|
|
1569
1682
|
onUpdate: customOnTimeUpdate,
|
|
1570
|
-
getCurrentTime
|
|
1683
|
+
getCurrentTime
|
|
1571
1684
|
});
|
|
1572
|
-
return
|
|
1685
|
+
return {
|
|
1573
1686
|
ref: elementRef,
|
|
1574
1687
|
player: playerRef.current,
|
|
1575
|
-
play
|
|
1576
|
-
pause
|
|
1577
|
-
mute
|
|
1578
|
-
unmute
|
|
1579
|
-
setVolume
|
|
1580
|
-
seek
|
|
1581
|
-
setLoop
|
|
1582
|
-
ready
|
|
1583
|
-
currentTime
|
|
1584
|
-
loaded
|
|
1688
|
+
play,
|
|
1689
|
+
pause,
|
|
1690
|
+
mute,
|
|
1691
|
+
unmute,
|
|
1692
|
+
setVolume,
|
|
1693
|
+
seek,
|
|
1694
|
+
setLoop,
|
|
1695
|
+
ready,
|
|
1696
|
+
currentTime,
|
|
1697
|
+
loaded,
|
|
1585
1698
|
muted: volume === 0,
|
|
1586
|
-
volume
|
|
1587
|
-
|
|
1699
|
+
volume,
|
|
1700
|
+
...metadata,
|
|
1701
|
+
...playState
|
|
1702
|
+
};
|
|
1588
1703
|
};
|
|
1589
1704
|
|
|
1590
1705
|
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;
|
|
1706
|
+
const {
|
|
1707
|
+
service = null,
|
|
1708
|
+
videoId = null,
|
|
1709
|
+
url = null,
|
|
1710
|
+
onLoaded: customOnLoaded = null,
|
|
1711
|
+
onPlay: customOnPlay = null,
|
|
1712
|
+
onPause: customOnPause = null,
|
|
1713
|
+
onEnd: customOnEnd = null,
|
|
1714
|
+
onMetadataChange: customOnMetadataChange = null,
|
|
1715
|
+
onBufferStart: customOnBufferStart = null,
|
|
1716
|
+
onBufferEnded: customOnBufferEnded = null
|
|
1717
|
+
} = params || {};
|
|
1718
|
+
const dailymotionPlayer = useDailymotionPlayer(service === 'dailymotion' ? videoId || url : null, params);
|
|
1719
|
+
const youtubePlayer = useYouTubePlayer(service === 'youtube' ? videoId || url : null, params);
|
|
1720
|
+
const vimeoPlayer = useVimeoPlayer(service === 'vimeo' ? videoId || url : null, params);
|
|
1721
|
+
const nativePlayer = useNativeVideoPlayer(url, params);
|
|
1722
|
+
let player = null;
|
|
1618
1723
|
|
|
1619
1724
|
if (service === 'dailymotion') {
|
|
1620
1725
|
player = dailymotionPlayer;
|
|
@@ -1626,53 +1731,45 @@ function useVideoPlayer(params) {
|
|
|
1626
1731
|
player = nativePlayer;
|
|
1627
1732
|
}
|
|
1628
1733
|
|
|
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 () {
|
|
1734
|
+
const {
|
|
1735
|
+
playing = false,
|
|
1736
|
+
paused = false,
|
|
1737
|
+
buffering = false,
|
|
1738
|
+
ended = false,
|
|
1739
|
+
ready = false,
|
|
1740
|
+
width: metaWidth = null,
|
|
1741
|
+
height: metaHeight = null,
|
|
1742
|
+
duration: metaDuration = null
|
|
1743
|
+
} = player || {};
|
|
1744
|
+
useEffect(() => {
|
|
1648
1745
|
if (ready && customOnLoaded !== null) {
|
|
1649
1746
|
customOnLoaded();
|
|
1650
1747
|
}
|
|
1651
1748
|
}, [ready, customOnLoaded]);
|
|
1652
|
-
useEffect(
|
|
1749
|
+
useEffect(() => {
|
|
1653
1750
|
if (playing && customOnPlay !== null) {
|
|
1654
1751
|
customOnPlay();
|
|
1655
1752
|
}
|
|
1656
1753
|
}, [playing, customOnPlay]);
|
|
1657
|
-
useEffect(
|
|
1754
|
+
useEffect(() => {
|
|
1658
1755
|
if (paused && customOnPause !== null) {
|
|
1659
1756
|
customOnPause();
|
|
1660
1757
|
}
|
|
1661
1758
|
}, [paused, customOnPause]);
|
|
1662
|
-
useEffect(
|
|
1759
|
+
useEffect(() => {
|
|
1663
1760
|
if (buffering && customOnBufferStart !== null) {
|
|
1664
1761
|
customOnBufferStart();
|
|
1665
1762
|
} else if (!buffering && customOnBufferEnded !== null) {
|
|
1666
1763
|
customOnBufferEnded();
|
|
1667
1764
|
}
|
|
1668
1765
|
}, [buffering, customOnBufferStart, customOnBufferEnded]);
|
|
1669
|
-
useEffect(
|
|
1766
|
+
useEffect(() => {
|
|
1670
1767
|
if (ended && customOnEnd !== null) {
|
|
1671
1768
|
customOnEnd();
|
|
1672
1769
|
}
|
|
1673
1770
|
}, [ended, customOnEnd]);
|
|
1674
|
-
useEffect(
|
|
1675
|
-
|
|
1771
|
+
useEffect(() => {
|
|
1772
|
+
const hasMetadata = metaWidth !== null || metaHeight !== null || metaDuration !== null;
|
|
1676
1773
|
|
|
1677
1774
|
if (hasMetadata && customOnMetadataChange !== null) {
|
|
1678
1775
|
customOnMetadataChange({
|
|
@@ -1685,27 +1782,21 @@ function useVideoPlayer(params) {
|
|
|
1685
1782
|
return player;
|
|
1686
1783
|
}
|
|
1687
1784
|
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
};
|
|
1693
|
-
};
|
|
1694
|
-
|
|
1695
|
-
var currentScroll = getWindowScroll();
|
|
1785
|
+
const getWindowScroll = () => ({
|
|
1786
|
+
x: typeof window !== 'undefined' ? window.scrollX || 0 : 0,
|
|
1787
|
+
y: typeof window !== 'undefined' ? window.scrollY || 0 : 0
|
|
1788
|
+
});
|
|
1696
1789
|
|
|
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;
|
|
1790
|
+
const currentScroll = getWindowScroll();
|
|
1701
1791
|
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1792
|
+
const useWindowScroll = function () {
|
|
1793
|
+
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1794
|
+
const {
|
|
1795
|
+
onChange = null
|
|
1796
|
+
} = opts;
|
|
1797
|
+
const [scroll, setScroll] = useState(currentScroll);
|
|
1798
|
+
const updateScroll = useCallback(() => {
|
|
1799
|
+
const newScroll = getWindowScroll();
|
|
1709
1800
|
|
|
1710
1801
|
if (currentScroll.x !== newScroll.x || currentScroll.y !== newScroll.y) {
|
|
1711
1802
|
currentScroll.x = newScroll.x;
|
|
@@ -1716,40 +1807,33 @@ var useWindowScroll = function useWindowScroll() {
|
|
|
1716
1807
|
|
|
1717
1808
|
return null;
|
|
1718
1809
|
}, [setScroll]);
|
|
1719
|
-
|
|
1720
|
-
|
|
1810
|
+
const onScroll = useCallback(() => {
|
|
1811
|
+
const newScroll = updateScroll();
|
|
1721
1812
|
|
|
1722
1813
|
if (newScroll !== null && onChange !== null) {
|
|
1723
1814
|
onChange(newScroll);
|
|
1724
1815
|
}
|
|
1725
1816
|
}, [updateScroll, onChange]);
|
|
1726
1817
|
useWindowEvent('scroll', onScroll);
|
|
1727
|
-
useEffect(
|
|
1818
|
+
useEffect(() => {
|
|
1728
1819
|
onScroll();
|
|
1729
1820
|
}, []);
|
|
1730
1821
|
return scroll;
|
|
1731
1822
|
};
|
|
1732
1823
|
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
};
|
|
1739
|
-
var currentSize = getWindowSize();
|
|
1824
|
+
const getWindowSize = () => ({
|
|
1825
|
+
width: typeof window !== 'undefined' ? window.innerWidth || 0 : 0,
|
|
1826
|
+
height: typeof window !== 'undefined' ? window.innerHeight || 0 : 0
|
|
1827
|
+
});
|
|
1828
|
+
let currentSize = getWindowSize();
|
|
1740
1829
|
|
|
1741
1830
|
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();
|
|
1831
|
+
let {
|
|
1832
|
+
onChange = null
|
|
1833
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1834
|
+
const [size, setSize] = useState(currentSize);
|
|
1835
|
+
const updateSize = useCallback(() => {
|
|
1836
|
+
const newSize = getWindowSize();
|
|
1753
1837
|
|
|
1754
1838
|
if (currentSize.width !== newSize.width || currentSize.height !== newSize.height) {
|
|
1755
1839
|
currentSize = newSize;
|
|
@@ -1759,18 +1843,18 @@ function useWindowSize() {
|
|
|
1759
1843
|
|
|
1760
1844
|
return null;
|
|
1761
1845
|
}, [setSize]);
|
|
1762
|
-
|
|
1763
|
-
|
|
1846
|
+
const onResize = useCallback(() => {
|
|
1847
|
+
const newSize = updateSize();
|
|
1764
1848
|
|
|
1765
1849
|
if (newSize !== null && onChange !== null) {
|
|
1766
1850
|
onChange(newSize);
|
|
1767
1851
|
}
|
|
1768
1852
|
}, [onChange]);
|
|
1769
1853
|
useWindowEvent('resize', onResize);
|
|
1770
|
-
useEffect(
|
|
1854
|
+
useEffect(() => {
|
|
1771
1855
|
onResize();
|
|
1772
1856
|
}, []);
|
|
1773
1857
|
return size;
|
|
1774
1858
|
}
|
|
1775
1859
|
|
|
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 };
|
|
1860
|
+
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 };
|