import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem, Tooltip, } from 'reactstrap'; import { CopyToClipboard } from 'react-copy-to-clipboard'; class CopyDropdown extends React.Component { constructor(props) { super(props); this.state = { dropdownOpen: false, tooltipOpen: false, }; this.toggle = this.toggle.bind(this); this.showToolTip = this.showToolTip.bind(this); this.generatePagePathWithParams = this.generatePagePathWithParams.bind(this); this.generatePagePathUrl = this.generatePagePathUrl.bind(this); this.generatePermalink = this.generatePermalink.bind(this); this.generateMarkdownLink = this.generateMarkdownLink.bind(this); } toggle() { this.setState({ dropdownOpen: !this.state.dropdownOpen }); } showToolTip() { this.setState({ tooltipOpen: true }); setTimeout(() => { this.setState({ tooltipOpen: false }); }, 1000); } generatePagePathWithParams() { const { pagePath } = this.props; const { search, hash, } = window.location; return decodeURI(`${pagePath}${search}${hash}`); } generatePagePathUrl() { const { origin } = window.location; return `${origin}${this.generatePagePathWithParams()}`; } generatePermalink() { const { pageId } = this.props; const { location } = window; if (pageId == null) { return null; } const { origin, search, hash, } = location; return decodeURI(`${origin}/${pageId}${search}${hash}`); } generateMarkdownLink() { const { pagePath } = this.props; const { search, hash, } = window.location; const label = `${pagePath}${search}${hash}`; const permalink = this.generatePermalink(); return decodeURI(`[${label}](${permalink})`); } DropdownItemContents = ({ title, contents }) => ( <>
{title}
{contents}
); render() { const { t, pageId } = this.props; const pagePathWithParams = this.generatePagePathWithParams(); const pagePathUrl = this.generatePagePathUrl(); const permalink = this.generatePermalink(); const { DropdownItemContents } = this; return ( <> { t('copy_to_clipboard.Copy to clipboard') } {/* Page path */} {/* Page path URL */} {/* Parmanent Link */} { pageId && ( )} {/* Page path + Parmanent Link */} { pageId && ( {pagePathWithParams}
{permalink}} />
)} {/* Markdown Link */} { pageId && ( )}
copied! ); } } CopyDropdown.propTypes = { t: PropTypes.func.isRequired, // i18next pagePath: PropTypes.string.isRequired, pageId: PropTypes.string, buttonStyle: PropTypes.object, }; export default withTranslation()(CopyDropdown);