2021-09-16 22:09:39 +02:00
|
|
|
const attributeService = require("./attributes");
|
|
|
|
const dateNoteService = require("./date_notes");
|
|
|
|
const becca = require("../becca/becca");
|
|
|
|
const noteService = require("./notes");
|
|
|
|
const cls = require("./cls");
|
|
|
|
const dateUtils = require("./date_utils");
|
2022-12-09 16:13:22 +01:00
|
|
|
const log = require("./log");
|
2022-12-07 12:46:41 +01:00
|
|
|
const hiddenSubtreeService = require("./hidden_subtree");
|
2022-12-15 16:38:05 +01:00
|
|
|
const searchService = require("./search/services/search.js");
|
|
|
|
const SearchContext = require("./search/search_context.js");
|
2022-11-28 23:39:23 +01:00
|
|
|
|
2021-09-16 22:09:39 +02:00
|
|
|
function getInboxNote(date) {
|
|
|
|
const hoistedNote = getHoistedNote();
|
|
|
|
|
|
|
|
let inbox;
|
|
|
|
|
|
|
|
if (!hoistedNote.isRoot()) {
|
|
|
|
inbox = hoistedNote.searchNoteInSubtree('#hoistedInbox');
|
|
|
|
|
|
|
|
if (!inbox) {
|
|
|
|
inbox = hoistedNote.searchNoteInSubtree('#inbox');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inbox) {
|
|
|
|
inbox = hoistedNote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
inbox = attributeService.getNoteWithLabel('inbox')
|
2022-01-10 17:09:20 +01:00
|
|
|
|| dateNoteService.getDayNote(date);
|
2021-09-16 22:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return inbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSqlConsole() {
|
|
|
|
const {note} = noteService.createNewNote({
|
2022-12-15 16:38:05 +01:00
|
|
|
parentNoteId: getMonthlyParentNoteId('sqlConsole'),
|
2021-09-16 22:09:39 +02:00
|
|
|
title: 'SQL Console',
|
|
|
|
content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n",
|
|
|
|
type: 'code',
|
|
|
|
mime: 'text/x-sqlite;schema=trilium'
|
|
|
|
});
|
|
|
|
|
|
|
|
note.setLabel("sqlConsole", dateUtils.localNowDate());
|
2022-11-25 15:29:57 +01:00
|
|
|
note.setLabel('iconClass', 'bx bx-data');
|
2022-12-14 10:29:14 +01:00
|
|
|
note.setLabel('keepCurrentHoisting');
|
2021-09-16 22:09:39 +02:00
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveSqlConsole(sqlConsoleNoteId) {
|
|
|
|
const sqlConsoleNote = becca.getNote(sqlConsoleNoteId);
|
|
|
|
const today = dateUtils.localNowDate();
|
|
|
|
|
|
|
|
const sqlConsoleHome =
|
|
|
|
attributeService.getNoteWithLabel('sqlConsoleHome')
|
2022-01-10 17:09:20 +01:00
|
|
|
|| dateNoteService.getDayNote(today);
|
2021-09-16 22:09:39 +02:00
|
|
|
|
2021-10-24 14:53:45 +02:00
|
|
|
const result = sqlConsoleNote.cloneTo(sqlConsoleHome.noteId);
|
|
|
|
|
|
|
|
for (const parentBranch of sqlConsoleNote.getParentBranches()) {
|
|
|
|
if (parentBranch.parentNote.hasAncestor("hidden")) {
|
|
|
|
parentBranch.markAsDeleted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-09-16 22:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createSearchNote(searchString, ancestorNoteId) {
|
|
|
|
const {note} = noteService.createNewNote({
|
2022-12-15 16:38:05 +01:00
|
|
|
parentNoteId: getMonthlyParentNoteId('search'),
|
2021-09-16 22:09:39 +02:00
|
|
|
title: 'Search: ' + searchString,
|
|
|
|
content: "",
|
|
|
|
type: 'search',
|
|
|
|
mime: 'application/json'
|
|
|
|
});
|
|
|
|
|
|
|
|
note.setLabel('searchString', searchString);
|
2022-12-14 10:29:14 +01:00
|
|
|
note.setLabel('keepCurrentHoisting');
|
2021-09-16 22:09:39 +02:00
|
|
|
|
|
|
|
if (ancestorNoteId) {
|
|
|
|
note.setRelation('ancestor', ancestorNoteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2021-10-24 14:53:45 +02:00
|
|
|
function getSearchHome() {
|
2021-09-16 22:09:39 +02:00
|
|
|
const hoistedNote = getHoistedNote();
|
|
|
|
|
|
|
|
if (!hoistedNote.isRoot()) {
|
2021-10-24 14:53:45 +02:00
|
|
|
return hoistedNote.searchNoteInSubtree('#hoistedSearchHome')
|
2021-09-16 22:09:39 +02:00
|
|
|
|| hoistedNote.searchNoteInSubtree('#searchHome')
|
|
|
|
|| hoistedNote;
|
2021-10-24 14:53:45 +02:00
|
|
|
} else {
|
2021-09-16 22:09:39 +02:00
|
|
|
const today = dateUtils.localNowDate();
|
|
|
|
|
2021-10-24 14:53:45 +02:00
|
|
|
return hoistedNote.searchNoteInSubtree('#searchHome')
|
2022-01-10 17:09:20 +01:00
|
|
|
|| dateNoteService.getDayNote(today);
|
2021-09-16 22:09:39 +02:00
|
|
|
}
|
2021-10-24 14:53:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveSearchNote(searchNoteId) {
|
|
|
|
const searchNote = becca.getNote(searchNoteId);
|
|
|
|
const searchHome = getSearchHome();
|
|
|
|
|
|
|
|
const result = searchNote.cloneTo(searchHome.noteId);
|
|
|
|
|
|
|
|
for (const parentBranch of searchNote.getParentBranches()) {
|
|
|
|
if (parentBranch.parentNote.hasAncestor("hidden")) {
|
|
|
|
parentBranch.markAsDeleted();
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 22:09:39 +02:00
|
|
|
|
2021-10-24 14:53:45 +02:00
|
|
|
return result;
|
2021-09-16 22:09:39 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 16:38:05 +01:00
|
|
|
function getMonthlyParentNoteId(rootNoteId) {
|
|
|
|
const month = dateUtils.localNowDate().substring(0, 7);
|
|
|
|
const labelName = `${rootNoteId}MonthNote`;
|
|
|
|
|
|
|
|
let monthNote = searchService.findFirstNoteWithQuery(`#${labelName}="${month}"`,
|
|
|
|
new SearchContext({ancestorNoteId: rootNoteId}));
|
|
|
|
|
|
|
|
if (!monthNote) {
|
|
|
|
monthNote = noteService.createNewNote({
|
|
|
|
parentNoteId: rootNoteId,
|
|
|
|
title: month,
|
|
|
|
content: '',
|
|
|
|
isProtected: false,
|
|
|
|
type: 'book'
|
|
|
|
}).note
|
|
|
|
|
|
|
|
monthNote.addLabel(labelName, month);
|
|
|
|
}
|
|
|
|
|
|
|
|
return monthNote.noteId;
|
|
|
|
}
|
|
|
|
|
2021-09-16 22:20:59 +02:00
|
|
|
function getHoistedNote() {
|
|
|
|
return becca.getNote(cls.getHoistedNoteId());
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:16:57 +01:00
|
|
|
function createLauncher(parentNoteId, launcherType) {
|
2022-08-07 15:34:59 +02:00
|
|
|
let note;
|
|
|
|
|
2022-12-01 10:16:57 +01:00
|
|
|
if (launcherType === 'note') {
|
2022-08-07 15:34:59 +02:00
|
|
|
note = noteService.createNewNote({
|
2022-12-07 12:46:41 +01:00
|
|
|
title: "Note Launcher",
|
2022-12-01 10:16:57 +01:00
|
|
|
type: 'launcher',
|
2022-08-07 15:34:59 +02:00
|
|
|
content: '',
|
|
|
|
parentNoteId: parentNoteId
|
|
|
|
}).note;
|
2022-11-29 16:16:57 +01:00
|
|
|
|
2022-12-07 12:46:41 +01:00
|
|
|
note.addRelation('template', 'lbTplNoteLauncher');
|
2022-12-01 10:16:57 +01:00
|
|
|
} else if (launcherType === 'script') {
|
2022-08-08 23:13:31 +02:00
|
|
|
note = noteService.createNewNote({
|
2022-12-07 12:46:41 +01:00
|
|
|
title: "Script Launcher",
|
2022-12-01 10:16:57 +01:00
|
|
|
type: 'launcher',
|
2022-08-08 23:13:31 +02:00
|
|
|
content: '',
|
|
|
|
parentNoteId: parentNoteId
|
|
|
|
}).note;
|
|
|
|
|
2022-12-07 12:46:41 +01:00
|
|
|
note.addRelation('template', 'lbTplScriptLauncher');
|
2022-12-01 10:16:57 +01:00
|
|
|
} else if (launcherType === 'customWidget') {
|
2022-08-07 15:34:59 +02:00
|
|
|
note = noteService.createNewNote({
|
2022-12-07 12:46:41 +01:00
|
|
|
title: "Widget Launcher",
|
2022-12-01 10:16:57 +01:00
|
|
|
type: 'launcher',
|
2022-08-07 15:34:59 +02:00
|
|
|
content: '',
|
|
|
|
parentNoteId: parentNoteId
|
|
|
|
}).note;
|
|
|
|
|
2022-12-07 12:46:41 +01:00
|
|
|
note.addRelation('template', 'lbTplCustomWidget');
|
2022-12-01 10:16:57 +01:00
|
|
|
} else if (launcherType === 'spacer') {
|
2022-08-07 15:34:59 +02:00
|
|
|
note = noteService.createNewNote({
|
|
|
|
title: "Spacer",
|
2022-12-01 10:16:57 +01:00
|
|
|
type: 'launcher',
|
2022-08-07 15:34:59 +02:00
|
|
|
content: '',
|
|
|
|
parentNoteId: parentNoteId
|
|
|
|
}).note;
|
2022-08-07 13:23:03 +02:00
|
|
|
|
2022-12-07 12:46:41 +01:00
|
|
|
note.addRelation('template', 'lbTplSpacer');
|
2022-08-07 15:34:59 +02:00
|
|
|
} else {
|
2022-12-07 12:46:41 +01:00
|
|
|
throw new Error(`Unrecognized launcher type '${launcherType}'`);
|
2022-08-07 13:23:03 +02:00
|
|
|
}
|
2022-08-07 15:34:59 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
note
|
|
|
|
};
|
2022-08-07 13:23:03 +02:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:16:57 +01:00
|
|
|
function resetLauncher(noteId) {
|
2022-12-06 23:48:44 +01:00
|
|
|
const note = becca.getNote(noteId);
|
2022-11-25 15:29:57 +01:00
|
|
|
|
2022-12-06 23:48:44 +01:00
|
|
|
if (note.isLauncherConfig()) {
|
2022-11-25 15:29:57 +01:00
|
|
|
if (note) {
|
2022-12-06 23:48:44 +01:00
|
|
|
if (noteId === 'lbRoot') {
|
2022-11-25 15:29:57 +01:00
|
|
|
// deleting hoisted notes are not allowed, so we just reset the children
|
|
|
|
for (const childNote of note.getChildNotes()) {
|
|
|
|
childNote.deleteNote();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
note.deleteNote();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.info(`Note ${noteId} has not been found and cannot be reset.`);
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-01 10:16:57 +01:00
|
|
|
log.info(`Note ${noteId} is not a resettable launcher note.`);
|
2022-11-25 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
2022-12-07 12:46:41 +01:00
|
|
|
hiddenSubtreeService.checkHiddenSubtree();
|
2022-11-25 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:09:39 +02:00
|
|
|
module.exports = {
|
|
|
|
getInboxNote,
|
|
|
|
createSqlConsole,
|
|
|
|
saveSqlConsole,
|
|
|
|
createSearchNote,
|
2021-09-16 22:20:59 +02:00
|
|
|
saveSearchNote,
|
2022-12-01 10:16:57 +01:00
|
|
|
createLauncher,
|
|
|
|
resetLauncher
|
2021-09-16 22:09:39 +02:00
|
|
|
};
|