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

Fix newlines in block editor after a user mention #41749

Closed
wants to merge 3 commits into from
Closed
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/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `FormTokenField`: Do not suggest the selected one even if `{ value: string }` is passed ([#41216](https://github.com/WordPress/gutenberg/pull/41216)).
- `CustomGradientBar`: Fix insertion and control point positioning to more closely follow cursor. ([#41492](https://github.com/WordPress/gutenberg/pull/41492))
- `FormTokenField`: Added Padding to resolve close button overlap issue ([#41556](https://github.com/WordPress/gutenberg/pull/41556)).
- `Autocomplete`: Prevent autocompletion entries (such as user mentions) from breaking subsequent newlines in the same block ([#41749](https://github.com/WordPress/gutenberg/pull/41749)).

### Enhancements

Expand Down
59 changes: 33 additions & 26 deletions packages/components/src/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
useState,
useRef,
useMemo,
useCallback,
} from '@wordpress/element';
import {
ENTER,
Expand Down Expand Up @@ -183,40 +184,46 @@ function useAutocomplete( {
setAutocompleterUI( null );
}

function announce( options ) {
if ( ! debouncedSpeak ) {
return;
}
if ( !! options.length ) {
debouncedSpeak(
sprintf(
/* translators: %d: number of results. */
_n(
'%d result found, use up and down arrow keys to navigate.',
'%d results found, use up and down arrow keys to navigate.',
const announce = useCallback(
( options ) => {
if ( ! debouncedSpeak ) {
return;
}
if ( !! options.length ) {
debouncedSpeak(
sprintf(
/* translators: %d: number of results. */
_n(
'%d result found, use up and down arrow keys to navigate.',
'%d results found, use up and down arrow keys to navigate.',
options.length
),
options.length
),
options.length
),
'assertive'
);
} else {
debouncedSpeak( __( 'No results.' ), 'assertive' );
}
}
'assertive'
);
} else {
debouncedSpeak( __( 'No results.' ), 'assertive' );
}
},
[ debouncedSpeak ]
);

/**
* Load options for an autocompleter.
*
* @param {Array} options
*/
function onChangeOptions( options ) {
setSelectedIndex(
options.length === filteredOptions.length ? selectedIndex : 0
);
setFilteredOptions( options );
announce( options );
}
const onChangeOptions = useCallback(
( options ) => {
setSelectedIndex(
options.length === filteredOptions.length ? selectedIndex : 0
);
setFilteredOptions( options );
announce( options );
},
[ announce, filteredOptions.length, selectedIndex ]
);
Comment on lines +217 to +226
Copy link
Member

Choose a reason for hiding this comment

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

So... I'm starting to think about when it's better to use a useCallback vs. putting the callback in a ref.

In this case, the ref approach would be to change this

useLayoutEffect( () => {
onChangeOptions( items );
}, [ onChangeOptions, items ] );

to something like this:

		const onChangeOptionsRef = useRef( onChangeOptions );
		onChangeOptionsRef.current = onChangeOptions;

		useLayoutEffect( () => {
			onChangeOptionsRef.current( items );
		}, [ items ] );

The reason I think this approach is preferable here is because, it should be AutocompleterUI's responsibility that its own effects work correctly. If we instead address it with a useCallback in the consumer, then all potential consumers will need to do the same thing, which is hard to know about.

I might be missing something though. What do you think?


function handleKeyDown( event ) {
backspacing.current = event.keyCode === BACKSPACE;
Expand Down