PageCreateModal.jsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import React, { useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Modal, ModalHeader, ModalBody } from 'reactstrap';
  4. import { withTranslation } from 'react-i18next';
  5. import { format } from 'date-fns';
  6. import { pagePathUtils, pathUtils } from '@growi/core';
  7. import AppContainer from '~/client/services/AppContainer';
  8. import NavigationContainer from '~/client/services/NavigationContainer';
  9. import { withUnstatedContainers } from './UnstatedUtils';
  10. import { toastError } from '~/client/util/apiNotification';
  11. import PagePathAutoComplete from './PagePathAutoComplete';
  12. const {
  13. userPageRoot, isCreatablePage, generateEditorPath,
  14. } = pagePathUtils;
  15. const PageCreateModal = (props) => {
  16. const { t, appContainer, navigationContainer } = props;
  17. const config = appContainer.getConfig();
  18. const isReachable = config.isSearchServiceReachable;
  19. const pathname = decodeURI(window.location.pathname);
  20. const userPageRootPath = userPageRoot(appContainer.currentUser);
  21. const pageNameInputInitialValue = isCreatablePage(pathname) ? pathUtils.addTrailingSlash(pathname) : '/';
  22. const now = format(new Date(), 'yyyy/MM/dd');
  23. const [todayInput1, setTodayInput1] = useState(t('Memo'));
  24. const [todayInput2, setTodayInput2] = useState('');
  25. const [pageNameInput, setPageNameInput] = useState(pageNameInputInitialValue);
  26. const [template, setTemplate] = useState(null);
  27. function transitBySubmitEvent(e, transitHandler) {
  28. // prevent page transition by submit
  29. e.preventDefault();
  30. transitHandler();
  31. }
  32. /**
  33. * change todayInput1
  34. * @param {string} value
  35. */
  36. function onChangeTodayInput1Handler(value) {
  37. setTodayInput1(value);
  38. }
  39. /**
  40. * change todayInput2
  41. * @param {string} value
  42. */
  43. function onChangeTodayInput2Handler(value) {
  44. setTodayInput2(value);
  45. }
  46. /**
  47. * change pageNameInput
  48. * @param {string} value
  49. */
  50. function onChangePageNameInputHandler(value) {
  51. setPageNameInput(value);
  52. }
  53. /**
  54. * change template
  55. * @param {string} value
  56. */
  57. function onChangeTemplateHandler(value) {
  58. setTemplate(value);
  59. }
  60. /**
  61. * join path, check if creatable, then redirect
  62. * @param {string} paths
  63. */
  64. async function redirectToEditor(...paths) {
  65. try {
  66. const editorPath = await generateEditorPath(...paths);
  67. window.location.href = editorPath;
  68. }
  69. catch (err) {
  70. toastError(err);
  71. }
  72. }
  73. /**
  74. * access today page
  75. */
  76. function createTodayPage() {
  77. let tmpTodayInput1 = todayInput1;
  78. if (tmpTodayInput1 === '') {
  79. tmpTodayInput1 = t('Memo');
  80. }
  81. redirectToEditor(userPageRootPath, tmpTodayInput1, now, todayInput2);
  82. }
  83. /**
  84. * access input page
  85. */
  86. function createInputPage() {
  87. redirectToEditor(pageNameInput);
  88. }
  89. function ppacInputChangeHandler(value) {
  90. setPageNameInput(value);
  91. }
  92. function ppacSubmitHandler() {
  93. createInputPage();
  94. }
  95. /**
  96. * access template page
  97. */
  98. function createTemplatePage(e) {
  99. const pageName = (template === 'children') ? '_template' : '__template';
  100. redirectToEditor(pathname, pageName);
  101. }
  102. function renderCreateTodayForm() {
  103. return (
  104. <div className="row">
  105. <fieldset className="col-12 mb-4">
  106. <h3 className="grw-modal-head pb-2">{t("Create today's")}</h3>
  107. <div className="d-sm-flex align-items-center justify-items-between">
  108. <div className="d-flex align-items-center flex-fill flex-wrap flex-lg-nowrap">
  109. <div className="d-flex align-items-center">
  110. <span>{userPageRootPath}/</span>
  111. <form onSubmit={e => transitBySubmitEvent(e, createTodayPage)}>
  112. <input
  113. type="text"
  114. className="page-today-input1 form-control text-center mx-2"
  115. value={todayInput1}
  116. onChange={e => onChangeTodayInput1Handler(e.target.value)}
  117. />
  118. </form>
  119. <span className="page-today-suffix">/{now}/</span>
  120. </div>
  121. <form className="mt-1 mt-lg-0 ml-lg-2 w-100" onSubmit={e => transitBySubmitEvent(e, createTodayPage)}>
  122. <input
  123. type="text"
  124. className="page-today-input2 form-control w-100"
  125. id="page-today-input2"
  126. placeholder={t('Input page name (optional)')}
  127. value={todayInput2}
  128. onChange={e => onChangeTodayInput2Handler(e.target.value)}
  129. />
  130. </form>
  131. </div>
  132. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  133. <button type="button" className="grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3" onClick={createTodayPage}>
  134. <i className="icon-fw icon-doc"></i>{t('Create')}
  135. </button>
  136. </div>
  137. </div>
  138. </fieldset>
  139. </div>
  140. );
  141. }
  142. function renderInputPageForm() {
  143. return (
  144. <div className="row">
  145. <fieldset className="col-12 mb-4">
  146. <h3 className="grw-modal-head pb-2">{t('Create under')}</h3>
  147. <div className="d-sm-flex align-items-center justify-items-between">
  148. <div className="flex-fill">
  149. {isReachable
  150. ? (
  151. <PagePathAutoComplete
  152. initializedPath={pageNameInput}
  153. addTrailingSlash
  154. onSubmit={ppacSubmitHandler}
  155. onInputChange={ppacInputChangeHandler}
  156. autoFocus
  157. />
  158. )
  159. : (
  160. <form onSubmit={e => transitBySubmitEvent(e, createInputPage)}>
  161. <input
  162. type="text"
  163. value={pageNameInput}
  164. className="form-control flex-fill"
  165. placeholder={t('Input page name')}
  166. onChange={e => onChangePageNameInputHandler(e.target.value)}
  167. required
  168. />
  169. </form>
  170. )}
  171. </div>
  172. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  173. <button type="button" className="grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3" onClick={createInputPage}>
  174. <i className="icon-fw icon-doc"></i>{t('Create')}
  175. </button>
  176. </div>
  177. </div>
  178. </fieldset>
  179. </div>
  180. );
  181. }
  182. function renderTemplatePageForm() {
  183. return (
  184. <div className="row">
  185. <fieldset className="col-12">
  186. <h3 className="grw-modal-head pb-2">
  187. {t('template.modal_label.Create template under')}<br />
  188. <code className="h6">{pathname}</code>
  189. </h3>
  190. <div className="d-sm-flex align-items-center justify-items-between">
  191. <div id="dd-template-type" className="dropdown flex-fill">
  192. <button id="template-type" type="button" className="btn btn-secondary btn dropdown-toggle w-100" data-toggle="dropdown">
  193. {template == null && t('template.option_label.select')}
  194. {template === 'children' && t('template.children.label')}
  195. {template === 'decendants' && t('template.decendants.label')}
  196. </button>
  197. <div className="dropdown-menu" aria-labelledby="userMenu">
  198. <button className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('children')}>
  199. {t('template.children.label')} (_template)<br className="d-block d-md-none" />
  200. <small className="text-muted text-wrap">- {t('template.children.desc')}</small>
  201. </button>
  202. <button className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('decendants')}>
  203. {t('template.decendants.label')} (__template) <br className="d-block d-md-none" />
  204. <small className="text-muted">- {t('template.decendants.desc')}</small>
  205. </button>
  206. </div>
  207. </div>
  208. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  209. <button
  210. type="button"
  211. className={`grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3 ${template == null && 'disabled'}`}
  212. onClick={createTemplatePage}
  213. >
  214. <i className="icon-fw icon-doc"></i>{t('Edit')}
  215. </button>
  216. </div>
  217. </div>
  218. </fieldset>
  219. </div>
  220. );
  221. }
  222. return (
  223. <Modal
  224. size="lg"
  225. isOpen={navigationContainer.state.isPageCreateModalShown}
  226. toggle={navigationContainer.closePageCreateModal}
  227. className="grw-create-page"
  228. autoFocus={false}
  229. >
  230. <ModalHeader tag="h4" toggle={navigationContainer.closePageCreateModal} className="bg-primary text-light">
  231. {t('New Page')}
  232. </ModalHeader>
  233. <ModalBody>
  234. {renderCreateTodayForm()}
  235. {renderInputPageForm()}
  236. {renderTemplatePageForm()}
  237. </ModalBody>
  238. </Modal>
  239. );
  240. };
  241. /**
  242. * Wrapper component for using unstated
  243. */
  244. const ModalControlWrapper = withUnstatedContainers(PageCreateModal, [AppContainer, NavigationContainer]);
  245. PageCreateModal.propTypes = {
  246. t: PropTypes.func.isRequired, // i18next
  247. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  248. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  249. };
  250. export default withTranslation()(ModalControlWrapper);