Skip to content

Commit 1636d7b

Browse files
committed
fix(web): keep macOS arrow navigation at editor boundaries
- Preserve Command-arrow movement and selection at Markdown markers - Keep the caret outside closing markers after DOM updates
1 parent 673c5e3 commit 1636d7b

2 files changed

Lines changed: 132 additions & 27 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.navigation.browser.test.tsx‎

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Editor } from "@tiptap/core";
22
import { TextSelection } from "@tiptap/pm/state";
33
import { act, createRef } from "react";
44
import { createRoot, type Root } from "react-dom/client";
5-
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
66

77
import { serializeEditorDoc } from "~/composer-rich-text-doc";
88
import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";
@@ -23,6 +23,7 @@ beforeEach(() => {
2323
afterEach(async () => {
2424
await act(async () => root.unmount());
2525
container.remove();
26+
vi.restoreAllMocks();
2627
vi.unstubAllGlobals();
2728
});
2829

@@ -97,19 +98,110 @@ it("leaves vertical navigation between paragraphs to the browser", async () => {
9798
});
9899
});
99100

100-
it.each([
101-
{ shiftKey: true },
102-
{ altKey: true },
103-
{ metaKey: true },
104-
{ ctrlKey: true },
105-
{ isComposing: true },
106-
])("preserves modified Up behavior for %j", async (options) => {
107-
const editor = await mountEditor(draft);
108-
await act(async () => {
109-
expect(press(editor, "ArrowUp", options)).toBe(false);
110-
editor.commands.insertContent("x");
101+
it.each([{ shiftKey: true }, { altKey: true }, { ctrlKey: true }, { isComposing: true }])(
102+
"preserves modified Up behavior for %j",
103+
async (options) => {
104+
const editor = await mountEditor(draft);
105+
await act(async () => {
106+
expect(press(editor, "ArrowUp", options)).toBe(false);
107+
editor.commands.insertContent("x");
108+
});
109+
expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x"));
110+
},
111+
);
112+
113+
describe("macOS boundary navigation", () => {
114+
beforeEach(() => {
115+
vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel");
116+
container.style.width = "600px";
117+
});
118+
119+
const markdown = String.raw`Fixed the top padding to 16px and updated the offline index and ZIP.
120+
121+
**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
122+
\[
123+
f(x,y)=2x^2+y^2,\qquad x^2+y^2\le16.
124+
\]
125+
That's exactly a disk with the circular boundary \(x^2+y^2=16\), and it is within section 14.7.
126+
127+
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.**`;
128+
129+
it.each(["ArrowRight", "ArrowDown"])(
130+
"keeps Command-%s after the closing marker in the pasted draft",
131+
async (key) => {
132+
const editor = await mountEditor(markdown);
133+
await act(async () => {
134+
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
135+
editor.view.focus();
136+
expect(press(editor, "ArrowRight")).toBe(true);
137+
expect(press(editor, key, { metaKey: true })).toBe(true);
138+
expect(editor.state.selection.eq(TextSelection.atEnd(editor.state.doc))).toBe(true);
139+
// Force the DOM selection rewrite that used to pull the caret back.
140+
editor.view.updateState(editor.state);
141+
const markers = editor.view.dom.querySelectorAll(".composer-rich-marker");
142+
const closingMarker = markers[markers.length - 1];
143+
const caret = window.getSelection()?.getRangeAt(0);
144+
if (!closingMarker || !caret) throw new Error("Caret or closing marker is missing");
145+
const markerRange = document.createRange();
146+
markerRange.selectNode(closingMarker);
147+
expect(caret.compareBoundaryPoints(Range.START_TO_END, markerRange)).toBeGreaterThanOrEqual(
148+
0,
149+
);
150+
editor.commands.insertContent("x");
151+
});
152+
expect(serializeEditorDoc(editor.state.doc).value).toBe(markdown + "x");
153+
},
154+
);
155+
156+
it.each(["ArrowRight", "ArrowDown"])(
157+
"moves Command-%s from inside bold to outside its closing marker",
158+
async (key) => {
159+
const editor = await mountEditor("**bold**");
160+
await act(async () => {
161+
editor.commands.setTextSelection(3);
162+
editor.view.focus();
163+
expect(press(editor, key, { metaKey: true })).toBe(true);
164+
editor.commands.insertContent("x");
165+
});
166+
expect(serializeEditorDoc(editor.state.doc).value).toBe("**bold**x");
167+
},
168+
);
169+
170+
it.each(["ArrowLeft", "ArrowUp"])("moves Command-%s before the opening marker", async (key) => {
171+
const editor = await mountEditor("**bold**");
172+
await act(async () => {
173+
editor.commands.setTextSelection(3);
174+
editor.view.focus();
175+
expect(press(editor, key, { metaKey: true })).toBe(true);
176+
editor.commands.insertContent("x");
177+
});
178+
expect(serializeEditorDoc(editor.state.doc).value).toBe("x**bold**");
179+
});
180+
181+
it.each(["ArrowRight", "ArrowDown"])(
182+
"extends Shift-Command-%s to the end without collapsing the selection",
183+
async (key) => {
184+
const editor = await mountEditor("**bold**");
185+
await act(async () => {
186+
editor.commands.setTextSelection(3);
187+
editor.view.focus();
188+
expect(press(editor, key, { metaKey: true, shiftKey: true })).toBe(true);
189+
expect(editor.state.selection.anchor).toBe(3);
190+
expect(editor.state.selection.head).toBe(TextSelection.atEnd(editor.state.doc).to);
191+
editor.commands.insertContent("x");
192+
});
193+
expect(serializeEditorDoc(editor.state.doc).value).toBe("**box**");
194+
},
195+
);
196+
197+
it("leaves Command-arrow navigation to the browser on other platforms", async () => {
198+
vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
199+
const editor = await mountEditor("**bold**");
200+
await act(async () => {
201+
editor.view.focus();
202+
expect(press(editor, "ArrowDown", { metaKey: true })).toBe(false);
203+
});
111204
});
112-
expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x"));
113205
});
114206

115207
it("leaves literal Markdown navigation to the browser in plain mode", async () => {

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,11 +1106,19 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
11061106
editorProps: {
11071107
attributes: editorAttributes,
11081108
handleKeyDown: (view, event) => {
1109+
const boundary = event.metaKey
1110+
? event.key === "ArrowLeft" || event.key === "ArrowRight"
1111+
? "lineboundary"
1112+
: event.key === "ArrowUp" || event.key === "ArrowDown"
1113+
? "documentboundary"
1114+
: null
1115+
: event.key === "Home" || event.key === "End"
1116+
? "lineboundary"
1117+
: null;
11091118
if (
11101119
isMacPlatform(navigator.platform) &&
1111-
(event.key === "Home" || event.key === "End") &&
1120+
boundary &&
11121121
!event.altKey &&
1113-
!event.metaKey &&
11141122
!event.ctrlKey &&
11151123
!event.isComposing
11161124
) {
@@ -1122,23 +1130,28 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
11221130
) {
11231131
event.preventDefault();
11241132
event.stopPropagation();
1133+
const backward =
1134+
event.key === "Home" || event.key === "ArrowLeft" || event.key === "ArrowUp";
11251135
selection.modify(
11261136
event.shiftKey ? "extend" : "move",
1127-
event.key === "Home" ? "backward" : "forward",
1128-
"lineboundary",
1137+
backward ? "backward" : "forward",
1138+
boundary,
11291139
);
11301140
if (selection.anchorNode && selection.focusNode) {
1131-
view.dispatch(
1132-
view.state.tr
1133-
.setSelection(
1134-
TextSelection.create(
1135-
view.state.doc,
1136-
view.posAtDOM(selection.anchorNode, selection.anchorOffset),
1137-
view.posAtDOM(selection.focusNode, selection.focusOffset),
1138-
),
1139-
)
1140-
.scrollIntoView(),
1141+
const tr = view.state.tr.setSelection(
1142+
TextSelection.create(
1143+
view.state.doc,
1144+
view.posAtDOM(selection.anchorNode, selection.anchorOffset),
1145+
view.posAtDOM(selection.focusNode, selection.focusOffset),
1146+
),
11411147
);
1148+
// A marker has two caret stops at one document position. Keep
1149+
// the outer stop when native navigation reaches a boundary.
1150+
if (richText && tr.selection.empty) {
1151+
const { $head } = tr.selection;
1152+
tr.setStoredMarks((backward ? $head.nodeBefore : $head.nodeAfter)?.marks ?? []);
1153+
}
1154+
view.dispatch(tr.scrollIntoView());
11421155
}
11431156
return true;
11441157
}

0 commit comments

Comments
 (0)