[99] Befehle: Esc = Schritt zurück, Esc-Esc = Abbruch, Enter = abschliessen, unabhängig vom Fokus
Ein Tasten-Verteiler (state/commandKeys.ts) statt nur über das fokussierte Befehlsfeld; Schritt-Verlauf in der Engine (escape/back, ESC_DOUBLE_MS); Transformation mit gleichem Esc/Enter. Tests: Engine je Befehl, Verteiler, e2e für jeden registrierten Befehl in drei Fokus-Lagen.
This commit is contained in:
10 files changed
+554
-9
No files matched your search
@@ -0,0 +1,118 @@
|
||||
// [99] Esc/Enter in JEDEM Befehl gleich: Esc = ein Schritt zurück (ohne Schritt: Befehl endet),
|
||||
// Esc-Esc schnell (≤ ESC_DOUBLE_MS) = ganzer Befehl abbrechen (nichts aus dem offenen Teil
|
||||
// committet), Enter = abschliessen. Geprüft an der Engine mit einem Test-Host.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { COMMANDS } from "./registry";
|
||||
import { CommandEngine, ESC_DOUBLE_MS, type EngineHost } from "./engine";
|
||||
import type { CommandContext } from "./types";
|
||||
import type { ToolDraft } from "../tools/types";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
|
||||
const level = sampleProject.drawingLevels.find((l) => l.kind === "floor")!;
|
||||
const ctx: CommandContext = {
|
||||
project: sampleProject,
|
||||
level,
|
||||
defaultCategoryCode: sampleProject.layers[0]?.code ?? "20",
|
||||
activeLineStyleId: "",
|
||||
activeWallTypeId: sampleProject.wallTypes[0]?.id ?? "",
|
||||
lastPoint: null,
|
||||
selection: { wallIds: [], drawingId: null, drawingIds: [] } as unknown as CommandContext["selection"],
|
||||
};
|
||||
|
||||
function setup() {
|
||||
const log = { commits: 0, draft: null as ToolDraft | null };
|
||||
const host: EngineHost = {
|
||||
context: (lastPoint) => ({ ...ctx, lastPoint }),
|
||||
snap: () => null,
|
||||
pxPerMeter: () => 50,
|
||||
setDraft: (d) => (log.draft = d),
|
||||
commit: () => void log.commits++,
|
||||
isFloor: () => true,
|
||||
};
|
||||
return { engine: new CommandEngine(host), log };
|
||||
}
|
||||
|
||||
const P = [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 5, y: 0 },
|
||||
{ x: 5, y: 4 },
|
||||
{ x: 1, y: 6 },
|
||||
];
|
||||
|
||||
describe("Esc/Enter der Befehls-Engine", () => {
|
||||
it("Wand: Esc nimmt den letzten Punkt zurück, Enter übernimmt den Rest", () => {
|
||||
const { engine, log } = setup();
|
||||
engine.start("wall");
|
||||
for (const p of P.slice(0, 3)) engine.pick(p);
|
||||
expect(engine.stepDepth()).toBe(3);
|
||||
engine.escape(1000);
|
||||
expect(engine.isActive()).toBe(true);
|
||||
expect(engine.stepDepth()).toBe(2);
|
||||
expect(log.draft?.vertices.length).toBe(2);
|
||||
// Pause > ESC_DOUBLE_MS: zweites Esc ist wieder EIN Schritt zurück, kein Abbruch.
|
||||
engine.escape(1000 + ESC_DOUBLE_MS + 50);
|
||||
expect(engine.isActive()).toBe(true);
|
||||
expect(engine.stepDepth()).toBe(1);
|
||||
engine.pick(P[3]);
|
||||
engine.confirm();
|
||||
expect(engine.isActive()).toBe(false);
|
||||
expect(log.commits).toBe(1);
|
||||
});
|
||||
|
||||
it("Esc ohne Schritt beendet den Befehl", () => {
|
||||
const { engine } = setup();
|
||||
engine.start("polyline");
|
||||
engine.escape(0);
|
||||
expect(engine.isActive()).toBe(false);
|
||||
});
|
||||
|
||||
it("Esc-Esc schnell bricht den ganzen Befehl ab, nichts wird committet", () => {
|
||||
const { engine, log } = setup();
|
||||
engine.start("polyline");
|
||||
for (const p of P) engine.pick(p);
|
||||
engine.escape(5000);
|
||||
engine.escape(5000 + ESC_DOUBLE_MS - 50);
|
||||
expect(engine.isActive()).toBe(false);
|
||||
expect(log.commits).toBe(0);
|
||||
expect(log.draft).toBe(null);
|
||||
});
|
||||
|
||||
it("Esc löst zuerst einen getippten Feldwert (Lock), dann erst Punkte", () => {
|
||||
const { engine } = setup();
|
||||
engine.start("wall");
|
||||
engine.pick(P[0]);
|
||||
if (!engine.hasFields()) return;
|
||||
engine.submitText("3");
|
||||
expect(engine.view().fields.some((f) => f.locked)).toBe(true);
|
||||
engine.escape(0);
|
||||
expect(engine.view().fields.some((f) => f.locked)).toBe(false);
|
||||
expect(engine.stepDepth()).toBe(1);
|
||||
});
|
||||
|
||||
// Jeder registrierte interaktive Befehl: Punkte setzen, Esc = genau ein Schritt zurück
|
||||
// (bzw. Ende ohne Schritt), Esc-Esc = aus, ohne dass Esc etwas committet.
|
||||
for (const name of Object.keys(COMMANDS)) {
|
||||
const cmd = COMMANDS[name];
|
||||
if (cmd.autoRun) continue;
|
||||
it(`${name}: Esc einen Schritt zurück, Esc-Esc bricht ab`, () => {
|
||||
const { engine, log } = setup();
|
||||
if (!engine.start(name)) return;
|
||||
for (const p of P.slice(0, 2)) {
|
||||
engine.move(p);
|
||||
engine.pick(p);
|
||||
}
|
||||
if (!engine.isActive()) return; // Befehl mit zwei Punkten fertig
|
||||
const depth = engine.stepDepth();
|
||||
const commits = log.commits;
|
||||
engine.escape(10_000);
|
||||
if (depth === 0) expect(engine.isActive()).toBe(false);
|
||||
else {
|
||||
expect(engine.isActive()).toBe(true);
|
||||
expect(engine.stepDepth()).toBe(depth - 1);
|
||||
}
|
||||
engine.escape(10_000 + 100);
|
||||
expect(engine.isActive()).toBe(false);
|
||||
expect(log.commits).toBe(commits);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -85,6 +85,25 @@ export interface EngineView {
|
||||
locked: boolean;
|
||||
active: boolean;
|
||||
}[];
|
||||
/** Schritte, die Esc zurücknehmen kann ([99]; für Anzeige/E2E). */
|
||||
steps?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwei Esc innerhalb dieser Zeit (ms) = ganzen Befehl abbrechen; langsamer = je ein Schritt
|
||||
* zurück ([99], Vorgabe Maintainer: Esc-Verhalten in jedem Befehl gleich).
|
||||
*/
|
||||
export const ESC_DOUBLE_MS = 300;
|
||||
|
||||
/** Zwei Befehls-Zustände gleich (ohne Maus-`cursor`)? Dann war die Eingabe kein Schritt. */
|
||||
function sameStep(a: CommandState, b: CommandState): boolean {
|
||||
if (a === b) return true;
|
||||
try {
|
||||
const strip = (k: string, v: unknown) => (k === "cursor" || k === "hover" ? undefined : v);
|
||||
return JSON.stringify(a, strip) === JSON.stringify(b, strip);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class CommandEngine {
|
||||
@@ -101,6 +120,13 @@ export class CommandEngine {
|
||||
/** Identität des Schritts, für den der Feld-Zustand gilt (Reset bei Wechsel). */
|
||||
private fieldPhaseKey: string | null = null;
|
||||
private listeners = new Set<() => void>();
|
||||
/**
|
||||
* Schritt-Verlauf des laufenden Befehls ([99]): Zustand VOR jeder Eingabe, die den Schritt
|
||||
* änderte. Esc holt den letzten zurück. Ein Commit ist eine Grenze (Committetes bleibt).
|
||||
*/
|
||||
private steps: CommandState[] = [];
|
||||
/** Zeitpunkt des letzten Esc (für Esc-Esc = Abbruch). */
|
||||
private lastEscAt = -Infinity;
|
||||
/** Nutzer-Kürzel-Übersteuerungen (s. `state/commandAliasOverrides.ts`); leer bis gesetzt. */
|
||||
private aliasOverrides: Record<string, string> = {};
|
||||
|
||||
@@ -161,6 +187,7 @@ export class CommandEngine {
|
||||
options: this.command.options(this.state),
|
||||
commandName: this.command.name,
|
||||
fields,
|
||||
steps: this.steps.length,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -278,6 +305,7 @@ export class CommandEngine {
|
||||
this.state = cmd.init();
|
||||
this.lastCommandName = cmd.name;
|
||||
this.lastCursor = null;
|
||||
this.steps = [];
|
||||
this.resetFields();
|
||||
this.host.setDraft(null);
|
||||
// Sofort-Befehl (Join/Split u. Ä.): direkt ausführen, ohne auf einen Pick zu
|
||||
@@ -306,11 +334,69 @@ export class CommandEngine {
|
||||
}
|
||||
this.command = null;
|
||||
this.state = null;
|
||||
this.steps = [];
|
||||
this.resetFields();
|
||||
this.host.setDraft(null);
|
||||
this.emit();
|
||||
}
|
||||
|
||||
/** Anzahl Schritte, die Esc zurücknehmen kann (Test/Anzeige). */
|
||||
stepDepth(): number {
|
||||
return this.command ? this.steps.length : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Esc ([99]): einmal = ein Schritt zurück (`back`), zweimal innerhalb `ESC_DOUBLE_MS` =
|
||||
* ganzen Befehl abbrechen. `now` nur für Tests; sonst die Uhr.
|
||||
*/
|
||||
escape(now: number = Date.now()): void {
|
||||
if (!this.command) return;
|
||||
const quick = now - this.lastEscAt <= ESC_DOUBLE_MS;
|
||||
this.lastEscAt = now;
|
||||
if (quick) this.cancel();
|
||||
else this.back();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ein Schritt zurück: zuerst ein getippter Feldwert (Lock), sonst der letzte gesetzte Punkt/
|
||||
* gewählte Schritt. Ohne Schritt endet der Befehl (wie Abbruch).
|
||||
*/
|
||||
back(): void {
|
||||
if (!this.command || !this.state) return;
|
||||
if (Object.keys(this.locks).length > 0) {
|
||||
this.locks = {};
|
||||
this.activeFieldIndex = 0;
|
||||
this.redraw();
|
||||
this.emit();
|
||||
return;
|
||||
}
|
||||
const prev = this.steps.pop();
|
||||
if (!prev) {
|
||||
this.cancel();
|
||||
return;
|
||||
}
|
||||
this.state = prev;
|
||||
this.resetFields();
|
||||
this.redraw();
|
||||
this.emit();
|
||||
}
|
||||
|
||||
/** Vorschau des aktuellen Zustands neu zeichnen (nach einem Schritt zurück). */
|
||||
private redraw(): void {
|
||||
if (!this.command || !this.state) return;
|
||||
if (!this.lastCursor) {
|
||||
this.host.setDraft(null);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const [st, res] = this.command.onMove(this.state, this.lastCursor, null, this.context());
|
||||
this.state = st;
|
||||
this.host.setDraft(res.draft ?? null);
|
||||
} catch {
|
||||
this.host.setDraft(null);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Eingabe-Routing ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
@@ -464,8 +550,11 @@ export class CommandEngine {
|
||||
}
|
||||
}
|
||||
const ctx = this.context();
|
||||
const before = this.state;
|
||||
const [st, res] = this.command.onConfirm(this.state, ctx);
|
||||
this.state = st;
|
||||
if (res.commit) this.steps = [];
|
||||
else if (!res.done && !sameStep(before, st)) this.steps.push(before);
|
||||
this.applyResult(res);
|
||||
}
|
||||
|
||||
@@ -483,8 +572,13 @@ export class CommandEngine {
|
||||
if (!this.command || !this.state) return;
|
||||
if (input.kind === "point") this.lastCursor = input.point;
|
||||
const ctx = this.context();
|
||||
const before = this.state;
|
||||
const [st, res] = this.command.onInput(this.state, input, ctx);
|
||||
this.state = st;
|
||||
// Schritt-Verlauf: die befehlseigene Option „Zurück" nimmt selbst einen Schritt zurück.
|
||||
if (input.kind === "option" && input.id === "undo") this.steps.pop();
|
||||
else if (!res.commit && !res.done && !sameStep(before, st)) this.steps.push(before);
|
||||
if (res.commit) this.steps = [];
|
||||
this.applyResult(res);
|
||||
}
|
||||
|
||||
@@ -496,6 +590,7 @@ export class CommandEngine {
|
||||
if (res.done) {
|
||||
this.command = null;
|
||||
this.state = null;
|
||||
this.steps = [];
|
||||
this.resetFields();
|
||||
}
|
||||
this.emit();
|
||||
|
||||
@@ -194,7 +194,10 @@ export interface Command {
|
||||
): [CommandState, CommandResult];
|
||||
/** Enter/Space/Rechtsklick: Schritt bestätigen / mehrteilig beenden. */
|
||||
onConfirm(state: CommandState, ctx: CommandContext): [CommandState, CommandResult];
|
||||
/** Esc: Entwurf verwerfen, Befehl beenden. */
|
||||
/**
|
||||
* Abbruch (Esc-Esc bzw. Esc ohne Schritt): Entwurf verwerfen, Befehl beenden, nichts committen.
|
||||
* Den Schritt zurück bei einfachem Esc führt die Engine selbst (Schritt-Verlauf, [99]).
|
||||
*/
|
||||
onCancel(state: CommandState): [CommandState, CommandResult];
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user