-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Allow inserting a link with no text selected #6886
Conversation
75ff93c
to
44f29c7
Compare
editor/components/rich-text/index.js
Outdated
} ); | ||
|
||
// mceInsertLink doesn't return the anchor it creates, so query for it | ||
// using a temporary attribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stole this idea from the classic editor.
editor/components/rich-text/index.js
Outdated
} ); | ||
|
||
// Place the cursor immediately after the newly inserted link | ||
this.editor.selection.select( anchor ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be part of the transact callback?
editor/components/rich-text/index.js
Outdated
// mceInsertLink doesn't return the anchor it creates, so query for it | ||
// using a temporary attribute | ||
anchor = this.editor.getBody().querySelector( '[data-wp-temp-link]' ); | ||
anchor.removeAttribute( 'data-wp-temp-link' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite following why it is necessary to use the temporary attribute. Can't we insert the text and element at once? So I'm guessing when the selection is collapsed, we could use editor.insertContent( '<a href="link">link</a>' )
or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't having any luck with isCollapsed()
yesterday but now when I try it it's fine—amazing what some sleep can do! I've reworked this in 4a02305ff1472d58a32e14fca6abbb01b0fea827 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks for iterating on this.
When creating a link with no text selected, the link should be inserted with its text set to the URL.
We can avoid the ugly data-wp-temp-link hack by using isCollapsed() and insertContent().
Fix regression where the 'Open in new window' does not work when adding a new link.
2ceec21
to
fafe328
Compare
Thanks for reviewing! I reworked my approach in 7d35c92 and snuck in two bug fixes while I was at it:
|
Thanks for the improvement! |
Description
Closes #5960 and fixes #6900. When creating a link with no text selected, the link should be inserted with its text set to the URL.
How has this been tested?
Screenshots