Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: yearly calender view week position and resizing and monthly calender view weeks #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const headerHeight = headerDayHeight + headerWeekHeight + headerMonthHeig
export const weekWidth = 84;
export const boxHeight = 56;
export const leftColumnWidth = 196;
export const singleDayWidth = 12;
export const singleDayWidth = weekWidth / 7;
export const zoom2ColumnWidth = 50;
export const zoom2HeaderTopRowHeight = 24;
export const zoom2HeaderMiddleRowHeight = 16;
Expand Down
7 changes: 6 additions & 1 deletion src/context/CalendarProvider/CalendarProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ const CalendarProvider = ({
const loadMore = useCallback(
(direction: Direction) => {
const cols = getVisibleCols(zoom);
let weekOffset: number;
let offset: number;
switch (zoom) {
case 0:
offset = cols * 7;
weekOffset = cols * 7;
offset = Math.round(weekOffset);
if (offset % 2 !== 0) {
offset += offset > weekOffset ? 2 : 5;
}
break;
case 1:
offset = cols;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const parseDay = (data: dayjs.Dayjs): Day => {
hour: data.hour(),
dayName: data.format("ddd"),
dayOfMonth: data.date(),
weekOfYear: data.isoWeek(),
weekOfYear: data.week(),
month: data.month(),
monthName: data.format("MMMM"),
isBusinessDay: getIsBusinessDay(data),
Expand Down
12 changes: 9 additions & 3 deletions src/utils/drawHeader/drawRows/drawWeeksInMiddle.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import dayjs from "dayjs";
import isoWeeksInYear from "dayjs/plugin/isoWeeksInYear";
import isLeapYear from "dayjs/plugin/isLeapYear";
import { Day } from "@/types/global";
import {
dayWidth,
fonts,
headerMonthHeight,
headerWeekHeight,
middleRowTextYPos,
weeksInYear
middleRowTextYPos
} from "@/constants";
import { drawRow } from "@/utils/drawRow";
import { Theme } from "@/styles";

dayjs.extend(isoWeeksInYear);
dayjs.extend(isLeapYear);

export const drawWeeksInMiddle = (
ctx: CanvasRenderingContext2D,
startDate: Day,
Expand All @@ -25,7 +29,9 @@ export const drawWeeksInMiddle = (
let xPos = 0;

for (let i = 0; i < weeksThreshold; i++) {
const day = dayjs(`${startDate.year}-${startDate.month + 1}-${startDate.dayOfMonth}`).day();
const formattedDate = `${startDate.year}-${startDate.month + 1}-${startDate.dayOfMonth}`;
const day = dayjs(formattedDate).day();
const weeksInYear = dayjs(formattedDate).isoWeeksInYear();
let weekIndex = (startWeek + i) % weeksInYear;

if (weekIndex <= 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/getCols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import {
export const getCols = (zoom: number) => {
const wrapperWidth = document.getElementById(outsideWrapperId)?.clientWidth || 0;
const componentWidth = wrapperWidth - leftColumnWidth;
let ceiledValue = 0;

switch (zoom) {
case 1:
return Math.ceil(componentWidth / dayWidth) * screenWidthMultiplier;
case 2:
return Math.ceil(componentWidth / zoom2ColumnWidth) * screenWidthMultiplier;
default:
return Math.ceil(componentWidth / weekWidth) * screenWidthMultiplier;
ceiledValue = Math.ceil((componentWidth / weekWidth) * screenWidthMultiplier);
return ceiledValue % 2 === 0 ? ceiledValue : ceiledValue + 1;
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/utils/getDatesRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const getDatesRange = (date: dayjs.Dayjs, zoom: number): DatesRange => {
const colsOffset = getCols(zoom) / 2;

let startDate;
let daysFromMonday: number;
switch (zoom) {
case 1:
startDate = date.subtract(colsOffset, "days");
Expand All @@ -23,7 +24,8 @@ export const getDatesRange = (date: dayjs.Dayjs, zoom: number): DatesRange => {
startDate = date.subtract(colsOffset, "hours");
break;
default:
startDate = date.subtract(colsOffset, "weeks");
daysFromMonday = (date.day() + 6) % 7;
startDate = date.subtract(colsOffset, "weeks").subtract(daysFromMonday, "days");
break;
}

Expand Down