302 lines
12 KiB
TypeScript
302 lines
12 KiB
TypeScript
// Eigenes Projektdatei-Format (Endung `.obp`, "openbureau project") + Lock-Datei
|
|
// gegen gleichzeitiges Öffnen derselben Projektdatei aus zwei Instanzen.
|
|
//
|
|
// Der Dateiinhalt ist unverändert das bisherige Projekt-JSON — nur Endung/
|
|
// Dialog-Filter sind neu. Alte `.json`-Projekte bleiben ladbar (Filter erlaubt
|
|
// beide, s. PROJECT_DIALOG_FILTERS).
|
|
//
|
|
// Der Lock ist ein OS-Advisory-Lock im Rust-Kern (src-tauri/src/lock.rs, Crate
|
|
// `fs4`): eine Sidecar-Datei `foo.obp.lock` wird exklusiv gelockt, solange die
|
|
// Instanz die Datei offen hat. Der OS-Lock fällt beim Prozessende (auch bei
|
|
// Absturz) automatisch weg — hier nur "best effort" für sauberes Timing beim
|
|
// normalen Schliessen. Nur unter Tauri relevant: im reinen Browser gibt es
|
|
// keinen echten Dateipfad (Blob-Download), daher entfällt der Lock dort.
|
|
|
|
import { saveTextFile } from "./saveFile";
|
|
|
|
/** Datei-Endung für Projektdateien (ohne Punkt). Einzige Änderungsstelle. */
|
|
export const PROJECT_EXT = "obp";
|
|
|
|
/** Dialog-Filter für den nativen Speichern-/Öffnen-Dialog: neue Endung + Alt-JSON. */
|
|
export const PROJECT_DIALOG_FILTERS = [
|
|
{ name: "openbureau-Projekt", extensions: [PROJECT_EXT] },
|
|
{ name: "openbureau-Projekt (alt, JSON)", extensions: ["json"] },
|
|
];
|
|
|
|
/** Datei-Endung für VORLAGEN (dasselbe JSON-Format wie `.obp`, aber als Start-
|
|
* punkt für ein neues, noch ungespeichertes Projekt gedacht — "Neu aus
|
|
* Vorlage" öffnet sie, hängt sich aber NICHT an ihren Pfad (kein Lock, kein
|
|
* Überschreiben der Vorlage beim nächsten Speichern). */
|
|
export const PROJECT_TEMPLATE_EXT = "obpt";
|
|
|
|
/** Dialog-Filter für den nativen Vorlage-Öffnen-Dialog. */
|
|
export const PROJECT_TEMPLATE_DIALOG_FILTERS = [
|
|
{ name: "openbureau-Vorlage", extensions: [PROJECT_TEMPLATE_EXT] },
|
|
];
|
|
|
|
/** Erkennt Tauri wie an anderen Stellen (saveFile.ts, nativeSync.ts). */
|
|
export function isTauriRuntime(): boolean {
|
|
return typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
|
|
}
|
|
|
|
/** Ergebnis eines erfolgreichen nativen Öffnen-Vorgangs. */
|
|
export interface OpenedProjectFile {
|
|
path: string;
|
|
content: string;
|
|
}
|
|
|
|
/**
|
|
* Öffnet eine Projektdatei über den NATIVEN Öffnen-Dialog (Filter: `.obp` +
|
|
* `.json`). Nur unter Tauri sinnvoll — liefert `null` im Browser, dort greift
|
|
* weiterhin der bestehende `<input type=file>`-Fluss (FileReader).
|
|
*
|
|
* @returns `null` bei Abbruch/ohne Tauri, sonst Pfad + Textinhalt.
|
|
*/
|
|
export async function openProjectFileNative(): Promise<OpenedProjectFile | null> {
|
|
if (!isTauriRuntime()) return null;
|
|
const { open } = await import("@tauri-apps/plugin-dialog");
|
|
const selected = await open({
|
|
multiple: false,
|
|
directory: false,
|
|
filters: PROJECT_DIALOG_FILTERS,
|
|
});
|
|
if (!selected || Array.isArray(selected)) return null; // Abbruch (oder Mehrfachauswahl, sollte hier nicht vorkommen)
|
|
const { readTextFile } = await import("@tauri-apps/plugin-fs");
|
|
const content = await readTextFile(selected);
|
|
return { path: selected, content };
|
|
}
|
|
|
|
/**
|
|
* Öffnet eine VORLAGEN-Datei (`.obpt`) über den nativen Öffnen-Dialog. Nur
|
|
* unter Tauri sinnvoll — liefert `null` im Browser, dort greift der
|
|
* `<input type=file accept=".obpt">`-Fluss (analog `openProjectFileNative`).
|
|
* Anders als beim Projekt-Öffnen wird der zurückgegebene Pfad NICHT als
|
|
* "aktuell offene Datei" gemerkt — die Vorlage bleibt unverändert.
|
|
*
|
|
* @returns `null` bei Abbruch/ohne Tauri, sonst Pfad + Textinhalt.
|
|
*/
|
|
export async function openProjectTemplateNative(): Promise<OpenedProjectFile | null> {
|
|
if (!isTauriRuntime()) return null;
|
|
const { open } = await import("@tauri-apps/plugin-dialog");
|
|
const selected = await open({
|
|
multiple: false,
|
|
directory: false,
|
|
filters: PROJECT_TEMPLATE_DIALOG_FILTERS,
|
|
});
|
|
if (!selected || Array.isArray(selected)) return null;
|
|
const { readTextFile } = await import("@tauri-apps/plugin-fs");
|
|
const content = await readTextFile(selected);
|
|
return { path: selected, content };
|
|
}
|
|
|
|
/**
|
|
* Öffnet eine Projektdatei über einen BEKANNTEN Pfad (kein Dialog) — für den
|
|
* Startbildschirm/„Zuletzt geöffnet" (state/recentProjects.ts): der Pfad kommt
|
|
* dort schon aus einem früheren Öffnen/Speichern. Nur unter Tauri sinnvoll.
|
|
*
|
|
* @returns `null` ohne Tauri oder wenn die Datei nicht mehr lesbar ist (z. B.
|
|
* gelöscht/verschoben) — der Aufrufer sollte den Eintrag dann aus der
|
|
* Zuletzt-geöffnet-Liste entfernen.
|
|
*/
|
|
export async function openProjectFileAtPath(path: string): Promise<OpenedProjectFile | null> {
|
|
if (!isTauriRuntime()) return null;
|
|
try {
|
|
const { readTextFile } = await import("@tauri-apps/plugin-fs");
|
|
const content = await readTextFile(path);
|
|
return { path, content };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Speichert das Projekt-JSON in eine `.obp`-Datei. Unter Tauri nativer
|
|
* Speichern-Dialog mit Projekt-Filter, im Browser Blob-Download — beides via
|
|
* {@link saveTextFile} (dort steckt die WKWebView-Härtung).
|
|
*
|
|
* @param json Fertig serialisiertes Projekt (JSON.stringify des ProjectSlice).
|
|
* @param projectName `project.name` für den Standard-Dateinamen (Fallback "projekt").
|
|
* @param onSavedPath Optionaler Callback mit dem gewählten Pfad (Zuletzt-
|
|
* geöffnet-Liste, s. state/recentProjects.ts).
|
|
* Schreibt atomar ([116]); wirft `SaveError`, wenn das Schreiben scheitert.
|
|
*/
|
|
export async function saveProjectFile(
|
|
json: string,
|
|
projectName: string,
|
|
onSavedPath?: (path: string) => void,
|
|
knownPath?: string | null,
|
|
): Promise<boolean> {
|
|
// „Speichern" (Cmd/Ctrl+S): bekannter Pfad → ohne Dialog atomar überschreiben.
|
|
if (knownPath && isTauriRuntime()) {
|
|
const { writeFileAtomic } = await import("./saveFile");
|
|
await writeFileAtomic(knownPath, json);
|
|
onSavedPath?.(knownPath);
|
|
return true;
|
|
}
|
|
const filename = `${projectName || "projekt"}.${PROJECT_EXT}`;
|
|
return saveTextFile(json, filename, "application/json", PROJECT_DIALOG_FILTERS, onSavedPath, true);
|
|
}
|
|
|
|
// ── Lock gegen Doppelöffnung (nur Tauri) ────────────────────────────────────
|
|
|
|
/** Info-Block aus der Lock-Sidecar-Datei — für die Konflikt-Meldung an den Nutzer. */
|
|
export interface LockInfo {
|
|
pid: number;
|
|
hostname: string;
|
|
timestamp: string;
|
|
}
|
|
|
|
export type LockResult = { ok: true } | { ok: false; info: LockInfo | null };
|
|
|
|
/**
|
|
* Holt (ungeguardet) die Tauri-`invoke`-Funktion, oder `null` ohne Tauri-Core.
|
|
* Anders als das Muster in plan/nativeSync.ts schluckt dieser Helfer KEINE
|
|
* Fehler aus dem eigentlichen Aufruf — ein `Err(...)` vom
|
|
* Rust-Command (hier: „schon gelockt", inkl. {@link LockInfo}-Payload) muss bis
|
|
* zum Aufrufer durchgereicht werden.
|
|
*/
|
|
async function tauriInvokeFn(): Promise<
|
|
((cmd: string, args?: Record<string, unknown>) => Promise<unknown>) | null
|
|
> {
|
|
try {
|
|
// @ts-ignore optionales Paket
|
|
const mod = await import(/* @vite-ignore */ "@tauri-apps/api/core").catch(() => null);
|
|
if (!mod || typeof (mod as any).invoke !== "function") return null;
|
|
return (mod as any).invoke;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fordert den exklusiven Lock für `path` an (Rust-Command `acquire_project_lock`,
|
|
* legt `path + ".lock"` an und hält einen fs4-OS-Lock darauf). Im Browser (kein
|
|
* Tauri) sofort `{ok:true}` — dort gibt es keinen echten Pfad, also keinen Lock.
|
|
*/
|
|
export async function acquireProjectLock(path: string): Promise<LockResult> {
|
|
if (!isTauriRuntime()) return { ok: true };
|
|
const invoke = await tauriInvokeFn();
|
|
if (!invoke) return { ok: true }; // Tauri-Core nicht verfügbar -> nicht blockieren
|
|
try {
|
|
await invoke("acquire_project_lock", { path });
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, info: isLockInfo(err) ? err : null };
|
|
}
|
|
}
|
|
|
|
/** Gibt den Lock für `path` frei (Rust-Command `release_project_lock`). No-op ohne Tauri. */
|
|
export async function releaseProjectLock(path: string): Promise<void> {
|
|
if (!isTauriRuntime()) return;
|
|
const invoke = await tauriInvokeFn();
|
|
if (!invoke) return;
|
|
try {
|
|
await invoke("release_project_lock", { path });
|
|
} catch {
|
|
// Freigabe ist best effort — der OS-Lock fällt beim Prozessende ohnehin weg.
|
|
}
|
|
}
|
|
|
|
function isLockInfo(v: unknown): v is LockInfo {
|
|
return (
|
|
typeof v === "object" &&
|
|
v !== null &&
|
|
typeof (v as LockInfo).pid === "number" &&
|
|
typeof (v as LockInfo).hostname === "string"
|
|
);
|
|
}
|
|
|
|
/** Was beim Schliessen des Hauptfensters zu tun ist ([116]). */
|
|
export interface CloseGuardOptions {
|
|
/** Pfad der offenen Projektdatei (Lock-Freigabe), sonst `null`. */
|
|
getPath: () => string | null;
|
|
/** Ungespeicherte Änderungen? */
|
|
isDirty: () => boolean;
|
|
/** Fragt den Nutzer (Overlay-Dialog Speichern / Nicht speichern / Abbrechen); `proceed` schliesst wirklich. */
|
|
requestClose: (proceed: () => void) => void;
|
|
}
|
|
|
|
/**
|
|
* Schliessen angefordert: ohne ungespeicherte Änderungen Lock freigeben und durchlassen (`false`); sonst das
|
|
* Schliessen verhindern, den Dialog zeigen und erst nach Bestätigung (`proceed`) Lock freigeben und `close` rufen (`true`).
|
|
* Cmd+Q, die Titelleiste und der Schliessen-Knopf laufen alle hierüber.
|
|
*/
|
|
export function handleCloseRequested(event: { preventDefault: () => void }, o: CloseGuardOptions, close: () => void): boolean {
|
|
const release = () => {
|
|
const path = o.getPath();
|
|
if (path) void releaseProjectLock(path);
|
|
};
|
|
if (!o.isDirty()) {
|
|
release();
|
|
return false;
|
|
}
|
|
event.preventDefault();
|
|
o.requestClose(() => {
|
|
release();
|
|
close();
|
|
});
|
|
return true;
|
|
}
|
|
|
|
/** Beendet die App ohne weitere Rückfrage (Tauri; nach dem Bestätigen des Dialogs). */
|
|
export async function quitApp(): Promise<void> {
|
|
if (!isTauriRuntime()) return;
|
|
try {
|
|
const { exit } = await import("@tauri-apps/plugin-process");
|
|
await exit(0);
|
|
} catch (e) {
|
|
console.error("App beenden fehlgeschlagen.", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Schliess-Wächter des Hauptfensters ([116]): ungespeicherte Änderungen fragen nach (Overlay, kein `window.confirm`),
|
|
* der Datei-Lock wird beim Schliessen frei. Tauri: `onCloseRequested` (zerstört das Fenster erst nach Bestätigung);
|
|
* Electron: Hauptprozess leitet das Schliessen an uns (`window:close-requested`); Browser: `beforeunload`
|
|
* (das System-Fenster des Browsers; ein eigener Dialog ist dort nicht möglich). Liefert die Abmeldung.
|
|
*/
|
|
export function registerCloseGuard(o: CloseGuardOptions): () => void {
|
|
if (typeof window === "undefined") return () => {};
|
|
if (isTauriRuntime()) {
|
|
let off: (() => void) | null = null;
|
|
let cancelled = false;
|
|
void (async () => {
|
|
try {
|
|
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
|
const win = getCurrentWindow();
|
|
const un = await win.onCloseRequested((event) => {
|
|
handleCloseRequested(event, o, () => void win.destroy());
|
|
});
|
|
if (cancelled) un();
|
|
else off = un;
|
|
} catch {
|
|
// Tauri-window-API nicht verfügbar -> ignorieren, OS-Lock greift ohnehin.
|
|
}
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
off?.();
|
|
};
|
|
}
|
|
const ew = window.dossierWindow;
|
|
if (ew?.setCloseGuard && ew.onCloseRequested && ew.forceClose) {
|
|
const force = () => ew.forceClose!();
|
|
ew.setCloseGuard(true);
|
|
const un = ew.onCloseRequested(() => {
|
|
if (!handleCloseRequested({ preventDefault: () => {} }, o, force)) force();
|
|
});
|
|
return () => {
|
|
un();
|
|
ew.setCloseGuard!(false);
|
|
};
|
|
}
|
|
if (typeof window.addEventListener !== "function") return () => {};
|
|
const onBeforeUnload = (e: BeforeUnloadEvent) => {
|
|
if (!o.isDirty()) return;
|
|
e.preventDefault();
|
|
e.returnValue = "";
|
|
};
|
|
window.addEventListener("beforeunload", onBeforeUnload);
|
|
return () => window.removeEventListener("beforeunload", onBeforeUnload);
|
|
}
|