Skip to content

Commit 28d0a43

Browse files
authored
Merge pull request #1 from hoon2hooni/feature-select-area-horizontally
Feature select area horizontally
2 parents 81d2094 + ff8e04e commit 28d0a43

File tree

5 files changed

+229
-80
lines changed

5 files changed

+229
-80
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"simple-import-sort/imports": "error",
88
"simple-import-sort/exports": "error",
99
"no-unused-vars": "off",
10-
"@typescript-eslint/no-unused-vars": "warn"
10+
"@typescript-eslint/no-unused-vars": ["error"]
1111
}
1212
}

components/events/Days.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const Days: FC<ComponentProps> = ({
110110
});
111111
const selectedDates = getSelectedDates({
112112
startDate,
113-
currentTableIndex: dayIndex,
113+
startXIndex: dayIndex,
114114
pageIndex: currentPageIndex,
115115
});
116116
const method = {

components/events/Timetable.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import useUrlEventId from "@lib/hooks/useUrlEventId";
1717
import {
1818
generateSelectedArea,
1919
getColumnIndexAtTimetable,
20+
getXOfTable,
2021
} from "@lib/tableHelper";
2122
import updateCurrentAttendee from "@lib/updateCurrentAttendee";
2223
import { FC, useCallback, useRef, useState } from "react";
@@ -80,7 +81,7 @@ const Timetable: FC<ComponentProps> = ({
8081
});
8182
const hasNotStartMove = startClientX === 0 && startClientY === 0;
8283
const columnIndexOfSelectedAreaRef = useRef(0);
83-
const { moveClientY, initializeMouseAndTouchMove } =
84+
const { moveClientX, moveClientY, initializeMouseAndTouchMove } =
8485
useMouseAndTouchMoveLocation({
8586
skipEvent: hasNotStartMove,
8687
});
@@ -92,13 +93,28 @@ const Timetable: FC<ComponentProps> = ({
9293
startClientX
9394
);
9495

95-
currentSelectedAreaRef.current = generateSelectedArea(
96-
hasNotStartMove,
97-
moveClientY,
96+
const availableIndexAtTable = new Array(7)
97+
.fill(0)
98+
.map((_, i) =>
99+
isInRange(endDate, startWeekOfMonday, currentPageIndex, i, startDate)
100+
);
101+
const startDayIndex = availableIndexAtTable.findIndex((v) => v);
102+
const endDayIndex = 6 - availableIndexAtTable.reverse().findIndex((v) => v);
103+
const minClientX = getXOfTable(1, timetableArea.w / 7, startDayIndex) - 1;
104+
const maxClientX = getXOfTable(1, timetableArea.w / 7, endDayIndex);
105+
const client = {
106+
startClientX,
98107
startClientY,
108+
currentClientX: moveClientX,
109+
currentClientY: moveClientY,
110+
};
111+
const limitClient = { minClientX, maxClientX };
112+
currentSelectedAreaRef.current = generateSelectedArea({
113+
hasNotStartMove,
114+
client,
115+
limitClient,
99116
timetableArea,
100-
startClientX
101-
);
117+
});
102118

103119
const resizeTimeTableHandler = () => {
104120
if (containerRef.current) {
@@ -128,7 +144,6 @@ const Timetable: FC<ComponentProps> = ({
128144
currentSelectedArea,
129145
timetableAreaRef.current,
130146
startDate,
131-
columnIndexOfSelectedAreaRef.current,
132147
currentPageIndex
133148
);
134149

@@ -153,14 +168,15 @@ const Timetable: FC<ComponentProps> = ({
153168
currentAttendee,
154169
isEraseMode,
155170
startDate,
156-
currentPageIndex,
157171
initializeMouseAndTouchMove,
158172
initializeMouseAndTouchStart,
159173
id,
174+
currentPageIndex,
160175
]);
161176

162177
useMouseAndTouchEnd(updateAttendeesAndResetSelectedArea);
163178
const dateToAttendees = generateDateToAttendees(attendees);
179+
164180
return (
165181
<Container ref={containerRef}>
166182
{DAY_TIME_ARRAY.map((hours, dayIndex) => {

lib/days.ts

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getTimetableColumnAreaWidth } from "./tableHelper";
12
interface TimeProps {
23
x: number;
34
y: number;
@@ -88,53 +89,88 @@ export function getSelectedDatesWithSelectedArea(
8889
selectedArea: TimeProps,
8990
table: TimeProps,
9091
startDate: Date,
91-
currentTableIndex: number,
9292
pageIndex: number
9393
) {
94-
const { startIdx, endIdx } = getIndexesFromTable(selectedArea, table);
94+
const { startXIndex, endXIndex } = getXIndexesFromTable(selectedArea, table);
95+
const { startYIndex, endYIndex } = getYIndexesFromTable(selectedArea, table);
96+
9597
return getSelectedDates({
96-
startIdx,
97-
endIdx,
98-
startDate,
99-
currentTableIndex,
10098
pageIndex,
99+
startDate,
100+
startXIndex,
101+
endXIndex,
102+
startYIndex,
103+
endYIndex,
101104
});
102105
}
103106

104-
function getIndexesFromTable(selectedArea: TimeProps, table: TimeProps) {
107+
function getXIndexesFromTable(selectedArea: TimeProps, timetable: TimeProps) {
108+
const GAP_SIZE = 1;
109+
110+
const { x: selectedAreaX, w: selectedAreaWidth } = selectedArea;
111+
const { x: timetableX, w: timetableWidth } = timetable;
112+
113+
const columnWidth = getTimetableColumnAreaWidth(timetableWidth);
114+
115+
let startXIndex = 0;
116+
for (let index = 0; index < 7; index++) {
117+
const eachColumnXByIndex =
118+
timetableX + columnWidth * index + GAP_SIZE * index;
119+
//해당 인덱스의 컬럼의 x좌표와 선택영역의 x좌표가 소수점으로인해 다를수 있기에 반올림했음
120+
if (Math.round(selectedAreaX - eachColumnXByIndex) === 0) {
121+
startXIndex = index;
122+
break;
123+
}
124+
}
125+
126+
const endXIndex =
127+
Math.round(selectedAreaWidth / columnWidth) + startXIndex - 1;
128+
129+
return { startXIndex, endXIndex };
130+
}
131+
132+
function getYIndexesFromTable(selectedArea: TimeProps, table: TimeProps) {
105133
const fromTableToSelectedArea = selectedArea.y - table.y;
106134
const GAP = 1;
107135
const HEIGHT =
108136
(table.h - GAP * (END_TIME - START_TIME - 1)) / (END_TIME - START_TIME);
109137

110-
const startIdx = Math.round(fromTableToSelectedArea / (HEIGHT + GAP));
111-
const endIdx =
138+
const startYIndex = Math.round(fromTableToSelectedArea / (HEIGHT + GAP));
139+
const endYIndex =
112140
Math.round((fromTableToSelectedArea + selectedArea.h) / (HEIGHT + GAP)) - 1;
113-
return { startIdx, endIdx };
141+
return { startYIndex, endYIndex };
114142
}
115143

116144
export function getSelectedDates({
117-
endIdx = 15,
118-
startIdx = 0,
119-
startDate,
120-
currentTableIndex,
121145
pageIndex,
146+
startDate,
147+
startXIndex,
148+
endXIndex = startXIndex,
149+
startYIndex = 0,
150+
endYIndex = 15,
122151
startTime = 8,
123152
}: {
124-
endIdx?: number;
125-
startIdx?: number;
126-
startDate: Date;
127-
currentTableIndex: number;
128153
pageIndex: number;
154+
startDate: Date;
155+
startXIndex: number;
156+
endXIndex?: number;
157+
startYIndex?: number;
158+
endYIndex?: number;
129159
startTime?: number;
130160
}) {
131-
if (endIdx - startIdx + 1 < 0) return [];
132-
const selectedDates = new Array(endIdx - startIdx + 1).fill(0).map((_, idx) =>
133-
addDateAndTime(startDate, {
134-
days: currentTableIndex + pageIndex * 7,
135-
hours: startIdx + idx + startTime,
136-
})
137-
);
161+
if (endYIndex - startYIndex + 1 < 0) return [];
162+
let selectedDates: Date[] = [];
163+
for (let startIndex = startXIndex; startIndex <= endXIndex; startIndex++) {
164+
selectedDates = selectedDates.concat(
165+
new Array(endYIndex - startYIndex + 1).fill(0).map((_, idx) =>
166+
addDateAndTime(startDate, {
167+
days: startIndex + pageIndex * 7,
168+
hours: startYIndex + idx + startTime,
169+
})
170+
)
171+
);
172+
}
173+
console.log(selectedDates);
138174
return selectedDates;
139175
}
140176

0 commit comments

Comments
 (0)