import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import { UncontrolledDropdown, 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, isParamsAppended: true, }; this.id = (Math.random() * 1000).toString(); 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); } get uriParams() { const { isParamsAppended } = this.state; if (!isParamsAppended) { return ''; } const { search, hash, } = window.location; return `${search}${hash}`; } encodeSpaces(str) { if (str == null) { return null; } // Encode SPACE and IDEOGRAPHIC SPACE return str.replace(/ /g, '%20').replace(/\u3000/g, '%E3%80%80'); } generatePagePathWithParams() { const { pagePath } = this.props; return decodeURI(`${pagePath}${this.uriParams}`); } generatePagePathUrl() { const { origin } = window.location; return `${origin}${this.encodeSpaces(this.generatePagePathWithParams())}`; } generatePermalink() { const { origin } = window.location; const { pageId, isShareLinkMode } = this.props; if (pageId == null) { return null; } if (isShareLinkMode) { return decodeURI(`${origin}/share/${pageId}`); } return this.encodeSpaces(decodeURI(`${origin}/${pageId}${this.uriParams}`)); } generateMarkdownLink() { const { pagePath } = this.props; const label = decodeURI(`${pagePath}${this.uriParams}`); const permalink = this.generatePermalink(); return `[${label}](${permalink})`; } DropdownItemContents = ({ title, contents }) => ( <>
{title}
{contents}
); render() { const { t, pageId, isShareLinkMode, } = this.props; const { isParamsAppended } = this.state; const pagePathWithParams = this.generatePagePathWithParams(); const pagePathUrl = this.generatePagePathUrl(); const permalink = this.generatePermalink(); const copyTarget = isShareLinkMode ? `copyShareLink${pageId}` : 'copyPagePathDropdown'; const dropdownToggleStyle = isShareLinkMode ? 'btn btn-secondary' : 'd-block text-muted bg-transparent btn-copy border-0'; const { id, DropdownItemContents } = this; const customSwitchForParamsId = `customSwitchForParams_${id}`; return ( <> { isShareLinkMode ? ( <>Copy Link ) : ()}
{ t('copy_to_clipboard.Copy to clipboard') }
this.setState({ isParamsAppended: !isParamsAppended })} />
{/* Page path */} {/* Page path URL */} {/* Permanent Link */} { pageId && ( )} {/* Page path + Permanent 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, isShareLinkMode: PropTypes.bool, }; export default withTranslation()(CopyDropdown);