[116] Exporte atomar (Tauri save_export), Electron speichert Projekte atomar im Hauptprozess, Wiederherstellung aus .obp~ (Öffnen-Angebot + Datei-Menü)
This commit is contained in:
22 files changed
+755
-29
No files matched your search
@@ -0,0 +1,75 @@
|
||||
// electron-files.cjs — Projektdateien im Electron-Hauptprozess ([116]): atomar schreiben (Temp-Datei + fsync + Umbenennen,
|
||||
// Vorversion als `<pfad>~`), „Speichern unter" mit Dialog, Sicherung erkennen/lesen. Gegenstück zu src-tauri/src/atomic_save.rs
|
||||
// (Tauri). Renderer-Seite: src/native/projectFile.ts (`window.dossierFiles`, electron-preload.cjs).
|
||||
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
/** Schreibt `text` atomar nach `file`; mit `keepBackup` landet die Vorversion in `<file>~`. Wirft bei Fehler, das Ziel bleibt heil. */
|
||||
function writeAtomic(file, text, keepBackup) {
|
||||
const tmp = `${file}.tmp`;
|
||||
const backup = `${file}~`;
|
||||
try {
|
||||
const fd = fs.openSync(tmp, "w");
|
||||
try {
|
||||
fs.writeFileSync(fd, text, "utf8");
|
||||
fs.fsyncSync(fd);
|
||||
} finally {
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
// Kopie statt Umbenennen: der Zielpfad existiert so zu jedem Zeitpunkt.
|
||||
if (keepBackup && fs.existsSync(file)) fs.copyFileSync(file, backup);
|
||||
fs.renameSync(tmp, file);
|
||||
} catch (err) {
|
||||
try {
|
||||
fs.rmSync(tmp, { force: true });
|
||||
} catch {
|
||||
/* ignorieren */
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
try {
|
||||
const dir = fs.openSync(path.dirname(file), "r");
|
||||
try {
|
||||
fs.fsyncSync(dir);
|
||||
} finally {
|
||||
fs.closeSync(dir);
|
||||
}
|
||||
} catch {
|
||||
/* Ordner-Sync ist best effort (Windows) */
|
||||
}
|
||||
}
|
||||
|
||||
const backupExists = (file) => {
|
||||
try {
|
||||
return fs.statSync(`${file}~`).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/** Meldet die IPC-Kanäle an. `getWindow()` liefert das Hauptfenster (Elternfenster der Dialoge). */
|
||||
function register(ipcMain, dialog, getWindow) {
|
||||
ipcMain.handle("file:save-atomic", (_e, file, text) => {
|
||||
if (typeof file !== "string" || !file || typeof text !== "string") throw new Error("Ungültiger Aufruf");
|
||||
writeAtomic(file, text, true);
|
||||
return true;
|
||||
});
|
||||
// „Speichern unter": Dialog, dann atomar schreiben; null = abgebrochen.
|
||||
ipcMain.handle("file:save-as", async (_e, defaultName, text, filters) => {
|
||||
if (typeof text !== "string") throw new Error("Ungültiger Aufruf");
|
||||
const win = getWindow();
|
||||
const opts = { defaultPath: String(defaultName || "projekt.obp"), filters: Array.isArray(filters) ? filters : undefined };
|
||||
const r = win ? await dialog.showSaveDialog(win, opts) : await dialog.showSaveDialog(opts);
|
||||
if (r.canceled || !r.filePath) return null;
|
||||
writeAtomic(r.filePath, text, true);
|
||||
return r.filePath;
|
||||
});
|
||||
ipcMain.handle("file:backup-exists", (_e, file) => typeof file === "string" && backupExists(file));
|
||||
ipcMain.handle("file:read-backup", (_e, file) => {
|
||||
if (typeof file !== "string" || !backupExists(file)) return null;
|
||||
return fs.readFileSync(`${file}~`, "utf8");
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { register, writeAtomic, backupExists };
|
||||
@@ -4,11 +4,12 @@
|
||||
// (render2d-WASM-Engine, ?engine=wasm) zuverlässig läuft. Kein Rust-Backend
|
||||
// nötig: compute_joins hat einen TS-Fallback (src/compute/index.ts).
|
||||
|
||||
const { app, BrowserWindow, Menu, ipcMain, protocol, session } = require("electron");
|
||||
const { app, BrowserWindow, Menu, ipcMain, protocol, session, dialog } = require("electron");
|
||||
const path = require("node:path");
|
||||
const fs = require("node:fs");
|
||||
const { buildCsp } = require("./csp.cjs");
|
||||
require("./electron-blender.cjs").register(ipcMain); // [211] Blender als eigener Prozess
|
||||
require("./electron-files.cjs").register(ipcMain, dialog, () => mainWindow); // [116] Projekt atomar speichern, Sicherung lesen
|
||||
|
||||
// Randloses App-Fenster wie chromium-shell.sh (--app=…): keine native
|
||||
// Menüleiste (File/Edit/View/Window), kein Fensterrahmen. Minimieren/
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// electron-main.cjs) — sonst gaebe es keine Moeglichkeit, das Fenster ueberhaupt
|
||||
// zu steuern.
|
||||
|
||||
const { contextBridge, ipcRenderer } = require("electron");
|
||||
const { contextBridge, ipcRenderer, webUtils } = require("electron");
|
||||
|
||||
contextBridge.exposeInMainWorld("dossierWindow", {
|
||||
minimize: () => ipcRenderer.send("window:minimize"),
|
||||
@@ -56,3 +56,19 @@ contextBridge.exposeInMainWorld("dossierLaunch", {
|
||||
return () => ipcRenderer.removeListener("launch:open-file", listener);
|
||||
},
|
||||
});
|
||||
|
||||
// [116] Projektdateien: atomar speichern (Temp + Umbenennen, Vorversion als `~`), „Speichern unter", Sicherung lesen.
|
||||
// Renderer-Seite: src/native/projectFile.ts. `pathForFile` liefert den Pfad einer im Dialog gewählten Datei.
|
||||
contextBridge.exposeInMainWorld("dossierFiles", {
|
||||
saveAtomic: (path, text) => ipcRenderer.invoke("file:save-atomic", path, text),
|
||||
saveAs: (defaultName, text, filters) => ipcRenderer.invoke("file:save-as", defaultName, text, filters),
|
||||
backupExists: (path) => ipcRenderer.invoke("file:backup-exists", path),
|
||||
readBackup: (path) => ipcRenderer.invoke("file:read-backup", path),
|
||||
pathForFile: (file) => {
|
||||
try {
|
||||
return webUtils.getPathForFile(file) || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user