## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
7cbb12af88,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
370 lines
12 KiB
TypeScript
370 lines
12 KiB
TypeScript
import type { OccupiedSpace } from "constants/CanvasEditorConstants";
|
|
import { getMovementMap } from "./reflowHelpers";
|
|
import type {
|
|
BlockSpace,
|
|
CollidingSpaceMap,
|
|
CollisionMap,
|
|
GridProps,
|
|
MovementLimitMap,
|
|
OrientationAccessors,
|
|
PrevReflowState,
|
|
ReflowedSpaceMap,
|
|
SecondOrderCollisionMap,
|
|
SpaceAttributes,
|
|
SpaceMap,
|
|
} from "./reflowTypes";
|
|
import { ReflowDirection } from "./reflowTypes";
|
|
import {
|
|
changeExitContainerDirection,
|
|
filterCommonSpaces,
|
|
buildArrayToCollisionMap,
|
|
getAccessor,
|
|
getCollidingSpaceMap,
|
|
getDelta,
|
|
getMaxSpaceAttributes,
|
|
getModifiedOccupiedSpacesMap,
|
|
getOrientationAccessors,
|
|
getShouldReflow,
|
|
getSpacesMapFromArray,
|
|
getSortedOccupiedSpaces,
|
|
getSortedNewPositions,
|
|
getSortedCollidingSpaces,
|
|
getCalculatedDirection,
|
|
getOrientationAccessor,
|
|
initializeMovementLimitMap,
|
|
verifyMovementLimits,
|
|
} from "./reflowUtils";
|
|
|
|
/**
|
|
* Reflow method that returns the displacement metrics of all other colliding spaces
|
|
* This is the entry point for the reflow algorithm, It is all pure javascript methods from this point onwards
|
|
*
|
|
* @param newSpacePositions new/current positions array of the dragging/resizing space/block
|
|
* @param OGSpacePositions original positions array of the space before movement
|
|
* @param occupiedSpaces array of all the occupied spaces on the canvas
|
|
* @param direction direction of movement of the moving space
|
|
* @param gridProps properties of the canvas's grid
|
|
* @param forceDirection boolean to force the direction on certain scenarios
|
|
* @param shouldResize boolean to indicate if colliding spaces should resize
|
|
* @param prevReflowState this contains a map of reference to the key values of previous reflow method call to back trace widget movements
|
|
* @param exitContainerId sting, Id of recent exit container
|
|
* @param mousePosition mouse Position on canvas grid
|
|
* @param shouldReflowDropTarget boolean which indicates if we should reflow drop targets
|
|
* @param onTimeout indicates if the reflow is called on timeout
|
|
* @returns movement information of the dragging/resizing space and other colliding spaces
|
|
*/
|
|
export function reflow(
|
|
newSpacePositions: BlockSpace[],
|
|
OGSpacePositions: OccupiedSpace[],
|
|
occupiedSpaces: BlockSpace[],
|
|
direction: ReflowDirection,
|
|
gridProps: GridProps,
|
|
forceDirection = false,
|
|
shouldResize = true,
|
|
prevReflowState: PrevReflowState = {} as PrevReflowState,
|
|
exitContainerId?: string,
|
|
mousePosition?: OccupiedSpace,
|
|
shouldReflowDropTarget = true,
|
|
onTimeout = false,
|
|
) {
|
|
const newSpacePositionsMap = getSpacesMapFromArray(newSpacePositions);
|
|
const OGSpacePositionsMap = getSpacesMapFromArray(OGSpacePositions);
|
|
const occupiedSpacesMap = getSpacesMapFromArray(occupiedSpaces);
|
|
|
|
const [primaryDirection, secondaryDirection] = getCalculatedDirection(
|
|
newSpacePositionsMap,
|
|
prevReflowState.prevSpacesMap,
|
|
direction,
|
|
);
|
|
|
|
//initializing variables
|
|
let movementLimitMap: MovementLimitMap =
|
|
initializeMovementLimitMap(newSpacePositions);
|
|
const globalCollidingSpaces: CollidingSpaceMap = {
|
|
horizontal: {},
|
|
vertical: {},
|
|
};
|
|
|
|
// Reflow is split into two orientation, current direction's orientation is done first
|
|
//and based on that reflowed values, opposite orientation is taken up next.
|
|
//for example, if primary direction is LEFT or RIGHT, then the horizontal orientation of reflow is calculated first
|
|
let currentDirection = forceDirection ? direction : primaryDirection;
|
|
|
|
if (!OGSpacePositionsMap || currentDirection === ReflowDirection.UNSET) {
|
|
return {
|
|
movementMap: prevReflowState.prevMovementMap,
|
|
movementLimit: {
|
|
canHorizontalMove: true,
|
|
canVerticalMove: true,
|
|
},
|
|
};
|
|
}
|
|
|
|
const currentAccessor = getAccessor(currentDirection);
|
|
const { isHorizontal } = currentAccessor;
|
|
|
|
// This Mutates occupiedSpacesMap to filter out the common spaces with newSpacePositionsMap
|
|
filterCommonSpaces(newSpacePositionsMap, occupiedSpacesMap);
|
|
|
|
//The primary and secondary accessors for maximum and minium dimensions of a space
|
|
const maxSpaceAttributes = getMaxSpaceAttributes(currentAccessor);
|
|
|
|
//The primary and secondary Orientations
|
|
const orientation: OrientationAccessors =
|
|
getOrientationAccessors(isHorizontal);
|
|
|
|
const delta = getDelta(newSpacePositionsMap, OGSpacePositionsMap, direction);
|
|
|
|
//Reflow in the primary orientation
|
|
const {
|
|
collidingSpaces: primaryCollidingSpaces,
|
|
currSpacePositionMap: primarySpacePositionMap,
|
|
isColliding: primaryIsColliding,
|
|
movementMap: primaryMovementMap,
|
|
movementVariablesMap: primaryMovementVariablesMap,
|
|
secondOrderCollisionMap: primarySecondOrderCollisionMap,
|
|
shouldRegisterContainerTimeout: primaryShouldRegisterContainerTimeout,
|
|
} = getOrientationalMovementInfo(
|
|
newSpacePositionsMap,
|
|
occupiedSpacesMap,
|
|
currentDirection,
|
|
isHorizontal,
|
|
gridProps,
|
|
delta,
|
|
shouldResize,
|
|
forceDirection,
|
|
exitContainerId,
|
|
shouldReflowDropTarget,
|
|
onTimeout,
|
|
mousePosition,
|
|
maxSpaceAttributes.primary,
|
|
prevReflowState,
|
|
);
|
|
|
|
globalCollidingSpaces[orientation.primary] = buildArrayToCollisionMap(
|
|
primaryCollidingSpaces,
|
|
);
|
|
getShouldReflow(movementLimitMap, primaryMovementVariablesMap, delta);
|
|
|
|
//Reflow in the opposite/secondary orientation
|
|
if (!forceDirection && secondaryDirection)
|
|
currentDirection = secondaryDirection;
|
|
|
|
const {
|
|
collidingSpaces: secondaryCollidingSpaces,
|
|
currSpacePositionMap: secondarySpacePositionMap,
|
|
isColliding: secondaryIsColliding,
|
|
movementMap,
|
|
movementVariablesMap: secondaryMovementVariablesMap,
|
|
secondOrderCollisionMap,
|
|
shouldRegisterContainerTimeout: secondaryShouldRegisterContainerTimeout,
|
|
} = getOrientationalMovementInfo(
|
|
primarySpacePositionMap,
|
|
occupiedSpacesMap,
|
|
currentDirection,
|
|
!isHorizontal,
|
|
gridProps,
|
|
delta,
|
|
shouldResize,
|
|
forceDirection,
|
|
exitContainerId,
|
|
shouldReflowDropTarget,
|
|
onTimeout,
|
|
mousePosition,
|
|
maxSpaceAttributes.secondary,
|
|
prevReflowState,
|
|
primaryMovementMap || {},
|
|
globalCollidingSpaces[orientation.primary],
|
|
primarySecondOrderCollisionMap,
|
|
);
|
|
|
|
if (secondaryIsColliding) {
|
|
globalCollidingSpaces[orientation.secondary] = buildArrayToCollisionMap(
|
|
secondaryCollidingSpaces,
|
|
);
|
|
getShouldReflow(movementLimitMap, secondaryMovementVariablesMap, delta);
|
|
}
|
|
|
|
// If we are not reflowing drop targets, verify the limits of dragging widget
|
|
if (!shouldReflowDropTarget && newSpacePositions.length === 1) {
|
|
movementLimitMap = verifyMovementLimits(
|
|
movementLimitMap,
|
|
secondarySpacePositionMap,
|
|
occupiedSpacesMap,
|
|
);
|
|
}
|
|
|
|
if (!primaryIsColliding && !secondaryIsColliding) {
|
|
return {
|
|
movementLimitMap,
|
|
spacePositionMap: secondarySpacePositionMap,
|
|
shouldRegisterContainerTimeout:
|
|
primaryShouldRegisterContainerTimeout ||
|
|
secondaryShouldRegisterContainerTimeout,
|
|
};
|
|
}
|
|
|
|
return {
|
|
movementLimitMap,
|
|
movementMap: movementMap || primaryMovementMap,
|
|
collidingSpaceMap: globalCollidingSpaces,
|
|
secondOrderCollisionMap:
|
|
secondOrderCollisionMap || primarySecondOrderCollisionMap,
|
|
shouldRegisterContainerTimeout:
|
|
primaryShouldRegisterContainerTimeout ||
|
|
secondaryShouldRegisterContainerTimeout,
|
|
spacePositionMap: secondarySpacePositionMap,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Reflow method that returns the movement variables in a particular orientation, like "horizontal" or "vertical"
|
|
* movement variables involve movementMap of the reflowed spaces, movement Limits of moving/dragging/resizing spaces/blocks
|
|
*
|
|
* @param newSpacePositionsMap new/current positions map of the dragging/resizing space/block
|
|
* @param occupiedSpacesMap all the occupied spaces map on the canvas
|
|
* @param direction direction of movement of the moving space
|
|
* @param isHorizontal boolean to indicate if the orientation is horizontal
|
|
* @param gridProps properties of the canvas's grid
|
|
* @param delta X and Y distance from original positions
|
|
* @param shouldResize boolean to indicate if colliding spaces should resize
|
|
* @param forceDirection boolean to force the direction on certain scenarios
|
|
* @param exitContainerId string, Id of recent exit container
|
|
* @param shouldReflowDropTarget boolean which indicates if we should reflow drop targets
|
|
* @param onTimeout indicates if the reflow is called on timeout
|
|
* @param mousePosition mouse Position on canvas grid
|
|
* @param maxSpaceAttributes object containing accessors for maximum and minimum dimensions in a particular direction
|
|
* @param prevReflowState this contains a map of reference to the key values of previous reflow method call to back trace widget movements
|
|
* @param primaryMovementMap movement map/information from previous run of the algorithm
|
|
* @param primaryCollisionMap direct collision spaces map on the previous run of the algorithm
|
|
* @returns movement information of the dragging/resizing space and other colliding spaces
|
|
*/
|
|
function getOrientationalMovementInfo(
|
|
newSpacePositionsMap: SpaceMap,
|
|
occupiedSpacesMap: SpaceMap,
|
|
direction: ReflowDirection,
|
|
isHorizontal: boolean,
|
|
gridProps: GridProps,
|
|
delta = { X: 0, Y: 0 },
|
|
shouldResize: boolean,
|
|
forceDirection: boolean,
|
|
exitContainerId: string | undefined,
|
|
shouldReflowDropTarget = true,
|
|
onTimeout = false,
|
|
mousePosition: OccupiedSpace | undefined,
|
|
maxSpaceAttributes: { max: SpaceAttributes; min: SpaceAttributes },
|
|
prevReflowState: PrevReflowState,
|
|
primaryMovementMap?: ReflowedSpaceMap,
|
|
primaryCollisionMap?: CollisionMap,
|
|
primarySecondOrderCollisionMap?: SecondOrderCollisionMap,
|
|
) {
|
|
const { prevCollidingSpaceMap, prevMovementMap, prevSpacesMap } =
|
|
prevReflowState;
|
|
const accessors = getAccessor(direction);
|
|
const orientationAccessor = getOrientationAccessor(isHorizontal);
|
|
|
|
//modifying the occupied space's dimension based on the orientation and previous movement maps
|
|
//for example, if current orientation is horizontal, then top and bottom of the spaces is modified to be equal to previous reflowed Y values
|
|
const orientationOccupiedSpacesMap = getModifiedOccupiedSpacesMap(
|
|
occupiedSpacesMap,
|
|
primaryMovementMap || prevMovementMap,
|
|
isHorizontal,
|
|
gridProps,
|
|
maxSpaceAttributes.max,
|
|
maxSpaceAttributes.min,
|
|
);
|
|
|
|
const sortedOccupiedSpaces = getSortedOccupiedSpaces(
|
|
orientationOccupiedSpacesMap,
|
|
accessors,
|
|
);
|
|
|
|
const newSpacePositions = getSortedNewPositions(
|
|
newSpacePositionsMap,
|
|
accessors,
|
|
);
|
|
|
|
const prevCollisionMap =
|
|
(prevCollidingSpaceMap && prevCollidingSpaceMap[orientationAccessor]) || {};
|
|
|
|
//gets a map of all colliding spaces of the current dragging spaces
|
|
const {
|
|
collidingSpaceMap,
|
|
currSpacePositions,
|
|
isColliding,
|
|
shouldRegisterContainerTimeout,
|
|
} = getCollidingSpaceMap(
|
|
newSpacePositions,
|
|
sortedOccupiedSpaces,
|
|
direction,
|
|
prevCollidingSpaceMap,
|
|
isHorizontal,
|
|
prevSpacesMap,
|
|
forceDirection,
|
|
primaryCollisionMap,
|
|
shouldReflowDropTarget,
|
|
onTimeout,
|
|
mousePosition,
|
|
);
|
|
|
|
const currSpacePositionMap = getSpacesMapFromArray(currSpacePositions);
|
|
const collidingSpaces = getSortedCollidingSpaces(
|
|
collidingSpaceMap,
|
|
isHorizontal,
|
|
prevCollisionMap,
|
|
);
|
|
|
|
if (!collidingSpaces.length)
|
|
return { currSpacePositionMap, shouldRegisterContainerTimeout };
|
|
|
|
if (
|
|
!primaryMovementMap &&
|
|
shouldReflowDropTarget &&
|
|
Object.keys(currSpacePositionMap).length === 1
|
|
) {
|
|
changeExitContainerDirection(
|
|
collidingSpaceMap,
|
|
exitContainerId,
|
|
mousePosition,
|
|
currSpacePositionMap,
|
|
);
|
|
}
|
|
|
|
//if it is the first orientation, we use the original positions of the occupiedSpaces
|
|
const currentOccupiedSpaces = !!primaryCollisionMap
|
|
? sortedOccupiedSpaces
|
|
: getSortedOccupiedSpaces(occupiedSpacesMap, accessors);
|
|
const currentOccupiedSpacesMap = !!primaryCollisionMap
|
|
? orientationOccupiedSpacesMap
|
|
: occupiedSpacesMap;
|
|
|
|
const { movementMap, movementVariablesMap, secondOrderCollisionMap } =
|
|
getMovementMap(
|
|
currSpacePositions,
|
|
currSpacePositionMap,
|
|
currentOccupiedSpaces,
|
|
currentOccupiedSpacesMap,
|
|
occupiedSpacesMap,
|
|
collidingSpaces,
|
|
collidingSpaceMap,
|
|
gridProps,
|
|
delta,
|
|
shouldResize,
|
|
direction,
|
|
isHorizontal,
|
|
prevReflowState,
|
|
primaryMovementMap,
|
|
primarySecondOrderCollisionMap,
|
|
);
|
|
|
|
return {
|
|
movementMap,
|
|
movementVariablesMap,
|
|
secondOrderCollisionMap,
|
|
isColliding,
|
|
collidingSpaces,
|
|
currSpacePositionMap,
|
|
shouldRegisterContainerTimeout,
|
|
};
|
|
}
|