1 | "use strict";
|
2 |
|
3 | const {remote, ipcRenderer} = require("electron");
|
4 | const EVENT_CLOSE_PROJECT = "atom-mocha:close-project";
|
5 | const EVENT_JUMP_TO_FILE = "atom-mocha:jump-to-file";
|
6 |
|
7 |
|
8 | class IPC{
|
9 |
|
10 | init(projectPath){
|
11 | global.AtomMocha = {};
|
12 | this.projectPath = projectPath;
|
13 | this.injectHooks();
|
14 | }
|
15 |
|
16 |
|
17 | jumpToFile(path, row, column){
|
18 | ipcRenderer.send(EVENT_JUMP_TO_FILE, path, row, column);
|
19 | }
|
20 |
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | injectHooks(){
|
29 | const path = this.projectPath.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
30 | const code = this.getInjectedCode(path);
|
31 |
|
32 | for(const item of remote.webContents.getAllWebContents())
|
33 | if("window" === item.getType())
|
34 | item.executeJavaScript(code);
|
35 | }
|
36 |
|
37 |
|
38 | getInjectedCode(path){
|
39 | return "if(global.atom && null == global.AtomMocha && " +
|
40 | `-1 !== global.atom.project.getPaths().indexOf("${ path }")){
|
41 | const {remote, ipcRenderer} = require("electron");
|
42 | const {Range} = require("atom");
|
43 |
|
44 | const AtomMocha = {
|
45 | handleJump(event, ...args){
|
46 | AtomMocha.jumpToFile(...args);
|
47 | },
|
48 | jumpToFile(file, row, col){
|
49 | atom.project.contains(file) && atom.workspace.open(file).then(editor => {
|
50 | const cursor = editor.getLastCursor();
|
51 | const offset = Math.floor(editor.rowsPerPage / 2);
|
52 | cursor.setBufferPosition([row, col], {autoscroll: false});
|
53 | editor.scrollToScreenRange(new Range([row - offset, col], [row + offset, col]));
|
54 | atom.focus();
|
55 | });
|
56 | }
|
57 | };
|
58 |
|
59 | global.AtomMocha = AtomMocha;
|
60 | remote.ipcMain.on("${ EVENT_JUMP_TO_FILE }", AtomMocha.handleJump);
|
61 | window.addEventListener("beforeunload", () => {
|
62 | ipcRenderer.send("${ EVENT_CLOSE_PROJECT }", "${ path }");
|
63 | remote.ipcMain.removeListener("${ EVENT_JUMP_TO_FILE }", AtomMocha.handleJump);
|
64 | });
|
65 | }`;
|
66 | }
|
67 | }
|
68 |
|
69 | module.exports = new IPC();
|