Skip to content

Rich text: derive event handler relevance from the selection#79045

Closed
ellatrix wants to merge 2 commits into
trunkfrom
try/richtext-selection-driven
Closed

Rich text: derive event handler relevance from the selection#79045
ellatrix wants to merge 2 commits into
trunkfrom
try/richtext-selection-driven

Conversation

@ellatrix

@ellatrix ellatrix commented Jun 9, 2026

Copy link
Copy Markdown
Member

What?

Make the rich text copy, cut, paste, and selection sync handlers decide whether an event is theirs based on the current selection, instead of the focused element or the event target.

Why?

Extracted from #63671 (the reverted iOS multi-select PR): this is the behaviour-neutral, selection-driven relevance part of that change, split out on its own.

Preparation for re-enabling multi-block selection on iOS/touch. That requires the writing flow wrapper to be the editing host during selection, in which case events target the wrapper rather than the individual rich text element, so handlers keyed off event.target / activeElement would not fire. Keying off the selection is robust to which element is focused. No behaviour change today, since the element holding the selection is also the focused/target element.

How?

  1. Paste: check that the selection (anchor and focus nodes) is within the element, instead of element.contains( event.target ).
  2. Copy/cut: check selection containment and the native selection collapsed state, instead of activeElement containment and the record collapsed state.
  3. Selection sync (selectionchange): gate on selection containment instead of activeElement === element, and remove the listener on cleanup rather than inline.

The clipboard listeners are already attached at the window level, so only the relevance check changes.

Testing Instructions

  1. Select text within a paragraph, copy, and paste elsewhere. Formatting is preserved.
  2. Select text and cut. The text is removed and placed on the clipboard.
  3. Paste plain and rich content into a block.
  4. Move the caret and select text with the mouse and keyboard; the formatting toolbar and selection behave as before.

Everything should behave exactly as before.

Testing Instructions for Keyboard

As above, using Shift+Arrows to select and Ctrl/Cmd+C / X / V.

Use of AI Tools

Drafted with Claude Code (Claude Opus 4.8) and reviewed before submitting.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ellatrix <ellatrix@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added [Package] Rich text /packages/rich-text [Package] Block editor /packages/block-editor labels Jun 9, 2026
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

Size Change: +75 B (0%)

Total Size: 8.44 MB

📦 View Changed
Filename Size Change
build/scripts/block-editor/index.min.js 380 kB +31 B (+0.01%)
build/scripts/rich-text/index.min.js 14.2 kB +44 B (+0.31%)

compressed-size-action

@ellatrix ellatrix changed the title Rich text: derive clipboard handler relevance from the selection Rich text: derive event handler relevance from the selection Jun 9, 2026
ellatrix and others added 2 commits June 9, 2026 17:58
Make the copy, cut, and paste handlers check whether the current
selection is within the element, instead of relying on the focused
element or the event target. Behaviour is unchanged today; this keeps
the handlers working when the editing host is the writing flow wrapper.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Gate the selectionchange handler on selection containment instead of the
active element, so it works when the editing host is an ancestor of the
element. Remove the selectionchange listener on cleanup rather than
inline. Behaviour is unchanged today.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ellatrix ellatrix force-pushed the try/richtext-selection-driven branch from 7864803 to f350dd4 Compare June 9, 2026 15:58
@ellatrix ellatrix closed this Jun 10, 2026
@ellatrix ellatrix deleted the try/richtext-selection-driven branch June 10, 2026 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor [Package] Rich text /packages/rich-text

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant