feat(fitView): add padding option
This commit is contained in:
@@ -62,7 +62,7 @@ const GraphView = memo((props) => {
|
||||
props.onLoad({
|
||||
nodes: state.nodes,
|
||||
edges: state.edges,
|
||||
fitView: () => dispatch(fitView()),
|
||||
fitView: opts => dispatch(fitView(opts)),
|
||||
zoomIn: () => dispatch(zoomIn()),
|
||||
zoomOut: () => dispatch(zoomOut())
|
||||
});
|
||||
|
||||
@@ -54,8 +54,8 @@ export const initD3 = ({ zoom, selection }) => {
|
||||
};
|
||||
};
|
||||
|
||||
export const fitView = () => {
|
||||
return { type: FIT_VIEW };
|
||||
export const fitView = ({ padding = 0 }) => {
|
||||
return { type: FIT_VIEW, payload: { padding } };
|
||||
};
|
||||
|
||||
export const zoomIn = () => {
|
||||
|
||||
+2
-1
@@ -69,7 +69,8 @@ export const reducer = (state, action) => {
|
||||
}
|
||||
case FIT_VIEW: {
|
||||
const bounds = getBoundingBox(state.nodes);
|
||||
const k = Math.min(state.width, state.height) / Math.max(bounds.width, bounds.height);
|
||||
const maxBoundsSize = Math.max(bounds.width, bounds.height);
|
||||
const k = Math.min(state.width, state.height) / (maxBoundsSize + (maxBoundsSize * action.payload.padding));
|
||||
const boundsCenterX = bounds.x + (bounds.width / 2);
|
||||
const boundsCenterY = bounds.y + (bounds.height / 2);
|
||||
const transform = [(state.width / 2) - (boundsCenterX * k), (state.height / 2) - (boundsCenterY * k)];
|
||||
|
||||
Reference in New Issue
Block a user