Skip to content

fix(ui): reveal the caret after the composer's scripted edits - #5812

Closed
UncertaintyDeterminesYou4ndMe wants to merge 1 commit into
apache:mainfrom
UncertaintyDeterminesYou4ndMe:fix/composer-shift-enter-trailing-break
Closed

UncertaintyDeterminesYou4ndMe wants to merge 1 commit into
apache:mainfrom
UncertaintyDeterminesYou4ndMe:fix/composer-shift-enter-trailing-break

Conversation

@UncertaintyDeterminesYou4ndMe

@UncertaintyDeterminesYou4ndMe UncertaintyDeterminesYou4ndMe commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5811.

Problem

Shift+Enter at the end of a draft that has reached the composer's row cap inserts the line break, but the editor does not scroll: the new line, and the caret on it, sit below the viewport until the next keystroke. The same happens when a break in the bottom row pushes the rest of the line down, and after a multi-line paste past the cap.

The issue's other claim — that the editable stays one line short and the caret falls outside the box — did not reproduce. Measured in Chromium against the editor's own styles: execCommand('insertLineBreak') at the draft end inserts the newline plus a placeholder newline and parks the caret between them; the box grows by one line and the caret paints on it. What the scripted commands skip, unlike a native Enter keypress, is revealing the caret.

Fix

After the Shift/Alt+Enter break, the multi-line insert seam (beforeinput) and the plain-text paste (insertHTML), revealCaretAfterEdit() scrolls the editable to the caret: to the end when nothing but the placeholder follows it, otherwise to the caret's line box. A caret before a newline paints no client rect, so that case is located from the last painted leaf before it, found in document order within the editable (token chips and other inline wrappers nest text, so that leaf can sit outside the caret's own parent). No DOM, text or style change, so serialization and drafts are untouched.

A caret at the end of a restored draft that ends in a newline needs nothing: Chromium paints it at the end of the last line.

Test plan

  • Storybook, real Chromium (Product/Composer Caret Reveal, two stories): a draft typed past the row cap, then Shift+Enter at the end from scrollTop 0, in the middle of the bottom row, and at the end of an upper line while scrolled to the bottom (the caret then sits before a newline with content after it, above the viewport); a Cmd+V of multi-line text and a beforeinput multi-line insert at the end. Each step checks, from the DOM text before the caret rather than from the composer's helper, that the caret's line box lies inside the scroll viewport. Both stories fail without the reveal. EmptyHome also asserts the capped-draft Shift+Enter scrolls.
  • packages/ui composer-shortcuts.test.tsx: 6 tests over a harness that models Chromium's measured DOM and rect behaviour, clamps scrollTop like a real scroll container, and walks nested leaves. Removing the reveal fails four; calling it before the execCommand fails two; a sibling-only previous-leaf lookup fails the nested-caret test.
  • Chromium probe (Playwright, same engine generation as Electron 43) of the same scenarios plus a paste with a trailing <br> run mid-draft: the painted caret ends inside the viewport in each, and a break far from the end does not jump to the bottom.

@github-actions github-actions Bot added the effort/M Under 500 readable lines label Sep 29, 2026

@hqhq1025 hqhq1025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 1bcb969ad37eac9896e9adf166990138fdd5d37f. The three-file change adds a trailing-break class and CSS padding to reveal the composer caret without changing serialized drafts. The two issues below keep this head out of the merge candidate set: the original end-of-draft path still bypasses the compensation in a real Electron/Chromium window, and the current-head Storybook smoke fails on draft restoration.

Local Node 24 clean install, build:test, 38 focused composer/caret tests, focused Biome, ASF headers, diff-check, and merge-tree against current main passed. In the real Electron probe, 12 lines plus Shift+Enter left scrollTop at 0 with 74px available to scroll and no compensation class. Hosted test failed in the EmptyHome Storybook play assertion (expected 96 to be 74) after switching away from and back to a three-line draft; its isolated retry also failed. The four new linkedom tests model caret placement rather than exercising Chromium selection or CSS layout. I did not test macOS/Windows or the WorkHub compact layout. No schema or migration changes were found.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

Comment thread packages/ui/src/composer.tsx Outdated
const tail = range.cloneRange();
tail.selectNodeContents(editable);
tail.setStart(range.startContainer, range.startOffset);
if (tail.toString() !== '') return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] The real end-of-draft Shift+Enter path still returns here. In an Electron/Chromium composer, insertLineBreak leaves the collapsed caret at offset 0 of the final "\n" text node, with one "\n" after the caret. Thus this range's toString() is "\n", not empty, and the function never adds the class or scrolls. With a 12-line draft I measured scrollTop=0, scrollHeight-clientHeight=74, and no class after Shift+Enter, so the original capped-editor defect remains. The new linkedom harness instead advances its synthetic caret past both inserted newlines (composer-shortcuts.test.tsx:252-253), making the test green without covering this browser path. Please exercise actual Chromium selection and the capped scroll case.

Comment thread packages/ui/src/composer.tsx Outdated
}
caretPendingRef.current = false;
placeCaretAtEnd(editable);
// A restored draft can itself end in a newline (a pasted "abc\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] This new compensation call on caret restore also regresses the existing EmptyHome lifecycle contract. On the current head, hosted Storybook smoke fails reproducibly (including its isolated retry) after a three-line draft is switched away and restored: editor.clientHeight becomes 96px instead of its pre-switch 74px, exactly one extra line. The only added layout path is the trailing-break class/padding, so the restore-time guard is treating a draft with a real final line as boxless. Keep the restored editor at its original height unless the caret truly needs a missing line box; the required test check is red on this head.

@UncertaintyDeterminesYou4ndMe
UncertaintyDeterminesYou4ndMe force-pushed the fix/composer-shift-enter-trailing-break branch from 1bcb969 to 1ff5612 Compare September 29, 2026 06:15
@UncertaintyDeterminesYou4ndMe UncertaintyDeterminesYou4ndMe changed the title fix(ui): grow the composer for the boxless line Shift+Enter leaves at the draft end fix(ui): reveal the caret after the composer's scripted edits Sep 29, 2026
@UncertaintyDeterminesYou4ndMe

Copy link
Copy Markdown
Contributor Author

Rewritten as 1ff5612b8 after re-measuring in Chromium; both findings of the automated review pointed at the same wrong premise.

  • The trailing-break class never applied in a real window because Chromium leaves a placeholder newline after the caret, so the guard's "nothing after the caret" check always failed. More to the point, the premise was wrong: the box does grow and the caret paints on the new line. The only missing piece is the scroll, which a native Enter keypress performs and the scripted execCommand does not.
  • EmptyHome failed because a restored draft ending in a newline got the padding line while Chromium paints that caret at the end of the last line; the class and its CSS are gone.

The fix now scrolls the caret into view after the Shift/Alt+Enter break, the multi-line insert seam and the plain-text paste, covering the bottom-row mid-line break as well. EmptyHome gained an assertion for the capped-draft Shift+Enter that fails without the fix. Scenarios and measurements are in the PR description.

@likun666661

Copy link
Copy Markdown
Member

Review summary

The rewritten head (1ff5612b8) has a better problem definition than the original issue: the observable bug is that scripted edits (execCommand) do not reveal the caret, while native keypresses do. revealCaretAfterEdit() is a reasonable mechanism: it uses the caret range when available, falls back to the previous character when Chromium gives no rect before a newline, and adjusts scrollTop without changing draft serialization.

I see two merge-blocking concerns:

  1. The unit test harness does not validate real Chromium behavior. It mocks Range, selection, execCommand, layout, and scrolling. In particular, the test expects editor.scrollTop === editor.scrollHeight, but a real scroll container clamps the maximum to scrollHeight - clientHeight. The Storybook assertion is closer to reality, but it only covers the capped end-of-draft case. Please add or retain a real Electron/Chromium regression covering at least:

    • end-of-draft Shift+Enter;
    • caret before a newline with following content (the no-client-rect fallback);
    • bottom-row mid-line break;
    • multiline paste / beforeinput insert.
  2. rangeBeforeCaret() only checks previousSibling within the current parent. The composer supports inline tokens/chips, so a caret inside a nested text node may have no local previous sibling even though a previous renderable leaf exists higher in the DOM. In that case the helper returns null and skips scrolling. Please either make the previous-leaf traversal DOM-tree aware or document and test the editor DOM invariant that makes the current implementation safe.

The old automated review comments were against the pre-rewrite head and should not be treated as current findings. CI for the current head is green, but I would not sign off until the browser-level coverage and nested-DOM behavior above are resolved.

@UncertaintyDeterminesYou4ndMe
UncertaintyDeterminesYou4ndMe force-pushed the fix/composer-shift-enter-trailing-break branch from 1ff5612 to 47fff77 Compare September 29, 2026 07:13
@UncertaintyDeterminesYou4ndMe

Copy link
Copy Markdown
Contributor Author

Thanks — both addressed in 47fff7717.

Browser-level coverage. Added Product/Composer Caret Reveal (apps/desktop/stories/composer-caret-reveal.stories.tsx), two stories driving the real editable past the row cap:

  • ShiftEnterRevealsTheCaret: end-of-draft Shift+Enter from scrollTop 0; a break in the middle of the bottom row; a break at the end of an upper line while scrolled to the bottom, which leaves the caret before a newline with content after it (the no-client-rect fallback) and above the viewport.
  • MultiLineInsertsRevealTheCaret: a Cmd+V of multi-line text (userEvent.paste, through the insertHTML seam) and a beforeinput multi-line insert, both at the end.

The oracle is independent of the helper: the caret's line is counted from the DOM text before the selection, and the assertion is that its line box lies inside [scrollTop, scrollTop + clientHeight]. With the reveal calls removed both stories fail at their first step. The unit harness now clamps scrollTop to scrollHeight - clientHeight and asserts against that, so the mock no longer allows a scroll position a real container would refuse.

Nested caret. rangeBeforeCaret now walks to the last painted leaf before the caret in document order, bounded by the editable: it climbs out of inline wrappers and skips empty text nodes. A unit test places the caret at the start of a text node inside a <span> whose previous leaf is a top-level text node; the sibling-only version fails it.

@hqhq1025 hqhq1025 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 47fff77. The revised caret-reveal logic addresses the earlier Shift+Enter failure in the exercised paths, and the previous draft-height regression is absent: the two new real-Chromium Storybook scenarios and EmptyHome pass locally. The 36 focused UI tests and test build also pass. However, the current-head hosted test fails before those gates because the new story uses a noncanonical ASF header; see the inline finding. This PR is not ready until that file is corrected and the current-head CI passes. Diff-check and a merge-tree against main 0323714 are clean. I did not run packaged Electron or macOS/Windows validation. No schema/migration change is present. This is not a merge approval.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0

@hqhq1025 hqhq1025 Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Use the repository's canonical ASF header spacing here. This line has three spaces after *, while the checked template requires five; npm run check:asf-headers exits 1 and the current-head hosted test stops before its remaining jobs. Align this header with the adjacent Storybook files and rerun CI.

Shift+Enter at the end of a draft that has reached the composer's row cap
inserts the line break but does not scroll: the new line, and the caret on
it, sit below the viewport until the next keystroke. The same happens when a
break in the bottom row pushes the rest of the line down, and after a
multi-line paste.

Measured in Chromium against the editor's own styles: execCommand
('insertLineBreak') at the draft end inserts the newline plus a placeholder
newline and parks the caret between them; the box grows by one line and the
caret paints on it. What the scripted commands skip, unlike a native
keypress, is revealing the caret.

After the Shift/Alt+Enter break, the multi-line insert seam and the
plain-text paste, scroll the editable to the caret: to the end when nothing
but the placeholder follows it, otherwise to the caret's line box — located
from the character before the caret when the caret itself paints no rect
(before a newline). No DOM, text or style change, so serialization and
drafts are untouched.
@UncertaintyDeterminesYou4ndMe

Copy link
Copy Markdown
Contributor Author

Header fixed in a741bcae8 (the URL line of the ASF block was indented three spaces instead of five; npm run check:asf-headers passes locally). No other change.

@UncertaintyDeterminesYou4ndMe
UncertaintyDeterminesYou4ndMe force-pushed the fix/composer-shift-enter-trailing-break branch from 47fff77 to a741bca Compare September 29, 2026 07:44

@hqhq1025 hqhq1025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit a741bcae8f1e12d471d333715fb7f30626519e7a. No new P0–P3 issue found in this head.

Compared with the prior reviewed commit 47fff771, the only change corrects indentation in the ASF license header of apps/desktop/stories/composer-caret-reveal.stories.tsx. The prior CI-blocking header finding is fixed. The caret behavior and tests are otherwise unchanged from that review; the previously reported two P2 behavior issues remained fixed there. No schema or migration change.

The current-head hosted test check passed. Locally, ASF headers, check:app-shell-hooks, git diff --check, and merge-tree against main ae71ab31 passed. I did not rerun Electron/Chromium behavior tests on this header-only commit, and the packaged Desktop remains untested. This is not merge approval.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

@github-actions github-actions Bot added effort/L Under 1000 readable lines and removed effort/M Under 500 readable lines labels Sep 30, 2026
@github-actions github-actions Bot added the stale No qualifying activity within the lifecycle policy window label Oct 30, 2026
@github-actions

Copy link
Copy Markdown

This pull request has had no new commits for 30 days and has been marked stale. It will be closed in 7 days unless a new commit is pushed.

Comments do not reset this timer: only a new commit does. If the pull request is intentionally long-lived, a maintainer can apply the pinned label.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This pull request has been closed because it received no new commits during the 7-day grace period. This is not a judgement on the merit of the change; it only keeps the review queue aligned with work that is actively in flight.

The branch and review history are preserved. Push a new commit and reopen the pull request whenever the work is ready to continue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/L Under 1000 readable lines stale No qualifying activity within the lifecycle policy window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Composer does not grow (or scroll) for Shift+Enter at the end of the draft

3 participants