Skip to content

Commit

Permalink
Link Shortcut: Only trigger the link shortcut if there's a text selec…
Browse files Browse the repository at this point in the history
…tion (WordPress#66056)

Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: richtabor <[email protected]>
Co-authored-by: stokesman <[email protected]>
Co-authored-by: getdave <[email protected]>
  • Loading branch information
6 people authored and karthick-murugan committed Nov 13, 2024
1 parent ab80e4f commit 66313b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
10 changes: 9 additions & 1 deletion packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,17 @@ function Edit( {
openedBy?.el?.tagName === 'A' && openedBy?.action === 'click'
);

const hasSelection = ! isCollapsed( value );

return (
<>
<RichTextShortcut type="primary" character="k" onUse={ addLink } />
{ hasSelection && (
<RichTextShortcut
type="primary"
character="k"
onUse={ addLink }
/>
) }
<RichTextShortcut
type="primaryShift"
character="k"
Expand Down
48 changes: 7 additions & 41 deletions test/e2e/specs/editor/blocks/links.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,38 +121,6 @@ test.describe( 'Links', () => {
).toHaveValue( '' );
} );

test( `can be created without any text selected`, async ( {
page,
editor,
pageUtils,
} ) => {
// Create a block with some text.
await editor.insertBlock( {
name: 'core/paragraph',
} );
await page.keyboard.type( 'This is Gutenberg: ' );

// Press Cmd+K to insert a link.
await pageUtils.pressKeys( 'primary+K' );

// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );

// Press Enter to apply the link.
await pageUtils.pressKeys( 'Enter' );

// A link with the URL as its text should have been inserted.
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: {
content:
'This is Gutenberg: <a href="https://wordpress.org/gutenberg">https://wordpress.org/gutenberg</a>',
},
},
] );
} );

test( `will automatically create a link if selected text is a valid HTTP based URL`, async ( {
page,
editor,
Expand Down Expand Up @@ -341,7 +309,7 @@ test.describe( 'Links', () => {
// Make a collapsed selection inside the link.
await pageUtils.pressKeys( 'ArrowLeft' );
await pageUtils.pressKeys( 'ArrowRight' );
await pageUtils.pressKeys( 'primary+k' );
await editor.clickBlockToolbarButton( 'Link' );

const linkPopover = LinkUtils.getLinkPopover();
await linkPopover.getByRole( 'button', { name: 'Edit' } ).click();
Expand Down Expand Up @@ -485,9 +453,8 @@ test.describe( 'Links', () => {
await pageUtils.pressKeys( 'End' );
await expect( linkPopover ).toBeHidden();

// Move the caret back into the link text and the link popover
// should not be displayed.
await pageUtils.pressKeys( 'ArrowLeft' );
// Move the caret back into and selects the link text.
await pageUtils.pressKeys( 'shiftAlt+ArrowLeft' );
await expect( linkPopover ).toBeHidden();

// Switch the Link UI into "Edit" mode via keyboard shortcut
Expand Down Expand Up @@ -1015,7 +982,6 @@ test.describe( 'Links', () => {
test( 'should not display text input when initially creating the link', async ( {
page,
editor,
pageUtils,
LinkUtils,
} ) => {
// Create a block with some text.
Expand All @@ -1024,8 +990,8 @@ test.describe( 'Links', () => {
} );
await page.keyboard.type( 'This is Gutenberg: ' );

// Press Cmd+K to insert a link.
await pageUtils.pressKeys( 'primary+k' );
// Insert a link
await editor.clickBlockToolbarButton( 'Link' );

const linkPopover = LinkUtils.getLinkPopover();

Expand All @@ -1048,8 +1014,8 @@ test.describe( 'Links', () => {
// Make a collapsed selection inside the link. This is used
// as a stress test to ensure we can find the link text from a
// collapsed RichTextValue that contains a link format.
await pageUtils.pressKeys( 'ArrowLeft' );
await pageUtils.pressKeys( 'ArrowRight' );
await pageUtils.pressKeys( 'End' );
await pageUtils.pressKeys( 'shiftAlt+ArrowLeft' );
await pageUtils.pressKeys( 'primary+k' );

const linkPopover = LinkUtils.getLinkPopover();
Expand Down

0 comments on commit 66313b5

Please sign in to comment.