Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 27 additions & 7 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
createContext,
} from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { useMergeRefs } from '@wordpress/compose';
import { useMergeRefs, useInstanceId } from '@wordpress/compose';
import {
__unstableUseRichText as useRichText,
removeFormat,
Expand Down Expand Up @@ -52,6 +52,8 @@ import { canBindBlock } from '../../hooks/use-bindings-attributes';
export const keyboardShortcutContext = createContext();
export const inputEventContext = createContext();

const instanceIdKey = Symbol( 'instanceId' );

/**
* Removes props used for the native version of RichText so that they are not
* passed to the DOM element and log warnings.
Expand Down Expand Up @@ -117,6 +119,8 @@ export function RichTextWrapper(
) {
props = removeNativeProps( props );

const instanceId = useInstanceId( RichTextWrapper );

const anchorRef = useRef();
const context = useBlockEditContext();
const { clientId, isSelected: isBlockSelected, name: blockName } = context;
Expand All @@ -139,7 +143,9 @@ export function RichTextWrapper(
isSelected =
selectionStart.clientId === clientId &&
selectionEnd.clientId === clientId &&
selectionStart.attributeKey === identifier;
( identifier
? selectionStart.attributeKey === identifier
: selectionStart[ instanceIdKey ] === instanceId );
} else if ( originalIsSelected ) {
isSelected = selectionStart.clientId === clientId;
}
Expand All @@ -153,6 +159,7 @@ export function RichTextWrapper(
const { selectionStart, selectionEnd, isSelected } = useSelect( selector, [
clientId,
identifier,
instanceId,
originalIsSelected,
isBlockSelected,
] );
Expand Down Expand Up @@ -213,6 +220,13 @@ export function RichTextWrapper(
const selection = {};
const unset = start === undefined && end === undefined;

const baseSelection = {
clientId,
[ identifier ? 'attributeKey' : instanceIdKey ]: identifier
? identifier
: instanceId,
};

if ( typeof start === 'number' || unset ) {
// If we are only setting the start (or the end below), which
// means a partial selection, and we're not updating a selection
Expand All @@ -227,8 +241,7 @@ export function RichTextWrapper(
}

selection.start = {
clientId,
attributeKey: identifier,
...baseSelection,
offset: start,
};
}
Expand All @@ -243,15 +256,22 @@ export function RichTextWrapper(
}

selection.end = {
clientId,
attributeKey: identifier,
...baseSelection,
offset: end,
};
}

selectionChange( selection );
},
[ clientId, identifier ]
[
clientId,
getBlockRootClientId,
getSelectionEnd,
getSelectionStart,
identifier,
instanceId,
selectionChange,
]
);

const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { forwardRef } from '@wordpress/element';
import { children as childrenSource } from '@wordpress/blocks';
import { useInstanceId } from '@wordpress/compose';
import { __unstableCreateElement } from '@wordpress/rich-text';
import deprecated from '@wordpress/deprecated';

Expand Down Expand Up @@ -36,12 +35,10 @@ export function withDeprecations( Component ) {
}

const NewComponent = props.multiline ? RichTextMultiline : Component;
const instanceId = useInstanceId( NewComponent );

return (
<NewComponent
{ ...props }
identifier={ props.identifier || instanceId }
value={ value }
onChange={ onChange }
ref={ ref }
Expand Down