Skip to content

Commit

Permalink
Return caret to after the link boundary so typing can continue
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryj committed Feb 9, 2024
1 parent 2865dd9 commit a48cac8
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 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 @@ -127,8 +134,18 @@ function InlineLinkUI( {
);

onChange( newValue );
// If there was no selection, move straight back to editing content flow

// 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 );
Expand Down

0 comments on commit a48cac8

Please sign in to comment.