2025-05-10 02:09:06 +02:00
|
|
|
/*!
|
|
|
|
* Copyright (c) 2024 PLANKA Software GmbH
|
|
|
|
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
|
|
|
*/
|
|
|
|
|
2024-04-10 15:53:05 +02:00
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import LinkifyReact from 'linkify-react';
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
import history from '../../history';
|
2024-04-10 15:53:05 +02:00
|
|
|
|
|
|
|
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);
|
2025-05-10 02:09:06 +02:00
|
|
|
} catch {
|
|
|
|
/* empty */
|
|
|
|
}
|
2024-04-10 15:53:05 +02:00
|
|
|
|
|
|
|
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;
|