Merge [K4][7][8] Ops, Pruefung und Undo/Redo im Rust-Kern; Fix Redo-Verlust (Ausschnitt-Leiste schrieb automatisch ins Projekt), Neu/Oeffnen leeren den Verlauf, Browser-Oeffnen liest Text

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-28 20:47:50 +00:00
commit df468b987e
35 files changed
+1701 -432

No files matched your search

+107 -3
View File
@@ -1,12 +1,14 @@
// Projekt-Dokument im Rust-Kern ([K4], Stufe S1): spiegelt das TS-Projekt über Ops nach Rust
// (`document.rs`). TS bleibt vorerst Quelle der Änderungen (`setProject` → `diffToOps`); Rust hält
// Projekt-Dokument im Rust-Kern ([K4], Stufe S1; S3: Prüfung und Undo/Redo im Kern, s. `commit`).
// Spiegelt das TS-Projekt über Ops nach Rust (`document.rs`). Befehle liefern vorerst noch
// Projekt → Projekt (`setProject` → `diffToOps`); Rust hält
// den Stand, auf dem die Ableitungen später direkt rechnen, ohne dass TS das Projekt je Aufruf
// flach neu sammeln muss. Bei jedem Widerspruch (abgelehnte Ops, Fehler) wird das Dokument aus dem
// vollen Projekt neu geöffnet — der Spiegel heilt sich selbst.
import type { Project } from "../model/types";
import { core } from "../engine/coreWasm";
import { diffToOps } from "./ops";
import { applyOps, diffToOps, opsToJson } from "./ops";
import type { CoreOp } from "./ops";
/** Abfragen des Kerns (`queries.rs`). */
export type QueryName =
@@ -36,6 +38,8 @@ export type QueryName =
export class ProjectDocument {
private handle = 0;
private mirror: Project;
/** Verlaufsgrenzen (Schritte, Bytes), beim Neuöffnen wieder gesetzt. */
private limits: [number, number] | null = null;
constructor(project: Project) {
this.mirror = project;
@@ -51,6 +55,7 @@ export class ProjectDocument {
if (this.handle) core().doc_close(this.handle);
this.handle = core().doc_open(JSON.stringify(project));
if (!this.handle) console.warn("Projekt-Dokument im Kern konnte nicht geöffnet werden.");
else if (this.limits) core().doc_history_limits(this.handle, this.limits[0], this.limits[1]);
}
/** Bringt das Dokument auf den Stand `next` (über die Differenz als Ops). */
@@ -106,9 +111,107 @@ export class ProjectDocument {
if (this.handle) core().doc_close(this.handle);
this.handle = 0;
}
// ── Änderungen mit Verlauf ([K4] S3, [7], [8]) ──────────────────────────────
// Der Kern prüft, wendet an und zeichnet Undo-Schritte als inverse Ops auf; das TS-Projekt ist
// die Leseansicht und wird mit genau den Ops nachgeführt, die der Kern angenommen bzw. für
// Undo/Redo geliefert hat (`applyOps`, unveränderte Elemente behalten ihre Identität).
/** Stand des Spiegels (die zuletzt übernommene Leseansicht). */
get current(): Project {
return this.mirror;
}
/**
* Stellt sicher, dass das Dokument `base` spiegelt. Wurde das Projekt vorbei an den Ops
* geändert (Laden in Tests, fremder Stand), passt der Verlauf nicht mehr: er wird geleert.
*/
private ensure(base: Project): void {
if (base === this.mirror && this.handle) return;
this.sync(base);
if (!this.handle) this.reopen(base);
if (this.handle) core().doc_history_clear(this.handle);
}
/**
* Benutzer-Änderung: `ops` (auf `base`) im Kern prüfen, anwenden und als Undo-Schritt
* aufzeichnen (`coalesceKey` + `now` in ms: Serien im Zeitfenster = ein Schritt).
* Ergebnis: neues Projekt oder die Meldungen des Kerns (dann bleibt alles unverändert).
*/
commit(base: Project, ops: readonly CoreOp[], coalesceKey: string | undefined, now: number): { project: Project } | { problems: string[] } {
this.ensure(base);
if (!this.handle) return { problems: ["Projekt-Dokument im Kern nicht offen."] };
const res = core().doc_commit(this.handle, opsToJson(ops), coalesceKey ?? "", now);
if (res) {
let problems: string[];
try {
problems = JSON.parse(res) as string[];
} catch {
problems = [res];
}
return { problems };
}
const project = applyOps(base, ops);
this.mirror = project;
return { project };
}
/** Einen Schritt zurück auf `base` (= aktuelles Projekt); `null` = nichts rückgängig zu machen. */
undo(base: Project): Project | null {
return this.jump(base, "doc_undo");
}
/** Einen Schritt vor; `null` = nichts wiederherzustellen. */
redo(base: Project): Project | null {
return this.jump(base, "doc_redo");
}
private jump(base: Project, fn: "doc_undo" | "doc_redo"): Project | null {
if (base !== this.mirror || !this.handle) {
this.ensure(base);
return null;
}
const json = core()[fn](this.handle);
if (!json) return null;
const project = applyOps(base, JSON.parse(json) as CoreOp[]);
this.mirror = project;
return project;
}
/** Verlaufsstand: Undo-/Redo-Tiefe und geschätzter Speicher (Bytes). */
historyState(): { undo: number; redo: number; bytes: number } {
const s = this.handle ? core().doc_history_state(this.handle) : new Float64Array(0);
return { undo: s[0] ?? 0, redo: s[1] ?? 0, bytes: s[2] ?? 0 };
}
clearHistory(): void {
if (this.handle) core().doc_history_clear(this.handle);
}
/** Grenzen des Verlaufs (Schritte, ungefähre Bytes). */
setHistoryLimits(steps: number, bytes: number): void {
this.limits = [steps, bytes];
if (this.handle) core().doc_history_limits(this.handle, steps, bytes);
}
/** Gruppe: alle Änderungen bis {@link endGroup} werden ein Undo-Schritt (verschachtelbar). */
beginGroup(): void {
if (this.handle) core().doc_group_begin(this.handle);
}
endGroup(): void {
if (this.handle) core().doc_group_end(this.handle);
}
}
let shared: ProjectDocument | null = null;
/** Dokument des Stores (mit Verlauf); Abfragen auf dessen aktuellem Projekt laufen direkt darüber. */
let primary: ProjectDocument | null = null;
/** Meldet das Dokument des Stores an (s. {@link documentFor}). */
export function setPrimaryDocument(doc: ProjectDocument | null): void {
primary = doc;
}
/**
* Das gemeinsame Kern-Dokument, auf den Stand von `project` gebracht. Der Abgleich läuft über die
@@ -116,6 +219,7 @@ let shared: ProjectDocument | null = null;
* wechselt ein Aufrufer zwischen sehr verschiedenen Projekten, ist es nur langsamer, nie falsch.
*/
export function documentFor(project: Project): ProjectDocument {
if (primary && primary.current === project && primary.id) return primary;
if (!shared) shared = new ProjectDocument(project);
else shared.sync(project);
return shared;