feat(fitView): add padding option

This commit is contained in:
moklick
2019-07-26 00:57:03 +02:00
parent bb17c2fcb6
commit a6d191355a
5 changed files with 20 additions and 13 deletions
+14 -8
View File
@@ -29936,7 +29936,8 @@
case FIT_VIEW: case FIT_VIEW:
{ {
var bounds = getBoundingBox(state.nodes); var bounds = getBoundingBox(state.nodes);
var k = Math.min(state.width, state.height) / Math.max(bounds.width, bounds.height); var maxBoundsSize = Math.max(bounds.width, bounds.height);
var k = Math.min(state.width, state.height) / (maxBoundsSize + maxBoundsSize * action.payload.padding);
var boundsCenterX = bounds.x + bounds.width / 2; var boundsCenterX = bounds.x + bounds.width / 2;
var boundsCenterY = bounds.y + bounds.height / 2; var boundsCenterY = bounds.y + bounds.height / 2;
var transform = [state.width / 2 - boundsCenterX * k, state.height / 2 - boundsCenterY * k]; var transform = [state.width / 2 - boundsCenterX * k, state.height / 2 - boundsCenterY * k];
@@ -30075,9 +30076,14 @@
} }
}; };
}; };
var fitView = function fitView() { var fitView = function fitView(_ref2) {
var _ref2$padding = _ref2.padding,
padding = _ref2$padding === void 0 ? 0 : _ref2$padding;
return { return {
type: FIT_VIEW type: FIT_VIEW,
payload: {
padding: padding
}
}; };
}; };
var zoomIn = function zoomIn() { var zoomIn = function zoomIn() {
@@ -30098,9 +30104,9 @@
} }
}; };
}; };
var setNodesSelection = function setNodesSelection(_ref2) { var setNodesSelection = function setNodesSelection(_ref3) {
var isActive = _ref2.isActive, var isActive = _ref3.isActive,
selection = _ref2.selection; selection = _ref3.selection;
return { return {
type: SET_NODES_SELECTION, type: SET_NODES_SELECTION,
payload: { payload: {
@@ -32776,8 +32782,8 @@
props.onLoad({ props.onLoad({
nodes: state.nodes, nodes: state.nodes,
edges: state.edges, edges: state.edges,
fitView: function fitView$1() { fitView: function fitView$1(opts) {
return dispatch(fitView()); return dispatch(fitView(opts));
}, },
zoomIn: function zoomIn$1() { zoomIn: function zoomIn$1() {
return dispatch(zoomIn()); return dispatch(zoomIn());
+1 -1
View File
@@ -61,7 +61,7 @@ class App extends PureComponent {
window.rg = graphInstance; window.rg = graphInstance;
this.graphInstance = graphInstance; this.graphInstance = graphInstance;
this.graphInstance.fitView(); this.graphInstance.fitView({ padding: 0.1 });
this.setState({ this.setState({
graphLoaded: true graphLoaded: true
}); });
+1 -1
View File
@@ -62,7 +62,7 @@ const GraphView = memo((props) => {
props.onLoad({ props.onLoad({
nodes: state.nodes, nodes: state.nodes,
edges: state.edges, edges: state.edges,
fitView: () => dispatch(fitView()), fitView: opts => dispatch(fitView(opts)),
zoomIn: () => dispatch(zoomIn()), zoomIn: () => dispatch(zoomIn()),
zoomOut: () => dispatch(zoomOut()) zoomOut: () => dispatch(zoomOut())
}); });
+2 -2
View File
@@ -54,8 +54,8 @@ export const initD3 = ({ zoom, selection }) => {
}; };
}; };
export const fitView = () => { export const fitView = ({ padding = 0 }) => {
return { type: FIT_VIEW }; return { type: FIT_VIEW, payload: { padding } };
}; };
export const zoomIn = () => { export const zoomIn = () => {
+2 -1
View File
@@ -69,7 +69,8 @@ export const reducer = (state, action) => {
} }
case FIT_VIEW: { case FIT_VIEW: {
const bounds = getBoundingBox(state.nodes); 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 boundsCenterX = bounds.x + (bounds.width / 2);
const boundsCenterY = bounds.y + (bounds.height / 2); const boundsCenterY = bounds.y + (bounds.height / 2);
const transform = [(state.width / 2) - (boundsCenterX * k), (state.height / 2) - (boundsCenterY * k)]; const transform = [(state.width / 2) - (boundsCenterX * k), (state.height / 2) - (boundsCenterY * k)];