LinkEditModal.jsx 7.8 KB

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