generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make the app work inside of electron
- Loading branch information
1 parent
1545a93
commit eed1849
Showing
9 changed files
with
3,116 additions
and
3,758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
dist | ||
test | ||
*.log | ||
*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const { app, BrowserWindow, dialog, ipcMain } = require('electron'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { createServer } = require('http'); | ||
const next = require('next'); | ||
|
||
let mainWindow; | ||
const nextApp = next({ dev: process.env.NODE_ENV !== 'production', dir: path.join(__dirname, '../') }); | ||
const nextHandler = nextApp.getRequestHandler(); | ||
|
||
async function createWindow() { | ||
const isDev = (await import('electron-is-dev')).default; | ||
await nextApp.prepare(); | ||
|
||
mainWindow = new BrowserWindow({ | ||
width: 800, | ||
height: 600, | ||
webPreferences: { | ||
preload: path.join(__dirname, './preload.js'), | ||
contextIsolation: true, // Ensure contextIsolation is true | ||
enableRemoteModule: false, // Disable remote module for security | ||
}, | ||
}); | ||
|
||
const server = createServer((req, res) => { | ||
nextHandler(req, res); | ||
}); | ||
|
||
let port = process.env.PORT || 3000; | ||
server.listen(port, () => { | ||
const url = `http://localhost:${port}`; | ||
mainWindow.loadURL(url); | ||
}).on('error', (err) => { | ||
if (err.code === 'EADDRINUSE') { | ||
port += 1; | ||
server.listen(port, () => { | ||
const url = `http://localhost:${port}`; | ||
mainWindow.loadURL(url); | ||
}); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
|
||
if (isDev) { | ||
mainWindow.webContents.openDevTools(); | ||
} | ||
|
||
mainWindow.on('closed', () => { | ||
mainWindow = null; | ||
}); | ||
} | ||
|
||
app.on('ready', createWindow); | ||
|
||
ipcMain.handle('select-folder', async () => { | ||
const result = await dialog.showOpenDialog(mainWindow, { | ||
properties: ['openDirectory'], | ||
}); | ||
|
||
if (result.canceled) { | ||
return null; | ||
} else { | ||
return result.filePaths[0]; | ||
} | ||
}); | ||
|
||
ipcMain.on('watch-folder', (event, folderPath) => { | ||
fs.watch(folderPath, (eventType, filename) => { | ||
if (eventType === 'rename' && filename) { | ||
event.sender.send('new-file', filename); | ||
} | ||
}); | ||
}); | ||
|
||
app.on('window-all-closed', () => { | ||
if (process.platform !== 'darwin') { | ||
app.quit(); | ||
} | ||
}); | ||
|
||
app.on('activate', () => { | ||
if (mainWindow === null) { | ||
createWindow(); | ||
} | ||
}); |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.