-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default Layout #15
Default Layout #15
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple nits in here that don't really matter and are just suggestions. I'm happy to accept the PR without them. The main thing I'd like to avoid is creating a new array of nodes with useState
in the useDefaultLayout
thats an exact copy of the ReactFlow internal state. If I'm reading correctly, regardless of whether the default is being used or not, this hook will require that our graph takes 2x the memory required.
I would prefer an approach that avoids this, and if at all possible. For example, I think it would be possible to accomplish everything you're trying to do with this patch. Let me know if I'm overlooking something.
Index: lib/NodeGraphEditor.tsx
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lib/NodeGraphEditor.tsx b/lib/NodeGraphEditor.tsx
--- a/lib/NodeGraphEditor.tsx (revision 7fefe87350b6ddabf907ad316ab12656d327e738)
+++ b/lib/NodeGraphEditor.tsx (date 1706378584380)
@@ -7,6 +7,7 @@
ReactFlowProps,
ReactFlowProvider,
useEdgesState,
+ useNodesInitialized,
useNodesState,
useReactFlow,
useStoreApi,
@@ -23,6 +24,7 @@
useMemo,
JSX,
CSSProperties,
+ useEffect,
} from 'react'
import { defaultEdgeTypes } from './edge-types'
import { IGraphConfig } from './config'
@@ -84,6 +86,10 @@
type FlowProps = ReactFlowProps & {
backgroundStyles?: CSSProperties
+ /**
+ * The default layout engine to use when nodes are provided without positions.
+ */
+ layoutEngine?: LayoutEngine
}
export type NodeGraphHandle = {
layout: () => void
@@ -91,7 +97,13 @@
const Flow = forwardRef<NodeGraphHandle, FlowProps>(
(
- { defaultNodes, defaultEdges, backgroundStyles, ...props }: FlowProps,
+ {
+ defaultNodes,
+ defaultEdges,
+ backgroundStyles,
+ layoutEngine,
+ ...props
+ }: FlowProps,
ref,
) => {
const nodeTypes = useNodeTypes()
@@ -112,7 +124,7 @@
)
// Provide methods to parent components
- const layout = useLayoutEngine(LayoutEngine.Dagre)
+ const layout = useLayoutEngine(layoutEngine ?? LayoutEngine.Dagre)
useImperativeHandle(
ref,
() => ({
@@ -121,6 +133,16 @@
[],
)
+ const initialized = useNodesInitialized()
+ useEffect(() => {
+ const shouldLayout = !!getState().nodes.find(
+ (node) => node.position == undefined,
+ )
+ if (initialized && shouldLayout) {
+ layout()
+ }
+ }, [initialized])
+
return (
<div
style={{
That's why it's a draft! Looks good, I have a couple of other small things, like nodes that have a parent have a new 0,0. |
One reason I called it |
Anyhow, this is looks much better than my hack. :) I am working on my own layouts, I'll keep you updated when I get around to making them clean. |
Also, I am not sure Input Group is the right example to use, it was just available. |
Set and use a default layout if one or more of the nodes does not have a position.
2193841
to
20a8804
Compare
I rebased to main, fyi. |
Reasoning is good, Yeah I was thinking about creating a new story for it, but there's not much to "show" if you know what I mean. It just works if there are un-positioned nodes, so I'm perfectly fine with having it apply to the input groups story for now if you are. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to rename still, I'll wait to merge
Naw, I think it's ok as is. 😃 |
Set and use a default layout if one or more of the nodes does not have a position.