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