trilium/src/services/date_notes.js

130 lines
3.9 KiB
JavaScript
Raw Normal View History

"use strict";
const sql = require('./sql');
const noteService = require('./notes');
const labelService = require('./labels');
2018-04-03 08:46:46 +08:00
const dateUtils = require('./date_utils');
2018-04-03 09:56:55 +08:00
const CALENDAR_ROOT_LABEL = 'calendarRoot';
const YEAR_LABEL = 'year_note';
const MONTH_LABEL = 'month_note';
const DATE_LABEL = 'date_note';
const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December'];
async function createNote(parentNoteId, noteTitle, noteText) {
const {note} = await noteService.createNewNote(parentNoteId, {
2018-01-29 08:30:14 +08:00
title: noteTitle,
content: noteText,
target: 'into',
2018-01-29 08:30:14 +08:00
isProtected: false
2018-04-01 23:42:12 +08:00
});
return note.noteId;
}
async function getNoteStartingWith(parentNoteId, startsWith) {
2018-03-25 09:39:15 +08:00
return await sql.getValue(`SELECT noteId FROM notes JOIN branches USING(noteId)
2018-01-29 08:30:14 +08:00
WHERE parentNoteId = ? AND title LIKE '${startsWith}%'
AND notes.isDeleted = 0 AND isProtected = 0
2018-03-25 09:39:15 +08:00
AND branches.isDeleted = 0`, [parentNoteId]);
}
async function getRootCalendarNoteId() {
let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);
if (!rootNoteId) {
const {rootNote} = await noteService.createNewNote('root', {
2018-01-29 08:30:14 +08:00
title: 'Calendar',
target: 'into',
2018-01-29 08:30:14 +08:00
isProtected: false
2018-04-01 23:42:12 +08:00
});
const rootNoteId = rootNote.noteId;
await labelService.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);
}
return rootNoteId;
}
async function getYearNoteId(dateTimeStr, rootNoteId) {
const yearStr = dateTimeStr.substr(0, 4);
let yearNoteId = await labelService.getNoteIdWithLabel(YEAR_LABEL, yearStr);
if (!yearNoteId) {
yearNoteId = await getNoteStartingWith(rootNoteId, yearStr);
if (!yearNoteId) {
yearNoteId = await createNote(rootNoteId, yearStr);
}
await labelService.createLabel(yearNoteId, YEAR_LABEL, yearStr);
}
return yearNoteId;
}
async function getMonthNoteId(dateTimeStr, rootNoteId) {
const monthStr = dateTimeStr.substr(0, 7);
const monthNumber = dateTimeStr.substr(5, 2);
let monthNoteId = await labelService.getNoteIdWithLabel(MONTH_LABEL, monthStr);
if (!monthNoteId) {
const yearNoteId = await getYearNoteId(dateTimeStr, rootNoteId);
monthNoteId = await getNoteStartingWith(yearNoteId, monthNumber);
if (!monthNoteId) {
2018-04-03 08:46:46 +08:00
const dateObj = dateUtils.parseDate(dateTimeStr);
const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()];
monthNoteId = await createNote(yearNoteId, noteTitle);
}
await labelService.createLabel(monthNoteId, MONTH_LABEL, monthStr);
}
return monthNoteId;
}
async function getDateNoteId(dateTimeStr, rootNoteId = null) {
if (!rootNoteId) {
rootNoteId = await getRootCalendarNoteId();
}
const dateStr = dateTimeStr.substr(0, 10);
const dayNumber = dateTimeStr.substr(8, 2);
let dateNoteId = await labelService.getNoteIdWithLabel(DATE_LABEL, dateStr);
if (!dateNoteId) {
const monthNoteId = await getMonthNoteId(dateTimeStr, rootNoteId);
dateNoteId = await getNoteStartingWith(monthNoteId, dayNumber);
if (!dateNoteId) {
2018-04-03 08:46:46 +08:00
const dateObj = dateUtils.parseDate(dateTimeStr);
const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()];
dateNoteId = await createNote(monthNoteId, noteTitle);
}
await labelService.createLabel(dateNoteId, DATE_LABEL, dateStr);
}
return dateNoteId;
}
module.exports = {
getRootCalendarNoteId,
getYearNoteId,
getMonthNoteId,
getDateNoteId
};