plugin system
This commit is contained in:
25
src/main/core/BaseNotesAdapter.js
Normal file
25
src/main/core/BaseNotesAdapter.js
Normal file
@@ -0,0 +1,25 @@
|
||||
export default class BaseNotesAdapter {
|
||||
constructor(config = {}) {
|
||||
this.config = config
|
||||
}
|
||||
|
||||
async init() {
|
||||
throw new Error('init() not implemented')
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
throw new Error('getAll() not implemented')
|
||||
}
|
||||
|
||||
async create(note) {
|
||||
throw new Error('create() not implemented')
|
||||
}
|
||||
|
||||
async update(note) {
|
||||
throw new Error('update() not implemented')
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
throw new Error('delete() not implemented')
|
||||
}
|
||||
}
|
||||
129
src/main/core/NotesAPI.js
Normal file
129
src/main/core/NotesAPI.js
Normal file
@@ -0,0 +1,129 @@
|
||||
import { Index } from 'flexsearch'
|
||||
import crypto from 'crypto'
|
||||
|
||||
export default class NotesAPI {
|
||||
constructor(adapter) {
|
||||
if (!adapter) {
|
||||
throw new Error('NotesAPI requires a storage adapter')
|
||||
}
|
||||
|
||||
this.adapter = adapter
|
||||
this.notesCache = new Map()
|
||||
|
||||
this.index = new Index({
|
||||
tokenize: 'tolerant',
|
||||
resolution: 9,
|
||||
})
|
||||
}
|
||||
|
||||
async init() {
|
||||
await this.adapter.init()
|
||||
|
||||
const notes = await this.adapter.getAll()
|
||||
|
||||
for (const note of notes) {
|
||||
this.notesCache.set(note.id, note)
|
||||
this.index.add(note.id, note.title + '\n' + note.content)
|
||||
}
|
||||
}
|
||||
|
||||
/* -----------------------
|
||||
Public API
|
||||
------------------------*/
|
||||
getCategories() {
|
||||
const categories = new Set()
|
||||
|
||||
for (const note of this.notesCache.values()) {
|
||||
if (note.category) {
|
||||
categories.add(note.category)
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(categories).sort()
|
||||
}
|
||||
|
||||
getCategoryNotes(categoryName) {
|
||||
return Array.from(this.notesCache.values())
|
||||
.filter((n) => n.category === categoryName)
|
||||
.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt))
|
||||
}
|
||||
|
||||
getNote(id) {
|
||||
return this.notesCache.get(id) ?? null
|
||||
}
|
||||
|
||||
async createNote(metadata = {}, content = '') {
|
||||
const id = crypto.randomUUID()
|
||||
const now = new Date().toISOString()
|
||||
|
||||
const note = {
|
||||
id,
|
||||
title: metadata.title || 'Untitled',
|
||||
category: metadata.category || null,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
content,
|
||||
}
|
||||
|
||||
this.notesCache.set(id, note)
|
||||
this.index.add(id, note.title + '\n' + content)
|
||||
|
||||
await this.adapter.create(note)
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
async deleteNote(id) {
|
||||
await this.adapter.delete(id)
|
||||
|
||||
this.notesCache.delete(id)
|
||||
this.index.remove(id)
|
||||
}
|
||||
|
||||
async updateNote(id, content) {
|
||||
const note = this.notesCache.get(id)
|
||||
if (!note) throw new Error('Note not found')
|
||||
|
||||
note.content = content
|
||||
note.updatedAt = new Date().toISOString()
|
||||
|
||||
this.index.update(id, note.title + '\n' + content)
|
||||
|
||||
await this.adapter.update(note)
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
async updateNoteMetadata(id, updates = {}) {
|
||||
const note = this.notesCache.get(id)
|
||||
if (!note) throw new Error('Note not found')
|
||||
|
||||
const allowedFields = ['title', 'category']
|
||||
for (const key of Object.keys(updates)) {
|
||||
if (!allowedFields.includes(key)) {
|
||||
throw new Error(`Invalid metadata field: ${key}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (updates.title !== undefined) {
|
||||
note.title = updates.title
|
||||
}
|
||||
|
||||
if (updates.category !== undefined) {
|
||||
note.category = updates.category
|
||||
}
|
||||
|
||||
note.updatedAt = new Date().toISOString()
|
||||
|
||||
this.index.update(id, note.title + '\n' + note.content)
|
||||
|
||||
await this.adapter.update(note)
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
search(query) {
|
||||
const ids = this.index.search(query)
|
||||
return ids.map((id) => this.notesCache.get(id))
|
||||
}
|
||||
}
|
||||
52
src/main/core/PluginConfig.js
Normal file
52
src/main/core/PluginConfig.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { app } from 'electron'
|
||||
|
||||
export default class PluginConfig {
|
||||
constructor(defaultPlugin) {
|
||||
this.defaultPlugin = defaultPlugin
|
||||
|
||||
this.configPath = path.join(app.getPath('userData'), 'config.json')
|
||||
}
|
||||
|
||||
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 = {
|
||||
activeAdapter: this.defaultPlugin.id,
|
||||
adapterConfig: defaultConfig,
|
||||
}
|
||||
|
||||
await this.write(parsed)
|
||||
}
|
||||
|
||||
return parsed
|
||||
}
|
||||
|
||||
async write(configObject) {
|
||||
const dir = path.dirname(this.configPath)
|
||||
|
||||
// Ensure directory exists
|
||||
await fs.mkdir(dir, { recursive: true })
|
||||
|
||||
await fs.writeFile(
|
||||
this.configPath,
|
||||
JSON.stringify(configObject, null, 2),
|
||||
'utf8',
|
||||
)
|
||||
}
|
||||
}
|
||||
21
src/main/core/PluginRegistry.js
Normal file
21
src/main/core/PluginRegistry.js
Normal file
@@ -0,0 +1,21 @@
|
||||
export default class PluginRegistry {
|
||||
constructor() {
|
||||
this.plugins = new Map()
|
||||
}
|
||||
|
||||
register(plugin) {
|
||||
if (!plugin.id) {
|
||||
throw new Error('Plugin must have an id')
|
||||
}
|
||||
|
||||
this.plugins.set(plugin.id, plugin)
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.plugins.get(id)
|
||||
}
|
||||
|
||||
list() {
|
||||
return Array.from(this.plugins.values())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user