From e0921357e376898ef2ad7f7955a3a2e1f2dc5de7 Mon Sep 17 00:00:00 2001 From: DarkPhoenix2704 Date: Tue, 20 Feb 2024 07:16:01 +0000 Subject: [PATCH] feat(nc-gui): rewrite month view rendering way --- .../smartsheet/calendar/MonthView.vue | 202 ++++++------------ 1 file changed, 62 insertions(+), 140 deletions(-) diff --git a/packages/nc-gui/components/smartsheet/calendar/MonthView.vue b/packages/nc-gui/components/smartsheet/calendar/MonthView.vue index fc1932de04..143b76163a 100644 --- a/packages/nc-gui/components/smartsheet/calendar/MonthView.vue +++ b/packages/nc-gui/components/smartsheet/calendar/MonthView.vue @@ -20,91 +20,42 @@ const isDayInPagedMonth = (date: Date) => { } const dates = computed(() => { - const startOfMonth = new Date(pageDate.value.getFullYear(), pageDate.value.getMonth(), 1) - const dayOffset = isMondayFirst.value ? 1 : 0 - const dayOfWeek = (startOfMonth.getDay() - dayOffset + 7) % 7 - startOfMonth.setDate(startOfMonth.getDate() - dayOfWeek) - const datesArray = [] - while (datesArray.length < 42) { - datesArray.push(new Date(startOfMonth)) - startOfMonth.setDate(startOfMonth.getDate() + 1) - } - return datesArray -}) - -const getGridPosition = (record: Row) => { - if (!calendarRange.value || !calendarRange[0]) - return { - colStart: 1, - colEnd: 1, - rowStart: 1, - rowEnd: 1, + const startOfMonth = dayjs(pageDate.value).startOf('month') + const endOfMonth = dayjs(pageDate.value).endOf('month') + + const firstDayToDisplay = startOfMonth.startOf('week').add(isMondayFirst.value ? 0 : -1, 'day') + const lastDayToDisplay = endOfMonth.endOf('week').add(isMondayFirst.value ? 0 : -1, 'day') + + const daysToDisplay = lastDayToDisplay.diff(firstDayToDisplay, 'day') + 1 + let numberOfRows = Math.ceil(daysToDisplay / 7) + numberOfRows = Math.max(numberOfRows, 5) + + const weeksArray = [] + let currentDay = firstDayToDisplay + for (let week = 0; week < numberOfRows; week++) { + const weekArray = [] + for (let day = 0; day < 7; day++) { + weekArray.push(currentDay.toDate()) + currentDay = currentDay.add(1, 'day') } - const firstDayOfMonth = new Date(pageDate.value.getFullYear(), pageDate.value.getMonth(), 1).getDay() - - const startDate = new Date(record.row[calendarRange[0].fk_from_col.title]) - const startDayIndex = startDate.getDate() - 1 + firstDayOfMonth - const endDate = new Date(record.row[calendarRange[0].fk_to_col.title]) - const endDayIndex = endDate.getDate() - 1 + firstDayOfMonth - - const startRow = Math.floor(startDayIndex / 7) + 1 - let endRow = Math.floor(endDayIndex / 7) + 1 - - if (endDate.getMonth() !== pageDate.value.getMonth()) { - endRow = Math.ceil((new Date(pageDate.value.getFullYear(), pageDate.value.getMonth() + 1, 0).getDate() + firstDayOfMonth) / 7) + weeksArray.push(weekArray) } - const startCol = (startDayIndex % 7) + 1 - let endCol = (endDayIndex % 7) + 1 - - if (endCol === 1) { - endRow++ - endCol = 8 - } - - return { - colStart: startCol, - colEnd: endCol, - rowStart: startRow, - rowEnd: endRow, - } -} + return weeksArray +}) const selectDate = (date: Date) => { - if (!date) return selectedDate.value = date } -const isSameDate = (date1: Date, date2: Date) => { - if (!date1 || !date2) return false - return ( - date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate() - ) -} - const isDateSelected = (date: Date) => { if (!selectedDate.value) return false - const propDate = new Date(selectedDate.value) - return isSameDate(propDate, date) + return dayjs(date).isSame(selectedDate.value, 'day') } - -const eventsByDate = computed(() => { - if (!formattedData.value) return {} - const events = {} - formattedData.value.forEach((record) => { - const date = record.row[calendarRange.value[0].fk_from_col.title] - const dateObj = dayjs(date).format('MM/DD/YYYY') - if (!events[dateObj]) { - events[dateObj] = [] - } - events[dateObj].push(record) - }) - return events -})