Notes/src/services/special_notes.js

346 lines
9.2 KiB
JavaScript
Raw Normal View History

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");
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);
}
return inbox;
}
function getHiddenRoot() {
let hidden = becca.getNote('hidden');
if (!hidden) {
hidden = noteService.createNewNote({
2021-12-21 13:22:13 +01:00
branchId: 'hidden',
noteId: 'hidden',
title: 'hidden',
type: 'text',
content: '',
parentNoteId: 'root'
}).note;
// isInheritable: false means that this notePath is automatically not preffered but at the same time
// the flag is not inherited to the children
hidden.addLabel('archived', "", false);
hidden.addLabel('excludeFromNoteMap', "", true);
}
return hidden;
}
function getSearchRoot() {
let searchRoot = becca.getNote('search');
if (!searchRoot) {
searchRoot = noteService.createNewNote({
noteId: 'search',
title: 'search',
type: 'text',
content: '',
parentNoteId: getHiddenRoot().noteId
}).note;
}
return searchRoot;
}
function getSinglesNoteRoot() {
let singlesNoteRoot = becca.getNote('singles');
if (!singlesNoteRoot) {
singlesNoteRoot = noteService.createNewNote({
noteId: 'singles',
title: 'singles',
type: 'text',
content: '',
parentNoteId: getHiddenRoot().noteId
}).note;
}
return singlesNoteRoot;
}
2021-09-20 23:04:41 +02:00
function getGlobalNoteMap() {
let globalNoteMap = becca.getNote('globalnotemap');
2021-09-20 23:04:41 +02:00
if (!globalNoteMap) {
globalNoteMap = noteService.createNewNote({
2021-09-20 22:19:47 +02:00
noteId: 'globalnotemap',
title: 'Global Note Map',
type: 'note-map',
content: '',
parentNoteId: getSinglesNoteRoot().noteId
}).note;
2021-09-20 23:04:41 +02:00
globalNoteMap.addLabel('mapRootNoteId', 'hoisted');
}
2021-09-20 23:04:41 +02:00
return globalNoteMap;
}
function getSqlConsoleRoot() {
let sqlConsoleRoot = becca.getNote('sqlconsole');
if (!sqlConsoleRoot) {
sqlConsoleRoot = noteService.createNewNote({
noteId: 'sqlconsole',
title: 'SQL Console',
type: 'text',
content: '',
parentNoteId: getHiddenRoot().noteId
}).note;
}
return sqlConsoleRoot;
}
function createSqlConsole() {
const {note} = noteService.createNewNote({
parentNoteId: getSqlConsoleRoot().noteId,
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());
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);
const result = sqlConsoleNote.cloneTo(sqlConsoleHome.noteId);
for (const parentBranch of sqlConsoleNote.getParentBranches()) {
if (parentBranch.parentNote.hasAncestor("hidden")) {
parentBranch.markAsDeleted();
}
}
return result;
}
function createSearchNote(searchString, ancestorNoteId) {
const {note} = noteService.createNewNote({
parentNoteId: getSearchRoot().noteId,
title: 'Search: ' + searchString,
content: "",
type: 'search',
mime: 'application/json'
});
note.setLabel('searchString', searchString);
if (ancestorNoteId) {
note.setRelation('ancestor', ancestorNoteId);
}
return note;
}
function getSearchHome() {
const hoistedNote = getHoistedNote();
if (!hoistedNote.isRoot()) {
return hoistedNote.searchNoteInSubtree('#hoistedSearchHome')
|| hoistedNote.searchNoteInSubtree('#searchHome')
|| hoistedNote;
} else {
const today = dateUtils.localNowDate();
return hoistedNote.searchNoteInSubtree('#searchHome')
2022-01-10 17:09:20 +01:00
|| dateNoteService.getDayNote(today);
}
}
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();
}
}
return result;
}
function getHoistedNote() {
return becca.getNote(cls.getHoistedNoteId());
}
2021-12-07 23:03:49 +01:00
function getShareRoot() {
let shareRoot = becca.getNote('share');
if (!shareRoot) {
shareRoot = noteService.createNewNote({
2021-12-20 17:30:47 +01:00
branchId: 'share',
2021-12-07 23:03:49 +01:00
noteId: 'share',
2021-12-20 17:30:47 +01:00
title: 'Shared notes',
2021-12-07 23:03:49 +01:00
type: 'text',
content: '',
2021-12-20 17:30:47 +01:00
parentNoteId: 'root'
2021-12-07 23:03:49 +01:00
}).note;
}
return shareRoot;
}
2022-06-03 17:29:08 +02:00
function getBulkActionNote() {
let bulkActionNote = becca.getNote('bulkaction');
if (!bulkActionNote) {
bulkActionNote = noteService.createNewNote({
branchId: 'bulkaction',
noteId: 'bulkaction',
title: 'Bulk action',
type: 'text',
content: '',
parentNoteId: getHiddenRoot().noteId
}).note;
}
return bulkActionNote;
}
2022-08-02 17:01:09 +02:00
function getLaunchBarRoot() {
2022-08-04 23:00:32 +02:00
let note = becca.getNote('lb_root');
2022-08-02 17:01:09 +02:00
if (!note) {
note = noteService.createNewNote({
2022-08-04 23:00:32 +02:00
branchId: 'lb_root',
noteId: 'lb_root',
2022-08-02 17:01:09 +02:00
title: 'Launch bar',
type: 'text',
content: '',
parentNoteId: getHiddenRoot().noteId
}).note;
}
return note;
}
function getLaunchBarAvailableShortcutsRoot() {
let note = becca.getNote('lb_availableshortcuts');
if (!note) {
note = noteService.createNewNote({
branchId: 'lb_availableshortcuts',
noteId: 'lb_availableshortcuts',
title: 'Available shortcuts',
type: 'text',
content: '',
parentNoteId: getLaunchBarRoot().noteId
}).note;
}
return note;
}
function getLaunchBarVisibleShortcutsRoot() {
let note = becca.getNote('lb_visibleshortcuts');
if (!note) {
note = noteService.createNewNote({
branchId: 'lb_visibleshortcuts',
noteId: 'lb_visibleshortcuts',
title: 'Visible shortcuts',
type: 'text',
content: '',
parentNoteId: getLaunchBarRoot().noteId
}).note;
}
return note;
}
const shortcuts = [
2022-08-04 23:00:32 +02:00
{ id: 'lb_newnote', command: 'createNoteIntoInbox', title: 'New note', icon: 'bx bx-file-blank', isVisible: true },
{ id: 'lb_search', command: 'searchNotes', title: 'Search notes', icon: 'bx bx-search', isVisible: true },
{ id: 'lb_jumpto', command: 'jumpToNote', title: 'Jump to note', icon: 'bx bx-send', isVisible: true },
{ id: 'lb_notemap', targetNote: 'globalnotemap', title: 'Note map', icon: 'bx bx-map-alt', isVisible: true },
{ id: 'lb_recentchanges', command: 'showRecentChanges', title: 'Show recent changes', icon: 'bx bx-history', isVisible: false }
2022-08-02 17:01:09 +02:00
];
function createMissingSpecialNotes() {
getSinglesNoteRoot();
getSqlConsoleRoot();
2021-09-20 23:04:41 +02:00
getGlobalNoteMap();
2022-06-03 17:29:08 +02:00
getBulkActionNote();
2022-08-02 17:01:09 +02:00
getLaunchBarRoot();
getLaunchBarAvailableShortcutsRoot();
2022-08-04 23:00:32 +02:00
getLaunchBarVisibleShortcutsRoot()
for (const shortcut of shortcuts) {
let note = becca.getNote(shortcut.id);
const parentNoteId = shortcut.isVisible ? getLaunchBarVisibleShortcutsRoot().noteId : getLaunchBarAvailableShortcutsRoot().noteId;
if (!note) {
note = noteService.createNewNote({
branchId: shortcut.id,
noteId: shortcut.id,
title: shortcut.title,
type: 'text',
content: '',
parentNoteId: parentNoteId
}).note;
note.addLabel('builtinShortcut');
note.addLabel('iconClass', shortcut.icon);
note.addLabel('command', shortcut.command);
}
}
2022-08-02 17:01:09 +02:00
2021-12-20 17:30:47 +01:00
// share root is not automatically created since it's visible in the tree and many won't need it/use it
const hidden = getHiddenRoot();
if (!hidden.hasOwnedLabel('excludeFromNoteMap')) {
hidden.addLabel('excludeFromNoteMap', "", true);
}
}
module.exports = {
getInboxNote,
createSqlConsole,
saveSqlConsole,
createSearchNote,
saveSearchNote,
2021-12-20 17:30:47 +01:00
createMissingSpecialNotes,
2022-06-03 17:29:08 +02:00
getShareRoot,
getBulkActionNote,
};