PromucFlow_constructor/app/client/src/utils/hooks/useReflow.ts
Abhinav Jha 67f7d217a1
feat: (Internal Change) Add auto height computation functions (#17962)
* Add auto height reflow functions libary

* Add comments in hard to understand parts

* feat: auto height reflow lib (#17978)

added 2 tests for boxHelper and 1 simple test for reflow computeChangeInPositionBasedOnDelta

Co-authored-by: Ankur Singhal <ankurrsinghal@gmail.com>

* Reduce one loop. Fix typo

* Add helper functions and use them in lib

* Use helper function

* Add types

* Fix issue where widgets don't get close to the bottom most above widget if that widget hasn't changed

* fix: auto height reflow lib merge release (#18193)

* feat: show number of tabs on the header (#18071)

* number of tabs displayed alongside label

* styling for span removed

* feature added and cypress test cases written

* code refactoring after review

* Update top contributors

* feat: Auto-height add reducers and actions (#17953)

* Add reducers for auto height feature (Internal Change, No changes reflected to users)
Co-authored-by: ankurrsinghal <ankur@appsmith.com>

* [Bug] Incorrect count of users in workspace when adding multiple users (#17728)

fix: filtering unique users by userId

Co-authored-by: Anubhav <anubhav@appsmith.com>

* fix: Instrumentation for execution errors (#18093)

* fix: Instrumentation for execution errors

* added widget editor error event

* fix:  Sidebar heading fontSize & checkbox alignment  (#18104)

sidebar heading & checkbox alignment to heading

* chore: added type for feature flag (#18152)

add: new type for env variable

* Update top contributors

* feat: [Context Switching]: Change focus target and fix cursor position (#17794)

Co-authored-by: 
rahulramesha <rahul@appsmith.com>

* fix: JS Objects save failures due to AST changes (#18018)

* fix: update local_testing.sh to build image for external contributor PRs (#18024)

* chore: use `typography` and `getTypographyFromKey` from the design-system (#18050)

Change typography imports, change function call

* dummy

Co-authored-by: Rishabh Kashyap <rishabh.kashyap@appsmith.com>
Co-authored-by: Appsmith Bot <74705725+appsmith-bot@users.noreply.github.com>
Co-authored-by: Abhinav Jha <abhinav@appsmith.com>
Co-authored-by: Ankit Srivastava <67647761+ankitsrivas14@users.noreply.github.com>
Co-authored-by: Anubhav <anubhav@appsmith.com>
Co-authored-by: ChandanBalajiBP <104058110+ChandanBalajiBP@users.noreply.github.com>
Co-authored-by: Rohit Agarwal <rohit_agarwal@live.in>
Co-authored-by: Ayush Pahwa <ayush@appsmith.com>
Co-authored-by: Hetu Nandu <hetu@appsmith.com>
Co-authored-by: subratadeypappu <subrata@appsmith.com>
Co-authored-by: Sumit Kumar <sumit@appsmith.com>
Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com>
Co-authored-by: Ankur Singhal <ankurrsinghal@gmail.com>

Co-authored-by: ankurrsinghal <ankur@appsmith.com>
Co-authored-by: Ankur Singhal <ankurrsinghal@gmail.com>
Co-authored-by: Rishabh Kashyap <rishabh.kashyap@appsmith.com>
Co-authored-by: Appsmith Bot <74705725+appsmith-bot@users.noreply.github.com>
Co-authored-by: Ankit Srivastava <67647761+ankitsrivas14@users.noreply.github.com>
Co-authored-by: Anubhav <anubhav@appsmith.com>
Co-authored-by: ChandanBalajiBP <104058110+ChandanBalajiBP@users.noreply.github.com>
Co-authored-by: Rohit Agarwal <rohit_agarwal@live.in>
Co-authored-by: Ayush Pahwa <ayush@appsmith.com>
Co-authored-by: Hetu Nandu <hetu@appsmith.com>
Co-authored-by: subratadeypappu <subrata@appsmith.com>
Co-authored-by: Sumit Kumar <sumit@appsmith.com>
Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com>
2022-11-20 11:42:32 +05:30

190 lines
5.7 KiB
TypeScript

import { reflowMoveAction, stopReflowAction } from "actions/reflowActions";
import { OccupiedSpace, WidgetSpace } from "constants/CanvasEditorConstants";
import { isEmpty, throttle } from "lodash";
import { useEffect, useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import { getContainerWidgetSpacesSelectorWhileMoving } from "selectors/editorSelectors";
import { reflow } from "reflow";
import {
CollidingSpace,
CollidingSpaceMap,
GridProps,
MovementLimitMap,
PrevReflowState,
ReflowDirection,
ReflowedSpaceMap,
SecondOrderCollisionMap,
} from "reflow/reflowTypes";
import {
getBottomMostRow,
getLimitedMovementMap,
getSpacesMapFromArray,
} from "reflow/reflowUtils";
import { getBottomRowAfterReflow } from "utils/reflowHookUtils";
import { checkIsDropTarget } from "components/designSystems/appsmith/PositionedContainer";
import { getIsReflowing } from "selectors/widgetReflowSelectors";
import { AppState } from "@appsmith/reducers";
import { areIntersecting } from "utils/boxHelpers";
type WidgetCollidingSpace = CollidingSpace & {
type: string;
};
type WidgetCollidingSpaceMap = {
horizontal: WidgetCollisionMap;
vertical: WidgetCollisionMap;
};
export type WidgetCollisionMap = {
[key: string]: WidgetCollidingSpace;
};
export interface ReflowInterface {
(
newPositions: OccupiedSpace[],
direction: ReflowDirection,
stopMoveAfterLimit?: boolean,
shouldSkipContainerReflow?: boolean,
forceDirection?: boolean,
immediateExitContainer?: string,
mousePosition?: OccupiedSpace,
): {
movementLimitMap?: MovementLimitMap;
movementMap: ReflowedSpaceMap;
bottomMostRow: number;
isIdealToJumpContainer: boolean;
};
}
export const useReflow = (
OGPositions: OccupiedSpace[],
parentId: string,
gridProps: GridProps,
): ReflowInterface => {
const dispatch = useDispatch();
const isReflowingGlobal = useSelector(getIsReflowing);
const isDragging = useSelector(
(state: AppState) => state.ui.widgetDragResize.isDragging,
);
const throttledDispatch = throttle(dispatch, 50);
const isReflowing = useRef<boolean>(false);
const reflowSpacesSelector = getContainerWidgetSpacesSelectorWhileMoving(
parentId,
);
const widgetSpaces: WidgetSpace[] = useSelector(reflowSpacesSelector) || [];
const prevPositions = useRef<OccupiedSpace[] | undefined>(OGPositions);
const prevCollidingSpaces = useRef<WidgetCollidingSpaceMap>();
const prevMovementMap = useRef<ReflowedSpaceMap>({});
const prevSecondOrderCollisionMap = useRef<SecondOrderCollisionMap>({});
useEffect(() => {
//only have it run when the user has completely stopped dragging and stopped Reflowing
if (!isReflowingGlobal && !isDragging) {
isReflowing.current = false;
prevPositions.current = [...OGPositions];
prevCollidingSpaces.current = { horizontal: {}, vertical: {} };
prevMovementMap.current = {};
prevSecondOrderCollisionMap.current = {};
}
}, [isReflowingGlobal, isDragging]);
// will become a state if we decide that resize should be a "toggle on-demand" feature
const shouldResize = true;
return function reflowSpaces(
newPositions: OccupiedSpace[],
direction: ReflowDirection,
stopMoveAfterLimit = false,
shouldSkipContainerReflow = false,
forceDirection = false,
immediateExitContainer?: string,
mousePosition?: OccupiedSpace,
) {
const prevReflowState: PrevReflowState = {
prevSpacesMap: getSpacesMapFromArray(prevPositions.current),
prevCollidingSpaceMap: prevCollidingSpaces.current as CollidingSpaceMap,
prevMovementMap: prevMovementMap.current,
prevSecondOrderCollisionMap: prevSecondOrderCollisionMap.current,
};
// To track container jumps
let isIdealToJumpContainer = false;
const {
collidingSpaceMap,
movementLimitMap,
movementMap,
secondOrderCollisionMap,
} = reflow(
newPositions,
OGPositions,
widgetSpaces,
direction,
gridProps,
forceDirection,
shouldResize,
prevReflowState,
immediateExitContainer,
);
prevPositions.current = newPositions;
prevCollidingSpaces.current = collidingSpaceMap as WidgetCollidingSpaceMap;
prevSecondOrderCollisionMap.current = secondOrderCollisionMap || {};
let correctedMovementMap = movementMap || {};
if (stopMoveAfterLimit)
correctedMovementMap = getLimitedMovementMap(
movementMap,
prevMovementMap.current,
{ canHorizontalMove: true, canVerticalMove: true },
);
if (shouldSkipContainerReflow && collidingSpaceMap) {
const collidingSpaces = [
...Object.values(collidingSpaceMap.horizontal),
...Object.values(collidingSpaceMap.vertical),
] as WidgetCollidingSpace[];
for (const collidingSpace of collidingSpaces) {
if (
checkIsDropTarget(collidingSpace.type) &&
mousePosition &&
areIntersecting(mousePosition, collidingSpace)
) {
isIdealToJumpContainer = true;
correctedMovementMap = {};
}
}
}
prevMovementMap.current = correctedMovementMap;
if (!isEmpty(correctedMovementMap)) {
isReflowing.current = true;
if (forceDirection) dispatch(reflowMoveAction(correctedMovementMap));
else throttledDispatch(reflowMoveAction(correctedMovementMap));
} else if (isReflowing.current) {
isReflowing.current = false;
throttledDispatch.cancel();
dispatch(stopReflowAction());
}
const bottomMostRow = getBottomRowAfterReflow(
movementMap,
getBottomMostRow(newPositions),
widgetSpaces,
gridProps,
);
return {
movementLimitMap,
movementMap: correctedMovementMap,
bottomMostRow,
isIdealToJumpContainer,
};
};
};