| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- import type { FC, JSX } from 'react';
- import { useCallback, useEffect, useRef } from 'react';
- import dynamic from 'next/dynamic';
- import type { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '@growi/core';
- import { getIdStringForRef } from '@growi/core';
- import { useTranslation } from 'next-i18next';
- import { DropdownItem } from 'reactstrap';
- import { debounce } from 'throttle-debounce';
- import type {
- AdditionalMenuItemsRendererProps,
- ForceHideMenuItems,
- } from '~/client/components/Common/Dropdown/PageItemControl';
- import type { RevisionLoaderProps } from '~/client/components/Page/RevisionLoader';
- import { watchRenderingAndReScroll } from '~/client/hooks/use-content-auto-scroll/watch-rendering-and-rescroll';
- import { exportAsMarkdown } from '~/client/services/page-operation';
- import { scrollWithinContainer } from '~/client/util/smooth-scroll';
- import { toastSuccess } from '~/client/util/toastr';
- import { PagePathNav } from '~/components/Common/PagePathNav';
- import type { IPageWithSearchMeta } from '~/interfaces/search';
- import type {
- OnDeletedFunction,
- OnDuplicatedFunction,
- OnRenamedFunction,
- } from '~/interfaces/ui';
- import { useShouldExpandContent } from '~/services/layout/use-should-expand-content';
- import { useCurrentUser } from '~/states/global';
- import { usePageDeleteModalActions } from '~/states/ui/modal/page-delete';
- import { usePageDuplicateModalActions } from '~/states/ui/modal/page-duplicate';
- import { usePageRenameModalActions } from '~/states/ui/modal/page-rename';
- import {
- mutatePageList,
- mutatePageTree,
- mutateRecentlyUpdated,
- } from '~/stores/page-listing';
- import { useSearchResultOptions } from '~/stores/renderer';
- import { mutateSearching } from '~/stores/search';
- import styles from './SearchResultContent.module.scss';
- const moduleClass = styles['search-result-content'];
- const _fluidLayoutClass = styles['fluid-layout'];
- const PageControls = dynamic(
- () =>
- import('~/client/components/PageControls').then((mod) => mod.PageControls),
- { ssr: false },
- );
- const RevisionLoader = dynamic<RevisionLoaderProps>(
- () =>
- import('~/client/components/Page/RevisionLoader').then(
- (mod) => mod.RevisionLoader,
- ),
- { ssr: false },
- );
- const PageComment = dynamic(
- () =>
- import('~/client/components/PageComment').then((mod) => mod.PageComment),
- { ssr: false },
- );
- const PageContentFooter = dynamic(
- () =>
- import('~/components/PageView/PageContentFooter').then(
- (mod) => mod.PageContentFooter,
- ),
- { ssr: false },
- );
- type AdditionalMenuItemsProps = AdditionalMenuItemsRendererProps & {
- pageId: string;
- revisionId: string;
- };
- const AdditionalMenuItems = (props: AdditionalMenuItemsProps): JSX.Element => {
- const { t } = useTranslation();
- const { pageId, revisionId } = props;
- return (
- // Export markdown
- <DropdownItem
- onClick={() => exportAsMarkdown(pageId, revisionId, 'md')}
- className="grw-page-control-dropdown-item"
- >
- <span className="material-symbols-outlined me-1 grw-page-control-dropdown-icon">
- cloud_download
- </span>
- {t('page_export.export_page_markdown')}
- </DropdownItem>
- );
- };
- const SCROLL_OFFSET_TOP = 30;
- const MUTATION_OBSERVER_CONFIG = { childList: true, subtree: true }; // omit 'subtree: true'
- type Props = {
- pageWithMeta: IPageWithSearchMeta;
- highlightKeywords?: string[];
- showPageControlDropdown?: boolean;
- forceHideMenuItems?: ForceHideMenuItems;
- };
- const scrollToTargetWithinContainer = (
- target: HTMLElement,
- container: HTMLElement,
- ): void => {
- const distance =
- target.getBoundingClientRect().top -
- container.getBoundingClientRect().top -
- SCROLL_OFFSET_TOP;
- scrollWithinContainer(container, distance);
- };
- const scrollToFirstHighlightedKeyword = (scrollElement: HTMLElement): void => {
- // use querySelector to intentionally get the first element found
- const toElem = scrollElement.querySelector(
- '.highlighted-keyword',
- ) as HTMLElement | null;
- if (toElem == null) return;
- scrollToTargetWithinContainer(toElem, scrollElement);
- };
- const scrollToFirstHighlightedKeywordDebounced = debounce(
- 500,
- scrollToFirstHighlightedKeyword,
- );
- export const SearchResultContent: FC<Props> = (props: Props) => {
- const scrollElementRef = useRef<HTMLDivElement | null>(null);
- const { pageWithMeta } = props;
- const page = pageWithMeta.data;
- // *************************** Keyword Scroll ***************************
- // biome-ignore lint/correctness/useExhaustiveDependencies: page._id is a trigger dep: re-run this effect when the selected page changes
- useEffect(() => {
- const scrollElement = scrollElementRef.current;
- if (scrollElement == null) return;
- const scrollToKeyword = (): boolean => {
- const toElem = scrollElement.querySelector(
- '.highlighted-keyword',
- ) as HTMLElement | null;
- if (toElem == null) return false;
- scrollToTargetWithinContainer(toElem, scrollElement);
- return true;
- };
- const observer = new MutationObserver(() => {
- scrollToFirstHighlightedKeywordDebounced(scrollElement);
- });
- observer.observe(scrollElement, MUTATION_OBSERVER_CONFIG);
- // Re-scroll to keyword after async renderers (drawio/mermaid) cause layout shifts
- const cleanupWatch = watchRenderingAndReScroll(
- scrollElement,
- scrollToKeyword,
- );
- return cleanupWatch;
- }, [page._id]);
- // ******************************* end *******************************
- const { highlightKeywords, showPageControlDropdown, forceHideMenuItems } =
- props;
- const { t } = useTranslation();
- const { open: openDuplicateModal } = usePageDuplicateModalActions();
- const { open: openRenameModal } = usePageRenameModalActions();
- const { open: openDeleteModal } = usePageDeleteModalActions();
- const { data: rendererOptions } = useSearchResultOptions(
- pageWithMeta.data.path,
- highlightKeywords,
- );
- const currentUser = useCurrentUser();
- const shouldExpandContent = useShouldExpandContent(page);
- const duplicateItemClickedHandler = useCallback(
- async (pageToDuplicate) => {
- const duplicatedHandler: OnDuplicatedFunction = (fromPath, _toPath) => {
- toastSuccess(t('duplicated_pages', { fromPath }));
- mutatePageTree();
- mutateRecentlyUpdated();
- mutateSearching();
- mutatePageList();
- };
- openDuplicateModal(pageToDuplicate, { onDuplicated: duplicatedHandler });
- },
- [openDuplicateModal, t],
- );
- const renameItemClickedHandler = useCallback(
- (pageToRename: IPageToRenameWithMeta) => {
- const renamedHandler: OnRenamedFunction = (path) => {
- toastSuccess(t('renamed_pages', { path }));
- mutatePageTree();
- mutateRecentlyUpdated();
- mutateSearching();
- mutatePageList();
- };
- openRenameModal(pageToRename, { onRenamed: renamedHandler });
- },
- [openRenameModal, t],
- );
- const onDeletedHandler: OnDeletedFunction = useCallback(
- (pathOrPathsToDelete, isRecursively, isCompletely) => {
- if (typeof pathOrPathsToDelete !== 'string') {
- return;
- }
- const path = pathOrPathsToDelete;
- if (isCompletely) {
- toastSuccess(t('deleted_pages_completely', { path }));
- } else {
- toastSuccess(t('deleted_pages', { path }));
- }
- mutatePageTree();
- mutateRecentlyUpdated();
- mutateSearching();
- mutatePageList();
- },
- [t],
- );
- const deleteItemClickedHandler = useCallback(
- (pageToDelete: IPageToDeleteWithMeta) => {
- openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
- },
- [onDeletedHandler, openDeleteModal],
- );
- const RightComponent = useCallback(() => {
- if (page == null) {
- return <></>;
- }
- const revisionId =
- page.revision != null ? getIdStringForRef(page.revision) : null;
- const additionalMenuItemRenderer =
- revisionId != null
- ? (props) => (
- <AdditionalMenuItems
- {...props}
- pageId={page._id}
- revisionId={revisionId}
- />
- )
- : undefined;
- return (
- <div className="d-flex flex-column flex-row-reverse flex px-2 py-1">
- <PageControls
- pageId={page._id}
- revisionId={revisionId}
- path={page.path}
- expandContentWidth={shouldExpandContent}
- showPageControlDropdown={showPageControlDropdown}
- forceHideMenuItems={forceHideMenuItems}
- additionalMenuItemRenderer={additionalMenuItemRenderer}
- onClickDuplicateMenuItem={duplicateItemClickedHandler}
- onClickRenameMenuItem={renameItemClickedHandler}
- onClickDeleteMenuItem={deleteItemClickedHandler}
- />
- </div>
- );
- }, [
- page,
- shouldExpandContent,
- showPageControlDropdown,
- forceHideMenuItems,
- duplicateItemClickedHandler,
- renameItemClickedHandler,
- deleteItemClickedHandler,
- ]);
- const fluidLayoutClass = shouldExpandContent ? _fluidLayoutClass : '';
- return (
- <div
- key={page._id}
- data-testid="search-result-content"
- className={`dynamic-layout-root ${moduleClass} ${fluidLayoutClass}`}
- >
- <RightComponent />
- <div className="container-lg grw-container-convertible pt-2 pb-2">
- <PagePathNav
- pageId={page._id}
- pagePath={page.path}
- isWipPage={page.wip}
- formerLinkClassName="small"
- latterLinkClassName="fs-3 text-truncate"
- />
- </div>
- <div
- id="search-result-content-body-container"
- ref={scrollElementRef}
- className="search-result-content-body-container container-lg grw-container-convertible overflow-y-scroll"
- >
- {page.revision != null && rendererOptions != null && (
- <RevisionLoader
- rendererOptions={rendererOptions}
- pageId={page._id}
- revisionId={page.revision}
- />
- )}
- {page.revision != null && (
- <PageComment
- rendererOptions={rendererOptions}
- pageId={page._id}
- pagePath={page.path}
- revision={page.revision}
- currentUser={currentUser}
- isReadOnly
- />
- )}
- <PageContentFooter page={page} />
- </div>
- </div>
- );
- };
|