plugin system

This commit is contained in:
nicwands
2026-02-24 11:18:37 -05:00
parent 0ab0620da8
commit d21076a785
8 changed files with 488 additions and 46 deletions

View File

@@ -1,13 +1,16 @@
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import { app, shell, BrowserWindow, ipcMain } from 'electron'
import NotesAPI from './notesAPI'
import PluginRegistry from './core/PluginRegistry.js'
import filesystemPlugin from './plugins/filesystem'
import PluginConfig from './core/PluginConfig.js'
import NotesAPI from './core/NotesAPI.js'
import { join } from 'path'
const preloadPath = join(__dirname, '../preload/index.js')
const rendererPath = join(__dirname, '../renderer/index.html')
// Main window
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 354,
height: 549,
@@ -28,8 +31,6 @@ function createWindow() {
return { action: 'deny' }
})
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
@@ -37,6 +38,7 @@ function createWindow() {
}
}
// Open note in new window
function createNoteWindow(noteId) {
const noteWindow = new BrowserWindow({
width: 354,
@@ -60,26 +62,16 @@ function createNoteWindow(noteId) {
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Set app user model id for windows
app.whenReady().then(async () => {
electronApp.setAppUserModelId('com.electron')
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
// Create main window
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
@@ -88,9 +80,24 @@ app.whenReady().then(() => {
createNoteWindow(noteId)
})
// Create plugin registry
const registry = new PluginRegistry()
// Register built-in plugins
registry.register(filesystemPlugin)
// Pull plugin config
const config = await new PluginConfig(filesystemPlugin).load()
// Create instance of active adapter
const plugin = registry.get(config.activeAdapter)
const adapter = plugin.createAdapter(config.adapterConfig)
// Init Notes API
const notesAPI = new NotesAPI()
notesAPI.init()
const notesAPI = new NotesAPI(adapter)
await notesAPI.init()
// Handle Notes API
ipcMain.handle('notesAPI:call', (_, method, args) => {
if (!notesAPI[method]) {
throw new Error('Invalid method')
@@ -99,9 +106,6 @@ app.whenReady().then(() => {
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()