Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2873f86
core-data: add CursorEndpoint/WholeBlockEndpoint discriminated union …
giteshsarvaiya Jul 2, 2026
4a8713c
editor: fix getBlocksBetween to skip blocks already covered by a parent
giteshsarvaiya Jul 2, 2026
f615913
editor: handle SelectionInMultipleBlocks in the collaborators overlay
giteshsarvaiya Jul 2, 2026
ef1fbd2
@
giteshsarvaiya Jul 4, 2026
b73a353
editor: promote inner blocks to container for multi-block highlights
giteshsarvaiya Jul 5, 2026
7b05075
editor: fix isNodeBefore bitmask check for nested-block endpoints
giteshsarvaiya Jul 8, 2026
faa734f
editor: fix blockContainerOf to promote only direct-parent containers
giteshsarvaiya Jul 8, 2026
e5b6d9a
editor: guard against missing endpoint elements in block highlighting
giteshsarvaiya Jul 8, 2026
02a56f0
editor: move inner-block promotion out of compute-selection into use-…
giteshsarvaiya Jul 8, 2026
3f58832
Merge branch 'trunk' into fix/79643-rtc-awareness-mixed-block-selection
giteshsarvaiya Jul 13, 2026
48c9915
Merge branch 'trunk' into fix/79643-rtc-awareness-mixed-block-selection
giteshsarvaiya Jul 31, 2026
2ddb212
Merge branch 'trunk' into fix/79643-rtc-awareness-mixed-block-selection
giteshsarvaiya Jul 31, 2026
b1ca76e
editor: fix scroll-to-avatar for multi-block selections
giteshsarvaiya Aug 1, 2026
2afd8af
editor: key usersWithAvatar by user ID instead of color
giteshsarvaiya Aug 1, 2026
e92e530
core-data, editor: drop CursorEndpoint/WholeBlockEndpoint in favour o…
giteshsarvaiya Aug 1, 2026
71cf639
editor: extract shared block-range DOM logic into getOrderedBlockRange
giteshsarvaiya Aug 3, 2026
a9f5c1e
Merge branch 'trunk' into fix/79643-rtc-awareness-mixed-block-selection
giteshsarvaiya Aug 3, 2026
df5bf45
Merge branch 'trunk' into fix/79643-rtc-awareness-mixed-block-selection
alecgeatches Aug 3, 2026
52b34df
Merge branch 'trunk' into fix/79643-rtc-awareness-mixed-block-selection
alecgeatches Aug 3, 2026
ca5b5d7
Merge branch 'trunk' into fix/79643-rtc-awareness-mixed-block-selection
giteshsarvaiya Aug 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/core-data/src/awareness/post-editor-awareness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,18 @@ export class PostEditorAwareness extends BaseAwarenessState< PostEditorState > {
};
}

// SelectionInMultipleBlocks is decomposed by the caller into per-endpoint
// Cursor / WholeBlock calls and should never arrive here directly.
if ( selection.type === SelectionType.SelectionInMultipleBlocks ) {
return {
richTextOffset: null,
localClientId: null,
attributeKey: null,
};
}

// Text-based selections: resolve cursor position and navigate up.
// SelectionCursor → cursorPosition; SelectionInOneBlock → cursorStartPosition.
const cursorPos =
'cursorPosition' in selection
? selection.cursorPosition
Expand Down
19 changes: 14 additions & 5 deletions packages/core-data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,22 @@ export type SelectionInOneBlock = {
selectionDirection?: SelectionDirection;
};

/**
* One end of a multi-block selection. Uses SelectionCursor when the endpoint
* lands inside a RichText field (character-level anchor) or SelectionWholeBlock
* when the entire block is selected (block-slot anchor). The type discriminant
* tells the receiver how to resolve this endpoint.
*/
export type SelectionEndpoint = SelectionCursor | SelectionWholeBlock;

export type SelectionInMultipleBlocks = {
// The user has highlighted text over multiple blocks.
// The blocks are derived on the receiver side by navigating up from the
// resolved cursor positions via Y.AbstractType.parent.
// The user's selection spans more than one block. Each endpoint is resolved
// independently: text blocks carry a character-level CursorEndpoint while
// blocks without a RichText field (or selected as whole blocks) carry a
// WholeBlockEndpoint. Mixed combinations (e.g. paragraph → image) are valid.
type: SelectionType.SelectionInMultipleBlocks;
cursorStartPosition: CursorPosition;
cursorEndPosition: CursorPosition;
startEndpoint: SelectionEndpoint;
endEndpoint: SelectionEndpoint;
// The direction of the selection, indicating where the caret sits.
selectionDirection?: SelectionDirection;
};
Expand Down
99 changes: 84 additions & 15 deletions packages/core-data/src/utils/crdt-user-selections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
SelectionInMultipleBlocks,
SelectionWholeBlock,
CursorPosition,
SelectionEndpoint,
} from '../types';

/**
Expand Down Expand Up @@ -145,18 +146,20 @@ export function getSelectionState(
};
}

// Case 5: Selection in multiple blocks
const cursorStartPosition = getCursorPosition( selectionStart, yBlocks );
const cursorEndPosition = getCursorPosition( selectionEnd, yBlocks );
if ( ! cursorStartPosition || ! cursorEndPosition ) {
// If we can't find the cursor positions in block text, treat it as a non-selection.
// Case 5: Selection in multiple blocks.
// Each endpoint is resolved independently — text fields get a CursorEndpoint
// (character-level anchor), everything else gets a WholeBlockEndpoint
// (block-slot anchor). Mixed combinations across block types are valid.
const startEndpoint = getSelectionEndpoint( selectionStart, yBlocks );
const endEndpoint = getSelectionEndpoint( selectionEnd, yBlocks );
if ( ! startEndpoint || ! endEndpoint ) {
return noSelection;
}

return {
type: SelectionType.SelectionInMultipleBlocks,
cursorStartPosition,
cursorEndPosition,
startEndpoint,
endEndpoint,
selectionDirection,
};
}
Expand Down Expand Up @@ -207,6 +210,42 @@ function getCursorPosition(
};
}

/**
* Build a SelectionEndpoint for one side of a multi-block selection.
*
* WHY two shapes: Yjs relative positions must be anchored to the right node
* type. If the block has a RichText field with a known character offset, we
* anchor to a Y.Text position (CursorEndpoint) so the cursor survives
* concurrent text edits. If there is no text offset (whole-block selection,
* or a block type with no RichText), we anchor to the block's index slot in
* its parent Y.Array (WholeBlockEndpoint) so it survives block insertions and
* deletions above or below it.
*
* @param selection - The WPBlockSelection for this side.
* @param blocks - The root-level Yjs blocks array.
* @return A SelectionEndpoint, or null if the block cannot be located in Yjs.
*/
function getSelectionEndpoint(
selection: WPBlockSelection,
blocks: YBlocks
): SelectionEndpoint | null {
const cursorPosition = getCursorPosition( selection, blocks );
if ( cursorPosition ) {
return { type: SelectionType.Cursor, cursorPosition };
}

// No character offset available — anchor to the block's slot in its
// parent Y.Array instead.
const path = getBlockPathForLocalClientId( selection.clientId );
const blockPosition = path
? createRelativePositionForBlockPath( path, blocks )
: null;
if ( blockPosition ) {
return { type: SelectionType.WholeBlock, blockPosition };
}
return null;
}

/**
* Resolves a local block-editor clientId to its index path relative to the
* post content blocks. This allows finding the corresponding block in the Yjs
Expand Down Expand Up @@ -349,15 +388,13 @@ export function areSelectionsStatesEqual(

case SelectionType.SelectionInMultipleBlocks:
return (
areCursorPositionsEqual(
selection1.cursorStartPosition,
( selection2 as SelectionInMultipleBlocks )
.cursorStartPosition
areEndpointsEqual(
selection1.startEndpoint,
( selection2 as SelectionInMultipleBlocks ).startEndpoint
) &&
areCursorPositionsEqual(
selection1.cursorEndPosition,
( selection2 as SelectionInMultipleBlocks )
.cursorEndPosition
areEndpointsEqual(
selection1.endEndpoint,
( selection2 as SelectionInMultipleBlocks ).endEndpoint
) &&
selection1.selectionDirection ===
( selection2 as SelectionInMultipleBlocks )
Expand All @@ -374,6 +411,38 @@ export function areSelectionsStatesEqual(
}
}

/**
* Check if two SelectionEndpoints are equal.
*
* Endpoints of different kinds (cursor vs whole-block) are never equal.
*
* @param ep1 - First endpoint.
* @param ep2 - Second endpoint.
* @return True if the endpoints represent the same position.
*/
function areEndpointsEqual(
ep1: SelectionEndpoint,
ep2: SelectionEndpoint
): boolean {
if ( ep1.type !== ep2.type ) {
return false;
}
if (
ep1.type === SelectionType.Cursor &&
ep2.type === SelectionType.Cursor
) {
return areCursorPositionsEqual(
ep1.cursorPosition,
ep2.cursorPosition
);
}
// Both SelectionType.WholeBlock.
return Y.compareRelativePositions(
( ep1 as SelectionWholeBlock ).blockPosition,
( ep2 as SelectionWholeBlock ).blockPosition
);
}

/**
* Check if two cursor positions are equal.
*
Expand Down
128 changes: 107 additions & 21 deletions packages/core-data/src/utils/test/crdt-user-selections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,55 +269,136 @@ describe( 'areSelectionsStatesEqual', () => {
} );

describe( 'SelectionType.SelectionInMultipleBlocks', () => {
test( 'returns true when selections in multiple blocks are identical', () => {
const cursorStartPosition = createCursorPosition(
'first block',
5
test( 'returns true when both endpoints are identical cursor endpoints', () => {
const startEndpoint: SelectionCursor = {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'first block', 5 ),
};
const endEndpoint: SelectionCursor = {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'second block', 3 ),
};
const selection1: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
startEndpoint,
endEndpoint,
};
const selection2: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
startEndpoint,
endEndpoint,
};

expect( areSelectionsStatesEqual( selection1, selection2 ) ).toBe(
true
);
const cursorEndPosition = createCursorPosition( 'second block', 3 );
} );

test( 'returns true when both endpoints are identical whole-block endpoints', () => {
const startEndpoint: SelectionWholeBlock = {
type: SelectionType.WholeBlock,
blockPosition: createBlockPosition( 0 ),
};
const endEndpoint: SelectionWholeBlock = {
type: SelectionType.WholeBlock,
blockPosition: createBlockPosition( 1 ),
};
const selection1: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
cursorStartPosition,
cursorEndPosition,
startEndpoint,
endEndpoint,
};
const selection2: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
cursorStartPosition,
cursorEndPosition,
startEndpoint,
endEndpoint,
};

expect( areSelectionsStatesEqual( selection1, selection2 ) ).toBe(
true
);
} );

test( 'returns false when start cursor position differs', () => {
test( 'returns false when start cursor endpoint position differs', () => {
const selection1: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
cursorStartPosition: createCursorPosition( 'first block', 5 ),
cursorEndPosition: createCursorPosition( 'second block', 3 ),
startEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'first block', 5 ),
},
endEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'second block', 3 ),
},
};
const selection2: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
cursorStartPosition: createCursorPosition( 'first block', 6 ),
cursorEndPosition: createCursorPosition( 'second block', 3 ),
startEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'first block', 6 ),
},
endEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'second block', 3 ),
},
};

expect( areSelectionsStatesEqual( selection1, selection2 ) ).toBe(
false
);
} );

test( 'returns false when end cursor position differs', () => {
test( 'returns false when end cursor endpoint position differs', () => {
const selection1: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
cursorStartPosition: createCursorPosition( 'first block', 5 ),
cursorEndPosition: createCursorPosition( 'second block', 3 ),
startEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'first block', 5 ),
},
endEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'second block', 3 ),
},
};
const selection2: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
cursorStartPosition: createCursorPosition( 'first block', 5 ),
cursorEndPosition: createCursorPosition( 'second block', 4 ),
startEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'first block', 5 ),
},
endEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'second block', 4 ),
},
};

expect( areSelectionsStatesEqual( selection1, selection2 ) ).toBe(
false
);
} );

test( 'returns false when endpoint types differ (text vs block)', () => {
const selection1: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
startEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'first block', 5 ),
},
endEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'second block', 3 ),
},
};
const selection2: SelectionInMultipleBlocks = {
type: SelectionType.SelectionInMultipleBlocks,
startEndpoint: {
type: SelectionType.WholeBlock,
blockPosition: createBlockPosition( 0 ),
},
endEndpoint: {
type: SelectionType.Cursor,
cursorPosition: createCursorPosition( 'second block', 3 ),
},
};

expect( areSelectionsStatesEqual( selection1, selection2 ) ).toBe(
Expand Down Expand Up @@ -732,12 +813,17 @@ describe( 'getSelectionState', () => {
expect( result.type ).toBe(
SelectionType.SelectionInMultipleBlocks
);
const multiBlock = result as SelectionInMultipleBlocks;
expect( multiBlock.startEndpoint.type ).toBe(
SelectionType.Cursor
);
expect( multiBlock.endEndpoint.type ).toBe( SelectionType.Cursor );
expect(
( result as SelectionInMultipleBlocks ).cursorStartPosition
( multiBlock.startEndpoint as SelectionCursor ).cursorPosition
.absoluteOffset
).toBe( 5 );
expect(
( result as SelectionInMultipleBlocks ).cursorEndPosition
( multiBlock.endEndpoint as SelectionCursor ).cursorPosition
.absoluteOffset
).toBe( 3 );
} );
Expand Down
Loading
Loading