LinkEditModal.jsx 11 KB

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