Add local file open support and improve URL handling
Introduces a dialog to open local HTML files via Ctrl+O/Cmd+O, converting file paths to file:// URLs for navigation. Also improves input handling by stripping quotes and better detecting local file paths. Updates bookmarks and clears site history.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const { app, BrowserWindow, ipcMain, session, screen, shell } = require('electron');
|
||||
const { app, BrowserWindow, ipcMain, session, screen, shell, dialog } = require('electron');
|
||||
const { pathToFileURL } = require('url');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
@@ -548,3 +549,29 @@ ipcMain.handle('open-devtools', (event) => {
|
||||
}
|
||||
return contents.isDevToolsOpened();
|
||||
});
|
||||
|
||||
// Open local file dialog -> returns file:// URL (or null if cancelled)
|
||||
ipcMain.handle('show-open-file-dialog', async () => {
|
||||
try {
|
||||
const result = await dialog.showOpenDialog({
|
||||
properties: ['openFile'],
|
||||
filters: [
|
||||
{ name: 'HTML Files', extensions: ['html', 'htm', 'xhtml'] },
|
||||
{ name: 'All Files', extensions: ['*'] }
|
||||
]
|
||||
});
|
||||
if (result.canceled || !result.filePaths || !result.filePaths.length) return null;
|
||||
const filePath = result.filePaths[0];
|
||||
try {
|
||||
return pathToFileURL(filePath).href;
|
||||
} catch {
|
||||
// Fallback manual conversion
|
||||
let p = filePath.replace(/\\/g, '/');
|
||||
if (!p.startsWith('/')) p = '/' + p; // ensure leading slash for drive letters
|
||||
return 'file://' + (p.startsWith('/') ? '/' : '') + p; // double slash safety
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('open-file dialog failed:', err);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user