task(A4.11): documentService singleton + global v2 undo/redo shortcuts

This commit is contained in:
Agent Zero
2026-08-27 07:14:41 +02:00
parent e76b3efe20
commit 25e721d3d3
3 changed files with 123 additions and 0 deletions
@@ -0,0 +1,43 @@
/**
* 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;
}