From 375d9d06c905e812eacca049c750aa1752703eb4 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Tue, 12 Jun 2018 20:52:08 -0700 Subject: [PATCH] Extract calculateBoundingBox --- js/components/WindowManager.js | 12 +++--------- js/utils.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/js/components/WindowManager.js b/js/components/WindowManager.js index c45f220f..5eb1db5b 100644 --- a/js/components/WindowManager.js +++ b/js/components/WindowManager.js @@ -14,6 +14,7 @@ import { import { getWindowsInfo } from "../selectors"; import { updateWindowPositions } from "../actionCreators"; import { WINDOW_HEIGHT, WINDOW_WIDTH } from "../constants"; +import { calculateBoundingBox } from "../utils"; const abuts = (a, b) => { // TODO: This is kinda a hack. They should really be touching, not just within snapping distance. @@ -74,15 +75,8 @@ class WindowManager extends React.Component { // A layout has been suplied. We will compute the bounding box and // center the given layout. const info = this.props.windowsInfo; - const bounding = info.reduce( - (b, w) => ({ - left: Math.min(b.left, w.x), - top: Math.min(b.top, w.y), - bottom: Math.max(b.bottom, w.y + w.height), - right: Math.max(b.right, w.x + w.width) - }), - { top: 0, bottom: 0, left: 0, right: 0 } - ); + + const bounding = calculateBoundingBox(info); const boxHeight = bounding.bottom - bounding.top; const boxWidth = bounding.right - bounding.left; diff --git a/js/utils.js b/js/utils.js index 67a4848a..93f2029b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -248,3 +248,14 @@ export const objectFilter = (obj, predicate) => } return newObj; }, {}); + +export const calculateBoundingBox = windows => + windows.reduce( + (b, w) => ({ + left: Math.min(b.left, w.x), + top: Math.min(b.top, w.y), + bottom: Math.max(b.bottom, w.y + w.height), + right: Math.max(b.right, w.x + w.width) + }), + { top: 0, bottom: 0, left: 0, right: 0 } + );