-
Notifications
You must be signed in to change notification settings - Fork 4k
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
External link component: Add a check for on page anchor links #42259
Conversation
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.
Thanks for this PR. It makes sense to catch this situation and will be a good improvement.
Left a few ideas.
/* Anchor links are percieved as external links. | ||
This constant helps check for on page anchor links, | ||
to prevent them from being opened in the editor. */ | ||
const shouldPreventDefault = href.startsWith( '#' ); |
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 we guarantee href
is always a String
? For example is it possible for it to be undefined
? If it is then we should guard here and also coerce to Boolean
.
const shouldPreventDefault = href.startsWith( '#' ); | |
const shouldPreventDefault = !! href?.startsWith( '#' ); |
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 am not sure how to backtrace it to see if it can be undefined for some reason, so I believe it is easier to add the check :)
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.
Though the type for href
is set to string
in types.ts. I think @walbo would be able to answer how to best handle the check.
Do I also need to add an entry to packages/components/CHANGELOG.md? |
Based on this I'd say you have to maintain that file manually. So add this PR under the |
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 think this is ready to 🚢
What?
This is an attempt to solve an issue where clicking an anchor link inside the link control modal opens the editor in a new tab.
Please see #10117.
This PR prevents following the anchor link by checking if the
href
starts with # and then usingevent.preventDefault
on click. There is also a valid suggestion in the issue, to open the link but with a front view, not editor view. I was unsure of how to make that change, so I went with this smaller change.While I believe that this PR works, it feels like treating the symptoms rather than the cause.
Does anyone have insight in why the external link component is not only used for, well, external links?
Testing Instructions
#test
).Screenshots or screencast