forked from Kagami/mpv.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (102 loc) · 2.86 KB
/
index.js
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
/**
* Corresponding JS part of mpv pepper plugin.
* @module mpv.js
*/
const React = require('react');
const PropTypes = require('prop-types');
const PLUGIN_MIME_TYPE = 'application/x-mpv';
class MPV extends React.PureComponent {
command(cmd, ...args) {
args = args.map((arg) => arg.toString());
this._postData('command', [cmd].concat(args));
}
property(name, value) {
const data = { name, value };
this._postData('set_property', data);
}
observe(name) {
this._postData('observe_property', name);
}
keypress({ key, shiftKey, ctrlKey, altKey }) {
if (['Escape', 'Shift', 'Control', 'Alt', 'Compose', 'CapsLock', 'Meta'].includes(key))
return;
if (key.startsWith('Arrow')) {
key = key.slice(5).toUpperCase();
if (shiftKey) {
key = `Shift+${key}`;
}
}
if (ctrlKey) {
key = `Ctrl+${key}`;
}
if (altKey) {
key = `Alt+${key}`;
}
if (
[
'q',
'Q',
'ESC',
'POWER',
'STOP',
'CLOSE_WIN',
'CLOSE_WIN',
'Ctrl+c',
'AR_PLAY_HOLD',
'AR_CENTER_HOLD',
].includes(key)
)
return;
this.command('keypress', key);
}
fullscreen() {
this.node().webkitRequestFullscreen();
}
destroy() {
this.node().remove();
}
node() {
return this.plugin;
}
constructor(props) {
super(props);
this.plugin = null;
}
_postData(type, data) {
const msg = { type, data };
this.node().postMessage(msg);
}
_handleMessage(e) {
const msg = e.data;
const { type, data } = msg;
if (type === 'property_change' && this.props.onPropertyChange) {
const { name, value } = data;
this.props.onPropertyChange(name, value);
} else if (type === 'ready' && this.props.onReady) {
this.props.onReady(this);
}
}
componentDidMount() {
this.node().addEventListener('message', this._handleMessage.bind(this));
}
render() {
const defaultStyle = { display: 'block', width: '100%', height: '100%' };
const props = Object.assign({}, this.props, {
ref: (el) => {
this.plugin = el;
},
type: PLUGIN_MIME_TYPE,
style: Object.assign(defaultStyle, this.props.style),
});
delete props.onReady;
delete props.onPropertyChange;
return React.createElement('embed', props);
}
}
MPV.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
onReady: PropTypes.func,
onPropertyChange: PropTypes.func,
};
module.exports = MPV;