From 8e0469056896d0797d5ad446857c2f833701d140 Mon Sep 17 00:00:00 2001 From: Geekswordsman Date: Thu, 7 Aug 2025 15:33:43 -0400 Subject: [PATCH 1/2] Adds duplicateSubtree to backend API. --- apps/server/src/services/backend_script_api.ts | 8 ++++++++ apps/server/src/services/notes.ts | 9 +++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/server/src/services/backend_script_api.ts b/apps/server/src/services/backend_script_api.ts index 249d09ea0..6513245c4 100644 --- a/apps/server/src/services/backend_script_api.ts +++ b/apps/server/src/services/backend_script_api.ts @@ -412,6 +412,13 @@ export interface Api { */ backupNow(backupName: string): Promise; + /** + * Exposes the backend duplicateSubtree to the API + * @param origNoteId - the noteId for the original note to be duplicated + * @param newParentNoteId - the noteId for the parent note where the duplication is to be placed. + */ + duplicateSubtree(origNoteId: string, newParentNoteId: string): { note: BNote; branch: BBranch; } + /** * This object contains "at your risk" and "no BC guarantees" objects for advanced use cases. */ @@ -703,6 +710,7 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) { this.runOutsideOfSync = syncMutex.doExclusively; this.backupNow = backupService.backupNow; + this.duplicateSubtree = noteService.duplicateSubtree; this.__private = { becca diff --git a/apps/server/src/services/notes.ts b/apps/server/src/services/notes.ts index 998da821b..1f91b4db0 100644 --- a/apps/server/src/services/notes.ts +++ b/apps/server/src/services/notes.ts @@ -931,9 +931,10 @@ function duplicateSubtree(origNoteId: string, newParentNoteId: string) { const noteIdMapping = getNoteIdMapping(origNote); - if (!origBranch) { - throw new Error("Unable to find original branch to duplicate."); - } + // Not required - if origBranch doesn't exist, the subtree can still be created. + // if (!origBranch) { + // throw new Error("Unable to find original branch to duplicate."); + // } const res = duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping); @@ -966,7 +967,7 @@ function duplicateSubtreeWithoutRoot(origNoteId: string, newNoteId: string) { } } -function duplicateSubtreeInner(origNote: BNote, origBranch: BBranch, newParentNoteId: string, noteIdMapping: Record) { +function duplicateSubtreeInner(origNote: BNote, origBranch: BBranch | null | undefined, newParentNoteId: string, noteIdMapping: Record) { if (origNote.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { throw new Error(`Cannot duplicate note '${origNote.noteId}' because it is protected and protected session is not available. Enter protected session and try again.`); } From 911fee0213ebf2091de2a8a578ec69b1345c89fb Mon Sep 17 00:00:00 2001 From: Geekswordsman Date: Thu, 7 Aug 2025 16:54:21 -0400 Subject: [PATCH 2/2] Updated documentation for the duplicateSubtree, and removed commented out code per request. --- apps/server/src/services/backend_script_api.ts | 6 +++++- apps/server/src/services/notes.ts | 5 ----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/apps/server/src/services/backend_script_api.ts b/apps/server/src/services/backend_script_api.ts index 6513245c4..60af0fb97 100644 --- a/apps/server/src/services/backend_script_api.ts +++ b/apps/server/src/services/backend_script_api.ts @@ -413,9 +413,13 @@ export interface Api { backupNow(backupName: string): Promise; /** - * Exposes the backend duplicateSubtree to the API + * Enables the complete duplication of the specified original note and all its children into the specified parent note. + * The new note will be named the same as the original, with (Dup) added to the end of it. + * * @param origNoteId - the noteId for the original note to be duplicated * @param newParentNoteId - the noteId for the parent note where the duplication is to be placed. + * + * @returns the note and the branch of the newly created note. */ duplicateSubtree(origNoteId: string, newParentNoteId: string): { note: BNote; branch: BBranch; } diff --git a/apps/server/src/services/notes.ts b/apps/server/src/services/notes.ts index 1f91b4db0..e225cdb52 100644 --- a/apps/server/src/services/notes.ts +++ b/apps/server/src/services/notes.ts @@ -931,11 +931,6 @@ function duplicateSubtree(origNoteId: string, newParentNoteId: string) { const noteIdMapping = getNoteIdMapping(origNote); - // Not required - if origBranch doesn't exist, the subtree can still be created. - // if (!origBranch) { - // throw new Error("Unable to find original branch to duplicate."); - // } - const res = duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping); const duplicateNoteSuffix = t("notes.duplicate-note-suffix");