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

Close link preview if collapsed selection when creating link #58896

Merged
merged 2 commits into from
Feb 12, 2024
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
13 changes: 1 addition & 12 deletions packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ function Edit( {
return;
}

// Close the Link popover if there is no active selection
// after the link was added - this can happen if the user
// adds a link without any text selected.
// We assume that if there is no active selection after
// link insertion there are no active formats.
if ( ! value.activeFormats ) {
editableContentElement.focus();
setAddingLink( false );
return;
}

function handleClick( event ) {
// There is a situation whereby there is an existing link in the rich text
// and the user clicks on the leftmost edge of that link and fails to activate
Expand All @@ -78,7 +67,7 @@ function Edit( {
return () => {
editableContentElement.removeEventListener( 'click', handleClick );
};
}, [ contentRef, isActive, addingLink, value ] );
}, [ contentRef, isActive ] );

function addLink( target ) {
const text = getTextContent( slice( value ) );
Expand Down
44 changes: 33 additions & 11 deletions packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
store as blockEditorStore,
useCachedTruthy,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -53,15 +53,22 @@ function InlineLinkUI( {
// Get the text content minus any HTML tags.
const richTextText = richLinkTextValue.text;

const { createPageEntity, userCanCreatePages } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const _settings = getSettings();

return {
createPageEntity: _settings.__experimentalCreatePageEntity,
userCanCreatePages: _settings.__experimentalUserCanCreatePages,
};
}, [] );
const { selectionChange } = useDispatch( blockEditorStore );

const { createPageEntity, userCanCreatePages, selectionStart } = useSelect(
( select ) => {
const { getSettings, getSelectionStart } =
select( blockEditorStore );
const _settings = getSettings();

return {
createPageEntity: _settings.__experimentalCreatePageEntity,
userCanCreatePages: _settings.__experimentalUserCanCreatePages,
selectionStart: getSelectionStart(),
};
},
[]
);

const linkValue = useMemo(
() => ( {
Expand Down Expand Up @@ -123,8 +130,23 @@ function InlineLinkUI( {
inserted,
linkFormat,
value.start,
value.end + newText.length
value.start + newText.length
);

onChange( newValue );

// Close the Link UI.
stopAddingLink();

// Move the selection to the end of the inserted link outside of the format boundary
// so the user can continue typing after the link.
selectionChange( {
clientId: selectionStart.clientId,
identifier: selectionStart.attributeKey,
start: value.start + newText.length + 1,
} );

return;
} else if ( newText === richTextText ) {
newValue = applyFormat( value, linkFormat );
} else {
Expand Down
Loading