LinkEditModal.jsx 12 KB

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