-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
180 lines (151 loc) · 5.37 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
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
const path = require('path');
const { app, BrowserWindow, Menu, ipcMain } = require('electron');
const { formatText, splitTextIntoLines, splitLineIntoColumns } = require('./format');
if (require('electron-squirrel-startup')) app.quit();
const isMac = process.platform === 'darwin'; // Check if platform is a Mac (darwin: mac, win32: windows, linux: linux)
process.env.NODE_ENV = 'production';
const isDev = process.env.NODE_ENV !== 'production'; // Check if we're in DEV mode
// Auto reload window on change if in DEV mode
if (isDev) {
console.log('dev mode');
require('electron-reload')(__dirname, {
electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
})
}
/*
---- FUNCTIONALITY ----
*/
// Get text form input from Home page
ipcMain.on('detectColumns', (e, args) => {
detectColumns(args.data.unformattedText, args.data.source);
});
// Detect existing columns of first line
function detectColumns(text) {
const lines = splitTextIntoLines(text);
const columns = splitLineIntoColumns(lines[0]);
// Send columns to renderer -> to select an option for each column
mainWindow.webContents.send('columnsDetected', {text, columns});
}
// Get options form input from Options page
ipcMain.on('formatText', (e, args) => {
const columnsOptions = args.data.columnsOptions.map(value => value.columnType);
let fullTextOptionsNaamLand = "";
let fullTextOptionsGemeenteWaar = "";
let fullTextOptionsSnelheidWaar = "";
let fullTextOptionsSnelheidNummers = 0;
args.data.fullTextOptions.forEach(function (item, index) {
if (item.columnType === 'naam' && item.option === 'land') fullTextOptionsNaamLand = item.selection;
if (item.columnType === 'gemeente' && item.option === 'waar') fullTextOptionsGemeenteWaar = item.selection;
if (item.columnType === 'snelheid') {
if (item.option === 'waar') fullTextOptionsSnelheidWaar = item.selection;
if (item.option === 'nummers') fullTextOptionsSnelheidNummers = item.selection;
}
});
try {
const formattedText = formatText(args.data.unformattedText, columnsOptions, fullTextOptionsGemeenteWaar, fullTextOptionsSnelheidWaar, fullTextOptionsSnelheidNummers);
mainWindow.webContents.send('textFormatted', formattedText);
} catch (e) {
mainWindow.webContents.send('displayError', e.message);
}
});
// Get result form input from Result page
ipcMain.on('copy:result', (e, result) => {
// Write the value of the text field to system clipboard
const { clipboard } = require('electron');
clipboard.writeText(result);
// Send confirmation to renderer
mainWindow.webContents.send('copy:done');
});
/*
---- CREATE MENUS ----
*/
// Application menu (bar at top)
const applicationMenuTemplate = [{
label: app.name,
submenu: [
{ label: 'Quit', accelerator: "Command+Q", click: () => app.quit() }
]},{
label: "Edit",
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' }
]},{
label: "View",
submenu: [
{ role: "reload" },
{ role: "toggleDevTools" },
{ type: "separator" },
{ role: "resetzoom" },
{ role: "zoomIn" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" }
]}
];
// Context menu (right click)
const contextMenuTemplate = [
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ type: 'separator' },
{ role: 'undo' },
{ role: 'redo' },
];
/*
---- CREATE WINDOWS ----
*/
let mainWindow;
function createMainWindow() {
mainWindow = new BrowserWindow({
title: "Tekst opmaken",
width: isDev ? 1300 : 1000,
height: 850,
backgroundColor: "white",
webPreferences: {
nodeIntegration: false,
worldSafeExecuteJavaScript: true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
},
// icon: path.join(__dirname, 'src/js/assets/logo/icon.icns')
})
// Open developer tools in window if in DEV mode
if (isDev) { mainWindow.webContents.openDevTools(); }
mainWindow.loadFile(path.join(__dirname, 'index.html'));
}
/*
---- START AND STOP RUNNING APP ----
*/
// App is ready
app.whenReady().then(() => {
createMainWindow();
// Implement menus
const applicationMenu = Menu.buildFromTemplate(applicationMenuTemplate);
Menu.setApplicationMenu(applicationMenu);
const contextMenu = Menu.buildFromTemplate(contextMenuTemplate);
mainWindow.webContents.on('context-menu', (e, params) => {
contextMenu.popup(mainWindow, params.x, params.y);
})
// Remove mainWindow from memory on close
mainWindow.on('closed', () => (mainWindow = null));
app.on('activate', () => {
if (BrowserWindow.getAllWindows.length === 0) {
createMainWindow()
}
})
// Send app info to console
// console.log(app.name + " | version: " + app.getVersion());
});
// If not on a Mac, then quit the process when windows are closed, otherwise it keeps running
app.on('window-all-closed', () => {
if (!isMac) {
app.quit()
}
});