[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
@@ -93,6 +93,7 @@ export function useNativeMenu(opts: {
|
||||
onOpenProject,
|
||||
onSaveProject,
|
||||
onSaveProjectAs,
|
||||
onRestoreBackup,
|
||||
onExportSchedule,
|
||||
onExportIfc,
|
||||
onExportObj,
|
||||
@@ -122,6 +123,7 @@ export function useNativeMenu(opts: {
|
||||
onOpenProject,
|
||||
onSaveProject,
|
||||
onSaveProjectAs,
|
||||
onRestoreBackup,
|
||||
onImport: () => dxfInputRef.current?.click(),
|
||||
onExportPdf: () => setExportOpen(true),
|
||||
onExportDxf: () => setExportDxfOpen(true),
|
||||
|
||||
@@ -30,7 +30,7 @@ import { loadFileio } from "../engine/fileioWasm";
|
||||
import { exportProjectStep } from "../export/exportStep";
|
||||
import { exportGlb, exportMtl, exportObj, exportStl } from "../export/exportMesh";
|
||||
import type { ExportSaveFormat } from "../ui/ExportSaveDialog";
|
||||
import { saveBinaryFile, saveTextFile, saveTextWithCompanion } from "../native/saveFile";
|
||||
import { saveBinaryFile, saveTextFile, saveTextWithCompanion, setExportErrorReporter } from "../native/saveFile";
|
||||
import {
|
||||
saveProjectFile,
|
||||
openProjectFileNative,
|
||||
@@ -41,6 +41,8 @@ import {
|
||||
registerCloseGuard,
|
||||
quitApp,
|
||||
isTauriRuntime,
|
||||
electronFiles,
|
||||
readBackupText,
|
||||
} from "../native/projectFile";
|
||||
import type { LockInfo } from "../native/projectFile";
|
||||
import { addRecentProject, removeRecentProject } from "./recentProjects";
|
||||
@@ -70,6 +72,8 @@ export type ImportDialogState = {
|
||||
/** Von electron-preload.cjs injiziert (nur Electron/Linux, [170]): Projektdatei aus `dossier datei.obp`. */
|
||||
interface LaunchProject {
|
||||
name: string;
|
||||
/** Voller Pfad der Datei ([116]): Speichern schreibt dann atomar dorthin. */
|
||||
path?: string;
|
||||
text: string;
|
||||
}
|
||||
declare global {
|
||||
@@ -99,6 +103,15 @@ function openProjectOrReport(content: string, notify: (msg: string, kind?: Notif
|
||||
return r.project;
|
||||
}
|
||||
|
||||
/** Angebot „Vorige Fassung (.obp~) öffnen" ([116]): die Datei liess sich nicht laden, die Sicherung schon. */
|
||||
export interface BackupOffer {
|
||||
path: string;
|
||||
text: string;
|
||||
/** Fehlermeldung beim Laden der eigentlichen Datei. */
|
||||
error: string;
|
||||
lockOwned: boolean;
|
||||
}
|
||||
|
||||
export function useProjectFileActions(
|
||||
setImportState: (next: ImportDialogState) => void,
|
||||
setExportSaveFormat: (format: ExportSaveFormat | null) => void,
|
||||
@@ -143,6 +156,12 @@ export function useProjectFileActions(
|
||||
// Eine verworfene Aktion (Neu/Öffnen), die erst nach Bestätigung des
|
||||
// "Ungespeicherte Änderungen"-Dialogs ausgeführt wird — s. `guardDiscard`.
|
||||
const [pendingDiscard, setPendingDiscard] = useState<(() => void) | null>(null);
|
||||
const [backupOffer, setBackupOffer] = useState<BackupOffer | null>(null);
|
||||
// Gescheiterte Export-Schreibvorgänge ([116], native/saveFile.ts) als Meldung statt stiller Konsole.
|
||||
useEffect(() => {
|
||||
setExportErrorReporter((msg) => notify(`${t("export.save.failed")} ${msg}`, "error"));
|
||||
return () => setExportErrorReporter(null);
|
||||
}, []);
|
||||
/**
|
||||
* Führt `action` (Projekt ERSETZEN — Neu/Öffnen) nur direkt aus, wenn seit
|
||||
* dem letzten Speichern/Laden nichts geändert wurde; sonst wird sie als
|
||||
@@ -539,6 +558,59 @@ export function useProjectFileActions(
|
||||
r.readAsText(file);
|
||||
};
|
||||
|
||||
// Öffnet `content`; lässt sich die Datei nicht laden, aber ihre Sicherung `<pfad>~` ([116]), bietet ein Overlay-Dialog die
|
||||
// vorige Fassung an (der Lock bleibt bis zur Antwort), sonst wird der Fehler gemeldet. `null` = nicht geladen.
|
||||
const openOrOfferBackup = (content: string, path: string | null, lockOwned: boolean): Project | null => {
|
||||
const r = getState().openProjectFile(content);
|
||||
if (r.ok) {
|
||||
for (const w of r.warnings) console.info(`Projekt geladen mit Hinweis: ${w}`);
|
||||
return r.project;
|
||||
}
|
||||
const fail = () => {
|
||||
notify(`${t("project.open.failed")} ${r.error}`, "error");
|
||||
if (path && lockOwned) void releaseProjectLock(path);
|
||||
};
|
||||
if (!path) fail();
|
||||
else
|
||||
void readBackupText(path).then((text) => {
|
||||
if (text && parseProjectFile(text).ok) setBackupOffer({ path, text, error: r.error, lockOwned });
|
||||
else fail();
|
||||
});
|
||||
return null;
|
||||
};
|
||||
const onBackupOfferAccept = () => {
|
||||
if (!backupOffer) return;
|
||||
const { path, text, lockOwned } = backupOffer;
|
||||
setBackupOffer(null);
|
||||
applyOpenedProjectFile(path, text, lockOwned, true);
|
||||
};
|
||||
const onBackupOfferCancel = () => {
|
||||
if (backupOffer?.lockOwned) void releaseProjectLock(backupOffer.path);
|
||||
setBackupOffer(null);
|
||||
};
|
||||
// Datei-Menü „Vorige Fassung wiederherstellen …": die Sicherung der aktuellen Datei (`<pfad>~`) wird zum offenen
|
||||
// Projekt, die Datei selbst bleibt bis zum nächsten Speichern unverändert (dann wird die jetzige Fassung zur Sicherung).
|
||||
const onRestoreBackup = () => {
|
||||
const path = savePathRef.current;
|
||||
if (!path) {
|
||||
notify(t("backup.none"), "info");
|
||||
return;
|
||||
}
|
||||
void readBackupText(path).then((text) => {
|
||||
if (!text) {
|
||||
notify(t("backup.none"), "info");
|
||||
return;
|
||||
}
|
||||
if (!parseProjectFile(text).ok) {
|
||||
notify(t("backup.damaged"), "error");
|
||||
return;
|
||||
}
|
||||
guardDiscard(() => {
|
||||
if (openProjectOrReport(text, notify)) notify(t("backup.restored"), "info");
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Gibt den Lock der zuletzt geöffneten Projektdatei frei (falls wir ihn
|
||||
// halten) und vergisst den Pfad — vor dem Wechsel auf ein anderes Projekt.
|
||||
const releaseCurrentProjectLock = () => {
|
||||
@@ -553,15 +625,13 @@ export function useProjectFileActions(
|
||||
// Übernimmt ein per Pfad geöffnetes Projekt (Tauri-Fluss): parst, prüft grob
|
||||
// die Form, gibt einen evtl. vorher gehaltenen Lock frei und merkt sich Pfad
|
||||
// + ob WIR den Schreib-Lock für die neue Datei halten.
|
||||
const applyOpenedProjectFile = (path: string, content: string, lockOwned: boolean) => {
|
||||
const applyOpenedProjectFile = (path: string, content: string, lockOwned: boolean, fromBackup = false) => {
|
||||
guardDiscard(() => {
|
||||
const loaded = openProjectOrReport(content, notify);
|
||||
if (!loaded) {
|
||||
if (lockOwned) void releaseProjectLock(path);
|
||||
return;
|
||||
}
|
||||
const loaded = openOrOfferBackup(content, path, lockOwned);
|
||||
if (!loaded) return;
|
||||
releaseCurrentProjectLock();
|
||||
lastSavedProjectRef.current = loaded;
|
||||
// Aus der Sicherung: bleibt „ungespeichert", bis der Nutzer die Datei bewusst überschreibt.
|
||||
if (!fromBackup) lastSavedProjectRef.current = loaded;
|
||||
openedProjectPathRef.current = path;
|
||||
savePathRef.current = path;
|
||||
setAnonymityProjectFileName(path);
|
||||
@@ -689,18 +759,21 @@ export function useProjectFileActions(
|
||||
}
|
||||
};
|
||||
|
||||
const onProjectFileChosen = (file: File) => {
|
||||
const onProjectFileChosen = (file: File, knownPath?: string | null) => {
|
||||
const r = new FileReader();
|
||||
r.onload = () => {
|
||||
const content = String(r.result ?? "");
|
||||
// Electron ([116]): der Pfad der gewählten Datei erlaubt atomares Speichern und die Sicherung `.obp~`; im Browser gibt es keinen.
|
||||
const path = knownPath ?? electronFiles()?.pathForFile(file) ?? null;
|
||||
// Browser-Pfad hat keinen echten Dateipfad → kein Lock für die neue
|
||||
// Datei, aber ein evtl. vorher (unter Tauri) gehaltener Lock wird frei.
|
||||
guardDiscard(() => {
|
||||
const loaded = openProjectOrReport(content, notify);
|
||||
const loaded = openOrOfferBackup(content, path, false);
|
||||
if (!loaded) return;
|
||||
releaseCurrentProjectLock();
|
||||
lastSavedProjectRef.current = loaded;
|
||||
setAnonymityProjectFileName(file.name); // [208] Browser/Electron-Start: kein Pfad, aber der Dateiname
|
||||
if (path) savePathRef.current = path;
|
||||
setAnonymityProjectFileName(path ?? file.name); // [208] Browser/Electron-Start: kein Pfad, aber der Dateiname
|
||||
});
|
||||
};
|
||||
r.onerror = () => notify(`${t("project.open.failed")} ${r.error?.message ?? ""}`, "error");
|
||||
@@ -714,7 +787,7 @@ export function useProjectFileActions(
|
||||
useEffect(() => {
|
||||
const api = window.dossierLaunch;
|
||||
if (!api) return;
|
||||
const open = (f: LaunchProject) => chosenRef.current(new File([f.text], f.name));
|
||||
const open = (f: LaunchProject) => chosenRef.current(new File([f.text], f.name), f.path);
|
||||
void api.takeOpenFile().then((f) => f && open(f));
|
||||
return api.onOpenFile(open);
|
||||
}, []);
|
||||
@@ -774,6 +847,10 @@ export function useProjectFileActions(
|
||||
projectLockOwnedRef,
|
||||
lockConflict,
|
||||
setLockConflict,
|
||||
backupOffer,
|
||||
onBackupOfferAccept,
|
||||
onBackupOfferCancel,
|
||||
onRestoreBackup,
|
||||
recovery,
|
||||
onRecoverRestore,
|
||||
onRecoverDiscard,
|
||||
|
||||
Reference in new issue
Block a user