local file saving/reading
This commit is contained in:
@@ -1,7 +1,77 @@
|
||||
"use strict";
|
||||
const electron = require("electron");
|
||||
const path = require("path");
|
||||
const utils = require("@electron-toolkit/utils");
|
||||
const electron = require("electron");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const BASE_DIR = path.join(electron.app.getPath("userData"), "notes-storage");
|
||||
const ensureBaseDir = () => {
|
||||
if (!fs.existsSync(BASE_DIR)) {
|
||||
fs.mkdirSync(BASE_DIR, { recursive: true });
|
||||
}
|
||||
};
|
||||
const sanitizeRelativePath = (relativePath) => {
|
||||
const resolved = path.join(BASE_DIR, relativePath);
|
||||
if (!resolved.startsWith(BASE_DIR)) {
|
||||
throw new Error("Invalid path");
|
||||
}
|
||||
return resolved;
|
||||
};
|
||||
const readAllNotesRecursive = (dir = BASE_DIR, base = BASE_DIR) => {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
let results = [];
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
results = results.concat(readAllNotesRecursive(fullPath, base));
|
||||
}
|
||||
if (entry.isFile() && entry.name.endsWith(".md")) {
|
||||
const content = fs.readFileSync(fullPath, "utf-8");
|
||||
results.push({
|
||||
name: entry.name,
|
||||
path: path.relative(base, fullPath),
|
||||
content
|
||||
});
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
const createNote = (relativePath, content = "") => {
|
||||
const fullPath = sanitizeRelativePath(relativePath);
|
||||
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||
fs.writeFileSync(fullPath, content, "utf-8");
|
||||
return true;
|
||||
};
|
||||
const createDirectory = (relativePath) => {
|
||||
const fullPath = sanitizeRelativePath(relativePath);
|
||||
fs.mkdirSync(fullPath, { recursive: true });
|
||||
return true;
|
||||
};
|
||||
const readNote = (relativePath) => {
|
||||
const fullPath = sanitizeRelativePath(relativePath);
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
createNote(relativePath);
|
||||
}
|
||||
return fs.readFileSync(fullPath, "utf-8");
|
||||
};
|
||||
const updateNote = (relativePath, content) => {
|
||||
const fullPath = sanitizeRelativePath(relativePath);
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
throw new Error("Note does not exist");
|
||||
}
|
||||
fs.writeFileSync(fullPath, content, "utf-8");
|
||||
return true;
|
||||
};
|
||||
const notes = {
|
||||
ensureBaseDir,
|
||||
sanitizeRelativePath,
|
||||
readAllNotesRecursive,
|
||||
createNote,
|
||||
createDirectory,
|
||||
readNote,
|
||||
updateNote
|
||||
};
|
||||
const preloadPath = path.join(__dirname, "../preload/index.js");
|
||||
const rendererPath = path.join(__dirname, "../renderer/index.html");
|
||||
function createWindow() {
|
||||
const mainWindow2 = new electron.BrowserWindow({
|
||||
width: 354,
|
||||
@@ -9,7 +79,7 @@ function createWindow() {
|
||||
show: false,
|
||||
autoHideMenuBar: true,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "../preload/index.js"),
|
||||
preload: preloadPath,
|
||||
sandbox: false
|
||||
}
|
||||
});
|
||||
@@ -23,7 +93,7 @@ function createWindow() {
|
||||
if (utils.is.dev && process.env["ELECTRON_RENDERER_URL"]) {
|
||||
mainWindow2.loadURL(process.env["ELECTRON_RENDERER_URL"]);
|
||||
} else {
|
||||
mainWindow2.loadFile(path.join(__dirname, "../renderer/index.html"));
|
||||
mainWindow2.loadFile(rendererPath);
|
||||
}
|
||||
}
|
||||
function createNoteWindow(noteId) {
|
||||
@@ -32,7 +102,7 @@ function createNoteWindow(noteId) {
|
||||
height: 549,
|
||||
autoHideMenuBar: true,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "preload.js"),
|
||||
preload: preloadPath,
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false
|
||||
}
|
||||
@@ -42,7 +112,7 @@ function createNoteWindow(noteId) {
|
||||
`${process.env["ELECTRON_RENDERER_URL"]}/note/${noteId}`
|
||||
);
|
||||
} else {
|
||||
mainWindow.loadFile(path.join(__dirname, "../renderer/index.html"), {
|
||||
mainWindow.loadFile(rendererPath, {
|
||||
path: `/notes/${noteId}`
|
||||
});
|
||||
}
|
||||
@@ -52,14 +122,29 @@ electron.app.whenReady().then(() => {
|
||||
electron.app.on("browser-window-created", (_, window) => {
|
||||
utils.optimizer.watchWindowShortcuts(window);
|
||||
});
|
||||
console.log(electron.app.getPath("userData"));
|
||||
createWindow();
|
||||
notes.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.app.on("window-all-closed", () => {
|
||||
if (process.platform !== "darwin") {
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
"use strict";
|
||||
const electron = require("electron");
|
||||
const preload = require("@electron-toolkit/preload");
|
||||
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 })
|
||||
};
|
||||
if (process.contextIsolated) {
|
||||
try {
|
||||
electron.contextBridge.exposeInMainWorld("electron", preload.electronAPI);
|
||||
electron.contextBridge.exposeInMainWorld("api", api);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
} else {
|
||||
window.electron = preload.electronAPI;
|
||||
window.api = api;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user