refactor(react): handle parent expand in store instead of applyChanges

This commit is contained in:
moklick
2024-03-28 17:08:05 +01:00
parent 844d574c4f
commit 67d979985d
10 changed files with 203 additions and 100 deletions
+2 -54
View File
@@ -10,51 +10,6 @@ import type {
InternalNode,
} from '../types';
export function handleParentExpand(updatedElements: any[], updateItem: any) {
for (const [index, item] of updatedElements.entries()) {
if (item.id === updateItem.parentNode) {
const parent = { ...item };
parent.computed ??= {};
const extendWidth = updateItem.position.x + updateItem.computed.width - parent.computed.width;
const extendHeight = updateItem.position.y + updateItem.computed.height - parent.computed.height;
if (extendWidth > 0 || extendHeight > 0 || updateItem.position.x < 0 || updateItem.position.y < 0) {
parent.width = parent.width ?? parent.computed.width;
parent.height = parent.height ?? parent.computed.height;
if (extendWidth > 0) {
parent.width += extendWidth;
}
if (extendHeight > 0) {
parent.height += extendHeight;
}
if (updateItem.position.x < 0) {
const xDiff = Math.abs(updateItem.position.x);
parent.position.x = parent.position.x - xDiff;
parent.width += xDiff;
updateItem.position.x = 0;
}
if (updateItem.position.y < 0) {
const yDiff = Math.abs(updateItem.position.y);
parent.position.y = parent.position.y - yDiff;
parent.height += yDiff;
updateItem.position.y = 0;
}
parent.computed.width = parent.width;
parent.computed.height = parent.height;
updatedElements[index] = parent;
}
break;
}
}
}
// This function applies changes to nodes or edges that are triggered by React Flow internally.
// When you drag a node for example, React Flow will send a position change update.
// This function then applies the changes and returns the updated elements.
@@ -111,7 +66,7 @@ function applyChanges(changes: any[], elements: any[]): any[] {
const updatedElement = { ...element };
for (const change of changes) {
applyChange(change, updatedElement, updatedElements);
applyChange(change, updatedElement);
}
updatedElements.push(updatedElement);
@@ -121,7 +76,7 @@ function applyChanges(changes: any[], elements: any[]): any[] {
}
// Applies a single change to an element. This is a *mutable* update.
function applyChange(change: any, element: any, elements: any[] = []): any {
function applyChange(change: any, element: any): any {
switch (change.type) {
case 'select': {
element.selected = change.selected;
@@ -137,9 +92,6 @@ function applyChange(change: any, element: any, elements: any[] = []): any {
element.dragging = change.dragging;
}
if (element.expandParent) {
handleParentExpand(elements, element);
}
break;
}
@@ -159,10 +111,6 @@ function applyChange(change: any, element: any, elements: any[] = []): any {
element.resizing = change.resizing;
}
if (element.expandParent) {
handleParentExpand(elements, element);
}
break;
}
}