Skip to content

Commit ed5072d

Browse files
committed
fix: prevent zero'd entries
1 parent 8ebbb35 commit ed5072d

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

src/components/Week.vue

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,14 @@ import {
192192
addMinutes,
193193
endOfDay,
194194
isAfter,
195-
isBefore,
196195
startOfDay,
197196
differenceInCalendarDays,
198197
addDays,
199198
addHours,
200199
differenceInDays,
201200
isSameDay,
202-
min,
203-
max
201+
differenceInMinutes,
202+
roundToNearestMinutes,
204203
} from "date-fns";
205204
import { guid } from "../helpers/Utility";
206205
@@ -314,8 +313,14 @@ function onMouseMove(mouseEvent: MouseEvent) {
314313
const pixelsToTime = props.intervalMinutes / props.intervalHeight;
315314
const minuteDelta = (currentInterval - initialInterval) * pixelsToTime;
316315
317-
const newStartTime = addMinutes(initialState.startDate, minuteDelta);
318-
const newEndTime = addMinutes(initialState.endDate, minuteDelta);
316+
const newStartTime = roundToNearestMinutes(
317+
addMinutes(initialState.startDate, minuteDelta),
318+
{ nearestTo: props.intervalMinutes }
319+
);
320+
const newEndTime = roundToNearestMinutes(
321+
addMinutes(initialState.endDate, minuteDelta),
322+
{ nearestTo: props.intervalMinutes }
323+
);
319324
320325
if (handle == "body") {
321326
if (
@@ -336,17 +341,30 @@ function onMouseMove(mouseEvent: MouseEvent) {
336341
handle === "top" ? initialState.endDate : initialState.startDate;
337342
const newTime = handle === "top" ? newStartTime : newEndTime;
338343
339-
const [startDate, endDate] = isAfter(newTime, anchor)
344+
let [startDate, endDate] = isAfter(newTime, anchor)
340345
? [anchor, newTime]
341346
: [newTime, anchor];
342347
343-
const mouseDownColumnDate = getDateFromX(startX);
348+
// Prevent resizing to less than one interval in duration
349+
if (differenceInMinutes(endDate, startDate) < props.intervalMinutes) {
350+
if (mousePosition.value.y < getYFromDate(anchor)) {
351+
startDate = addMinutes(endDate, -props.intervalMinutes);
352+
} else {
353+
endDate = addMinutes(startDate, props.intervalMinutes);
354+
}
355+
}
344356
357+
const mouseDownColumnDate = getDateFromX(startX);
345358
const startOfNextDay = addDays(mouseDownColumnDate, 1);
346-
const max = addHours(startOfNextDay, props.hoursPastMidnight);
359+
const maxEndDate = addHours(startOfNextDay, props.hoursPastMidnight);
360+
const maxStartDate = addMinutes(startOfNextDay, -props.intervalMinutes);
361+
362+
if (isAfter(startDate, maxStartDate) || isAfter(endDate, maxEndDate)) {
363+
return;
364+
}
347365
348-
event.startDate = min([startDate, addMinutes(startOfNextDay, -props.intervalMinutes)]);
349-
event.endDate = min([endDate, max]);
366+
event.startDate = startDate;
367+
event.endDate = endDate;
350368
}
351369
}
352370
@@ -385,6 +403,11 @@ function getDateFromY(startingDate: Date, y: number): Date {
385403
return date;
386404
}
387405
406+
function getYFromDate(date: Date): number {
407+
let minutes = differenceInMinutes(date, startOfDay(date));
408+
return (minutes / props.intervalMinutes) * props.intervalHeight;
409+
}
410+
388411
function getDateFromX(x: number): Date {
389412
let totalWidth = rootDiv.value?.clientWidth ?? 0;
390413
let columnWidth = totalWidth / range.value.length;
@@ -410,15 +433,12 @@ function getTotalTime(date: Date) {
410433
function createEvent() {
411434
const hoveredDay = getDateFromX(mousePosition.value.x);
412435
413-
// When creating an event, we fudge the initial mouse position so that
436+
// When creating an event, we fudge the initial mouse position so that
414437
// the start of the event is always at the beginning of the hovered interval
415438
const effectiveMouseY = floorToNearestInterval(mousePosition.value.y);
416439
417-
let start = getDateFromY(
418-
hoveredDay,
419-
effectiveMouseY
420-
);
421-
440+
let start = getDateFromY(hoveredDay, effectiveMouseY);
441+
422442
if (isAfter(start, endOfDay(hoveredDay))) {
423443
return;
424444
}
@@ -428,7 +448,7 @@ function createEvent() {
428448
description: props.defaultEventProperties?.description,
429449
color: props.defaultEventProperties?.color ?? "#2196f3",
430450
startDate: start,
431-
endDate: start,
451+
endDate: addMinutes(start, props.intervalMinutes),
432452
nOfPreviousConcurrentEvents: 0,
433453
totalConcurrentEvents: 0,
434454
left: 0,
@@ -441,7 +461,7 @@ function createEvent() {
441461
initialEventState: { ...event },
442462
handle: "bottom",
443463
x: mousePosition.value.x,
444-
y: effectiveMouseY,
464+
y: effectiveMouseY + props.intervalHeight,
445465
};
446466
creatingEvent = true;
447467
isDragging = true;

0 commit comments

Comments
 (0)