diff --git a/src/state/escapeClear.test.ts b/src/state/escapeClear.test.ts new file mode 100644 index 00000000..db442011 --- /dev/null +++ b/src/state/escapeClear.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from "vitest"; +import { clearSelectionAfterEscape } from "./useKeyboardShortcuts"; +import { EMPTY_SELECTION, fromIds, mergeSelection } from "./selection"; + +// [204] Esc hebt die Auswahl erst nach dem Ereignis auf; ein Klick unmittelbar nach Esc (E2E: Playwright +// sendet die Ereignisse ohne Pause) darf von diesem späten Esc nicht wieder gelöscht werden. +describe("clearSelectionAfterEscape", () => { + const line1 = mergeSelection(EMPTY_SELECTION, fromIds("drawings", ["a"])); + + it("hebt die Auswahl auf, wenn sie seit Esc unverändert ist", () => { + const calls: unknown[] = []; + expect(clearSelectionAfterEscape(line1, line1, (p) => calls.push(p))).toBe(true); + expect(calls).toHaveLength(1); + }); + + it("lässt eine seit Esc neu gewählte Auswahl stehen", () => { + const line2 = mergeSelection(EMPTY_SELECTION, fromIds("drawings", ["b"])); + const calls: unknown[] = []; + expect(clearSelectionAfterEscape(line1, line2, (p) => calls.push(p))).toBe(false); + expect(calls).toHaveLength(0); + }); + + it("tut bei leerer Auswahl nichts", () => { + const calls: unknown[] = []; + expect(clearSelectionAfterEscape(EMPTY_SELECTION, EMPTY_SELECTION, (p) => calls.push(p))).toBe(false); + expect(calls).toHaveLength(0); + }); +}); diff --git a/src/state/useKeyboardShortcuts.ts b/src/state/useKeyboardShortcuts.ts index 3ae4476c..55cbfec6 100644 --- a/src/state/useKeyboardShortcuts.ts +++ b/src/state/useKeyboardShortcuts.ts @@ -246,6 +246,22 @@ export function useActiveToolKeys( }, [activeTool]); } +/** + * Esc hebt die Auswahl erst nach dem Ereignis (Task) auf (s. u.). Wurde sie dazwischen geändert — z. B. ein + * Klick unmittelbar nach Esc —, gilt die neue Auswahl und nicht der längst vergangene Esc (sonst verschwindet + * die frisch gewählte Auswahl wieder). + */ +export function clearSelectionAfterEscape( + atKey: Selection, + current: Selection, + setSelection: (patch: Partial) => void, +): boolean { + if (current !== atKey) return false; + if (isEmpty(current, { withGroups: false })) return false; + setSelection(clearKinds()); + return true; +} + /** * Tastatur im Auswahl-Werkzeug: Ctrl/Cmd+A = alles auf der aktiven Ebene * wählen; Entf/Backspace = ALLE Selektierten (Wände + 2D-Elemente) löschen. @@ -285,10 +301,10 @@ export function useSelectAllAndDeleteShortcut(opts: { // (Esc gehört dann dem Dialog, die Auswahl dahinter bleibt wie in Archicad). if (e.key === "Escape" && !e.ctrlKey && !e.metaKey && !e.altKey) { if (document.querySelector("[aria-modal='true']")) return; + const atKey = getState().selection; setTimeout(() => { if (e.defaultPrevented) return; - if (isEmpty(opts.selection, { withGroups: false })) return; - setSelection(clearKinds()); + clearSelectionAfterEscape(atKey, getState().selection, setSelection); }); return; }