143 lines
4.1 KiB
JavaScript
143 lines
4.1 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 notesAPI = {
|
|
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();
|
|
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("notesAPI:call", async (_, method, args) => {
|
|
if (!notesAPI[method]) {
|
|
throw new Error("Invalid method");
|
|
}
|
|
return notesAPI[method](...args);
|
|
});
|
|
});
|
|
electron.app.on("window-all-closed", () => {
|
|
if (process.platform !== "darwin") {
|
|
electron.app.quit();
|
|
}
|
|
});
|