notes API cleanup

This commit is contained in:
nicwands
2026-02-23 14:19:07 -05:00
parent 7a670aab92
commit 9ac9d73b0a
8 changed files with 48 additions and 64 deletions

View File

@@ -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)
})
})

View File

@@ -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,

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -1,6 +1,6 @@
import { useRouter } from 'vue-router'
export function useOpenNote() {
export default () => {
const router = useRouter()
function openNote(noteId, options = {}) {

View File

@@ -22,7 +22,9 @@
<script setup>
import { format } from 'fecha'
import { useOpenNote } from '@/composables/useOpenNote'
import useOpenNote from '@/composables/useOpenNote'
import useNotes from '@/composables/useNotes'
import { onMounted } from 'vue'
const capitulum = [
{
@@ -61,6 +63,12 @@ const summarium = [
]
const { openNote } = useOpenNote()
const { notes, fetchNotes } = useNotes()
onMounted(async () => {
await fetchNotes()
console.log(notes.value)
})
const formatDate = (date) => {
return format(date, 'MM/DD/YYYY')