Skip to content

Commit 9674662

Browse files
committed
fix(web): settle remount caret at end and keep surround-typing in answers
Summary: - The focus restore after a plainText remount now places the caret at the end of the current value instead of the snapshot cursor, which was seeded from the previous mode's stale cursor prop. Both mode transitions settle the caret at the end anyway, so this removes the brief window where a keystroke could land at a stale offset. - ComposerSurroundSelectionPlugin is mounted in both modes: wrapping a selection by typing brackets/quotes is plain typing behavior, not token behavior. The plugin takes a plainText prop and uses raw cursor clamps in plain mode; the mention-boundary guard is skipped there so answer text that merely looks like a mention still wraps. Rationale: - Follow-up to PR #7818 bot re-review: Cursor Bugbot flagged the stale caret on mode remount, Macroscope flagged the surround-typing regression for question answers introduced by the plugin gate. Tests: - vp test run src/components/ComposerPromptEditor.test.ts (5 passed) - tsc --noEmit on apps/web clean; oxlint on both touched files clean AI-Assisted-By: Codex AI-Assisted: true AI-Agent: claude-code AI-Model: anthropic/claude-fable-5
1 parent fdea833 commit 9674662

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,15 @@ function ComposerInlineTokenPastePlugin() {
12761276
function ComposerSurroundSelectionPlugin(props: {
12771277
terminalContexts: ReadonlyArray<TerminalContextDraft>;
12781278
skills: ReadonlyArray<ServerProviderSkill>;
1279+
plainText: boolean;
12791280
}) {
12801281
const [editor] = useLexicalComposerContext();
1282+
// Surround-typing is not token behavior, so this plugin renders in both
1283+
// modes. In plain mode there are no token nodes: cursor mapping is a raw
1284+
// clamp and the mention-boundary guard would false-positive on answer text
1285+
// that merely looks like a mention, so it is skipped.
1286+
const collapseCursor = props.plainText ? clampExpandedCursor : collapseExpandedComposerCursor;
1287+
const touchesMentionBoundary = props.plainText ? () => false : selectionTouchesMentionBoundary;
12811288
const terminalContextsRef = useRef(props.terminalContexts);
12821289
const skillMetadataRef = useRef(skillMetadataByName(props.skills));
12831290
const pendingSurroundSelectionRef = useRef<{
@@ -1324,7 +1331,7 @@ function ComposerSurroundSelectionPlugin(props: {
13241331
return null;
13251332
}
13261333
const value = $getRoot().getTextContent();
1327-
if (selectionTouchesMentionBoundary(value, range.start, range.end)) {
1334+
if (touchesMentionBoundary(value, range.start, range.end)) {
13281335
return null;
13291336
}
13301337
return {
@@ -1343,17 +1350,13 @@ function ComposerSurroundSelectionPlugin(props: {
13431350
selectionSnapshot.expandedEnd,
13441351
);
13451352
const nextValue = `${selectionSnapshot.value.slice(0, selectionSnapshot.expandedStart)}${inputData}${selectedText}${surroundCloseSymbol}${selectionSnapshot.value.slice(selectionSnapshot.expandedEnd)}`;
1346-
// This plugin only renders in rich (tokenized) mode.
13471353
$setComposerEditorPrompt(
13481354
nextValue,
13491355
terminalContextsRef.current,
13501356
skillMetadataRef.current,
1351-
false,
1352-
);
1353-
const selectionStart = collapseExpandedComposerCursor(
1354-
nextValue,
1355-
selectionSnapshot.expandedStart,
1357+
props.plainText,
13561358
);
1359+
const selectionStart = collapseCursor(nextValue, selectionSnapshot.expandedStart);
13571360
$setSelectionRangeAtComposerOffsets(
13581361
selectionStart + inputData.length,
13591362
selectionStart + inputData.length + selectedText.length,
@@ -1399,7 +1402,7 @@ function ComposerSurroundSelectionPlugin(props: {
13991402
return;
14001403
}
14011404
const value = $getRoot().getTextContent();
1402-
if (selectionTouchesMentionBoundary(value, range.start, range.end)) {
1405+
if (touchesMentionBoundary(value, range.start, range.end)) {
14031406
pendingSurroundSelectionRef.current = null;
14041407
pendingDeadKeySelectionRef.current = null;
14051408
return;
@@ -1480,7 +1483,7 @@ function ComposerSurroundSelectionPlugin(props: {
14801483
pendingDeadKeySelection.expandedStart,
14811484
pendingDeadKeySelection.expandedEnd,
14821485
);
1483-
const replacementStart = collapseExpandedComposerCursor(
1486+
const replacementStart = collapseCursor(
14841487
currentValue,
14851488
pendingDeadKeySelection.expandedStart,
14861489
);
@@ -1691,14 +1694,19 @@ function ComposerPromptEditorInner({
16911694
useLayoutEffect(() => {
16921695
if (restoreFocusOnRemountRef.current) {
16931696
restoreFocusOnRemountRef.current = false;
1694-
focusAt(snapshotRef.current.cursor);
1697+
// Focus at the end: the snapshot cursor was seeded from the previous
1698+
// mode's stale cursor prop, while both mode transitions settle the
1699+
// caret at the end (answer end on question open, draft end on resolve).
1700+
// Placing it there directly leaves no window for keystrokes to land at
1701+
// a stale offset before the parent's effects run.
1702+
focusAt(collapseComposerCursor(snapshotRef.current.value, snapshotRef.current.value.length));
16951703
}
16961704
return () => {
16971705
// Layout cleanup runs before the old DOM node is detached, so
16981706
// activeElement still points at it here.
16991707
restoreFocusOnRemountRef.current = editor.getRootElement() === document.activeElement;
17001708
};
1701-
}, [editor, focusAt, restoreFocusOnRemountRef]);
1709+
}, [collapseComposerCursor, editor, focusAt, restoreFocusOnRemountRef]);
17021710

17031711
const readSnapshot = useCallback((): {
17041712
value: string;
@@ -1832,9 +1840,16 @@ function ComposerPromptEditorInner({
18321840
/>
18331841
<OnChangePlugin onChange={handleEditorChange} />
18341842
<ComposerCommandKeyPlugin {...(onCommandKeyDown ? { onCommandKeyDown } : {})} />
1843+
{/* Surround-typing (wrap a selection in brackets/quotes) is plain
1844+
typing behavior, not token behavior, so it stays mounted in both
1845+
modes; only the token-dependent plugins are mode-gated. */}
1846+
<ComposerSurroundSelectionPlugin
1847+
terminalContexts={terminalContexts}
1848+
skills={skills}
1849+
plainText={plainText}
1850+
/>
18351851
{plainText ? null : (
18361852
<>
1837-
<ComposerSurroundSelectionPlugin terminalContexts={terminalContexts} skills={skills} />
18381853
<ComposerInlineTokenArrowPlugin />
18391854
<ComposerInlineTokenSelectionNormalizePlugin />
18401855
<ComposerInlineTokenBackspacePlugin />

0 commit comments

Comments
 (0)