shortcuts improvements

This commit is contained in:
zadam
2022-11-27 23:43:25 +01:00
parent e4f57ab2fe
commit a459230aa9
10 changed files with 168 additions and 174 deletions

View File

@@ -103,6 +103,28 @@ function getNewNoteTitle(parentNote) {
return title;
}
function getAndValidateParent(params) {
const parentNote = becca.notes[params.parentNoteId];
if (!parentNote) {
throw new Error(`Parent note "${params.parentNoteId}" not found.`);
}
if (parentNote.type === 'shortcut') {
throw new Error(`Shortcuts should not have child notes.`);
}
if (['hidden', 'lb_root'].includes(parentNote.noteId)) {
throw new Error(`Creating child notes into '${parentNote.noteId}' is not allowed.`);
}
if (['lb_availableshortcuts', 'lb_visibleshortcuts'].includes(parentNote.noteId) && params.type !== 'shortcut') {
throw new Error(`Creating child notes into '${parentNote.noteId}' is only possible for type 'shortcut'.`);
}
return parentNote;
}
/**
* Following object properties are mandatory:
* - {string} parentNoteId
@@ -121,11 +143,7 @@ function getNewNoteTitle(parentNote) {
* @return {{note: Note, branch: Branch}}
*/
function createNewNote(params) {
const parentNote = becca.notes[params.parentNoteId];
if (!parentNote) {
throw new Error(`Parent note "${params.parentNoteId}" not found.`);
}
const parentNote = getAndValidateParent(params);
if (params.title === null || params.title === undefined) {
params.title = getNewNoteTitle(parentNote);