Skip to content

Commit fb8f057

Browse files
Merge pull request #261 from eccenca/fix/nodeContentDimensionsReset-MT-54
Fix node content dimensions reset (MT-54)
2 parents 61dfc99 + 90bddad commit fb8f057

3 files changed

Lines changed: 64 additions & 24 deletions

File tree

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -390,31 +390,31 @@ export function NodeContent<CONTENT_PROPS = any>({
390390
flowVersionCheck === "legacy" ? ([] as NodeContentHandleLegacyProps[]) : ([] as NodeContentHandleNextProps[]);
391391

392392
const saveOriginalSize = () => {
393+
const currentClassNames = nodeContentRef.current.classList;
394+
if (currentClassNames.contains("was-resized") && !width && !height) {
395+
currentClassNames.remove("was-resized");
396+
}
393397
originalSize.current.width = nodeContentRef.current.offsetWidth as number;
394398
originalSize.current.height = nodeContentRef.current.offsetHeight as number;
395399
}
396400

397401
React.useEffect(() => {
398-
if(nodeContentRef.current && !(originalSize.current.width || originalSize.current.height)) {
402+
if(nodeContentRef.current && (!(originalSize.current.width || originalSize.current.height) || !(width || height))) {
399403
saveOriginalSize();
400404
}
401-
}, [!!nodeContentRef.current, !(originalSize.current.width || originalSize.current.height)])
405+
}, [!!nodeContentRef.current, !(originalSize.current.width || originalSize.current.height), !(width || height)])
402406

403407
// Update width and height when node dimensions parameters has changed
404408
React.useEffect(() => {
405409
const updateWidth = nodeDimensions?.width ? validateWidth(nodeDimensions?.width) : undefined;
406410
const updateHeight = nodeDimensions?.height ? validateHeight(nodeDimensions?.height) : undefined;
407411
setWidth(updateWidth);
408412
setHeight(updateHeight);
409-
if (!nodeDimensions?.width && !nodeDimensions?.height) {
410-
// provoke new measuring if no dimensions are set
411-
saveOriginalSize();
412-
}
413413
}, [nodeDimensions]);
414414

415415
const isResizingActive = React.useCallback((): boolean => {
416416
const currentClassNames = nodeContentRef.current.classList;
417-
return resizeDirections.right === currentClassNames.contains("is-resizable-horizontal") &&
417+
return resizeDirections.right === currentClassNames.contains("is-resizable-horizontal") ||
418418
resizeDirections.bottom === currentClassNames.contains("is-resizable-vertical");
419419
}, [])
420420

@@ -439,17 +439,17 @@ export function NodeContent<CONTENT_PROPS = any>({
439439

440440
if (isResizable && !resizingActive) {
441441
if (currentClassNames.contains("is-resizable-horizontal")) {
442-
nodeContentRef.current.classList.remove("is-resizable-horizontal");
442+
currentClassNames.remove("is-resizable-horizontal");
443443
}
444444
if (currentClassNames.contains("is-resizable-vertical")) {
445-
nodeContentRef.current.classList.remove("is-resizable-vertical");
445+
currentClassNames.remove("is-resizable-vertical");
446446
}
447-
447+
448448
if (resizeDirections.right) {
449-
nodeContentRef.current.classList.add("is-resizable-horizontal");
449+
currentClassNames.add("is-resizable-horizontal");
450450
}
451451
if (resizeDirections.bottom) {
452-
nodeContentRef.current.classList.add("is-resizable-vertical");
452+
currentClassNames.add("is-resizable-vertical");
453453
}
454454
}
455455
}); // need to be done everytime a property is changed and the element is re-rendered, otherwise the resizing class is lost
@@ -524,8 +524,8 @@ export function NodeContent<CONTENT_PROPS = any>({
524524
? {
525525
width,
526526
height,
527-
maxWidth: resizeMaxDimensions?.width ?? undefined,
528-
maxHeight: resizeMaxDimensions?.height ?? undefined,
527+
maxWidth: resizeDirections.right ? resizeMaxDimensions?.width ?? undefined : undefined,
528+
maxHeight: resizeDirections.bottom ? resizeMaxDimensions?.height ?? undefined : undefined,
529529
}
530530
: {};
531531

@@ -705,6 +705,10 @@ export function NodeContent<CONTENT_PROPS = any>({
705705
const nextHeight = resizeDirections.bottom
706706
? (height ?? originalSize.current.height ?? 0) + d.height
707707
: undefined;
708+
if (nextWidth || nextHeight) {
709+
const currentClassNames = nodeContentRef.current.classList;
710+
currentClassNames.add("was-resized");
711+
}
708712
if (nextWidth) {
709713
nodeContentRef.current.style.width = `${nextWidth}px`;
710714
}

src/extensions/react-flow/nodes/_nodes.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
box-shadow: 0 0 0 6 * $reactflow-node-border-width rgba($reactflow-edge-stroke-color-selected, 0.05);
5151
}
5252

53-
&.is-resizable-vertical {
53+
&.was-resized.is-resizable-vertical {
5454
max-height: unset;
5555
}
5656
}

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ export default {
103103
},
104104
intent: {
105105
control: "select",
106-
options: { "Not set": undefined, ...Definitions },
106+
options: [ undefined, ...Object.values(Definitions) ],
107107
},
108108
highlightColor: {
109109
control: "select",
110-
options: {
110+
options: [ "Not set", "Default", "Alternate", "Default + alternate", "Custom (red)", "Default + Custom (red)", "Custom (green) + alternate", "Custom (purple) + custom (yellow)"],
111+
mapping: {
111112
"Not set": undefined,
112113
Default: "default",
113114
Alternate: "alternate",
@@ -118,8 +119,8 @@ export default {
118119
"Custom (purple) + custom (yellow)": ["purple", "yellow"],
119120
},
120121
},
121-
content: { control: "none" },
122-
footerContent: { control: "none" },
122+
content: { control: false },
123+
footerContent: { control: false },
123124
isConnectable: { table: { disable: true } },
124125
targetPosition: { table: { disable: true } },
125126
sourcePosition: { table: { disable: true } },
@@ -128,17 +129,52 @@ export default {
128129
},
129130
} as Meta<typeof NodeContent>;
130131

132+
let forcedUpdateKey = 0; // @see https://github.com/storybookjs/storybook/issues/13375#issuecomment-1291011856
131133
const NodeContentExample = (args: any) => {
132134
const [reactflowInstance, setReactflowInstance] = useState(null);
133135
const [elements, setElements] = useState([] as Elements);
134136

137+
const defaultElement = {
138+
id: "example-1",
139+
type: "default",
140+
data: args,
141+
position: { x: 50, y: 50 },
142+
};
143+
135144
useEffect(() => {
145+
const sizeReset = {}
146+
if (args.resizeMaxDimensions && args.resizeDirections) {
147+
sizeReset["onNodeResize"] = (dimensions) => {
148+
// eslint-disable-next-line no-console
149+
console.log("call onNodeResize method")
150+
if (args.onNodeResize) {
151+
args.onNodeResize(dimensions);
152+
}
153+
if (dimensions?.width || dimensions?.height) {
154+
sizeReset["menuButtons"] = <IconButton name="item-reset" onClick={() => {
155+
// eslint-disable-next-line no-console
156+
console.log("reset size");
157+
setElements([
158+
{
159+
...defaultElement,
160+
data: {...defaultElement.data, ...sizeReset, ...{ nodeDimensions: {} }},
161+
},
162+
] as Elements);
163+
164+
}}/>;
165+
}
166+
setElements([
167+
{
168+
...defaultElement,
169+
data: {...defaultElement.data, ...sizeReset, ...{ nodeDimensions: dimensions }},
170+
},
171+
] as Elements);
172+
}
173+
}
136174
setElements([
137175
{
138-
id: "example-1",
139-
type: "default",
140-
data: args,
141-
position: { x: 50, y: 50 },
176+
...defaultElement,
177+
data: {...defaultElement.data, ...sizeReset},
142178
},
143179
] as Elements);
144180
}, [args]);
@@ -165,7 +201,7 @@ const NodeContentExample = (args: any) => {
165201
);
166202
};
167203

168-
const Template: StoryFn<typeof NodeContent> = (args) => <NodeContentExample {...args} /*some comment*/ />;
204+
const Template: StoryFn<typeof NodeContent> = (args) => <NodeContentExample {...args} /*some comment*/ key={++forcedUpdateKey} />;
169205

170206
export const Default = Template.bind({});
171207
Default.args = {

0 commit comments

Comments
 (0)