Skip to content

Fix/79641 rtc cursor hidden details block - #81179

Closed
giteshsarvaiya wants to merge 17 commits into
WordPress:trunkfrom
giteshsarvaiya:fix/79641-rtc-cursor-hidden-details-block
Closed

giteshsarvaiya wants to merge 17 commits into
WordPress:trunkfrom
giteshsarvaiya:fix/79641-rtc-cursor-hidden-details-block

Conversation

@giteshsarvaiya

@giteshsarvaiya giteshsarvaiya commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Draft: work in progress for #79641.

This branch is stacked on fix/79643-rtc-awareness-mixed-block-selection (#79836), since this fix depends on that PR's text-selection logic. I'll pick this back up and move it out of draft once #79836 merges into trunk.

giteshsarvaiya and others added 17 commits July 4, 2026 17:24
…for multi-block selections

Introduces a SelectionEndpoint discriminated union to distinguish between two
kinds of selection endpoints in a multi-block selection:

- CursorEndpoint (kind: cursor): the endpoint lands inside a RichText field
  and carries a character-level Y.Text relative position. Any block type that
  has a text field with a known offset produces this shape.

- WholeBlockEndpoint (kind: whole-block): the endpoint has no character
  offset, either because the block has no RichText field (e.g. image, spacer)
  or it is selected as a whole block via keyboard block-selection mode. The
  anchor is pinned to the block slot in its parent Y.Array instead.

Previously both endpoint shapes were treated identically, which caused the
overlay to break when a selection spanned mixed block types (e.g. paragraph
to image) because the receiver tried to resolve a text cursor on a block that
has no Y.Text.
When a selection spans a list block, getBlocksBetween previously returned
both the list and each of its list items, causing every item to receive a
duplicate CSS outline on top of the one already applied to the parent.

The fix filters out any block element that is a descendant of a block
already collected, so each region is highlighted exactly once regardless
of nesting depth.
Fixes RTC selection awareness breaking when a collaborator selects across
blocks of different types (e.g. a paragraph into an image).

Before this change, multi-block selections fell through to the same code
path as single-block text selections, which tried to resolve a RichText
cursor on every endpoint. Non-text blocks have no Y.Text anchor, so the
resolution threw and the overlay rendered nothing.

Changes:
- use-render-cursors: adds a dedicated branch for SelectionInMultipleBlocks
  that resolves each endpoint independently via its kind (cursor vs
  whole-block), so mixed-type combinations resolve correctly.

- compute-selection: simplifies the multi-block path to return only the
  cursor position at the active end. Pixel-level selection rects across
  multiple blocks are replaced by a CSS outline approach (see below), so
  this function no longer needs to compute them.

- use-block-highlighting: extended to cover SelectionInMultipleBlocks.
  Every block in the selection range receives the is-collaborator-selected
  CSS class and a --collaborator-outline-color custom property. The avatar
  label is placed on the topmost block in document order only, avoiding
  one label per block per collaborator.
@
editor: mixed visual treatment for multi-block awareness (overlay + outline)

Replaces the earlier CSS-outline-only approach with a two-track visual
system that more closely mirrors how a selection looks to the person making it:

Text blocks (paragraph, heading, list, etc.):
  - Full bounding-box semi-transparent fill overlay via selectionRects
  - First block gets a full bounding-box when the selection starts at offset 0;
    partial text-level rects (cursor offset → end) when it starts mid-block
  - Middle blocks always get the full bounding-box fill
  - Last block always uses text-level rects (0 → cursor offset) so only the
    selected portion is highlighted, not the unselected tail

Non-text blocks (image, spacer, etc.):
  - CSS outline border only (is-collaborator-selected class +
    --collaborator-outline-color custom property)
  - No fill overlay so image content is not obscured

Additional fixes included:
  - Stale CSS class bug: is-collaborator-selected is now reset for every
    block on each render before being conditionally re-applied, preventing
    it from persisting when a block transitions between selection types
  - resolveTargetElement gains a richer fallback chain (contenteditable →
    common text elements p/h1-h6/etc.) so partial text rects work correctly
    even when the observer has not focused the block
  - Text block detection uses innerText.trim() instead of querying
    [contenteditable] or [data-wp-block-attribute-key], which are absent on
    blocks the observer has not clicked into
@
When a multi-block selection starts or ends inside an inner block (e.g. a
list-item inside a list), the sibling inner blocks between the endpoint and
the other selection end appeared as individual middle blocks — each getting
its own fill rect, producing a striped banded look instead of one rect
covering the whole list.

Fix: promote each selection endpoint to its nearest [data-block] ancestor
before computing rects and block highlights. Middle blocks that are
descendants of the promoted container are filtered out. When both endpoints
promote to the same container (e.g. two list-items inside one list), a
single bounding-box rect is emitted for that container.

Adds a blockContainerOf helper to cursor-dom-utils and applies the
promotion in both compute-selection and use-block-highlighting.
compareDocumentPosition returns a bitmask. When b is a following
descendant of a the result is FOLLOWING | CONTAINED_BY (4 | 16 = 20),
not 4. The strict === 4 check returned false, silently skipping the
start/end swap for any endpoint pair where one element is nested inside
the other (e.g. a list-item resolved before blockContainerOf runs).

Fix: use a bitwise AND so any value with the FOLLOWING bit set returns true.
The previous closest('[data-block]') call walked arbitrarily far up the
ancestor chain. A core/list nested inside core/group has a non-[data-block]
wrapper div between them, so closest() would skip past the list and
promote to the group — wrong block gets highlighted.

The correct scope is the immediate parentElement: if the direct parent is
itself a [data-block] element (as in list-item → list), promote; otherwise
leave the element as-is.
When an endpoint block is absent from the DOM (e.g. deleted by a
concurrent edit), the previous code set effectiveFirstEl/effectiveLastEl
to null via a ternary. null?.contains(el) evaluates to undefined and
!undefined is true, so every intermediate block passed the containment
filter — spuriously highlighting the entire selection range.

Add an early return when either endpoint element is missing. Also remove
the null-guard ternaries that are now unreachable, letting TypeScript
narrow firstEl/lastEl to HTMLElement directly.
…render-cursors

computeMultiBlockOverlayRects is a pure computation function; DOM-based
inner-block promotion (list-item → list) was an architectural mismatch.

Move the promotion step to use-render-cursors, where resolved endpoints
are built before computeSelectionVisual is called. A promote() helper
checks if the resolved element's immediate parentElement is a [data-block]
container and rewrites localClientId + nulls out richTextOffset if so.
This matches the same boundary check used by blockContainerOf.

computeMultiBlockOverlayRects now trusts that its inputs are already
container-level IDs: it drops the effectiveFirstEl/effectiveLastEl/wasPromoted
variables and uses docFirstEl/docLastEl directly. The same-container early
return becomes a simple localClientId equality check.
Clicking a collaborator's avatar in the presence list did nothing when
that user had a SelectionInMultipleBlocks selection active. The cursor
registry only held elements for cursor/single-block states; highlight
avatar elements rendered for multi-block selections were never registered,
so scrollToCursor returned false silently.

Fix: thread clientId through BlockHighlightData, wrap each highlight
Avatar in a positioned div captured via the existing setCursorRef/
cursorRefsMap pattern, and sync both cursor and highlight elements in
one unified registry effect. The pulse animation on scroll is extended
to cover block-label avatars via a shared keyframe parameterised with
--label-base-transform, keeping the CSS DRY.
Only 7 colors exist in the palette so with 8+ collaborators, users
sharing a color would have their avatar label skipped entirely. Key by
collaboratorInfo.id (the WordPress user ID) instead — consistent with
how user identity is used everywhere else in this file.
…f SelectionEndpoint = SelectionCursor | SelectionWholeBlock

CursorEndpoint and WholeBlockEndpoint duplicated SelectionCursor and
SelectionWholeBlock with a different discriminant field (kind vs type).
Reuse the existing types so SelectionEndpoint = SelectionCursor |
SelectionWholeBlock, and simplify resolveEndpoint callers to pass the
endpoint directly to resolveSelection.

Also fixes a latent type error where BlockEntry.userId was typed as
string but collaboratorInfo.id is number, and fills in the missing
clientId/userId fields on the same-container early-return path.
Adds getOrderedBlockRange() to cursor-dom-utils, consolidating the
repeated query + DOM-order normalisation + blockContainerOf promotion +
getBlocksBetween/filter pattern that existed independently in both
useBlockHighlighting and computeMultiBlockOverlayRects.

Returns { firstEl, firstId, lastEl, lastId, middleEls, sameContainer }
so callers get IDs alongside elements (no extra getAttribute calls) and
a clear sameContainer flag instead of comparing IDs manually.

getBlocksBetween and isNodeBefore are now internal helpers (no longer
exported) since all external callers have been replaced by
getOrderedBlockRange.
…ontent

A remote collaborator's cursor, text selection, or whole-block highlight
inside content hidden by a collapsed container (e.g. a closed core/details
panel or an inactive core/accordion panel) had no layout box to measure,
so the existing zero-rect fallback drew it at the collapsed wrapper's
position instead. Detect this via block-editor's isElementVisible (newly
exposed as a private API) and suppress the indicator entirely rather than
misplace it.

Fixes WordPress#79641.
@github-actions github-actions Bot added [Package] Core data /packages/core-data [Package] Editor /packages/editor [Package] Block editor /packages/block-editor labels Aug 4, 2026
@giteshsarvaiya
giteshsarvaiya deleted the fix/79641-rtc-cursor-hidden-details-block branch August 7, 2026 08:16
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] Core data /packages/core-data [Package] Editor /packages/editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant