-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
138 lines (108 loc) · 2.99 KB
/
main.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
'use strict';
const {app, BrowserWindow, ipcMain, globalShortcut, clipboard} = require('electron');
const path = require('path');
const url = require('url');
// const db = require('./mongohandler');
const db = require('./nehandler');
let mainWindow = null;
let snipWindow = null;
app.on('ready', function () {
const screen = require('electron').screen;
registerShortcut();
const {width, height} = screen.getPrimaryDisplay().workAreaSize
mainWindow = new BrowserWindow({width, height});
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'public_static', 'index.html'),
protocol: 'file:',
slashes: true
}));
// mainWindow.webContents.openDevTools()
require('./menu')
});
function registerShortcut() {
const ret = globalShortcut.register('CommandOrControl+N', () => {
newSnip();
});
if (!ret) {
console.log('registration failed')
}
// Check whether a shortcut is registered.
console.log(globalShortcut.isRegistered('CommandOrControl+N'))
}
function newSnip() {
snipWindow = new BrowserWindow({
height: 578,
width: 800,
frame: false,
});
snipWindow.loadURL(url.format({
pathname: path.join(__dirname, 'public_static', 'snip.html'),
protocol: 'file:',
slashes: true
}));
}
function sendAllSnips() {
db.allSnips(function (snips) {
let result = [];
for (let i = 0; i < snips.length; i++) {
result.push({
title: snips[i].title,
language: snips[i].language,
id: snips[i]._id.toString(),
code: snips[i].code
})
}
mainWindow.webContents.send('all-snips', result);
})
}
/* IPC's */
ipcMain.on('new-snip', function (event, arg) {
newSnip();
});
ipcMain.on('get-snips', function () {
sendAllSnips()
});
ipcMain.on('delete-snip', function (event, arg) {
db.deleteSnip(arg, function () {
sendAllSnips()
})
});
ipcMain.on('edit-snip', function (event, arg) {
db.findSnip(arg, function (result) {
editSnip(result)
})
});
ipcMain.on('close-snip-win', function (event, arg) {
if (snipWindow) {
snipWindow.close();
}
});
ipcMain.on('new-snip-add', function (event, arg) {
let snip = JSON.parse(arg);
if (snip.id) {
db.updateSnip(snip.id, {
title: snip.title,
language: snip.language,
code: snip.code
}, function () {
sendAllSnips();
});
}
else {
db.insertSnip(snip, function () {
sendAllSnips();
if (snipWindow) {
snipWindow.close();
}
})
}
});
ipcMain.on('copy-to-clip', function (event, code) {
clipboard.writeText(code);
});
ipcMain.on('search-snip', function (event, arg) {
db.searchSnip(arg,function (result) {
mainWindow.webContents.send('all-snips', result);
})
})
module.exports = {sendAllSnips, newSnip}