Files
takerofnotes-app/out/main/index.js
2026-02-23 13:43:04 -05:00

154 lines
4.5 KiB
JavaScript

"use strict";
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,
height: 549,
show: false,
autoHideMenuBar: true,
webPreferences: {
preload: preloadPath,
sandbox: false
}
});
mainWindow2.on("ready-to-show", () => {
mainWindow2.show();
});
mainWindow2.webContents.setWindowOpenHandler((details) => {
electron.shell.openExternal(details.url);
return { action: "deny" };
});
if (utils.is.dev && process.env["ELECTRON_RENDERER_URL"]) {
mainWindow2.loadURL(process.env["ELECTRON_RENDERER_URL"]);
} else {
mainWindow2.loadFile(rendererPath);
}
}
function createNoteWindow(noteId) {
const noteWindow = new electron.BrowserWindow({
width: 354,
height: 549,
autoHideMenuBar: true,
webPreferences: {
preload: preloadPath,
contextIsolation: true,
nodeIntegration: false
}
});
if (utils.is.dev && process.env["ELECTRON_RENDERER_URL"]) {
noteWindow.loadURL(
`${process.env["ELECTRON_RENDERER_URL"]}/note/${noteId}`
);
} else {
mainWindow.loadFile(rendererPath, {
path: `/notes/${noteId}`
});
}
}
electron.app.whenReady().then(() => {
utils.electronApp.setAppUserModelId("com.electron");
electron.app.on("browser-window-created", (_, window) => {
utils.optimizer.watchWindowShortcuts(window);
});
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") {
electron.app.quit();
}
});