fix(xod-client): make computation of bounding boxes safer

Fixes #1312
This commit is contained in:
Evgeny Kochetkov
2018-06-29 12:45:12 +03:00
parent e29614d1a3
commit 1239b0192c
2 changed files with 24 additions and 15 deletions

View File

@@ -92,6 +92,7 @@ export const getNewSelection = R.compose(
// :: ClipboardEntities -> Position
export const getBBoxTopLeftPosition = R.compose(
foldMaybe({ x: 0, y: 0 }, R.identity),
getTopLeftPosition,
R.converge(R.concat, [
R.compose(R.map(XP.getNodePosition), R.prop('nodes')),
@@ -124,6 +125,7 @@ const getCommentBottomRightPosition = R.converge(addPoints, [
export const getBBoxBottomRightPosition = R.curry(
(currentPatch, project, entities) =>
R.compose(
foldMaybe({ x: 0, y: 0 }, R.identity),
getBottomRightPosition,
R.converge(R.concat, [
R.compose(

View File

@@ -1,4 +1,6 @@
import * as R from 'ramda';
import { Maybe } from 'ramda-fantasy';
import { foldMaybe } from 'xod-func-tools';
const BASE_SIZE_UNIT = 17;
@@ -160,21 +162,28 @@ export const snapNodeSizeToSlots = R.compose(
sizeToPoint
);
// :: ([Number] -> Number) -> ([Number] -> Number) -> [Position] -> Position
// :: ([Number] -> Number) -> ([Number] -> Number) -> [Position] -> Maybe Position
export const findPosition = R.uncurryN(3)((xFn, yFn) =>
R.converge((x, y) => ({ x, y }), [
R.compose(xFn, R.map(R.prop('x'))),
R.compose(yFn, R.map(R.prop('y'))),
])
R.ifElse(
R.isEmpty,
R.always(Maybe.Nothing()),
R.compose(
Maybe.Just,
R.converge((x, y) => ({ x, y }), [
R.compose(xFn, R.map(R.prop('x'))),
R.compose(yFn, R.map(R.prop('y'))),
])
)
)
);
// :: [Position] -> Position
// :: [Position] -> Maybe Position
export const getTopLeftPosition = findPosition(
R.apply(Math.min),
R.apply(Math.min)
);
// :: [Position] -> Position
// :: [Position] -> Maybe Position
export const getBottomRightPosition = findPosition(
R.apply(Math.max),
R.apply(Math.max)
@@ -182,12 +191,10 @@ export const getBottomRightPosition = findPosition(
// Given a list of positions of all entities, returns optimal patch panning offset
// :: [Position] -> Position
export const getOptimalPanningOffset = R.ifElse(
R.isEmpty,
R.always(DEFAULT_PANNING_OFFSET),
R.compose(
addPoints(DEFAULT_PANNING_OFFSET),
R.map(R.negate),
getTopLeftPosition
)
export const getOptimalPanningOffset = R.compose(
foldMaybe(
DEFAULT_PANNING_OFFSET,
R.pipe(R.map(R.negate), addPoints(DEFAULT_PANNING_OFFSET))
),
getTopLeftPosition
);