LinkEditModal.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. Modal,
  5. ModalHeader,
  6. ModalBody,
  7. ModalFooter,
  8. } from 'reactstrap';
  9. import Preview from './Preview';
  10. import AppContainer from '../../services/AppContainer';
  11. import PageContainer from '../../services/PageContainer';
  12. import SearchTypeahead from '../SearchTypeahead';
  13. import Linker from '../../models/Linker';
  14. import { withUnstatedContainers } from '../UnstatedUtils';
  15. class LinkEditModal extends React.PureComponent {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. show: false,
  20. isUseRelativePath: false,
  21. isUsePermanentLink: false,
  22. linkInputValue: '',
  23. labelInputValue: '',
  24. linkerType: Linker.types.markdownLink,
  25. markdown: '',
  26. permalink: '',
  27. };
  28. this.isApplyPukiwikiLikeLinkerPlugin = window.growiRenderer.preProcessors.some(process => process.constructor.name === 'PukiwikiLikeLinker');
  29. this.show = this.show.bind(this);
  30. this.hide = this.hide.bind(this);
  31. this.cancel = this.cancel.bind(this);
  32. this.handleChangeTypeahead = this.handleChangeTypeahead.bind(this);
  33. this.handleChangeLabelInput = this.handleChangeLabelInput.bind(this);
  34. this.handleChangeLinkInput = this.handleChangeLinkInput.bind(this);
  35. this.handleSelecteLinkerType = this.handleSelecteLinkerType.bind(this);
  36. this.toggleIsUseRelativePath = this.toggleIsUseRelativePath.bind(this);
  37. this.toggleIsUsePamanentLink = this.toggleIsUsePamanentLink.bind(this);
  38. this.save = this.save.bind(this);
  39. this.generateLink = this.generateLink.bind(this);
  40. this.getPreview = this.getPreview.bind(this);
  41. this.renderPreview = this.renderPreview.bind(this);
  42. }
  43. componentDidUpdate(prevState) {
  44. const { linkInputValue: prevLinkInputValue } = prevState;
  45. const { linkInputValue } = this.state;
  46. if (linkInputValue !== prevLinkInputValue) {
  47. this.getPreview(linkInputValue);
  48. }
  49. }
  50. // defaultMarkdownLink is an instance of Linker
  51. show(defaultMarkdownLink = null) {
  52. // if defaultMarkdownLink is null, set default value in inputs.
  53. const { label = '', link = '' } = defaultMarkdownLink;
  54. let { type = Linker.types.markdownLink } = defaultMarkdownLink;
  55. // if type of defaultMarkdownLink is pukiwikiLink when pukiwikiLikeLinker plugin is disable, change type(not change label and link)
  56. if (type === Linker.types.pukiwikiLink && !this.isApplyPukiwikiLikeLinkerPlugin) {
  57. type = Linker.types.markdownLink;
  58. }
  59. this.setState({
  60. show: true,
  61. labelInputValue: label,
  62. linkInputValue: link,
  63. linkerType: type,
  64. });
  65. }
  66. cancel() {
  67. this.hide();
  68. }
  69. hide() {
  70. this.setState({
  71. show: false,
  72. });
  73. }
  74. toggleIsUseRelativePath() {
  75. if (this.state.linkerType === Linker.types.growiLink) {
  76. return;
  77. }
  78. // User can't use both relativePath and permalink at the same time
  79. this.setState({ isUseRelativePath: !this.state.isUseRelativePath, isUsePermanentLink: false });
  80. }
  81. toggleIsUsePamanentLink() {
  82. if (this.state.permalink === '' || this.state.linkerType === Linker.types.growiLink) {
  83. return;
  84. }
  85. // User can't use both relativePath and permalink at the same time
  86. this.setState({ isUsePermanentLink: !this.state.isUsePermanentLink, isUseRelativePath: false });
  87. }
  88. renderPreview() {
  89. return (
  90. <div className="linkedit-preview">
  91. <Preview
  92. markdown={this.state.markdown}
  93. />
  94. </div>
  95. );
  96. }
  97. async setMarkdown(path) {
  98. let markdown = '';
  99. try {
  100. await this.props.appContainer.apiGet('/pages.get', { path }).then((res) => {
  101. markdown = res.page.revision.body;
  102. });
  103. }
  104. catch (err) {
  105. markdown = `<div class="alert alert-warning" role="alert"><strong>${err.message}</strong></div>`;
  106. }
  107. this.setState({ markdown });
  108. }
  109. handleChangeTypeahead(selected) {
  110. const page = selected[0];
  111. if (page != null) {
  112. this.setState({ linkInputValue: page.path });
  113. }
  114. }
  115. handleChangeLabelInput(label) {
  116. this.setState({ labelInputValue: label });
  117. }
  118. handleChangeLinkInput(link) {
  119. this.setState({ linkInputValue: link });
  120. }
  121. handleSelecteLinkerType(linkerType) {
  122. let { isUseRelativePath, isUsePermanentLink } = this.state;
  123. if (linkerType === Linker.types.growiLink) {
  124. isUseRelativePath = false;
  125. isUsePermanentLink = false;
  126. }
  127. this.setState({ linkerType, isUseRelativePath, isUsePermanentLink });
  128. }
  129. save() {
  130. const output = this.generateLink();
  131. if (this.props.onSave != null) {
  132. this.props.onSave(output);
  133. }
  134. this.hide();
  135. }
  136. async getPreview(path) {
  137. let markdown = '';
  138. let permalink = '';
  139. try {
  140. const res = await this.props.appContainer.apiGet('/pages.get', { path });
  141. markdown = res.page.revision.body;
  142. permalink = `${window.location.origin}/${res.page.id}`;
  143. }
  144. catch (err) {
  145. markdown = `<div class="alert alert-warning" role="alert"><strong>${err.message}</strong></div>`;
  146. }
  147. this.setState({ markdown, permalink });
  148. }
  149. generateLink() {
  150. const { pageContainer } = this.props;
  151. const {
  152. linkInputValue,
  153. labelInputValue,
  154. linkerType,
  155. isUseRelativePath,
  156. isUsePermanentLink,
  157. permalink,
  158. } = this.state;
  159. return new Linker(
  160. linkerType,
  161. labelInputValue,
  162. linkInputValue,
  163. isUseRelativePath,
  164. pageContainer.state.path,
  165. isUsePermanentLink,
  166. permalink,
  167. );
  168. }
  169. render() {
  170. return (
  171. <Modal isOpen={this.state.show} toggle={this.cancel} size="lg">
  172. <ModalHeader tag="h4" toggle={this.cancel} className="bg-primary text-light">
  173. Edit Links
  174. </ModalHeader>
  175. <ModalBody className="container">
  176. <div className="row">
  177. <div className="col-12 col-lg-6">
  178. <form className="form-group">
  179. <div className="form-gorup my-3">
  180. <label htmlFor="linkInput">Link</label>
  181. <div className="input-group">
  182. <SearchTypeahead
  183. onChange={this.handleChangeTypeahead}
  184. onInputChange={this.handleChangeLinkInput}
  185. inputName="link"
  186. placeholder="Input page path or URL"
  187. keywordOnInit={this.state.linkInputValue}
  188. />
  189. </div>
  190. </div>
  191. </form>
  192. <div className="d-block d-lg-none mb-3 overflow-auto">
  193. {this.renderPreview()}
  194. </div>
  195. <div className="card">
  196. <div className="card-body">
  197. <form className="form-group">
  198. <div className="form-group btn-group d-flex" role="group" aria-label="type">
  199. <button
  200. type="button"
  201. name={Linker.types.markdownLink}
  202. className={`btn btn-outline-secondary w-100 ${this.state.linkerType === Linker.types.markdownLink && 'active'}`}
  203. onClick={e => this.handleSelecteLinkerType(e.target.name)}
  204. >
  205. Markdown
  206. </button>
  207. <button
  208. type="button"
  209. name={Linker.types.growiLink}
  210. className={`btn btn-outline-secondary w-100 ${this.state.linkerType === Linker.types.growiLink && 'active'}`}
  211. onClick={e => this.handleSelecteLinkerType(e.target.name)}
  212. >
  213. Growi Original
  214. </button>
  215. {this.isApplyPukiwikiLikeLinkerPlugin && (
  216. <button
  217. type="button"
  218. name={Linker.types.pukiwikiLink}
  219. className={`btn btn-outline-secondary w-100 ${this.state.linkerType === Linker.types.pukiwikiLink && 'active'}`}
  220. onClick={e => this.handleSelecteLinkerType(e.target.name)}
  221. >
  222. Pukiwiki
  223. </button>
  224. )}
  225. </div>
  226. <div className="form-group">
  227. <label htmlFor="label">Label</label>
  228. <input
  229. type="text"
  230. className="form-control"
  231. id="label"
  232. value={this.state.labelInputValue}
  233. onChange={e => this.handleChangeLabelInput(e.target.value)}
  234. disabled={this.state.linkerType === Linker.types.growiLink}
  235. />
  236. </div>
  237. <div className="form-inline">
  238. <div className="custom-control custom-checkbox custom-checkbox-info">
  239. <input
  240. className="custom-control-input"
  241. id="relativePath"
  242. type="checkbox"
  243. checked={this.state.isUseRelativePath}
  244. disabled={this.state.linkerType === Linker.types.growiLink}
  245. />
  246. <label className="custom-control-label" htmlFor="relativePath" onClick={this.toggleIsUseRelativePath}>
  247. Use relative path
  248. </label>
  249. </div>
  250. </div>
  251. <div className="form-inline">
  252. <div className="custom-control custom-checkbox custom-checkbox-info">
  253. <input
  254. className="custom-control-input"
  255. id="permanentLink"
  256. type="checkbox"
  257. checked={this.state.isUsePermanentLink}
  258. disabled={this.state.permalink === '' || this.state.linkerType === Linker.types.growiLink}
  259. />
  260. <label className="custom-control-label" htmlFor="permanentLink" onClick={this.toggleIsUsePamanentLink}>
  261. Use permanent link
  262. </label>
  263. </div>
  264. </div>
  265. </form>
  266. </div>
  267. </div>
  268. </div>
  269. <div className="col d-none d-lg-block pr-0 mr-3 overflow-auto">
  270. {this.renderPreview()}
  271. </div>
  272. </div>
  273. </ModalBody>
  274. <ModalFooter>
  275. <button type="button" className="btn btn-sm btn-outline-secondary" onClick={this.hide}>
  276. Cancel
  277. </button>
  278. <button type="submit" className="btn btn-sm btn-primary" onClick={this.save}>
  279. Done
  280. </button>
  281. </ModalFooter>
  282. </Modal>
  283. );
  284. }
  285. }
  286. LinkEditModal.propTypes = {
  287. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  288. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  289. onSave: PropTypes.func,
  290. };
  291. /**
  292. * Wrapper component for using unstated
  293. */
  294. const LinkEditModalWrapper = withUnstatedContainers(LinkEditModal, [AppContainer, PageContainer]);
  295. export default LinkEditModalWrapper;