Skip to content

Commit 2380f2d

Browse files
authored
RichText: don't set focus when applying format (#19536)
* Return focus to original element * Fix list e2e tests * Fix annotation e2e tests * Fix some rich text e2e tests * Fix tests * Focus after link * Add onFocus * Corrections
1 parent 0f2ec15 commit 2380f2d

File tree

13 files changed

+82
-35
lines changed

13 files changed

+82
-35
lines changed

packages/block-editor/src/components/rich-text/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ class RichTextWrapper extends Component {
411411
start={ start }
412412
reversed={ reversed }
413413
>
414-
{ ( { isSelected, value, onChange, Editable } ) =>
414+
{ ( { isSelected, value, onChange, onFocus, Editable } ) =>
415415
<>
416-
{ children && children( { value, onChange } ) }
416+
{ children && children( { value, onChange, onFocus } ) }
417417
{ isSelected && hasFormats && ( <FormatToolbarContainer inline={ inlineToolbar } anchorRef={ forwardedRef.current } /> ) }
418418
{ isSelected && <RemoveBrowserShortcuts /> }
419419
<Autocomplete

packages/block-library/src/list/edit.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function ListEdit( {
3838
const { ordered, values, type, reversed, start } = attributes;
3939
const tagName = ordered ? 'ol' : 'ul';
4040

41-
const controls = ( { value, onChange } ) => (
41+
const controls = ( { value, onChange, onFocus } ) => (
4242
<>
4343
{ ( isSelected && <>
4444
<RichTextShortcut
@@ -79,6 +79,7 @@ export default function ListEdit( {
7979
isActive: isActiveListType( value, 'ul', tagName ),
8080
onClick() {
8181
onChange( changeListType( value, { type: 'ul' } ) );
82+
onFocus();
8283

8384
if ( isListRootSelected( value ) ) {
8485
setAttributes( { ordered: false } );
@@ -91,6 +92,7 @@ export default function ListEdit( {
9192
isActive: isActiveListType( value, 'ol', tagName ),
9293
onClick() {
9394
onChange( changeListType( value, { type: 'ol' } ) );
95+
onFocus();
9496

9597
if ( isListRootSelected( value ) ) {
9698
setAttributes( { ordered: true } );
@@ -104,6 +106,7 @@ export default function ListEdit( {
104106
isDisabled: ! canOutdentListItems( value ),
105107
onClick() {
106108
onChange( outdentListItems( value ) );
109+
onFocus();
107110
},
108111
},
109112
{
@@ -113,6 +116,7 @@ export default function ListEdit( {
113116
isDisabled: ! canIndentListItems( value ),
114117
onClick() {
115118
onChange( indentListItems( value, { type: tagName } ) );
119+
onFocus();
116120
},
117121
},
118122
] }

packages/e2e-tests/specs/editor/plugins/annotations.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe( 'Using Plugins API', () => {
4949
// Click add annotation button.
5050
const addAnnotationButton = ( await page.$x( "//button[contains(text(), 'Add annotation')]" ) )[ 0 ];
5151
await addAnnotationButton.click();
52+
await page.evaluate( () => document.querySelector( '[contenteditable]' ).focus() );
5253
}
5354

5455
/**
@@ -60,6 +61,7 @@ describe( 'Using Plugins API', () => {
6061
// Click remove annotations button.
6162
const addAnnotationButton = ( await page.$x( "//button[contains(text(), 'Remove annotations')]" ) )[ 0 ];
6263
await addAnnotationButton.click();
64+
await page.evaluate( () => document.querySelector( '[contenteditable]' ).focus() );
6365
}
6466

6567
/**

packages/format-library/src/bold/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ export const bold = {
1313
title,
1414
tagName: 'strong',
1515
className: null,
16-
edit( { isActive, value, onChange } ) {
17-
const onToggle = () => onChange( toggleFormat( value, { type: name } ) );
16+
edit( { isActive, value, onChange, onFocus } ) {
17+
function onToggle() {
18+
onChange( toggleFormat( value, { type: name } ) );
19+
}
20+
21+
function onClick() {
22+
onToggle();
23+
onFocus();
24+
}
1825

1926
return (
2027
<>
@@ -27,7 +34,7 @@ export const bold = {
2734
name="bold"
2835
icon="editor-bold"
2936
title={ title }
30-
onClick={ onToggle }
37+
onClick={ onClick }
3138
isActive={ isActive }
3239
shortcutType="primary"
3340
shortcutCharacter="b"

packages/format-library/src/code/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ export const code = {
4343

4444
return value;
4545
},
46-
edit( { value, onChange, isActive } ) {
47-
const onToggle = () => onChange( toggleFormat( value, { type: name } ) );
46+
edit( { value, onChange, onFocus, isActive } ) {
47+
function onClick() {
48+
onChange( toggleFormat( value, { type: name } ) );
49+
onFocus();
50+
}
4851

4952
return (
5053
<RichTextToolbarButton
5154
icon="editor-code"
5255
title={ title }
53-
onClick={ onToggle }
56+
onClick={ onClick }
5457
isActive={ isActive }
5558
/>
5659
);

packages/format-library/src/image/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const image = {
8585
}
8686

8787
render() {
88-
const { value, onChange, isObjectActive, activeObjectAttributes } = this.props;
88+
const { value, onChange, onFocus, isObjectActive, activeObjectAttributes } = this.props;
8989

9090
return (
9191
<MediaUploadCheck>
@@ -108,6 +108,7 @@ export const image = {
108108
alt,
109109
},
110110
} ) );
111+
onFocus();
111112
} }
112113
onClose={ this.closeModal }
113114
render={ ( { open } ) => {

packages/format-library/src/italic/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ export const italic = {
1313
title,
1414
tagName: 'em',
1515
className: null,
16-
edit( { isActive, value, onChange } ) {
17-
const onToggle = () => onChange( toggleFormat( value, { type: name } ) );
16+
edit( { isActive, value, onChange, onFocus } ) {
17+
function onToggle() {
18+
onChange( toggleFormat( value, { type: name } ) );
19+
}
20+
21+
function onClick() {
22+
onToggle();
23+
onFocus();
24+
}
1825

1926
return (
2027
<>
@@ -27,7 +34,7 @@ export const italic = {
2734
name="italic"
2835
icon="editor-italic"
2936
title={ title }
30-
onClick={ onToggle }
37+
onClick={ onClick }
3138
isActive={ isActive }
3239
shortcutType="primary"
3340
shortcutCharacter="i"

packages/format-library/src/link/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const link = {
9191
}
9292

9393
render() {
94-
const { isActive, activeAttributes, value, onChange } = this.props;
94+
const { isActive, activeAttributes, value, onChange, onFocus } = this.props;
9595

9696
return (
9797
<>
@@ -131,6 +131,7 @@ export const link = {
131131
activeAttributes={ activeAttributes }
132132
value={ value }
133133
onChange={ onChange }
134+
onFocus={ onFocus }
134135
/>
135136
</>
136137
);

packages/format-library/src/link/inline.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class InlineLinkUI extends Component {
135135
}
136136

137137
submitLink( event ) {
138-
const { isActive, value, onChange, speak } = this.props;
138+
const { isActive, value, onChange, onFocus, speak } = this.props;
139139
const { inputValue, opensInNewWindow } = this.state;
140140
const url = prependHTTP( inputValue );
141141
const selectedText = getTextContent( slice( value ) );
@@ -154,6 +154,8 @@ class InlineLinkUI extends Component {
154154
onChange( applyFormat( value, format ) );
155155
}
156156

157+
onFocus();
158+
157159
this.resetState();
158160

159161
if ( ! isValidHref( url ) ) {

packages/format-library/src/strikethrough/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ export const strikethrough = {
1313
title,
1414
tagName: 's',
1515
className: null,
16-
edit( { isActive, value, onChange } ) {
17-
const onToggle = () => onChange( toggleFormat( value, { type: name } ) );
16+
edit( { isActive, value, onChange, onFocus } ) {
17+
function onClick() {
18+
onChange( toggleFormat( value, { type: name } ) );
19+
onFocus();
20+
}
1821

1922
return (
2023
<RichTextToolbarButton
2124
icon="editor-strikethrough"
2225
title={ title }
23-
onClick={ onToggle }
26+
onClick={ onClick }
2427
isActive={ isActive }
2528
/>
2629
);

0 commit comments

Comments
 (0)