-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Display clickable links in tasks (#694)
Closes #330
- Loading branch information
1 parent
ea1b3b7
commit cc32daa
Showing
6 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useCallback } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import LinkifyReact from 'linkify-react'; | ||
|
||
import history from '../history'; | ||
|
||
const Linkify = React.memo(({ children, linkStopPropagation, ...props }) => { | ||
const handleLinkClick = useCallback( | ||
(event) => { | ||
if (linkStopPropagation) { | ||
event.stopPropagation(); | ||
} | ||
|
||
if (!event.target.getAttribute('target')) { | ||
event.preventDefault(); | ||
history.push(event.target.href); | ||
} | ||
}, | ||
[linkStopPropagation], | ||
); | ||
|
||
const linkRenderer = useCallback( | ||
({ attributes: { href, ...linkProps }, content }) => { | ||
let url; | ||
try { | ||
url = new URL(href, window.location); | ||
} catch (error) {} // eslint-disable-line no-empty | ||
|
||
const isSameSite = !!url && url.origin === window.location.origin; | ||
|
||
return ( | ||
<a | ||
{...linkProps} // eslint-disable-line react/jsx-props-no-spreading | ||
href={href} | ||
target={isSameSite ? undefined : '_blank'} | ||
rel={isSameSite ? undefined : 'noreferrer'} | ||
onClick={handleLinkClick} | ||
> | ||
{isSameSite ? url.pathname : content} | ||
</a> | ||
); | ||
}, | ||
[handleLinkClick], | ||
); | ||
|
||
return ( | ||
<LinkifyReact | ||
{...props} // eslint-disable-line react/jsx-props-no-spreading | ||
options={{ | ||
defaultProtocol: 'https', | ||
render: linkRenderer, | ||
}} | ||
> | ||
{children} | ||
</LinkifyReact> | ||
); | ||
}); | ||
|
||
Linkify.propTypes = { | ||
children: PropTypes.string.isRequired, | ||
linkStopPropagation: PropTypes.bool, | ||
}; | ||
|
||
Linkify.defaultProps = { | ||
linkStopPropagation: false, | ||
}; | ||
|
||
export default Linkify; |