total restructure: isolate functionality to core so web works
This commit is contained in:
@@ -10,6 +10,7 @@ export default defineConfig({
|
|||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
'@': resolve('src/renderer/src'),
|
'@': resolve('src/renderer/src'),
|
||||||
|
'@core': resolve('src/core'),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ import { electronApp, optimizer, is } from "@electron-toolkit/utils";
|
|||||||
import { app, ipcMain, BrowserWindow, shell } from "electron";
|
import { app, ipcMain, BrowserWindow, shell } from "electron";
|
||||||
import filesystemPlugin from "@takerofnotes/plugin-filesystem";
|
import filesystemPlugin from "@takerofnotes/plugin-filesystem";
|
||||||
import supabasePlugin from "@takerofnotes/plugin-supabase";
|
import supabasePlugin from "@takerofnotes/plugin-supabase";
|
||||||
|
import "libsodium-wrappers";
|
||||||
|
import "uuid";
|
||||||
|
import "flexsearch";
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import path, { join } from "path";
|
import path, { join } from "path";
|
||||||
import __cjs_mod__ from "node:module";
|
import __cjs_mod__ from "node:module";
|
||||||
@@ -10,13 +13,18 @@ const __filename = import.meta.filename;
|
|||||||
const __dirname = import.meta.dirname;
|
const __dirname = import.meta.dirname;
|
||||||
const require2 = __cjs_mod__.createRequire(import.meta.url);
|
const require2 = __cjs_mod__.createRequire(import.meta.url);
|
||||||
class PluginRegistry {
|
class PluginRegistry {
|
||||||
constructor() {
|
constructor(environment = "web") {
|
||||||
this.plugins = /* @__PURE__ */ new Map();
|
this.plugins = /* @__PURE__ */ new Map();
|
||||||
|
this.environment = environment;
|
||||||
}
|
}
|
||||||
register(plugin) {
|
register(plugin) {
|
||||||
if (!plugin.id) {
|
if (!plugin.id) {
|
||||||
throw new Error("Plugin must have an 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);
|
this.plugins.set(plugin.id, plugin);
|
||||||
}
|
}
|
||||||
get(id) {
|
get(id) {
|
||||||
@@ -31,72 +39,146 @@ class PluginRegistry {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const USER_DATA_STRING = "__DEFAULT_USER_DATA__";
|
function createIpcStorage() {
|
||||||
class Config {
|
return {
|
||||||
constructor(defaultPlugin) {
|
async load() {
|
||||||
this.defaultPlugin = defaultPlugin;
|
return await window.api.getConfig();
|
||||||
this.configPath = path.join(app.getPath("userData"), "config.json");
|
},
|
||||||
|
async save(data) {
|
||||||
|
await window.api.setConfig(data);
|
||||||
}
|
}
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
return resolved;
|
function getDefaultConfig() {
|
||||||
} else if (typeof config === "string" && config.includes(USER_DATA_STRING)) {
|
return {
|
||||||
return config.replace(USER_DATA_STRING, app.getPath("userData"));
|
activeAdapter: "supabase",
|
||||||
|
adapters: {
|
||||||
|
supabase: {
|
||||||
|
supabaseUrl: "https://example.supabase.co",
|
||||||
|
supabaseKey: "",
|
||||||
|
bucket: "notes"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
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 {
|
} else {
|
||||||
|
storage = createIpcStorage();
|
||||||
|
}
|
||||||
|
const loadConfig = async () => {
|
||||||
|
if (config !== null) {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
const stored = await storage.load();
|
||||||
|
config = stored || getDefaultConfig();
|
||||||
|
if (!stored) {
|
||||||
|
await storage.save(config);
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return resolved;
|
||||||
|
} else if (typeof obj === "string" && obj.includes(USER_DATA_STRING)) {
|
||||||
|
return obj.replace(USER_DATA_STRING, app.getPath("userData"));
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
return {
|
||||||
async load() {
|
async load() {
|
||||||
let parsed;
|
let parsed;
|
||||||
try {
|
try {
|
||||||
const raw = await fs.readFile(this.configPath, "utf8");
|
const raw = await fs.readFile(configPath, "utf8");
|
||||||
parsed = JSON.parse(raw);
|
parsed = JSON.parse(raw);
|
||||||
} catch (err) {
|
} catch {
|
||||||
parsed = null;
|
parsed = null;
|
||||||
}
|
}
|
||||||
if (!parsed || !parsed.activeAdapter) {
|
if (!parsed || !parsed.activeAdapter) {
|
||||||
const defaultConfig = {};
|
const defaultConfig = {};
|
||||||
for (const field of this.defaultPlugin.configSchema) {
|
if (defaultPlugin) {
|
||||||
|
for (const field of defaultPlugin.configSchema) {
|
||||||
defaultConfig[field.key] = field.default ?? null;
|
defaultConfig[field.key] = field.default ?? null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
parsed = {
|
parsed = {
|
||||||
...parsed ? parsed : {},
|
...parsed ? parsed : {},
|
||||||
activeAdapter: this.defaultPlugin.id,
|
activeAdapter: defaultPlugin?.id || "supabase",
|
||||||
adapters: {}
|
adapters: {},
|
||||||
|
theme: "dark"
|
||||||
};
|
};
|
||||||
parsed.adapters[this.defaultPlugin.id] = defaultConfig;
|
if (defaultPlugin) {
|
||||||
parsed[theme] = "dark";
|
parsed.adapters[defaultPlugin.id] = defaultConfig;
|
||||||
await this.write(parsed);
|
}
|
||||||
|
await this.save(parsed);
|
||||||
} else {
|
} else {
|
||||||
parsed.adapters = this._resolveDefaults(parsed.adapters);
|
parsed.adapters = resolveDefaults(parsed.adapters);
|
||||||
}
|
}
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
},
|
||||||
async write(configObject) {
|
async save(configObject) {
|
||||||
const dir = path.dirname(this.configPath);
|
const dir = path.dirname(configPath);
|
||||||
await fs.mkdir(dir, { recursive: true });
|
await fs.mkdir(dir, { recursive: true });
|
||||||
const resolvedConfig = {
|
const resolved = {
|
||||||
...configObject,
|
...configObject,
|
||||||
adapters: this._resolveDefaults(configObject.adapters)
|
adapters: resolveDefaults(configObject.adapters)
|
||||||
};
|
};
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
this.configPath,
|
configPath,
|
||||||
JSON.stringify(resolvedConfig, null, 2),
|
JSON.stringify(resolved, null, 2),
|
||||||
"utf8"
|
"utf8"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
const DEFAULT_WINDOW_SIZE = { width: 354, height: 549 };
|
const DEFAULT_WINDOW_SIZE = { width: 354, height: 549 };
|
||||||
const DEFAULT_MOVE_WINDOW_SIZE = { width: 708, height: 549 };
|
const DEFAULT_MOVE_WINDOW_SIZE = { width: 708, height: 549 };
|
||||||
const preloadPath = join(__dirname, "../preload/index.mjs");
|
const preloadPath = join(__dirname, "../preload/index.mjs");
|
||||||
const rendererPath = join(__dirname, "../renderer/index.html");
|
const rendererPath = join(__dirname, "../renderer/index.html");
|
||||||
|
let activeAdapter = null;
|
||||||
function createWindow() {
|
function createWindow() {
|
||||||
const mainWindow = new BrowserWindow({
|
const mainWindow = new BrowserWindow({
|
||||||
width: DEFAULT_WINDOW_SIZE.width,
|
width: DEFAULT_WINDOW_SIZE.width,
|
||||||
@@ -152,34 +234,38 @@ app.whenReady().then(async () => {
|
|||||||
win.webContents.send(event, data);
|
win.webContents.send(event, data);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const registry = new PluginRegistry();
|
const registry = new PluginRegistry("electron");
|
||||||
registry.register(filesystemPlugin);
|
registry.register(filesystemPlugin);
|
||||||
registry.register(supabasePlugin);
|
registry.register(supabasePlugin);
|
||||||
const config = new Config(filesystemPlugin);
|
const nodeStorage = createNodeStorage(filesystemPlugin);
|
||||||
const initialConfig = await config.load();
|
const configManager = createConfigManager("electron", nodeStorage);
|
||||||
|
const initialConfig = await configManager.loadConfig();
|
||||||
const setActivePlugin = async (pluginId) => {
|
const setActivePlugin = async (pluginId) => {
|
||||||
const currentConfig = await config.load();
|
const currentConfig = await configManager.loadConfig();
|
||||||
await config.write({ ...currentConfig, activeAdapter: pluginId });
|
await configManager.setConfig({
|
||||||
|
...currentConfig,
|
||||||
|
activeAdapter: pluginId
|
||||||
|
});
|
||||||
const plugin = registry.get(pluginId);
|
const plugin = registry.get(pluginId);
|
||||||
const adapterConfig = currentConfig.adapters[pluginId] || {};
|
const adapterConfig = currentConfig.adapters[pluginId] || {};
|
||||||
const adapter = plugin.createAdapter(adapterConfig);
|
activeAdapter = plugin.createAdapter(adapterConfig);
|
||||||
await adapter.init();
|
await activeAdapter.init();
|
||||||
ipcMain.removeHandler("adapter:call");
|
ipcMain.removeHandler("adapter:call");
|
||||||
ipcMain.handle("adapter:call", async (_, method, args) => {
|
ipcMain.handle("adapter:call", async (_, method, args) => {
|
||||||
if (!adapter[method]) {
|
if (!activeAdapter[method]) {
|
||||||
throw new Error(`Invalid adapter method: ${method}`);
|
throw new Error(`Invalid adapter method: ${method}`);
|
||||||
}
|
}
|
||||||
return await adapter[method](...args);
|
return await activeAdapter[method](...args);
|
||||||
});
|
});
|
||||||
broadcastNoteChange("plugin-changed", pluginId);
|
broadcastNoteChange("plugin-changed", pluginId);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
await setActivePlugin(initialConfig.activeAdapter);
|
await setActivePlugin(initialConfig.activeAdapter);
|
||||||
ipcMain.handle("getConfig", async () => {
|
ipcMain.handle("getConfig", async () => {
|
||||||
return await config.load();
|
return await configManager.loadConfig();
|
||||||
});
|
});
|
||||||
ipcMain.handle("setConfig", async (_, newConfig) => {
|
ipcMain.handle("setConfig", async (_, newConfig) => {
|
||||||
await config.write(newConfig);
|
await configManager.setConfig(newConfig);
|
||||||
});
|
});
|
||||||
ipcMain.handle("listPlugins", async () => {
|
ipcMain.handle("listPlugins", async () => {
|
||||||
return registry.list();
|
return registry.list();
|
||||||
@@ -211,8 +297,8 @@ app.whenReady().then(async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
electronApp.setAppUserModelId("com.electron");
|
electronApp.setAppUserModelId("com.electron");
|
||||||
app.on("browser-window-created", (_, window) => {
|
app.on("browser-window-created", (_, window2) => {
|
||||||
optimizer.watchWindowShortcuts(window);
|
optimizer.watchWindowShortcuts(window2);
|
||||||
});
|
});
|
||||||
createWindow();
|
createWindow();
|
||||||
app.on("activate", function() {
|
app.on("activate", function() {
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<title>Taker of Notes</title>
|
<title>Electron</title>
|
||||||
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
|
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
|
||||||
<meta
|
<meta
|
||||||
http-equiv="Content-Security-Policy"
|
http-equiv="Content-Security-Policy"
|
||||||
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:"
|
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">
|
<link rel="stylesheet" crossorigin href="./assets/index-s71dsOUL.css">
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
|||||||
148
src/core/Config.js
Normal file
148
src/core/Config.js
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
const DB_NAME = 'takerofnotes'
|
||||||
|
const DB_VERSION = 1
|
||||||
|
const STORE_NAME = 'config'
|
||||||
|
const CONFIG_KEY = 'app_config'
|
||||||
|
|
||||||
|
let db = null
|
||||||
|
|
||||||
|
function openDB() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (db) {
|
||||||
|
resolve(db)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const request = indexedDB.open(DB_NAME, DB_VERSION)
|
||||||
|
|
||||||
|
request.onerror = () => reject(request.error)
|
||||||
|
request.onsuccess = () => {
|
||||||
|
db = request.result
|
||||||
|
resolve(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
request.onupgradeneeded = (event) => {
|
||||||
|
const database = event.target.result
|
||||||
|
if (!database.objectStoreNames.contains(STORE_NAME)) {
|
||||||
|
database.createObjectStore(STORE_NAME, { keyPath: 'id' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getFromDB() {
|
||||||
|
const database = await openDB()
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const transaction = database.transaction(STORE_NAME, 'readonly')
|
||||||
|
const store = transaction.objectStore(STORE_NAME)
|
||||||
|
const request = store.get(CONFIG_KEY)
|
||||||
|
|
||||||
|
request.onerror = () => reject(request.error)
|
||||||
|
request.onsuccess = () => resolve(request.result?.data || null)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveToDB(data) {
|
||||||
|
const database = await openDB()
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const transaction = database.transaction(STORE_NAME, 'readwrite')
|
||||||
|
const store = transaction.objectStore(STORE_NAME)
|
||||||
|
const request = store.put({ id: CONFIG_KEY, data })
|
||||||
|
|
||||||
|
request.onerror = () => reject(request.error)
|
||||||
|
request.onsuccess = () => resolve()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function createIndexedDBStorage() {
|
||||||
|
return {
|
||||||
|
async load() {
|
||||||
|
const stored = await getFromDB()
|
||||||
|
return stored || null
|
||||||
|
},
|
||||||
|
async save(data) {
|
||||||
|
await saveToDB(data)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createIpcStorage() {
|
||||||
|
return {
|
||||||
|
async load() {
|
||||||
|
return await window.api.getConfig()
|
||||||
|
},
|
||||||
|
async save(data) {
|
||||||
|
await window.api.setConfig(data)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDefaultConfig() {
|
||||||
|
return {
|
||||||
|
activeAdapter: 'supabase',
|
||||||
|
theme: 'dark',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let config = null
|
||||||
|
let configResolve = null
|
||||||
|
const configPromise = new Promise((resolve) => {
|
||||||
|
configResolve = resolve
|
||||||
|
})
|
||||||
|
|
||||||
|
export function createConfigManager(environment, customStorage = null) {
|
||||||
|
let storage
|
||||||
|
|
||||||
|
if (customStorage) {
|
||||||
|
storage = customStorage
|
||||||
|
} else if (environment === 'electron') {
|
||||||
|
storage = createIpcStorage()
|
||||||
|
} else {
|
||||||
|
storage = createIndexedDBStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadConfig = async () => {
|
||||||
|
if (config !== null) {
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
const stored = await storage.load()
|
||||||
|
config = stored || getDefaultConfig()
|
||||||
|
if (!stored) {
|
||||||
|
await storage.save(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default createConfigManager
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
import sodium from 'libsodium-wrappers'
|
import sodium from 'libsodium-wrappers'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
import { Index } from 'flexsearch'
|
import { Index } from 'flexsearch'
|
||||||
import * as uint from '@/libs/uint'
|
|
||||||
|
|
||||||
export default class NotesAPI {
|
export default class NotesAPI {
|
||||||
constructor(adapter, encryptionKey = null) {
|
constructor(adapter, encryptionKey = null) {
|
||||||
@@ -26,12 +25,44 @@ export default class NotesAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_hexToUint8Array(hex) {
|
||||||
|
const bytes = new Uint8Array(hex.length / 2)
|
||||||
|
for (let i = 0; i < bytes.length; i++) {
|
||||||
|
bytes[i] = parseInt(hex.substr(i * 2, 2), 16)
|
||||||
|
}
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
_concatUint8Arrays(a, b) {
|
||||||
|
const result = new Uint8Array(a.length + b.length)
|
||||||
|
result.set(a, 0)
|
||||||
|
result.set(b, a.length)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
_uint8ArrayToBase64(bytes) {
|
||||||
|
let binary = ''
|
||||||
|
for (let i = 0; i < bytes.length; i++) {
|
||||||
|
binary += String.fromCharCode(bytes[i])
|
||||||
|
}
|
||||||
|
return btoa(binary)
|
||||||
|
}
|
||||||
|
|
||||||
|
_base64ToUint8Array(base64) {
|
||||||
|
const binary = atob(base64)
|
||||||
|
const bytes = new Uint8Array(binary.length)
|
||||||
|
for (let i = 0; i < binary.length; i++) {
|
||||||
|
bytes[i] = binary.charCodeAt(i)
|
||||||
|
}
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
|
|
||||||
_encrypt(note) {
|
_encrypt(note) {
|
||||||
if (!this.encryptionKey) {
|
if (!this.encryptionKey) {
|
||||||
throw new Error('Encryption key not set')
|
throw new Error('Encryption key not set')
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = uint.hexToUint8Array(this.encryptionKey)
|
const key = this._hexToUint8Array(this.encryptionKey)
|
||||||
if (key.length !== 32) {
|
if (key.length !== 32) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Encryption key must be 64 hex characters (32 bytes)',
|
'Encryption key must be 64 hex characters (32 bytes)',
|
||||||
@@ -47,8 +78,8 @@ export default class NotesAPI {
|
|||||||
key,
|
key,
|
||||||
)
|
)
|
||||||
|
|
||||||
const combined = uint.concatUint8Arrays(nonce, ciphertext)
|
const combined = this._concatUint8Arrays(nonce, ciphertext)
|
||||||
return uint.uint8ArrayToBase64(combined)
|
return this._uint8ArrayToBase64(combined)
|
||||||
}
|
}
|
||||||
|
|
||||||
_decrypt(encryptedData) {
|
_decrypt(encryptedData) {
|
||||||
@@ -56,7 +87,7 @@ export default class NotesAPI {
|
|||||||
throw new Error('Encryption key not set')
|
throw new Error('Encryption key not set')
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = uint.hexToUint8Array(this.encryptionKey)
|
const key = this._hexToUint8Array(this.encryptionKey)
|
||||||
if (key.length !== 32) {
|
if (key.length !== 32) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Encryption key must be 64 hex characters (32 bytes)',
|
'Encryption key must be 64 hex characters (32 bytes)',
|
||||||
@@ -65,7 +96,7 @@ export default class NotesAPI {
|
|||||||
|
|
||||||
let combined
|
let combined
|
||||||
try {
|
try {
|
||||||
combined = uint.base64ToUint8Array(encryptedData)
|
combined = this._base64ToUint8Array(encryptedData)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error('Invalid encrypted data: not valid base64')
|
throw new Error('Invalid encrypted data: not valid base64')
|
||||||
}
|
}
|
||||||
@@ -141,9 +172,6 @@ export default class NotesAPI {
|
|||||||
return extractText(content)
|
return extractText(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------
|
|
||||||
Public API
|
|
||||||
------------------------*/
|
|
||||||
getCategories() {
|
getCategories() {
|
||||||
const categories = new Set()
|
const categories = new Set()
|
||||||
|
|
||||||
56
src/core/PluginManager.js
Normal file
56
src/core/PluginManager.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import PluginRegistry from './PluginRegistry.js'
|
||||||
|
import IpcAdapter from './IpcAdapter.js'
|
||||||
|
|
||||||
|
let registry = null
|
||||||
|
let activePluginId = null
|
||||||
|
let adapter = null
|
||||||
|
|
||||||
|
export default function createPluginManager(environment, plugins = []) {
|
||||||
|
registry = new PluginRegistry(environment)
|
||||||
|
|
||||||
|
for (const plugin of plugins) {
|
||||||
|
registry.register(plugin)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
listPlugins() {
|
||||||
|
return registry.list()
|
||||||
|
},
|
||||||
|
|
||||||
|
getPlugin(pluginId) {
|
||||||
|
return registry.get(pluginId)
|
||||||
|
},
|
||||||
|
|
||||||
|
getAdapter(pluginId, adapterConfig = {}) {
|
||||||
|
const plugin = registry.get(pluginId)
|
||||||
|
|
||||||
|
if (environment === 'electron') {
|
||||||
|
return new IpcAdapter()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!plugin) {
|
||||||
|
throw new Error(`Plugin not found: ${pluginId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return plugin.createAdapter(adapterConfig)
|
||||||
|
},
|
||||||
|
|
||||||
|
setActivePlugin(pluginId, adapterConfig = {}) {
|
||||||
|
activePluginId = pluginId
|
||||||
|
adapter = this.getAdapter(pluginId, adapterConfig)
|
||||||
|
return adapter
|
||||||
|
},
|
||||||
|
|
||||||
|
getActiveAdapter() {
|
||||||
|
return adapter
|
||||||
|
},
|
||||||
|
|
||||||
|
getActivePluginId() {
|
||||||
|
return activePluginId
|
||||||
|
},
|
||||||
|
|
||||||
|
getEnvironment() {
|
||||||
|
return environment
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
export default class PluginRegistry {
|
export default class PluginRegistry {
|
||||||
constructor() {
|
constructor(environment = 'web') {
|
||||||
this.plugins = new Map()
|
this.plugins = new Map()
|
||||||
|
this.environment = environment
|
||||||
}
|
}
|
||||||
|
|
||||||
register(plugin) {
|
register(plugin) {
|
||||||
@@ -8,6 +9,11 @@ export default class PluginRegistry {
|
|||||||
throw new Error('Plugin must have an 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)
|
this.plugins.set(plugin.id, plugin)
|
||||||
}
|
}
|
||||||
|
|
||||||
77
src/core/index.js
Normal file
77
src/core/index.js
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import createPluginManager from './PluginManager.js'
|
||||||
|
import createConfigManager from './Config.js'
|
||||||
|
import NotesAPI from './NotesAPI.js'
|
||||||
|
|
||||||
|
const generateEncryptionKey = () => {
|
||||||
|
const array = new Uint8Array(32)
|
||||||
|
crypto.getRandomValues(array)
|
||||||
|
return Array.from(array)
|
||||||
|
.map((b) => b.toString(16).padStart(2, '0'))
|
||||||
|
.join('')
|
||||||
|
}
|
||||||
|
|
||||||
|
let coreInstance = null
|
||||||
|
|
||||||
|
export function initializeCore(environment, plugins = []) {
|
||||||
|
const pluginManager = createPluginManager(environment, plugins)
|
||||||
|
const configManager = createConfigManager(environment)
|
||||||
|
|
||||||
|
let notesAPI = null
|
||||||
|
let initPromise = null
|
||||||
|
|
||||||
|
const getNotesAPI = async () => {
|
||||||
|
if (notesAPI) return notesAPI
|
||||||
|
|
||||||
|
if (!initPromise) {
|
||||||
|
initPromise = (async () => {
|
||||||
|
const config = await configManager.loadConfig()
|
||||||
|
|
||||||
|
let encryptionKey = config?.encryptionKey
|
||||||
|
|
||||||
|
if (!encryptionKey) {
|
||||||
|
encryptionKey = generateEncryptionKey()
|
||||||
|
await configManager.setConfig({ ...config, encryptionKey })
|
||||||
|
}
|
||||||
|
|
||||||
|
const pluginId = config?.activeAdapter || 'filesystem'
|
||||||
|
const adapterConfig =
|
||||||
|
config.adapters?.[config.activeAdapter] || {}
|
||||||
|
const adapter = pluginManager.getAdapter(
|
||||||
|
pluginId,
|
||||||
|
adapterConfig,
|
||||||
|
)
|
||||||
|
|
||||||
|
notesAPI = new NotesAPI(adapter, encryptionKey)
|
||||||
|
await notesAPI.init()
|
||||||
|
|
||||||
|
return notesAPI
|
||||||
|
})()
|
||||||
|
}
|
||||||
|
|
||||||
|
return initPromise
|
||||||
|
}
|
||||||
|
|
||||||
|
coreInstance = {
|
||||||
|
environment,
|
||||||
|
pluginManager,
|
||||||
|
configManager,
|
||||||
|
getNotesAPI,
|
||||||
|
}
|
||||||
|
|
||||||
|
return coreInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCore() {
|
||||||
|
if (!coreInstance) {
|
||||||
|
throw new Error('Core not initialized. Call initializeCore() first.')
|
||||||
|
}
|
||||||
|
return coreInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
export { default as PluginRegistry } from './PluginRegistry.js'
|
||||||
|
export { default as IpcAdapter } from './IpcAdapter.js'
|
||||||
|
export { default as NotesAPI } from './NotesAPI.js'
|
||||||
|
export {
|
||||||
|
default as createConfigManager,
|
||||||
|
createConfigManager as createConfig,
|
||||||
|
} from './Config.js'
|
||||||
4
src/core/types.ts
Normal file
4
src/core/types.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export enum ENVIRONMENTS {
|
||||||
|
ELECTRON = 'electron',
|
||||||
|
WEB = 'web',
|
||||||
|
}
|
||||||
79
src/main/NodeStorage.js
Normal file
79
src/main/NodeStorage.js
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import fs from 'fs/promises'
|
||||||
|
import path from 'path'
|
||||||
|
import { app } from 'electron'
|
||||||
|
|
||||||
|
const USER_DATA_STRING = '__DEFAULT_USER_DATA__'
|
||||||
|
|
||||||
|
export 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)
|
||||||
|
}
|
||||||
|
return resolved
|
||||||
|
} else if (typeof obj === 'string' && obj.includes(USER_DATA_STRING)) {
|
||||||
|
return obj.replace(USER_DATA_STRING, app.getPath('userData'))
|
||||||
|
}
|
||||||
|
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',
|
||||||
|
)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default createNodeStorage
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
import fs from 'fs/promises'
|
|
||||||
import path from 'path'
|
|
||||||
import { app } from 'electron'
|
|
||||||
|
|
||||||
const USER_DATA_STRING = '__DEFAULT_USER_DATA__'
|
|
||||||
|
|
||||||
export default 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)
|
|
||||||
}
|
|
||||||
return resolved
|
|
||||||
} else if (
|
|
||||||
typeof config === 'string' &&
|
|
||||||
config.includes(USER_DATA_STRING)
|
|
||||||
) {
|
|
||||||
return config.replace(USER_DATA_STRING, app.getPath('userData'))
|
|
||||||
} else {
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async load() {
|
|
||||||
let parsed
|
|
||||||
|
|
||||||
try {
|
|
||||||
const raw = await fs.readFile(this.configPath, 'utf8')
|
|
||||||
parsed = JSON.parse(raw)
|
|
||||||
} catch (err) {
|
|
||||||
parsed = null
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!parsed || !parsed.activeAdapter) {
|
|
||||||
const defaultConfig = {}
|
|
||||||
|
|
||||||
for (const field of this.defaultPlugin.configSchema) {
|
|
||||||
defaultConfig[field.key] = field.default ?? null
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed = {
|
|
||||||
...(parsed ? parsed : {}),
|
|
||||||
activeAdapter: this.defaultPlugin.id,
|
|
||||||
adapters: {},
|
|
||||||
}
|
|
||||||
parsed.adapters[this.defaultPlugin.id] = defaultConfig
|
|
||||||
|
|
||||||
parsed[theme] = 'dark'
|
|
||||||
|
|
||||||
await this.write(parsed)
|
|
||||||
} else {
|
|
||||||
// Ensure any "__DEFAULT_USER_DATA__" values are resolved on load
|
|
||||||
parsed.adapters = this._resolveDefaults(parsed.adapters)
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsed
|
|
||||||
}
|
|
||||||
|
|
||||||
async write(configObject) {
|
|
||||||
const dir = path.dirname(this.configPath)
|
|
||||||
|
|
||||||
// Ensure directory exists
|
|
||||||
await fs.mkdir(dir, { recursive: true })
|
|
||||||
|
|
||||||
// Resolve defaults before writing
|
|
||||||
const resolvedConfig = {
|
|
||||||
...configObject,
|
|
||||||
adapters: this._resolveDefaults(configObject.adapters),
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.writeFile(
|
|
||||||
this.configPath,
|
|
||||||
JSON.stringify(resolvedConfig, null, 2),
|
|
||||||
'utf8',
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,8 +3,8 @@ import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
|||||||
import { app, shell, BrowserWindow, ipcMain } from 'electron'
|
import { app, shell, BrowserWindow, ipcMain } from 'electron'
|
||||||
import filesystemPlugin from '@takerofnotes/plugin-filesystem'
|
import filesystemPlugin from '@takerofnotes/plugin-filesystem'
|
||||||
import supabasePlugin from '@takerofnotes/plugin-supabase'
|
import supabasePlugin from '@takerofnotes/plugin-supabase'
|
||||||
import PluginRegistry from './core/PluginRegistry.js'
|
import { PluginRegistry, createConfigManager } from '../core/index.js'
|
||||||
import Config from './core/Config.js'
|
import { createNodeStorage } from './NodeStorage.js'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
|
|
||||||
const DEFAULT_WINDOW_SIZE = { width: 354, height: 549 }
|
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 preloadPath = join(__dirname, '../preload/index.mjs')
|
||||||
const rendererPath = join(__dirname, '../renderer/index.html')
|
const rendererPath = join(__dirname, '../renderer/index.html')
|
||||||
|
|
||||||
// Main window
|
let activeAdapter = null
|
||||||
|
|
||||||
function createWindow() {
|
function createWindow() {
|
||||||
const mainWindow = new BrowserWindow({
|
const mainWindow = new BrowserWindow({
|
||||||
width: DEFAULT_WINDOW_SIZE.width,
|
width: DEFAULT_WINDOW_SIZE.width,
|
||||||
@@ -42,7 +43,6 @@ function createWindow() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open note in new window
|
|
||||||
function createNoteWindow(noteId) {
|
function createNoteWindow(noteId) {
|
||||||
const noteWindow = new BrowserWindow({
|
const noteWindow = new BrowserWindow({
|
||||||
width: DEFAULT_WINDOW_SIZE.width,
|
width: DEFAULT_WINDOW_SIZE.width,
|
||||||
@@ -68,48 +68,44 @@ function createNoteWindow(noteId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(async () => {
|
app.whenReady().then(async () => {
|
||||||
// Open note in new window
|
|
||||||
ipcMain.on('open-note-window', (_, noteId) => {
|
ipcMain.on('open-note-window', (_, noteId) => {
|
||||||
createNoteWindow(noteId)
|
createNoteWindow(noteId)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Broadcast note changes to all windows
|
|
||||||
const broadcastNoteChange = (event, data) => {
|
const broadcastNoteChange = (event, data) => {
|
||||||
BrowserWindow.getAllWindows().forEach((win) => {
|
BrowserWindow.getAllWindows().forEach((win) => {
|
||||||
win.webContents.send(event, data)
|
win.webContents.send(event, data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create plugin registry
|
const registry = new PluginRegistry('electron')
|
||||||
const registry = new PluginRegistry()
|
|
||||||
|
|
||||||
// Register built-in plugins
|
|
||||||
registry.register(filesystemPlugin)
|
registry.register(filesystemPlugin)
|
||||||
registry.register(supabasePlugin)
|
registry.register(supabasePlugin)
|
||||||
|
|
||||||
// Pull plugin config
|
const nodeStorage = createNodeStorage(filesystemPlugin)
|
||||||
const config = new Config(filesystemPlugin)
|
const configManager = createConfigManager('electron', nodeStorage)
|
||||||
const initialConfig = await config.load()
|
const initialConfig = await configManager.loadConfig()
|
||||||
|
|
||||||
const setActivePlugin = async (pluginId) => {
|
const setActivePlugin = async (pluginId) => {
|
||||||
const currentConfig = await config.load()
|
const currentConfig = await configManager.loadConfig()
|
||||||
await config.write({ ...currentConfig, activeAdapter: pluginId })
|
await configManager.setConfig({
|
||||||
|
...currentConfig,
|
||||||
|
activeAdapter: pluginId,
|
||||||
|
})
|
||||||
|
|
||||||
const plugin = registry.get(pluginId)
|
const plugin = registry.get(pluginId)
|
||||||
const adapterConfig = currentConfig.adapters[pluginId] || {}
|
const adapterConfig = currentConfig.adapters[pluginId] || {}
|
||||||
const adapter = plugin.createAdapter(adapterConfig)
|
activeAdapter = plugin.createAdapter(adapterConfig)
|
||||||
|
|
||||||
// Initialize adapter
|
await activeAdapter.init()
|
||||||
await adapter.init()
|
|
||||||
|
|
||||||
// Handle adapter methods via IPC
|
|
||||||
ipcMain.removeHandler('adapter:call')
|
ipcMain.removeHandler('adapter:call')
|
||||||
ipcMain.handle('adapter:call', async (_, method, args) => {
|
ipcMain.handle('adapter:call', async (_, method, args) => {
|
||||||
if (!adapter[method]) {
|
if (!activeAdapter[method]) {
|
||||||
throw new Error(`Invalid adapter method: ${method}`)
|
throw new Error(`Invalid adapter method: ${method}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return await adapter[method](...args)
|
return await activeAdapter[method](...args)
|
||||||
})
|
})
|
||||||
|
|
||||||
broadcastNoteChange('plugin-changed', pluginId)
|
broadcastNoteChange('plugin-changed', pluginId)
|
||||||
@@ -117,18 +113,15 @@ app.whenReady().then(async () => {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set active plugin
|
|
||||||
await setActivePlugin(initialConfig.activeAdapter)
|
await setActivePlugin(initialConfig.activeAdapter)
|
||||||
|
|
||||||
// Get/set config
|
|
||||||
ipcMain.handle('getConfig', async () => {
|
ipcMain.handle('getConfig', async () => {
|
||||||
return await config.load()
|
return await configManager.loadConfig()
|
||||||
})
|
})
|
||||||
ipcMain.handle('setConfig', async (_, newConfig) => {
|
ipcMain.handle('setConfig', async (_, newConfig) => {
|
||||||
await config.write(newConfig)
|
await configManager.setConfig(newConfig)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Get/set plugins
|
|
||||||
ipcMain.handle('listPlugins', async () => {
|
ipcMain.handle('listPlugins', async () => {
|
||||||
return registry.list()
|
return registry.list()
|
||||||
})
|
})
|
||||||
@@ -136,12 +129,10 @@ app.whenReady().then(async () => {
|
|||||||
return await setActivePlugin(pluginId)
|
return await setActivePlugin(pluginId)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handle note change events from renderer
|
|
||||||
ipcMain.on('note-changed', (_, event, data) => {
|
ipcMain.on('note-changed', (_, event, data) => {
|
||||||
broadcastNoteChange(event, data)
|
broadcastNoteChange(event, data)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handle resizing for note "move" functionality
|
|
||||||
ipcMain.handle('move-opened', (_) => {
|
ipcMain.handle('move-opened', (_) => {
|
||||||
const activeWindow = BrowserWindow.getFocusedWindow()
|
const activeWindow = BrowserWindow.getFocusedWindow()
|
||||||
const windowSize = activeWindow.getSize()
|
const windowSize = activeWindow.getSize()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { ref, watch, toRaw, onMounted } from 'vue'
|
import { ref, watch, toRaw, onMounted } from 'vue'
|
||||||
|
import { getCore } from '@core/index.js'
|
||||||
|
|
||||||
const config = ref()
|
const config = ref()
|
||||||
let configResolve = null
|
let configResolve = null
|
||||||
@@ -7,19 +8,24 @@ const configPromise = new Promise((resolve) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
|
const core = getCore()
|
||||||
|
const { configManager } = core
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
if (config.value) {
|
if (config.value) {
|
||||||
configResolve()
|
configResolve()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
config.value = await window.api.getConfig()
|
|
||||||
|
const loadedConfig = await configManager.loadConfig()
|
||||||
|
config.value = loadedConfig
|
||||||
configResolve()
|
configResolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
config,
|
config,
|
||||||
async (newValue) => {
|
async (newValue) => {
|
||||||
await window.api.setConfig(toRaw(newValue))
|
await configManager.setConfig(toRaw(newValue))
|
||||||
},
|
},
|
||||||
{ deep: true },
|
{ deep: true },
|
||||||
)
|
)
|
||||||
@@ -30,7 +36,9 @@ export default () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const refreshConfig = async () => {
|
const refreshConfig = async () => {
|
||||||
config.value = await window.api.getConfig()
|
await configManager.refreshConfig()
|
||||||
|
const newConfig = await configManager.getConfig()
|
||||||
|
config.value = newConfig
|
||||||
configResolve()
|
configResolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import _omit from 'lodash/omit'
|
import _omit from 'lodash/omit'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { getNotesAPI } from '@/libs/core/getNotesAPI'
|
import { getNotesAPI } from '@/libs/getNotesAPI'
|
||||||
|
import { useEnvironment } from '@/composables/useEnvironment.js'
|
||||||
|
|
||||||
const categories = ref([])
|
const categories = ref([])
|
||||||
const searchResults = ref([])
|
const searchResults = ref([])
|
||||||
@@ -8,6 +9,8 @@ const notesChangeCount = ref(0)
|
|||||||
|
|
||||||
let listenersInitialized = false
|
let listenersInitialized = false
|
||||||
|
|
||||||
|
const environment = useEnvironment()
|
||||||
|
|
||||||
const setupListeners = () => {
|
const setupListeners = () => {
|
||||||
if (listenersInitialized || typeof window === 'undefined') return
|
if (listenersInitialized || typeof window === 'undefined') return
|
||||||
listenersInitialized = true
|
listenersInitialized = true
|
||||||
@@ -41,7 +44,9 @@ const broadcastChange = (event, data) => {
|
|||||||
window.api.notifyNoteChanged(event, data)
|
window.api.notifyNoteChanged(event, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (environment === 'electron') {
|
||||||
setupListeners()
|
setupListeners()
|
||||||
|
}
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
/* -------------------------
|
/* -------------------------
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
import NotesAPI from '@/libs/core/NotesAPI.js'
|
|
||||||
import IpcAdapter from '@/libs/core/IpcAdapter.js'
|
|
||||||
import useConfig from '@/composables/useConfig.js'
|
|
||||||
|
|
||||||
// Singleton pattern to make sure only one instance of NotesAPI exists
|
|
||||||
let notesAPI = null
|
|
||||||
let initPromise = null
|
|
||||||
|
|
||||||
const generateEncryptionKey = () => {
|
|
||||||
const array = new Uint8Array(32)
|
|
||||||
crypto.getRandomValues(array)
|
|
||||||
return Array.from(array)
|
|
||||||
.map((b) => b.toString(16).padStart(2, '0'))
|
|
||||||
.join('')
|
|
||||||
}
|
|
||||||
|
|
||||||
const createInstance = async () => {
|
|
||||||
const { config, ensureConfig } = useConfig()
|
|
||||||
await ensureConfig()
|
|
||||||
|
|
||||||
let encryptionKey = config.value?.encryptionKey
|
|
||||||
|
|
||||||
if (!encryptionKey) {
|
|
||||||
encryptionKey = generateEncryptionKey()
|
|
||||||
config.value.encryptionKey = encryptionKey
|
|
||||||
}
|
|
||||||
|
|
||||||
const adapter = new IpcAdapter()
|
|
||||||
const api = new NotesAPI(adapter, encryptionKey)
|
|
||||||
|
|
||||||
await api.init()
|
|
||||||
|
|
||||||
return api
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getNotesAPI = async () => {
|
|
||||||
if (notesAPI) return notesAPI
|
|
||||||
|
|
||||||
if (!initPromise) {
|
|
||||||
initPromise = createInstance().then((api) => {
|
|
||||||
notesAPI = api
|
|
||||||
return api
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return initPromise
|
|
||||||
}
|
|
||||||
18
src/renderer/src/libs/getNotesAPI.js
Normal file
18
src/renderer/src/libs/getNotesAPI.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { getCore } from '@core/index.js'
|
||||||
|
|
||||||
|
let notesAPI = null
|
||||||
|
let initPromise = null
|
||||||
|
|
||||||
|
export const getNotesAPI = async () => {
|
||||||
|
if (notesAPI) return notesAPI
|
||||||
|
|
||||||
|
if (!initPromise) {
|
||||||
|
initPromise = (async () => {
|
||||||
|
const core = getCore()
|
||||||
|
notesAPI = await core.getNotesAPI()
|
||||||
|
return notesAPI
|
||||||
|
})()
|
||||||
|
}
|
||||||
|
|
||||||
|
return initPromise
|
||||||
|
}
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
import './styles/main.scss'
|
import './styles/main.scss'
|
||||||
import { createApp } from 'vue'
|
import { createApp } from 'vue'
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
|
import { initCore } from './plugins/core'
|
||||||
import { router } from './plugins/router'
|
import { router } from './plugins/router'
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
|
|
||||||
// Plugins
|
app.use(initCore)
|
||||||
app.use(router)
|
app.use(router)
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|||||||
12
src/renderer/src/plugins/core.js
Normal file
12
src/renderer/src/plugins/core.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { useEnvironment } from '@/composables/useEnvironment'
|
||||||
|
import { initializeCore } from '@core/index.js'
|
||||||
|
import supabasePlugin from '@takerofnotes/plugin-supabase'
|
||||||
|
|
||||||
|
export const initCore = () => {
|
||||||
|
const environment = useEnvironment()
|
||||||
|
|
||||||
|
// Plugins that are valid for web (electron uses IPC)
|
||||||
|
const plugins = [supabasePlugin]
|
||||||
|
|
||||||
|
initializeCore(environment, plugins)
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ export default defineConfig({
|
|||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
'@': fileURLToPath(new URL('./src/renderer/src', import.meta.url)),
|
'@': fileURLToPath(new URL('./src/renderer/src', import.meta.url)),
|
||||||
|
'@core': fileURLToPath(new URL('./src/core', import.meta.url)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
|
|||||||
Reference in New Issue
Block a user