Skip to content
TypeScript

fix(web): keep macOS Command-arrow navigation at editor boundaries (#22011)

Yash Singhe8d87689b8b8
diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.navigation.browser.test.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.navigation.browser.test.tsx
index 01d913925f4..3f9f6ccbb23 100644
--- a/apps/web/src/components/ComposerPromptEditorTiptap.navigation.browser.test.tsx
+++ b/apps/web/src/components/ComposerPromptEditorTiptap.navigation.browser.test.tsx
@@ -2,7 +2,7 @@ import type { Editor } from "@tiptap/core";
import { TextSelection } from "@tiptap/pm/state";
import { act, createRef } from "react";
import { createRoot, type Root } from "react-dom/client";
-import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { serializeEditorDoc } from "~/composer-rich-text-doc";
import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";
@@ -23,6 +23,7 @@ beforeEach(() => {
afterEach(async () => {
await act(async () => root.unmount());
container.remove();
+ vi.restoreAllMocks();
vi.unstubAllGlobals();
});
@@ -97,19 +98,110 @@ it("leaves vertical navigation between paragraphs to the browser", async () => {
});
});
-it.each([
- { shiftKey: true },
- { altKey: true },
- { metaKey: true },
- { ctrlKey: true },
- { isComposing: true },
-])("preserves modified Up behavior for %j", async (options) => {
- const editor = await mountEditor(draft);
- await act(async () => {
- expect(press(editor, "ArrowUp", options)).toBe(false);
- editor.commands.insertContent("x");
+it.each([{ shiftKey: true }, { altKey: true }, { ctrlKey: true }, { isComposing: true }])(
+ "preserves modified Up behavior for %j",
+ async (options) => {
+ const editor = await mountEditor(draft);
+ await act(async () => {
+ expect(press(editor, "ArrowUp", options)).toBe(false);
+ editor.commands.insertContent("x");
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x"));
+ },
+);
+
+describe("macOS boundary navigation", () => {
+ beforeEach(() => {
+ vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel");
+ container.style.width = "600px";
+ });
+
+ const markdown = String.raw`Fixed the top padding to 16px and updated the offline index and ZIP.
+
+**Yes: [HW08, problem 6, page 2](/Users/yashsingh/.t3/scratch/2026-10-16-gather-all-info-about-the-2fcd52f5/calculus-resources/files/HW08.pdf)** asks for the absolute maximum and minimum of
+\[
+f(x,y)=2x^2+y^2,\qquad x^2+y^2\le16.
+\]
+That's exactly a disk with the circular boundary \(x^2+y^2=16\), and it is within section 14.7.
+
+The original Fall 2022, 2023, and 2024 sample exams also have circle-constrained extrema problems, all question 6. Those explicitly require Lagrange multipliers, so they were removed from your filtered copies. **HW08 #6 is the best match for your exam.**`;
+
+ it.each(["ArrowRight", "ArrowDown"])(
+ "keeps Command-%s after the closing marker in the pasted draft",
+ async (key) => {
+ const editor = await mountEditor(markdown);
+ await act(async () => {
+ editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
+ editor.view.focus();
+ expect(press(editor, "ArrowRight")).toBe(true);
+ expect(press(editor, key, { metaKey: true })).toBe(true);
+ expect(editor.state.selection.eq(TextSelection.atEnd(editor.state.doc))).toBe(true);
+ // Force the DOM selection rewrite that used to pull the caret back.
+ editor.view.updateState(editor.state);
+ const markers = editor.view.dom.querySelectorAll(".composer-rich-marker");
+ const closingMarker = markers[markers.length - 1];
+ const caret = window.getSelection()?.getRangeAt(0);
+ if (!closingMarker || !caret) throw new Error("Caret or closing marker is missing");
+ const markerRange = document.createRange();
+ markerRange.selectNode(closingMarker);
+ expect(caret.compareBoundaryPoints(Range.START_TO_END, markerRange)).toBeGreaterThanOrEqual(
+ 0,
+ );
+ editor.commands.insertContent("x");
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(markdown + "x");
+ },
+ );
+
+ it.each(["ArrowRight", "ArrowDown"])(
+ "moves Command-%s from inside bold to outside its closing marker",
+ async (key) => {
+ const editor = await mountEditor("**bold**");
+ await act(async () => {
+ editor.commands.setTextSelection(3);
+ editor.view.focus();
+ expect(press(editor, key, { metaKey: true })).toBe(true);
+ editor.commands.insertContent("x");
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe("**bold**x");
+ },
+ );
+
+ it.each(["ArrowLeft", "ArrowUp"])("moves Command-%s before the opening marker", async (key) => {
+ const editor = await mountEditor("**bold**");
+ await act(async () => {
+ editor.commands.setTextSelection(3);
+ editor.view.focus();
+ expect(press(editor, key, { metaKey: true })).toBe(true);
+ editor.commands.insertContent("x");
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe("x**bold**");
+ });
+
+ it.each(["ArrowRight", "ArrowDown"])(
+ "extends Shift-Command-%s to the end without collapsing the selection",
+ async (key) => {
+ const editor = await mountEditor("**bold**");
+ await act(async () => {
+ editor.commands.setTextSelection(3);
+ editor.view.focus();
+ expect(press(editor, key, { metaKey: true, shiftKey: true })).toBe(true);
+ expect(editor.state.selection.anchor).toBe(3);
+ expect(editor.state.selection.head).toBe(TextSelection.atEnd(editor.state.doc).to);
+ editor.commands.insertContent("x");
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe("**box**");
+ },
+ );
+
+ it("leaves Command-arrow navigation to the browser on other platforms", async () => {
+ vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
+ const editor = await mountEditor("**bold**");
+ await act(async () => {
+ editor.view.focus();
+ expect(press(editor, "ArrowDown", { metaKey: true })).toBe(false);
+ });
});
- expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x"));
});
it("leaves literal Markdown navigation to the browser in plain mode", async () => {
diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx
index 14562f73cd4..92915b0c942 100644
--- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx
+++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx
@@ -1106,11 +1106,19 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
editorProps: {
attributes: editorAttributes,
handleKeyDown: (view, event) => {
+ const boundary = event.metaKey
+ ? event.key === "ArrowLeft" || event.key === "ArrowRight"
+ ? "lineboundary"
+ : event.key === "ArrowUp" || event.key === "ArrowDown"
+ ? "documentboundary"
+ : null
+ : event.key === "Home" || event.key === "End"
+ ? "lineboundary"
+ : null;
if (
isMacPlatform(navigator.platform) &&
- (event.key === "Home" || event.key === "End") &&
+ boundary &&
!event.altKey &&
- !event.metaKey &&
!event.ctrlKey &&
!event.isComposing
) {
@@ -1122,23 +1130,28 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
) {
event.preventDefault();
event.stopPropagation();
+ const backward =
+ event.key === "Home" || event.key === "ArrowLeft" || event.key === "ArrowUp";
selection.modify(
event.shiftKey ? "extend" : "move",
- event.key === "Home" ? "backward" : "forward",
- "lineboundary",
+ backward ? "backward" : "forward",
+ boundary,
);
if (selection.anchorNode && selection.focusNode) {
- view.dispatch(
- view.state.tr
- .setSelection(
- TextSelection.create(
- view.state.doc,
- view.posAtDOM(selection.anchorNode, selection.anchorOffset),
- view.posAtDOM(selection.focusNode, selection.focusOffset),
- ),
- )
- .scrollIntoView(),
+ const tr = view.state.tr.setSelection(
+ TextSelection.create(
+ view.state.doc,
+ view.posAtDOM(selection.anchorNode, selection.anchorOffset),
+ view.posAtDOM(selection.focusNode, selection.focusOffset),
+ ),
);
+ // A marker has two caret stops at one document position. Keep
+ // the outer stop when native navigation reaches a boundary.
+ if (richText && tr.selection.empty) {
+ const { $head } = tr.selection;
+ tr.setStoredMarks((backward ? $head.nodeBefore : $head.nodeAfter)?.marks ?? []);
+ }
+ view.dispatch(tr.scrollIntoView());
}
return true;
}