Skip to content

Commit

Permalink
Insert post title instead of URL, when adding a link to an existing p…
Browse files Browse the repository at this point in the history
…ost (#21240)

* Fix for #11930
Make the link anchor also retrieve the title of the post/page

* Fix long titles
Also made code a bit more readable

* Add test to check link to existing post

* small change in naming
  • Loading branch information
pwkip authored Apr 1, 2020
1 parent a60554d commit 0b3f6ef
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
36 changes: 36 additions & 0 deletions packages/e2e-tests/specs/editor/various/links.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@ describe( 'Links', () => {
);
};

it( 'will use Post title as link text if link to existing post is created without any text selected', async () => {
const titleText = 'Post to create a link to';
await createPostWithTitle( titleText );

await createNewPost();
await clickBlockAppender();

// Now in a new post and try to create a link from an autocomplete suggestion using the keyboard.
await page.keyboard.type( 'Here comes a link: ' );

// Press Cmd+K to insert a link
await pressKeyWithModifier( 'primary', 'K' );

// Wait for the URL field to auto-focus
await waitForAutoFocus();
expect(
await page.$(
'.components-popover__content .block-editor-link-control'
)
).not.toBeNull();

// Trigger the autocomplete suggestion list and select the first suggestion.
await page.keyboard.type( titleText.substr( 0, titleText.length - 2 ) );
await page.waitForSelector( '.block-editor-link-control__search-item' );
await page.keyboard.press( 'ArrowDown' );

await page.keyboard.press( 'Enter' );

const actualText = await page.evaluate(
() =>
document.querySelector( '.block-editor-rich-text__editable a' )
.textContent
);
expect( actualText ).toBe( titleText );
} );

it( 'can be created by selecting text and clicking Link', async () => {
// Create a block with some text
await clickBlockAppender();
Expand Down
5 changes: 3 additions & 2 deletions packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ function InlineLinkUI( {
} );

if ( isCollapsed( value ) && ! isActive ) {
const newText = nextValue.title || newUrl;
const toInsert = applyFormat(
create( { text: newUrl } ),
create( { text: newText } ),
format,
0,
newUrl.length
newText.length
);
onChange( insert( value, toInsert ) );
} else {
Expand Down

0 comments on commit 0b3f6ef

Please sign in to comment.