Merge pull request #3893 from mirrorcult/2023-05-01/ensure-note-return-branch

Return created/existing branch in `EnsureNoteIsPresentInParent`
This commit is contained in:
zadam 2023-05-02 21:12:43 +02:00 committed by GitHub
commit c9aab9b479
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 9 deletions

View File

@ -137,13 +137,13 @@ function BackendScriptApi(currentNote, apiParams) {
this.getNoteWithLabel = attributeService.getNoteWithLabel; this.getNoteWithLabel = attributeService.getNoteWithLabel;
/** /**
* If there's no branch between note and parent note, create one. Otherwise, do nothing. * If there's no branch between note and parent note, create one. Otherwise, do nothing. Returns the new or existing branch.
* *
* @method * @method
* @param {string} noteId * @param {string} noteId
* @param {string} parentNoteId * @param {string} parentNoteId
* @param {string} prefix - if branch will be created between note and parent note, set this prefix * @param {string} prefix - if branch will be created between note and parent note, set this prefix
* @returns {void} * @returns {{branch: BBranch|null}}
*/ */
this.ensureNoteIsPresentInParent = cloningService.ensureNoteIsPresentInParent; this.ensureNoteIsPresentInParent = cloningService.ensureNoteIsPresentInParent;

View File

@ -61,15 +61,15 @@ function cloneNoteToBranch(noteId, parentBranchId, prefix) {
function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) { function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {
if (isNoteDeleted(noteId)) { if (isNoteDeleted(noteId)) {
return { success: false, message: `Note '${noteId}' is deleted.` }; return { branch: null, success: false, message: `Note '${noteId}' is deleted.` };
} else if (isNoteDeleted(parentNoteId)) { } else if (isNoteDeleted(parentNoteId)) {
return { success: false, message: `Note '${parentNoteId}' is deleted.` }; return { branch: null, success: false, message: `Note '${parentNoteId}' is deleted.` };
} }
const parentNote = becca.getNote(parentNoteId); const parentNote = becca.getNote(parentNoteId);
if (parentNote.type === 'search') { if (parentNote.type === 'search') {
return { success: false, message: "Can't clone into a search note" }; return { branch: null, success: false, message: "Can't clone into a search note" };
} }
const validationResult = treeService.validateParentChild(parentNoteId, noteId); const validationResult = treeService.validateParentChild(parentNoteId, noteId);
@ -87,7 +87,7 @@ function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {
log.info(`Ensured note '${noteId}' is in parent note '${parentNoteId}' with prefix '${branch.prefix}'`); log.info(`Ensured note '${noteId}' is in parent note '${parentNoteId}' with prefix '${branch.prefix}'`);
return { success: true }; return { branch: branch, success: true };
} }
function ensureNoteIsAbsentFromParent(noteId, parentNoteId) { function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {

View File

@ -31,12 +31,12 @@ function getNotes(noteIds) {
function validateParentChild(parentNoteId, childNoteId, branchId = null) { function validateParentChild(parentNoteId, childNoteId, branchId = null) {
if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) { if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) {
return { success: false, message: `Cannot change this note's location.`}; return { branch: null, success: false, message: `Cannot change this note's location.`};
} }
if (parentNoteId === 'none') { if (parentNoteId === 'none') {
// this shouldn't happen // this shouldn't happen
return { success: false, message: `Cannot move anything into 'none' parent.` }; return { branch: null, success: false, message: `Cannot move anything into 'none' parent.` };
} }
const existing = getExistingBranch(parentNoteId, childNoteId); const existing = getExistingBranch(parentNoteId, childNoteId);
@ -46,6 +46,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
const childNote = becca.getNote(childNoteId); const childNote = becca.getNote(childNoteId);
return { return {
branch: existing,
success: false, success: false,
message: `Note "${childNote.title}" note already exists in the "${parentNote.title}".` message: `Note "${childNote.title}" note already exists in the "${parentNote.title}".`
}; };
@ -53,6 +54,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
if (!checkTreeCycle(parentNoteId, childNoteId)) { if (!checkTreeCycle(parentNoteId, childNoteId)) {
return { return {
branch: null,
success: false, success: false,
message: 'Moving/cloning note here would create cycle.' message: 'Moving/cloning note here would create cycle.'
}; };
@ -60,12 +62,13 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
if (parentNoteId !== '_lbBookmarks' && becca.getNote(parentNoteId).type === 'launcher') { if (parentNoteId !== '_lbBookmarks' && becca.getNote(parentNoteId).type === 'launcher') {
return { return {
branch: null,
success: false, success: false,
message: 'Launcher note cannot have any children.' message: 'Launcher note cannot have any children.'
}; };
} }
return { success: true }; return { branch: null, success: true };
} }
function getExistingBranch(parentNoteId, childNoteId) { function getExistingBranch(parentNoteId, childNoteId) {