Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(channel): start cursor on new line after quote reply (#4707)

Peter Chinman7badf3427c69
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 6f4479b1c5..7df8c5dfe9 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -352,6 +352,7 @@ export function Channel(props: ChannelProps) {
requestAnimationFrame(() => {
state.replyInputHandle?.()?.restoreSnapshot(nextSnapshot, {
focus: false,
+ cursor: 'trailing-paragraph',
});
state.replyInputFocusRequest.request();
});
diff --git a/js/app/packages/channel/Input/ChannelInput.tsx b/js/app/packages/channel/Input/ChannelInput.tsx
index 488e1311f9..d57a356f4c 100644
--- a/js/app/packages/channel/Input/ChannelInput.tsx
+++ b/js/app/packages/channel/Input/ChannelInput.tsx
@@ -50,11 +50,13 @@ import type {
InputHandle,
InputPersistenceKey,
InputSnapshot,
+ RestoreSnapshotOptions,
} from './types';
import { isReplyInput } from './types';
import { uploadInputAttachments } from './upload-attachments';
import { entityToDocumentMentionInfo } from './utils/entity-mention';
import { applyInlineFormat, applyNodeFormat } from './utils/formatting';
+import { $selectTrailingParagraph } from './utils/select-trailing-paragraph';
import { hasSendableInputContent } from './utils/sendable-content';
export type ChannelInputProps = InputCallbacks & {
@@ -180,19 +182,23 @@ export function ChannelInput(props: ChannelInputProps) {
let pendingRestore:
| {
snapshot: InputSnapshot;
- options?: { focus?: boolean };
+ options?: RestoreSnapshotOptions;
}
| undefined;
let pendingFocus = false;
+ // Caret placement requested by a `cursor: 'trailing-paragraph'` restore.
+ // Applied on the next programmatic focus rather than at restore time.
+ let pendingCursor: RestoreSnapshotOptions['cursor'];
const applySnapshot = (
snapshot: InputSnapshot,
- options?: { focus?: boolean }
+ options?: RestoreSnapshotOptions
) => {
markdownEditor.controls.setMarkdown(snapshot.value);
+ pendingCursor = options?.cursor;
attachmentTracker.setAttachments(snapshot.attachments);
mentionsTracker.setMentions(snapshot.mentions);
- if (options?.focus !== false) markdownEditor.controls.focus();
+ if (options?.focus !== false) focusEditorNow();
};
const flushPendingRestore = () => {
@@ -202,18 +208,26 @@ export function ChannelInput(props: ChannelInputProps) {
queueMicrotask(() => applySnapshot(restore.snapshot, restore.options));
};
+ const focusEditorNow = () => {
+ if (pendingCursor === 'trailing-paragraph') {
+ pendingCursor = undefined;
+ lexicalEditor().update(() => $selectTrailingParagraph());
+ }
+ markdownEditor.controls.focus();
+ };
+
const focusEditor = () => {
if (!isEditorConnected) {
pendingFocus = true;
return;
}
- markdownEditor.controls.focus();
+ focusEditorNow();
};
const flushPendingFocus = () => {
if (!pendingFocus) return;
pendingFocus = false;
- queueMicrotask(() => markdownEditor.controls.focus());
+ queueMicrotask(() => focusEditorNow());
};
// Macro AI is mentionable in every channel. It is surfaced through the same
diff --git a/js/app/packages/channel/Input/tests/select-trailing-paragraph.ts b/js/app/packages/channel/Input/tests/select-trailing-paragraph.ts
new file mode 100644
index 0000000000..5c262b2685
--- /dev/null
+++ b/js/app/packages/channel/Input/tests/select-trailing-paragraph.ts
@@ -0,0 +1,116 @@
+/**
+ * @vitest-environment jsdom
+ */
+
+import { $createQuoteNode, QuoteNode } from '@lexical/rich-text';
+import {
+ $createParagraphNode,
+ $createTextNode,
+ $getRoot,
+ $getSelection,
+ $isParagraphNode,
+ $isRangeSelection,
+ createEditor,
+ type LexicalEditor,
+} from 'lexical';
+import { describe, expect, test } from 'vitest';
+import { $selectTrailingParagraph } from '../utils/select-trailing-paragraph';
+
+function createTestEditor(): LexicalEditor {
+ const editor = createEditor({
+ namespace: 'select-content-end-test',
+ nodes: [QuoteNode],
+ onError: (error) => {
+ throw error;
+ },
+ });
+
+ const root = document.createElement('div');
+ root.contentEditable = 'true';
+ document.body.appendChild(root);
+ editor.setRootElement(root);
+
+ return editor;
+}
+
+function expectCaretInBlock(editor: LexicalEditor, blockIndex: number) {
+ editor.read(() => {
+ const block = $getRoot().getChildren()[blockIndex];
+ const selection = $getSelection();
+ expect($isRangeSelection(selection)).toBe(true);
+ if (!$isRangeSelection(selection)) return;
+ expect(selection.isCollapsed()).toBe(true);
+ expect(selection.anchor.getNode().getTopLevelElement()?.getKey()).toBe(
+ block.getKey()
+ );
+ });
+}
+
+describe('$selectContentEnd', () => {
+ test('appends an empty paragraph below a trailing quote and selects it', () => {
+ const editor = createTestEditor();
+
+ editor.update(
+ () => {
+ const quote = $createQuoteNode();
+ quote.append($createTextNode('quoted message'));
+ $getRoot().clear().append(quote);
+ $selectTrailingParagraph();
+ },
+ { discrete: true }
+ );
+
+ editor.read(() => {
+ const children = $getRoot().getChildren();
+ expect(children).toHaveLength(2);
+ expect($isParagraphNode(children[1])).toBe(true);
+ expect(children[1].getTextContent()).toBe('');
+ });
+ expectCaretInBlock(editor, 1);
+ });
+
+ test('keeps a trailing paragraph as-is and moves the caret to its end', () => {
+ const editor = createTestEditor();
+
+ editor.update(
+ () => {
+ const quote = $createQuoteNode();
+ quote.append($createTextNode('quoted message'));
+ const draft = $createParagraphNode();
+ draft.append($createTextNode('draft'));
+ $getRoot().clear().append(quote, draft);
+ $selectTrailingParagraph();
+ },
+ { discrete: true }
+ );
+
+ editor.read(() => {
+ const children = $getRoot().getChildren();
+ expect(children).toHaveLength(2);
+ const selection = $getSelection();
+ expect($isRangeSelection(selection)).toBe(true);
+ if (!$isRangeSelection(selection)) return;
+ expect(selection.anchor.offset).toBe('draft'.length);
+ });
+ expectCaretInBlock(editor, 1);
+ });
+
+ test('appends a paragraph to an empty root and selects it', () => {
+ const editor = createTestEditor();
+
+ editor.update(
+ () => {
+ $getRoot().clear();
+ $selectTrailingParagraph();
+ },
+ { discrete: true }
+ );
+
+ editor.read(() => {
+ const children = $getRoot().getChildren();
+ expect(children).toHaveLength(1);
+ expect($isParagraphNode(children[0])).toBe(true);
+ });
+ expectCaretInBlock(editor, 0);
+ });
+});
diff --git a/js/app/packages/channel/Input/types.ts b/js/app/packages/channel/Input/types.ts
index bec4bd8649..35a230e103 100644
--- a/js/app/packages/channel/Input/types.ts
+++ b/js/app/packages/channel/Input/types.ts
@@ -81,6 +81,19 @@ export type InputCommands = {
removeAttachment: (attachment: InputAttachmentData) => void;
};
+export type RestoreSnapshotOptions = {
+ focus?: boolean;
+ /**
+ * `'trailing-paragraph'` places the caret at the end of the restored
+ * content's trailing paragraph, appending an empty one when the content
+ * ends in a non-paragraph block (e.g. a quote-reply's blockquote) — so
+ * typing never extends the block itself. With `focus: false` the caret
+ * is placed when the input is next focused programmatically (e.g. via a
+ * focus request), so the restore itself never steals focus.
+ */
+ cursor?: 'trailing-paragraph';
+};
+
export type InputHandle = {
clear: () => void;
focus: () => void;
@@ -88,7 +101,7 @@ export type InputHandle = {
attachFiles: (files: File[]) => Promise<void>;
restoreSnapshot: (
snapshot: InputSnapshot,
- options?: { focus?: boolean }
+ options?: RestoreSnapshotOptions
) => void;
/**
* Inserts a mention for a dragged soup entity into the editor. Only provided
diff --git a/js/app/packages/channel/Input/utils/select-trailing-paragraph.ts b/js/app/packages/channel/Input/utils/select-trailing-paragraph.ts
new file mode 100644
index 0000000000..696f913a73
--- /dev/null
+++ b/js/app/packages/channel/Input/utils/select-trailing-paragraph.ts
@@ -0,0 +1,25 @@
+import { $createParagraphNode, $getRoot, $isParagraphNode } from 'lexical';
+
+/**
+ * Move the caret to the end of the editor content, ensuring it lands on its
+ * own paragraph when the content ends in a non-paragraph block.
+ *
+ * Restoring a quote-reply leaves the blockquote as the final block — the
+ * markdown importer drops whitespace-only trailing paragraphs and parks the
+ * caret at the start of the content — so typing would extend the quote
+ * itself. Appending an empty paragraph puts the caret on a fresh line below
+ * the quote instead. Content that already ends in a paragraph (e.g. an
+ * existing draft) keeps its shape, with the caret at the end of it.
+ *
+ * Must be called inside `editor.update()`.
+ */
+export function $selectTrailingParagraph(): void {
+ const lastChild = $getRoot().getLastChild();
+ if ($isParagraphNode(lastChild)) {
+ lastChild.selectEnd();
+ return;
+ }
+ const paragraph = $createParagraphNode();
+ $getRoot().append(paragraph);
+ paragraph.selectEnd();
+}
diff --git a/js/app/packages/channel/StandaloneThread/EditableThread.tsx b/js/app/packages/channel/StandaloneThread/EditableThread.tsx
index 054ee5401f..9939bfd842 100644
--- a/js/app/packages/channel/StandaloneThread/EditableThread.tsx
+++ b/js/app/packages/channel/StandaloneThread/EditableThread.tsx
@@ -55,7 +55,10 @@ function EditableThreadInner() {
};
setReplyInputState(nextSnapshot);
requestAnimationFrame(() => {
- replyInputHandle()?.restoreSnapshot(nextSnapshot, { focus: false });
+ replyInputHandle()?.restoreSnapshot(nextSnapshot, {
+ focus: false,
+ cursor: 'trailing-paragraph',
+ });
ctx.replyInputFocusRequest.request();
});
} else {