SearchResultContent.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import type { FC } from 'react';
  2. import React, {
  3. useCallback, useEffect, useRef,
  4. } from 'react';
  5. import { getIdStringForRef } from '@growi/core';
  6. import type { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '@growi/core';
  7. import { useTranslation } from 'next-i18next';
  8. import dynamic from 'next/dynamic';
  9. import { animateScroll } from 'react-scroll';
  10. import { DropdownItem } from 'reactstrap';
  11. import { debounce } from 'throttle-debounce';
  12. import { exportAsMarkdown } from '~/client/services/page-operation';
  13. import { toastSuccess } from '~/client/util/toastr';
  14. import { PagePathNav } from '~/components/Common/PagePathNav';
  15. import type { IPageWithSearchMeta } from '~/interfaces/search';
  16. import type { OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction } from '~/interfaces/ui';
  17. import { useShouldExpandContent } from '~/services/layout/use-should-expand-content';
  18. import { useCurrentUser } from '~/stores-universal/context';
  19. import {
  20. usePageDuplicateModal, usePageRenameModal, usePageDeleteModal,
  21. } from '~/stores/modal';
  22. import { mutatePageList, mutatePageTree, mutateRecentlyUpdated } from '~/stores/page-listing';
  23. import { useSearchResultOptions } from '~/stores/renderer';
  24. import { mutateSearching } from '~/stores/search';
  25. import type { AdditionalMenuItemsRendererProps, ForceHideMenuItems } from '../Common/Dropdown/PageItemControl';
  26. import type { RevisionLoaderProps } from '../Page/RevisionLoader';
  27. import styles from './SearchResultContent.module.scss';
  28. const moduleClass = styles['search-result-content'];
  29. const _fluidLayoutClass = styles['fluid-layout'];
  30. const PageControls = dynamic(() => import('../PageControls').then(mod => mod.PageControls), { ssr: false });
  31. const RevisionLoader = dynamic<RevisionLoaderProps>(() => import('../Page/RevisionLoader').then(mod => mod.RevisionLoader), { ssr: false });
  32. const PageComment = dynamic(() => import('../PageComment').then(mod => mod.PageComment), { ssr: false });
  33. const PageContentFooter = dynamic(
  34. () => import('~/components/PageView/PageContentFooter').then(mod => mod.PageContentFooter),
  35. { ssr: false },
  36. );
  37. type AdditionalMenuItemsProps = AdditionalMenuItemsRendererProps & {
  38. pageId: string,
  39. revisionId: string,
  40. }
  41. const AdditionalMenuItems = (props: AdditionalMenuItemsProps): JSX.Element => {
  42. const { t } = useTranslation();
  43. const { pageId, revisionId } = props;
  44. return (
  45. // Export markdown
  46. <DropdownItem
  47. onClick={() => exportAsMarkdown(pageId, revisionId, 'md')}
  48. className="grw-page-control-dropdown-item"
  49. >
  50. <span className="material-symbols-outlined me-1 grw-page-control-dropdown-icon">cloud_download</span>
  51. {t('page_export.export_page_markdown')}
  52. </DropdownItem>
  53. );
  54. };
  55. const SCROLL_OFFSET_TOP = 30;
  56. const MUTATION_OBSERVER_CONFIG = { childList: true, subtree: true }; // omit 'subtree: true'
  57. type Props ={
  58. pageWithMeta : IPageWithSearchMeta,
  59. highlightKeywords?: string[],
  60. showPageControlDropdown?: boolean,
  61. forceHideMenuItems?: ForceHideMenuItems,
  62. }
  63. const scrollToFirstHighlightedKeyword = (scrollElement: HTMLElement): void => {
  64. // use querySelector to intentionally get the first element found
  65. const toElem = scrollElement.querySelector('.highlighted-keyword') as HTMLElement | null;
  66. if (toElem == null) {
  67. return;
  68. }
  69. const distance = toElem.getBoundingClientRect().top - scrollElement.getBoundingClientRect().top - SCROLL_OFFSET_TOP;
  70. animateScroll.scrollMore(distance, {
  71. containerId: scrollElement.id,
  72. duration: 200,
  73. });
  74. };
  75. const scrollToFirstHighlightedKeywordDebounced = debounce(500, scrollToFirstHighlightedKeyword);
  76. export const SearchResultContent: FC<Props> = (props: Props) => {
  77. const scrollElementRef = useRef<HTMLDivElement|null>(null);
  78. // *************************** Auto Scroll ***************************
  79. useEffect(() => {
  80. const scrollElement = scrollElementRef.current;
  81. if (scrollElement == null) return;
  82. const observer = new MutationObserver(() => {
  83. scrollToFirstHighlightedKeywordDebounced(scrollElement);
  84. });
  85. observer.observe(scrollElement, MUTATION_OBSERVER_CONFIG);
  86. // no cleanup function -- 2023.07.31 Yuki Takei
  87. // see: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
  88. // > You can call observe() multiple times on the same MutationObserver
  89. // > to watch for changes to different parts of the DOM tree and/or different types of changes.
  90. });
  91. // ******************************* end *******************************
  92. const {
  93. pageWithMeta,
  94. highlightKeywords,
  95. showPageControlDropdown,
  96. forceHideMenuItems,
  97. } = props;
  98. const { t } = useTranslation();
  99. const page = pageWithMeta.data;
  100. const { open: openDuplicateModal } = usePageDuplicateModal();
  101. const { open: openRenameModal } = usePageRenameModal();
  102. const { open: openDeleteModal } = usePageDeleteModal();
  103. const { data: rendererOptions } = useSearchResultOptions(pageWithMeta.data.path, highlightKeywords);
  104. const { data: currentUser } = useCurrentUser();
  105. const shouldExpandContent = useShouldExpandContent(page);
  106. const duplicateItemClickedHandler = useCallback(async(pageToDuplicate) => {
  107. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  108. const duplicatedHandler: OnDuplicatedFunction = (fromPath, toPath) => {
  109. toastSuccess(t('duplicated_pages', { fromPath }));
  110. mutatePageTree();
  111. mutateRecentlyUpdated();
  112. mutateSearching();
  113. mutatePageList();
  114. };
  115. openDuplicateModal(pageToDuplicate, { onDuplicated: duplicatedHandler });
  116. }, [openDuplicateModal, t]);
  117. const renameItemClickedHandler = useCallback((pageToRename: IPageToRenameWithMeta) => {
  118. const renamedHandler: OnRenamedFunction = (path) => {
  119. toastSuccess(t('renamed_pages', { path }));
  120. mutatePageTree();
  121. mutateRecentlyUpdated();
  122. mutateSearching();
  123. mutatePageList();
  124. };
  125. openRenameModal(pageToRename, { onRenamed: renamedHandler });
  126. }, [openRenameModal, t]);
  127. const onDeletedHandler: OnDeletedFunction = useCallback((pathOrPathsToDelete, isRecursively, isCompletely) => {
  128. if (typeof pathOrPathsToDelete !== 'string') {
  129. return;
  130. }
  131. const path = pathOrPathsToDelete;
  132. if (isCompletely) {
  133. toastSuccess(t('deleted_pages_completely', { path }));
  134. }
  135. else {
  136. toastSuccess(t('deleted_pages', { path }));
  137. }
  138. mutatePageTree();
  139. mutateRecentlyUpdated();
  140. mutateSearching();
  141. mutatePageList();
  142. }, [t]);
  143. const deleteItemClickedHandler = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
  144. openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
  145. }, [onDeletedHandler, openDeleteModal]);
  146. const RightComponent = useCallback(() => {
  147. if (page == null) {
  148. return <></>;
  149. }
  150. const revisionId = page.revision != null ? getIdStringForRef(page.revision) : null;
  151. const additionalMenuItemRenderer = revisionId != null
  152. ? props => <AdditionalMenuItems {...props} pageId={page._id} revisionId={revisionId} />
  153. : undefined;
  154. return (
  155. <div className="d-flex flex-column flex-row-reverse flex px-2 py-1">
  156. <PageControls
  157. pageId={page._id}
  158. revisionId={revisionId}
  159. path={page.path}
  160. expandContentWidth={shouldExpandContent}
  161. showPageControlDropdown={showPageControlDropdown}
  162. forceHideMenuItems={forceHideMenuItems}
  163. additionalMenuItemRenderer={additionalMenuItemRenderer}
  164. onClickDuplicateMenuItem={duplicateItemClickedHandler}
  165. onClickRenameMenuItem={renameItemClickedHandler}
  166. onClickDeleteMenuItem={deleteItemClickedHandler}
  167. />
  168. </div>
  169. );
  170. }, [page, shouldExpandContent, showPageControlDropdown, forceHideMenuItems,
  171. duplicateItemClickedHandler, renameItemClickedHandler, deleteItemClickedHandler]);
  172. const fluidLayoutClass = shouldExpandContent ? _fluidLayoutClass : '';
  173. return (
  174. <div
  175. key={page._id}
  176. data-testid="search-result-content"
  177. className={`dynamic-layout-root ${moduleClass} ${fluidLayoutClass}`}
  178. >
  179. <RightComponent />
  180. <div className="container-lg grw-container-convertible pt-2 pb-2">
  181. <PagePathNav pageId={page._id} pagePath={page.path} formerLinkClassName="small" latterLinkClassName="fs-3 text-truncate" />
  182. </div>
  183. <div
  184. id="search-result-content-body-container"
  185. ref={scrollElementRef}
  186. className="search-result-content-body-container container-lg grw-container-convertible overflow-y-scroll"
  187. >
  188. { page.revision != null && rendererOptions != null && (
  189. <RevisionLoader
  190. rendererOptions={rendererOptions}
  191. pageId={page._id}
  192. revisionId={page.revision}
  193. />
  194. )}
  195. { page.revision != null && (
  196. <PageComment
  197. rendererOptions={rendererOptions}
  198. pageId={page._id}
  199. pagePath={page.path}
  200. revision={page.revision}
  201. currentUser={currentUser}
  202. isReadOnly
  203. />
  204. )}
  205. <PageContentFooter
  206. page={page}
  207. />
  208. </div>
  209. </div>
  210. );
  211. };