-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathindex.tsx
197 lines (183 loc) · 5.09 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as React from "react";
import type {
DependencyList,
DetailedHTMLProps,
MutableRefObject,
Ref,
VideoHTMLAttributes,
} from "react";
import PlyrJS, { Options, SourceInfo } from "plyr";
import PropTypes from "prop-types";
import useAptor, { Destroy, GetAPI, Instantiate } from "react-aptor";
export type PlyrInstance = PlyrJS;
export type PlyrOptions = Options;
export type PlyrSource = SourceInfo;
type PlyrConfigurationProps = {
source: PlyrSource | null;
options?: PlyrOptions | null;
};
type ReactVideoProps = DetailedHTMLProps<
VideoHTMLAttributes<HTMLVideoElement>,
HTMLVideoElement
>;
export type PlyrProps = Omit<ReactVideoProps, "ref"> & PlyrConfigurationProps;
export interface APITypes {
plyr: PlyrInstance;
}
/**
* It creates a new PlyrJS instance, and if a source is provided, it sets the source of the PlyrJS
* instance to the source provided
* @param node - The latest element of the DOM node which has plyr in it.
* @param params - The plyr params that are passed to the component
* @returns An instance of PlyrJS.
*/
/* REACT-APTOR */
const instantiate: Instantiate<
PlyrJS,
HTMLVideoElement,
PlyrConfigurationProps
> = (_, params) => {
// The node update of ref in react life cycle seems to have issue, used class selector instead
const plyr = new PlyrJS(".plyr-react", params?.options ?? {});
if (params?.source) plyr.source = params?.source;
return plyr;
};
/**
* It destroys the PlyrJS instance.
* @param {PlyrJS | null} plyr - PlyrJS | null
*/
const destroy: Destroy<PlyrJS, PlyrConfigurationProps> = (
plyr: PlyrJS | null
) => {
if (plyr) plyr.destroy();
};
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
/**
* It returns an object with a `plyr` property that contains the Plyr instance
* @param {PlyrJS | null} plyr - PlyrJS | null
* @returns A function that returns an object with a single property, plyr.
*/
const getAPI: GetAPI<PlyrJS, PlyrConfigurationProps> = (
plyr: PlyrJS | null
) => {
if (!plyr) {
return () =>
new Proxy({ plyr: { source: null } } as unknown as APITypes, {
get: (target, prop) => {
if (prop === "plyr") {
return target[prop];
}
return noop;
},
});
}
return () => ({
/**
* Plyr instance with all of its functionality
*/
plyr,
});
};
/**
* It creates a React hook that returns a ref to a video element that is initialized with Plyr
* @param ref - Ref<APITypes>
* @param {PlyrConfigurationProps} params - PlyrConfigurationProps,
* @param {DependencyList | null} [deps=null] - DependencyList | null = null
* @returns A function that returns a React component.
*/
export function usePlyr(
ref: Ref<APITypes>,
params: PlyrConfigurationProps,
deps: DependencyList | null = null
) {
return useAptor<PlyrInstance, HTMLVideoElement, PlyrConfigurationProps>(
ref,
{
instantiate,
getAPI,
destroy,
params,
},
deps ?? [params.options, params.source]
);
}
/* Creating a React component that is initialized with Plyr. */
const Plyr = React.forwardRef<APITypes, PlyrProps>((props, ref) => {
const { source, options = null, ...rest } = props;
const raptorRef = usePlyr(ref, {
source,
options,
}) as MutableRefObject<HTMLVideoElement>;
return <video ref={raptorRef} className="plyr-react plyr" {...rest} />;
});
/* Setting the default props and prop types for the component. */
if (__DEV__) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Plyr.displayName = "Plyr";
Plyr.defaultProps = {
options: {
controls: [
"rewind",
"play",
"fast-forward",
"progress",
"current-time",
"duration",
"mute",
"volume",
"settings",
"fullscreen",
],
i18n: {
restart: "Restart",
rewind: "Rewind {seektime}s",
play: "Play",
pause: "Pause",
fastForward: "Forward {seektime}s",
seek: "Seek",
seekLabel: "{currentTime} of {duration}",
played: "Played",
buffered: "Buffered",
currentTime: "Current time",
duration: "Duration",
volume: "Volume",
mute: "Mute",
unmute: "Unmute",
enableCaptions: "Enable captions",
disableCaptions: "Disable captions",
download: "Download",
enterFullscreen: "Enter fullscreen",
exitFullscreen: "Exit fullscreen",
frameTitle: "Player for {title}",
captions: "Captions",
settings: "Settings",
menuBack: "Go back to previous menu",
speed: "Speed",
normal: "Normal",
quality: "Quality",
loop: "Loop",
},
},
source: {
type: "video",
sources: [
{
src: "https://cdn.plyr.io/static/blank.mp4",
type: "video/mp4",
size: 720,
},
{
src: "https://cdn.plyr.io/static/blank.mp4",
type: "video/mp4",
size: 1080,
},
],
},
};
Plyr.propTypes = {
options: PropTypes.object,
source: PropTypes.any,
};
}
export default Plyr;