Init
This commit is contained in:
154
electron/main.cjs
Normal file
154
electron/main.cjs
Normal file
@@ -0,0 +1,154 @@
|
||||
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
|
||||
const path = require('path');
|
||||
const fs = require('fs').promises;
|
||||
|
||||
let mainWindow;
|
||||
|
||||
function createWindow() {
|
||||
mainWindow = new BrowserWindow({
|
||||
width: 1400,
|
||||
height: 900,
|
||||
webPreferences: {
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
preload: path.join(__dirname, 'preload.cjs')
|
||||
}
|
||||
});
|
||||
|
||||
mainWindow.setMenuBarVisibility(false);
|
||||
mainWindow.setMenu(null);
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
mainWindow.loadURL('http://localhost:5173');
|
||||
} else {
|
||||
mainWindow.loadFile(path.join(__dirname, '../dist/index.html'));
|
||||
}
|
||||
}
|
||||
|
||||
app.whenReady().then(createWindow);
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('read-file', async (event, filePath) => {
|
||||
try {
|
||||
const content = await fs.readFile(filePath, 'utf-8');
|
||||
return { success: true, content };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('write-file', async (event, filePath, content) => {
|
||||
try {
|
||||
await fs.writeFile(filePath, content, 'utf-8');
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('open-file-dialog', async () => {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
properties: ['openFile', 'multiSelections'],
|
||||
filters: [
|
||||
{ name: 'All Files', extensions: ['*'] },
|
||||
{ name: 'Web Files', extensions: ['html', 'css', 'js', 'jsx', 'ts', 'tsx'] },
|
||||
{ name: 'Python', extensions: ['py'] }
|
||||
]
|
||||
});
|
||||
|
||||
if (!result.canceled) {
|
||||
return { success: true, filePaths: result.filePaths };
|
||||
}
|
||||
return { success: false };
|
||||
});
|
||||
|
||||
ipcMain.handle('save-file-dialog', async (event, defaultPath) => {
|
||||
const result = await dialog.showSaveDialog(mainWindow, {
|
||||
defaultPath: defaultPath,
|
||||
filters: [
|
||||
{ name: 'All Files', extensions: ['*'] }
|
||||
]
|
||||
});
|
||||
|
||||
if (!result.canceled) {
|
||||
return { success: true, filePath: result.filePath };
|
||||
}
|
||||
return { success: false };
|
||||
});
|
||||
|
||||
ipcMain.handle('open-folder-dialog', async () => {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
properties: ['openDirectory']
|
||||
});
|
||||
|
||||
if (!result.canceled) {
|
||||
return { success: true, folderPath: result.filePaths[0] };
|
||||
}
|
||||
return { success: false };
|
||||
});
|
||||
|
||||
ipcMain.handle('read-directory', async (event, dirPath) => {
|
||||
try {
|
||||
const items = await fs.readdir(dirPath, { withFileTypes: true });
|
||||
const files = items.map(item => ({
|
||||
name: item.name,
|
||||
isDirectory: item.isDirectory(),
|
||||
path: path.join(dirPath, item.name)
|
||||
}));
|
||||
return { success: true, files };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('create-file', async (event, filePath) => {
|
||||
try {
|
||||
await fs.writeFile(filePath, '', 'utf-8');
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('create-folder', async (event, folderPath) => {
|
||||
try {
|
||||
await fs.mkdir(folderPath);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('delete-file', async (event, filePath) => {
|
||||
try {
|
||||
const stats = await fs.stat(filePath);
|
||||
if (stats.isDirectory()) {
|
||||
await fs.rm(filePath, { recursive: true, force: true });
|
||||
} else {
|
||||
await fs.unlink(filePath);
|
||||
}
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('rename-file', async (event, oldPath, newPath) => {
|
||||
try {
|
||||
await fs.rename(oldPath, newPath);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
14
electron/preload.cjs
Normal file
14
electron/preload.cjs
Normal file
@@ -0,0 +1,14 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron');
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
readFile: (filePath) => ipcRenderer.invoke('read-file', filePath),
|
||||
writeFile: (filePath, content) => ipcRenderer.invoke('write-file', filePath, content),
|
||||
openFileDialog: () => ipcRenderer.invoke('open-file-dialog'),
|
||||
saveFileDialog: (defaultPath) => ipcRenderer.invoke('save-file-dialog', defaultPath),
|
||||
openFolderDialog: () => ipcRenderer.invoke('open-folder-dialog'),
|
||||
readDirectory: (dirPath) => ipcRenderer.invoke('read-directory', dirPath),
|
||||
createFile: (filePath) => ipcRenderer.invoke('create-file', filePath),
|
||||
createFolder: (folderPath) => ipcRenderer.invoke('create-folder', folderPath),
|
||||
deleteFile: (filePath) => ipcRenderer.invoke('delete-file', filePath),
|
||||
renameFile: (oldPath, newPath) => ipcRenderer.invoke('rename-file', oldPath, newPath)
|
||||
});
|
||||
Reference in New Issue
Block a user