-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipc.js
69 lines (55 loc) · 1.87 KB
/
ipc.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
"use strict";
const {remote, ipcRenderer} = require("electron");
const EVENT_CLOSE_PROJECT = "atom-mocha:close-project";
const EVENT_JUMP_TO_FILE = "atom-mocha:jump-to-file";
class IPC{
init(projectPath){
global.AtomMocha = {};
this.projectPath = projectPath;
this.injectHooks();
}
jumpToFile(path, row, column){
ipcRenderer.send(EVENT_JUMP_TO_FILE, path, row, column);
}
/**
* Install the `jumpToFile` command in project's window.
*
* NB: There's probably a better way to implement this.
* @private
*/
injectHooks(){
const path = this.projectPath.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
const code = this.getInjectedCode(path);
for(const item of remote.webContents.getAllWebContents())
if("window" === item.getType())
item.executeJavaScript(code);
}
getInjectedCode(path){
return "if(global.atom && null == global.AtomMocha && " +
`-1 !== global.atom.project.getPaths().indexOf("${ path }")){
const {remote, ipcRenderer} = require("electron");
const {Range} = require("atom");
const AtomMocha = {
handleJump(event, ...args){
AtomMocha.jumpToFile(...args);
},
jumpToFile(file, row, col){
atom.project.contains(file) && atom.workspace.open(file).then(editor => {
const cursor = editor.getLastCursor();
const offset = Math.floor(editor.rowsPerPage / 2);
cursor.setBufferPosition([row, col], {autoscroll: false});
editor.scrollToScreenRange(new Range([row - offset, col], [row + offset, col]));
atom.focus();
});
}
};
global.AtomMocha = AtomMocha;
remote.ipcMain.on("${ EVENT_JUMP_TO_FILE }", AtomMocha.handleJump);
window.addEventListener("beforeunload", () => {
ipcRenderer.send("${ EVENT_CLOSE_PROJECT }", "${ path }");
remote.ipcMain.removeListener("${ EVENT_JUMP_TO_FILE }", AtomMocha.handleJump);
});
}`;
}
}
module.exports = new IPC();