165 lines
4.7 KiB
JavaScript
165 lines
4.7 KiB
JavaScript
import 'dotenv/config'
|
|
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
|
import { app, shell, BrowserWindow, ipcMain, dialog } from 'electron'
|
|
import filesystemPlugin from '@takerofnotes/plugin-filesystem'
|
|
import supabasePlugin from '@takerofnotes/plugin-supabase'
|
|
import s3Plugin from '@takerofnotes/plugin-s3'
|
|
import postgresPlugin from '@takerofnotes/plugin-postgre-sql'
|
|
import { initializeCore } from '../core/index.js'
|
|
import { join } from 'path'
|
|
|
|
const DEFAULT_WINDOW_SIZE = { width: 354, height: 549 }
|
|
const DEFAULT_MOVE_WINDOW_SIZE = { width: 708, height: 549 }
|
|
|
|
const preloadPath = join(__dirname, '../preload/index.mjs')
|
|
const rendererPath = join(__dirname, '../renderer/index.html')
|
|
|
|
function createWindow() {
|
|
const mainWindow = new BrowserWindow({
|
|
width: DEFAULT_WINDOW_SIZE.width,
|
|
height: DEFAULT_WINDOW_SIZE.height,
|
|
show: false,
|
|
autoHideMenuBar: true,
|
|
webPreferences: {
|
|
preload: preloadPath,
|
|
sandbox: false,
|
|
},
|
|
})
|
|
|
|
mainWindow.on('ready-to-show', () => {
|
|
mainWindow.show()
|
|
})
|
|
|
|
mainWindow.webContents.setWindowOpenHandler((details) => {
|
|
shell.openExternal(details.url)
|
|
return { action: 'deny' }
|
|
})
|
|
|
|
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
|
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
|
|
} else {
|
|
mainWindow.loadFile(rendererPath)
|
|
}
|
|
}
|
|
|
|
function createNoteWindow(noteId) {
|
|
const noteWindow = new BrowserWindow({
|
|
width: DEFAULT_WINDOW_SIZE.width,
|
|
height: DEFAULT_WINDOW_SIZE.height,
|
|
autoHideMenuBar: true,
|
|
webPreferences: {
|
|
preload: preloadPath,
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
sandbox: false,
|
|
},
|
|
})
|
|
|
|
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
|
noteWindow.loadURL(
|
|
`${process.env['ELECTRON_RENDERER_URL']}/#/note/${noteId}`,
|
|
)
|
|
} else {
|
|
noteWindow.loadFile(rendererPath, {
|
|
hash: `/note/${noteId}`,
|
|
})
|
|
}
|
|
}
|
|
|
|
app.whenReady().then(async () => {
|
|
ipcMain.on('open-note-window', (_, noteId) => {
|
|
createNoteWindow(noteId)
|
|
})
|
|
|
|
const broadcastNoteChange = (event, data) => {
|
|
BrowserWindow.getAllWindows().forEach((win) => {
|
|
win.webContents.send(event, data)
|
|
})
|
|
}
|
|
|
|
const { pluginManager, configManager } = await initializeCore(
|
|
'electron-main',
|
|
{
|
|
plugins: [
|
|
filesystemPlugin,
|
|
s3Plugin,
|
|
postgresPlugin,
|
|
supabasePlugin,
|
|
],
|
|
},
|
|
)
|
|
|
|
ipcMain.handle('pluginManager:call', async (_, method, ...args) => {
|
|
const methodCall = await pluginManager[method](...args)
|
|
|
|
if (method === 'setActivePlugin') {
|
|
broadcastNoteChange('plugin-changed')
|
|
}
|
|
|
|
return methodCall
|
|
})
|
|
ipcMain.handle('configManager:call', async (_, method, ...args) => {
|
|
return await configManager[method](...args)
|
|
})
|
|
ipcMain.handle('adapter:call', async (_, method, ...args) => {
|
|
const adapter = pluginManager.getActiveAdapter()
|
|
if (!adapter[method]) {
|
|
throw new Error(`Invalid adapter method: ${method}`)
|
|
}
|
|
|
|
return await adapter[method](...args)
|
|
})
|
|
|
|
ipcMain.on('note-changed', (_, event, data) => {
|
|
broadcastNoteChange(event, data)
|
|
})
|
|
|
|
ipcMain.handle('move-opened', (_) => {
|
|
const activeWindow = BrowserWindow.getFocusedWindow()
|
|
const windowSize = activeWindow.getSize()
|
|
|
|
if (windowSize[0] < DEFAULT_MOVE_WINDOW_SIZE.width) {
|
|
activeWindow.setSize(
|
|
DEFAULT_MOVE_WINDOW_SIZE.width,
|
|
DEFAULT_MOVE_WINDOW_SIZE.height,
|
|
)
|
|
}
|
|
})
|
|
ipcMain.handle('move-closed', (_) => {
|
|
const activeWindow = BrowserWindow.getFocusedWindow()
|
|
const windowSize = activeWindow.getSize()
|
|
|
|
if (windowSize[0] === 708) {
|
|
activeWindow.setSize(
|
|
DEFAULT_WINDOW_SIZE.width,
|
|
DEFAULT_WINDOW_SIZE.height,
|
|
)
|
|
}
|
|
})
|
|
|
|
ipcMain.handle('open-directory-dialog', async () => {
|
|
const result = await dialog.showOpenDialog({
|
|
properties: ['openDirectory'],
|
|
})
|
|
return result.canceled ? null : result.filePaths[0]
|
|
})
|
|
|
|
electronApp.setAppUserModelId('com.electron')
|
|
|
|
app.on('browser-window-created', (_, window) => {
|
|
optimizer.watchWindowShortcuts(window)
|
|
})
|
|
|
|
createWindow()
|
|
|
|
app.on('activate', function () {
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
})
|
|
})
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit()
|
|
}
|
|
})
|