[84] Undo-Verlauf als Liste: Schritt-Beschriftungen im Kern, Sprung zu Schritt N, Panel "Verlauf"
This commit is contained in:
19 files changed
+668
-24
No files matched your search
@@ -0,0 +1,26 @@
|
||||
// Beschriftungen der Undo-Schritte ([84]). Der Kern speichert je Schritt einen kurzen Text: einen
|
||||
// i18n-Schlüssel (`history.…`, Sprache folgt der Oberfläche), `history.op.<art>:<liste>` (aus den Ops
|
||||
// abgeleitet) oder freien Text; leer = Standard.
|
||||
|
||||
import { t } from "../i18n";
|
||||
import type { CoreOp } from "../core/ops";
|
||||
|
||||
/** Beschriftung aus den Ops (erste Op), wenn der Aufrufer keine setzt: `history.op.<art>:<liste>`. */
|
||||
export function opsLabel(ops: readonly CoreOp[]): string {
|
||||
const op = ops[0];
|
||||
if (!op) return "";
|
||||
return op.kind === "setField" ? `history.op.setField:${op.key}` : `history.op.${op.kind}:${op.collection}`;
|
||||
}
|
||||
|
||||
/** Anzeigetext eines Schritts in der aktuellen Sprache. */
|
||||
export function historyLabelText(label: string): string {
|
||||
if (!label) return t("history.step.generic");
|
||||
if (!label.startsWith("history.")) return label;
|
||||
const [key, arg] = label.split(":", 2);
|
||||
if (arg !== undefined) {
|
||||
const coll = t(`history.coll.${arg}`);
|
||||
return t(key!, { what: coll === `history.coll.${arg}` ? arg : coll });
|
||||
}
|
||||
const text = t(key!);
|
||||
return text === key ? t("history.step.generic") : text;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// Verlaufsliste [84]: Beschriftungen der Schritte und Sprung zu einem Schritt über den Store.
|
||||
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { getState, setState } from "./appStore";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import { withHistoryLabel } from "./historySlice";
|
||||
import { historyLabelText, opsLabel } from "./historyLabels";
|
||||
|
||||
beforeEach(() => {
|
||||
setState({ project: sampleProject, undoDepth: 0, redoDepth: 0, canUndo: false, canRedo: false });
|
||||
getState().clearHistory();
|
||||
});
|
||||
|
||||
const egVisible = () => getState().project.drawingLevels.find((l) => l.id === "eg")?.visible;
|
||||
|
||||
describe("Verlaufsliste", () => {
|
||||
it("Schritte tragen Beschriftungen (explizit, Gruppe)", () => {
|
||||
getState().toggleLevelVisible("eg");
|
||||
withHistoryLabel("Eigenschaft ändern: Dicke", () => getState().toggleLevelVisible("eg"));
|
||||
getState().groupChanges(
|
||||
() => {
|
||||
getState().toggleLevelVisible("eg");
|
||||
getState().toggleLevelVisible("eg");
|
||||
},
|
||||
undefined,
|
||||
"history.import",
|
||||
);
|
||||
const { undo, redo } = getState().historyEntries();
|
||||
expect(undo).toHaveLength(3);
|
||||
expect(undo[0]).toBe("history.op.replace:drawingLevels");
|
||||
expect(undo[1]).toBe("Eigenschaft ändern: Dicke");
|
||||
expect(undo[2]).toBe("history.import");
|
||||
expect(redo).toEqual([]);
|
||||
});
|
||||
|
||||
it("Anzeigetexte: Schlüssel, Ops-Ableitung, freier Text, Standard", () => {
|
||||
expect(opsLabel([])).toBe("");
|
||||
expect(opsLabel([{ kind: "add", collection: "walls", items: [] }])).toBe("history.op.add:walls");
|
||||
expect(historyLabelText("Eigenschaft ändern: Dicke")).toBe("Eigenschaft ändern: Dicke");
|
||||
expect(historyLabelText("")).toBe("Änderung");
|
||||
expect(historyLabelText("history.op.add:walls")).toBe("Wand hinzufügen");
|
||||
expect(historyLabelText("history.op.replace:unbekannt")).toBe("unbekannt ändern");
|
||||
expect(historyLabelText("history.gibtsnicht")).toBe("Änderung");
|
||||
expect(historyLabelText("history.cmd.wall")).toBe("Wand zeichnen");
|
||||
});
|
||||
|
||||
it("jumpHistory springt über mehrere Schritte und zurück", () => {
|
||||
const start = egVisible();
|
||||
for (let i = 0; i < 3; i++) getState().toggleLevelVisible("eg");
|
||||
const after3 = egVisible();
|
||||
expect(after3).toBe(!start);
|
||||
getState().jumpHistory(1);
|
||||
expect(getState().undoDepth).toBe(1);
|
||||
expect(getState().redoDepth).toBe(2);
|
||||
expect(egVisible()).toBe(!start);
|
||||
expect(getState().historyEntries().redo).toHaveLength(2);
|
||||
getState().jumpHistory(0);
|
||||
expect(egVisible()).toBe(start);
|
||||
expect(getState().canUndo).toBe(false);
|
||||
getState().jumpHistory(3);
|
||||
expect(egVisible()).toBe(after3);
|
||||
expect(getState().canRedo).toBe(false);
|
||||
getState().jumpHistory(99);
|
||||
expect(getState().undoDepth).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -38,7 +38,36 @@ export interface HistorySlice {
|
||||
* Alle Projekt-Änderungen in `fn` werden EIN Undo-Schritt. Mit `coalesceKey` schliessen
|
||||
* aufeinanderfolgende Gruppen mit gleichem Schlüssel (im Zeitfenster) an denselben Schritt an.
|
||||
*/
|
||||
groupChanges: (fn: () => void, coalesceKey?: string) => void;
|
||||
groupChanges: (fn: () => void, coalesceKey?: string, label?: string) => void;
|
||||
/** Beschriftungen der Schritte (Undo: ältester zuerst; Redo: nächster zuerst), für die Verlaufsliste [84]. */
|
||||
historyEntries: () => { undo: string[]; redo: string[] };
|
||||
/** Springt auf Undo-Tiefe `target` (0 = Anfang), mehrere Schritte in einem Kern-Aufruf. */
|
||||
jumpHistory: (target: number) => void;
|
||||
}
|
||||
|
||||
let activeLabel = "";
|
||||
|
||||
/** Alle Änderungen in `fn` ohne eigene Beschriftung bekommen `label` (i18n-Schlüssel oder Text) als Schritt-Name. */
|
||||
export function withHistoryLabel<T>(label: string, fn: () => T): T {
|
||||
const prev = activeLabel;
|
||||
activeLabel = label;
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
activeLabel = prev;
|
||||
}
|
||||
}
|
||||
|
||||
/** Führt einen Commit mit Beschriftung aus: die des umgebenden {@link withHistoryLabel}, sonst `fallback`. */
|
||||
export function labelCommit<T>(d: ProjectDocument, fallback: string, run: () => T): T {
|
||||
const label = activeLabel || fallback;
|
||||
if (!label) return run();
|
||||
d.setHistoryLabel(label);
|
||||
try {
|
||||
return run();
|
||||
} finally {
|
||||
d.setHistoryLabel("");
|
||||
}
|
||||
}
|
||||
|
||||
let doc: ProjectDocument | null = null;
|
||||
@@ -85,15 +114,23 @@ export function createHistorySlice(api: StoreApi<HistorySlice & { project: Proje
|
||||
d.clearHistory();
|
||||
set(historyPatch(d));
|
||||
},
|
||||
groupChanges: (fn, coalesceKey) => {
|
||||
groupChanges: (fn, coalesceKey, label) => {
|
||||
const d = storeDocument(get().project);
|
||||
if (coalesceKey) d.beginKeyedGroup(coalesceKey, Date.now());
|
||||
else d.beginGroup();
|
||||
try {
|
||||
fn();
|
||||
if (label) withHistoryLabel(label, fn);
|
||||
else fn();
|
||||
} finally {
|
||||
d.endGroup();
|
||||
}
|
||||
},
|
||||
historyEntries: () => storeDocument(get().project).historyLabels(),
|
||||
jumpHistory: (target) => {
|
||||
const { project } = get();
|
||||
const d = storeDocument(project);
|
||||
const next = d.jumpTo(project, target);
|
||||
if (next) set({ project: next, ...historyPatch(d) });
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -66,8 +66,9 @@ import { DEFAULT_SCALE_DENOMINATOR } from "../model/dimension";
|
||||
import { DEFAULT_PRESETS, type TextStylePreset } from "../text/richText";
|
||||
import { t } from "../i18n";
|
||||
import type { StoreApi } from "./store";
|
||||
import { createHistorySlice, historyPatch, storeDocument } from "./historySlice";
|
||||
import { createHistorySlice, historyPatch, labelCommit, storeDocument } from "./historySlice";
|
||||
import type { HistorySlice } from "./historySlice";
|
||||
import { opsLabel } from "./historyLabels";
|
||||
import type { NotifyKind } from "./notifySlice";
|
||||
|
||||
/** Felder, die diese Slice in den RootState beisteuert. */
|
||||
@@ -97,6 +98,7 @@ export interface ProjectSlice {
|
||||
*/
|
||||
commitImport: <R extends object>(
|
||||
run: (doc: ProjectDocument, base: Project, now: number) => ({ project: Project; ops: CoreOp[] } & R) | { problems: string[] },
|
||||
label?: string,
|
||||
) => ({ ops: CoreOp[] } & R) | null;
|
||||
/**
|
||||
* Ein anderes Projekt übernehmen (Neu, Musterhaus; [K4] S4): lädt es als Ganzes ins Kern-Dokument
|
||||
@@ -485,7 +487,7 @@ export function createProjectSlice(
|
||||
): Partial<ProjectSlice & HistorySlice> => {
|
||||
if (ops.length === 0) return {};
|
||||
const doc = storeDocument(s.project);
|
||||
const res = doc.commit(s.project, ops, coalesceKey, Date.now());
|
||||
const res = labelCommit(doc, opsLabel(ops), () => doc.commit(s.project, ops, coalesceKey, Date.now()));
|
||||
if ("problems" in res) {
|
||||
const problems = res.problems;
|
||||
console.warn("Projektänderung verworfen:", problems);
|
||||
@@ -509,7 +511,10 @@ export function createProjectSlice(
|
||||
const applyCmd = (req: object, coalesceKey?: string) =>
|
||||
set((s) => {
|
||||
const doc = storeDocument(s.project);
|
||||
const res = doc.commitCmd(s.project, req, coalesceKey, Date.now());
|
||||
const cmd = (req as { cmd?: unknown }).cmd;
|
||||
const res = labelCommit(doc, typeof cmd === "string" ? `history.cmd.${cmd}` : "", () =>
|
||||
doc.commitCmd(s.project, req, coalesceKey, Date.now()),
|
||||
);
|
||||
if ("problems" in res) {
|
||||
const problems = res.problems;
|
||||
console.warn("Projektänderung verworfen:", problems);
|
||||
@@ -534,7 +539,7 @@ export function createProjectSlice(
|
||||
let notice: string | undefined;
|
||||
set((s) => {
|
||||
const doc = storeDocument(s.project);
|
||||
const res = doc.commitGrip(s.project, req, coalesceKey, Date.now());
|
||||
const res = labelCommit(doc, "history.grip", () => doc.commitGrip(s.project, req, coalesceKey, Date.now()));
|
||||
if ("problems" in res) {
|
||||
const problems = res.problems;
|
||||
console.warn("Projektänderung verworfen:", problems);
|
||||
@@ -563,11 +568,11 @@ export function createProjectSlice(
|
||||
applyChange,
|
||||
recentOps: () => opLog.slice(),
|
||||
|
||||
commitImport: (run) => {
|
||||
commitImport: (run, label) => {
|
||||
let out: ReturnType<typeof run> | null = null;
|
||||
set((s) => {
|
||||
const doc = storeDocument(s.project);
|
||||
const res = run(doc, s.project, Date.now());
|
||||
const res = labelCommit(doc, label ?? "history.import", () => run(doc, s.project, Date.now()));
|
||||
out = res;
|
||||
if ("problems" in res) {
|
||||
const problems = res.problems;
|
||||
|
||||
@@ -599,6 +599,7 @@ export function useProjectFileActions(
|
||||
},
|
||||
now,
|
||||
),
|
||||
"history.import.drawing",
|
||||
);
|
||||
} catch (err) {
|
||||
// Kaputter Puffer o. Ä. (Meldung des Kerns).
|
||||
|
||||
Reference in new issue
Block a user