LinkEditModal.jsx 12 KB

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