2017-05-02 17:04:16 -07:00
|
|
|
import React from 'react';
|
2017-04-21 11:05:35 -07:00
|
|
|
import PropTypes from 'prop-types';
|
2016-12-02 06:05:50 -08:00
|
|
|
|
2017-06-23 10:36:54 -07:00
|
|
|
export default class Permalink extends React.PureComponent {
|
2016-12-02 06:05:50 -08:00
|
|
|
|
2017-05-12 05:44:10 -07:00
|
|
|
static contextTypes = {
|
2017-05-20 08:31:47 -07:00
|
|
|
router: PropTypes.object,
|
2017-05-12 05:44:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
href: PropTypes.string.isRequired,
|
|
|
|
to: PropTypes.string.isRequired,
|
2017-05-20 08:31:47 -07:00
|
|
|
children: PropTypes.node,
|
2018-10-02 07:01:28 -07:00
|
|
|
onInterceptClick: PropTypes.func,
|
2017-05-12 05:44:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
handleClick = (e) => {
|
2018-10-02 07:01:28 -07:00
|
|
|
if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
|
|
|
|
if (this.props.onInterceptClick && this.props.onInterceptClick()) {
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.context.router) {
|
|
|
|
e.preventDefault();
|
2023-02-03 11:52:07 -08:00
|
|
|
let state = { ...this.context.router.history.location.state };
|
2019-04-15 13:23:05 -07:00
|
|
|
state.mastodonBackSteps = (state.mastodonBackSteps || 0) + 1;
|
|
|
|
this.context.router.history.push(this.props.to, state);
|
2018-10-02 07:01:28 -07:00
|
|
|
}
|
2016-12-02 06:05:50 -08:00
|
|
|
}
|
2023-02-03 11:52:07 -08:00
|
|
|
};
|
2016-12-02 06:05:50 -08:00
|
|
|
|
|
|
|
render () {
|
2018-01-05 20:04:13 -08:00
|
|
|
const {
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
href,
|
|
|
|
to,
|
2018-10-02 07:01:28 -07:00
|
|
|
onInterceptClick,
|
2018-01-05 20:04:13 -08:00
|
|
|
...other
|
|
|
|
} = this.props;
|
2016-12-02 06:05:50 -08:00
|
|
|
|
2017-05-02 17:04:16 -07:00
|
|
|
return (
|
2017-07-11 06:27:59 -07:00
|
|
|
<a target='_blank' href={href} onClick={this.handleClick} {...other} className={`permalink${className ? ' ' + className : ''}`}>
|
2017-05-02 17:04:16 -07:00
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
);
|
2016-12-02 06:05:50 -08:00
|
|
|
}
|
|
|
|
|
2017-04-21 11:05:35 -07:00
|
|
|
}
|