Skip to content

Commit 1c8696b

Browse files
committed
feat: add mouse position utility and integrate it into grid event handling
1 parent 70857ad commit 1c8696b

File tree

8 files changed

+121
-57
lines changed

8 files changed

+121
-57
lines changed

packages/web/src/common/utils/position/position.util.test.ts renamed to packages/web/src/common/utils/position/event.position.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
getAllDayEventWidth,
66
getLeftPosition,
77
widthMinusPadding,
8-
} from "@web/common/utils/position/position.util";
8+
} from "@web/common/utils/position/event.position";
99

1010
describe("getAllDayEventWidth", () => {
1111
it("is never wider than 1 week", () => {

packages/web/src/common/utils/position/position.util.ts renamed to packages/web/src/common/utils/position/event.position.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export const getEventCategory = (
136136
return Category.ThisWeekOnly;
137137
};
138138

139-
export const getPosition = (
139+
export const getEventPosition = (
140140
event: Schema_GridEvent,
141141
startOfView: Dayjs,
142142
endOfView: Dayjs,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { getMousePosition } from "./mouse.position";
2+
3+
describe("getMousePosition", () => {
4+
const layout = {
5+
allDayRowBottom: 100,
6+
allDayRowTop: 50,
7+
gridYStart: 150,
8+
sidebarXStart: 200,
9+
};
10+
it("should return false when x is less than sidebarXStart", () => {
11+
const result = getMousePosition(layout, { x: 100, y: 100 });
12+
13+
expect(result).toEqual({
14+
isOverGrid: false,
15+
isOverMainGrid: false,
16+
isOverAllDayRow: false,
17+
});
18+
});
19+
20+
it("should return isOverAllDayRow as true when y is between allDayRowTop and allDayRowBottom", () => {
21+
const result = getMousePosition(layout, { x: 250, y: 75 });
22+
23+
expect(result).toEqual({
24+
isOverGrid: true,
25+
isOverMainGrid: false,
26+
isOverAllDayRow: true,
27+
});
28+
});
29+
30+
it("should return isOverMainGrid as true when y is greater than gridYStart", () => {
31+
const result = getMousePosition(layout, { x: 250, y: 200 });
32+
33+
expect(result).toEqual({
34+
isOverGrid: true,
35+
isOverMainGrid: true,
36+
isOverAllDayRow: false,
37+
});
38+
});
39+
40+
it("should return isOverGrid as false when y is less than allDayRowTop and gridYStart", () => {
41+
const result = getMousePosition(layout, { x: 250, y: 25 });
42+
43+
expect(result).toEqual({
44+
isOverGrid: false,
45+
isOverMainGrid: false,
46+
isOverAllDayRow: false,
47+
});
48+
});
49+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Coordinates } from "@web/common/types/util.types";
2+
3+
type Layout = {
4+
allDayRowBottom: number;
5+
allDayRowTop: number;
6+
gridYStart: number;
7+
sidebarXStart: number;
8+
};
9+
10+
export const getMousePosition = (layout: Layout, coordinates: Coordinates) => {
11+
const { allDayRowTop, allDayRowBottom, gridYStart, sidebarXStart } = layout;
12+
const { x, y } = coordinates;
13+
14+
const isPastSidebar = x > sidebarXStart;
15+
16+
const isOverAllDayRow =
17+
isPastSidebar && y < allDayRowBottom && y > allDayRowTop;
18+
19+
const isOverMainGrid = isPastSidebar && !isOverAllDayRow && y > gridYStart;
20+
21+
const isOverGrid = isOverAllDayRow || isOverMainGrid;
22+
23+
return {
24+
isOverGrid,
25+
isOverMainGrid,
26+
isOverAllDayRow,
27+
};
28+
};

packages/web/src/views/Calendar/components/Event/Grid/GridEvent/GridEvent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ZIndex } from "@web/common/constants/web.constants";
2323
import { Text } from "@web/components/Text";
2424

2525
import { StyledEvent, StyledEventScaler, StyledEventTitle } from "../../styled";
26-
import { getPosition } from "@web/common/utils/position/position.util";
26+
import { getEventPosition } from "@web/common/utils/position/event.position";
2727

2828
interface Props {
2929
event: Schema_GridEvent;
@@ -61,7 +61,7 @@ const _GridEvent = (
6161
const event = _event;
6262
const isOptimistic = isOptimisticEvent(event);
6363

64-
const position = getPosition(
64+
const position = getEventPosition(
6565
event,
6666
component.startOfView,
6767
component.endOfView,

packages/web/src/views/Calendar/components/Grid/AllDayRow/AllDayEvent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Priorities } from "@core/constants/core.constants";
44
import { Flex } from "@web/components/Flex";
55
import { Schema_GridEvent } from "@web/common/types/web.event.types";
66
import { isOptimisticEvent } from "@web/common/utils/event.util";
7-
import { getPosition } from "@web/common/utils/position/position.util";
7+
import { getEventPosition } from "@web/common/utils/position/event.position";
88
import { AlignItems, FlexDirections } from "@web/components/Flex/styled";
99
import { SpaceCharacter } from "@web/components/SpaceCharacter";
1010
import { Measurements_Grid } from "@web/views/Calendar/hooks/grid/useGridLayout";
@@ -30,7 +30,7 @@ const AllDayEvent = ({
3030
endOfView,
3131
onMouseDown,
3232
}: Props) => {
33-
const position = getPosition(
33+
const position = getEventPosition(
3434
event,
3535
startOfView,
3636
endOfView,

packages/web/src/views/Calendar/hooks/draft/useGridMouseMove.ts

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { MouseEvent, useCallback, useState } from "react";
22

3-
import { useEventListener } from "../mouse/useEventListener";
43
import { State_GridDraft, Util_GridDraft } from "./useDraftUtil";
54
import { Categories_Event } from "@core/types/event.types";
65
import { assembleDefaultEvent } from "@web/common/utils/event.util";
6+
import { getMousePosition } from "@web/common/utils/position/mouse.position";
77
import { getX } from "@web/common/utils/grid.util";
88
import { draftSlice } from "@web/ducks/events/slices/draft.slice";
99
import { useAppDispatch, useAppSelector } from "@web/store/store.hooks";
@@ -14,39 +14,10 @@ import {
1414
SIDEBAR_X_START,
1515
} from "../../layout.constants";
1616
import { DateCalcs } from "../grid/useDateCalcs";
17+
import { useEventListener } from "../mouse/useEventListener";
1718
import { WeekProps } from "../useWeek";
18-
import { useMousePosition } from "./useMousePosition";
1919
import { Measurements_Grid } from "../grid/useGridLayout";
20-
21-
type Layout = {
22-
allDayRowBottom: number;
23-
allDayRowTop: number;
24-
gridYStart: number;
25-
sidebarXStart: number;
26-
};
27-
28-
const getMousePosition = (e: MouseEvent, layout: Layout) => {
29-
console.log("handling mouse move ...");
30-
const x = e.clientX;
31-
const y = e.clientY;
32-
33-
const { allDayRowTop, allDayRowBottom, gridYStart, sidebarXStart } = layout;
34-
35-
const isPastSidebar = x > sidebarXStart;
36-
37-
const isOverAllDayRow =
38-
isPastSidebar && y < allDayRowBottom && y > allDayRowTop;
39-
40-
const isOverMainGrid = isPastSidebar && !isOverAllDayRow && y > gridYStart;
41-
42-
const isOverGrid = isOverAllDayRow || isOverMainGrid;
43-
44-
return {
45-
isOverGrid,
46-
isOverMainGrid,
47-
isOverAllDayRow,
48-
};
49-
};
20+
import { useMousePosition } from "./useMousePosition";
5021

5122
export const useGridMouseMove = (
5223
draftState: State_GridDraft,
@@ -90,16 +61,28 @@ export const useGridMouseMove = (
9061

9162
const onMouseMove = useCallback(
9263
(e: MouseEvent) => {
64+
//TODO handle when dragging over grid
65+
// TODO handle when dropping
9366
if (isMouseDown) {
94-
const position = getMousePosition(e, {
95-
allDayRowBottom: measurements.allDayRow?.bottom,
96-
allDayRowTop: measurements.allDayRow?.top,
97-
gridYStart: GRID_Y_START,
98-
sidebarXStart: SIDEBAR_X_START,
99-
});
100-
console.log(position);
101-
if (position.isOverGrid) {
102-
console.log("over grid...");
67+
const allDayRow = measurements.allDayRow;
68+
if (!allDayRow?.bottom || !allDayRow?.top) {
69+
throw Error("Missing measurements for all-day row");
70+
}
71+
72+
const { isOverMainGrid } = getMousePosition(
73+
{
74+
allDayRowBottom: allDayRow.bottom,
75+
allDayRowTop: allDayRow.top,
76+
gridYStart: GRID_Y_START,
77+
sidebarXStart: SIDEBAR_X_START,
78+
},
79+
{
80+
x: e.clientX,
81+
y: e.clientY,
82+
}
83+
);
84+
if (isOverMainGrid) {
85+
console.log("over main grid...");
10386
}
10487
if (isDrafting) {
10588
console.log("moved while mouse down and draftin!!!");

packages/web/src/views/Calendar/hooks/draft/useMousePosition.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GRID_Y_START,
66
} from "@web/views/Calendar/layout.constants";
77
import { Coordinates } from "@web/common/types/util.types";
8+
import { getMousePosition } from "@web/common/utils/position/mouse.position";
89

910
export const useMousePosition = (
1011
isDragging: boolean,
@@ -22,18 +23,21 @@ export const useMousePosition = (
2223
useEffect(() => {
2324
const handleMouseMove = (e: MouseEvent) => {
2425
console.log("handling mouse move ...");
26+
if (!allDayRow?.bottom || !allDayRow?.top) {
27+
throw Error("Missing measurements for all-day row");
28+
}
29+
2530
const x = e.clientX;
2631
const y = e.clientY;
27-
28-
const isPastSidebar = x > SIDEBAR_X_START;
29-
30-
const isOverAllDayRow =
31-
isPastSidebar && y < allDayRow.bottom && y > allDayRow.top;
32-
33-
const isOverMainGrid =
34-
isPastSidebar && !isOverAllDayRow && y > GRID_Y_START;
35-
36-
const isOverGrid = isOverAllDayRow || isOverMainGrid;
32+
const { isOverGrid, isOverAllDayRow, isOverMainGrid } = getMousePosition(
33+
{
34+
allDayRowBottom: allDayRow?.bottom,
35+
allDayRowTop: allDayRow?.top,
36+
gridYStart: GRID_Y_START,
37+
sidebarXStart: SIDEBAR_X_START,
38+
},
39+
{ x, y }
40+
);
3741

3842
setIsOverGrid(isOverGrid);
3943
setIsOverAllDayRow(isOverAllDayRow);

0 commit comments

Comments
 (0)