diff --git a/apps/client/src/widgets/view_widgets/table_view/context_menu.ts b/apps/client/src/widgets/view_widgets/table_view/context_menu.ts index a9cf68be1..af463027b 100644 --- a/apps/client/src/widgets/view_widgets/table_view/context_menu.ts +++ b/apps/client/src/widgets/view_widgets/table_view/context_menu.ts @@ -22,7 +22,17 @@ export function showRowContextMenu(_e: UIEvent, row: RowComponent, parentNote: F title: "Insert row above", uiIcon: "bx bx-list-plus", handler: () => { - + const target = e.target; + if (!target) { + return; + } + const component = $(target).closest(".component").prop("component"); + component.triggerCommand("addNewRow", { + customOpts: { + target: "before", + targetBranchId: rowData.branchId, + } + }); } }, { diff --git a/apps/server/src/routes/api/notes.ts b/apps/server/src/routes/api/notes.ts index 40a955be9..b53bb1a2a 100644 --- a/apps/server/src/routes/api/notes.ts +++ b/apps/server/src/routes/api/notes.ts @@ -110,7 +110,7 @@ function createNote(req: Request) { const { target, targetBranchId } = req.query; - if (target !== "into" && target !== "after") { + if (target !== "into" && target !== "after" && target !== "before") { throw new ValidationError("Invalid target type."); } diff --git a/apps/server/src/services/notes.ts b/apps/server/src/services/notes.ts index 4c82c0e8c..998da821b 100644 --- a/apps/server/src/services/notes.ts +++ b/apps/server/src/services/notes.ts @@ -254,7 +254,7 @@ function createNewNote(params: NoteParams): { }); } -function createNewNoteWithTarget(target: "into" | "after", targetBranchId: string | undefined, params: NoteParams) { +function createNewNoteWithTarget(target: "into" | "after" | "before", targetBranchId: string | undefined, params: NoteParams) { if (!params.type) { const parentNote = becca.notes[params.parentNoteId]; @@ -277,6 +277,19 @@ function createNewNoteWithTarget(target: "into" | "after", targetBranchId: strin entityChangesService.putNoteReorderingEntityChange(params.parentNoteId); + return retObject; + } else if (target === "before" && targetBranchId) { + const beforeBranch = becca.branches[targetBranchId]; + + // not updating utcDateModified to avoid having to sync whole rows + sql.execute("UPDATE branches SET notePosition = notePosition - 10 WHERE parentNoteId = ? AND notePosition < ? AND isDeleted = 0", [params.parentNoteId, beforeBranch.notePosition]); + + params.notePosition = beforeBranch.notePosition - 10; + + const retObject = createNewNote(params); + + entityChangesService.putNoteReorderingEntityChange(params.parentNoteId); + return retObject; } else { throw new Error(`Unknown target '${target}'`);