LinkEditModal.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import path from 'path';
  4. import {
  5. Modal,
  6. ModalHeader,
  7. ModalBody,
  8. ModalFooter,
  9. } from 'reactstrap';
  10. import PageContainer from '../../services/PageContainer';
  11. import PagePathAutoComplete from '../PagePathAutoComplete';
  12. import { withUnstatedContainers } from '../UnstatedUtils';
  13. class LinkEditModal extends React.PureComponent {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. show: false,
  18. isUseRelativePath: false,
  19. isUsePermanentLink: false,
  20. linkInputValue: '',
  21. labelInputValue: '',
  22. linkerType: 'mdLink',
  23. };
  24. this.show = this.show.bind(this);
  25. this.hide = this.hide.bind(this);
  26. this.cancel = this.cancel.bind(this);
  27. this.inputChangeHandler = this.inputChangeHandler.bind(this);
  28. this.submitHandler = this.submitHandler.bind(this);
  29. this.handleChangeLabelInput = this.handleChangeLabelInput.bind(this);
  30. this.handleSelecteLinkerType = this.handleSelecteLinkerType.bind(this);
  31. this.toggleIsUseRelativePath = this.toggleIsUseRelativePath.bind(this);
  32. this.toggleIsUsePamanentLink = this.toggleIsUsePamanentLink.bind(this);
  33. this.save = this.save.bind(this);
  34. this.generateLink = this.generateLink.bind(this);
  35. }
  36. show(defaultLabelInputValue = '') {
  37. this.setState({ show: true, labelInputValue: defaultLabelInputValue });
  38. }
  39. cancel() {
  40. this.hide();
  41. }
  42. hide() {
  43. this.setState({
  44. show: false,
  45. });
  46. }
  47. toggleIsUseRelativePath() {
  48. if (this.state.linkerType === 'growiLink') {
  49. return;
  50. }
  51. this.setState({ isUseRelativePath: !this.state.isUseRelativePath });
  52. }
  53. toggleIsUsePamanentLink() {
  54. this.setState({ isUsePermanentLink: !this.state.isUsePermanentLink });
  55. }
  56. renderPreview() {
  57. // TODO GW-2658
  58. }
  59. insertLinkIntoEditor() {
  60. // TODO GW-2659
  61. }
  62. handleChangeLabelInput(label) {
  63. this.setState({ labelInputValue: label });
  64. }
  65. handleSelecteLinkerType(linkerType) {
  66. if (this.state.isUseRelativePath && linkerType === 'growiLink') {
  67. this.toggleIsUseRelativePath();
  68. }
  69. this.setState({ linkerType });
  70. }
  71. save() {
  72. const output = this.generateLink();
  73. if (this.props.onSave != null) {
  74. this.props.onSave(output);
  75. }
  76. this.hide();
  77. }
  78. inputChangeHandler(inputChangeValue) {
  79. this.setState({ linkInputValue: inputChangeValue });
  80. }
  81. submitHandler(submitValue) {
  82. this.setState({ linkInputValue: submitValue });
  83. }
  84. generateLink() {
  85. const { pageContainer } = this.props;
  86. const {
  87. linkInputValue,
  88. labelInputValue,
  89. linkerType,
  90. isUseRelativePath,
  91. } = this.state;
  92. let reshapedLink = linkInputValue;
  93. if (isUseRelativePath && linkInputValue.match(/^\//)) {
  94. reshapedLink = path.relative(pageContainer.state.path, linkInputValue);
  95. }
  96. if (linkerType === 'pukiwikiLink') {
  97. return `[[${labelInputValue}>${reshapedLink}]]`;
  98. }
  99. if (linkerType === 'growiLink') {
  100. return `[${reshapedLink}]`;
  101. }
  102. if (linkerType === 'mdLink') {
  103. return `[${labelInputValue}](${reshapedLink})`;
  104. }
  105. }
  106. render() {
  107. return (
  108. <Modal isOpen={this.state.show} toggle={this.cancel} size="lg">
  109. <ModalHeader tag="h4" toggle={this.cancel} className="bg-primary text-light">
  110. Edit Links
  111. </ModalHeader>
  112. <ModalBody className="container">
  113. <div className="row">
  114. <div className="col-12 col-lg-6">
  115. <form className="form-group">
  116. <div className="form-gorup my-3">
  117. <label htmlFor="linkInput">Link</label>
  118. <div className="input-group">
  119. <PagePathAutoComplete
  120. onInputChange={this.inputChangeHandler}
  121. onSubmit={this.submitHandler}
  122. />
  123. </div>
  124. </div>
  125. <div className="form-inline">
  126. <div className="custom-control custom-checkbox custom-checkbox-info">
  127. <input
  128. className="custom-control-input"
  129. id="permanentLink"
  130. type="checkbox"
  131. checked={this.state.isUsePamanentLink}
  132. />
  133. <label className="custom-control-label" htmlFor="permanentLink" onClick={this.toggleIsUsePamanentLink}>
  134. Use permanent link
  135. </label>
  136. </div>
  137. </div>
  138. </form>
  139. <div className="card">
  140. <div className="card-body">
  141. <form className="form-group">
  142. <div className="form-group btn-group d-flex" role="group" aria-label="type">
  143. <button
  144. type="button"
  145. name="mdLink"
  146. className={`btn btn-outline-secondary w-100 ${this.state.linkerType === 'mdLink' && 'active'}`}
  147. onClick={e => this.handleSelecteLinkerType(e.target.name)}
  148. >
  149. Markdown
  150. </button>
  151. <button
  152. type="button"
  153. name="growiLink"
  154. className={`btn btn-outline-secondary w-100 ${this.state.linkerType === 'growiLink' && 'active'}`}
  155. onClick={e => this.handleSelecteLinkerType(e.target.name)}
  156. >
  157. Growi Original
  158. </button>
  159. <button
  160. type="button"
  161. name="pukiwikiLink"
  162. className={`btn btn-outline-secondary w-100 ${this.state.linkerType === 'pukiwikiLink' && 'active'}`}
  163. onClick={e => this.handleSelecteLinkerType(e.target.name)}
  164. >
  165. Pukiwiki
  166. </button>
  167. </div>
  168. <div className="form-group">
  169. <label htmlFor="label">Label</label>
  170. <input
  171. type="text"
  172. className="form-control"
  173. id="label"
  174. value={this.state.labelInputValue}
  175. onChange={e => this.handleChangeLabelInput(e.target.value)}
  176. disabled={this.state.linkerType === 'growiLink'}
  177. />
  178. </div>
  179. <div className="form-inline">
  180. <div className="custom-control custom-checkbox custom-checkbox-info">
  181. <input
  182. className="custom-control-input"
  183. id="relativePath"
  184. type="checkbox"
  185. checked={this.state.isUseRelativePath}
  186. disabled={this.state.linkerType === 'growiLink'}
  187. />
  188. <label className="custom-control-label" htmlFor="relativePath" onClick={this.toggleIsUseRelativePath}>
  189. Use relative path
  190. </label>
  191. </div>
  192. </div>
  193. </form>
  194. </div>
  195. </div>
  196. </div>
  197. <div className="col-12 col-lg-6">
  198. <div className="d-block d-lg-none">
  199. {this.renderPreview}
  200. render preview
  201. </div>
  202. </div>
  203. </div>
  204. </ModalBody>
  205. <ModalFooter>
  206. <button type="button" className="btn btn-sm btn-outline-secondary" onClick={this.hide}>
  207. Cancel
  208. </button>
  209. <button type="submit" className="btn btn-sm btn-primary" onClick={this.save}>
  210. Done
  211. </button>
  212. </ModalFooter>
  213. </Modal>
  214. );
  215. }
  216. }
  217. LinkEditModal.propTypes = {
  218. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  219. onSave: PropTypes.func,
  220. };
  221. /**
  222. * Wrapper component for using unstated
  223. */
  224. const LinkEditModalWrapper = withUnstatedContainers(LinkEditModal, [PageContainer]);
  225. export default LinkEditModalWrapper;