(dimensions?: Dimensions, offset?: XYCoord | null)
| 10 | } |
| 11 | |
| 12 | export function determineDropDirection(dimensions?: Dimensions, offset?: XYCoord | null): DropDirection | undefined { |
| 13 | // console.log("determineDropDirection", dimensions, offset); |
| 14 | if (!offset || !dimensions) return undefined; |
| 15 | const { width, height, left, top } = dimensions; |
| 16 | let { x, y } = offset; |
| 17 | x -= left; |
| 18 | y -= top; |
| 19 | |
| 20 | // Lies outside of the box |
| 21 | if (y < 0 || y > height || x < 0 || x > width) return undefined; |
| 22 | |
| 23 | // Determines if a drop point falls within the center fifth of the box, meaning we should return Center. |
| 24 | const centerX1 = (2 * width) / 5; |
| 25 | const centerX2 = (3 * width) / 5; |
| 26 | const centerY1 = (2 * height) / 5; |
| 27 | const centerY2 = (3 * height) / 5; |
| 28 | |
| 29 | if (x > centerX1 && x < centerX2 && y > centerY1 && y < centerY2) return DropDirection.Center; |
| 30 | |
| 31 | const diagonal1 = y * width - x * height; |
| 32 | const diagonal2 = y * width + x * height - height * width; |
| 33 | |
| 34 | // Lies on diagonal |
| 35 | if (diagonal1 == 0 || diagonal2 == 0) return undefined; |
| 36 | |
| 37 | let code = 0; |
| 38 | |
| 39 | if (diagonal2 > 0) { |
| 40 | code += 1; |
| 41 | } |
| 42 | |
| 43 | if (diagonal1 > 0) { |
| 44 | code += 2; |
| 45 | code = 5 - code; |
| 46 | } |
| 47 | |
| 48 | // Determines whether a drop is close to an edge of the box, meaning drop direction should be OuterX, instead of X |
| 49 | const xOuter1 = width / 5; |
| 50 | const xOuter2 = width - width / 5; |
| 51 | const yOuter1 = height / 5; |
| 52 | const yOuter2 = height - height / 5; |
| 53 | |
| 54 | if (y < yOuter1 || y > yOuter2 || x < xOuter1 || x > xOuter2) { |
| 55 | code += 4; |
| 56 | } |
| 57 | |
| 58 | return code; |
| 59 | } |
| 60 | |
| 61 | export function setTransform( |
| 62 | { top, left, width, height }: Dimensions, |
no outgoing calls
no test coverage detected