Skip to content

Commit 358ea04

Browse files
committed
Refactoring of NodeContent
1 parent 6184a0f commit 358ea04

1 file changed

Lines changed: 12 additions & 20 deletions

File tree

src/extensions/react-flow/nodes/NodeContent.tsx

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ export function NodeContent<CONTENT_PROPS = any>({
359359

360360
const [width, setWidth] = React.useState<number | undefined>(nodeDimensions?.width ?? undefined);
361361
const [height, setHeight] = React.useState<number | undefined>(nodeDimensions?.height ?? undefined);
362-
363-
const originalSize = {} as NodeDimensions;
362+
// Keeps the inital size of the element
363+
const originalSize = React.useRef<NodeDimensions>({})
364364

365365
let zoom = 1;
366366
if (isResizable)
@@ -389,12 +389,12 @@ export function NodeContent<CONTENT_PROPS = any>({
389389
handleStack[Position.Left] =
390390
flowVersionCheck === "legacy" ? ([] as NodeContentHandleLegacyProps[]) : ([] as NodeContentHandleNextProps[]);
391391

392-
const saveOriginalSize = () => {
393-
if (!originalSize.width && !originalSize.height) {
394-
originalSize.width = nodeContentRef.current.offsetWidth as number;
395-
originalSize.height = nodeContentRef.current.offsetHeight as number;
392+
React.useEffect(() => {
393+
if(nodeContentRef.current && !(originalSize.current.width || originalSize.current.height)) {
394+
originalSize.current.width = nodeContentRef.current.offsetWidth as number;
395+
originalSize.current.height = nodeContentRef.current.offsetHeight as number;
396396
}
397-
};
397+
}, [!!nodeContentRef.current, (!originalSize.current.width || !originalSize.current.height)])
398398

399399
// update node dimensions when resized
400400
React.useEffect(() => {
@@ -406,7 +406,6 @@ export function NodeContent<CONTENT_PROPS = any>({
406406

407407
// resizing check and conditional enhancements
408408
React.useEffect(() => {
409-
saveOriginalSize();
410409
const currentClassNames = nodeContentRef.current.classList;
411410
const resizingActive =
412411
resizeDirections.right === currentClassNames.contains("is-resizable-horizontal") &&
@@ -420,20 +419,14 @@ export function NodeContent<CONTENT_PROPS = any>({
420419
nodeContentRef.current.classList.remove("is-resizable-vertical");
421420
}
422421

423-
const newWidth = validateWidth(originalSize?.width as number);
424-
const newHeight = validateHeight(originalSize?.height as number);
425-
426-
setWidth(newWidth);
427-
setHeight(newHeight);
428-
429422
if (resizeDirections.right) {
430423
nodeContentRef.current.classList.add("is-resizable-horizontal");
431424
}
432425
if (resizeDirections.bottom) {
433426
nodeContentRef.current.classList.add("is-resizable-vertical");
434427
}
435428
}
436-
}); // need to be done everytime a property is changed an the element is re-rendered, otherwise the resizing class is lost
429+
}, [nodeContentRef.current, onNodeResize, minimalShape, resizeDirections?.bottom, resizeDirections?.right]); // need to be done everytime a property is changed and the element is re-rendered, otherwise the resizing class is lost
437430

438431
// remove introduction class
439432
React.useEffect(() => {
@@ -680,12 +673,11 @@ export function NodeContent<CONTENT_PROPS = any>({
680673
scale={zoom}
681674
onResize={(_0, _1, _2, d) => {
682675
if (nodeContentRef.current) {
683-
saveOriginalSize();
684676
const nextWidth = resizeDirections.right
685-
? (width ?? originalSize.width ?? 0) + d.width
677+
? (width ?? originalSize.current.width ?? 0) + d.width
686678
: undefined;
687679
const nextHeight = resizeDirections.bottom
688-
? (height ?? originalSize.height ?? 0) + d.height
680+
? (height ?? originalSize.current.height ?? 0) + d.height
689681
: undefined;
690682
if (nextWidth) {
691683
nodeContentRef.current.style.width = `${nextWidth}px`;
@@ -696,8 +688,8 @@ export function NodeContent<CONTENT_PROPS = any>({
696688
}
697689
}}
698690
onResizeStop={(_0, _1, _2, d) => {
699-
const nextWidth = validateWidth((width ?? originalSize.width ?? 0) + d.width);
700-
const nextHeight = validateHeight((height ?? originalSize.height ?? 0) + d.height);
691+
const nextWidth = validateWidth((width ?? originalSize.current.width ?? 0) + d.width);
692+
const nextHeight = validateHeight((height ?? originalSize.current.height ?? 0) + d.height);
701693
setWidth(nextWidth);
702694
setHeight(nextHeight);
703695
if (onNodeResize) {

0 commit comments

Comments
 (0)