total restructure: isolate functionality to core so web works
This commit is contained in:
@@ -3,6 +3,9 @@ import { electronApp, optimizer, is } from "@electron-toolkit/utils";
|
||||
import { app, ipcMain, BrowserWindow, shell } from "electron";
|
||||
import filesystemPlugin from "@takerofnotes/plugin-filesystem";
|
||||
import supabasePlugin from "@takerofnotes/plugin-supabase";
|
||||
import "libsodium-wrappers";
|
||||
import "uuid";
|
||||
import "flexsearch";
|
||||
import fs from "fs/promises";
|
||||
import path, { join } from "path";
|
||||
import __cjs_mod__ from "node:module";
|
||||
@@ -10,13 +13,18 @@ const __filename = import.meta.filename;
|
||||
const __dirname = import.meta.dirname;
|
||||
const require2 = __cjs_mod__.createRequire(import.meta.url);
|
||||
class PluginRegistry {
|
||||
constructor() {
|
||||
constructor(environment = "web") {
|
||||
this.plugins = /* @__PURE__ */ new Map();
|
||||
this.environment = environment;
|
||||
}
|
||||
register(plugin) {
|
||||
if (!plugin.id) {
|
||||
throw new Error("Plugin must have an id");
|
||||
}
|
||||
const environments = plugin.environments || ["electron", "web"];
|
||||
if (!environments.includes(this.environment)) {
|
||||
return;
|
||||
}
|
||||
this.plugins.set(plugin.id, plugin);
|
||||
}
|
||||
get(id) {
|
||||
@@ -31,72 +39,146 @@ class PluginRegistry {
|
||||
}));
|
||||
}
|
||||
}
|
||||
const USER_DATA_STRING = "__DEFAULT_USER_DATA__";
|
||||
class Config {
|
||||
constructor(defaultPlugin) {
|
||||
this.defaultPlugin = defaultPlugin;
|
||||
this.configPath = path.join(app.getPath("userData"), "config.json");
|
||||
}
|
||||
// Helper to replace placeholders with dynamic values
|
||||
_resolveDefaults(config) {
|
||||
if (Array.isArray(config)) {
|
||||
return config.map((item) => this._resolveDefaults(item));
|
||||
} else if (config && typeof config === "object") {
|
||||
const resolved = {};
|
||||
for (const [key, value] of Object.entries(config)) {
|
||||
resolved[key] = this._resolveDefaults(value);
|
||||
function createIpcStorage() {
|
||||
return {
|
||||
async load() {
|
||||
return await window.api.getConfig();
|
||||
},
|
||||
async save(data) {
|
||||
await window.api.setConfig(data);
|
||||
}
|
||||
};
|
||||
}
|
||||
function getDefaultConfig() {
|
||||
return {
|
||||
activeAdapter: "supabase",
|
||||
adapters: {
|
||||
supabase: {
|
||||
supabaseUrl: "https://example.supabase.co",
|
||||
supabaseKey: "",
|
||||
bucket: "notes"
|
||||
}
|
||||
return resolved;
|
||||
} else if (typeof config === "string" && config.includes(USER_DATA_STRING)) {
|
||||
return config.replace(USER_DATA_STRING, app.getPath("userData"));
|
||||
} else {
|
||||
},
|
||||
theme: "light"
|
||||
};
|
||||
}
|
||||
let config = null;
|
||||
let configResolve = null;
|
||||
const configPromise = new Promise((resolve) => {
|
||||
configResolve = resolve;
|
||||
});
|
||||
function createConfigManager(environment, customStorage = null) {
|
||||
let storage;
|
||||
if (customStorage) {
|
||||
storage = customStorage;
|
||||
} else {
|
||||
storage = createIpcStorage();
|
||||
}
|
||||
const loadConfig = async () => {
|
||||
if (config !== null) {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
async load() {
|
||||
let parsed;
|
||||
try {
|
||||
const raw = await fs.readFile(this.configPath, "utf8");
|
||||
parsed = JSON.parse(raw);
|
||||
} catch (err) {
|
||||
parsed = null;
|
||||
const stored = await storage.load();
|
||||
config = stored || getDefaultConfig();
|
||||
if (!stored) {
|
||||
await storage.save(config);
|
||||
}
|
||||
if (!parsed || !parsed.activeAdapter) {
|
||||
const defaultConfig = {};
|
||||
for (const field of this.defaultPlugin.configSchema) {
|
||||
defaultConfig[field.key] = field.default ?? null;
|
||||
configResolve(config);
|
||||
return config;
|
||||
};
|
||||
const writeConfig = async (newConfig) => {
|
||||
await storage.save(newConfig);
|
||||
config = newConfig;
|
||||
};
|
||||
const getConfig = () => {
|
||||
if (config !== null) {
|
||||
return config;
|
||||
}
|
||||
return configPromise;
|
||||
};
|
||||
const setConfig = async (newConfig) => {
|
||||
config = newConfig;
|
||||
await writeConfig(newConfig);
|
||||
};
|
||||
const refreshConfig = async () => {
|
||||
config = await storage.load();
|
||||
configResolve(config);
|
||||
};
|
||||
return {
|
||||
loadConfig,
|
||||
getConfig,
|
||||
setConfig,
|
||||
refreshConfig
|
||||
};
|
||||
}
|
||||
const USER_DATA_STRING = "__DEFAULT_USER_DATA__";
|
||||
function createNodeStorage(defaultPlugin = null) {
|
||||
const configPath = path.join(app.getPath("userData"), "config.json");
|
||||
function resolveDefaults(obj) {
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(resolveDefaults);
|
||||
} else if (obj && typeof obj === "object") {
|
||||
const resolved = {};
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
resolved[key] = resolveDefaults(value);
|
||||
}
|
||||
parsed = {
|
||||
...parsed ? parsed : {},
|
||||
activeAdapter: this.defaultPlugin.id,
|
||||
adapters: {}
|
||||
};
|
||||
parsed.adapters[this.defaultPlugin.id] = defaultConfig;
|
||||
parsed[theme] = "dark";
|
||||
await this.write(parsed);
|
||||
} else {
|
||||
parsed.adapters = this._resolveDefaults(parsed.adapters);
|
||||
return resolved;
|
||||
} else if (typeof obj === "string" && obj.includes(USER_DATA_STRING)) {
|
||||
return obj.replace(USER_DATA_STRING, app.getPath("userData"));
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
async write(configObject) {
|
||||
const dir = path.dirname(this.configPath);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
const resolvedConfig = {
|
||||
...configObject,
|
||||
adapters: this._resolveDefaults(configObject.adapters)
|
||||
};
|
||||
await fs.writeFile(
|
||||
this.configPath,
|
||||
JSON.stringify(resolvedConfig, null, 2),
|
||||
"utf8"
|
||||
);
|
||||
return obj;
|
||||
}
|
||||
return {
|
||||
async load() {
|
||||
let parsed;
|
||||
try {
|
||||
const raw = await fs.readFile(configPath, "utf8");
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = null;
|
||||
}
|
||||
if (!parsed || !parsed.activeAdapter) {
|
||||
const defaultConfig = {};
|
||||
if (defaultPlugin) {
|
||||
for (const field of defaultPlugin.configSchema) {
|
||||
defaultConfig[field.key] = field.default ?? null;
|
||||
}
|
||||
}
|
||||
parsed = {
|
||||
...parsed ? parsed : {},
|
||||
activeAdapter: defaultPlugin?.id || "supabase",
|
||||
adapters: {},
|
||||
theme: "dark"
|
||||
};
|
||||
if (defaultPlugin) {
|
||||
parsed.adapters[defaultPlugin.id] = defaultConfig;
|
||||
}
|
||||
await this.save(parsed);
|
||||
} else {
|
||||
parsed.adapters = resolveDefaults(parsed.adapters);
|
||||
}
|
||||
return parsed;
|
||||
},
|
||||
async save(configObject) {
|
||||
const dir = path.dirname(configPath);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
const resolved = {
|
||||
...configObject,
|
||||
adapters: resolveDefaults(configObject.adapters)
|
||||
};
|
||||
await fs.writeFile(
|
||||
configPath,
|
||||
JSON.stringify(resolved, null, 2),
|
||||
"utf8"
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
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");
|
||||
let activeAdapter = null;
|
||||
function createWindow() {
|
||||
const mainWindow = new BrowserWindow({
|
||||
width: DEFAULT_WINDOW_SIZE.width,
|
||||
@@ -152,34 +234,38 @@ app.whenReady().then(async () => {
|
||||
win.webContents.send(event, data);
|
||||
});
|
||||
};
|
||||
const registry = new PluginRegistry();
|
||||
const registry = new PluginRegistry("electron");
|
||||
registry.register(filesystemPlugin);
|
||||
registry.register(supabasePlugin);
|
||||
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);
|
||||
await adapter.init();
|
||||
activeAdapter = plugin.createAdapter(adapterConfig);
|
||||
await activeAdapter.init();
|
||||
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);
|
||||
return true;
|
||||
};
|
||||
await setActivePlugin(initialConfig.activeAdapter);
|
||||
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);
|
||||
});
|
||||
ipcMain.handle("listPlugins", async () => {
|
||||
return registry.list();
|
||||
@@ -211,8 +297,8 @@ app.whenReady().then(async () => {
|
||||
}
|
||||
});
|
||||
electronApp.setAppUserModelId("com.electron");
|
||||
app.on("browser-window-created", (_, window) => {
|
||||
optimizer.watchWindowShortcuts(window);
|
||||
app.on("browser-window-created", (_, window2) => {
|
||||
optimizer.watchWindowShortcuts(window2);
|
||||
});
|
||||
createWindow();
|
||||
app.on("activate", function() {
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Taker of Notes</title>
|
||||
<title>Electron</title>
|
||||
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
|
||||
<meta
|
||||
http-equiv="Content-Security-Policy"
|
||||
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:"
|
||||
/>
|
||||
<script type="module" crossorigin src="./assets/index-dVpT1Jfp.js"></script>
|
||||
<script type="module" crossorigin src="./assets/index-Cj2aGG-N.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="./assets/index-s71dsOUL.css">
|
||||
</head>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user