PromucFlow_constructor/app/client/src/utils/autoHeight/generateTree.ts

84 lines
4.1 KiB
TypeScript
Raw Normal View History

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 06:12:32 +00:00
import { areIntersecting } from "utils/boxHelpers";
import { pushToArray } from "utils/helpers";
import { MAX_BOX_SIZE, NodeSpace, TreeNode } from "./constants";
// This function uses the spaces occupied by sibling boxes and provides us with
// a data structure which defines the relative vertical positioning of the boxes
// in the form of "aboves" and "belows" for each box, which are array of box ids
export function generateTree(
spaces: NodeSpace[],
layoutUpdated: boolean,
previousTree: Record<string, TreeNode>,
): Record<string, TreeNode> {
// If widget doesn't exist in this DS, this means that its height changes does not effect any other sibling
spaces.sort((a, b) => a.top - b.top); // Sort based on position, top to bottom, so that we know which is above the other
const _spaces = [...spaces];
const aboveMap: Record<string, string[]> = {};
const belowMap: Record<string, string[]> = {};
const tree: Record<string, TreeNode> = {};
// For each of the sibling boxes
for (let i = 0; i < spaces.length; i++) {
// Get the left most box in the array (Remember: we sorted from top to bottom, so the leftmost will be the top most)
const _curr = _spaces.shift();
if (_curr) {
// Create a reference copy as we need to override the bottom value
const currentSpace = { ..._curr };
// Add a randomly large value to the bottom; this will help us know if any box is below this box
currentSpace.bottom += MAX_BOX_SIZE;
// For each of the remaining sibling widgets
for (let j = 0; j < _spaces.length; j++) {
// Create a reference copy as we need to override the bottom value
const comparisionSpace = { ..._spaces[j] };
// Add a randomly large value to the bottom; this will help us know if any box is below this box
// TODO(abhinav): This addition may not be necessary, as we're only looking to see if these boxes
// are below the currentSpace
comparisionSpace.bottom += MAX_BOX_SIZE;
// Check if comparison space has an overlap with current space
if (areIntersecting(currentSpace, comparisionSpace)) {
// If there is an overlap, comparisonSpace is below the current space
// so, we update the aboveMap and belowMap accordingly
aboveMap[comparisionSpace.id] = pushToArray(
currentSpace.id,
aboveMap[comparisionSpace.id],
) as string[];
belowMap[currentSpace.id] = pushToArray(
comparisionSpace.id,
belowMap[currentSpace.id],
) as string[];
}
}
// Get the originalTop and originalBottom from the previous tree.
// This is so that we can get close to the original (user defined) positions of the boxes
// For example, if box1 increases in size and pushes box2 by 100 rows, while box3 is also above box2
// When the box1 subsequently decrease by 50 rows, we need to maintain spacing between box3 and box2
// Otherwise, if box1 happens to go below the bottomRow of box3, box2 will tend to overlap with box3.
let originalTopRow = previousTree[currentSpace.id]?.originalTopRow;
let originalBottomRow = previousTree[currentSpace.id]?.originalBottomRow;
// We also udpate the original if the layout is being updated
// This happens when the user repositions/resizes boxes
// If the previousTree doesn't have any originals, we can assume that this is the
// first time we're generating the tree, hence we need to keep the current top and bottom
// for subsequent tree generation
if (originalTopRow === undefined || layoutUpdated) {
originalTopRow = currentSpace.top;
}
if (originalBottomRow === undefined || layoutUpdated) {
originalBottomRow = currentSpace.bottom - MAX_BOX_SIZE;
}
tree[currentSpace.id] = {
aboves: aboveMap[currentSpace.id] || [],
belows: belowMap[currentSpace.id] || [],
topRow: currentSpace.top,
bottomRow: currentSpace.bottom - MAX_BOX_SIZE,
originalTopRow,
originalBottomRow,
};
}
}
return tree;
}