@devries-cameron20 / t3code Mirror · read-only / commits / 7bdfed98d7
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 13:00 UTC

fix(web): keep vertical cursor stops outside styled markers (#19651)

Yash Singh committed Oct 14, 2026 at 18:09 UTC 7bdfed98d733d1bcda99f43d43b24cd6913f2fce
2 files changed +142 -4
apps/web/src/components/ComposerPromptEditorTiptap.navigation.test.tsx new
+126
@@ -0,0 +1,126 @@
1 +// @vitest-environment jsdom
2 +
3 +import type { Editor } from "@tiptap/core";
4 +import { TextSelection } from "@tiptap/pm/state";
5 +import { act, createRef } from "react";
6 +import { createRoot, type Root } from "react-dom/client";
7 +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
8 +
9 +import { serializeEditorDoc } from "~/composer-rich-text-doc";
10 +import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";
11 +
12 +const draft =
13 + "> **Assistant and reasoning text still save intermediate full snapshots**, using the existing 50 ms batching. Compaction still preserves their `turn-item.updated` events. [Code](/Users/yashsingh/p/projects/t3code/apps/server/src/orchestration-v2/ProjectionMaintenance.ts:235)";
14 +
15 +let root: Root;
16 +let container: HTMLDivElement;
17 +
18 +beforeEach(() => {
19 + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
20 + container = document.createElement("div");
21 + document.body.append(container);
22 + root = createRoot(container);
23 +});
24 +
25 +afterEach(async () => {
26 + await act(async () => root.unmount());
27 + container.remove();
28 + vi.unstubAllGlobals();
29 +});
30 +
31 +async function mountEditor(value: string, richTextEnabled = true) {
32 + await act(async () =>
33 + root.render(
34 + <ComposerPromptEditor
35 + editorRef={createRef<ComposerPromptEditorHandle>()}
36 + value={value}
37 + cursor={0}
38 + richTextEnabled={richTextEnabled}
39 + contextRecords={new Map()}
40 + skills={[]}
41 + disabled={false}
42 + placeholder=""
43 + onChange={() => {}}
44 + onPaste={() => {}}
45 + />,
46 + ),
47 + );
48 + const element = container.querySelector<HTMLElement & { editor: Editor }>(".tiptap");
49 + if (!element) throw new Error("Composer did not mount");
50 + return element.editor;
51 +}
52 +
53 +function press(editor: Editor, key: string, options: KeyboardEventInit = {}) {
54 + const event = new KeyboardEvent("keydown", { key, cancelable: true, ...options });
55 + const handled = editor.view.props.handleKeyDown?.(editor.view, event);
56 + expect(event.defaultPrevented).toBe(Boolean(handled));
57 + return handled;
58 +}
59 +
60 +it("keeps Up before the opening bold marker in a quote, including while typing", async () => {
61 + const editor = await mountEditor(draft);
62 + const position = editor.state.selection.from;
63 + await act(async () => {
64 + editor.view.focus();
65 + expect(press(editor, "ArrowUp")).toBe(true);
66 + expect(editor.state.selection.from).toBe(position);
67 + editor.view.updateState(editor.state);
68 + const marker = editor.view.dom.querySelector(".composer-rich-marker");
69 + const caret = window.getSelection()?.getRangeAt(0);
70 + if (!marker || !caret) throw new Error("Caret or bold marker is missing");
71 + const markerRange = document.createRange();
72 + markerRange.selectNode(marker);
73 + expect(caret.compareBoundaryPoints(Range.START_TO_START, markerRange)).toBeLessThanOrEqual(0);
74 + // A repeated Up stays at the outside stop, ready for native navigation.
75 + expect(press(editor, "ArrowUp")).toBe(false);
76 + editor.commands.insertContent("x");
77 + });
78 + expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> x**"));
79 +});
80 +
81 +it("keeps Down after a closing marker, including while typing", async () => {
82 + const editor = await mountEditor("> **bold**");
83 + await act(async () => {
84 + editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
85 + expect(press(editor, "ArrowDown")).toBe(true);
86 + expect(press(editor, "ArrowDown")).toBe(false);
87 + editor.commands.insertContent("x");
88 + });
89 + expect(serializeEditorDoc(editor.state.doc).value).toBe("> **bold**x");
90 +});
91 +
92 +it("leaves vertical navigation between paragraphs to the browser", async () => {
93 + const editor = await mountEditor("**first**\n**second**");
94 + await act(async () => {
95 + const first = editor.state.doc.child(0);
96 + editor.commands.setTextSelection(first.nodeSize + 1);
97 + expect(press(editor, "ArrowUp")).toBe(false);
98 + editor.commands.setTextSelection(first.nodeSize - 1);
99 + expect(press(editor, "ArrowDown")).toBe(false);
100 + });
101 +});
102 +
103 +it.each([
104 + { shiftKey: true },
105 + { altKey: true },
106 + { metaKey: true },
107 + { ctrlKey: true },
108 + { isComposing: true },
109 +])("preserves modified Up behavior for %j", async (options) => {
110 + const editor = await mountEditor(draft);
111 + await act(async () => {
112 + expect(press(editor, "ArrowUp", options)).toBe(false);
113 + editor.commands.insertContent("x");
114 + });
115 + expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x"));
116 +});
117 +
118 +it("leaves literal Markdown navigation to the browser in plain mode", async () => {
119 + const editor = await mountEditor(draft, false);
120 + await act(async () => {
121 + editor.commands.setTextSelection(5);
122 + expect(press(editor, "ArrowUp")).toBe(false);
123 + editor.commands.insertContent("x");
124 + });
125 + expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x"));
126 +});
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+16 -4
@@ -1238,7 +1238,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
1238 }
1239 }
1240 if (
1241 - (event.key === "ArrowLeft" || event.key === "ArrowRight") &&
1241 + ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key) &&
1242 !event.shiftKey &&
1243 !event.altKey &&
1244 !event.metaKey &&
@@ -1247,10 +1247,22 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
1247 view.state.selection.empty
1248 ) {
1249 const { $from } = view.state.selection;
1250 - const direction = event.key === "ArrowLeft" ? -1 : 1;
1250 + const direction = event.key === "ArrowLeft" || event.key === "ArrowUp" ? -1 : 1;
1251 + const horizontal = event.key === "ArrowLeft" || event.key === "ArrowRight";
1252 + // At the document's ends, native Up/Down can cross a marker without
1253 + // changing the document position. Record the stop so DOM selection
1254 + // synchronization does not put the caret back inside the styling.
1255 + const atDocumentEdge =
1256 + $from.pos ===
1257 + (direction === -1
1258 + ? TextSelection.atStart(view.state.doc).from
1259 + : TextSelection.atEnd(view.state.doc).to);
1260 // Take the other stop of a styled edge before skipping a chip, so
1261 // the plain stop between styled text and a chip stays reachable.
1253 - const step = stepCaretAcrossStyledEdge(view.state, direction);
1262 + const step =
1263 + horizontal || atDocumentEdge
1264 + ? stepCaretAcrossStyledEdge(view.state, direction)
1265 + : null;
1266 if (step) {
1267 event.preventDefault();
1268 event.stopPropagation();
@@ -1258,7 +1270,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
1270 return true;
1271 }
1272 const adjacent = direction === -1 ? $from.nodeBefore : $from.nodeAfter;
1261 - if (adjacent?.type.name.startsWith("composer-")) {
1273 + if (horizontal && adjacent?.type.name.startsWith("composer-")) {
1274 event.preventDefault();
1275 event.stopPropagation();
1276 view.dispatch(