Notes/src/services/date_notes.ts

246 lines
7.9 KiB
TypeScript
Raw Normal View History

import noteService from "./notes.js";
import attributeService from "./attributes.js";
import dateUtils from "./date_utils.js";
import sql from "./sql.js";
import protectedSessionService from "./protected_session.js";
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 { t } from "i18next";
2025-01-09 18:07:02 +02:00
const CALENDAR_ROOT_LABEL = "calendarRoot";
const YEAR_LABEL = "yearNote";
const MONTH_LABEL = "monthNote";
const DATE_LABEL = "dateNote";
2025-01-09 18:07:02 +02:00
const WEEKDAY_TRANSLATION_IDS = ["weekdays.sunday", "weekdays.monday", "weekdays.tuesday", "weekdays.wednesday", "weekdays.thursday", "weekdays.friday", "weekdays.saturday", "weekdays.sunday"];
const MONTH_TRANSLATION_IDS = [
2025-01-09 18:07:02 +02:00
"months.january",
"months.february",
"months.march",
"months.april",
"months.may",
"months.june",
"months.july",
"months.august",
"months.september",
"months.october",
"months.november",
"months.december"
];
2024-02-18 13:42:05 +02:00
type StartOfWeek = "monday" | "sunday";
function createNote(parentNote: BNote, noteTitle: string) {
return noteService.createNewNote({
parentNoteId: parentNote.noteId,
2018-01-28 19:30:14 -05:00
title: noteTitle,
2025-01-09 18:07:02 +02:00
content: "",
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
2025-01-09 18:07:02 +02:00
type: "text"
}).note;
}
2024-02-18 13:42:05 +02:00
function getRootCalendarNote(): BNote {
let rootNote;
2022-12-23 15:07:48 +01:00
const workspaceNote = hoistedNoteService.getWorkspaceNote();
2024-02-18 13:42:05 +02:00
if (!workspaceNote || !workspaceNote.isRoot()) {
2025-01-09 18:07:02 +02:00
rootNote = searchService.findFirstNoteWithQuery("#workspaceCalendarRoot", new SearchContext({ ignoreHoistedNote: false }));
}
if (!rootNote) {
rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL);
}
if (!rootNote) {
2020-08-18 21:32:45 +02:00
sql.transactional(() => {
rootNote = noteService.createNewNote({
2025-01-09 18:07:02 +02:00
parentNoteId: "root",
title: "Calendar",
target: "into",
2020-08-18 21:32:45 +02:00
isProtected: false,
2025-01-09 18:07:02 +02:00
type: "text",
content: ""
2020-08-18 21:32:45 +02:00
}).note;
attributeService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL);
2025-01-09 18:07:02 +02:00
attributeService.createLabel(rootNote.noteId, "sorted");
2020-08-18 21:32:45 +02:00
});
}
2024-02-18 13:42:05 +02:00
return rootNote as BNote;
}
2024-02-18 13:42:05 +02:00
function getYearNote(dateStr: string, _rootNote: BNote | null = null): BNote {
const rootNote = _rootNote || getRootCalendarNote();
2025-04-01 00:08:48 +02:00
const yearStr = dateStr.trim().substring(0, 4);
2025-01-09 18:07:02 +02:00
let yearNote = searchService.findFirstNoteWithQuery(`#${YEAR_LABEL}="${yearStr}"`, new SearchContext({ ancestorNoteId: rootNote.noteId }));
2021-07-24 11:28:47 +02:00
if (yearNote) {
return yearNote;
}
2021-07-24 11:28:47 +02:00
sql.transactional(() => {
yearNote = createNote(rootNote, yearStr);
2021-07-24 11:28:47 +02:00
attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr);
2025-01-09 18:07:02 +02:00
attributeService.createLabel(yearNote.noteId, "sorted");
2025-01-09 18:07:02 +02:00
const yearTemplateAttr = rootNote.getOwnedAttribute("relation", "yearTemplate");
2021-07-24 11:28:47 +02:00
if (yearTemplateAttr) {
2025-01-09 18:07:02 +02:00
attributeService.createRelation(yearNote.noteId, "template", yearTemplateAttr.value);
}
2021-07-24 11:28:47 +02:00
});
2024-02-18 13:42:05 +02:00
return yearNote as unknown as BNote;
}
2024-02-18 13:42:05 +02:00
function getMonthNoteTitle(rootNote: BNote, monthNumber: string, dateObj: Date) {
2020-06-20 12:31:38 +02:00
const pattern = rootNote.getOwnedLabelValue("monthPattern") || "{monthNumberPadded} - {month}";
const monthName = t(MONTH_TRANSLATION_IDS[dateObj.getMonth()]);
return pattern
2025-01-09 18:07:02 +02:00
.replace(/{shortMonth3}/g, monthName.slice(0, 3))
.replace(/{shortMonth4}/g, monthName.slice(0, 4))
.replace(/{isoMonth}/g, dateUtils.utcDateStr(dateObj).slice(0, 7))
.replace(/{monthNumberPadded}/g, monthNumber)
.replace(/{month}/g, monthName);
}
2024-02-18 13:42:05 +02:00
function getMonthNote(dateStr: string, _rootNote: BNote | null = null): BNote {
const rootNote = _rootNote || getRootCalendarNote();
2025-04-01 00:08:48 +02:00
const monthStr = dateStr.substring(0, 7);
const monthNumber = dateStr.substring(5, 2);
2025-01-09 18:07:02 +02:00
let monthNote = searchService.findFirstNoteWithQuery(`#${MONTH_LABEL}="${monthStr}"`, new SearchContext({ ancestorNoteId: rootNote.noteId }));
2021-07-24 11:28:47 +02:00
if (monthNote) {
return monthNote;
}
const dateObj = dateUtils.parseLocalDate(dateStr);
2021-07-24 11:28:47 +02:00
const noteTitle = getMonthNoteTitle(rootNote, monthNumber, dateObj);
2021-12-11 14:15:38 +01:00
const yearNote = getYearNote(dateStr, rootNote);
2021-07-24 11:28:47 +02:00
sql.transactional(() => {
monthNote = createNote(yearNote, noteTitle);
2021-07-24 11:28:47 +02:00
attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr);
2025-01-09 18:07:02 +02:00
attributeService.createLabel(monthNote.noteId, "sorted");
2025-01-09 18:07:02 +02:00
const monthTemplateAttr = rootNote.getOwnedAttribute("relation", "monthTemplate");
2021-07-24 11:28:47 +02:00
if (monthTemplateAttr) {
2025-01-09 18:07:02 +02:00
attributeService.createRelation(monthNote.noteId, "template", monthTemplateAttr.value);
}
2021-07-24 11:28:47 +02:00
});
2024-02-18 13:42:05 +02:00
return monthNote as unknown as BNote;
}
2024-02-18 13:42:05 +02:00
function getDayNoteTitle(rootNote: BNote, dayNumber: string, dateObj: Date) {
2020-06-20 12:31:38 +02:00
const pattern = rootNote.getOwnedLabelValue("datePattern") || "{dayInMonthPadded} - {weekDay}";
const weekDay = t(WEEKDAY_TRANSLATION_IDS[dateObj.getDay()]);
return pattern
.replace(/{ordinal}/g, ordinal(parseInt(dayNumber)))
.replace(/{dayInMonthPadded}/g, dayNumber)
.replace(/{isoDate}/g, dateUtils.utcDateStr(dateObj))
.replace(/{weekDay}/g, weekDay)
2025-04-01 00:08:48 +02:00
.replace(/{weekDay3}/g, weekDay.substring(0, 3))
.replace(/{weekDay2}/g, weekDay.substring(0, 2));
}
2023-11-03 13:49:18 +01:00
/** produces 1st, 2nd, 3rd, 4th, 21st, 31st for 1, 2, 3, 4, 21, 31 */
2024-02-18 13:42:05 +02:00
function ordinal(dayNumber: number) {
2023-11-03 13:49:18 +01:00
const suffixes = ["th", "st", "nd", "rd"];
const suffix = suffixes[(dayNumber - 20) % 10] || suffixes[dayNumber] || suffixes[0];
return `${dayNumber}${suffix}`;
}
2024-02-18 13:42:05 +02:00
function getDayNote(dateStr: string, _rootNote: BNote | null = null): BNote {
const rootNote = _rootNote || getRootCalendarNote();
2025-04-01 00:08:48 +02:00
dateStr = dateStr.trim().substring(0, 10);
2021-12-27 20:48:14 +01:00
2025-01-09 18:07:02 +02:00
let dateNote = searchService.findFirstNoteWithQuery(`#${DATE_LABEL}="${dateStr}"`, new SearchContext({ ancestorNoteId: rootNote.noteId }));
2021-07-24 11:28:47 +02:00
if (dateNote) {
return dateNote;
}
2021-07-24 11:28:47 +02:00
const monthNote = getMonthNote(dateStr, rootNote);
2025-04-01 00:08:48 +02:00
const dayNumber = dateStr.substring(8, 2);
2021-07-24 11:28:47 +02:00
const dateObj = dateUtils.parseLocalDate(dateStr);
2022-01-10 17:09:20 +01:00
const noteTitle = getDayNoteTitle(rootNote, dayNumber, dateObj);
2021-07-24 11:28:47 +02:00
sql.transactional(() => {
dateNote = createNote(monthNote, noteTitle);
2025-04-01 00:08:48 +02:00
attributeService.createLabel(dateNote.noteId, DATE_LABEL, dateStr.substring(0, 10));
2021-07-24 11:28:47 +02:00
2025-01-09 18:07:02 +02:00
const dateTemplateAttr = rootNote.getOwnedAttribute("relation", "dateTemplate");
2021-07-24 11:28:47 +02:00
if (dateTemplateAttr) {
2025-01-09 18:07:02 +02:00
attributeService.createRelation(dateNote.noteId, "template", dateTemplateAttr.value);
}
2021-07-24 11:28:47 +02:00
});
2024-02-18 13:42:05 +02:00
return dateNote as unknown as BNote;
}
function getTodayNote(rootNote: BNote | null = null) {
return getDayNote(dateUtils.localNowDate(), rootNote);
2019-11-27 23:07:10 +01:00
}
2024-02-18 13:42:05 +02:00
function getStartOfTheWeek(date: Date, startOfTheWeek: StartOfWeek) {
const day = date.getDay();
let diff;
2025-01-09 18:07:02 +02:00
if (startOfTheWeek === "monday") {
diff = date.getDate() - day + (day === 0 ? -6 : 1); // adjust when day is sunday
2025-01-09 18:07:02 +02:00
} else if (startOfTheWeek === "sunday") {
diff = date.getDate() - day;
2025-01-09 18:07:02 +02:00
} else {
throw new Error(`Unrecognized start of the week ${startOfTheWeek}`);
}
return new Date(date.setDate(diff));
}
2024-02-18 13:42:05 +02:00
interface WeekNoteOpts {
2025-01-09 18:07:02 +02:00
startOfTheWeek?: StartOfWeek;
2024-02-18 13:42:05 +02:00
}
function getWeekNote(dateStr: string, options: WeekNoteOpts = {}, rootNote: BNote | null = null) {
const startOfTheWeek = options.startOfTheWeek || "monday";
const dateObj = getStartOfTheWeek(dateUtils.parseLocalDate(dateStr), startOfTheWeek);
2021-11-04 19:52:00 +01:00
dateStr = dateUtils.utcDateTimeStr(dateObj);
return getDayNote(dateStr, rootNote);
}
export default {
getRootCalendarNote,
getYearNote,
getMonthNote,
getWeekNote,
2022-01-10 17:09:20 +01:00
getDayNote,
2019-11-27 23:07:10 +01:00
getTodayNote
2020-06-20 12:31:38 +02:00
};