From b257e9b5d9855d6e9302fcbf968b8e93adf1fce6 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Fri, 7 Feb 2025 01:39:58 +0700 Subject: [PATCH] scroll to cell on flow view change --- src/constants/dom/index.ts | 2 + .../FlowCellText/styles.module.scss | 4 -- .../FlowGrid/components/FlowCell/index.tsx | 40 ++++++++++++++----- .../components/FlowCell/styles.module.scss | 7 +--- src/containers/flow/FlowGrid/index.tsx | 2 +- src/utils/dom.ts | 12 ++++++ 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/constants/dom/index.ts b/src/constants/dom/index.ts index be18c3a4..3d9462a8 100644 --- a/src/constants/dom/index.ts +++ b/src/constants/dom/index.ts @@ -5,3 +5,5 @@ export const isTablet = () => { return window.innerWidth < 599; }; + +export const headerHeight = 64; // px diff --git a/src/containers/flow/FlowGrid/components/FlowCell/components/FlowCellText/styles.module.scss b/src/containers/flow/FlowGrid/components/FlowCell/components/FlowCellText/styles.module.scss index 2624f594..0a2896ab 100644 --- a/src/containers/flow/FlowGrid/components/FlowCell/components/FlowCellText/styles.module.scss +++ b/src/containers/flow/FlowGrid/components/FlowCell/components/FlowCellText/styles.module.scss @@ -9,8 +9,4 @@ .heading { margin-bottom: 0.25em; - - h4 { - // line-height: 1.1em; - } } diff --git a/src/containers/flow/FlowGrid/components/FlowCell/index.tsx b/src/containers/flow/FlowGrid/components/FlowCell/index.tsx index ed305920..ffd837b9 100644 --- a/src/containers/flow/FlowGrid/components/FlowCell/index.tsx +++ b/src/containers/flow/FlowGrid/components/FlowCell/index.tsx @@ -1,4 +1,4 @@ -import { FC, useMemo } from 'react'; +import { FC, useCallback, useMemo } from 'react'; import classNames from 'classnames'; @@ -9,6 +9,8 @@ import { useWindowSize } from '~/hooks/dom/useWindowSize'; import { useFlowCellControls } from '~/hooks/flow/useFlowCellControls'; import { FlowDisplay, INode } from '~/types'; +import { isFullyVisible } from '../../../../../utils/dom'; + import { CellShade } from './components/CellShade'; import { FlowCellImage } from './components/FlowCellImage'; import { FlowCellMenu } from './components/FlowCellMenu'; @@ -25,7 +27,7 @@ interface Props { text?: string; flow: FlowDisplay; canEdit?: boolean; - onChangeCellView: (id: INode['id'], flow: FlowDisplay) => void; + onChange: (id: INode['id'], flow: FlowDisplay) => void; } const FlowCell: FC = ({ @@ -37,7 +39,7 @@ const FlowCell: FC = ({ text, title, canEdit = false, - onChangeCellView, + onChange, }) => { const { isTablet } = useWindowSize(); @@ -45,6 +47,30 @@ const FlowCell: FC = ({ ((!!flow.display && flow.display !== 'single') || !image) && flow.show_description && !!text; + + const { + isActive: isMenuActive, + activate, + ref, + deactivate, + } = useClickOutsideFocus(); + + const onChangeWithScroll = useCallback( + (...args) => { + onChange(...args); + + setTimeout(() => { + if (!isFullyVisible(ref.current)) { + ref.current?.scrollIntoView({ + behavior: 'auto', + block: 'center', + }); + } + }, 0); + }, + [onChange, ref], + ); + const { hasDescription, setViewHorizontal, @@ -52,13 +78,7 @@ const FlowCell: FC = ({ setViewQuadro, setViewSingle, toggleViewDescription, - } = useFlowCellControls(id, text, flow, onChangeCellView); - const { - isActive: isMenuActive, - activate, - ref, - deactivate, - } = useClickOutsideFocus(); + } = useFlowCellControls(id, text, flow, onChangeWithScroll); const shadeSize = useMemo(() => { const min = isTablet ? 10 : 15; diff --git a/src/containers/flow/FlowGrid/components/FlowCell/styles.module.scss b/src/containers/flow/FlowGrid/components/FlowCell/styles.module.scss index 708196e0..6a0fb5ac 100644 --- a/src/containers/flow/FlowGrid/components/FlowCell/styles.module.scss +++ b/src/containers/flow/FlowGrid/components/FlowCell/styles.module.scss @@ -116,12 +116,7 @@ } .display_modal { - @include appear; - position: absolute; - top: 0; - left: 0; - right: 0; - bottom: 0; + inset: 0; z-index: 11; } diff --git a/src/containers/flow/FlowGrid/index.tsx b/src/containers/flow/FlowGrid/index.tsx index 637a0d00..7bed5279 100644 --- a/src/containers/flow/FlowGrid/index.tsx +++ b/src/containers/flow/FlowGrid/index.tsx @@ -46,7 +46,7 @@ export const FlowGrid: FC = observer( text={node.description} title={node.title} canEdit={fetched && isUser && canEditNode(node, user)} - onChangeCellView={onChangeCellView} + onChange={onChangeCellView} /> ))} diff --git a/src/utils/dom.ts b/src/utils/dom.ts index dd0b9d4d..473877d0 100644 --- a/src/utils/dom.ts +++ b/src/utils/dom.ts @@ -10,6 +10,7 @@ import { COMMENT_BLOCK_TYPES, ICommentBlock, } from '~/constants/comment'; +import { headerHeight } from '~/constants/dom'; import { IFile, ValueOf } from '~/types'; import { CONFIG } from '~/utils/config'; import { @@ -213,3 +214,14 @@ export const sizeOf = (bytes: number): string => { (bytes / Math.pow(1024, e)).toFixed(2) + ' ' + ' KMGTP'.charAt(e) + 'B' ); }; + +/** Tells if element is in view */ +export const isFullyVisible = (element?: HTMLElement): boolean => { + if (!element) { + return false; + } + + const rect = element.getBoundingClientRect(); + + return rect?.top > headerHeight && rect?.bottom < window.innerHeight; +};