import React from 'react'; import PropTypes from 'prop-types'; import path from 'path'; import { Modal, ModalHeader, ModalBody, ModalFooter, } from 'reactstrap'; import PageContainer from '../../services/PageContainer'; import PagePathAutoComplete from '../PagePathAutoComplete'; import { withUnstatedContainers } from '../UnstatedUtils'; class LinkEditModal extends React.PureComponent { constructor(props) { super(props); this.state = { show: false, isUseRelativePath: false, isUsePermanentLink: false, linkInputValue: '', labelInputValue: '', linkerType: 'mdLink', }; this.show = this.show.bind(this); this.hide = this.hide.bind(this); this.cancel = this.cancel.bind(this); this.inputChangeHandler = this.inputChangeHandler.bind(this); this.submitHandler = this.submitHandler.bind(this); this.handleChangeLabelInput = this.handleChangeLabelInput.bind(this); this.handleSelecteLinkerType = this.handleSelecteLinkerType.bind(this); this.toggleIsUseRelativePath = this.toggleIsUseRelativePath.bind(this); this.toggleIsUsePamanentLink = this.toggleIsUsePamanentLink.bind(this); this.save = this.save.bind(this); this.generateLink = this.generateLink.bind(this); } show(defaultLabelInputValue = '') { this.setState({ show: true, labelInputValue: defaultLabelInputValue }); } cancel() { this.hide(); } hide() { this.setState({ show: false, }); } toggleIsUseRelativePath() { if (this.state.linkerType === 'growiLink') { return; } this.setState({ isUseRelativePath: !this.state.isUseRelativePath }); } toggleIsUsePamanentLink() { this.setState({ isUsePermanentLink: !this.state.isUsePermanentLink }); } renderPreview() { // TODO GW-2658 } insertLinkIntoEditor() { // TODO GW-2659 } handleChangeLabelInput(label) { this.setState({ labelInputValue: label }); } handleSelecteLinkerType(linkerType) { if (this.state.isUseRelativePath && linkerType === 'growiLink') { this.toggleIsUseRelativePath(); } this.setState({ linkerType }); } save() { const output = this.generateLink(); if (this.props.onSave != null) { this.props.onSave(output); } this.hide(); } inputChangeHandler(inputChangeValue) { this.setState({ linkInputValue: inputChangeValue }); } submitHandler(submitValue) { this.setState({ linkInputValue: submitValue }); } generateLink() { const { pageContainer } = this.props; const { linkInputValue, labelInputValue, linkerType, isUseRelativePath, } = this.state; let reshapedLink = linkInputValue; if (isUseRelativePath && linkInputValue.match(/^\//)) { reshapedLink = path.relative(pageContainer.state.path, linkInputValue); } if (linkerType === 'pukiwikiLink') { return `[[${labelInputValue}>${reshapedLink}]]`; } if (linkerType === 'growiLink') { return `[${reshapedLink}]`; } if (linkerType === 'mdLink') { return `[${labelInputValue}](${reshapedLink})`; } } render() { return ( Edit Links
this.handleChangeLabelInput(e.target.value)} disabled={this.state.linkerType === 'growiLink'} />
{this.renderPreview} render preview
); } } LinkEditModal.propTypes = { pageContainer: PropTypes.instanceOf(PageContainer).isRequired, onSave: PropTypes.func, }; /** * Wrapper component for using unstated */ const LinkEditModalWrapper = withUnstatedContainers(LinkEditModal, [PageContainer]); export default LinkEditModalWrapper;