Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote v2: retain selection after transform #39838

Merged
merged 2 commits into from
Mar 30, 2022
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
1 change: 1 addition & 0 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ function RichTextWrapper(
__unstableAllowPrefixTransformations,
formatTypes,
onReplace,
selectionChange,
} ),
useRemoveBrowserShortcuts(),
useShortcuts( keyboardShortcuts ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* External dependencies
*/
import { findKey } from 'lodash';

/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
import { slice, toHTMLString } from '@wordpress/rich-text';
import { insert, toHTMLString } from '@wordpress/rich-text';
import { getBlockTransforms, findTransform } from '@wordpress/blocks';
import { useDispatch } from '@wordpress/data';

Expand All @@ -13,6 +18,37 @@ import { useDispatch } from '@wordpress/data';
import { store as blockEditorStore } from '../../store';
import { preventEventDiscovery } from './prevent-event-discovery';

// A robust way to retain selection position through various
// transforms is to insert a special character at the position and
// then recover it.
const START_OF_SELECTED_AREA = '\u0086';

function findSelection( blocks ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is the only use case for nested blocks, there may be value in reusing this function in other places where we use the START_OF_SELECTED_AREA trick.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took advantage of this and abstracted it into a reusable function while removing Lodash in #41806. Would love to get your feedback there when you get a chance @ellatrix.

let i = blocks.length;

while ( i-- ) {
const attributeKey = findKey(
blocks[ i ].attributes,
( v ) =>
typeof v === 'string' &&
v.indexOf( START_OF_SELECTED_AREA ) !== -1
);

if ( attributeKey ) {
blocks[ i ].attributes[ attributeKey ] = blocks[ i ].attributes[
attributeKey
].replace( START_OF_SELECTED_AREA, '' );
return blocks[ i ].clientId;
}

const nestedSelection = findSelection( blocks[ i ].innerBlocks );

if ( nestedSelection ) {
return nestedSelection;
}
}
}

export function useInputRules( props ) {
const {
__unstableMarkLastChangeAsPersistent,
Expand All @@ -22,7 +58,7 @@ export function useInputRules( props ) {
propsRef.current = props;
return useRefEffect( ( element ) => {
function inputRule() {
const { value, onReplace } = propsRef.current;
const { value, onReplace, selectionChange } = propsRef.current;

if ( ! onReplace ) {
return;
Expand Down Expand Up @@ -52,10 +88,11 @@ export function useInputRules( props ) {
}

const content = toHTMLString( {
value: slice( value, start, text.length ),
value: insert( value, START_OF_SELECTED_AREA, 0, start ),
} );
const block = transformation.transform( content );

selectionChange( findSelection( [ block ] ) );
onReplace( [ block ] );
__unstableMarkAutomaticChange();
}
Expand Down