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 }) => ( <>