LinkEditModal.jsx 12 KB

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