-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
562 lines (500 loc) · 17.2 KB
/
Copy pathmain.js
File metadata and controls
562 lines (500 loc) · 17.2 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
const { app, BrowserWindow, Menu, dialog, ipcMain, shell } = require('electron');
const path = require('path');
const fs = require('fs');
const iconv = require('iconv-lite');
const { exec } = require('child_process');
let mainWindow;
let fileToOpen = null; // Store file path from command line
// Recent files management
function getRecentFiles() {
try {
const recentFilesPath = path.join(app.getPath('userData'), 'recent-files.json');
if (fs.existsSync(recentFilesPath)) {
return JSON.parse(fs.readFileSync(recentFilesPath, 'utf-8'));
}
} catch (error) {
console.error('Error loading recent files:', error);
}
return [];
}
function addRecentFile(filePath) {
try {
let recentFiles = getRecentFiles();
// Remove if already exists
recentFiles = recentFiles.filter(f => f.path !== filePath);
// Add to beginning
recentFiles.unshift({
path: filePath,
name: path.basename(filePath),
timestamp: Date.now()
});
// Keep only last 10
recentFiles = recentFiles.slice(0, 10);
const recentFilesPath = path.join(app.getPath('userData'), 'recent-files.json');
fs.writeFileSync(recentFilesPath, JSON.stringify(recentFiles, null, 2));
// Rebuild menu
const template = buildMenuTemplate();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
} catch (error) {
console.error('Error saving recent files:', error);
}
}
function detectAndReadFile(filePath) {
let content;
let encoding = 'utf-8';
let hasEncodingIssues = false;
try {
// Try UTF-8 first
content = fs.readFileSync(filePath, 'utf-8');
// Check for BOM and remove if present
if (content.charCodeAt(0) === 0xFEFF) {
content = content.slice(1);
}
// Check for explicit replacement characters
if (content.includes('\uFFFD') || content.includes('�')) {
hasEncodingIssues = true;
// Try latin1 (ISO-8859-1) which handles most Western European characters
const latin1Content = fs.readFileSync(filePath, 'latin1');
// Simple heuristic: if latin1 produces valid JSON, use it
try {
JSON.parse(latin1Content);
content = latin1Content;
encoding = 'latin1';
hasEncodingIssues = false;
} catch (e) {
// Keep UTF-8 content if latin1 doesn't parse
}
}
} catch (error) {
// If UTF-8 fails entirely, try latin1
try {
content = fs.readFileSync(filePath, 'latin1');
encoding = 'latin1';
} catch (fallbackError) {
throw error; // Re-throw original error
}
}
// Flag if we auto-detected a non-UTF8 encoding
const wasAutoDetected = encoding !== 'utf-8' && !hasEncodingIssues;
return { content, encoding, hasEncodingIssues, wasAutoDetected };
}
function handleCommandLineFile() {
// Check if a file was passed as argument
const args = process.argv.slice(1); // Skip the first argument (electron executable)
// In production, the file argument might be at different positions
let filePath = null;
// Look for a .json file in the arguments
for (const arg of args) {
if (arg.endsWith('.json') && fs.existsSync(arg)) {
filePath = arg;
break;
}
}
// Also check if fileToOpen was set (for second-instance handling)
if (!filePath && fileToOpen) {
filePath = fileToOpen;
fileToOpen = null; // Clear it after use
}
if (filePath) {
// Wait for the window to be ready
mainWindow.webContents.once('did-finish-load', () => {
setTimeout(() => {
openFileInWindow(filePath);
}, 500); // Small delay to ensure renderer is ready
});
}
}
function openFileInWindow(filePath) {
try {
const { content, encoding, hasEncodingIssues, wasAutoDetected } = detectAndReadFile(filePath);
const fileName = path.basename(filePath);
mainWindow.webContents.send('file-opened', { content, fileName, filePath, encoding, hasEncodingIssues, wasAutoDetected });
addRecentFile(filePath);
// Bring window to front
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
} catch (error) {
dialog.showErrorBox('Error', `Failed to open file: ${error.message}`);
}
}
function openRecentFile(filePath) {
try {
const { content, encoding, hasEncodingIssues, wasAutoDetected } = detectAndReadFile(filePath);
const fileName = path.basename(filePath);
mainWindow.webContents.send('file-opened', { content, fileName, filePath, encoding, hasEncodingIssues, wasAutoDetected });
addRecentFile(filePath);
} catch (error) {
dialog.showErrorBox('Error', `Failed to open recent file: ${error.message}`);
}
}
function buildMenuTemplate() {
const recentFiles = getRecentFiles();
const recentFilesMenu = recentFiles.length > 0 ?
recentFiles.map(file => ({
label: file.name,
click: () => openRecentFile(file.path)
})) :
[{ label: 'No recent files', enabled: false }];
return [
{
label: 'File',
submenu: [
{
label: 'Open JSON File',
accelerator: 'CmdOrCtrl+O',
click: async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'JSON Files', extensions: ['json'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (!result.canceled && result.filePaths.length > 0) {
const filePath = result.filePaths[0];
try {
const { content, encoding, hasEncodingIssues, wasAutoDetected } = detectAndReadFile(filePath);
const fileName = path.basename(filePath);
mainWindow.webContents.send('file-opened', { content, fileName, filePath, encoding, hasEncodingIssues, wasAutoDetected });
addRecentFile(filePath);
} catch (error) {
dialog.showErrorBox('Error', `Failed to read file: ${error.message}`);
}
}
}
},
{
label: 'Recent Files',
submenu: recentFilesMenu
},
{ type: 'separator' },
{
label: 'New Tab',
accelerator: 'CmdOrCtrl+T',
click: () => {
mainWindow.webContents.send('new-tab');
}
},
{
label: 'Close Tab',
accelerator: 'CmdOrCtrl+W',
click: () => {
mainWindow.webContents.send('close-tab');
}
},
{ type: 'separator' },
{
label: 'Settings',
accelerator: 'CmdOrCtrl+,',
click: () => {
mainWindow.webContents.send('toggle-settings');
}
},
{ type: 'separator' },
{
label: 'System Integration',
submenu: [
{
label: 'Register as JSON Handler',
click: () => registerFileAssociation()
},
{
label: 'Unregister as JSON Handler',
click: () => unregisterFileAssociation()
}
]
},
{ type: 'separator' },
{ role: 'quit' }
]
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'selectall' },
{ type: 'separator' },
{
label: 'Find',
accelerator: 'CmdOrCtrl+F',
click: () => {
mainWindow.webContents.send('toggle-search');
}
}
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
{ type: 'separator' },
{
label: 'Expand All',
accelerator: 'CmdOrCtrl+E',
click: () => {
mainWindow.webContents.send('expand-all');
}
},
{
label: 'Collapse All',
accelerator: 'CmdOrCtrl+Shift+E',
click: () => {
mainWindow.webContents.send('collapse-all');
}
}
]
},
{
label: 'Help',
submenu: [
{
label: 'About',
click: () => {
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'About JSONinja',
message: `JSONinja - Advanced JSON Viewer v${app.getVersion()}`,
detail: 'A powerful, customizable JSON viewer with multi-tab support.\nBuilt with Electron.'
});
}
}
]
}
];
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1400,
height: 900,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
},
title: 'JSONinja - Advanced JSON Viewer',
icon: path.join(__dirname, 'icon.png')
});
mainWindow.loadFile('index.html');
// Handle file passed as command line argument
handleCommandLineFile();
// Create application menu with recent files
const template = buildMenuTemplate();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
// Handle window closed
mainWindow.on('closed', () => {
mainWindow = null;
});
}
// IPC handlers
ipcMain.handle('get-version', async () => {
return app.getVersion();
});
ipcMain.handle('save-settings', async (event, settings) => {
try {
const settingsPath = path.join(app.getPath('userData'), 'settings.json');
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('load-settings', async () => {
try {
const settingsPath = path.join(app.getPath('userData'), 'settings.json');
if (fs.existsSync(settingsPath)) {
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'));
return { success: true, settings };
}
return { success: true, settings: null };
} catch (error) {
return { success: false, error: error.message };
}
});
ipcMain.handle('reload-with-encoding', async (event, { filePath, encoding }) => {
try {
let content;
// Use iconv-lite for encodings not supported by Node.js
if (encoding === 'cp1252' || encoding === 'windows-1252' || encoding === 'win1252') {
const buffer = fs.readFileSync(filePath);
content = iconv.decode(buffer, 'win1252');
} else {
// Use Node.js built-in for standard encodings
content = fs.readFileSync(filePath, encoding);
// Remove BOM if present
if (encoding === 'utf-8' && content.charCodeAt(0) === 0xFEFF) {
content = content.slice(1);
}
}
const fileName = path.basename(filePath);
return { success: true, content, fileName, filePath, encoding };
} catch (error) {
return { success: false, error: error.message };
}
});
// Handle show-open-dialog from renderer
ipcMain.on('show-open-dialog', async (event) => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'JSON Files', extensions: ['json'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (!result.canceled && result.filePaths.length > 0) {
const filePath = result.filePaths[0];
try {
const { content, encoding, hasEncodingIssues, wasAutoDetected } = detectAndReadFile(filePath);
const fileName = path.basename(filePath);
mainWindow.webContents.send('file-opened', { content, fileName, filePath, encoding, hasEncodingIssues, wasAutoDetected });
addRecentFile(filePath);
} catch (error) {
dialog.showErrorBox('Error', `Failed to read file: ${error.message}`);
}
}
});
// File association functions
function registerFileAssociation() {
if (process.platform === 'win32') {
// Windows registry approach for portable version
const appPath = process.execPath;
const appName = 'JSONinja';
// Create a batch script to add registry entries
const regCommands = `
@echo off
echo Registering JSONinja as JSON file handler...
echo Executable path: ${appPath}
REM Add JSONinja to Open With menu
reg add "HKEY_CURRENT_USER\\Software\\Classes\\Applications\\JSONinja.exe\\shell\\open\\command" /ve /d "\\"${appPath}\\" \\"%%1\\"" /f
REM Associate with .json files
reg add "HKEY_CURRENT_USER\\Software\\Classes\\.json\\OpenWithList\\JSONinja.exe" /ve /d "" /f
REM Add to context menu with icon
reg add "HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.json\\shell\\Open with JSONinja" /ve /d "Open with JSONinja" /f
reg add "HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.json\\shell\\Open with JSONinja" /v "Icon" /d "\\"${appPath}\\",0" /f
reg add "HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.json\\shell\\Open with JSONinja\\command" /ve /d "\\"${appPath}\\" \\"%%1\\"" /f
REM Also add to standard .json handler
reg add "HKEY_CURRENT_USER\\Software\\Classes\\.json" /ve /d "JSONinja.Document" /f
reg add "HKEY_CURRENT_USER\\Software\\Classes\\JSONinja.Document" /ve /d "JSON Document" /f
reg add "HKEY_CURRENT_USER\\Software\\Classes\\JSONinja.Document\\DefaultIcon" /ve /d "\\"${appPath}\\",0" /f
reg add "HKEY_CURRENT_USER\\Software\\Classes\\JSONinja.Document\\shell\\open\\command" /ve /d "\\"${appPath}\\" \\"%%1\\"" /f
echo.
echo Registration complete!
echo You may need to restart Explorer or log off/on for changes to take effect.
pause
`;
// Save to temp file and execute
const tempBatPath = path.join(app.getPath('temp'), 'jsoninja_register.bat');
fs.writeFileSync(tempBatPath, regCommands);
exec(`start cmd /c "${tempBatPath}"`, (error) => {
if (error) {
dialog.showErrorBox('Registration Error', 'Failed to register file association. You may need to run as administrator.');
} else {
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Success',
message: 'JSONinja has been registered as a JSON file handler.',
detail: 'You can now right-click JSON files and select "Open with JSONinja"'
});
}
// Clean up temp file
setTimeout(() => {
try { fs.unlinkSync(tempBatPath); } catch (e) {}
}, 5000);
});
} else if (process.platform === 'darwin') {
// macOS approach
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'macOS File Association',
message: 'To associate JSON files with JSONinja on macOS:',
detail: '1. Right-click any .json file\n2. Select "Get Info"\n3. Under "Open with", select JSONinja\n4. Click "Change All..."'
});
} else {
// Linux approach
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Linux File Association',
message: 'File association on Linux varies by desktop environment.',
detail: 'Right-click a JSON file and look for "Open With" or "Properties" to set JSONinja as the default handler.'
});
}
}
function unregisterFileAssociation() {
if (process.platform === 'win32') {
const regCommands = `
@echo off
echo Unregistering JSONinja file associations...
REM Remove from Open With menu
reg delete "HKEY_CURRENT_USER\\Software\\Classes\\Applications\\JSONinja.exe" /f 2>nul
REM Remove from .json associations
reg delete "HKEY_CURRENT_USER\\Software\\Classes\\.json\\OpenWithList\\JSONinja.exe" /f 2>nul
REM Remove from context menu
reg delete "HKEY_CURRENT_USER\\Software\\Classes\\SystemFileAssociations\\.json\\shell\\Open with JSONinja" /f 2>nul
REM Remove document type
reg delete "HKEY_CURRENT_USER\\Software\\Classes\\JSONinja.Document" /f 2>nul
echo Unregistration complete!
pause
`;
const tempBatPath = path.join(app.getPath('temp'), 'jsoninja_unregister.bat');
fs.writeFileSync(tempBatPath, regCommands);
exec(`start cmd /c "${tempBatPath}"`, (error) => {
if (!error) {
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Success',
message: 'JSONinja file associations have been removed.'
});
}
setTimeout(() => {
try { fs.unlinkSync(tempBatPath); } catch (e) {}
}, 5000);
});
}
}
// Single instance lock
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
// Another instance is running, quit this one
app.quit();
} else {
// Handle when another instance tries to run
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, focus our window instead
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
// Check if they're trying to open a file
for (const arg of commandLine.slice(1)) {
if (arg.endsWith('.json') && fs.existsSync(arg)) {
openFileInWindow(arg);
break;
}
}
}
});
app.whenReady().then(createWindow);
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});