Files
web-cad/frontend/src/kernel/document/documentService.ts
T

44 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* documentService Prozessweiter Zugriffspunkt auf das aktive CADDocument.
*
* Warum: CanvasArea besitzt die V2-Dokumentinstanz (Task A4.1), aber App-Level
* UI (HistoryPanel, Tastenkürzel Strg+Z/Y) benötigt Zugriff OHNE prop-
* threading durch die God-Component. Dieser Service ist die bewusste,
* dokumentierte Ausnahme von der Reinheitsregel — nur EINE Instanz aktiv.
*
* Setzen/Entfernen übernimmt CanvasArea im Mount-/Cleanup-Zyklus.
*/
import type { CADDocument } from './CADDocument';
type Listener = () => void;
let activeDoc: CADDocument | null = null;
const listeners = new Set<Listener>();
/** Registriert das aktive Dokument (CanvasArea-Mount). */
export function setActiveDocument(doc: CADDocument | null): void {
activeDoc = doc;
listeners.forEach((l) => l());
}
/** Aktives Dokument oder null (z.B. wenn Flag aus / Canvas unmounted). */
export function getActiveDocument(): CADDocument | null {
return activeDoc;
}
/** Änderungsabonnement (für React-Hooks). Liefert Unsubscribe. */
export function onActiveDocumentChanged(cb: Listener): () => void {
listeners.add(cb);
return () => listeners.delete(cb);
}
/** Bequemlichkeit: globaler Undo am aktiven Dokument. false wenn kein Doc/Stack leer. */
export function undoActive(): boolean {
return activeDoc?.undo() ?? false;
}
/** Bequemlichkeit: globales Redo am aktiven Dokument. */
export function redoActive(): boolean {
return activeDoc?.redo() ?? false;
}