mirror of
https://github.com/zadam/trilium.git
synced 2025-06-04 08:58:40 +02:00
add possibility to create week note, closes #416
This commit is contained in:
parent
80931a318f
commit
1d5fb0b646
@ -214,6 +214,16 @@ function BackendScriptApi(currentNote, apiParams) {
|
||||
*/
|
||||
this.getDateNote = dateNoteService.getDateNote;
|
||||
|
||||
/**
|
||||
* Returns note for the first date of the week of the given date.
|
||||
*
|
||||
* @method
|
||||
* @param {string} date in YYYY-MM-DD format
|
||||
* @param {object} options - "startOfTheWeek" - either "monday" (default) or "sunday"
|
||||
* @returns {Promise<Note|null>}
|
||||
*/
|
||||
this.getWeekNote = dateNoteService.getWeekNote;
|
||||
|
||||
/**
|
||||
* Returns month note for given date. If such note doesn't exist, it is created.
|
||||
*
|
||||
|
@ -48,6 +48,10 @@ async function getRootCalendarNote() {
|
||||
}
|
||||
|
||||
async function getYearNote(dateStr, rootNote) {
|
||||
if (!rootNote) {
|
||||
rootNote = await getRootCalendarNote();
|
||||
}
|
||||
|
||||
const yearStr = dateStr.substr(0, 4);
|
||||
|
||||
let yearNote = await attributeService.getNoteWithLabel(YEAR_LABEL, yearStr);
|
||||
@ -118,9 +122,37 @@ async function getDateNote(dateStr) {
|
||||
return dateNote;
|
||||
}
|
||||
|
||||
function getStartOfTheWeek(date, startOfTheWeek) {
|
||||
const day = date.getDay();
|
||||
let diff;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
async function getWeekNote(dateStr, options = {}) {
|
||||
const startOfTheWeek = options.startOfTheWeek || "monday";
|
||||
|
||||
const dateObj = getStartOfTheWeek(dateUtils.parseLocalDate(dateStr), startOfTheWeek);
|
||||
|
||||
dateStr = dateUtils.dateStr(dateObj);
|
||||
|
||||
return getDateNote(dateStr);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getRootCalendarNote,
|
||||
getYearNote,
|
||||
getMonthNote,
|
||||
getWeekNote,
|
||||
getDateNote
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user