From 3fe83e0ab46925d481d8b324561c581708c45c09 Mon Sep 17 00:00:00 2001 From: Jin <22962980+JYC333@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:25:03 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20use=20first=20day=20of=20?= =?UTF-8?q?week=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/date_notes.ts | 43 +++++++++++++------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/src/services/date_notes.ts b/src/services/date_notes.ts index 52c125962..c4577a6fc 100644 --- a/src/services/date_notes.ts +++ b/src/services/date_notes.ts @@ -7,6 +7,7 @@ import searchService from "../services/search/services/search.js"; import SearchContext from "../services/search/search_context.js"; import hoistedNoteService from "./hoisted_note.js"; import type BNote from "../becca/entities/bnote.js"; +import optionService from "./options.js"; import { t } from "i18next"; const CALENDAR_ROOT_LABEL = "calendarRoot"; @@ -32,8 +33,6 @@ const MONTH_TRANSLATION_IDS = [ "months.december" ]; -type StartOfWeek = "monday" | "sunday"; - function createNote(parentNote: BNote, noteTitle: string) { return noteService.createNewNote({ parentNoteId: parentNote.noteId, @@ -207,36 +206,24 @@ function getTodayNote(rootNote: BNote | null = null) { return getDayNote(dateUtils.localNowDate(), rootNote); } -function getStartOfTheWeek(date: Date, startOfTheWeek: StartOfWeek) { - const day = date.getDay(); - let diff; +function getWeekFirstDayNote(dateStr: string, rootNote: BNote | null = null) { + const startOfWeek = optionService.getOption("firstDayOfWeek") === '0' ? 'sunday' : 'monday'; - if (startOfTheWeek === "monday") { - diff = date.getDate() - day + (day === 0 ? -6 : 1); // adjust when day is sunday - } else if (startOfTheWeek === "sunday") { - diff = date.getDate() - day; - } else { - throw new Error(`Unrecognized start of the week ${startOfTheWeek}`); - } - - return new Date(date.setDate(diff)); -} - -interface WeekNoteOpts { - startOfTheWeek?: StartOfWeek; -} - -function getWeekFirstDayNote(dateStr: string, options: WeekNoteOpts = {}, rootNote: BNote | null = null) { - const startOfTheWeek = options.startOfTheWeek || "monday"; - - const dateObj = getStartOfTheWeek(dateUtils.parseLocalDate(dateStr), startOfTheWeek); + const dateObj = getWeekStartDate(dateUtils.parseLocalDate(dateStr), startOfWeek); dateStr = dateUtils.utcDateTimeStr(dateObj); return getDayNote(dateStr, rootNote); } -function getWeekStartDate(date: Date, startOfWeek: StartOfWeek): Date { +function checkWeekNoteEnabled(rootNote: BNote) { + if (!rootNote.hasLabel('enableWeekNote')) { + return false; + } + return true; +} + +function getWeekStartDate(date: Date, startOfWeek: string): Date { const day = date.getDay(); let diff; @@ -260,9 +247,9 @@ function getWeekNoteTitle(rootNote: BNote, weekNumber: number) { .replace(/{weekNumber}/g, weekNumber.toString()); } -function getWeekNote(weekStr: string, options: WeekNoteOpts = {}, _rootNote: BNote | null = null): BNote | null { +function getWeekNote(weekStr: string, _rootNote: BNote | null = null): BNote | null { const rootNote = _rootNote || getRootCalendarNote(); - if (!rootNote.hasLabel('enableWeekNote')) { + if (!checkWeekNoteEnabled(rootNote)) { return null; } @@ -283,7 +270,7 @@ function getWeekNote(weekStr: string, options: WeekNoteOpts = {}, _rootNote: BNo const weekStartDate = new Date(firstDayOfYear); weekStartDate.setDate(firstDayOfYear.getDate() + (weekNumber - 1) * 7); - const startDate = getWeekStartDate(weekStartDate, options.startOfTheWeek || "monday"); + const startDate = getWeekStartDate(weekStartDate, optionService.getOption("firstDayOfWeek") === '0' ? 'sunday' : 'monday'); const monthNote = getMonthNote(dateUtils.utcDateStr(startDate), rootNote); const noteTitle = getWeekNoteTitle(rootNote, weekNumber);