From 9ac9d73b0a09cbea342203af6b2baa57d3a3bbd3 Mon Sep 17 00:00:00 2001 From: nicwands Date: Mon, 23 Feb 2026 14:19:07 -0500 Subject: [PATCH] notes API cleanup --- out/main/index.js | 25 ++++++--------------- out/preload/index.js | 11 +++++---- src/main/index.js | 25 +++++++-------------- src/main/{notesStorage.js => notesAPI.js} | 15 +++++-------- src/preload/index.js | 14 ++++++------ src/renderer/src/composables/useNotes.js | 10 ++++----- src/renderer/src/composables/useOpenNote.js | 2 +- src/renderer/src/views/Directory.vue | 10 ++++++++- 8 files changed, 48 insertions(+), 64 deletions(-) rename src/main/{notesStorage.js => notesAPI.js} (82%) diff --git a/out/main/index.js b/out/main/index.js index a9dd42b..0e84aae 100644 --- a/out/main/index.js +++ b/out/main/index.js @@ -61,9 +61,7 @@ const updateNote = (relativePath, content) => { fs.writeFileSync(fullPath, content, "utf-8"); return true; }; -const notes = { - ensureBaseDir, - sanitizeRelativePath, +const notesAPI = { readAllNotesRecursive, createNote, createDirectory, @@ -123,27 +121,18 @@ electron.app.whenReady().then(() => { utils.optimizer.watchWindowShortcuts(window); }); createWindow(); - notes.ensureBaseDir(); + ensureBaseDir(); electron.app.on("activate", function() { if (electron.BrowserWindow.getAllWindows().length === 0) createWindow(); }); electron.ipcMain.on("open-note-window", (_, noteId) => { createNoteWindow(noteId); }); - electron.ipcMain.handle("notes:list", () => { - return notes.readAllNotesRecursive(); - }); - electron.ipcMain.handle("notes:create", (_, { path: path2, content }) => { - return notes.createNote(path2, content); - }); - electron.ipcMain.handle("notes:createDir", (_, path2) => { - return notes.createDirectory(path2); - }); - electron.ipcMain.handle("notes:read", (_, path2) => { - return notes.readNote(path2); - }); - electron.ipcMain.handle("notes:update", (_, { path: path2, content }) => { - return notes.updateNote(path2, content); + electron.ipcMain.handle("notesAPI:call", async (_, method, args) => { + if (!notesAPI[method]) { + throw new Error("Invalid method"); + } + return notesAPI[method](...args); }); }); electron.app.on("window-all-closed", () => { diff --git a/out/preload/index.js b/out/preload/index.js index fc9eb36..523f2d2 100644 --- a/out/preload/index.js +++ b/out/preload/index.js @@ -3,16 +3,15 @@ const electron = require("electron"); const api = { openNoteWindow: (noteId) => { electron.ipcRenderer.send("open-note-window", noteId); - }, - listNotes: () => electron.ipcRenderer.invoke("notes:list"), - createNote: (path, content) => electron.ipcRenderer.invoke("notes:create", { path, content }), - createNoteDir: (path) => electron.ipcRenderer.invoke("notes:createDir", path), - readNote: (path) => electron.ipcRenderer.invoke("notes:read", path), - updateNote: (path, content) => electron.ipcRenderer.invoke("notes:update", { path, content }) + } +}; +const notesAPI = { + call: (method, ...args) => electron.ipcRenderer.invoke("notesAPI:call", method, args) }; if (process.contextIsolated) { try { electron.contextBridge.exposeInMainWorld("api", api); + electron.contextBridge.exposeInMainWorld("notesAPI", notesAPI); } catch (error) { console.error(error); } diff --git a/src/main/index.js b/src/main/index.js index 655d0c5..3520f46 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,6 +1,6 @@ import { electronApp, optimizer, is } from '@electron-toolkit/utils' import { app, shell, BrowserWindow, ipcMain } from 'electron' -import notes from './notesStorage' +import notesAPI, { ensureBaseDir } from './notesAPI' import { join } from 'path' const preloadPath = join(__dirname, '../preload/index.js') @@ -78,7 +78,7 @@ app.whenReady().then(() => { createWindow() // Ensure data directory is present - notes.ensureBaseDir() + ensureBaseDir() app.on('activate', function () { // On macOS it's common to re-create a window in the app when the @@ -91,21 +91,12 @@ app.whenReady().then(() => { createNoteWindow(noteId) }) - // File access - ipcMain.handle('notes:list', () => { - return notes.readAllNotesRecursive() - }) - ipcMain.handle('notes:create', (_, { path, content }) => { - return notes.createNote(path, content) - }) - ipcMain.handle('notes:createDir', (_, path) => { - return notes.createDirectory(path) - }) - ipcMain.handle('notes:read', (_, path) => { - return notes.readNote(path) - }) - ipcMain.handle('notes:update', (_, { path, content }) => { - return notes.updateNote(path, content) + // Handle calls to Notes API + ipcMain.handle('notesAPI:call', async (_, method, args) => { + if (!notesAPI[method]) { + throw new Error('Invalid method') + } + return notesAPI[method](...args) }) }) diff --git a/src/main/notesStorage.js b/src/main/notesAPI.js similarity index 82% rename from src/main/notesStorage.js rename to src/main/notesAPI.js index 395e265..31e7f9d 100644 --- a/src/main/notesStorage.js +++ b/src/main/notesAPI.js @@ -3,14 +3,13 @@ import fs from 'fs' import { join, relative, dirname } from 'path' const BASE_DIR = join(app.getPath('userData'), 'notes-storage') - export const ensureBaseDir = () => { if (!fs.existsSync(BASE_DIR)) { fs.mkdirSync(BASE_DIR, { recursive: true }) } } -export const sanitizeRelativePath = (relativePath) => { +const sanitizeRelativePath = (relativePath) => { const resolved = join(BASE_DIR, relativePath) if (!resolved.startsWith(BASE_DIR)) { throw new Error('Invalid path') @@ -18,7 +17,7 @@ export const sanitizeRelativePath = (relativePath) => { return resolved } -export const readAllNotesRecursive = (dir = BASE_DIR, base = BASE_DIR) => { +const readAllNotesRecursive = (dir = BASE_DIR, base = BASE_DIR) => { const entries = fs.readdirSync(dir, { withFileTypes: true }) let results = [] @@ -43,7 +42,7 @@ export const readAllNotesRecursive = (dir = BASE_DIR, base = BASE_DIR) => { return results } -export const createNote = (relativePath, content = '') => { +const createNote = (relativePath, content = '') => { const fullPath = sanitizeRelativePath(relativePath) fs.mkdirSync(dirname(fullPath), { recursive: true }) @@ -52,13 +51,13 @@ export const createNote = (relativePath, content = '') => { return true } -export const createDirectory = (relativePath) => { +const createDirectory = (relativePath) => { const fullPath = sanitizeRelativePath(relativePath) fs.mkdirSync(fullPath, { recursive: true }) return true } -export const readNote = (relativePath) => { +const readNote = (relativePath) => { const fullPath = sanitizeRelativePath(relativePath) if (!fs.existsSync(fullPath)) { @@ -68,7 +67,7 @@ export const readNote = (relativePath) => { return fs.readFileSync(fullPath, 'utf-8') } -export const updateNote = (relativePath, content) => { +const updateNote = (relativePath, content) => { const fullPath = sanitizeRelativePath(relativePath) if (!fs.existsSync(fullPath)) { @@ -81,8 +80,6 @@ export const updateNote = (relativePath, content) => { } export default { - ensureBaseDir, - sanitizeRelativePath, readAllNotesRecursive, createNote, createDirectory, diff --git a/src/preload/index.js b/src/preload/index.js index 12d7114..14c4e86 100644 --- a/src/preload/index.js +++ b/src/preload/index.js @@ -5,18 +5,18 @@ const api = { openNoteWindow: (noteId) => { ipcRenderer.send('open-note-window', noteId) }, - listNotes: () => ipcRenderer.invoke('notes:list'), - createNote: (path, content) => - ipcRenderer.invoke('notes:create', { path, content }), - createNoteDir: (path) => ipcRenderer.invoke('notes:createDir', path), - readNote: (path) => ipcRenderer.invoke('notes:read', path), - updateNote: (path, content) => - ipcRenderer.invoke('notes:update', { path, content }), +} + +// Implement notes API +const notesAPI = { + call: (method, ...args) => + ipcRenderer.invoke('notesAPI:call', method, args), } if (process.contextIsolated) { try { contextBridge.exposeInMainWorld('api', api) + contextBridge.exposeInMainWorld('notesAPI', notesAPI) } catch (error) { console.error(error) } diff --git a/src/renderer/src/composables/useNotes.js b/src/renderer/src/composables/useNotes.js index 678c927..2e02710 100644 --- a/src/renderer/src/composables/useNotes.js +++ b/src/renderer/src/composables/useNotes.js @@ -8,7 +8,7 @@ export default () => { const fetchNotes = async () => { try { loading.value = true - notes.value = await window.api.listNotes() + notes.value = await window.notesAPI.call('readAllNotesRecursive') } catch (err) { error.value = err.message } finally { @@ -17,22 +17,22 @@ export default () => { } const createNote = async (path, content = '') => { - await window.api.createNote(path, content) + await window.notesAPI.call('createNote', path, content) await fetchNotes() } const createDirectory = async (path) => { - await window.api.createNoteDir(path) + await window.notesAPI.call('createDirectory', path) await fetchNotes() } const readNote = async (path) => { console.log(path) - return await window.api.readNote(path) + return await window.notesAPI.call('readNote', path) } const updateNote = async (path, content) => { - return await window.api.updateNote(path, content) + return await window.notesAPI.call('updateNote', path, content) } return { diff --git a/src/renderer/src/composables/useOpenNote.js b/src/renderer/src/composables/useOpenNote.js index c03bf50..f04a366 100644 --- a/src/renderer/src/composables/useOpenNote.js +++ b/src/renderer/src/composables/useOpenNote.js @@ -1,6 +1,6 @@ import { useRouter } from 'vue-router' -export function useOpenNote() { +export default () => { const router = useRouter() function openNote(noteId, options = {}) { diff --git a/src/renderer/src/views/Directory.vue b/src/renderer/src/views/Directory.vue index c329176..0a5428c 100644 --- a/src/renderer/src/views/Directory.vue +++ b/src/renderer/src/views/Directory.vue @@ -22,7 +22,9 @@