TypeScript
fix(web): preserve unsaved keybinding edits when saving another binding (#11109)
Ilkka Lappeteläinenf55409497774
diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.test.tsindex 8c95bc569b8..99e8ad7d20d 100644--- a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts+++ b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts@@ -1,6 +1,9 @@import { describe, expect, it } from "vite-plus/test";import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";-import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";+import {+ compileResolvedKeybindingsConfig,+ DEFAULT_RESOLVED_KEYBINDINGS,+} from "@t3tools/shared/keybindings";import {buildKeybindingRows,@@ -115,6 +118,48 @@ describe("KeybindingsSettings.logic", () => {}});+ it("keeps sibling row identities when a saved binding moves to the end", () => {+ const bindings = compileResolvedKeybindingsConfig([+ { command: "chat.new", key: "mod+n" },+ { command: "terminal.toggle", key: "mod+j" },+ { command: "terminal.toggle", key: "mod+k", when: "terminalFocus" },+ ]);+ const before = buildKeybindingRows(bindings);+ const after = buildKeybindingRows([+ ...bindings.slice(1),+ ...compileResolvedKeybindingsConfig([{ command: "chat.new", key: "mod+shift+n" }]),+ ]);++ expect(after.filter((row) => row.command === "terminal.toggle").map((row) => row.id)).toEqual(+ before.filter((row) => row.command === "terminal.toggle").map((row) => row.id),+ );+ expect(after.find((row) => row.command === "chat.new")?.id).not.toBe(+ before.find((row) => row.command === "chat.new")?.id,+ );+ });++ it("shares a stable editor for resolved duplicates but separates commands, shortcuts, and conditions", () => {+ const bindings = compileResolvedKeybindingsConfig([+ { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },+ { command: "terminal.toggle", key: "mod+escape", when: "(terminalFocus)" },+ { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },+ { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },+ { command: "chat.new", key: "mod+esc", when: "terminalFocus" },+ ]);+ const rows = buildKeybindingRows(bindings);+ expect(rows.map(({ command, key, when }) => ({ command, key, when }))).toEqual([+ { command: "chat.new", key: "mod+esc", when: "terminalFocus" },+ { command: "terminal.toggle", key: "mod+esc", when: "terminalFocus" },+ { command: "terminal.toggle", key: "mod+esc", when: "!terminalFocus" },+ { command: "terminal.toggle", key: "mod+j", when: "terminalFocus" },+ ]);+ expect(new Set(rows.map((row) => row.id)).size).toBe(4);+ expect(rows.find((row) => row.command === "chat.new")?.conflicts).toEqual(["Terminal: Toggle"]);+ expect(buildKeybindingRows(bindings.slice(1)).map((row) => row.id)).toEqual(+ rows.map((row) => row.id),+ );+ });+it("builds searchable rows with readable key and when values", () => {const rows = filterKeybindingRows(buildKeybindingRows([diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.tsindex 18fd0e1acb9..28409786007 100644--- a/apps/web/src/components/settings/KeybindingsSettings.logic.ts+++ b/apps/web/src/components/settings/KeybindingsSettings.logic.ts@@ -244,12 +244,14 @@ export function groupKeybindingRows(export function buildKeybindingRows(keybindings: ResolvedKeybindingsConfig,): ReadonlyArray<KeybindingRow> {- const rows = keybindings.map((binding, index) => {+ const rowsById = new Map<string, KeybindingRow>();+ for (const binding of keybindings) {const defaultBinding = defaultBindingForBinding(binding);const key = shortcutToKeybindingInput(binding.shortcut);const when = whenAstToExpression(binding.whenAst);- return {- id: `${keybindingRowId(binding.command, key, when)}\u0000${index}`,+ const id = keybindingRowId(binding.command, key, when);+ rowsById.set(id, {+ id,command: binding.command,key,when,@@ -258,8 +260,9 @@ export function buildKeybindingRows(defaultWhen: whenAstToExpression(defaultBinding?.whenAst),binding,conflicts: [],- } satisfies KeybindingRow;- });+ });+ }+ const rows = [...rowsById.values()];const rowsWithConflicts = rows.map((row) => {const conflicts = keybindingConflictLabels(rows, {