Skip to content

Commit 3b92ec9

Browse files
committed
feat(web): toggle rich text from the composer with mod+/
Switching the composer between rich text and literal Markdown meant opening Settings. A configurable composer.toggleRichText binding, mod+/ by default, now flips composerRichTextEnabled from the composer. The flip remounts the editor, so the composer asks the new instance to take the caret back at the stored cursor; the Settings switch flips the same setting without pulling focus. A held key flips once.
1 parent a3abb52 commit 3b92ec9

8 files changed

Lines changed: 118 additions & 0 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ export interface ComposerPromptEditorProps {
108108
* literal character.
109109
*/
110110
richTextEnabled?: boolean;
111+
/**
112+
* Set while a flip of `richTextEnabled` from the composer's shortcut is in
113+
* flight. The flip remounts the editor, and the new instance takes the caret
114+
* back at the stored cursor. Driven by the shortcut so that flipping the
115+
* same setting from Settings does not pull focus into the composer; the
116+
* editor clears it through `onFocusRestored`.
117+
*/
118+
restoreFocusOnMount?: boolean;
119+
/** Reports that a requested focus restore has been applied. */
120+
onFocusRestored?: (() => void) | undefined;
111121
/** Draft records behind the prompt's context references, keyed by context id. */
112122
contextRecords: ComposerDraftContextRecords;
113123
/** Structured clipboard payload for the given referenced ids, or null to skip. */
@@ -1207,6 +1217,37 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
12071217
[editor],
12081218
);
12091219

1220+
// This editor was mounted by a rich text flip from the composer's shortcut,
1221+
// so take the caret back at the cursor the last one reported. Two things
1222+
// make this less direct than it looks. `useEditor` returns null on its first
1223+
// render and builds the instance in an effect, so the restore has to wait
1224+
// for the instance. And the instance can be rebuilt again right after, which
1225+
// replaces the focused DOM node and drops focus to the body — so this
1226+
// restores for whichever instance is current rather than only the first.
1227+
// The cursor is captured at mount so typing never drags the caret back.
1228+
const [restoreFocusCursor] = useState(() => (props.restoreFocusOnMount ? initialCursor : null));
1229+
const onFocusRestoredRef = useRef(props.onFocusRestored);
1230+
useEffect(() => {
1231+
onFocusRestoredRef.current = props.onFocusRestored;
1232+
});
1233+
useEffect(() => {
1234+
if (restoreFocusCursor === null || !editor) return;
1235+
// Not `focusAt`: that bails before placing the caret whenever the editor's
1236+
// text and the store's disagree, a guard against reporting stale text to
1237+
// the store. A restore reports nothing, so it places the caret directly
1238+
// against the document as it is, clamped by the same coordinate mapping.
1239+
const map = serializeEditorDoc(editor.state.doc);
1240+
const flat = collapsedToFlat(map, clampCollapsedComposerCursor(map.value, restoreFocusCursor));
1241+
editor.commands.setTextSelection(flatToPm(map, flat));
1242+
// ProseMirror's focus, not the DOM's: it writes the selection into the
1243+
// DOM. A raw `dom.focus()` leaves the DOM selection at the start, and the
1244+
// first chip node view to mount makes ProseMirror re-read it from there,
1245+
// which resets the caret and reports a cursor of 0 to the store.
1246+
editor.view.focus();
1247+
scrollTiptapCaretIntoView(editor);
1248+
onFocusRestoredRef.current?.();
1249+
}, [editor, restoreFocusCursor]);
1250+
12101251
useImperativeHandle(
12111252
editorRef,
12121253
() => ({

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ import {
983983
} from "@t3tools/client-runtime/providerSkills";
984984
import { searchProviderSkills } from "../../providerSkillSearch";
985985
import { useDelayedStatus } from "../../hooks/useDelayedStatus";
986+
import { useUpdateClientSettings } from "../../hooks/useSettings";
986987
import { useMediaQuery } from "../../hooks/useMediaQuery";
987988
import { usePanelAnimationSettings } from "../../panelAnimations";
988989
import { useAtomCommand } from "../../state/use-atom-command";
@@ -2140,6 +2141,17 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
21402141
const hasWrappedPrompt = useComposerMultilinePrompt(composerMenuAnchor);
21412142
const hasMultilinePrompt = prompt.includes("\n") || hasWrappedPrompt;
21422143
const [isStashMenuOpen, setIsStashMenuOpen] = useState(false);
2144+
const updateClientSettings = useUpdateClientSettings();
2145+
// Flipping the setting remounts the editor, which drops focus. The flag
2146+
// tells the new instance that the user asked for this from the composer's
2147+
// shortcut and wants the caret back; changing the same setting from
2148+
// Settings leaves it alone. Rich text is a client setting rather than
2149+
// composer state, so both entry points drive the same switch.
2150+
const [pendingComposerFocusRestore, setPendingComposerFocusRestore] = useState(false);
2151+
const toggleComposerRichText = useCallback(() => {
2152+
setPendingComposerFocusRestore(true);
2153+
void updateClientSettings({ composerRichTextEnabled: !settings.composerRichTextEnabled });
2154+
}, [settings.composerRichTextEnabled, updateClientSettings]);
21432155
const [isTasksDrawerOpen, setIsTasksDrawerOpen] = useState(false);
21442156
const [stashPulse, setStashPulse] = useState<{ key: number; active: boolean }>({
21452157
key: 0,
@@ -5217,6 +5229,28 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
52175229
setIsStashMenuOpen(false);
52185230
}, [prompt]);
52195231

5232+
// The composer's own way to flip rich text, so the caret comes back after
5233+
// the remount. The Settings panel flips the same setting without a restore.
5234+
useEffect(() => {
5235+
const handler = (event: globalThis.KeyboardEvent) => {
5236+
const command = resolveShortcutCommand(event, keybindings, {
5237+
context: {
5238+
terminalFocus: getTerminalFocusOwner() !== null,
5239+
terminalOpen,
5240+
modelPickerOpen: isComposerModelPickerOpen,
5241+
},
5242+
});
5243+
if (command !== "composer.toggleRichText") return;
5244+
event.preventDefault();
5245+
event.stopPropagation();
5246+
// A held key auto-repeats keydown; one press is one flip.
5247+
if (event.repeat || isCommandPaletteOpen()) return;
5248+
toggleComposerRichText();
5249+
};
5250+
window.addEventListener("keydown", handler, true);
5251+
return () => window.removeEventListener("keydown", handler, true);
5252+
}, [isComposerModelPickerOpen, keybindings, terminalOpen, toggleComposerRichText]);
5253+
52205254
useEffect(() => {
52215255
const handler = (event: globalThis.KeyboardEvent) => {
52225256
const command = resolveShortcutCommand(event, keybindings, {
@@ -6877,6 +6911,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
68776911
}
68786912
editorRef={composerEditorRef}
68796913
richTextEnabled={settings.composerRichTextEnabled}
6914+
restoreFocusOnMount={pendingComposerFocusRestore}
6915+
onFocusRestored={() => setPendingComposerFocusRestore(false)}
68806916
value={
68816917
isComposerApprovalState
68826918
? ""

‎apps/web/src/components/settings/KeybindingsSettings.logic.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe("KeybindingsSettings.logic", () => {
2626
"composer.workspace",
2727
"composer.branch",
2828
"composer.previousWorktree",
29+
"composer.toggleRichText",
2930
"modelPicker.previousProvider",
3031
"modelPicker.nextProvider",
3132
"thread.copyReference",

‎apps/web/src/keybindings.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ const DEFAULT_BINDINGS = compile([
160160
command: "thread.settle",
161161
whenAst: whenNot(whenIdentifier("terminalFocus")),
162162
},
163+
{
164+
shortcut: modShortcut("/"),
165+
command: "composer.toggleRichText",
166+
whenAst: whenNot(whenIdentifier("terminalFocus")),
167+
},
163168
{ shortcut: modShortcut("1"), command: "thread.jump.1" },
164169
{ shortcut: modShortcut("2"), command: "thread.jump.2" },
165170
{ shortcut: modShortcut("3"), command: "thread.jump.3" },
@@ -1247,6 +1252,30 @@ describe("composer and pull request shortcuts", () => {
12471252
},
12481253
);
12491254

1255+
it("binds mod+/ to the composer rich text toggle on every platform", () => {
1256+
assert.equal(
1257+
resolveShortcutCommand(event({ key: "/", metaKey: true }), DEFAULT_BINDINGS, {
1258+
platform: "MacIntel",
1259+
context: { terminalFocus: false },
1260+
}),
1261+
"composer.toggleRichText",
1262+
);
1263+
assert.equal(
1264+
resolveShortcutCommand(event({ key: "/", ctrlKey: true }), DEFAULT_BINDINGS, {
1265+
platform: "Linux",
1266+
context: { terminalFocus: false },
1267+
}),
1268+
"composer.toggleRichText",
1269+
);
1270+
assert.notEqual(
1271+
resolveShortcutCommand(event({ key: "/", metaKey: true }), DEFAULT_BINDINGS, {
1272+
platform: "MacIntel",
1273+
context: { terminalFocus: true },
1274+
}),
1275+
"composer.toggleRichText",
1276+
);
1277+
});
1278+
12501279
const shortcuts = [
12511280
["h", "composer.host"],
12521281
["e", "composer.effort"],

‎docs/internals/composer-editors.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ Clipboard text must come from the Markdown serializer, not DOM text: chip labels
2525
the source and marker decorations are not content. Structured context records accompany
2626
that text when available. Paste completes trailing chip delimiters and adds a leading
2727
boundary when inserting a chip directly after text.
28+
29+
The `composer.toggleRichText` shortcut remounts the editor, which drops focus.
30+
The restore is signalled by the shortcut rather than derived from the setting,
31+
because a flag set during render never reaches the child React mounts in that
32+
render. It waits for the `useEditor` instance, which arrives in an effect and
33+
can be rebuilt once more, and places the caret with ProseMirror's `view.focus()`.
34+
A raw DOM focus leaves the DOM selection at the start, and the first chip node
35+
view to mount then resets the caret to 0.

‎docs/user/keybindings.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Use `mod+shift+e` for effort, `mod+shift+a` for access mode, `mod+shift+x` for t
1919
workspace, and `mod+shift+g` for the Git branch. The workspace menu includes the
2020
current checkout, a new worktree, and the previous worktree when available.
2121
Use `mod+shift+l` to reuse the previous worktree directly.
22+
Use `mod+/` to switch the composer between rich text and its Markdown source.
2223

2324
In the model picker, press Left in an empty search field or Shift+Tab to reach
2425
the provider list. Use Up/Down to move and Enter to choose. Right returns to

‎packages/contracts/src/keybindings.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const STATIC_KEYBINDING_COMMANDS = [
8383
"appearance.cycle",
8484
"themeEditor.toggle",
8585
"composer.stash",
86+
"composer.toggleRichText",
8687
"composer.host",
8788
"composer.effort",
8889
"composer.mode",

‎packages/shared/src/keybindings.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
4545
{ key: "mod+alt+shift+a", command: "appearance.cycle", when: "!terminalFocus" },
4646
{ key: "mod+alt+shift+t", command: "themeEditor.toggle" },
4747
{ key: "mod+s", command: "composer.stash", when: "!terminalFocus" },
48+
{ key: "mod+/", command: "composer.toggleRichText", when: "!terminalFocus" },
4849
{ key: "mod+shift+enter", command: "thread.steerQueuedMessage", when: "!terminalFocus" },
4950
{ key: "mod+n", command: "chat.new", when: "!terminalFocus" },
5051
{ key: "mod+shift+o", command: "chat.new", when: "!terminalFocus" },

0 commit comments

Comments
 (0)