Files
DOSSIER/src/state/keyboardShortcuts.ts

446 lines
17 KiB
TypeScript

// Registry + Nutzer-Übersteuerungen für die GLOBALEN Tastatur-Shortcuts
// (Undo/Redo, Alles-wählen, Löschen, Split/Join, Kopieren/Einfügen, Boolean-
// Ops, Bewegen/Spiegeln/Drehen/Messen, Gruppieren, Werkzeug-Zifferntasten).
// Nutzer-Wunsch: „Einstellungen vollständiger machen, damit man eigene
// Shortcuts setzen kann" — bisher waren alle Kombinationen hart in
// `useKeyboardShortcuts.ts` verdrahtet (literale `e.key`/`e.ctrlKey`-Checks,
// keine Indirektion). ACHTUNG Abgrenzung zu `commandAliasOverrides.ts`: DAS
// dort sind Tipp-Kürzel für die Befehlszeile („w" → wall); DIESES Modul sind
// echte `keydown`-Tastenkombinationen (Ctrl/Cmd+Z usw.).
//
// Bewusst NICHT rebindbar (bleiben hart codiert, kollidieren sonst mit
// Kontext-Verhalten, das gar nicht über dieses Registry läuft): Tab/Enter/
// Escape/Backspace als Werkzeug-Steuerung (Feld-Zyklus, Gesten-Abschluss,
// Punkt zurücknehmen, Entwurf verwerfen) sowie die Ctrl/Cmd+Y-Redo- und
// Backspace-Löschen-ZUSATZ-Bindungen (bleiben als fixe Zweit-Belegung neben
// der hier konfigurierbaren Haupt-Kombination bestehen, s. `useUndoRedoShortcut`/
// `useSelectAllAndDeleteShortcut`).
//
// Reine Logik (Parsen/Matchen/Formatieren/Persistenz); die React-Anbindung
// (Laden beim Start, Weiterreichen an die Hooks, Cross-Fenster-`storage`-
// Sync) bleibt in App.tsx — exaktes Muster wie `commandAliasOverrides.ts`.
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
import { getLanguage } from "../i18n/translate";
import { isMacPlatform, isPrimaryMod } from "./platform";
export type ShortcutId =
| "undo"
| "redo"
| "selectAll"
| "deleteSelection"
| "split"
| "join"
| "copyElements"
| "pasteElements"
| "booleanUnion"
| "booleanDifference"
| "booleanIntersection"
| "transformMove"
| "transformMirror"
| "transformRotate"
| "measure"
| "group"
| "ungroup"
| "toolText"
| "toolLine"
| "toolCircle"
| "toolRect"
| "toolPolyline"
| "toolWall"
| "toolCeiling"
| "toolWindow"
| "toolDoor"
| "toolRoom"
| "floorDown"
| "floorUp"
| "showSelection3d"
| "hideSelection3d"
| "objectSettings"
| "duplicate"
| "toggleLock"
| "hideSelectionLayers"
| "showAllLayers"
| "fitAll"
| "fitSelection"
| "isolateLayer"
| "toggleTrace"
| "walkMode";
export type ShortcutCategory =
| "general"
| "selection"
| "edit"
| "boolean"
| "transform"
| "group"
| "tools"
| "navigation";
export interface ShortcutDef {
id: ShortcutId;
category: ShortcutCategory;
/** i18n-Key für die Aktions-Beschriftung, s. `shortcut.<id>` in de.ts/en.ts. */
labelKey: string;
defaultCombo: string;
/** Bare-Key-Shortcuts (Werkzeug-/Transform-Kürzel): die Remap-UI verweigert
* hier eine Kombination MIT Modifikator (Ctrl/Shift/Alt bleiben frei fürs
* Tippen/OS-Kürzel — dieselbe Einschränkung, die die Hooks vorher schon
* hart hatten). */
bareKeyOnly?: boolean;
}
export const SHORTCUT_DEFS: ShortcutDef[] = [
{ id: "undo", category: "general", labelKey: "shortcut.undo", defaultCombo: "mod+z" },
{ id: "redo", category: "general", labelKey: "shortcut.redo", defaultCombo: "mod+shift+z" },
{ id: "selectAll", category: "selection", labelKey: "shortcut.selectAll", defaultCombo: "mod+a" },
{
id: "deleteSelection",
category: "selection",
labelKey: "shortcut.deleteSelection",
defaultCombo: "Delete",
},
{ id: "split", category: "edit", labelKey: "shortcut.split", defaultCombo: "mod+shift+j" },
{ id: "join", category: "edit", labelKey: "shortcut.join", defaultCombo: "mod+j" },
{ id: "copyElements", category: "edit", labelKey: "shortcut.copyElements", defaultCombo: "mod+c" },
{ id: "pasteElements", category: "edit", labelKey: "shortcut.pasteElements", defaultCombo: "mod+v" },
{
id: "booleanUnion",
category: "boolean",
labelKey: "shortcut.booleanUnion",
defaultCombo: "mod+shift+u",
},
{
id: "booleanDifference",
category: "boolean",
labelKey: "shortcut.booleanDifference",
defaultCombo: "mod+shift+d",
},
{
id: "booleanIntersection",
category: "boolean",
labelKey: "shortcut.booleanIntersection",
defaultCombo: "mod+shift+i",
},
{
id: "transformMove",
category: "transform",
labelKey: "shortcut.transformMove",
defaultCombo: "m",
bareKeyOnly: true,
},
{
id: "transformMirror",
category: "transform",
labelKey: "shortcut.transformMirror",
defaultCombo: "s",
bareKeyOnly: true,
},
{
id: "transformRotate",
category: "transform",
labelKey: "shortcut.transformRotate",
defaultCombo: "d",
bareKeyOnly: true,
},
{
id: "measure",
category: "transform",
labelKey: "shortcut.measure",
defaultCombo: "q",
bareKeyOnly: true,
},
{ id: "group", category: "group", labelKey: "shortcut.group", defaultCombo: "mod+g" },
{ id: "ungroup", category: "group", labelKey: "shortcut.ungroup", defaultCombo: "mod+shift+g" },
{
id: "toolText",
category: "tools",
labelKey: "shortcut.toolText",
defaultCombo: "1",
bareKeyOnly: true,
},
{
id: "toolLine",
category: "tools",
labelKey: "shortcut.toolLine",
defaultCombo: "2",
bareKeyOnly: true,
},
{
id: "toolCircle",
category: "tools",
labelKey: "shortcut.toolCircle",
defaultCombo: "3",
bareKeyOnly: true,
},
{
id: "toolRect",
category: "tools",
labelKey: "shortcut.toolRect",
defaultCombo: "4",
bareKeyOnly: true,
},
{
id: "toolPolyline",
category: "tools",
labelKey: "shortcut.toolPolyline",
defaultCombo: "5",
bareKeyOnly: true,
},
{
id: "toolWall",
category: "tools",
labelKey: "shortcut.toolWall",
defaultCombo: "6",
bareKeyOnly: true,
},
{
id: "toolCeiling",
category: "tools",
labelKey: "shortcut.toolCeiling",
defaultCombo: "7",
bareKeyOnly: true,
},
{
id: "toolWindow",
category: "tools",
labelKey: "shortcut.toolWindow",
defaultCombo: "8",
bareKeyOnly: true,
},
{
id: "toolDoor",
category: "tools",
labelKey: "shortcut.toolDoor",
defaultCombo: "9",
bareKeyOnly: true,
},
{
id: "toolRoom",
category: "tools",
labelKey: "shortcut.toolRoom",
defaultCombo: "0",
bareKeyOnly: true,
},
// [185] Navigation wie Archicad.
{ id: "floorDown", category: "navigation", labelKey: "shortcut.floorDown", defaultCombo: "mod+5" },
{ id: "floorUp", category: "navigation", labelKey: "shortcut.floorUp", defaultCombo: "mod+6" },
{ id: "showSelection3d", category: "navigation", labelKey: "shortcut.showSelection3d", defaultCombo: "mod+3" },
{ id: "hideSelection3d", category: "navigation", labelKey: "shortcut.hideSelection3d", defaultCombo: "mod+4" },
// [189] Einstellungsdialog des gewählten Objekts (Archicad Cmd+T), hier schlicht „t".
{ id: "objectSettings", category: "edit", labelKey: "shortcut.objectSettings", defaultCombo: "t", bareKeyOnly: true },
// [190] Weitere Archicad-Kürzel (Esc = Auswahl leeren / Befehl abbrechen ist fest verdrahtet, s. oben).
{ id: "duplicate", category: "edit", labelKey: "shortcut.duplicate", defaultCombo: "mod+d" },
{ id: "toggleLock", category: "edit", labelKey: "shortcut.toggleLock", defaultCombo: "mod+l" },
{ id: "hideSelectionLayers", category: "navigation", labelKey: "shortcut.hideSelectionLayers", defaultCombo: "mod+shift+h" },
{ id: "showAllLayers", category: "navigation", labelKey: "shortcut.showAllLayers", defaultCombo: "mod+shift+a" },
{ id: "fitAll", category: "navigation", labelKey: "shortcut.fitAll", defaultCombo: "mod+0" },
{ id: "fitSelection", category: "navigation", labelKey: "shortcut.fitSelection", defaultCombo: "mod+shift+f" },
// [192] Ebenen schnell schalten (Cmd/Ctrl+Shift+I ist der Boolean-Schnitt, daher L = Layer).
{ id: "isolateLayer", category: "navigation", labelKey: "shortcut.isolateLayer", defaultCombo: "mod+shift+l" },
// [205] Pausfolie ein/aus (Archicad „Referenz ein/aus"); T wie Trace, frei neben den übrigen Cmd+Shift-Kürzeln.
{ id: "toggleTrace", category: "navigation", labelKey: "shortcut.toggleTrace", defaultCombo: "mod+shift+t" },
// [213] Begehen im 3D (WASD): Umschalt+F schaltet den Begehen-Modus; die Bewegungstasten selbst (W/A/S/D, Q/E, Pfeile) gelten nur
// darin, damit sie nicht mit „s" (Spiegeln), „d" (Drehen) und „q" (Messen) kollidieren (`viewport/walk.ts`).
{ id: "walkMode", category: "navigation", labelKey: "shortcut.walkMode", defaultCombo: "shift+f" },
];
const DEFS_BY_ID = new Map<ShortcutId, ShortcutDef>(SHORTCUT_DEFS.map((d) => [d.id, d]));
export function shortcutDef(id: ShortcutId): ShortcutDef {
const def = DEFS_BY_ID.get(id);
if (!def) throw new Error(`Unbekannte ShortcutId: ${id}`);
return def;
}
export type ShortcutOverrides = Partial<Record<ShortcutId, string>>;
const STORAGE_KEY = "dossier.keyboardShortcutOverrides";
const KNOWN_IDS = new Set<string>(SHORTCUT_DEFS.map((d) => d.id));
/** Liest die gespeicherten Tastenkombinations-Übersteuerungen; `{}` ohne gespeicherten Wert oder bei kaputten Daten. */
export function loadShortcutOverrides(): ShortcutOverrides {
try {
const raw = window.localStorage.getItem(STORAGE_KEY);
if (!raw) return {};
const parsed: unknown = JSON.parse(raw);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
const out: ShortcutOverrides = {};
for (const [k, v] of Object.entries(parsed as Record<string, unknown>)) {
if (KNOWN_IDS.has(k) && typeof v === "string") out[k as ShortcutId] = v;
}
return out;
} catch {
return {};
}
}
/** Speichert die Tastenkombinations-Übersteuerungen (bestes Bemühen). */
export function saveShortcutOverrides(overrides: ShortcutOverrides): void {
try {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(overrides));
} catch {
// Ignorieren (Quota/privates Fenster) — gleiche Bestes-Bemühen-Haltung wie
// die übrigen localStorage-Einstellungen in diesem Projekt.
}
}
/** Aktuell wirksame Kombination (Übersteuerung falls vorhanden, sonst Default). */
export function resolveCombo(id: ShortcutId, overrides: ShortcutOverrides): string {
return overrides[id] ?? shortcutDef(id).defaultCombo;
}
const MODIFIER_KEYS = new Set(["Control", "Meta", "Shift", "Alt"]);
/**
* Kombination aus einem `keydown`-Event für die Aufnahme-UI („nächste
* Tastenkombination drücken"). `null`, solange nur ein reiner Modifikator
* gedrückt ist (kein Ziel-Ereignis) — die UI wartet dann weiter.
*/
export function comboFromEvent(e: KeyboardEvent): string | null {
if (MODIFIER_KEYS.has(e.key)) return null;
const parts: string[] = [];
if (isPrimaryMod(e)) parts.push("mod");
if (e.shiftKey) parts.push("shift");
if (e.altKey) parts.push("alt");
const key = e.key.length === 1 ? e.key.toLowerCase() : e.key;
parts.push(key);
return parts.join("+");
}
/** Prüft ein `keydown`-Event gegen eine Kombinations-Zeichenkette (exakte Modifikator-Übereinstimmung). */
export function comboMatches(e: KeyboardEvent, combo: string): boolean {
const parts = combo.split("+");
const key = parts[parts.length - 1];
const mods = parts.slice(0, -1);
const wantMod = mods.includes("mod");
const wantShift = mods.includes("shift");
const wantAlt = mods.includes("alt");
if (isPrimaryMod(e) !== wantMod) return false;
if (e.shiftKey !== wantShift) return false;
if (e.altKey !== wantAlt) return false;
const k = e.key.length === 1 ? e.key.toLowerCase() : e.key;
return k === key;
}
/** [170] Datei-Kürzel ausserhalb von Tauri. */
export type FileShortcut = "save" | "saveAs" | "open" | "new" | "print" | "quit" | "zoomIn" | "zoomOut";
/**
* [170] Strg/Cmd+S, Strg/Cmd+Umschalt+S, Strg/Cmd+O ([289]: auch N, P, Q, Zoom +/-) im Browser und in der Linux-App (Electron, ohne native Menüleiste).
* Unter Tauri liefert das native Menü dieselben Kürzel (`native/appMenu.ts`, Accelerator) — dort nicht verwenden.
* Fest wie die Menü-Accelerators, nicht umbelegbar (nicht in {@link SHORTCUT_DEFS}).
*/
export function fileShortcutFor(e: KeyboardEvent): FileShortcut | null {
if (comboMatches(e, "mod+shift+s")) return "saveAs";
if (comboMatches(e, "mod+s")) return "save";
if (comboMatches(e, "mod+o")) return "open";
// [289] Dieselben Kürzel wie im nativen Mac-Menü (CmdOrCtrl+N/Q) plus Drucken und Zoom, die es dort nicht gibt.
if (comboMatches(e, "mod+n")) return "new";
if (comboMatches(e, "mod+p")) return "print";
if (comboMatches(e, "mod+q")) return "quit";
// Zoom: „+" liegt auf deutscher Tastatur auf „+", auf US-Tastatur auf „=" (Shift) — beide gelten; NumPad-„+/-" ebenso.
if (isPrimaryMod(e) && !e.altKey && (e.key === "+" || e.key === "=")) return "zoomIn";
if (isPrimaryMod(e) && !e.altKey && (e.key === "-" || e.key === "_")) return "zoomOut";
return null;
}
/** Kurzform: matcht `e` gegen die (übersteuerte oder Default-)Kombination von `id`. */
export function matchesShortcut(
e: KeyboardEvent,
id: ShortcutId,
overrides: ShortcutOverrides,
): boolean {
return comboMatches(e, resolveCombo(id, overrides));
}
/** Ob `combo` bereits einer ANDEREN Aktion zugewiesen ist — für die Remap-UI (Kollisions-Warnung). */
export function findConflict(
combo: string,
excludeId: ShortcutId,
overrides: ShortcutOverrides,
): ShortcutId | null {
for (const def of SHORTCUT_DEFS) {
if (def.id === excludeId) continue;
if (resolveCombo(def.id, overrides) === combo) return def.id;
}
return null;
}
/** Bare-Keys, die system-weit fest für Werkzeug-/Gesten-Steuerung reserviert
* sind (Tab-Feld-Zyklus, Enter-Commit, Escape-Abbruch) und NICHT als neue
* Kombination vergeben werden dürfen — diese Tasten laufen ausserhalb dieses
* Registrys, eine Neu-Zuweisung würde still ins Leere laufen (die alte
* Kontext-Bindung bliebe unverändert bestehen, nur verwirrend doppelt belegt). */
const RESERVED_BARE_COMBOS = new Set(["Tab", "Enter", "Escape", "Backspace"]);
export function isReservedCombo(combo: string): boolean {
return RESERVED_BARE_COMBOS.has(combo);
}
/**
* [252] Fest verdrahtetes Tab-Verhalten (nicht umbelegbar): Tab gehört immer der Befehlszeile; Ctrl+Tab /
* Ctrl+Umschalt+Tab (auch auf dem Mac Ctrl, Cmd+Tab gehört dem System) springt durch die Ansichts-Reiter.
* `null` = keine Tab-Taste bzw. Cmd/Alt+Tab (System).
*/
export type TabKeyAction = "nextViewTab" | "prevViewTab" | "commandLine";
export function tabKeyAction(e: Pick<KeyboardEvent, "key" | "ctrlKey" | "metaKey" | "altKey" | "shiftKey">): TabKeyAction | null {
if (e.key !== "Tab" || e.metaKey || e.altKey) return null;
if (e.ctrlKey) return e.shiftKey ? "prevViewTab" : "nextViewTab";
return "commandLine";
}
const KEY_DISPLAY_LABELS: Record<string, string> = {
Delete: "Entf",
Backspace: "Rücktaste",
Escape: "Esc",
Enter: "Enter",
Tab: "Tab",
" ": "Leertaste",
ArrowUp: "↑",
ArrowDown: "↓",
ArrowLeft: "←",
ArrowRight: "→",
};
/** Englische Tastennamen, wo sie vom Deutschen abweichen (Rest wie `KEY_DISPLAY_LABELS`). */
const KEY_DISPLAY_LABELS_EN: Record<string, string> = { Delete: "Del", Backspace: "Backspace", " ": "Space" };
/** Menschenlesbare Darstellung einer Kombination, plattformabhängig (⌘⇧Z auf macOS, Strg+Umschalt+Z sonst). */
export function formatCombo(combo: string, mac: boolean = isMacPlatform()): string {
const parts = combo.split("+");
const key = parts[parts.length - 1];
const mods = parts.slice(0, -1);
const segs: string[] = [];
const en = getLanguage() === "en";
if (mods.includes("mod")) segs.push(mac ? "⌘" : en ? "Ctrl" : "Strg");
if (mods.includes("shift")) segs.push(mac ? "⇧" : en ? "Shift" : "Umschalt");
if (mods.includes("alt")) segs.push(mac ? "⌥" : "Alt");
const keyLabel = (en ? KEY_DISPLAY_LABELS_EN[key] : undefined) ?? KEY_DISPLAY_LABELS[key] ?? (key.length === 1 ? key.toUpperCase() : key);
segs.push(keyLabel);
return segs.join(mac ? "" : "+");
}
/** Reihenfolge/Zuordnung Ziffer→Werkzeug — Ids der rebindbaren Kombination in `keyboardShortcuts.ts`. */
export const TOOL_SHORTCUT_IDS: { id: ShortcutId; tool: string }[] = [
{ id: "toolText", tool: "text" },
{ id: "toolLine", tool: "line" },
{ id: "toolCircle", tool: "circle" },
{ id: "toolRect", tool: "rect" },
{ id: "toolPolyline", tool: "polyline" },
{ id: "toolWall", tool: "wall" },
{ id: "toolCeiling", tool: "ceiling" },
{ id: "toolWindow", tool: "window" },
{ id: "toolDoor", tool: "door" },
{ id: "toolRoom", tool: "room" },
];
/** [287] (12) Tastenkürzel eines Werkzeugs zur Anzeige (Tooltip „Linie (L)“), mit Übersteuerung; ohne Kürzel `null`. */
export function toolShortcutLabel(tool: string, overrides: ShortcutOverrides = loadShortcutOverrides()): string | null {
const m = TOOL_SHORTCUT_IDS.find((x) => x.tool === tool);
const combo = m ? resolveCombo(m.id, overrides) : "";
return combo ? formatCombo(combo) : null;
}