total restructure: isolate functionality to core so web works

This commit is contained in:
nicwands
2026-03-19 17:25:43 -04:00
parent f9e7fe1208
commit eea1cccf16
20 changed files with 635 additions and 247 deletions

View File

@@ -3,8 +3,8 @@ 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 from './core/PluginRegistry.js'
import Config from './core/Config.js'
import { PluginRegistry, createConfigManager } from '../core/index.js'
import { createNodeStorage } from './NodeStorage.js'
import { join } from 'path'
const DEFAULT_WINDOW_SIZE = { width: 354, height: 549 }
@@ -13,7 +13,8 @@ const DEFAULT_MOVE_WINDOW_SIZE = { width: 708, height: 549 }
const preloadPath = join(__dirname, '../preload/index.mjs')
const rendererPath = join(__dirname, '../renderer/index.html')
// Main window
let activeAdapter = null
function createWindow() {
const mainWindow = new BrowserWindow({
width: DEFAULT_WINDOW_SIZE.width,
@@ -42,7 +43,6 @@ function createWindow() {
}
}
// Open note in new window
function createNoteWindow(noteId) {
const noteWindow = new BrowserWindow({
width: DEFAULT_WINDOW_SIZE.width,
@@ -68,48 +68,44 @@ function createNoteWindow(noteId) {
}
app.whenReady().then(async () => {
// Open note in new window
ipcMain.on('open-note-window', (_, noteId) => {
createNoteWindow(noteId)
})
// Broadcast note changes to all windows
const broadcastNoteChange = (event, data) => {
BrowserWindow.getAllWindows().forEach((win) => {
win.webContents.send(event, data)
})
}
// Create plugin registry
const registry = new PluginRegistry()
// Register built-in plugins
const registry = new PluginRegistry('electron')
registry.register(filesystemPlugin)
registry.register(supabasePlugin)
// Pull plugin config
const config = new Config(filesystemPlugin)
const initialConfig = await config.load()
const nodeStorage = createNodeStorage(filesystemPlugin)
const configManager = createConfigManager('electron', nodeStorage)
const initialConfig = await configManager.loadConfig()
const setActivePlugin = async (pluginId) => {
const currentConfig = await config.load()
await config.write({ ...currentConfig, activeAdapter: pluginId })
const currentConfig = await configManager.loadConfig()
await configManager.setConfig({
...currentConfig,
activeAdapter: pluginId,
})
const plugin = registry.get(pluginId)
const adapterConfig = currentConfig.adapters[pluginId] || {}
const adapter = plugin.createAdapter(adapterConfig)
activeAdapter = plugin.createAdapter(adapterConfig)
// Initialize adapter
await adapter.init()
await activeAdapter.init()
// Handle adapter methods via IPC
ipcMain.removeHandler('adapter:call')
ipcMain.handle('adapter:call', async (_, method, args) => {
if (!adapter[method]) {
if (!activeAdapter[method]) {
throw new Error(`Invalid adapter method: ${method}`)
}
return await adapter[method](...args)
return await activeAdapter[method](...args)
})
broadcastNoteChange('plugin-changed', pluginId)
@@ -117,18 +113,15 @@ app.whenReady().then(async () => {
return true
}
// Set active plugin
await setActivePlugin(initialConfig.activeAdapter)
// Get/set config
ipcMain.handle('getConfig', async () => {
return await config.load()
return await configManager.loadConfig()
})
ipcMain.handle('setConfig', async (_, newConfig) => {
await config.write(newConfig)
await configManager.setConfig(newConfig)
})
// Get/set plugins
ipcMain.handle('listPlugins', async () => {
return registry.list()
})
@@ -136,12 +129,10 @@ app.whenReady().then(async () => {
return await setActivePlugin(pluginId)
})
// Handle note change events from renderer
ipcMain.on('note-changed', (_, event, data) => {
broadcastNoteChange(event, data)
})
// Handle resizing for note "move" functionality
ipcMain.handle('move-opened', (_) => {
const activeWindow = BrowserWindow.getFocusedWindow()
const windowSize = activeWindow.getSize()