diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts index 7bc544e7e..b8a6fc18f 100644 --- a/apps/client/src/components/app_context.ts +++ b/apps/client/src/components/app_context.ts @@ -28,7 +28,7 @@ import TouchBarComponent from "./touch_bar.js"; import type { CKTextEditor } from "@triliumnext/ckeditor5"; import type CodeMirror from "@triliumnext/codemirror"; import { StartupChecks } from "./startup_checks.js"; -import type { CreateNoteOpts } from "../services/note_create.js"; +import type { CreateNoteOpts, CreateNoteWithUrlOpts } from "../services/note_create.js"; import { ColumnComponent } from "tabulator-tables"; import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx"; import type RootContainer from "../widgets/containers/root_container.js"; @@ -357,8 +357,7 @@ export type CommandMappings = { // Table view addNewRow: CommandData & { - customOpts: CreateNoteOpts; - parentNotePath?: string; + customOpts?: CreateNoteWithUrlOpts; }; addNewTableColumn: CommandData & { columnToEdit?: ColumnComponent; diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.ts index 75e1f2fd7..bd4d80dab 100644 --- a/apps/client/src/services/note_create.ts +++ b/apps/client/src/services/note_create.ts @@ -83,15 +83,18 @@ type CreateNoteBase = { * Serves as a base for "into", "before", and "after" variants, * sharing common URL-related fields. */ -export type CreateNoteWithUrlOpts = CreateNoteBase & { - target: "into" | "after" | "before"; - // `Url` may refer to either parentNotePath or parentNoteId. - // The vocabulary is inspired by existing function getNoteIdFromUrl. - parentNoteUrl: string; - - // Disambiguates the position for cloned notes. - targetBranchId?: string; -}; +export type CreateNoteWithUrlOpts = + | (CreateNoteBase & { + target: "into"; + parentNoteUrl?: string; + // No branch ID needed for "into" + }) + | (CreateNoteBase & { + target: "before" | "after"; + parentNoteUrl?: string; + // Required for "before"/"after" + targetBranchId: string; + }); export type CreateNoteIntoInboxOpts = CreateNoteBase & { target: "inbox"; @@ -134,7 +137,7 @@ async function createNote( case "into": case "before": case "after": - return createNoteWithUrl(resolvedOptions.target, resolvedOptions); + return createNoteWithUrl(resolvedOptions); } } @@ -173,7 +176,6 @@ async function promptForType( * @returns A promise resolving with the created note and its branch. */ async function createNoteWithUrl( - target: "into" | "after" | "before", options: CreateNoteWithUrlOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }> { options = Object.assign( @@ -210,7 +212,12 @@ async function createNoteWithUrl( C-->D;`; } - const { note, branch } = await server.post(`notes/${parentNoteId}/children?target=${target}&targetBranchId=${options.targetBranchId || ""}`, { + const query = + options.target === "into" + ? `target=${options.target}` + : `target=${options.target}&targetBranchId=${options.targetBranchId}`; + + const { note, branch } = await server.post(`notes/${parentNoteId}/children?${query}`, { title: options.title, content: options.content || "", isProtected: options.isProtected, @@ -269,7 +276,7 @@ async function createNoteIntoInbox( inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable(); } - const result = await createNoteWithUrl("into", + const result = await createNoteWithUrl( { ...options, target: "into", diff --git a/apps/client/src/widgets/collections/table/context_menu.ts b/apps/client/src/widgets/collections/table/context_menu.ts index 9d3e5418f..312dea3c3 100644 --- a/apps/client/src/widgets/collections/table/context_menu.ts +++ b/apps/client/src/widgets/collections/table/context_menu.ts @@ -181,7 +181,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro uiIcon: "bx bx-horizontal-left bx-rotate-90", enabled: !sorters.length, handler: () => parentComponent?.triggerCommand("addNewRow", { - parentNotePath: parentNoteId, customOpts: { parentNoteUrl: parentNoteId, target: "before", @@ -199,7 +198,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro return; } parentComponent?.triggerCommand("addNewRow", { - parentNotePath: note.noteId, customOpts: { parentNoteUrl: note.noteId, target: "after", @@ -213,7 +211,6 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro uiIcon: "bx bx-horizontal-left bx-rotate-270", enabled: !sorters.length, handler: () => parentComponent?.triggerCommand("addNewRow", { - parentNotePath: parentNoteId, customOpts: { parentNoteUrl: parentNoteId, target: "after", diff --git a/apps/client/src/widgets/collections/table/row_editing.ts b/apps/client/src/widgets/collections/table/row_editing.ts index c948e9e1e..258355580 100644 --- a/apps/client/src/widgets/collections/table/row_editing.ts +++ b/apps/client/src/widgets/collections/table/row_editing.ts @@ -9,29 +9,20 @@ import server from "../../../services/server"; import branches from "../../../services/branches"; import AttributeDetailWidget from "../../attribute_widgets/attribute_detail"; -export default function useRowTableEditing(api: RefObject, attributeDetailWidget: AttributeDetailWidget, parentNotePath: string): Partial { - // Adding new rows +export default function useRowTableEditing(api: RefObject, attributeDetailWidget: AttributeDetailWidget, parentNotePath: string): Partial { // Adding new rows useLegacyImperativeHandlers({ - addNewRowCommand({ customOpts, parentNotePath: customNotePath }: CommandListenerData<"addNewRow">) { - const notePath = customNotePath ?? parentNotePath; - if (notePath) { - const opts: CreateNoteOpts = { - activate: false, - ...customOpts - } + addNewRowCommand({ customOpts }: CommandListenerData<"addNewRow">) { + if (!customOpts) { + customOpts = { + target: "into", + }; + } - // Normalize "inbox" targets into standard path-based creation. - // When adding a new row, we always have a concrete parent path (`notePath`), - // so even if the originating command requested an "inbox" creation, - // it should instead behave as "into" under the current note. - const normalizedOpts: CreateNoteWithUrlOpts = - opts.target === "inbox" - ? { ...opts, target: "into", parentNoteUrl: notePath } - : { ...opts, parentNoteUrl: notePath }; - - note_create.createNote( - normalizedOpts - ).then(({ branch }) => { + const noteUrl = customOpts.parentNoteUrl ?? parentNotePath; + if (noteUrl) { + customOpts.parentNoteUrl = noteUrl; + customOpts.activate = false; + note_create.createNote(customOpts).then(({ branch }) => { if (branch) { setTimeout(() => { if (!api.current) return; @@ -39,6 +30,7 @@ export default function useRowTableEditing(api: RefObject, attributeD }, 100); } }) + } } });