Skip to content

Commit d1155e0

Browse files
jeryjyouknowriad
authored andcommitted
Close link preview if collapsed selection when creating link (#58896)
Return caret to after the link boundary so typing can continue if no text is selected when creating a link.
1 parent 5f03c58 commit d1155e0

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ function Edit( {
4848
return;
4949
}
5050

51-
// Close the Link popover if there is no active selection
52-
// after the link was added - this can happen if the user
53-
// adds a link without any text selected.
54-
// We assume that if there is no active selection after
55-
// link insertion there are no active formats.
56-
if ( ! value.activeFormats ) {
57-
editableContentElement.focus();
58-
setAddingLink( false );
59-
return;
60-
}
61-
6251
function handleClick( event ) {
6352
// There is a situation whereby there is an existing link in the rich text
6453
// and the user clicks on the leftmost edge of that link and fails to activate
@@ -78,7 +67,7 @@ function Edit( {
7867
return () => {
7968
editableContentElement.removeEventListener( 'click', handleClick );
8069
};
81-
}, [ contentRef, isActive, addingLink, value ] );
70+
}, [ contentRef, isActive ] );
8271

8372
function addLink( target ) {
8473
const text = getTextContent( slice( value ) );

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
__experimentalLinkControl as LinkControl,
2323
store as blockEditorStore,
2424
} from '@wordpress/block-editor';
25-
import { useSelect } from '@wordpress/data';
25+
import { useDispatch, useSelect } from '@wordpress/data';
2626

2727
/**
2828
* Internal dependencies
@@ -52,15 +52,22 @@ function InlineLinkUI( {
5252
// Get the text content minus any HTML tags.
5353
const richTextText = richLinkTextValue.text;
5454

55-
const { createPageEntity, userCanCreatePages } = useSelect( ( select ) => {
56-
const { getSettings } = select( blockEditorStore );
57-
const _settings = getSettings();
58-
59-
return {
60-
createPageEntity: _settings.__experimentalCreatePageEntity,
61-
userCanCreatePages: _settings.__experimentalUserCanCreatePages,
62-
};
63-
}, [] );
55+
const { selectionChange } = useDispatch( blockEditorStore );
56+
57+
const { createPageEntity, userCanCreatePages, selectionStart } = useSelect(
58+
( select ) => {
59+
const { getSettings, getSelectionStart } =
60+
select( blockEditorStore );
61+
const _settings = getSettings();
62+
63+
return {
64+
createPageEntity: _settings.__experimentalCreatePageEntity,
65+
userCanCreatePages: _settings.__experimentalUserCanCreatePages,
66+
selectionStart: getSelectionStart(),
67+
};
68+
},
69+
[]
70+
);
6471

6572
const linkValue = useMemo(
6673
() => ( {
@@ -122,8 +129,23 @@ function InlineLinkUI( {
122129
inserted,
123130
linkFormat,
124131
value.start,
125-
value.end + newText.length
132+
value.start + newText.length
126133
);
134+
135+
onChange( newValue );
136+
137+
// Close the Link UI.
138+
stopAddingLink();
139+
140+
// Move the selection to the end of the inserted link outside of the format boundary
141+
// so the user can continue typing after the link.
142+
selectionChange( {
143+
clientId: selectionStart.clientId,
144+
identifier: selectionStart.attributeKey,
145+
start: value.start + newText.length + 1,
146+
} );
147+
148+
return;
127149
} else if ( newText === richTextText ) {
128150
newValue = applyFormat( value, linkFormat );
129151
} else {

0 commit comments

Comments
 (0)