diff --git a/packages/editor-ui/src/views/NodeView.vue b/packages/editor-ui/src/views/NodeView.vue index 1c69cbf8a7..7c2269d657 100644 --- a/packages/editor-ui/src/views/NodeView.vue +++ b/packages/editor-ui/src/views/NodeView.vue @@ -894,27 +894,10 @@ export default mixins( return; } - const {minX, minY, maxX, maxY} = CanvasHelpers.getWorkflowCorners(nodes); - - const PADDING = CanvasHelpers.NODE_SIZE * 4; - - const editorWidth = window.innerWidth; - const diffX = maxX - minX + CanvasHelpers.SIDEBAR_WIDTH + PADDING; - const scaleX = editorWidth / diffX; - - const editorHeight = window.innerHeight; - const diffY = maxY - minY + CanvasHelpers.HEADER_HEIGHT + PADDING; - const scaleY = editorHeight / diffY; - - const zoomLevel = Math.min(scaleX, scaleY, 1); - let xOffset = (minX * -1) * zoomLevel + CanvasHelpers.SIDEBAR_WIDTH; // find top right corner - xOffset += (editorWidth - CanvasHelpers.SIDEBAR_WIDTH - (maxX - minX + CanvasHelpers.NODE_SIZE) * zoomLevel) / 2; // add padding to center workflow - - let yOffset = (minY * -1) * zoomLevel + CanvasHelpers.HEADER_HEIGHT; // find top right corner - yOffset += (editorHeight - CanvasHelpers.HEADER_HEIGHT - (maxY - minY + CanvasHelpers.NODE_SIZE * 2) * zoomLevel) / 2; // add padding to center workflow + const {zoomLevel, offset} = CanvasHelpers.getZoomToFit(nodes); this.setZoomLevel(zoomLevel); - this.$store.commit('setNodeViewOffsetPosition', {newOffset: [xOffset, yOffset]}); + this.$store.commit('setNodeViewOffsetPosition', {newOffset: offset}); }, async stopExecution () { diff --git a/packages/editor-ui/src/views/canvasHelpers.ts b/packages/editor-ui/src/views/canvasHelpers.ts index b03a482789..65c747b406 100644 --- a/packages/editor-ui/src/views/canvasHelpers.ts +++ b/packages/editor-ui/src/views/canvasHelpers.ts @@ -452,3 +452,29 @@ export const addConnectionOutputSuccess = (connection: Connection, output: {tota showOrHideMidpointArrow(connection); }; + +export const getZoomToFit = (nodes: INodeUi[]): {offset: XYPosition, zoomLevel: number} => { + const {minX, minY, maxX, maxY} = getWorkflowCorners(nodes); + + const PADDING = NODE_SIZE * 4; + + const editorWidth = window.innerWidth; + const diffX = maxX - minX + SIDEBAR_WIDTH + PADDING; + const scaleX = editorWidth / diffX; + + const editorHeight = window.innerHeight; + const diffY = maxY - minY + HEADER_HEIGHT + PADDING; + const scaleY = editorHeight / diffY; + + const zoomLevel = Math.min(scaleX, scaleY, 1); + let xOffset = (minX * -1) * zoomLevel + SIDEBAR_WIDTH; // find top right corner + xOffset += (editorWidth - SIDEBAR_WIDTH - (maxX - minX + NODE_SIZE) * zoomLevel) / 2; // add padding to center workflow + + let yOffset = (minY * -1) * zoomLevel + HEADER_HEIGHT; // find top right corner + yOffset += (editorHeight - HEADER_HEIGHT - (maxY - minY + NODE_SIZE * 2) * zoomLevel) / 2; // add padding to center workflow + + return { + zoomLevel, + offset: [xOffset, yOffset], + }; +};