app.jsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Provider } from 'unstated';
  4. import { I18nextProvider } from 'react-i18next';
  5. import { DndProvider } from 'react-dnd';
  6. import { HTML5Backend } from 'react-dnd-html5-backend';
  7. import { SWRConfig } from 'swr';
  8. import loggerFactory from '~/utils/logger';
  9. import { swrGlobalConfiguration } from '~/utils/swr-utils';
  10. import InAppNotificationPage from '../components/InAppNotification/InAppNotificationPage';
  11. import ErrorBoundary from '../components/ErrorBoudary';
  12. import Sidebar from '../components/Sidebar';
  13. import { SearchPage } from '../components/SearchPage';
  14. import TagsList from '../components/TagsList';
  15. import DisplaySwitcher from '../components/Page/DisplaySwitcher';
  16. import { defaultEditorOptions, defaultPreviewOptions } from '../components/PageEditor/OptionsSelector';
  17. import Page from '../components/Page';
  18. import PageComments from '../components/PageComments';
  19. import PageContentFooter from '../components/PageContentFooter';
  20. import PageTimeline from '../components/PageTimeline';
  21. import CommentEditorLazyRenderer from '../components/PageComment/CommentEditorLazyRenderer';
  22. import PageManagement from '../components/Page/PageManagement';
  23. import ShareLinkAlert from '../components/Page/ShareLinkAlert';
  24. import DuplicatedAlert from '../components/Page/DuplicatedAlert';
  25. import RedirectedAlert from '../components/Page/RedirectedAlert';
  26. import RenamedAlert from '../components/Page/RenamedAlert';
  27. import TrashPageList from '../components/TrashPageList';
  28. import TrashPageAlert from '../components/Page/TrashPageAlert';
  29. import NotFoundPage from '../components/NotFoundPage';
  30. import NotFoundAlert from '../components/Page/NotFoundAlert';
  31. import ForbiddenPage from '../components/ForbiddenPage';
  32. import PageStatusAlert from '../components/PageStatusAlert';
  33. import RecentCreated from '../components/RecentCreated/RecentCreated';
  34. import RecentlyCreatedIcon from '../components/Icons/RecentlyCreatedIcon';
  35. import MyDraftList from '../components/MyDraftList/MyDraftList';
  36. import BookmarkList from '../components/PageList/BookmarkList';
  37. import Fab from '../components/Fab';
  38. import PersonalSettings from '../components/Me/PersonalSettings';
  39. import GrowiContextualSubNavigation from '../components/Navbar/GrowiContextualSubNavigation';
  40. import GrowiSubNavigationSwitcher from '../components/Navbar/GrowiSubNavigationSwitcher';
  41. import IdenticalPathPage from '~/components/IdenticalPathPage';
  42. import ContextExtractor from '~/client/services/ContextExtractor';
  43. import PageContainer from '~/client/services/PageContainer';
  44. import PageHistoryContainer from '~/client/services/PageHistoryContainer';
  45. import RevisionComparerContainer from '~/client/services/RevisionComparerContainer';
  46. import CommentContainer from '~/client/services/CommentContainer';
  47. import EditorContainer from '~/client/services/EditorContainer';
  48. import TagContainer from '~/client/services/TagContainer';
  49. import PersonalContainer from '~/client/services/PersonalContainer';
  50. import { appContainer, componentMappings } from './base';
  51. import { toastError } from './util/apiNotification';
  52. const logger = loggerFactory('growi:cli:app');
  53. appContainer.initContents();
  54. const { i18n } = appContainer;
  55. const socketIoContainer = appContainer.getContainer('SocketIoContainer');
  56. // create unstated container instance
  57. const pageContainer = new PageContainer(appContainer);
  58. const pageHistoryContainer = new PageHistoryContainer(appContainer, pageContainer);
  59. const revisionComparerContainer = new RevisionComparerContainer(appContainer, pageContainer);
  60. const commentContainer = new CommentContainer(appContainer);
  61. const editorContainer = new EditorContainer(appContainer, defaultEditorOptions, defaultPreviewOptions);
  62. const tagContainer = new TagContainer(appContainer);
  63. const personalContainer = new PersonalContainer(appContainer);
  64. const injectableContainers = [
  65. appContainer, socketIoContainer, pageContainer, pageHistoryContainer, revisionComparerContainer,
  66. commentContainer, editorContainer, tagContainer, personalContainer,
  67. ];
  68. logger.info('unstated containers have been initialized');
  69. /**
  70. * define components
  71. * key: id of element
  72. * value: React Element
  73. */
  74. Object.assign(componentMappings, {
  75. 'grw-sidebar-wrapper': <Sidebar />,
  76. 'search-page': <SearchPage appContainer={appContainer} />,
  77. 'all-in-app-notifications': <InAppNotificationPage />,
  78. 'identical-path-page': <IdenticalPathPage />,
  79. // 'revision-history': <PageHistory pageId={pageId} />,
  80. 'tags-page': <TagsList crowi={appContainer} />,
  81. 'grw-page-status-alert-container': <PageStatusAlert />,
  82. 'trash-page-alert': <TrashPageAlert />,
  83. 'trash-page-list': <TrashPageList />,
  84. 'not-found-page': <NotFoundPage />,
  85. 'forbidden-page': <ForbiddenPage isLinkSharingDisabled={appContainer.config.disableLinkSharing} />,
  86. 'page-timeline': <PageTimeline />,
  87. 'personal-setting': <PersonalSettings crowi={personalContainer} />,
  88. 'my-drafts': <MyDraftList />,
  89. 'grw-fab-container': <Fab />,
  90. 'share-link-alert': <ShareLinkAlert />,
  91. 'duplicated-alert': <DuplicatedAlert />,
  92. 'redirected-alert': <RedirectedAlert />,
  93. 'renamed-alert': <RenamedAlert />,
  94. 'not-found-alert': <NotFoundAlert
  95. isGuestUserMode={appContainer.isGuestUser}
  96. />,
  97. });
  98. // additional definitions if data exists
  99. if (pageContainer.state.pageId != null) {
  100. Object.assign(componentMappings, {
  101. 'page-comments-list': <PageComments />,
  102. 'page-comment-write': <CommentEditorLazyRenderer />,
  103. 'page-management': <PageManagement />,
  104. 'page-content-footer': <PageContentFooter />,
  105. 'recent-created-icon': <RecentlyCreatedIcon />,
  106. });
  107. // show the Page accessory modal when query of "compare" is requested
  108. if (revisionComparerContainer.getRevisionIDsToCompareAsParam().length > 0) {
  109. toastError('Sorry, opening PageAccessoriesModal is not implemented yet in v5.');
  110. // pageAccessoriesContainer.openPageAccessoriesModal('pageHistory');
  111. }
  112. }
  113. if (pageContainer.state.creator != null) {
  114. Object.assign(componentMappings, {
  115. 'user-created-list': <RecentCreated userId={pageContainer.state.creator._id} />,
  116. 'user-bookmark-list': <BookmarkList userId={pageContainer.state.creator._id} />,
  117. });
  118. }
  119. if (pageContainer.state.path != null) {
  120. Object.assign(componentMappings, {
  121. // eslint-disable-next-line quote-props
  122. 'page': <Page />,
  123. 'grw-subnav-container': <GrowiContextualSubNavigation isLinkSharingDisabled={appContainer.config.disableLinkSharing} />,
  124. 'grw-subnav-switcher-container': <GrowiSubNavigationSwitcher isLinkSharingDisabled={appContainer.config.disableLinkSharing} />,
  125. 'display-switcher': <DisplaySwitcher />,
  126. });
  127. }
  128. const renderMainComponents = () => {
  129. Object.keys(componentMappings).forEach((key) => {
  130. const elem = document.getElementById(key);
  131. if (elem) {
  132. ReactDOM.render(
  133. <I18nextProvider i18n={i18n}>
  134. <ErrorBoundary>
  135. <SWRConfig value={swrGlobalConfiguration}>
  136. <Provider inject={injectableContainers}>
  137. <DndProvider backend={HTML5Backend}>
  138. {componentMappings[key]}
  139. </DndProvider>
  140. </Provider>
  141. </SWRConfig>
  142. </ErrorBoundary>
  143. </I18nextProvider>,
  144. elem,
  145. );
  146. }
  147. });
  148. };
  149. // extract context before rendering main components
  150. const elem = document.getElementById('growi-context-extractor');
  151. if (elem != null) {
  152. ReactDOM.render(
  153. <SWRConfig value={swrGlobalConfiguration}>
  154. <ContextExtractor></ContextExtractor>
  155. </SWRConfig>,
  156. elem,
  157. renderMainComponents,
  158. );
  159. }
  160. else {
  161. renderMainComponents();
  162. }
  163. // initialize scrollpos-styler
  164. ScrollPosStyler.init();