|
|
@@ -8,7 +8,6 @@ import type { Nullable, IPageHasId, IPageToDeleteWithMeta } from '@growi/core';
|
|
|
import { useGlobalSocket } from '@growi/core/dist/swr';
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
import { useRouter } from 'next/router';
|
|
|
-import { debounce } from 'throttle-debounce';
|
|
|
|
|
|
import { toastError, toastSuccess } from '~/client/util/toastr';
|
|
|
import type { IPageForItem } from '~/interfaces/page';
|
|
|
@@ -23,7 +22,7 @@ import {
|
|
|
useSWRxPageAncestorsChildren, useSWRxRootPage, mutatePageTree, mutatePageList,
|
|
|
} from '~/stores/page-listing';
|
|
|
import { mutateSearching } from '~/stores/search';
|
|
|
-import { usePageTreeDescCountMap, useSidebarScrollerRef } from '~/stores/ui';
|
|
|
+import { usePageTreeDescCountMap } from '~/stores/ui';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
import { ItemNode, type TreeItemProps } from '../TreeItem';
|
|
|
@@ -116,7 +115,6 @@ export const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
|
|
|
const { data: currentPagePath } = useCurrentPagePath();
|
|
|
const { open: openDuplicateModal } = usePageDuplicateModal();
|
|
|
const { open: openDeleteModal } = usePageDeleteModal();
|
|
|
- const { data: sidebarScrollerRef } = useSidebarScrollerRef();
|
|
|
|
|
|
const { data: socket } = useGlobalSocket();
|
|
|
const { data: ptDescCountMap, update: updatePtDescCountMap } = usePageTreeDescCountMap();
|
|
|
@@ -124,9 +122,6 @@ export const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
|
|
|
// for mutation
|
|
|
const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
|
|
|
|
|
|
- const [isInitialScrollCompleted, setIsInitialScrollCompleted] = useState(false);
|
|
|
-
|
|
|
- const rootElemRef = useRef(null);
|
|
|
|
|
|
const renderingCondition = useMemo(() => {
|
|
|
return {
|
|
|
@@ -201,55 +196,6 @@ export const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
|
|
|
openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
|
|
|
}, [currentPagePath, mutateCurrentPage, openDeleteModal, router, t]);
|
|
|
|
|
|
- // *************************** Scroll on init ***************************
|
|
|
- const scrollOnInit = useCallback(() => {
|
|
|
- const scrollTargetElement = document.getElementById('grw-pagetree-current-page-item');
|
|
|
-
|
|
|
- if (sidebarScrollerRef?.current == null || scrollTargetElement == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- logger.debug('scrollOnInit has invoked');
|
|
|
-
|
|
|
- const scrollElement = sidebarScrollerRef.current;
|
|
|
-
|
|
|
- // NOTE: could not use scrollIntoView
|
|
|
- // https://stackoverflow.com/questions/11039885/scrollintoview-causing-the-whole-page-to-move
|
|
|
-
|
|
|
- // calculate the center point
|
|
|
- const scrollTop = scrollTargetElement.offsetTop - scrollElement.getBoundingClientRect().height / 2;
|
|
|
- scrollElement.scrollTo({ top: scrollTop });
|
|
|
-
|
|
|
- setIsInitialScrollCompleted(true);
|
|
|
- }, [sidebarScrollerRef]);
|
|
|
-
|
|
|
- const scrollOnInitDebounced = useMemo(() => debounce(500, scrollOnInit), [scrollOnInit]);
|
|
|
-
|
|
|
- useEffect(() => {
|
|
|
- if (!isSecondStageRenderingCondition(renderingCondition) || isInitialScrollCompleted) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const rootElement = rootElemRef.current as HTMLElement | null;
|
|
|
- if (rootElement == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const observerCallback = (mutationRecords: MutationRecord[]) => {
|
|
|
- mutationRecords.forEach(() => scrollOnInitDebounced());
|
|
|
- };
|
|
|
-
|
|
|
- const observer = new MutationObserver(observerCallback);
|
|
|
- observer.observe(rootElement, { childList: true, subtree: true });
|
|
|
-
|
|
|
- // first call for the situation that all rendering is complete at this point
|
|
|
- scrollOnInitDebounced();
|
|
|
-
|
|
|
- return () => {
|
|
|
- observer.disconnect();
|
|
|
- };
|
|
|
- }, [isInitialScrollCompleted, renderingCondition, scrollOnInitDebounced]);
|
|
|
- // ******************************* end *******************************
|
|
|
|
|
|
if (error1 != null || error2 != null) {
|
|
|
// TODO: improve message
|
|
|
@@ -276,7 +222,7 @@ export const ItemsTree = (props: ItemsTreeProps): JSX.Element => {
|
|
|
|
|
|
if (initialItemNode != null) {
|
|
|
return (
|
|
|
- <ul className={`grw-pagetree ${moduleClass} list-group py-4`} ref={rootElemRef}>
|
|
|
+ <ul className={`grw-pagetree ${moduleClass} list-group py-4`}>
|
|
|
<CustomTreeItem
|
|
|
key={initialItemNode.page.path}
|
|
|
targetPathOrId={targetPathOrId}
|