feat(web): navigate back and forward with mod+[ and mod+] (#13212)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
23c7ab901a
commit
829af7b73a
5 files changed
+85
-1
No files matched your search
@@ -7,7 +7,7 @@ import {
|
||||
type CSSProperties,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { useLocation, useNavigate } from "@tanstack/react-router";
|
||||
import { useLocation, useNavigate, useParams } from "@tanstack/react-router";
|
||||
|
||||
import { isElectron } from "../env";
|
||||
import { getLocalStorageItem, removeLocalStorageItem } from "../hooks/useLocalStorage";
|
||||
@@ -16,6 +16,13 @@ import {
|
||||
resolveShortcutCommand,
|
||||
shortcutLabelForCommand,
|
||||
} from "../keybindings";
|
||||
import { isEditableFocused } from "../lib/editableFocus";
|
||||
import { isPreviewFocused } from "../lib/previewFocus";
|
||||
import { isTerminalFocused } from "../lib/terminalFocus";
|
||||
import { isModelPickerOpen } from "../modelPickerVisibility";
|
||||
import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
|
||||
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
|
||||
import { resolveThreadRouteRef } from "../threadRoutes";
|
||||
import { cn, isMacPlatform } from "../lib/utils";
|
||||
import { primaryServerKeybindingsAtom } from "../state/server";
|
||||
import { useEnvironmentIdentificationMode, useLegacySidebarEnabled } from "../hooks/useSettings";
|
||||
@@ -147,6 +154,56 @@ function SidebarControl() {
|
||||
);
|
||||
}
|
||||
|
||||
// Moves through the app's route history like a browser's back/forward buttons.
|
||||
function NavigationHistoryShortcuts() {
|
||||
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
|
||||
const routeThreadRef = useParams({
|
||||
strict: false,
|
||||
select: (params) => resolveThreadRouteRef(params),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.defaultPrevented) return;
|
||||
if (
|
||||
event.target instanceof HTMLElement &&
|
||||
event.target.closest("[data-keybinding-capture]")
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const command = resolveShortcutCommand(event, keybindings, {
|
||||
context: {
|
||||
terminalFocus: isTerminalFocused(),
|
||||
terminalOpen: routeThreadRef
|
||||
? selectThreadTerminalUiState(
|
||||
useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
|
||||
routeThreadRef,
|
||||
).terminalOpen
|
||||
: false,
|
||||
previewFocus: isPreviewFocused(),
|
||||
previewOpen: routeThreadRef
|
||||
? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, routeThreadRef) ===
|
||||
"preview"
|
||||
: false,
|
||||
editableFocus: isEditableFocused(event.target),
|
||||
modelPickerOpen: isModelPickerOpen(),
|
||||
},
|
||||
});
|
||||
if (command !== "navigation.back" && command !== "navigation.forward") return;
|
||||
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (command === "navigation.back") window.history.back();
|
||||
else window.history.forward();
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
return () => window.removeEventListener("keydown", onKeyDown);
|
||||
}, [keybindings, routeThreadRef]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Settings swaps the thread sidebar out of the tree. Keep the lightweight
|
||||
// project projection subscribed so returning to a draft never renders the
|
||||
// zero-project state while the environment snapshot reconnects.
|
||||
@@ -269,6 +326,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
|
||||
</Sidebar>
|
||||
{children}
|
||||
<SidebarControl />
|
||||
<NavigationHistoryShortcuts />
|
||||
</SidebarProvider>
|
||||
</PanelAnimationSuppressionProvider>
|
||||
);
|
||||
|
||||
@@ -913,6 +913,25 @@ describe("resolveShortcutCommand", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("navigates history with mod+[ and mod+] outside the terminal", () => {
|
||||
const back = event({ key: "[", code: "BracketLeft", metaKey: true });
|
||||
const forward = event({ key: "]", code: "BracketRight", ctrlKey: true });
|
||||
assert.strictEqual(
|
||||
resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }),
|
||||
"navigation.back",
|
||||
);
|
||||
assert.strictEqual(
|
||||
resolveShortcutCommand(forward, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }),
|
||||
"navigation.forward",
|
||||
);
|
||||
assert.isNull(
|
||||
resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, {
|
||||
platform: "MacIntel",
|
||||
context: { terminalFocus: true },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("matches bracket shortcuts using the physical key code", () => {
|
||||
assert.strictEqual(
|
||||
resolveShortcutCommand(
|
||||
|
||||
@@ -117,6 +117,9 @@ actions of the same kind undo together. The notice remains available for five
|
||||
seconds after the latest action. The default shortcut skips text fields and
|
||||
terminals so native undo keeps working there.
|
||||
|
||||
`navigation.back` (`mod+[` by default) and `navigation.forward` (`mod+]`) move
|
||||
through the pages you have visited, like a browser's back and forward buttons.
|
||||
|
||||
`chat.new` may ask you to choose a project when there is more than one.
|
||||
`chat.newLocal` skips that chooser. Both use your
|
||||
[new-thread defaults](./thread-sidebar.md#start-a-thread).
|
||||
|
||||
@@ -57,6 +57,8 @@ export type ModelPickerKeybindingCommand = (typeof MODEL_PICKER_KEYBINDING_COMMA
|
||||
|
||||
export const STATIC_KEYBINDING_COMMANDS = [
|
||||
"sidebar.toggle",
|
||||
"navigation.back",
|
||||
"navigation.forward",
|
||||
"terminal.toggle",
|
||||
"terminal.split",
|
||||
"terminal.splitVertical",
|
||||
|
||||
@@ -20,6 +20,8 @@ type WhenToken =
|
||||
|
||||
export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
|
||||
{ key: "mod+b", command: "sidebar.toggle" },
|
||||
{ key: "mod+[", command: "navigation.back", when: "!terminalFocus" },
|
||||
{ key: "mod+]", command: "navigation.forward", when: "!terminalFocus" },
|
||||
{ key: "mod+j", command: "terminal.toggle" },
|
||||
{ key: "mod+alt+b", command: "rightPanel.toggle" },
|
||||
{ key: "mod+d", command: "terminal.split", when: "terminalFocus" },
|
||||
|
||||
Reference in new issue
Block a user