-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
220 lines (190 loc) · 6.03 KB
/
Copy pathmain.js
File metadata and controls
220 lines (190 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const { app, BrowserWindow, ipcMain, dialog } = require("electron");
const path = require("path");
const fs = require("fs");
const Database = require("better-sqlite3");
// Usa path.join con __dirname para asegurar que busque DENTRO de la carpeta de la app
const { initDB } = require(path.join(__dirname, "src", "database", "db"));
const { registerVaultHandlers } = require(
path.join(__dirname, "src", "ipc", "vault"),
);
const { registerMateriaHandlers } = require(
path.join(__dirname, "src", "ipc", "materias"),
);
const { registerApunteHandlers } = require(
path.join(__dirname, "src", "ipc", "apuntes"),
);
const { registerProyectoHandlers } = require(
path.join(__dirname, "src", "ipc", "proyectos"),
);
const { registerTareaHandlers } = require(
path.join(__dirname, "src", "ipc", "tareas"),
);
const { registerTagHandlers } = require(
path.join(__dirname, "src", "ipc", "tags"),
);
const { registerConfigHandlers } = require(
path.join(__dirname, "src", "ipc", "config"),
);
const evaluacionesModule = require(
path.join(__dirname, "src", "ipc", "evaluaciones"),
);
console.log("📦 Módulo evaluaciones completo:", evaluacionesModule);
const configPath = path.join(app.getPath("userData"), "config.json");
const isDev = !app.isPackaged;
// --- FUNCIONES DE CONFIGURACIÓN ---
function getConfig() {
if (!fs.existsSync(configPath)) return {};
try {
return JSON.parse(fs.readFileSync(configPath, "utf-8"));
} catch (e) {
return {};
}
}
function saveConfig(data) {
fs.writeFileSync(configPath, JSON.stringify(data, null, 2));
}
// --- GESTIÓN DE BASE DE DATOS ---
let db = null;
const getDb = () => {
if (!db) throw new Error("Base de datos no conectada.");
return db;
};
function conectarDB(vaultPath) {
try {
if (!vaultPath || !fs.existsSync(vaultPath)) return false;
if (db) db.close();
db = new Database(vaultPath);
initDB(db);
console.log("✅ Conectado a DB en:", vaultPath);
return true;
} catch (error) {
console.error("Error conectando a DB:", error);
return false;
}
}
// --- IPC HANDLERS GLOBALES ---
ipcMain.handle("check-db-status", () => db !== null);
ipcMain.handle("export-to-pdf", async (event, title) => {
const win = BrowserWindow.fromWebContents(event.sender);
try {
const data = await win.webContents.printToPDF({ printBackground: true });
const { filePath } = await dialog.showSaveDialog(win, {
title: "Exportar Apunte como PDF",
defaultPath: path.join(app.getPath("documents"), `${title}.pdf`),
filters: [{ name: "Adobe PDF", extensions: ["pdf"] }],
});
if (filePath) {
fs.writeFileSync(filePath, data);
return { success: true, path: filePath };
}
return { success: false };
} catch (error) {
console.error(error);
return { success: false, error: error.message };
}
});
ipcMain.handle("print-html", async (event, { html, title }) => {
const win = new BrowserWindow({ show: false });
try {
await win.loadURL(
`data:text/html;charset=utf-8,${encodeURIComponent(html)}`,
);
const pdfPath = path.join(app.getPath("documents"), `${title}.pdf`);
const data = await win.webContents.printToPDF({
printBackground: true,
pageSize: "A4",
margin: {
top: "1cm",
bottom: "1cm",
left: "1cm",
right: "1cm",
},
});
fs.writeFileSync(pdfPath, data);
win.close();
return { success: true, path: pdfPath };
} catch (error) {
win.close();
return { success: false, error: error.message };
}
});
// --- VENTANA ---
function createWindow() {
const win = new BrowserWindow({
width: 1440,
height: 1024,
minWidth: 800,
minHeight: 600,
backgroundColor: "#0c0c0c",
show: false,
webPreferences: {
preload: path.join(__dirname, "preload.js"), // path.join es preferible aquí
contextIsolation: true,
nodeIntegration: false,
sandbox: false,
},
autoHideMenuBar: true,
});
win.setMenuBarVisibility(false);
if (isDev) {
win.loadURL("http://localhost:5173");
} else {
// IMPORTANTE: Dado que en tu package.json pusiste "dist/**/*",
// el archivo index.html VIVE dentro de una carpeta dist en el asar.
const indexPath = path.join(__dirname, "dist", "index.html");
win.loadFile(indexPath).catch((err) => {
console.error("Error al cargar index.html:", err);
// Intento de rescate si el archivo quedó en la raíz por error
win.loadFile(path.join(__dirname, "index.html"));
});
}
win.once("ready-to-show", () => {
// Añade esto justo antes de win.show()
win.webContents.on(
"did-fail-load",
(event, errorCode, errorDescription) => {
console.log("❌ Error al cargar:", errorCode, errorDescription);
},
);
win.webContents.on(
"console-message",
(event, level, message, line, sourceId) => {
console.log("🖥 LOG DE LA APP:", message);
},
);
win.show();
win.webContents.openDevTools();
});
//if (isDev) {
// win.webContents.openDevTools();
//}
}
app.commandLine.appendSwitch("disable-dev-shm-usage");
app.whenReady().then(() => {
const config = getConfig();
if (config.vault_path) conectarDB(config.vault_path);
// Registro de Handlers
registerVaultHandlers(ipcMain, dialog, getConfig, saveConfig, conectarDB);
registerMateriaHandlers(ipcMain, getDb);
registerApunteHandlers(ipcMain, getDb);
registerProyectoHandlers(ipcMain, getDb);
registerTareaHandlers(ipcMain, getDb);
registerTagHandlers(ipcMain, getDb);
registerConfigHandlers(ipcMain, getConfig, saveConfig, getDb);
console.log("📝 Intentando registrar evaluaciones handlers...");
if (typeof evaluacionesModule === "function") {
evaluacionesModule(ipcMain, getDb);
} else {
console.error(
"❌ evaluacionesModule no es una función. Es:",
typeof evaluacionesModule,
);
}
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});