feat(props): add defaultPosition prop #358
This commit is contained in:
@@ -73,6 +73,7 @@ const BasicFlow = () => <ReactFlow elements={elements} />;
|
|||||||
- `minZoom`: default: `0.5`
|
- `minZoom`: default: `0.5`
|
||||||
- `maxZoom`: default: `2`
|
- `maxZoom`: default: `2`
|
||||||
- `defaultZoom`: default: `1`
|
- `defaultZoom`: default: `1`
|
||||||
|
- `defaultPosition`: default: `[0, 0]`
|
||||||
- `snapToGrid`: default: `false`
|
- `snapToGrid`: default: `false`
|
||||||
- `snapGrid`: [x, y] array - default: `[16, 16]`
|
- `snapGrid`: [x, y] array - default: `[16, 16]`
|
||||||
- `onlyRenderVisibleNodes`: default: `true`
|
- `onlyRenderVisibleNodes`: default: `true`
|
||||||
@@ -88,7 +89,9 @@ const BasicFlow = () => <ReactFlow elements={elements} />;
|
|||||||
- `onNodeContextMenu(evt: MouseEvent, node: Node)`: node context menu
|
- `onNodeContextMenu(evt: MouseEvent, node: Node)`: node context menu
|
||||||
- `onConnect({ source, target })`: called when user connects two nodes
|
- `onConnect({ source, target })`: called when user connects two nodes
|
||||||
- `onLoad(reactFlowInstance)`: called after flow is initialized
|
- `onLoad(reactFlowInstance)`: called after flow is initialized
|
||||||
- `onMove()`: called when user pans or zooms
|
- `onMove()`: called when user is panning or zooming
|
||||||
|
- `onMoveStart()`: called when user starts panning or zooming
|
||||||
|
- `onMoveEnd()`: called when user ends panning or zooming
|
||||||
- `onSelectionChange(elements: Elements)`: called when user selects one or multiple elements
|
- `onSelectionChange(elements: Elements)`: called when user selects one or multiple elements
|
||||||
|
|
||||||
#### Interaction
|
#### Interaction
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ export interface GraphViewProps {
|
|||||||
minZoom: number;
|
minZoom: number;
|
||||||
maxZoom: number;
|
maxZoom: number;
|
||||||
defaultZoom: number;
|
defaultZoom: number;
|
||||||
|
defaultPosition: [number, number];
|
||||||
arrowHeadColor: string;
|
arrowHeadColor: string;
|
||||||
markerEndId?: string;
|
markerEndId?: string;
|
||||||
zoomOnScroll: boolean;
|
zoomOnScroll: boolean;
|
||||||
@@ -93,6 +94,7 @@ const GraphView = memo(
|
|||||||
minZoom,
|
minZoom,
|
||||||
maxZoom,
|
maxZoom,
|
||||||
defaultZoom,
|
defaultZoom,
|
||||||
|
defaultPosition,
|
||||||
arrowHeadColor,
|
arrowHeadColor,
|
||||||
markerEndId,
|
markerEndId,
|
||||||
zoomOnScroll,
|
zoomOnScroll,
|
||||||
@@ -111,7 +113,7 @@ const GraphView = memo(
|
|||||||
const setNodesDraggable = useStoreActions((actions) => actions.setNodesDraggable);
|
const setNodesDraggable = useStoreActions((actions) => actions.setNodesDraggable);
|
||||||
const setNodesConnectable = useStoreActions((actions) => actions.setNodesConnectable);
|
const setNodesConnectable = useStoreActions((actions) => actions.setNodesConnectable);
|
||||||
const setElementsSelectable = useStoreActions((actions) => actions.setElementsSelectable);
|
const setElementsSelectable = useStoreActions((actions) => actions.setElementsSelectable);
|
||||||
const updateTransform = useStoreActions((actions) => actions.updateTransform);
|
const setInitTransform = useStoreActions((actions) => actions.setInitTransform);
|
||||||
const setMinMaxZoom = useStoreActions((actions) => actions.setMinMaxZoom);
|
const setMinMaxZoom = useStoreActions((actions) => actions.setMinMaxZoom);
|
||||||
const fitView = useStoreActions((actions) => actions.fitView);
|
const fitView = useStoreActions((actions) => actions.fitView);
|
||||||
const zoom = useStoreActions((actions) => actions.zoom);
|
const zoom = useStoreActions((actions) => actions.zoom);
|
||||||
@@ -144,10 +146,6 @@ const GraphView = memo(
|
|||||||
setOnConnect(onConnect);
|
setOnConnect(onConnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defaultZoom !== 1) {
|
|
||||||
updateTransform({ x: 0, y: 0, k: defaultZoom });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rendererNode.current) {
|
if (rendererNode.current) {
|
||||||
resizeObserver = new ResizeObserver((entries) => {
|
resizeObserver = new ResizeObserver((entries) => {
|
||||||
for (let _ of entries) {
|
for (let _ of entries) {
|
||||||
@@ -187,6 +185,18 @@ const GraphView = memo(
|
|||||||
getElements,
|
getElements,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (d3Initialised) {
|
||||||
|
const initialTransform = {
|
||||||
|
x: defaultPosition[0],
|
||||||
|
y: defaultPosition[1],
|
||||||
|
k: defaultZoom,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (initialTransform.x !== 0 || initialTransform.y !== 0 || initialTransform.k !== 1) {
|
||||||
|
setInitTransform(initialTransform);
|
||||||
|
}
|
||||||
|
}
|
||||||
}, [d3Initialised, onLoad]);
|
}, [d3Initialised, onLoad]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ export interface ReactFlowProps extends Omit<HTMLAttributes<HTMLDivElement>, 'on
|
|||||||
minZoom: number;
|
minZoom: number;
|
||||||
maxZoom: number;
|
maxZoom: number;
|
||||||
defaultZoom: number;
|
defaultZoom: number;
|
||||||
|
defaultPosition: [number, number];
|
||||||
arrowHeadColor: string;
|
arrowHeadColor: string;
|
||||||
markerEndId?: string;
|
markerEndId?: string;
|
||||||
zoomOnScroll: boolean;
|
zoomOnScroll: boolean;
|
||||||
@@ -104,6 +105,7 @@ const ReactFlow = ({
|
|||||||
minZoom,
|
minZoom,
|
||||||
maxZoom,
|
maxZoom,
|
||||||
defaultZoom,
|
defaultZoom,
|
||||||
|
defaultPosition,
|
||||||
arrowHeadColor,
|
arrowHeadColor,
|
||||||
markerEndId,
|
markerEndId,
|
||||||
zoomOnScroll,
|
zoomOnScroll,
|
||||||
@@ -147,6 +149,7 @@ const ReactFlow = ({
|
|||||||
minZoom={minZoom}
|
minZoom={minZoom}
|
||||||
maxZoom={maxZoom}
|
maxZoom={maxZoom}
|
||||||
defaultZoom={defaultZoom}
|
defaultZoom={defaultZoom}
|
||||||
|
defaultPosition={defaultPosition}
|
||||||
arrowHeadColor={arrowHeadColor}
|
arrowHeadColor={arrowHeadColor}
|
||||||
markerEndId={markerEndId}
|
markerEndId={markerEndId}
|
||||||
zoomOnScroll={zoomOnScroll}
|
zoomOnScroll={zoomOnScroll}
|
||||||
@@ -186,6 +189,7 @@ ReactFlow.defaultProps = {
|
|||||||
minZoom: 0.5,
|
minZoom: 0.5,
|
||||||
maxZoom: 2,
|
maxZoom: 2,
|
||||||
defaultZoom: 1,
|
defaultZoom: 1,
|
||||||
|
defaultPosition: [0, 0],
|
||||||
arrowHeadColor: '#bbb',
|
arrowHeadColor: '#bbb',
|
||||||
zoomOnScroll: true,
|
zoomOnScroll: true,
|
||||||
zoomOnDoubleClick: true,
|
zoomOnDoubleClick: true,
|
||||||
|
|||||||
@@ -105,6 +105,8 @@ export interface StoreModel {
|
|||||||
|
|
||||||
updateTransform: Action<StoreModel, TransformXYK>;
|
updateTransform: Action<StoreModel, TransformXYK>;
|
||||||
|
|
||||||
|
setInitTransform: Action<StoreModel, TransformXYK>;
|
||||||
|
|
||||||
updateSize: Action<StoreModel, Dimensions>;
|
updateSize: Action<StoreModel, Dimensions>;
|
||||||
|
|
||||||
initD3: Action<StoreModel, Element>;
|
initD3: Action<StoreModel, Element>;
|
||||||
@@ -342,6 +344,18 @@ export const storeModel: StoreModel = {
|
|||||||
state.transform[2] = transform.k;
|
state.transform[2] = transform.k;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
setInitTransform: action((state, transform) => {
|
||||||
|
state.transform[0] = transform.x;
|
||||||
|
state.transform[1] = transform.y;
|
||||||
|
state.transform[2] = transform.k;
|
||||||
|
|
||||||
|
if (state.d3Selection) {
|
||||||
|
const updatedTransform = zoomIdentity.translate(transform.x, transform.y).scale(transform.k);
|
||||||
|
// we need to sync the d3 zoom transform with the updated transform
|
||||||
|
state.d3Selection.property('__zoom', updatedTransform);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
updateSize: action((state, size) => {
|
updateSize: action((state, size) => {
|
||||||
state.width = size.width;
|
state.width = size.width;
|
||||||
state.height = size.height;
|
state.height = size.height;
|
||||||
|
|||||||
Reference in New Issue
Block a user