core refactor for runtime support
This commit is contained in:
47
.github/workflows/build.yml
vendored
47
.github/workflows/build.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: Build Electron App
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags:
|
|
||||||
- 'v*'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- os: windows-latest
|
|
||||||
script: build:win
|
|
||||||
- os: macos-latest
|
|
||||||
script: build:mac
|
|
||||||
- os: ubuntu-latest
|
|
||||||
script: build:linux
|
|
||||||
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Setup Node
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 22
|
|
||||||
cache: npm
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: npm ci
|
|
||||||
|
|
||||||
- name: Run platform build
|
|
||||||
run: npm run ${{ matrix.script }}
|
|
||||||
env:
|
|
||||||
CSC_LINK: ${{ secrets.CSC_LINK }}
|
|
||||||
CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
|
|
||||||
|
|
||||||
- name: Upload artifacts
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: ${{ matrix.os }}-build
|
|
||||||
path: |
|
|
||||||
dist/**
|
|
||||||
@@ -3,28 +3,61 @@ 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 sodium from "libsodium-wrappers";
|
||||||
import "uuid";
|
import { v4 } from "uuid";
|
||||||
import "flexsearch";
|
import { Index } from "flexsearch";
|
||||||
import fs from "fs/promises";
|
import { join } from "path";
|
||||||
import path, { join } from "path";
|
|
||||||
import __cjs_mod__ from "node:module";
|
import __cjs_mod__ from "node:module";
|
||||||
const __filename = import.meta.filename;
|
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);
|
||||||
|
const createPluginManager = (registry) => {
|
||||||
|
let activePluginId = null;
|
||||||
|
let adapter = null;
|
||||||
|
return {
|
||||||
|
listPlugins() {
|
||||||
|
return registry.list();
|
||||||
|
},
|
||||||
|
getPlugin(pluginId) {
|
||||||
|
return registry.get(pluginId);
|
||||||
|
},
|
||||||
|
getAdapter(pluginId, adapterConfig = {}) {
|
||||||
|
const plugin = registry.get(pluginId);
|
||||||
|
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;
|
||||||
|
},
|
||||||
|
async testPlugin(pluginId, adapterConfig = {}) {
|
||||||
|
const plugin = registry.get(pluginId);
|
||||||
|
if (!plugin) {
|
||||||
|
throw new Error(`Plugin not found: ${pluginId}`);
|
||||||
|
}
|
||||||
|
const adapter2 = this.getAdapter(pluginId, adapterConfig);
|
||||||
|
await adapter2.init();
|
||||||
|
return await adapter2.testConnection();
|
||||||
|
},
|
||||||
|
getActiveAdapter() {
|
||||||
|
return adapter;
|
||||||
|
},
|
||||||
|
getActivePluginId() {
|
||||||
|
return activePluginId;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
class PluginRegistry {
|
class PluginRegistry {
|
||||||
constructor(environment = "web") {
|
constructor() {
|
||||||
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) {
|
||||||
@@ -39,146 +72,314 @@ class PluginRegistry {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function createIpcStorage() {
|
const getDefaultConfig = () => {
|
||||||
return {
|
|
||||||
async load() {
|
|
||||||
return await window.api.getConfig();
|
|
||||||
},
|
|
||||||
async save(data) {
|
|
||||||
await window.api.setConfig(data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
function getDefaultConfig() {
|
|
||||||
return {
|
return {
|
||||||
activeAdapter: "supabase",
|
activeAdapter: "supabase",
|
||||||
adapters: {
|
theme: "dark"
|
||||||
supabase: {
|
|
||||||
supabaseUrl: "https://example.supabase.co",
|
|
||||||
supabaseKey: "",
|
|
||||||
bucket: "notes"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
theme: "light"
|
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
const createConfigManager = (storage) => {
|
||||||
let config = null;
|
let config = null;
|
||||||
let configResolve = null;
|
return {
|
||||||
const configPromise = new Promise((resolve) => {
|
async loadConfig() {
|
||||||
configResolve = resolve;
|
if (config) return config;
|
||||||
});
|
|
||||||
function createConfigManager(environment, customStorage = null) {
|
|
||||||
let storage;
|
|
||||||
if (customStorage) {
|
|
||||||
storage = customStorage;
|
|
||||||
} else {
|
|
||||||
storage = createIpcStorage();
|
|
||||||
}
|
|
||||||
const loadConfig = async () => {
|
|
||||||
if (config !== null) {
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
const stored = await storage.load();
|
const stored = await storage.load();
|
||||||
config = stored || getDefaultConfig();
|
config = stored || getDefaultConfig();
|
||||||
if (!stored) {
|
if (!stored) {
|
||||||
await storage.save(config);
|
await storage.save(config);
|
||||||
}
|
}
|
||||||
configResolve(config);
|
|
||||||
return 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() {
|
|
||||||
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) {
|
getConfig() {
|
||||||
const dir = path.dirname(configPath);
|
return config;
|
||||||
await fs.mkdir(dir, { recursive: true });
|
},
|
||||||
const resolved = {
|
async setConfig(newConfig) {
|
||||||
...configObject,
|
config = newConfig;
|
||||||
adapters: resolveDefaults(configObject.adapters)
|
await storage.save(newConfig);
|
||||||
|
},
|
||||||
|
async refreshConfig() {
|
||||||
|
config = await storage.load();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
await fs.writeFile(
|
};
|
||||||
configPath,
|
class NotesAPI {
|
||||||
JSON.stringify(resolved, null, 2),
|
constructor(adapter, encryptionKey = null) {
|
||||||
"utf8"
|
if (!adapter) {
|
||||||
|
throw new Error("NotesAPI requires a storage adapter");
|
||||||
|
}
|
||||||
|
this.adapter = adapter;
|
||||||
|
this.notesCache = /* @__PURE__ */ new Map();
|
||||||
|
this.encryptionKey = encryptionKey;
|
||||||
|
this._sodiumReady = false;
|
||||||
|
this.index = new Index({
|
||||||
|
tokenize: "forward"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async _initSodium() {
|
||||||
|
if (!this._sodiumReady) {
|
||||||
|
await sodium.ready;
|
||||||
|
this._sodiumReady = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_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) {
|
||||||
|
if (!this.encryptionKey) {
|
||||||
|
throw new Error("Encryption key not set");
|
||||||
|
}
|
||||||
|
const key = this._hexToUint8Array(this.encryptionKey);
|
||||||
|
if (key.length !== 32) {
|
||||||
|
throw new Error(
|
||||||
|
"Encryption key must be 64 hex characters (32 bytes)"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
const nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
|
||||||
|
const message = JSON.stringify(note);
|
||||||
|
const ciphertext = sodium.crypto_secretbox_easy(
|
||||||
|
new TextEncoder().encode(message),
|
||||||
|
nonce,
|
||||||
|
key
|
||||||
|
);
|
||||||
|
const combined = this._concatUint8Arrays(nonce, ciphertext);
|
||||||
|
return this._uint8ArrayToBase64(combined);
|
||||||
}
|
}
|
||||||
|
_decrypt(encryptedData) {
|
||||||
|
if (!this.encryptionKey) {
|
||||||
|
throw new Error("Encryption key not set");
|
||||||
|
}
|
||||||
|
const key = this._hexToUint8Array(this.encryptionKey);
|
||||||
|
if (key.length !== 32) {
|
||||||
|
throw new Error(
|
||||||
|
"Encryption key must be 64 hex characters (32 bytes)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let combined;
|
||||||
|
try {
|
||||||
|
combined = this._base64ToUint8Array(encryptedData);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error("Invalid encrypted data: not valid base64");
|
||||||
|
}
|
||||||
|
if (combined.length < sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES) {
|
||||||
|
throw new Error("Invalid encrypted data: too short");
|
||||||
|
}
|
||||||
|
const nonce = combined.slice(0, sodium.crypto_secretbox_NONCEBYTES);
|
||||||
|
const ciphertext = combined.slice(sodium.crypto_secretbox_NONCEBYTES);
|
||||||
|
let decrypted;
|
||||||
|
try {
|
||||||
|
decrypted = sodium.crypto_secretbox_open_easy(
|
||||||
|
ciphertext,
|
||||||
|
nonce,
|
||||||
|
key
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error("Decryption failed: wrong key or corrupted data");
|
||||||
|
}
|
||||||
|
if (!decrypted) {
|
||||||
|
throw new Error("Decryption failed: no data returned");
|
||||||
|
}
|
||||||
|
const decryptedStr = new TextDecoder().decode(decrypted);
|
||||||
|
try {
|
||||||
|
return JSON.parse(decryptedStr);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(
|
||||||
|
`Decryption succeeded but invalid JSON: ${decryptedStr}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async init() {
|
||||||
|
await this._initSodium();
|
||||||
|
await this.adapter.init();
|
||||||
|
this.notesCache.clear();
|
||||||
|
const encryptedNotes = await this.adapter.getAll();
|
||||||
|
for (const encryptedNote of encryptedNotes) {
|
||||||
|
try {
|
||||||
|
const note = this._decrypt(encryptedNote.data || encryptedNote);
|
||||||
|
this.notesCache.set(note.id, note);
|
||||||
|
const searchText = note.plainText || this._extractPlainText(note.content);
|
||||||
|
this.index.add(note.id, note.title + "\n" + searchText);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to decrypt note:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_extractPlainText(content) {
|
||||||
|
if (!content) return "";
|
||||||
|
if (typeof content === "string") return content;
|
||||||
|
const extractText = (node) => {
|
||||||
|
if (typeof node === "string") return node;
|
||||||
|
if (!node || !node.content) return "";
|
||||||
|
return node.content.map(extractText).join(" ");
|
||||||
|
};
|
||||||
|
return extractText(content);
|
||||||
|
}
|
||||||
|
getCategories() {
|
||||||
|
const categories = /* @__PURE__ */ new Set();
|
||||||
|
for (const note of this.notesCache.values()) {
|
||||||
|
if (note.category) {
|
||||||
|
categories.add(note.category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Array.from(categories).sort();
|
||||||
|
}
|
||||||
|
getCategoryNotes(categoryName = null) {
|
||||||
|
return Array.from(this.notesCache.values()).filter((n) => n.category === categoryName).sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt)).map((n) => ({ ...n }));
|
||||||
|
}
|
||||||
|
getNote(id) {
|
||||||
|
const note = this.notesCache.get(id);
|
||||||
|
return note ? { ...note } : null;
|
||||||
|
}
|
||||||
|
async createNote(metadata = {}, content = "", plainText = "") {
|
||||||
|
const id = v4();
|
||||||
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
||||||
|
const note = {
|
||||||
|
id,
|
||||||
|
title: metadata.title || "Untitled",
|
||||||
|
category: metadata.category || null,
|
||||||
|
createdAt: now,
|
||||||
|
updatedAt: now,
|
||||||
|
content,
|
||||||
|
plainText
|
||||||
|
};
|
||||||
|
const encryptedNote = {
|
||||||
|
id: note.id,
|
||||||
|
data: this._encrypt(note)
|
||||||
|
};
|
||||||
|
this.notesCache.set(id, note);
|
||||||
|
this.index.add(id, note.title + "\n" + plainText);
|
||||||
|
await this.adapter.create(encryptedNote);
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
async deleteNote(id) {
|
||||||
|
await this.adapter.delete(id);
|
||||||
|
this.notesCache.delete(id);
|
||||||
|
this.index.remove(id);
|
||||||
|
}
|
||||||
|
async updateNote(id, updates = {}) {
|
||||||
|
const note = this.notesCache.get(id);
|
||||||
|
if (!note) throw new Error("Note not found");
|
||||||
|
const allowedFields = ["title", "category", "content", "plainText"];
|
||||||
|
for (const key of Object.keys(updates)) {
|
||||||
|
if (!allowedFields.includes(key)) {
|
||||||
|
throw new Error(`Invalid update field: ${key}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const updatedNote = {
|
||||||
|
...note,
|
||||||
|
...updates,
|
||||||
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
||||||
|
};
|
||||||
|
const encryptedNote = {
|
||||||
|
id: updatedNote.id,
|
||||||
|
data: this._encrypt(updatedNote)
|
||||||
|
};
|
||||||
|
this.notesCache.set(id, updatedNote);
|
||||||
|
const searchText = updatedNote.plainText || this._extractPlainText(updatedNote.content);
|
||||||
|
this.index.update(id, updatedNote.title + "\n" + searchText);
|
||||||
|
await this.adapter.update(encryptedNote);
|
||||||
|
return updatedNote;
|
||||||
|
}
|
||||||
|
search(query) {
|
||||||
|
const ids = this.index.search(query, {
|
||||||
|
limit: 50,
|
||||||
|
suggest: true
|
||||||
|
});
|
||||||
|
return ids.map((id) => this.notesCache.get(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const generateEncryptionKey = () => {
|
||||||
|
const array = new Uint8Array(32);
|
||||||
|
crypto.getRandomValues(array);
|
||||||
|
return Array.from(array).map((b) => b.toString(16).padStart(2, "0")).join("");
|
||||||
|
};
|
||||||
|
const initPluginManager = (runtime, plugins, config) => {
|
||||||
|
const registry = new PluginRegistry();
|
||||||
|
for (const plugin of plugins) {
|
||||||
|
registry.register(plugin);
|
||||||
|
}
|
||||||
|
const manager = createPluginManager(registry);
|
||||||
|
manager.setActivePlugin(
|
||||||
|
config.activeAdapter,
|
||||||
|
config.adapters[config.activeAdapter]
|
||||||
|
);
|
||||||
|
return manager;
|
||||||
|
};
|
||||||
|
const initConfigManager = async (runtime) => {
|
||||||
|
let storage;
|
||||||
|
{
|
||||||
|
const { createNodeStorage } = await import("./NodeStorage-DrLmsh6l.js");
|
||||||
|
storage = createNodeStorage(filesystemPlugin);
|
||||||
|
}
|
||||||
|
return createConfigManager(storage);
|
||||||
|
};
|
||||||
|
const initializeCore = async (runtime, { plugins }) => {
|
||||||
|
const configManager = await initConfigManager();
|
||||||
|
const config = await configManager.loadConfig();
|
||||||
|
const pluginManager = initPluginManager(runtime, plugins, config);
|
||||||
|
let notesAPI = null;
|
||||||
|
let initPromise = null;
|
||||||
|
const getNotesAPI = async () => {
|
||||||
|
if (notesAPI) return notesAPI;
|
||||||
|
if (!initPromise) {
|
||||||
|
initPromise = (async () => {
|
||||||
|
const config2 = await configManager.loadConfig();
|
||||||
|
let encryptionKey = config2?.encryptionKey;
|
||||||
|
if (!encryptionKey) {
|
||||||
|
encryptionKey = generateEncryptionKey();
|
||||||
|
await configManager.setConfig({
|
||||||
|
...config2,
|
||||||
|
encryptionKey
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const pluginId = config2?.activeAdapter || "filesystem";
|
||||||
|
const adapterConfig = config2?.adapters?.[pluginId] || {};
|
||||||
|
const adapter = pluginManager.getAdapter(
|
||||||
|
pluginId,
|
||||||
|
adapterConfig
|
||||||
|
);
|
||||||
|
notesAPI = new NotesAPI(adapter, encryptionKey);
|
||||||
|
await notesAPI.init();
|
||||||
|
return notesAPI;
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
return initPromise;
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
runtime,
|
||||||
|
pluginManager,
|
||||||
|
configManager,
|
||||||
|
getNotesAPI
|
||||||
|
};
|
||||||
|
};
|
||||||
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,
|
||||||
@@ -234,44 +435,28 @@ app.whenReady().then(async () => {
|
|||||||
win.webContents.send(event, data);
|
win.webContents.send(event, data);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const registry = new PluginRegistry("electron");
|
const { pluginManager, configManager } = await initializeCore(
|
||||||
registry.register(filesystemPlugin);
|
"electron-main",
|
||||||
registry.register(supabasePlugin);
|
{
|
||||||
const nodeStorage = createNodeStorage(filesystemPlugin);
|
plugins: [filesystemPlugin, supabasePlugin]
|
||||||
const configManager = createConfigManager("electron", nodeStorage);
|
}
|
||||||
const initialConfig = await configManager.loadConfig();
|
);
|
||||||
const setActivePlugin = async (pluginId) => {
|
ipcMain.handle("pluginManager:call", async (_, method, ...args) => {
|
||||||
const currentConfig = await configManager.loadConfig();
|
const methodCall = await pluginManager[method](...args);
|
||||||
await configManager.setConfig({
|
if (method === "setActivePlugin") {
|
||||||
...currentConfig,
|
broadcastNoteChange("plugin-changed");
|
||||||
activeAdapter: pluginId
|
}
|
||||||
|
return methodCall;
|
||||||
});
|
});
|
||||||
const plugin = registry.get(pluginId);
|
ipcMain.handle("configManager:call", async (_, method, ...args) => {
|
||||||
const adapterConfig = currentConfig.adapters[pluginId] || {};
|
return await configManager[method](...args);
|
||||||
activeAdapter = plugin.createAdapter(adapterConfig);
|
});
|
||||||
await activeAdapter.init();
|
ipcMain.handle("adapter:call", async (_, method, ...args) => {
|
||||||
ipcMain.removeHandler("adapter:call");
|
const adapter = pluginManager.getActiveAdapter();
|
||||||
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 activeAdapter[method](...args);
|
return await adapter[method](...args);
|
||||||
});
|
|
||||||
broadcastNoteChange("plugin-changed", pluginId);
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
await setActivePlugin(initialConfig.activeAdapter);
|
|
||||||
ipcMain.handle("getConfig", async () => {
|
|
||||||
return await configManager.loadConfig();
|
|
||||||
});
|
|
||||||
ipcMain.handle("setConfig", async (_, newConfig) => {
|
|
||||||
await configManager.setConfig(newConfig);
|
|
||||||
});
|
|
||||||
ipcMain.handle("listPlugins", async () => {
|
|
||||||
return registry.list();
|
|
||||||
});
|
|
||||||
ipcMain.handle("setActivePlugin", async (_, pluginId) => {
|
|
||||||
return await setActivePlugin(pluginId);
|
|
||||||
});
|
});
|
||||||
ipcMain.on("note-changed", (_, event, data) => {
|
ipcMain.on("note-changed", (_, event, data) => {
|
||||||
broadcastNoteChange(event, data);
|
broadcastNoteChange(event, data);
|
||||||
@@ -297,8 +482,8 @@ app.whenReady().then(async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
electronApp.setAppUserModelId("com.electron");
|
electronApp.setAppUserModelId("com.electron");
|
||||||
app.on("browser-window-created", (_, window2) => {
|
app.on("browser-window-created", (_, window) => {
|
||||||
optimizer.watchWindowShortcuts(window2);
|
optimizer.watchWindowShortcuts(window);
|
||||||
});
|
});
|
||||||
createWindow();
|
createWindow();
|
||||||
app.on("activate", function() {
|
app.on("activate", function() {
|
||||||
|
|||||||
@@ -1,9 +1,20 @@
|
|||||||
import { contextBridge, ipcRenderer } from "electron";
|
import { contextBridge, ipcRenderer } from "electron";
|
||||||
const api = {
|
const api = {
|
||||||
getConfig: () => ipcRenderer.invoke("getConfig"),
|
pluginManagerCall: (method, ...args) => ipcRenderer.invoke(
|
||||||
setConfig: (config) => ipcRenderer.invoke("setConfig", config),
|
"pluginManager:call",
|
||||||
listPlugins: () => ipcRenderer.invoke("listPlugins"),
|
method,
|
||||||
setActivePlugin: (pluginId) => ipcRenderer.invoke("setActivePlugin", pluginId),
|
...args.length ? args : []
|
||||||
|
),
|
||||||
|
configManagerCall: (method, ...args) => ipcRenderer.invoke(
|
||||||
|
"configManager:call",
|
||||||
|
method,
|
||||||
|
...args.length ? args : []
|
||||||
|
),
|
||||||
|
adapterCall: (method, ...args) => ipcRenderer.invoke(
|
||||||
|
"adapter:call",
|
||||||
|
method,
|
||||||
|
...args.length ? args : []
|
||||||
|
),
|
||||||
openNoteWindow: (noteId) => {
|
openNoteWindow: (noteId) => {
|
||||||
ipcRenderer.send("open-note-window", noteId);
|
ipcRenderer.send("open-note-window", noteId);
|
||||||
},
|
},
|
||||||
@@ -29,17 +40,12 @@ const api = {
|
|||||||
ipcRenderer.invoke("move-closed");
|
ipcRenderer.invoke("move-closed");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const adapter = {
|
|
||||||
call: (method, ...args) => ipcRenderer.invoke("adapter:call", method, args)
|
|
||||||
};
|
|
||||||
if (process.contextIsolated) {
|
if (process.contextIsolated) {
|
||||||
try {
|
try {
|
||||||
contextBridge.exposeInMainWorld("api", api);
|
contextBridge.exposeInMainWorld("api", api);
|
||||||
contextBridge.exposeInMainWorld("adapter", adapter);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
window.api = api;
|
window.api = api;
|
||||||
window.adapter = adapter;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
const __viteBrowserExternal = {};
|
|
||||||
export {
|
|
||||||
__viteBrowserExternal as default
|
|
||||||
};
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,18 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<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-Cj2aGG-N.js"></script>
|
|
||||||
<link rel="stylesheet" crossorigin href="./assets/index-s71dsOUL.css">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div id="app"></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
55
src/core/ConfigManager.js
Normal file
55
src/core/ConfigManager.js
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
const getDefaultConfig = () => {
|
||||||
|
return {
|
||||||
|
activeAdapter: 'supabase',
|
||||||
|
theme: 'dark',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createConfigManager = (storage) => {
|
||||||
|
let config = null
|
||||||
|
|
||||||
|
return {
|
||||||
|
async loadConfig() {
|
||||||
|
if (config) return config
|
||||||
|
|
||||||
|
const stored = await storage.load()
|
||||||
|
config = stored || getDefaultConfig()
|
||||||
|
|
||||||
|
if (!stored) {
|
||||||
|
await storage.save(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
},
|
||||||
|
|
||||||
|
getConfig() {
|
||||||
|
return config
|
||||||
|
},
|
||||||
|
|
||||||
|
async setConfig(newConfig) {
|
||||||
|
config = newConfig
|
||||||
|
await storage.save(newConfig)
|
||||||
|
},
|
||||||
|
|
||||||
|
async refreshConfig() {
|
||||||
|
config = await storage.load()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createConfigManagerClient = () => {
|
||||||
|
return {
|
||||||
|
async loadConfig() {
|
||||||
|
return await window.api.configManagerCall('loadConfig')
|
||||||
|
},
|
||||||
|
async getConfig() {
|
||||||
|
return await window.api.configManagerCall('getConfig')
|
||||||
|
},
|
||||||
|
async setConfig(newConfig) {
|
||||||
|
return await window.api.configManagerCall('setConfig', newConfig)
|
||||||
|
},
|
||||||
|
async refreshConfig() {
|
||||||
|
return await window.api.configManagerCall('refreshConfig')
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,22 +4,22 @@ export default class IpcAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
return await window.adapter.call('init')
|
return await window.api.adapterCall('init')
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAll() {
|
async getAll() {
|
||||||
return await window.adapter.call('getAll')
|
return await window.api.adapterCall('getAll')
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(note) {
|
async create(note) {
|
||||||
return await window.adapter.call('create', note)
|
return await window.api.adapterCall('create', note)
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(note) {
|
async update(note) {
|
||||||
return await window.adapter.call('update', note)
|
return await window.api.adapterCall('update', note)
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id) {
|
async delete(id) {
|
||||||
return await window.adapter.call('delete', id)
|
return await window.api.adapterCall('delete', id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,9 @@
|
|||||||
import PluginRegistry from './PluginRegistry.js'
|
import IpcAdapter from './IpcAdapter'
|
||||||
import IpcAdapter from './IpcAdapter.js'
|
|
||||||
|
|
||||||
let registry = null
|
export const createPluginManager = (registry) => {
|
||||||
let activePluginId = null
|
let activePluginId = null
|
||||||
let adapter = null
|
let adapter = null
|
||||||
|
|
||||||
export default function createPluginManager(environment, plugins = []) {
|
|
||||||
registry = new PluginRegistry(environment)
|
|
||||||
|
|
||||||
for (const plugin of plugins) {
|
|
||||||
registry.register(plugin)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
listPlugins() {
|
listPlugins() {
|
||||||
return registry.list()
|
return registry.list()
|
||||||
@@ -24,10 +16,6 @@ export default function createPluginManager(environment, plugins = []) {
|
|||||||
getAdapter(pluginId, adapterConfig = {}) {
|
getAdapter(pluginId, adapterConfig = {}) {
|
||||||
const plugin = registry.get(pluginId)
|
const plugin = registry.get(pluginId)
|
||||||
|
|
||||||
if (environment === 'electron') {
|
|
||||||
return new IpcAdapter()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!plugin) {
|
if (!plugin) {
|
||||||
throw new Error(`Plugin not found: ${pluginId}`)
|
throw new Error(`Plugin not found: ${pluginId}`)
|
||||||
}
|
}
|
||||||
@@ -41,6 +29,19 @@ export default function createPluginManager(environment, plugins = []) {
|
|||||||
return adapter
|
return adapter
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async testPlugin(pluginId, adapterConfig = {}) {
|
||||||
|
const plugin = registry.get(pluginId)
|
||||||
|
|
||||||
|
if (!plugin) {
|
||||||
|
throw new Error(`Plugin not found: ${pluginId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const adapter = this.getAdapter(pluginId, adapterConfig)
|
||||||
|
await adapter.init()
|
||||||
|
|
||||||
|
return await adapter.testConnection()
|
||||||
|
},
|
||||||
|
|
||||||
getActiveAdapter() {
|
getActiveAdapter() {
|
||||||
return adapter
|
return adapter
|
||||||
},
|
},
|
||||||
@@ -48,9 +49,30 @@ export default function createPluginManager(environment, plugins = []) {
|
|||||||
getActivePluginId() {
|
getActivePluginId() {
|
||||||
return activePluginId
|
return activePluginId
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getEnvironment() {
|
// Client for calling manager through IPC
|
||||||
return environment
|
export const createPluginManagerClient = () => {
|
||||||
|
return {
|
||||||
|
listPlugins() {
|
||||||
|
return window.api.pluginManagerCall('listPlugins')
|
||||||
|
},
|
||||||
|
|
||||||
|
getAdapter(pluginId, config) {
|
||||||
|
return new IpcAdapter(pluginId, config)
|
||||||
|
},
|
||||||
|
|
||||||
|
setActivePlugin(pluginId, adapterConfig) {
|
||||||
|
return window.api.pluginManagerCall(
|
||||||
|
'setActivePlugin',
|
||||||
|
pluginId,
|
||||||
|
adapterConfig,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
|
async testPlugin(id, config) {
|
||||||
|
return window.api.pluginManagerCall('testPlugin', { id, config })
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
export default class PluginRegistry {
|
export default class PluginRegistry {
|
||||||
constructor(environment = 'web') {
|
constructor() {
|
||||||
this.plugins = new Map()
|
this.plugins = new Map()
|
||||||
this.environment = environment
|
|
||||||
}
|
}
|
||||||
|
|
||||||
register(plugin) {
|
register(plugin) {
|
||||||
@@ -9,11 +8,6 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
const USER_DATA_STRING = '__DEFAULT_USER_DATA__'
|
||||||
const DB_NAME = 'takerofnotes'
|
const DB_NAME = 'takerofnotes'
|
||||||
const DB_VERSION = 1
|
const DB_VERSION = 1
|
||||||
const STORE_NAME = 'config'
|
const STORE_NAME = 'config'
|
||||||
@@ -53,7 +54,7 @@ async function saveToDB(data) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function createIndexedDBStorage() {
|
export function createWebStorage() {
|
||||||
return {
|
return {
|
||||||
async load() {
|
async load() {
|
||||||
const stored = await getFromDB()
|
const stored = await getFromDB()
|
||||||
@@ -65,84 +66,4 @@ function createIndexedDBStorage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createIpcStorage() {
|
export default createWebStorage
|
||||||
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,5 +1,14 @@
|
|||||||
import createPluginManager from './PluginManager.js'
|
import filesystemPlugin from '@takerofnotes/plugin-filesystem'
|
||||||
import createConfigManager from './Config.js'
|
import {
|
||||||
|
createPluginManager,
|
||||||
|
createPluginManagerClient,
|
||||||
|
} from './PluginManager.js'
|
||||||
|
import { createWebStorage } from './WebStorage.js'
|
||||||
|
import PluginRegistry from './PluginRegistry.js'
|
||||||
|
import {
|
||||||
|
createConfigManager,
|
||||||
|
createConfigManagerClient,
|
||||||
|
} from './ConfigManager.js'
|
||||||
import NotesAPI from './NotesAPI.js'
|
import NotesAPI from './NotesAPI.js'
|
||||||
|
|
||||||
const generateEncryptionKey = () => {
|
const generateEncryptionKey = () => {
|
||||||
@@ -10,12 +19,46 @@ const generateEncryptionKey = () => {
|
|||||||
.join('')
|
.join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
let coreInstance = null
|
const initPluginManager = (runtime, plugins, config) => {
|
||||||
|
if (runtime === 'electron-renderer') return createPluginManagerClient()
|
||||||
|
|
||||||
export function initializeCore(environment, plugins = []) {
|
const registry = new PluginRegistry()
|
||||||
const pluginManager = createPluginManager(environment, plugins)
|
|
||||||
const configManager = createConfigManager(environment)
|
|
||||||
|
|
||||||
|
for (const plugin of plugins) {
|
||||||
|
registry.register(plugin)
|
||||||
|
}
|
||||||
|
|
||||||
|
const manager = createPluginManager(registry)
|
||||||
|
manager.setActivePlugin(
|
||||||
|
config.activeAdapter,
|
||||||
|
config.adapters[config.activeAdapter],
|
||||||
|
)
|
||||||
|
|
||||||
|
return manager
|
||||||
|
}
|
||||||
|
|
||||||
|
const initConfigManager = async (runtime) => {
|
||||||
|
if (runtime === 'electron-renderer') return createConfigManagerClient()
|
||||||
|
|
||||||
|
let storage
|
||||||
|
if (runtime === 'electron-main') {
|
||||||
|
const { createNodeStorage } = await import('./NodeStorage.js')
|
||||||
|
storage = createNodeStorage(filesystemPlugin)
|
||||||
|
} else if (runtime === 'web') {
|
||||||
|
storage = createWebStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
return createConfigManager(storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const initializeCore = async (runtime, { plugins }) => {
|
||||||
|
const configManager = await initConfigManager(runtime)
|
||||||
|
const config = await configManager.loadConfig()
|
||||||
|
const pluginManager = initPluginManager(runtime, plugins, config)
|
||||||
|
|
||||||
|
// -------------------------
|
||||||
|
// NotesAPI bootstrap
|
||||||
|
// -------------------------
|
||||||
let notesAPI = null
|
let notesAPI = null
|
||||||
let initPromise = null
|
let initPromise = null
|
||||||
|
|
||||||
@@ -30,12 +73,15 @@ export function initializeCore(environment, plugins = []) {
|
|||||||
|
|
||||||
if (!encryptionKey) {
|
if (!encryptionKey) {
|
||||||
encryptionKey = generateEncryptionKey()
|
encryptionKey = generateEncryptionKey()
|
||||||
await configManager.setConfig({ ...config, encryptionKey })
|
await configManager.setConfig({
|
||||||
|
...config,
|
||||||
|
encryptionKey,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const pluginId = config?.activeAdapter || 'filesystem'
|
const pluginId = config?.activeAdapter || 'filesystem'
|
||||||
const adapterConfig =
|
const adapterConfig = config?.adapters?.[pluginId] || {}
|
||||||
config.adapters?.[config.activeAdapter] || {}
|
|
||||||
const adapter = pluginManager.getAdapter(
|
const adapter = pluginManager.getAdapter(
|
||||||
pluginId,
|
pluginId,
|
||||||
adapterConfig,
|
adapterConfig,
|
||||||
@@ -51,27 +97,10 @@ export function initializeCore(environment, plugins = []) {
|
|||||||
return initPromise
|
return initPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
coreInstance = {
|
return {
|
||||||
environment,
|
runtime,
|
||||||
pluginManager,
|
pluginManager,
|
||||||
configManager,
|
configManager,
|
||||||
getNotesAPI,
|
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'
|
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
export enum ENVIRONMENTS {
|
|
||||||
ELECTRON = 'electron',
|
|
||||||
WEB = 'web',
|
|
||||||
}
|
|
||||||
@@ -3,8 +3,7 @@ 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, createConfigManager } from '../core/index.js'
|
import { initializeCore } from '../core/index.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,8 +12,6 @@ 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,
|
||||||
@@ -78,55 +75,32 @@ app.whenReady().then(async () => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const registry = new PluginRegistry('electron')
|
const { pluginManager, configManager } = await initializeCore(
|
||||||
registry.register(filesystemPlugin)
|
'electron-main',
|
||||||
registry.register(supabasePlugin)
|
{
|
||||||
|
plugins: [filesystemPlugin, supabasePlugin],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
const nodeStorage = createNodeStorage(filesystemPlugin)
|
ipcMain.handle('pluginManager:call', async (_, method, ...args) => {
|
||||||
const configManager = createConfigManager('electron', nodeStorage)
|
const methodCall = await pluginManager[method](...args)
|
||||||
const initialConfig = await configManager.loadConfig()
|
|
||||||
|
|
||||||
const setActivePlugin = async (pluginId) => {
|
if (method === 'setActivePlugin') {
|
||||||
const currentConfig = await configManager.loadConfig()
|
broadcastNoteChange('plugin-changed')
|
||||||
await configManager.setConfig({
|
}
|
||||||
...currentConfig,
|
|
||||||
activeAdapter: pluginId,
|
return methodCall
|
||||||
})
|
})
|
||||||
|
ipcMain.handle('configManager:call', async (_, method, ...args) => {
|
||||||
const plugin = registry.get(pluginId)
|
return await configManager[method](...args)
|
||||||
const adapterConfig = currentConfig.adapters[pluginId] || {}
|
})
|
||||||
activeAdapter = plugin.createAdapter(adapterConfig)
|
ipcMain.handle('adapter:call', async (_, method, ...args) => {
|
||||||
|
const adapter = pluginManager.getActiveAdapter()
|
||||||
await activeAdapter.init()
|
if (!adapter[method]) {
|
||||||
|
|
||||||
ipcMain.removeHandler('adapter:call')
|
|
||||||
ipcMain.handle('adapter:call', async (_, method, args) => {
|
|
||||||
if (!activeAdapter[method]) {
|
|
||||||
throw new Error(`Invalid adapter method: ${method}`)
|
throw new Error(`Invalid adapter method: ${method}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return await activeAdapter[method](...args)
|
return await adapter[method](...args)
|
||||||
})
|
|
||||||
|
|
||||||
broadcastNoteChange('plugin-changed', pluginId)
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
await setActivePlugin(initialConfig.activeAdapter)
|
|
||||||
|
|
||||||
ipcMain.handle('getConfig', async () => {
|
|
||||||
return await configManager.loadConfig()
|
|
||||||
})
|
|
||||||
ipcMain.handle('setConfig', async (_, newConfig) => {
|
|
||||||
await configManager.setConfig(newConfig)
|
|
||||||
})
|
|
||||||
|
|
||||||
ipcMain.handle('listPlugins', async () => {
|
|
||||||
return registry.list()
|
|
||||||
})
|
|
||||||
ipcMain.handle('setActivePlugin', async (_, pluginId) => {
|
|
||||||
return await setActivePlugin(pluginId)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('note-changed', (_, event, data) => {
|
ipcMain.on('note-changed', (_, event, data) => {
|
||||||
|
|||||||
@@ -2,11 +2,24 @@ import { contextBridge, ipcRenderer } from 'electron'
|
|||||||
|
|
||||||
// Custom APIs for renderer
|
// Custom APIs for renderer
|
||||||
const api = {
|
const api = {
|
||||||
getConfig: () => ipcRenderer.invoke('getConfig'),
|
pluginManagerCall: (method, ...args) =>
|
||||||
setConfig: (config) => ipcRenderer.invoke('setConfig', config),
|
ipcRenderer.invoke(
|
||||||
listPlugins: () => ipcRenderer.invoke('listPlugins'),
|
'pluginManager:call',
|
||||||
setActivePlugin: (pluginId) =>
|
method,
|
||||||
ipcRenderer.invoke('setActivePlugin', pluginId),
|
...(args.length ? args : []),
|
||||||
|
),
|
||||||
|
configManagerCall: (method, ...args) =>
|
||||||
|
ipcRenderer.invoke(
|
||||||
|
'configManager:call',
|
||||||
|
method,
|
||||||
|
...(args.length ? args : []),
|
||||||
|
),
|
||||||
|
adapterCall: (method, ...args) =>
|
||||||
|
ipcRenderer.invoke(
|
||||||
|
'adapter:call',
|
||||||
|
method,
|
||||||
|
...(args.length ? args : []),
|
||||||
|
),
|
||||||
openNoteWindow: (noteId) => {
|
openNoteWindow: (noteId) => {
|
||||||
ipcRenderer.send('open-note-window', noteId)
|
ipcRenderer.send('open-note-window', noteId)
|
||||||
},
|
},
|
||||||
@@ -33,19 +46,12 @@ const api = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement adapter API - communicates with plugin adapter in main process
|
|
||||||
const adapter = {
|
|
||||||
call: (method, ...args) => ipcRenderer.invoke('adapter:call', method, args),
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.contextIsolated) {
|
if (process.contextIsolated) {
|
||||||
try {
|
try {
|
||||||
contextBridge.exposeInMainWorld('api', api)
|
contextBridge.exposeInMainWorld('api', api)
|
||||||
contextBridge.exposeInMainWorld('adapter', adapter)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
window.api = api
|
window.api = api
|
||||||
window.adapter = adapter
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<title>Electron</title>
|
<title>Taker of Notes</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:"
|
||||||
/>
|
/>-->
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ const onMoveOpened = async () => {
|
|||||||
move: props.note.id,
|
move: props.note.id,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
console.log(route.query)
|
|
||||||
}
|
}
|
||||||
const moveActive = computed(() => route.query.move === props.note.id)
|
const moveActive = computed(() => route.query.move === props.note.id)
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ref, watch, toRaw, onMounted } from 'vue'
|
import { ref, watch, toRaw, onMounted } from 'vue'
|
||||||
import { getCore } from '@core/index.js'
|
import useCore from '@/composables/useCore'
|
||||||
|
|
||||||
const config = ref()
|
const config = ref()
|
||||||
let configResolve = null
|
let configResolve = null
|
||||||
@@ -8,8 +8,7 @@ const configPromise = new Promise((resolve) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const core = getCore()
|
const { configManager } = useCore()
|
||||||
const { configManager } = core
|
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
if (config.value) {
|
if (config.value) {
|
||||||
@@ -17,8 +16,7 @@ export default () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadedConfig = await configManager.loadConfig()
|
config.value = await configManager.loadConfig()
|
||||||
config.value = loadedConfig
|
|
||||||
configResolve()
|
configResolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
11
src/renderer/src/composables/useCore.js
Normal file
11
src/renderer/src/composables/useCore.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { inject } from 'vue'
|
||||||
|
|
||||||
|
let core
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
if (!core) {
|
||||||
|
core = inject('core')
|
||||||
|
}
|
||||||
|
|
||||||
|
return core
|
||||||
|
}
|
||||||
@@ -41,8 +41,10 @@ const setupListeners = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const broadcastChange = (event, data) => {
|
const broadcastChange = (event, data) => {
|
||||||
|
if (environment === 'electron') {
|
||||||
window.api.notifyNoteChanged(event, data)
|
window.api.notifyNoteChanged(event, data)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (environment === 'electron') {
|
if (environment === 'electron') {
|
||||||
setupListeners()
|
setupListeners()
|
||||||
|
|||||||
@@ -1,20 +1,27 @@
|
|||||||
import { ref } from 'vue'
|
import useCore from '@/composables/useCore'
|
||||||
import useConfig from './useConfig'
|
import useConfig from './useConfig'
|
||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
export default async () => {
|
export default async () => {
|
||||||
const { refreshConfig } = useConfig()
|
const { config } = useConfig()
|
||||||
|
const { pluginManager } = useCore()
|
||||||
|
|
||||||
const plugins = ref([])
|
const plugins = ref([])
|
||||||
|
|
||||||
plugins.value = await window.api.listPlugins()
|
plugins.value = await pluginManager.listPlugins()
|
||||||
|
|
||||||
const setActivePlugin = async (pluginId) => {
|
const setActivePlugin = async (pluginId, activeConfig = {}) => {
|
||||||
await window.api.setActivePlugin(pluginId)
|
await pluginManager.setActivePlugin(pluginId, { ...activeConfig })
|
||||||
await refreshConfig()
|
config.value.activeAdapter = pluginId
|
||||||
|
}
|
||||||
|
|
||||||
|
const testPlugin = async (pluginId, config = {}) => {
|
||||||
|
await pluginManager.testPlugin(pluginId, { ...config })
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
plugins,
|
plugins,
|
||||||
setActivePlugin,
|
setActivePlugin,
|
||||||
|
testPlugin,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getCore } from '@core/index.js'
|
import useCore from '@/composables/useCore'
|
||||||
|
|
||||||
let notesAPI = null
|
let notesAPI = null
|
||||||
let initPromise = null
|
let initPromise = null
|
||||||
@@ -8,8 +8,8 @@ export const getNotesAPI = async () => {
|
|||||||
|
|
||||||
if (!initPromise) {
|
if (!initPromise) {
|
||||||
initPromise = (async () => {
|
initPromise = (async () => {
|
||||||
const core = getCore()
|
const { getNotesAPI } = useCore()
|
||||||
notesAPI = await core.getNotesAPI()
|
notesAPI = await getNotesAPI()
|
||||||
return notesAPI
|
return notesAPI
|
||||||
})()
|
})()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { router } from './plugins/router'
|
|||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
|
|
||||||
app.use(initCore)
|
await initCore(app)
|
||||||
app.use(router)
|
app.use(router)
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|||||||
@@ -2,11 +2,18 @@ import { useEnvironment } from '@/composables/useEnvironment'
|
|||||||
import { initializeCore } from '@core/index.js'
|
import { initializeCore } from '@core/index.js'
|
||||||
import supabasePlugin from '@takerofnotes/plugin-supabase'
|
import supabasePlugin from '@takerofnotes/plugin-supabase'
|
||||||
|
|
||||||
export const initCore = () => {
|
export const initCore = async (app) => {
|
||||||
const environment = useEnvironment()
|
const environment = useEnvironment()
|
||||||
|
|
||||||
|
// Set runtime
|
||||||
|
const runtime = environment === 'electron' ? 'electron-renderer' : 'web'
|
||||||
|
|
||||||
// Plugins that are valid for web (electron uses IPC)
|
// Plugins that are valid for web (electron uses IPC)
|
||||||
const plugins = [supabasePlugin]
|
const plugins = [supabasePlugin]
|
||||||
|
|
||||||
initializeCore(environment, plugins)
|
const core = await initializeCore(runtime, {
|
||||||
|
plugins,
|
||||||
|
})
|
||||||
|
|
||||||
|
app.provide('core', core)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,6 @@
|
|||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import useNotes from '@/composables/useNotes'
|
import useNotes from '@/composables/useNotes'
|
||||||
import usePlugins from '@/composables/usePlugins'
|
|
||||||
import useConfig from '@/composables/useConfig'
|
|
||||||
import { onMounted, ref, watch } from 'vue'
|
import { onMounted, ref, watch } from 'vue'
|
||||||
import CategoryRow from '@/components/CategoryRow.vue'
|
import CategoryRow from '@/components/CategoryRow.vue'
|
||||||
import NoteRow from '@/components/NoteRow.vue'
|
import NoteRow from '@/components/NoteRow.vue'
|
||||||
@@ -29,8 +27,6 @@ import PageLoading from '@/components/PageLoading.vue'
|
|||||||
const { categories, loadCategories, loadCategoryNotes, notesChangeCount } =
|
const { categories, loadCategories, loadCategoryNotes, notesChangeCount } =
|
||||||
useNotes()
|
useNotes()
|
||||||
|
|
||||||
const { config } = useConfig()
|
|
||||||
|
|
||||||
const notes = ref()
|
const notes = ref()
|
||||||
const loaded = ref(false)
|
const loaded = ref(false)
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ import usePlugins from '@/composables/usePlugins'
|
|||||||
import useConfig from '@/composables/useConfig'
|
import useConfig from '@/composables/useConfig'
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
|
|
||||||
const { plugins, setActivePlugin } = await usePlugins()
|
const { plugins, setActivePlugin, testPlugin } = await usePlugins()
|
||||||
const { config, ensureConfig } = useConfig()
|
const { config, ensureConfig } = useConfig()
|
||||||
await ensureConfig()
|
await ensureConfig()
|
||||||
|
|
||||||
@@ -89,17 +89,26 @@ const save = async () => {
|
|||||||
validationError.value = ''
|
validationError.value = ''
|
||||||
|
|
||||||
const plugin = selectedPlugin.value
|
const plugin = selectedPlugin.value
|
||||||
if (plugin && plugin.configSchema.length) {
|
|
||||||
const adapterConfig = config.value.adapters[plugin.id] || {}
|
const adapterConfig = config.value.adapters[plugin.id] || {}
|
||||||
|
|
||||||
|
if (plugin && plugin.configSchema.length) {
|
||||||
|
// Check required fields
|
||||||
for (const field of plugin.configSchema) {
|
for (const field of plugin.configSchema) {
|
||||||
if (field.required && !adapterConfig[field.key]) {
|
if (field.required && !adapterConfig[field.key]) {
|
||||||
validationError.value = `Please fill in all required fields for ${plugin.name}`
|
validationError.value = `Please fill in all required fields for ${plugin.name}`
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test connection
|
||||||
|
// const testResult = await testPlugin(plugin.id, adapterConfig)
|
||||||
|
// console.log(testResult)
|
||||||
|
// if (!testResult) {
|
||||||
|
// validationError.value = `Failed to connect to ${plugin.name}`
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
await setActivePlugin(selectedPluginId.value)
|
await setActivePlugin(selectedPluginId.value, adapterConfig)
|
||||||
saving.value = false
|
saving.value = false
|
||||||
saved.value = true
|
saved.value = true
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user