core refactor for runtime support
This commit is contained in:
@@ -3,8 +3,7 @@ import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
||||
import { app, shell, BrowserWindow, ipcMain } from 'electron'
|
||||
import filesystemPlugin from '@takerofnotes/plugin-filesystem'
|
||||
import supabasePlugin from '@takerofnotes/plugin-supabase'
|
||||
import { PluginRegistry, createConfigManager } from '../core/index.js'
|
||||
import { createNodeStorage } from './NodeStorage.js'
|
||||
import { initializeCore } from '../core/index.js'
|
||||
import { join } from 'path'
|
||||
|
||||
const DEFAULT_WINDOW_SIZE = { width: 354, height: 549 }
|
||||
@@ -13,8 +12,6 @@ const DEFAULT_MOVE_WINDOW_SIZE = { width: 708, height: 549 }
|
||||
const preloadPath = join(__dirname, '../preload/index.mjs')
|
||||
const rendererPath = join(__dirname, '../renderer/index.html')
|
||||
|
||||
let activeAdapter = null
|
||||
|
||||
function createWindow() {
|
||||
const mainWindow = new BrowserWindow({
|
||||
width: DEFAULT_WINDOW_SIZE.width,
|
||||
@@ -78,55 +75,32 @@ app.whenReady().then(async () => {
|
||||
})
|
||||
}
|
||||
|
||||
const registry = new PluginRegistry('electron')
|
||||
registry.register(filesystemPlugin)
|
||||
registry.register(supabasePlugin)
|
||||
const { pluginManager, configManager } = await initializeCore(
|
||||
'electron-main',
|
||||
{
|
||||
plugins: [filesystemPlugin, supabasePlugin],
|
||||
},
|
||||
)
|
||||
|
||||
const nodeStorage = createNodeStorage(filesystemPlugin)
|
||||
const configManager = createConfigManager('electron', nodeStorage)
|
||||
const initialConfig = await configManager.loadConfig()
|
||||
ipcMain.handle('pluginManager:call', async (_, method, ...args) => {
|
||||
const methodCall = await pluginManager[method](...args)
|
||||
|
||||
const setActivePlugin = async (pluginId) => {
|
||||
const currentConfig = await configManager.loadConfig()
|
||||
await configManager.setConfig({
|
||||
...currentConfig,
|
||||
activeAdapter: pluginId,
|
||||
})
|
||||
if (method === 'setActivePlugin') {
|
||||
broadcastNoteChange('plugin-changed')
|
||||
}
|
||||
|
||||
const plugin = registry.get(pluginId)
|
||||
const adapterConfig = currentConfig.adapters[pluginId] || {}
|
||||
activeAdapter = plugin.createAdapter(adapterConfig)
|
||||
|
||||
await activeAdapter.init()
|
||||
|
||||
ipcMain.removeHandler('adapter:call')
|
||||
ipcMain.handle('adapter:call', async (_, method, args) => {
|
||||
if (!activeAdapter[method]) {
|
||||
throw new Error(`Invalid adapter method: ${method}`)
|
||||
}
|
||||
|
||||
return await activeAdapter[method](...args)
|
||||
})
|
||||
|
||||
broadcastNoteChange('plugin-changed', pluginId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
await setActivePlugin(initialConfig.activeAdapter)
|
||||
|
||||
ipcMain.handle('getConfig', async () => {
|
||||
return await configManager.loadConfig()
|
||||
return methodCall
|
||||
})
|
||||
ipcMain.handle('setConfig', async (_, newConfig) => {
|
||||
await configManager.setConfig(newConfig)
|
||||
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}`)
|
||||
}
|
||||
|
||||
ipcMain.handle('listPlugins', async () => {
|
||||
return registry.list()
|
||||
})
|
||||
ipcMain.handle('setActivePlugin', async (_, pluginId) => {
|
||||
return await setActivePlugin(pluginId)
|
||||
return await adapter[method](...args)
|
||||
})
|
||||
|
||||
ipcMain.on('note-changed', (_, event, data) => {
|
||||
|
||||
Reference in New Issue
Block a user