-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·60 lines (49 loc) · 1.16 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
'use strict';
const CoMws = require('comws');
let context = new CoMws();
function run(elm, click) {
const menu = [];
return context
.run({menu, elm, click})
.then(() => menu);
}
function onContextmenu(e) {
const electron = require('electron');
const remote = electron.remote;
const Menu = remote.Menu;
const click = {x: e.x, y: e.y};
const elm = e.target;
run(elm, click)
.then(template => {
if (template.length > 0) {
e.preventDefault();
e.stopPropagation();
const menu = Menu.buildFromTemplate(template);
menu.popup(remote.getCurrentWindow(), click.x, click.y);
}
})
.catch(err => process.stderr.write(err.stack + '\n'));
}
function reset() {
context = new CoMws();
}
function use(mw) {
if (typeof mw !== 'function') {
throw new TypeError('Function middleware argument required.');
}
context.use(mw);
return mw;
}
function activate() {
if (document.body) {
document.body.addEventListener('contextmenu', onContextmenu);
} else {
document.addEventListener('DOMContentLoaded', () => {
document.body.addEventListener('contextmenu', onContextmenu);
});
}
}
module.exports = {
activate, use, reset,
__test: {run}
};