|
|
@@ -1,11 +1,15 @@
|
|
|
-import React, { FC } from 'react';
|
|
|
-
|
|
|
+import React, {
|
|
|
+ FC, useRef, useState, useEffect,
|
|
|
+} from 'react';
|
|
|
import { IPageSearchResultData } from '../../interfaces/search';
|
|
|
|
|
|
import RevisionLoader from '../Page/RevisionLoader';
|
|
|
import AppContainer from '../../client/services/AppContainer';
|
|
|
+import { smoothScrollIntoView } from '~/client/util/smooth-scroll';
|
|
|
import SearchResultContentSubNavigation from './SearchResultContentSubNavigation';
|
|
|
|
|
|
+const SCROLL_OFFSET_TOP = 150;
|
|
|
+
|
|
|
// TODO : set focusedPage type to ?IPageSearchResultData once #80214 is merged
|
|
|
// PR: https://github.com/weseek/growi/pull/4649
|
|
|
|
|
|
@@ -17,6 +21,20 @@ type Props ={
|
|
|
|
|
|
|
|
|
const SearchResultContent: FC<Props> = (props: Props) => {
|
|
|
+ const [isRevisionBodyRendered, setIsRevisionBodyRendered] = useState(false);
|
|
|
+ const contentRef = useRef(null);
|
|
|
+ useEffect(() => {
|
|
|
+ // reset state
|
|
|
+ setIsRevisionBodyRendered(false);
|
|
|
+ if (isRevisionBodyRendered) {
|
|
|
+ const searchResultPageContent = contentRef.current as HTMLElement| null;
|
|
|
+ if (searchResultPageContent == null) return;
|
|
|
+ const highlightedWord = searchResultPageContent?.querySelector('.highlighted-keyword') as HTMLElement | null;
|
|
|
+ if (highlightedWord == null) return;
|
|
|
+ smoothScrollIntoView(highlightedWord, SCROLL_OFFSET_TOP, searchResultPageContent);
|
|
|
+ }
|
|
|
+ }, [isRevisionBodyRendered]);
|
|
|
+
|
|
|
const page = props.focusedSearchResultData?.pageData;
|
|
|
// return if page is null
|
|
|
if (page == null) return <></>;
|
|
|
@@ -29,13 +47,14 @@ const SearchResultContent: FC<Props> = (props: Props) => {
|
|
|
path={page.path}
|
|
|
>
|
|
|
</SearchResultContentSubNavigation>
|
|
|
- <div className="search-result-page-content">
|
|
|
+ <div className="search-result-page-content" ref={contentRef}>
|
|
|
<RevisionLoader
|
|
|
growiRenderer={growiRenderer}
|
|
|
pageId={page._id}
|
|
|
pagePath={page.path}
|
|
|
revisionId={page.revision}
|
|
|
highlightKeywords={props.searchingKeyword}
|
|
|
+ onRevisionBodyRendered={setIsRevisionBodyRendered}
|
|
|
/>
|
|
|
</div>
|
|
|
</div>
|