Skip to content

fix(RichTextEditorToolbar): Insert link with nothing selected inserts the link at the caret - #9668

Merged
cixzhang merged 3 commits into
mainfrom
fix/richtext-link-at-caret
Nov 3, 2026
Merged

cixzhang merged 3 commits into
mainfrom
fix/richtext-link-at-caret

Conversation

@cixzhang

@cixzhang cixzhang commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

User impact

In a RichTextEditor with the stock toolbar, a person who pressed Insert link (or Cmd/Ctrl+K) with nothing selected, entered a URL and pressed Add link got the wrong result, and the dialog closed as if it had worked:

  • in an empty paragraph, no link at all;
  • with the caret inside text, the whole text around the caret linked.

Now, with a collapsed selection outside a link, Insert link inserts the entered URL as link text at the caret. With a non-empty selection it links the selected text, and with the caret inside an existing link, Update link changes that link, both as before.

Problem and solution fit

  • Cause: applyLinkValue dispatched TOGGLE_LINK_COMMAND first, and fell back to inserting a link at a collapsed selection only when nothing handled the command. RichTextEditor mounts LinkPlugin, which handles that command for every selection, so the fallback never ran.
  • Fix: one editor.update restores the saved selection, decides, and either inserts the link at a caret outside a link or dispatches the toggle command. (Inside a command listener such as Cmd/Ctrl+K, Lexical queues updates, so a decision made in one update can't steer code after it.) The toggle command still handles a non-empty selection, a caret inside a link (it updates that link rather than splitting it), and link removal.

Current specification

The toolbar's documented behavior ("the form can add, update, or remove a link") is a released, documented interaction under spec:AST-017 FR3, and the toolbar's own collapsed-selection fallback shows the intent. RichText has no component record, so per the ruling the behavior goes in the hasLink JSDoc ("With a collapsed selection, Insert link inserts the entered URL as link text at the caret; with a non-empty selection it links the selected text") and in the changeset. No records change.

Evidence

  • Red-first: five RichTextEditor.test.tsx cases, "with a collapsed selection".
    • An empty paragraph and a caret inside "hello world" fail on main and pass here.
    • A non-empty selection passes on both.
    • "inserts the link at a caret from Cmd/Ctrl+K with promptForUrl" fails on the previous head, which also linked the whole text node, and passes here.
    • "updates the link around a caret instead of splitting it" (Edit link, then Update link with the caret inside "docs") guards the existing update path. It failed on this PR's first head, which split the link.
  • RichText suites: 610 passed. tsc and strict lint: clean.
  • Real Chrome: story With Toolbar, Cmd/Ctrl+K, URL https://example.com, Add link:
Case main This branch
Empty paragraph no link <a href="https://example.com/">https://example.com</a>
Caret before "world" in "hello world" the whole "hello world" linked "hello ", the new link, "world"

Scope

  • One change: Insert link at a collapsed selection inserts the link at the caret.
  • No record change; the JSDoc states the behavior.
  • Public text contains no internal context.

… the link at the caret

applyLinkValue dispatched TOGGLE_LINK_COMMAND first and fell back to
inserting a link at a collapsed selection only when nothing handled it.
RichTextEditor mounts LinkPlugin, which handles that command for every
selection, so the fallback never ran: an empty paragraph got no link, and
a caret inside text linked the whole text node, while the dialog closed as
if it worked. The collapsed-selection branch now runs first, and the toggle
handles a non-empty selection as before. The hasLink JSDoc states the
behavior; tests cover an empty paragraph, a caret inside text and a
selection.
@vercel

vercel Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 3, 2026 5:32am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 3, 2026
@astracat-bot

astracat-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 3, 2026, 5:45 AM UTC

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 1c73997

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
richtext/RichTextEditorToolbar 219.8 kB (+36 B) · 454 B CSS (0) 873 (+3) — · ✓ · ✓

Package size: @astryxdesign/richtext 191.1 kB packed (+519 B); no new dependencies.

API changes: none in this pull request

Committed API report behind this build: core 18 rows; run pnpm api:report.

Touched components checked; add ci:run-visual, ci:run-a11y or ci:run-rtl to run the full suites.


Generated by PR Enrichment workflow | View full report

@astracat-bot astracat-bot Bot 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.

This fixes a real bug, but approval waits on an engineering owner's decision about the new behavior and on three changes.

Owner decision: no current spec says what Insert link does with a collapsed selection, so inserting the entered URL as link text at the caret is new documented behavior. An engineering owner needs to provide or identify the governing spec for it, or record the decision by commenting /approve-spec <full head SHA> on this pull request; the author may do so if they are an engineering owner.

Required changes:

  • With the caret inside an existing link, Edit link then Update link now splits that link around a new link whose text is the new URL instead of updating its URL, so skip the caret-insert branch inside a link and add a test for that case.
  • With a promptForUrl override, Cmd/Ctrl+K at a caret still links the whole text and now also inserts the URL into it, because the shortcut runs inside a Lexical command where editor.update is queued, so make the insert-or-toggle decision inside one update and add a Cmd/Ctrl+K test for it.
  • The changeset's first sentence, "Insert link with nothing selected added no link.", is wrong for a caret inside text, where the whole text node was linked, so reword it, for example to "did not insert a link at the caret".

The new tests reproduce the original bug and pass on this branch.

[Automated review]

let isInsertedAtCaret = false;
editor.update(() => {
const selection = $getSelection();
if ($isRangeSelection(selection) && selection.isCollapsed()) {

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.

Blocking: a caret inside an existing link is also a collapsed selection, so Edit link then Update link now lands here. The link splits around a new link whose text is the new URL, and the old URL stays on both halves; before this change the toggle command updated that link's URL. Take this branch only when the caret is not inside a link, for example by checking $isLinkNode(node) || $isLinkNode(node.getParent()) on the anchor node as readLinkContext does, and add a test for Update link at a caret inside a link.

selection.insertNodes([linkNode]);
}
});
if (!isInsertedAtCaret) {

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.

Blocking: this flag assumes the editor.update above has already run. Cmd/Ctrl+K calls toggleLink from inside the KEY_DOWN_COMMAND listener, where Lexical queues nested updates, so with a promptForUrl override the flag is still false here: the toggle links the whole text node, then the queued insert adds the URL inside it ("hello world" becomes one link over "hello example.comworld"). Dispatch the toggle from inside the same editor.update callback instead, and add a Ctrl+K test with promptForUrl at a caret.

Comment on lines +703 to +706
// With a collapsed selection there is no text to link, so the entered
// URL goes in at the caret as the link's text. This runs before the
// toggle command: LinkPlugin handles that command for every selection,
// and at a caret it would link the whole text node around it.

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.

Non-blocking (comment style): the first sentence restates the branch below, and the second runs 26 words with two topics. Suggested:

// At a caret, LinkPlugin links the whole text node around it.
// Insert the URL as a new link instead.

Once the caret-in-link case is fixed, add: "Inside a link, the toggle command updates that link."

Comment thread .changeset/richtext-link-at-caret.md Outdated
'@astryxdesign/richtext': patch
---

[fix] RichTextEditorToolbar: Insert link with nothing selected added no link. In an empty paragraph nothing was inserted, and with the caret inside text the whole text around it was linked, while the dialog closed as if it had worked. Now, with a collapsed selection, Insert link inserts the entered URL as link text at the caret; with a non-empty selection it links the selected text, as before.

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.

Blocking: "Insert link with nothing selected added no link." holds only for an empty paragraph; with the caret inside text the whole text node was linked, as the next sentence says. Suggested: "Insert link with nothing selected did not insert a link at the caret."

@cixzhang
cixzhang marked this pull request as ready for review November 3, 2026 03:52
CodeBunny on #9668: the caret insert also ran inside an existing link, so
Update link split it. The insert now runs only for a caret outside a link;
a caret inside one keeps TOGGLE_LINK_COMMAND, which updates the link.
@cixzhang

cixzhang commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 68dbb11

@astracat-bot astracat-bot Bot 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.

The caret insert now has an engineering owner's approval, and Update link inside an existing link works again; two changes remain before this can be approved.

  • With promptForUrl, Cmd/Ctrl+K at a caret inside text still links the whole text node, because Lexical queues the inserting editor.update inside the keydown command listener and isInsertedAtCaret is still false when it is read; restore the selection, decide, and insert or dispatch TOGGLE_LINK_COMMAND inside one editor.update, and add a Cmd/Ctrl+K test with promptForUrl at a caret.
  • The changeset's first sentence, "Insert link with nothing selected added no link.", misstates the old behavior at a caret inside text, where the whole text was linked; for example: "Insert link with nothing selected did not insert a link at the caret."

The comment above the caret branch could also be shortened to its reason (optional, suggestion inline).

[Automated review]

selection.insertNodes([linkNode]);
}
});
if (!isInsertedAtCaret) {

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.

Blocking: with promptForUrl, Cmd/Ctrl+K calls toggleLink from inside the KEY_DOWN_COMMAND listener, where Lexical queues the editor.update above, so isInsertedAtCaret is still false here. The toggle then links the whole text node around the caret, and the queued insert finds its anchor inside that new link and does nothing: "hello world" becomes one link, while the Link button with the same prompt inserts at the caret. Restore the selection, decide, and insert or dispatch TOGGLE_LINK_COMMAND inside one editor.update callback (a nested dispatch runs at once), and add a Cmd/Ctrl+K test with promptForUrl at a caret inside text.

Comment thread .changeset/richtext-link-at-caret.md Outdated
'@astryxdesign/richtext': patch
---

[fix] RichTextEditorToolbar: Insert link with nothing selected added no link. In an empty paragraph nothing was inserted, and with the caret inside text the whole text around it was linked, while the dialog closed as if it had worked. Now, with a collapsed selection, Insert link inserts the entered URL as link text at the caret; with a non-empty selection it links the selected text, as before.

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.

Blocking: "Insert link with nothing selected added no link." holds only for an empty paragraph; with the caret inside text the whole text node was linked, as the next sentence says. Suggested: "Insert link with nothing selected did not insert a link at the caret."

Comment on lines +703 to +708
// With a collapsed selection outside a link there is no text to link,
// so the entered URL goes in at the caret as the link's text. This runs
// before the toggle command: LinkPlugin handles that command for every
// selection, and at a caret it would link the whole text node around
// it. A caret inside a link keeps the toggle command, which updates
// that link.

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.

Non-blocking (comment style): the first sentence restates the branch below, and the second runs 26 words with two topics. Suggested:

// At a caret, LinkPlugin links the whole text node around it.
// Insert the URL as a new link instead.
// Inside a link, the toggle command updates that link.

CodeBunny on #9668: inside a command listener (Cmd/Ctrl+K with promptForUrl)
Lexical queues an editor.update, so the caret flag was still false when read
and the toggle command also linked the whole text node. One update now
restores the selection, decides, and inserts or dispatches the toggle. The
changeset states the old behavior exactly.
@cixzhang

cixzhang commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 1c73997

@astracat-bot astracat-bot Bot 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.

This looks ready to approve. With nothing selected, Insert link now inserts the entered URL as a link at the caret on every entry path, including Cmd/Ctrl+K with promptForUrl, while linking a selection and Update link inside an existing link behave as before. The new tests fail on the old code and pass here, and the changeset now describes the old behavior accurately.

One optional cleanup: the first comment in applyLinkValue narrates the update below it; a shorter version is suggested inline.

[Automated review]

Comment on lines +703 to +705
// One update restores the selection, decides, and applies. Inside a
// command listener (Cmd/Ctrl+K) Lexical queues an update, so a flag set
// in one update cannot steer code that runs after it.

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.

Non-blocking (comment style): the first sentence restates the update below it, and the second ties the Lexical quirk to a flag the code no longer has. Suggested:

// Lexical queues a nested update inside a command listener, such as
// Cmd/Ctrl+K. Decide and apply in this one update.

@cixzhang
cixzhang merged commit 1c7150d into main Nov 3, 2026
88 of 94 checks passed
@github-actions
github-actions Bot deleted the fix/richtext-link-at-caret branch November 4, 2026 06:04
cixzhang added a commit that referenced this pull request Nov 11, 2026
Resolves RichTextEditorToolbar.tsx (the link prop's doc keeps the popover
text and adds main's #9668 caret insertion) and RichTextEditor.test.tsx
(imports from both sides). The popover's apply path is main's single
update, which inserts the URL as link text at a collapsed caret.

Co-authored-by: ernestt <[email protected]>
cixzhang added a commit that referenced this pull request Nov 13, 2026
…nk dialog with an anchored popover (#8717)

* feat(richtext): replace the toolbar link dialog with an anchored popover

RichTextEditorToolbar's Link button, and Cmd/Ctrl+K in the editor, open a
non-modal Popover anchored to the button instead of a modal Dialog. It
holds one plain-text URL field (URL keyboard, URL autocomplete, no
spellcheck, a placeholder) and Apply, Remove (when the selection is in a
link), and Cancel icon buttons with tooltips. Enter applies; Escape or
Cancel closes it and returns focus and the selection to the editor; a URL
that cannot be linked shows the field's error status and keeps focus in
the field.

The URL rules, hasNewTabLinks, promptForUrl, and the Cmd/Ctrl+K shortcut
are unchanged. RICHTEXT_ICON_KEYS gains applyLink, removeLink, and cancel
with bundled fallbacks, and the catalog gains the field's placeholder.

Co-authored-by: Emily Horsman <[email protected]>

* fix(richtext): the link popover uses shared icons, Popover focus and TextInput hints, and opens above the toolbar

- Apply and Cancel draw the shared check and close icons, so a theme
  restyles them through icons; Remove draws fixed artwork. The form adds
  no richtext:* icon key.
- The Popover's own focus handling moves focus to the URL field; the
  field's URL keyboard and spelling hints use TextInput's props.
- Cmd/Ctrl+K brings the Link button into view before the popover opens.
- The popover opens above the toolbar and flips below only when there is
  no room, so it does not cover the editor's first lines.
- A RichTextEditorToolbar record holds the link control's contract.

* fix(richtext): the link field keeps its own focus, and a missing scrollIntoView does not stop the popover

* Merge main into richtext-link-popover

Resolves RichTextEditorToolbar.tsx (the link prop's doc keeps the popover
text and adds main's #9668 caret insertion) and RichTextEditor.test.tsx
(imports from both sides). The popover's apply path is main's single
update, which inserts the URL as link text at a collapsed caret.

Co-authored-by: ernestt <[email protected]>

* docs(richtext): the link popover changeset names what ships; FR6 names the empty field and its placeholder

Apply and Cancel use the shared check and close icons and
RICHTEXT_ICON_KEYS is unchanged, so the changeset no longer claims new
icon keys. It also names the placement: above the toolbar, below only
without room.

Co-authored-by: ernestt <[email protected]>

* test(richtext): link tooltips are read from the layer when they repeat the button's name; the record's compatibility row is present tense

Button on main no longer describes itself by a tooltip that repeats its
name, so tooltipOf falls back to the tooltip layer's text.

Co-authored-by: ernestt <[email protected]>

* fix(richtext): the link popover anchors to a wrapper a press never scales

Button scales on press, so the open popover moved with a press on the
Link button it anchored to (layer-runtime INV22). It now anchors to a
wrapper around the button; Popover finds the button inside for its
handlers and ARIA.

Co-authored-by: ernestt <[email protected]>

* Merge main into richtext-link-popover

Brings in #11177 (Popover focus entry in document order).

Co-authored-by: ernestt <[email protected]>

* fix(richtext): the link popover's own focus entry focuses the URL field

With Popover's focus entry taking the first content control in document
order (#11177), the form no longer turns hasAutoFocus off and focuses
its field from an Effect. The record drops FR4's slot-system status
line and says AST-061 owns the toolbar's props; the link anchor styles
no longer split the Divider comment from its styles.

Co-authored-by: ernestt <[email protected]>

* docs(storybook): show the link popover above the toolbar, and its flip below

WithLinks gets room above the editor, so the link popover opens above
the toolbar as it does on a page. WithLinksNoRoomAbove puts the toolbar
at the top of the page, where the popover flips below it. Stories only.

Co-authored-by: ernestt <[email protected]>

* test(richtext): drop the unused afterAll import

Co-authored-by: ernestt <[email protected]>

* Merge branch 'main' into richtext-link-popover

* docs(RichTextEditorToolbar): hasLink and promptForUrl name the link popover

Co-authored-by: ernestt <[email protected]>

* Merge remote-tracking branch 'refs/remotes/l4/main-new' into lane4/x8717

Co-authored-by: Cindy Zhang <[email protected]>
Co-authored-by: cixzhang <[email protected]>
cixzhang added a commit that referenced this pull request Nov 13, 2026
…rs (#11189)

* feat(richtext): replace the toolbar link dialog with an anchored popover

RichTextEditorToolbar's Link button, and Cmd/Ctrl+K in the editor, open a
non-modal Popover anchored to the button instead of a modal Dialog. It
holds one plain-text URL field (URL keyboard, URL autocomplete, no
spellcheck, a placeholder) and Apply, Remove (when the selection is in a
link), and Cancel icon buttons with tooltips. Enter applies; Escape or
Cancel closes it and returns focus and the selection to the editor; a URL
that cannot be linked shows the field's error status and keeps focus in
the field.

The URL rules, hasNewTabLinks, promptForUrl, and the Cmd/Ctrl+K shortcut
are unchanged. RICHTEXT_ICON_KEYS gains applyLink, removeLink, and cancel
with bundled fallbacks, and the catalog gains the field's placeholder.

Co-authored-by: Emily Horsman <[email protected]>

* fix(richtext): the link popover uses shared icons, Popover focus and TextInput hints, and opens above the toolbar

- Apply and Cancel draw the shared check and close icons, so a theme
  restyles them through icons; Remove draws fixed artwork. The form adds
  no richtext:* icon key.
- The Popover's own focus handling moves focus to the URL field; the
  field's URL keyboard and spelling hints use TextInput's props.
- Cmd/Ctrl+K brings the Link button into view before the popover opens.
- The popover opens above the toolbar and flips below only when there is
  no room, so it does not cover the editor's first lines.
- A RichTextEditorToolbar record holds the link control's contract.

* fix(richtext): the link field keeps its own focus, and a missing scrollIntoView does not stop the popover

* Merge main into richtext-link-popover

Resolves RichTextEditorToolbar.tsx (the link prop's doc keeps the popover
text and adds main's #9668 caret insertion) and RichTextEditor.test.tsx
(imports from both sides). The popover's apply path is main's single
update, which inserts the URL as link text at a collapsed caret.

Co-authored-by: ernestt <[email protected]>

* docs(richtext): the link popover changeset names what ships; FR6 names the empty field and its placeholder

Apply and Cancel use the shared check and close icons and
RICHTEXT_ICON_KEYS is unchanged, so the changeset no longer claims new
icon keys. It also names the placement: above the toolbar, below only
without room.

Co-authored-by: ernestt <[email protected]>

* test(richtext): link tooltips are read from the layer when they repeat the button's name; the record's compatibility row is present tense

Button on main no longer describes itself by a tooltip that repeats its
name, so tooltipOf falls back to the tooltip layer's text.

Co-authored-by: ernestt <[email protected]>

* refactor(richtext): share the toolbar's link popover and format helpers

Behavior-neutral. The toolbar's link popover state and actions move to
useToolbarLink and ToolbarLinkPopover in ToolbarLinkPopover.tsx, with
isInsertLink. The URL form moves to linkForm.tsx as LinkForm, and the
address rule to linkEditing.ts as toLinkHref. The toolbar exports its
inline formats, their labels, the formats a selection carries, the
format command, and its glyph resolver for other RichText modules. The
package entry exports none of it.

Co-authored-by: Ernest Tien <[email protected]>

* refactor(richtext): name the system useToolbarLink's Effect syncs with

Co-authored-by: Ernest Tien <[email protected]>

* fix(richtext): the link popover anchors to a wrapper a press never scales

Button scales on press, so the open popover moved with a press on the
Link button it anchored to (layer-runtime INV22). It now anchors to a
wrapper around the button; Popover finds the button inside for its
handlers and ARIA.

Co-authored-by: ernestt <[email protected]>

* Merge richtext-link-popover (#8717) into richtext-link-extraction

Takes #8717's non-scaling wrapper around the toolbar's Link button: ToolbarLinkPopover anchors to that wrapper, and useToolbarLink's option, which scrolls the button into view, is now buttonRef.

Co-authored-by: Ernest Tien <[email protected]>

* Merge main into richtext-link-popover

Brings in #11177 (Popover focus entry in document order).

Co-authored-by: ernestt <[email protected]>

* fix(richtext): the link popover's own focus entry focuses the URL field

With Popover's focus entry taking the first content control in document
order (#11177), the form no longer turns hasAutoFocus off and focuses
its field from an Effect. The record drops FR4's slot-system status
line and says AST-061 owns the toolbar's props; the link anchor styles
no longer split the Divider comment from its styles.

Co-authored-by: ernestt <[email protected]>

* Merge richtext-link-popover (#8717) into richtext-link-extraction

Takes #8717's removal of the form's focus Effect and of hasAutoFocus={false}: Popover's focus entry focuses the URL field. In this branch that code lives in linkForm.tsx and ToolbarLinkPopover.tsx.

Co-authored-by: Ernest Tien <[email protected]>

* Merge main into richtext-link-extraction

* docs(RichTextEditorToolbar): the record names the link popover's files; comments tighten

This branch was successfully deployed

1 active deployment
Preview — 1c739974 Deployed Nov 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant