-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
218 lines (200 loc) · 5.28 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Corresponding JS part of mpv pepper plugin.
* @module mpv.js
*/
"use strict";
const path = require("path");
const React = require("react");
const PropTypes = require("prop-types");
/**
* The MIME type associated with mpv.js plugin.
*/
const PLUGIN_MIME_TYPE = "application/x-mpvjs";
function containsNonASCII(str) {
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 255) {
return true;
}
}
return false;
}
/**
* Return value to be passed to `register-pepper-plugins` switch.
*
* @param {string} pluginDir - Plugin directory
* @param {string} [pluginName=mpvjs.node] - Plugin name
* @throws {Error} Resulting path contains non-ASCII characters.
*/
function getPluginEntry(pluginDir, pluginName = "mpvjs.node") {
const fullPluginPath = path.join(pluginDir, pluginName);
// Try relative path to workaround ASCII-only path restriction.
let pluginPath = path.relative(process.cwd(), fullPluginPath);
if (path.dirname(pluginPath) === ".") {
// "./plugin" is required only on Linux.
if (process.platform === "linux") {
pluginPath = `.${path.sep}${pluginPath}`;
}
} else {
// Relative plugin paths doesn't work reliably on Windows, see
// <https://github.com/Kagami/mpv.js/issues/9>.
if (process.platform === "win32") {
pluginPath = fullPluginPath;
}
}
if (containsNonASCII(pluginPath)) {
if (containsNonASCII(fullPluginPath)) {
throw new Error("Non-ASCII plugin path is not supported");
} else {
pluginPath = fullPluginPath;
}
}
return `${pluginPath};${PLUGIN_MIME_TYPE}`;
}
/**
* React wrapper.
*/
class ReactMPV extends React.PureComponent {
/**
* Send a command to the player.
*
* @param {string} cmd - Command name
* @param {...*} args - Arguments
*/
command(cmd, ...args) {
args = args.map(arg => arg.toString());
this._postData("command", [cmd].concat(args));
}
/**
* Set a property to a given value.
*
* @param {string} name - Property name
* @param {*} value - Property value
*/
property(name, value) {
const data = {name, value};
this._postData("set_property", data);
}
/**
* Get a notification whenever the given property changes.
*
* @param {string} name - Property name
*/
observe(name) {
this._postData("observe_property", name);
}
/**
* Send a key event through mpv's input handler, triggering whatever
* behavior is configured to that key.
*
* @param {KeyboardEvent} event
*/
keypress({key, shiftKey, ctrlKey, altKey}) {
// Don't need modifier events.
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}`;
}
// Ignore exit keys for default keybindings settings.
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);
}
/**
* Enter fullscreen.
*/
fullscreen() {
this.node().webkitRequestFullscreen();
}
/**
* Synchronously destroy mpv instance. You might want to call this on
* quit in order to cleanup files currently being opened in mpv.
*/
destroy() {
this.node().remove();
}
/**
* Return a plugin DOM node.
*
* @return {HTMLEmbedElement}
*/
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);
}
}
/**
* Accepted properties. Other properties (not documented) are applied to
* the plugin element.
*/
ReactMPV.propTypes = {
/**
* The CSS class name of the plugin element.
*/
className: PropTypes.string,
/**
* Override the inline-styles of the plugin element.
*/
style: PropTypes.object,
/**
* Callback function that is fired when mpv is ready to accept
* commands.
*
* @param {Object} mpv - Component instance
*/
onReady: PropTypes.func,
/**
* Callback function that is fired when one of the observed properties
* changes.
*
* @param {string} name - Property name
* @param {*} value - Property value
*/
onPropertyChange: PropTypes.func,
};
module.exports = {PLUGIN_MIME_TYPE, getPluginEntry, ReactMPV};