LinkEditModal.jsx 11 KB

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