PageCreateModal.jsx 9.0 KB

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