Skip to content

Commit 9a251fb

Browse files
baxennpub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79
andauthored
fix(desktop): scope Emacs keys to lines (#2127)
Signed-off-by: npub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79 <1a7496365ae2df8adb1b932a1a6f68b9cb3950f3d179c8a451c32d194e8f7dc1@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79 <1a7496365ae2df8adb1b932a1a6f68b9cb3950f3d179c8a451c32d194e8f7dc1@sprout-oss.stage.blox.sqprod.co>
1 parent e84f3fe commit 9a251fb

2 files changed

Lines changed: 114 additions & 2 deletions

File tree

‎desktop/src/features/messages/lib/useRichTextEditor.ts‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Placeholder from "@tiptap/extension-placeholder";
77
import Link from "@tiptap/extension-link";
88
import { Extension, type KeyboardShortcutCommand } from "@tiptap/core";
99
import { Plugin, Selection, TextSelection } from "@tiptap/pm/state";
10+
import type { ResolvedPos } from "@tiptap/pm/model";
1011

1112
import {
1213
hasPrimaryShortcutModifier,
@@ -35,6 +36,24 @@ import {
3536
} from "./codeBlockExtensions";
3637
import { SpoilerMark } from "./spoilerMark";
3738

39+
function hardBreakLineBounds($from: ResolvedPos) {
40+
const parentStart = $from.start();
41+
let start = parentStart;
42+
let end = parentStart + $from.parent.content.size;
43+
44+
$from.parent.forEach((node, offset) => {
45+
if (node.type.name !== "hardBreak") return;
46+
const breakPosition = parentStart + offset;
47+
if (breakPosition < $from.pos) {
48+
start = breakPosition + node.nodeSize;
49+
} else if (breakPosition >= $from.pos && end > breakPosition) {
50+
end = breakPosition;
51+
}
52+
});
53+
54+
return { end, start };
55+
}
56+
3857
/**
3958
* Plain-text edit descriptor returned by autocomplete hooks
4059
* (mentions / channel links / emoji). Offsets are in plain-text space —
@@ -243,8 +262,8 @@ export function useRichTextEditor({
243262
link: false,
244263
}),
245264
// macOS text fields traditionally support a small set of Emacs-style
246-
// Control shortcuts. ProseMirror already handles Ctrl-A/E/H/D on macOS;
247-
// these fill in the common movement and kill-line gaps for the composer.
265+
// Control shortcuts. Keep movement and kill-line scoped to the current
266+
// hard-break-delimited line rather than the whole ProseMirror block.
248267
Extension.create({
249268
name: "macEmacsTextShortcuts",
250269
addKeyboardShortcuts() {
@@ -254,6 +273,20 @@ export function useRichTextEditor({
254273
}
255274

256275
return {
276+
"Ctrl-a": ({ editor: ed }) => {
277+
const { $from } = ed.state.selection;
278+
if (!$from.parent.inlineContent) return false;
279+
return ed.commands.setTextSelection(
280+
hardBreakLineBounds($from).start,
281+
);
282+
},
283+
"Ctrl-e": ({ editor: ed }) => {
284+
const { $from } = ed.state.selection;
285+
if (!$from.parent.inlineContent) return false;
286+
return ed.commands.setTextSelection(
287+
hardBreakLineBounds($from).end,
288+
);
289+
},
257290
"Ctrl-b": ({ editor: ed }) => {
258291
const { empty, from } = ed.state.selection;
259292
if (!empty || from <= 0) return false;
@@ -272,6 +305,21 @@ export function useRichTextEditor({
272305
return ed.commands.deleteSelection();
273306
}
274307

308+
if ($from.parent.inlineContent) {
309+
const lineEnd = hardBreakLineBounds($from).end;
310+
if (from < lineEnd) {
311+
return ed.commands.deleteRange({ from, to: lineEnd });
312+
}
313+
314+
const nodeAfter = $from.nodeAfter;
315+
if (nodeAfter?.type.name === "hardBreak") {
316+
return ed.commands.deleteRange({
317+
from,
318+
to: from + nodeAfter.nodeSize,
319+
});
320+
}
321+
}
322+
275323
const blockEnd = $from.end();
276324
if (from < blockEnd) {
277325
return ed.commands.deleteRange({ from, to: blockEnd });
@@ -460,6 +508,30 @@ export function useRichTextEditor({
460508
// command/caret logic, fires regardless of selection state, and works
461509
// the same across browser engines. Returning `true` consumes the key.
462510
handleKeyDown: (view, event) => {
511+
// Chromium handles Ctrl-A/E as whole-content movement before the
512+
// keymap on macOS. Claim them at the raw DOM layer so hard breaks
513+
// behave like actual line boundaries.
514+
if (
515+
isMacPlatform() &&
516+
event.ctrlKey &&
517+
!event.metaKey &&
518+
!event.altKey &&
519+
!event.shiftKey &&
520+
(event.key.toLowerCase() === "a" || event.key.toLowerCase() === "e")
521+
) {
522+
const { $from } = view.state.selection;
523+
if (!$from.parent.inlineContent) return false;
524+
const bounds = hardBreakLineBounds($from);
525+
const position =
526+
event.key.toLowerCase() === "a" ? bounds.start : bounds.end;
527+
view.dispatch(
528+
view.state.tr.setSelection(
529+
TextSelection.create(view.state.doc, position),
530+
),
531+
);
532+
return true;
533+
}
534+
463535
// ⌘K / Ctrl+K → link editor. The formatting toolbar has always
464536
// advertised this shortcut on its link button; bind it here so it
465537
// actually works. Kept alongside the ArrowUp handling below rather

‎desktop/tests/e2e/composer-link-shortcut.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,46 @@ test("macOS plain Ctrl+K still kill-lines in the composer", async ({
126126
await expect(page.getByTestId("search-dialog-input")).toHaveCount(0);
127127
});
128128

129+
test("macOS Ctrl+A, Ctrl+E, and Ctrl+K stay within hard-break lines", async ({
130+
page,
131+
}) => {
132+
test.skip(process.platform !== "darwin", "mac-only Emacs bindings");
133+
134+
await page.addInitScript(() => {
135+
Object.defineProperty(navigator, "platform", { value: "MacIntel" });
136+
});
137+
await installMockBridge(page);
138+
await openGeneral(page);
139+
140+
const input = page.getByTestId("message-input");
141+
await input.click();
142+
await input.pressSequentially("first");
143+
await input.press("Shift+Enter");
144+
await input.pressSequentially("middle");
145+
await input.press("Shift+Enter");
146+
await input.pressSequentially("last");
147+
await expect(input.locator("br")).toHaveCount(2);
148+
149+
// Start/end movement applies to the third visual line, not the whole editor.
150+
await page.keyboard.press("Control+a");
151+
await input.pressSequentially("[");
152+
await page.keyboard.press("Control+e");
153+
await input.pressSequentially("]");
154+
await expect(input).toHaveText("firstmiddle[last]");
155+
156+
// Kill only to this line's end, preserving both preceding lines.
157+
await page.keyboard.press("Control+a");
158+
await page.keyboard.press("Control+k");
159+
await expect(input).toHaveText("firstmiddle");
160+
await expect(input.locator("br")).toHaveCount(3);
161+
162+
// At end-of-line, kill the newline and join with the following line.
163+
await page.keyboard.press("Control+b");
164+
await page.keyboard.press("Control+k");
165+
await expect(input).toHaveText("firstmiddle");
166+
await expect(input.locator("br")).toHaveCount(1);
167+
});
168+
129169
test("⌘K outside the composer opens quick search", async ({ page }) => {
130170
await installMockBridge(page);
131171
await openGeneral(page);

0 commit comments

Comments
 (0)